1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-29 17:57:50 +01:00

Refactor GD TextWriter

This commit is contained in:
Oliver Vogel 2022-07-04 15:46:32 +02:00
parent 5dc4e66969
commit 6a4a7bfa8f
3 changed files with 45 additions and 42 deletions

View File

@ -17,8 +17,48 @@ abstract class AbstractTextWriter implements ModifierInterface
//
}
public function getTextBlock(): TextBlock
protected function getFont(): FontInterface
{
return new TextBlock($this->text);
return $this->font;
}
protected function getPosition(): Point
{
return $this->position;
}
/**
* Build TextBlock object from text string and align every line
* according to text writers font object and position.
*
* @return TextBlock
*/
public function getAlignedTextBlock(): TextBlock
{
$lines = new TextBlock($this->text);
$position = $this->getPosition();
$font = $this->getFont();
$boundingBox = $lines->getBoundingBox($font, $position);
$pivot = $boundingBox->last();
$leading = $font->leadingInPixels();
$blockWidth = $lines->longestLine()->widthInFont($font);
$x = $pivot->getX();
$y = $font->hasFilename() ? $pivot->getY() + $font->capHeight() : $pivot->getY();
$x_adjustment = 0;
foreach ($lines as $line) {
$x_adjustment = $font->getAlign() == 'left' ? 0 : $blockWidth - $line->widthInFont($font);
$x_adjustment = $font->getAlign() == 'right' ? intval(round($x_adjustment)) : $x_adjustment;
$x_adjustment = $font->getAlign() == 'center' ? intval(round($x_adjustment / 2)) : $x_adjustment;
$position = new Point($x + $x_adjustment, $y);
$position->rotate($font->getAngle(), $pivot);
$line->setPosition($position);
$y += $leading;
}
return $lines;
}
}

View File

@ -5,16 +5,14 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
use Intervention\Image\Drivers\Gd\Font;
use Intervention\Image\Exceptions\FontException;
use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\ImageInterface;
class TextWriter extends AbstractTextWriter
{
public function apply(ImageInterface $image): ImageInterface
{
$lines = $this->getTextBlock();
$boundingBox = $lines->getBoundingBox($this->getFont(), $this->position);
$lines->alignByFont($this->getFont(), $boundingBox->last());
$lines = $this->getAlignedTextBlock();
foreach ($image as $frame) {
if ($this->font->hasFilename()) {
foreach ($lines as $line) {
@ -29,9 +27,6 @@ class TextWriter extends AbstractTextWriter
$line
);
}
// debug
imagepolygon($frame->getCore(), $boundingBox->toArray(), 0);
} else {
foreach ($lines as $line) {
imagestring(
@ -42,7 +37,6 @@ class TextWriter extends AbstractTextWriter
$line,
$this->font->getColor()->toInt()
);
imagepolygon($frame->getCore(), $boundingBox->toArray(), 0);
}
}
}
@ -50,7 +44,7 @@ class TextWriter extends AbstractTextWriter
return $image;
}
private function getFont(): Font
protected function getFont(): FontInterface
{
if (!is_a($this->font, Font::class)) {
throw new FontException('Font is not compatible to current driver.');

View File

@ -17,37 +17,6 @@ class TextBlock extends Collection
}
}
/**
* Set position of each line in text block
* according to given font settings.
*
* @param FontInterface $font
* @param Point $pivot
* @return TextBlock
*/
public function alignByFont(FontInterface $font, Point $pivot = null): self
{
$pivot = $pivot ? $pivot : new Point();
$leading = $font->leadingInPixels();
$x = $pivot->getX();
$y = $font->hasFilename() ? $pivot->getY() + $font->capHeight() : $pivot->getY();
$x_adjustment = 0;
$total_width = $this->longestLine()->widthInFont($font);
foreach ($this as $line) {
$x_adjustment = $font->getAlign() == 'left' ? 0 : $total_width - $line->widthInFont($font);
$x_adjustment = $font->getAlign() == 'right' ? intval(round($x_adjustment)) : $x_adjustment;
$x_adjustment = $font->getAlign() == 'center' ? intval(round($x_adjustment / 2)) : $x_adjustment;
$position = new Point($x + $x_adjustment, $y);
$position->rotate($font->getAngle(), $pivot);
$line->setPosition($position);
$y += $leading;
}
return $this;
}
public function getBoundingBox(FontInterface $font, Point $pivot = null): Polygon
{
$pivot = $pivot ? $pivot : new Point();