Skip to content

Commit

Permalink
Refactor font processing
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 26, 2023
1 parent 5eb3eb5 commit 60f2bed
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
use Intervention\Image\Geometry\Polygon;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\FontProcessorInterface;
use Intervention\Image\Typography\Line;
use Intervention\Image\Typography\TextBlock;
use Intervention\Image\Typography\Line;

abstract class AbstractFontProcessor implements FontProcessorInterface
/**
* @property FontInterface $font
*/
abstract class AbstractTextModifier extends DriverModifier
{
public function __construct(protected FontInterface $font)
{
}
abstract protected function boxSize(string $text): Polygon;

public function leadingInPixels(): int
{
Expand Down
7 changes: 0 additions & 7 deletions src/Drivers/Gd/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\FontProcessorInterface;
use Intervention\Image\Interfaces\ImageInterface;

class Driver extends AbstractDriver
Expand Down Expand Up @@ -45,9 +43,4 @@ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorI
{
return new ColorProcessor($colorspace);
}

public function fontProcessor(FontInterface $font): FontProcessorInterface
{
return new FontProcessor($font);
}
}
87 changes: 0 additions & 87 deletions src/Drivers/Gd/FontProcessor.php

This file was deleted.

89 changes: 80 additions & 9 deletions src/Drivers/Gd/Modifiers/TextModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Intervention\Image\Drivers\Gd\Modifiers;

use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Drivers\Gd\FontProcessor;
use Intervention\Image\Drivers\AbstractTextModifier;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Polygon;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\FontInterface;

/**
* @property Point $position
* @property string $text
* @property FontInterface $font
*/
class TextModifier extends DriverModifier
class TextModifier extends AbstractTextModifier
{
public function apply(ImageInterface $image): ImageInterface
{
$processor = $this->fontProcessor();
$lines = $processor->alignedTextBlock($this->position, $this->text);
$lines = $this->alignedTextBlock($this->position, $this->text);

$color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->font->color())
Expand All @@ -29,7 +29,7 @@ public function apply(ImageInterface $image): ImageInterface
foreach ($lines as $line) {
imagettftext(
$frame->native(),
$processor->adjustedSize(),
$this->adjustedSize(),
$this->font->angle() * -1,
$line->position()->x(),
$line->position()->y(),
Expand All @@ -42,7 +42,7 @@ public function apply(ImageInterface $image): ImageInterface
foreach ($lines as $line) {
imagestring(
$frame->native(),
$processor->getGdFont(),
$this->getGdFont(),
$line->position()->x(),
$line->position()->y(),
$line,
Expand All @@ -55,8 +55,79 @@ public function apply(ImageInterface $image): ImageInterface
return $image;
}

private function fontProcessor(): FontProcessor
/**
* Calculate size of bounding box of given text
*
* @return Polygon
*/
protected function boxSize(string $text): Polygon
{
return $this->driver()->fontProcessor($this->font);
if (!$this->font->hasFilename()) {
// calculate box size from gd font
$box = new Rectangle(0, 0);
$chars = mb_strlen($text);
if ($chars > 0) {
$box->setWidth($chars * $this->getGdFontWidth());
$box->setHeight($this->getGdFontHeight());
}
return $box;
}

// calculate box size from font file with angle 0
$box = imageftbbox(
$this->adjustedSize(),
0,
$this->font->filename(),
$text
);

// build polygon from points
$polygon = new Polygon();
$polygon->addPoint(new Point($box[6], $box[7]));
$polygon->addPoint(new Point($box[4], $box[5]));
$polygon->addPoint(new Point($box[2], $box[3]));
$polygon->addPoint(new Point($box[0], $box[1]));

return $polygon;
}

private function adjustedSize(): float
{
return floatval(ceil($this->font->size() * .75));
}

private function getGdFont(): int
{
if (is_numeric($this->font->filename())) {
return intval($this->font->filename());
}

return 1;
}

private function getGdFontWidth(): int
{
return $this->getGdFont() + 4;
}

private function getGdFontHeight(): int
{
switch ($this->getGdFont()) {
case 2:
return 14;

case 3:
return 14;

case 4:
return 16;

case 5:
return 16;

default:
case 1:
return 8;
}
}
}
7 changes: 0 additions & 7 deletions src/Drivers/Imagick/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\FontProcessorInterface;
use Intervention\Image\Interfaces\ImageInterface;

class Driver extends AbstractDriver
Expand Down Expand Up @@ -43,9 +41,4 @@ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorI
{
return new ColorProcessor($colorspace);
}

public function fontProcessor(FontInterface $font): FontProcessorInterface
{
return new FontProcessor($font);
}
}
57 changes: 0 additions & 57 deletions src/Drivers/Imagick/FontProcessor.php

This file was deleted.

Loading

0 comments on commit 60f2bed

Please sign in to comment.