3 namespace Drupal\file_mdm_font\Plugin\FileMetadata;
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;
11 * FileMetadata plugin for TTF/OTF/WOFF font information.
13 * Uses the 'PHP Font Lib' library (PhenX/php-font-lib).
17 * title = @Translation("Font"),
18 * help = @Translation("File metadata plugin for TTF/OTF/WOFF font information, using the PHP Font Lib."),
21 class Font extends FileMetadataPluginBase {
26 public function getSupportedKeys($options = NULL) {
27 return array_merge(['FontType', 'FontWeight'], array_values(name::$nameIdCodes));
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.
38 $keys = $this->getSupportedKeys();
40 foreach ($keys as $key) {
41 $l_key = strtolower($key);
44 $metadata[$l_key] = $font->getFontType();
48 $metadata[$l_key] = $font->getFontWeight();
52 $code = array_search($l_key, array_map('strtolower', name::$nameIdCodes), TRUE);
53 if ($value = $font->getNameTableString($code)) {
54 $metadata[$l_key] = $value;
64 * Validates a file metadata key.
67 * TRUE if the key is valid.
69 * @throws \Drupal\file_mdm\FileMetadataException
70 * In case the key is invalid.
72 protected function validateKey($key, $method) {
73 if (!is_string($key)) {
74 throw new FileMetadataException("Invalid metadata key specified", $this->getPluginId(), $method);
76 if (!in_array(strtolower($key), array_map('strtolower', $this->getSupportedKeys()), TRUE)) {
77 throw new FileMetadataException("Invalid metadata key '{$key}' specified", $this->getPluginId(), $method);
85 protected function doGetMetadata($key = NULL) {
87 return $this->metadata;
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;
102 protected function doSetMetadata($key, $value) {
103 throw new FileMetadataException('Changing font metadata is not supported', $this->getPluginId());
109 protected function doRemoveMetadata($key) {
110 throw new FileMetadataException('Deleting font metadata is not supported', $this->getPluginId());