Permalink
Browse files

Make calculateBox() return Rectangle struct instead int array

  • Loading branch information...
stil committed Jun 4, 2017
1 parent f83f435 commit b395fef9009bd37e6df137afacf0d25237c1bd61
Showing with 23 additions and 7 deletions.
  1. +23 −7 src/Box.php
View
@@ -263,13 +263,12 @@ public function draw($text)
$n = 0;
foreach ($lines as $line) {
$box = $this->calculateBox($line);
$boxWidth = $box[2] - $box[0];
switch ($this->alignX) {
case HorizontalAlignment::Center:
$xAlign = ($this->box->getWidth() - $boxWidth) / 2;
$xAlign = ($this->box->getWidth() - $box->getWidth()) / 2;
break;
case HorizontalAlignment::Right:
$xAlign = ($this->box->getWidth() - $boxWidth);
$xAlign = ($this->box->getWidth() - $box->getWidth());
break;
case HorizontalAlignment::Left:
default:
@@ -289,7 +288,7 @@ public function draw($text)
new Rectangle(
$xMOD,
$this->box->getY() + $yAlign + ($n * $lineHeightPx) + ($lineHeightPx - $backgroundHeight) + (1 - $this->lineHeight) * 13 * (1 / 50 * $this->fontSize),
$boxWidth,
$box->getWidth(),
$backgroundHeight
),
$this->backgroundColor
@@ -302,7 +301,7 @@ public function draw($text)
new Rectangle(
$xMOD,
$this->box->getY() + $yAlign + ($n * $lineHeightPx),
$boxWidth,
$box->getWidth(),
$lineHeightPx
),
new Color(rand(1, 180), rand(1, 180), rand(1, 180))
@@ -350,7 +349,7 @@ protected function wrapTextWithOverflow($text)
$line = $words[0];
for ($i = 1; $i < count($words); $i++) {
$box = $this->calculateBox($line." ".$words[$i]);
if (($box[4]-$box[6]) >= $this->box->getWidth()) {
if ($box->getWidth() >= $this->box->getWidth()) {
$lines[] = $line;
$line = $words[$i];
} else {
@@ -382,9 +381,26 @@ protected function drawFilledRectangle(Rectangle $rect, Color $color)
);
}
/**
* Returns the bounding box of a text.
* @param string $text
* @return Rectangle
*/
protected function calculateBox($text)
{
return imagettfbbox($this->getFontSizeInPoints(), 0, $this->fontFace, $text);
$bounds = imagettfbbox($this->getFontSizeInPoints(), 0, $this->fontFace, $text);
$xLeft = $bounds[0]; // (lower|upper) left corner, X position
$xRight = $bounds[2]; // (lower|upper) right corner, X position
$yLower = $bounds[1]; // lower (left|right) corner, Y position
$yUpper = $bounds[5]; // upper (left|right) corner, Y position
return new Rectangle(
$xLeft,
$yUpper,
$xRight - $xLeft,
$yLower - $yUpper
);
}
protected function strokeText($x, $y, $text)

0 comments on commit b395fef

Please sign in to comment.