Skip to content

Commit 8c19cc2

Browse files
authored
Fix #25 (#31)
* Add test case * Add max attempts at wrapping * Successful generation * Some cleanup
1 parent dd61f1d commit 8c19cc2

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/Interfaces/Box.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Intervention\Image\Geometry\Point;
66
use Intervention\Image\Interfaces\ImageInterface;
7+
use SimonHamp\TheOg\Layout\Position;
78

89
interface Box
910
{
10-
public function anchor(): Point;
11+
public function anchor(?Position $position): Point;
1112

1213
public function name(string $name): static;
1314

src/Layout/TextBox.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
99
use Intervention\Image\Interfaces\ColorInterface;
1010
use Intervention\Image\Interfaces\ImageInterface;
11+
use Intervention\Image\Interfaces\SpecializedInterface;
1112
use Intervention\Image\Modifiers\TextModifier;
1213
use Intervention\Image\Typography\FontFactory;
1314
use Intervention\Image\Typography\TextBlock;
@@ -78,7 +79,7 @@ protected function getPrerenderedBox(): Rectangle
7879
return $this->getFinalTextBox($modifier);
7980
}
8081

81-
protected function generateModifier(string $text, Point $position = new Point()): CustomTextModifier
82+
protected function generateModifier(string $text, Point $position = new Point()): SpecializedInterface
8283
{
8384
return CustomTextModifier::buildSpecialized(
8485
new TextModifier(
@@ -131,7 +132,9 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
131132
$text = $this->text;
132133
$renderedBox = $this->getRenderedBoxForText($text, $modifier);
133134

134-
while (! $this->doesTextFitInBox($renderedBox)) {
135+
$attempts = 0;
136+
137+
while (! $this->doesTextFitInBox($renderedBox) && $attempts < 10) {
135138
if ($renderedBox->width() > $this->box->width()) {
136139
$text = wordwrap($text, intval(floor($this->box->width() / ($modifier->boxSize('M')->width() / 1.8))));
137140
$renderedBox = $this->getRenderedBoxForText($text, $modifier);
@@ -150,6 +153,8 @@ protected function getFinalTextBox(CustomTextModifier &$modifier): Rectangle
150153
}
151154

152155
$modifier = $this->generateModifier($text, $modifier->position);
156+
157+
$attempts++;
153158
}
154159

155160
return $renderedBox;

tests/Integration/ImageTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,12 @@ public static function snapshotImages(): iterable
201201
->title('Simone Hampstead'),
202202
'avatar-too-small',
203203
];
204+
205+
yield 'tricky title truncation' => [
206+
(new Image())
207+
->layout(new GitHubBasic)
208+
->title('Adding Unique Field to MySQL Table With Existing Records'),
209+
'tricky-title-truncation',
210+
];
204211
}
205212
}

0 commit comments

Comments
 (0)