diff --git a/css/image_effects.text_overlay_preview.css b/css/image_effects.text_overlay_preview.css index 3a0ae07..b4b00ac 100644 --- a/css/image_effects.text_overlay_preview.css +++ b/css/image_effects.text_overlay_preview.css @@ -8,7 +8,7 @@ text-align: center; } -#text-overlay-preview IMG { +#text-overlay-preview img { margin: 0; padding: 0; } diff --git a/src/Component/TextUtility.php b/src/Component/TextUtility.php index 38cb02a..32fca4b 100644 --- a/src/Component/TextUtility.php +++ b/src/Component/TextUtility.php @@ -55,7 +55,8 @@ EOD; */ public static function unicodePregMatch($pattern, $subject, &$matches, $flags = NULL, $offset = 0) { // Convert the offset value from characters to bytes. - // NOTE - strlen is used on purpose here, instead of Unicode::strlen + // NOTE - strlen is used on purpose here to get string length in bytes. + // @see https://www.drupal.org/node/465638#comment-1600860 $offset = strlen(Unicode::substr($subject, 0, $offset)); $return_value = preg_match($pattern, $subject, $matches, $flags, $offset); @@ -64,7 +65,8 @@ EOD; foreach ($matches as &$match) { // Convert the offset returned by preg_match from bytes back to // characters. - // NOTE - substr is used on purpose here, instead of Unicode::substr + // NOTE - substr is used on purpose here to get offset in bytes. + // @see https://www.drupal.org/node/465638#comment-1600860 $match[1] = Unicode::strlen(substr($subject, 0, $match[1])); } } diff --git a/src/Plugin/ImageEffect/TextOverlayImageEffect.php b/src/Plugin/ImageEffect/TextOverlayImageEffect.php index d764969..0f47d26 100644 --- a/src/Plugin/ImageEffect/TextOverlayImageEffect.php +++ b/src/Plugin/ImageEffect/TextOverlayImageEffect.php @@ -35,7 +35,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface { - // $info stores information about image and text wrapper. + /** + * Stores information about image and text wrapper. + * + * @var int[] + */ protected $info = [ 'image_xpos' => 0, 'image_ypos' => 0, @@ -178,7 +182,7 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont $form = array(); if ($this->getTextimageFactory()) { - // --- Preview effect. + // Preview effect. $this->configuration['preview_bar']['debug_visuals'] = empty($this->configuration['preview_bar']['debug_visuals']) ? FALSE : TRUE; list($success, $preview) = $this->buildPreviewRender($this->configuration); $form['preview'] = [ @@ -189,7 +193,7 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont '#preview' => $preview, ]; - // --- Preview bar. + // Preview bar. $form['preview_bar'] = array( '#type' => 'container', '#attributes' => array( @@ -213,13 +217,13 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont ); } - // --- Settings. + // Settings. $form['settings'] = array( '#type' => 'vertical_tabs', '#tree' => FALSE, ); - // --- Text default. + // Text default. $form['text_default'] = array( '#type' => 'details', '#title' => $this->t('Text default'), @@ -237,7 +241,7 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont $form['text_default']['tokens'] = $token_tree_builder->buildAllRenderable(); } - // ---- Font settings. + // Font settings. $form['font'] = array( '#type' => 'details', '#title' => $this->t('Font settings'), @@ -412,7 +416,7 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont '#default_value' => $this->configuration['font']['stroke_color'], ); - // ---- Text settings. + // Text settings. $form['text'] = array( '#type' => 'details', '#title' => $this->t('Text settings'), @@ -476,7 +480,7 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont '#default_value' => $this->configuration['text']['case_format'], ); - // ---- Layout settings. + // Layout settings. $form['layout'] = array( '#type' => 'details', '#title' => $this->t('Layout settings'), @@ -675,12 +679,6 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont /** * {@inheritdoc} */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - } - - /** - * {@inheritdoc} - */ public function getSummary() { $data = $this->configuration; $data['font_color_detail'] = array( @@ -719,6 +717,10 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont public function applyEffect(ImageInterface $image) { // Preserve current background image dimensions. if ($image->getWidth() === 1 && $image->getHeight() === 1) { + // Special case: when width and height of the source image is 1, we set + // starting width and height within the effect to be zero. This way we + // avoid that text overlays that extend the source image lead to images + // with a single transparent or colored pixel in the center of the image. $image_width = 0; $image_height = 0; } @@ -746,13 +748,13 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont // Check wrapper image overflowing the original image. if ($this->canvasResizeNeeded($wrapper)) { // Apply set_canvas, transparent background. - if (!$image->apply('set_canvas', [ - 'width' => $this->info['image_width'], - 'height' => $this->info['image_height'], - 'x_pos' => $this->info['image_xpos'], - 'y_pos' => $this->info['image_ypos'], - ] - )) { + $data = [ + 'width' => $this->info['image_width'], + 'height' => $this->info['image_height'], + 'x_pos' => $this->info['image_xpos'], + 'y_pos' => $this->info['image_ypos'], + ]; + if (!$image->apply('set_canvas', $data)) { return FALSE; } // Color fill the frame with extended color. @@ -870,23 +872,14 @@ class TextOverlayImageEffect extends ConfigurableImageEffectBase implements Cont $this->configuration['text_string'] = $this->token->replace($this->configuration['text_string']); // Convert case, if requested. - switch ($this->configuration['text']['case_format']) { - case 'upper': - $this->configuration['text_string'] = Unicode::strtoupper($this->configuration['text_string']); - break; - - case 'lower': - $this->configuration['text_string'] = Unicode::strtolower($this->configuration['text_string']); - break; - - case 'ucfirst': - $this->configuration['text_string'] = Unicode::ucfirst($this->configuration['text_string']); - break; - - case 'ucwords': - $this->configuration['text_string'] = Unicode::ucwords($this->configuration['text_string']); - break; - + if ($this->configuration['text']['case_format']) { + $method_map = [ + 'upper' => 'strtoupper', + 'lower' => 'strtolower', + 'ucwords' => 'ucwords', + 'ucfirst' => 'ucfirst', + ]; + $this->configuration['text_string'] = Unicode::{$method_map[$this->configuration['text']['case_format']]}($this->configuration['text_string']); } }