You are here

trait FontOperationTrait in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/FontOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\FontOperationTrait
  2. 8.2 src/Plugin/ImageToolkit/Operation/FontOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\FontOperationTrait

Base trait for image toolkit operations that require font handling.

Hierarchy

2 files declare their use of FontOperationTrait
TextOverlay.php in src/Plugin/ImageToolkit/Operation/gd/TextOverlay.php
TextToWrapper.php in src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php

File

src/Plugin/ImageToolkit/Operation/FontOperationTrait.php, line 10

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation
View source
trait FontOperationTrait {

  /**
   * The stream wrapper manager service.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManagerForFontHandling;

  /**
   * An array of resolved font file URIs.
   *
   * @var array
   */
  protected static $fontPaths = [];

  /**
   * Return the real path of the specified file.
   *
   * @param string $uri
   *   An URI.
   *
   * @return string
   *   The local path of the file.
   */
  protected function getRealFontPath($uri) {
    $uri_wrapper = $this
      ->getStreamWrapperManagerForFontHandling()
      ->getViaUri($uri);
    if ($uri_wrapper instanceof LocalStream) {
      return $uri_wrapper
        ->realpath();
    }
    else {
      return is_file($uri) ? $uri : NULL;
    }
  }

  /**
   * Return the path of the font file.
   *
   * @param string $font_uri
   *   The font URI.
   *
   * @return string
   *   The local path of the font file.
   */
  protected function getFontPath($font_uri) {
    if (!$font_uri) {
      throw new \InvalidArgumentException('Font file not specified');
    }
    if (!isset(static::$fontPaths[$font_uri])) {
      if (!($ret = $this
        ->getRealFontPath($font_uri))) {
        throw new \InvalidArgumentException("Could not find the font file {$font_uri}");
      }
      static::$fontPaths[$font_uri] = $ret;
    }
    return static::$fontPaths[$font_uri];
  }

  /**
   * Returns the stream wrapper manager service.
   *
   * @return \Drupal\Core\StreamWrapper\streamWrapperManagerInterface
   *   The stream wrapper manager service.
   */
  protected function getStreamWrapperManagerForFontHandling() {
    if (!$this->streamWrapperManagerForFontHandling) {
      $this->streamWrapperManagerForFontHandling = \Drupal::service('stream_wrapper_manager');
    }
    return $this->streamWrapperManagerForFontHandling;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FontOperationTrait::$fontPaths protected static property An array of resolved font file URIs.
FontOperationTrait::$streamWrapperManagerForFontHandling protected property The stream wrapper manager service.
FontOperationTrait::getFontPath protected function Return the path of the font file.
FontOperationTrait::getRealFontPath protected function Return the real path of the specified file.
FontOperationTrait::getStreamWrapperManagerForFontHandling protected function Returns the stream wrapper manager service.