You are here

class Font in File metadata manager 8.2

Same name and namespace in other branches
  1. 8 file_mdm_font/src/Plugin/FileMetadata/Font.php \Drupal\file_mdm_font\Plugin\FileMetadata\Font

FileMetadata plugin for TTF/OTF/WOFF font information.

Uses the 'PHP Font Lib' library (PhenX/php-font-lib).

Plugin annotation


@FileMetadata(
  id = "font",
  title = @Translation("Font"),
  help = @Translation("File metadata plugin for TTF/OTF/WOFF font information, using the PHP Font Lib."),
)

Hierarchy

Expanded class hierarchy of Font

File

file_mdm_font/src/Plugin/FileMetadata/Font.php, line 21

Namespace

Drupal\file_mdm_font\Plugin\FileMetadata
View source
class Font extends FileMetadataPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getSupportedKeys($options = NULL) {
    return array_merge([
      'FontType',
      'FontWeight',
    ], array_values(name::$nameIdCodes));
  }

  /**
   * {@inheritdoc}
   */
  protected function doGetMetadataFromFile() {
    $font = LibFont::load($this
      ->getLocalTempPath());

    // @todo ::parse raises 'Undefined offset' notices in phenx/php-font-lib
    // 0.5, suppress errors while upstream is fixed.
    @$font
      ->parse();
    $keys = $this
      ->getSupportedKeys();
    $metadata = [];
    foreach ($keys as $key) {
      $l_key = strtolower($key);
      switch ($l_key) {
        case 'fonttype':
          $metadata[$l_key] = $font
            ->getFontType();
          break;
        case 'fontweight':
          $metadata[$l_key] = $font
            ->getFontWeight();
          break;
        default:
          $code = array_search($l_key, array_map('strtolower', name::$nameIdCodes), TRUE);
          if ($value = $font
            ->getNameTableString($code)) {
            $metadata[$l_key] = $value;
          }
          break;
      }
    }
    return $metadata;
  }

  /**
   * Validates a file metadata key.
   *
   * @return bool
   *   TRUE if the key is valid.
   *
   * @throws \Drupal\file_mdm\FileMetadataException
   *   In case the key is invalid.
   */
  protected function validateKey($key, $method) {
    if (!is_string($key)) {
      throw new FileMetadataException("Invalid metadata key specified", $this
        ->getPluginId(), $method);
    }
    if (!in_array(strtolower($key), array_map('strtolower', $this
      ->getSupportedKeys()), TRUE)) {
      throw new FileMetadataException("Invalid metadata key '{$key}' specified", $this
        ->getPluginId(), $method);
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  protected function doGetMetadata($key = NULL) {
    if ($key === NULL) {
      return $this->metadata;
    }
    else {
      $this
        ->validateKey($key, __FUNCTION__);
      $l_key = strtolower($key);
      if (in_array($l_key, array_map('strtolower', $this
        ->getSupportedKeys()), TRUE)) {
        return isset($this->metadata[$l_key]) ? $this->metadata[$l_key] : NULL;
      }
      return NULL;
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function doSetMetadata($key, $value) {
    throw new FileMetadataException('Changing font metadata is not supported', $this
      ->getPluginId());
  }

  /**
   * {@inheritdoc}
   */
  protected function doRemoveMetadata($key) {
    throw new FileMetadataException('Deleting font metadata is not supported', $this
      ->getPluginId());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FileMetadataPluginBase::$cache protected property The cache service.
FileMetadataPluginBase::$configFactory protected property The config factory.
FileMetadataPluginBase::$hash protected property The hash used to reference the URI.
FileMetadataPluginBase::$hasMetadataChangedFromCacheVersion protected property Track if file metadata on cache needs update.
FileMetadataPluginBase::$hasMetadataChangedFromFileVersion protected property Track if metadata has been changed from version on file.
FileMetadataPluginBase::$isMetadataLoaded protected property The metadata loading status.
FileMetadataPluginBase::$localTempPath protected property The local filesystem path to the file.
FileMetadataPluginBase::$metadata protected property The metadata of the file.
FileMetadataPluginBase::$streamWrapperManager protected property The stream wrapper manager service.
FileMetadataPluginBase::$uri protected property The URI of the file.
FileMetadataPluginBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
FileMetadataPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
FileMetadataPluginBase::defaultConfiguration public static function Gets default configuration for this plugin. Overrides FileMetadataPluginInterface::defaultConfiguration
FileMetadataPluginBase::deleteCachedMetadata public function Removes cached metadata for file at URI. Overrides FileMetadataPluginInterface::deleteCachedMetadata
FileMetadataPluginBase::doSaveMetadataToFile protected function Saves metadata to file at URI. 1
FileMetadataPluginBase::getConfigObject protected function Gets the configuration object for this plugin.
FileMetadataPluginBase::getLocalTempPath public function Gets the local filesystem path to the file. Overrides FileMetadataPluginInterface::getLocalTempPath
FileMetadataPluginBase::getMetadata public function Gets a metadata element. Overrides FileMetadataPluginInterface::getMetadata
FileMetadataPluginBase::getMetadataToCache protected function Gets metadata to save to cache.
FileMetadataPluginBase::getUri public function Gets the URI of the file. Overrides FileMetadataPluginInterface::getUri
FileMetadataPluginBase::isMetadataLoaded public function Checks if file metadata has been already loaded. Overrides FileMetadataPluginInterface::isMetadataLoaded
FileMetadataPluginBase::isSaveToFileSupported public function Determines if plugin is capable of writing metadata to files. Overrides FileMetadataPluginInterface::isSaveToFileSupported 1
FileMetadataPluginBase::isUriFileMetadataCacheable protected function Checks if file metadata should be cached.
FileMetadataPluginBase::loadMetadata public function Loads file metadata from an in-memory object/array. Overrides FileMetadataPluginInterface::loadMetadata
FileMetadataPluginBase::loadMetadataFromCache public function Loads file metadata from a cache entry. Overrides FileMetadataPluginInterface::loadMetadataFromCache
FileMetadataPluginBase::loadMetadataFromFile public function Loads file metadata from the file at URI/local path. Overrides FileMetadataPluginInterface::loadMetadataFromFile
FileMetadataPluginBase::removeMetadata public function Removes a metadata element. Overrides FileMetadataPluginInterface::removeMetadata
FileMetadataPluginBase::saveMetadataToCache public function Caches metadata for file at URI. Overrides FileMetadataPluginInterface::saveMetadataToCache
FileMetadataPluginBase::saveMetadataToFile public function Saves metadata to file at URI. Overrides FileMetadataPluginInterface::saveMetadataToFile
FileMetadataPluginBase::setHash public function Sets the hash used to reference the URI by the metadata manager. Overrides FileMetadataPluginInterface::setHash
FileMetadataPluginBase::setLocalTempPath public function Sets the local filesystem path to the file. Overrides FileMetadataPluginInterface::setLocalTempPath
FileMetadataPluginBase::setMetadata public function Sets a metadata element. Overrides FileMetadataPluginInterface::setMetadata
FileMetadataPluginBase::setUri public function Sets the URI of the file. Overrides FileMetadataPluginInterface::setUri
FileMetadataPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
FileMetadataPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
FileMetadataPluginBase::__construct public function Constructs a FileMetadataPluginBase plugin. Overrides PluginBase::__construct 1
Font::doGetMetadata protected function Gets a metadata element. Overrides FileMetadataPluginBase::doGetMetadata
Font::doGetMetadataFromFile protected function Gets file metadata from the file at URI/local path. Overrides FileMetadataPluginBase::doGetMetadataFromFile
Font::doRemoveMetadata protected function Removes a metadata element. Overrides FileMetadataPluginBase::doRemoveMetadata
Font::doSetMetadata protected function Sets a metadata element. Overrides FileMetadataPluginBase::doSetMetadata
Font::getSupportedKeys public function Returns a list of metadata keys supported by the plugin. Overrides FileMetadataPluginInterface::getSupportedKeys
Font::validateKey protected function Validates a file metadata key.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.