Skip to content

Fix multiLineWrap line-construction #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/ImagickDemo/Tutorial/multiLineWrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -51,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);
}
}

Expand Down