Skip to content

Commit

Permalink
fixed interline
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Dec 22, 2023
1 parent 44c3a29 commit 56f2d53
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/Alterations/WriteText.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,33 @@ public function applyWithGd(Image $image): null
$color = $driver->parseColor($text->color);
$box = $text->getBox();

$opt = [];
$interlinePixels = 0;
if ($text->interline !== null) {
$opt['linespacing'] = $text->interline;
$interlinePixels = $text->interline * $text->getPointSize();
$interlinePixels -= $text->getPointSize();
}

if ($text->background !== null) {
$bgY = $y;
$bgOffset = -4.25 * (1 / 50 * $text->size);
if ($text->interline !== null) {
$interlinePixels += $bgOffset;
$interlinePixels /= 0.65;
}
foreach ($text->getMultiLineBoxes() as $lineBox) {
$image->rectangle(
$x + $bgOffset,
$bgY - $bgOffset,
$x + $lineBox->upperRight->x - $bgOffset,
$bgY + $lineBox->upperRight->y + $bgOffset,
fn (Rectangle $r) => $r->background($text->background)
fn (Rectangle $r) => $r->background($text->background)->border(0)
);
$bgY -= $box->upperRight->y - ($text->getPointSize() * 0.72);
$bgY -= $box->upperRight->y - ($text->getPointSize() * 0.72) - $interlinePixels;
}
}

$opt = [];
if ($text->interline !== null) {
$opt['linespacing'] = $text->interline;
}

if ($text->hasFont()) {
if ($text->angle !== 0 || $text->align !== Position::TOP_LEFT) {
switch ($text->align) {
Expand Down Expand Up @@ -299,7 +306,9 @@ public function applyWithImagick(Image $image): null
$draw->setFillColor($color->getPixel());
$draw->setTextKerning($text->kerning);
if ($text->interline !== null) {
$draw->setTextInterLineSpacing($text->interline);
$interline = $text->interline * $text->getPointSize();
$interline -= $text->getPointSize();
$draw->setTextInterLineSpacing($interline);
}

switch ($text->align) {
Expand Down
2 changes: 1 addition & 1 deletion tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ function prepare($instance, string $name, Backend $driver, string $ext = 'png'):
Image::make($file, $driver)
->text("Hello\nWorld!\nMore strings!", 100, 100, function (Text $text) {
$text->size(80)
->interline(0.1)
->interline(1.2)
->background(Color::fuchsia());
})
->save($out, quality: 100);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/Images/Gd/fruit_with_text_stroke.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 56f2d53

Please sign in to comment.