Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / file_mdm_font / src / Plugin / FileMetadata / Font.php
1 <?php
2
3 namespace Drupal\file_mdm_font\Plugin\FileMetadata;
4
5 use Drupal\file_mdm\FileMetadataException;
6 use Drupal\file_mdm\Plugin\FileMetadata\FileMetadataPluginBase;
7 use FontLib\Font as LibFont;
8 use FontLib\Table\Type\name;
9
10 /**
11  * FileMetadata plugin for TTF/OTF/WOFF font information.
12  *
13  * Uses the 'PHP Font Lib' library (PhenX/php-font-lib).
14  *
15  * @FileMetadata(
16  *   id = "font",
17  *   title = @Translation("Font"),
18  *   help = @Translation("File metadata plugin for TTF/OTF/WOFF font information, using the PHP Font Lib."),
19  * )
20  */
21 class Font extends FileMetadataPluginBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function getSupportedKeys($options = NULL) {
27     return array_merge(['FontType', 'FontWeight'], array_values(name::$nameIdCodes));
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function doGetMetadataFromFile() {
34     $font = LibFont::load($this->getLocalTempPath());
35     // @todo ::parse raises 'Undefined offset' notices in phenx/php-font-lib
36     // 0.5, suppress errors while upstream is fixed.
37     @$font->parse();
38     $keys = $this->getSupportedKeys();
39     $metadata = [];
40     foreach ($keys as $key) {
41       $l_key = strtolower($key);
42       switch ($l_key) {
43         case 'fonttype':
44           $metadata[$l_key] = $font->getFontType();
45           break;
46
47         case 'fontweight':
48           $metadata[$l_key] = $font->getFontWeight();
49           break;
50
51         default:
52           $code = array_search($l_key, array_map('strtolower', name::$nameIdCodes), TRUE);
53           if ($value = $font->getNameTableString($code)) {
54             $metadata[$l_key] = $value;
55           }
56           break;
57
58       }
59     }
60     return $metadata;
61   }
62
63   /**
64    * Validates a file metadata key.
65    *
66    * @return bool
67    *   TRUE if the key is valid.
68    *
69    * @throws \Drupal\file_mdm\FileMetadataException
70    *   In case the key is invalid.
71    */
72   protected function validateKey($key, $method) {
73     if (!is_string($key)) {
74       throw new FileMetadataException("Invalid metadata key specified", $this->getPluginId(), $method);
75     }
76     if (!in_array(strtolower($key), array_map('strtolower', $this->getSupportedKeys()), TRUE)) {
77       throw new FileMetadataException("Invalid metadata key '{$key}' specified", $this->getPluginId(), $method);
78     }
79     return TRUE;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   protected function doGetMetadata($key = NULL) {
86     if ($key === NULL) {
87       return $this->metadata;
88     }
89     else {
90       $this->validateKey($key, __FUNCTION__);
91       $l_key = strtolower($key);
92       if (in_array($l_key, array_map('strtolower', $this->getSupportedKeys()), TRUE)) {
93         return isset($this->metadata[$l_key]) ? $this->metadata[$l_key] : NULL;
94       }
95       return NULL;
96     }
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function doSetMetadata($key, $value) {
103     throw new FileMetadataException('Changing font metadata is not supported', $this->getPluginId());
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   protected function doRemoveMetadata($key) {
110     throw new FileMetadataException('Deleting font metadata is not supported', $this->getPluginId());
111   }
112
113 }