Skip to content

Commit

Permalink
Fix #25 (#31)
Browse files Browse the repository at this point in the history
* Add test case

* Add max attempts at wrapping

* Successful generation

* Some cleanup
  • Loading branch information
simonhamp authored Aug 14, 2024
1 parent dd61f1d commit 8c19cc2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Interfaces/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\ImageInterface;
use SimonHamp\TheOg\Layout\Position;

interface Box
{
public function anchor(): Point;
public function anchor(?Position $position): Point;

public function name(string $name): static;

Expand Down
9 changes: 7 additions & 2 deletions src/Layout/TextBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\TextModifier;
use Intervention\Image\Typography\FontFactory;
use Intervention\Image\Typography\TextBlock;
Expand Down Expand Up @@ -78,7 +79,7 @@ protected function getPrerenderedBox(): Rectangle
return $this->getFinalTextBox($modifier);
}

protected function generateModifier(string $text, Point $position = new Point()): CustomTextModifier
protected function generateModifier(string $text, Point $position = new Point()): SpecializedInterface
{
return CustomTextModifier::buildSpecialized(
new TextModifier(
Expand Down Expand Up @@ -131,7 +132,9 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
$text = $this->text;
$renderedBox = $this->getRenderedBoxForText($text, $modifier);

while (! $this->doesTextFitInBox($renderedBox)) {
$attempts = 0;

while (! $this->doesTextFitInBox($renderedBox) && $attempts < 10) {
if ($renderedBox->width() > $this->box->width()) {
$text = wordwrap($text, intval(floor($this->box->width() / ($modifier->boxSize('M')->width() / 1.8))));
$renderedBox = $this->getRenderedBoxForText($text, $modifier);
Expand All @@ -150,6 +153,8 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
}

$modifier = $this->generateModifier($text, $modifier->position);

$attempts++;
}

return $renderedBox;
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,12 @@ public static function snapshotImages(): iterable
->title('Simone Hampstead'),
'avatar-too-small',
];

yield 'tricky title truncation' => [
(new Image())
->layout(new GitHubBasic)
->title('Adding Unique Field to MySQL Table With Existing Records'),
'tricky-title-truncation',
];
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8c19cc2

Please sign in to comment.