1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-18 04:38:26 +01:00

Add doc blocks

This commit is contained in:
Oliver Vogel 2024-01-28 17:42:11 +01:00
parent e57ba4224f
commit f6a0e6cc62
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB
3 changed files with 54 additions and 5 deletions

View File

@ -17,18 +17,38 @@ use Intervention\Image\Typography\Line;
*/ */
abstract class AbstractTextModifier extends DriverSpecialized implements ModifierInterface abstract class AbstractTextModifier extends DriverSpecialized implements ModifierInterface
{ {
/**
* Calculate size of bounding box of given text
*
* @return Polygon
*/
abstract protected function boxSize(string $text): Polygon; abstract protected function boxSize(string $text): Polygon;
/**
* Calculates typographical leanding
*
* @return int
*/
public function leadingInPixels(): int public function leadingInPixels(): int
{ {
return intval(round($this->fontSizeInPixels() * $this->font->lineHeight())); return intval(round($this->fontSizeInPixels() * $this->font->lineHeight()));
} }
/**
* Calculates typographical cap height
*
* @return int
*/
public function capHeight(): int public function capHeight(): int
{ {
return $this->boxSize('T')->height(); return $this->boxSize('T')->height();
} }
/**
* Calculates the font size in pixels
*
* @return int
*/
public function fontSizeInPixels(): int public function fontSizeInPixels(): int
{ {
return $this->boxSize('Hy')->height(); 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 * 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 * @return TextBlock
*/ */
public function alignedTextBlock(Point $position, string $text): TextBlock public function alignedTextBlock(Point $position, string $text): TextBlock
@ -67,6 +89,14 @@ abstract class AbstractTextModifier extends DriverSpecialized implements Modifie
return $lines; 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 public function boundingBox(TextBlock $block, Point $pivot = null): Polygon
{ {
$pivot = $pivot ? $pivot : new Point(); $pivot = $pivot ? $pivot : new Point();
@ -89,6 +119,12 @@ abstract class AbstractTextModifier extends DriverSpecialized implements Modifie
return $box; return $box;
} }
/**
* Calculates the width of the given line in pixels
*
* @param Line $line
* @return int
*/
private function lineWidth(Line $line): int private function lineWidth(Line $line): int
{ {
return $this->boxSize((string) $line)->width(); return $this->boxSize((string) $line)->width();

View File

@ -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 protected function boxSize(string $text): Polygon
{ {

View File

@ -6,6 +6,8 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Imagick; use Imagick;
use ImagickDraw; use ImagickDraw;
use ImagickDrawException;
use ImagickException;
use ImagickPixel; use ImagickPixel;
use Intervention\Image\Drivers\AbstractTextModifier; use Intervention\Image\Drivers\AbstractTextModifier;
use Intervention\Image\Exceptions\FontException; 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 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 private function toImagickDraw(?ImagickPixel $color = null): ImagickDraw
{ {
if (!$this->font->hasFilename()) { if (!$this->font->hasFilename()) {