From 39448eafbbce02ad6c0fe4b31183b1a1dff0cd52 Mon Sep 17 00:00:00 2001 From: Frank Dana Date: Fri, 10 May 2024 16:45:47 -0400 Subject: [PATCH 1/2] Fix multiLineWrap line-construction Test the length of `$possibleLine` for overflow, not the length of `$currentLine`. Only insert a space before the next `$word` when appending to a non-empty line. --- src/ImagickDemo/Tutorial/multiLineWrap.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ImagickDemo/Tutorial/multiLineWrap.php b/src/ImagickDemo/Tutorial/multiLineWrap.php index 37310f50..5e963a3b 100644 --- a/src/ImagickDemo/Tutorial/multiLineWrap.php +++ b/src/ImagickDemo/Tutorial/multiLineWrap.php @@ -30,16 +30,21 @@ public static function fromText(\Imagick $imagick, \ImagickDraw $draw, $text, $m $lines = array(); $currentLine = ''; foreach ($words as $word) { - $currentLine .= $word; - $possibleLine = $currentLine . ' '. $word; - //Check to see if we can add another word to this line - $metrics = $imagick->queryFontMetrics($draw, $currentLine); + if (strlen($currentLine) > 0) { + $possibleLine = $currentLine . ' '. $word; + } else { + $possibleLine = $word; + } + //If adding the next word would make the current line + //too long, place that line into the array + //and use the next word to start a new line. + $metrics = $imagick->queryFontMetrics($draw, $possibleLine); if ($metrics['textWidth'] >= $maxWidth) { $lines[] = $currentLine; $currentLine = $word; } else { - $currentLine .= ' '; + $currentLine = $possibleLine; } //Finally, update line height if ($metrics['textHeight'] > $lineHeight) { From a8706205b9773fcd9d8670a42e86852f0d693dfd Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Fri, 10 May 2024 18:37:15 -0400 Subject: [PATCH 2/2] multiLineWrap: Also reverse array of output lines --- src/ImagickDemo/Tutorial/multiLineWrap.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ImagickDemo/Tutorial/multiLineWrap.php b/src/ImagickDemo/Tutorial/multiLineWrap.php index 5e963a3b..f02ca7bb 100644 --- a/src/ImagickDemo/Tutorial/multiLineWrap.php +++ b/src/ImagickDemo/Tutorial/multiLineWrap.php @@ -56,8 +56,11 @@ public static function fromText(\Imagick $imagick, \ImagickDraw $draw, $text, $m if (strlen($currentLine) > 0) { $lines[] = $currentLine; } - - return new self($lineHeight, $lines); + + //Put the lines in the right order + $orderedLines = array_reverse($lines); + + return new self($lineHeight, $orderedLines); } }