diff --git a/src/Drivers/AbstractTextModifier.php b/src/Drivers/AbstractTextModifier.php index cc34e6e3..1e179e3e 100644 --- a/src/Drivers/AbstractTextModifier.php +++ b/src/Drivers/AbstractTextModifier.php @@ -17,18 +17,38 @@ use Intervention\Image\Typography\Line; */ abstract class AbstractTextModifier extends DriverSpecialized implements ModifierInterface { + /** + * Calculate size of bounding box of given text + * + * @return Polygon + */ abstract protected function boxSize(string $text): Polygon; + /** + * Calculates typographical leanding + * + * @return int + */ public function leadingInPixels(): int { return intval(round($this->fontSizeInPixels() * $this->font->lineHeight())); } + /** + * Calculates typographical cap height + * + * @return int + */ public function capHeight(): int { return $this->boxSize('T')->height(); } + /** + * Calculates the font size in pixels + * + * @return int + */ public function fontSizeInPixels(): int { return $this->boxSize('Hy')->height(); @@ -36,8 +56,10 @@ abstract class AbstractTextModifier extends DriverSpecialized implements Modifie /** * Build TextBlock object from text string and align every line - * according to text writers font object and position. + * according to text modifier's font object and position. * + * @param Point $position + * @param string $text * @return TextBlock */ public function alignedTextBlock(Point $position, string $text): TextBlock @@ -67,6 +89,14 @@ abstract class AbstractTextModifier extends DriverSpecialized implements Modifie return $lines; } + /** + * Returns bounding box of the given text block according to text modifier's + * font settings and given pivot point + * + * @param TextBlock $block + * @param Point|null $pivot + * @return Polygon + */ public function boundingBox(TextBlock $block, Point $pivot = null): Polygon { $pivot = $pivot ? $pivot : new Point(); @@ -89,6 +119,12 @@ abstract class AbstractTextModifier extends DriverSpecialized implements Modifie return $box; } + /** + * Calculates the width of the given line in pixels + * + * @param Line $line + * @return int + */ private function lineWidth(Line $line): int { return $this->boxSize((string) $line)->width(); diff --git a/src/Drivers/Gd/Modifiers/TextModifier.php b/src/Drivers/Gd/Modifiers/TextModifier.php index 7f3f1a37..d9b2237c 100644 --- a/src/Drivers/Gd/Modifiers/TextModifier.php +++ b/src/Drivers/Gd/Modifiers/TextModifier.php @@ -59,9 +59,9 @@ class TextModifier extends AbstractTextModifier } /** - * Calculate size of bounding box of given text + * {@inheritdoc} * - * @return Polygon + * @see AbstractTextModifier::boxSize() */ protected function boxSize(string $text): Polygon { diff --git a/src/Drivers/Imagick/Modifiers/TextModifier.php b/src/Drivers/Imagick/Modifiers/TextModifier.php index 1be7f92a..97d5a40c 100644 --- a/src/Drivers/Imagick/Modifiers/TextModifier.php +++ b/src/Drivers/Imagick/Modifiers/TextModifier.php @@ -6,6 +6,8 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Imagick; use ImagickDraw; +use ImagickDrawException; +use ImagickException; use ImagickPixel; use Intervention\Image\Drivers\AbstractTextModifier; use Intervention\Image\Exceptions\FontException; @@ -48,9 +50,9 @@ class TextModifier extends AbstractTextModifier } /** - * Calculate box size of current font + * {@inheritdoc} * - * @return Polygon + * @see AbstractTextModifier::boxSize() */ protected function boxSize(string $text): Polygon { @@ -70,6 +72,17 @@ class TextModifier extends AbstractTextModifier )); } + /** + * Imagick::annotateImage() needs an ImagickDraw object - this method takes + * the text color as the base and adds the text modifiers font settings + * to the new ImagickDraw object. + * + * @param null|ImagickPixel $color + * @return ImagickDraw + * @throws FontException + * @throws ImagickDrawException + * @throws ImagickException + */ private function toImagickDraw(?ImagickPixel $color = null): ImagickDraw { if (!$this->font->hasFilename()) {