Skip to content

Commit

Permalink
Add support for auto-wrap feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Nov 16, 2017
1 parent 2707322 commit 7612e5b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Expand Up @@ -5,5 +5,5 @@ TO-DO
- Add metrics for standard fonts for width calculation [DONE]
- Add shortcut method to get string width [DONE]
- Fix width calculation for embedded fonts [DONE]
- Add auto-wrap feature
- Add auto-wrap feature [DONE]
- Add HTML/CSS rendering support
29 changes: 25 additions & 4 deletions src/Build/Compiler.php
Expand Up @@ -287,9 +287,22 @@ protected function prepareText(array $text, Object\PageObject $pageObject)
throw new Exception('Error: The font \'' . $txt['font'] . '\' has not been added to the document.');
}
$coordinates = $this->getCoordinates($txt['x'], $txt['y'], $pageObject);
$contentObject->appendStream(
$txt['text']->getStream($this->fontReferences[$txt['font']], $coordinates['x'], $coordinates['y'])
);

if ($txt['text']->hasAutoWrap()) {
$wrapStart = $coordinates['x'];
$wrapStop = $pageObject->getWidth() - $txt['text']->getAutoWrap();
$wrapLength = $wrapStop - $wrapStart;
$font = $this->fontReferences[$txt['font']];
$fontObject = $this->fonts[$txt['font']];
$stream = $txt['text']->startStream($font, $coordinates['x'], $coordinates['y']);
$stream .= $txt['text']->getPartialStream($font, $fontObject, $wrapLength);
$stream .= $txt['text']->endStream();
$contentObject->appendStream($stream);
} else {
$contentObject->appendStream(
$txt['text']->getStream($this->fontReferences[$txt['font']], $coordinates['x'], $coordinates['y'])
);
}
}
}

Expand Down Expand Up @@ -324,7 +337,15 @@ protected function prepareMultiText(array $multiText, Object\PageObject $pageObj
if (null === $stream) {
$stream = $txt->startStream($font, $coordinates['x'], $coordinates['y']);
}
$stream .= $txt->getPartialStream($font);
if ($txt->hasAutoWrap()) {
$wrapStart = $coordinates['x'];
$wrapStop = $pageObject->getWidth() - $txt->getAutoWrap();
$wrapLength = $wrapStop - $wrapStart;
$fontObject = $this->fonts[$font];
$stream .= $txt->getPartialStream($font, $fontObject, $wrapLength);
} else {
$stream .= $txt->getPartialStream($font);
}
}

$stream .= $txt->endStream();
Expand Down
79 changes: 76 additions & 3 deletions src/Document/Page/Text.php
Expand Up @@ -13,6 +13,8 @@
*/
namespace Pop\Pdf\Document\Page;

use Pop\Pdf\Document\Font;

/**
* Pdf page text class
*
Expand Down Expand Up @@ -73,11 +75,17 @@ class Text
];

/**
* Text wrap
* Text wrap (by number of characters)
* @var int
*/
protected $wrap = 0;

/**
* Auto text wrap (by width of characters vs width of page)
* @var int
*/
protected $autoWrap = 0;

/**
* Text line height
* @var int
Expand Down Expand Up @@ -235,6 +243,22 @@ public function setWrap($wrap, $lineHeight = null)
return $this;
}

/**
* Set the word auto-wrap
*
* @param int $wrap
* @param int $lineHeight
* @return Text
*/
public function setAutoWrap($wrap, $lineHeight = null)
{
$this->autoWrap = (int)$wrap;
if (null !== $lineHeight) {
$this->setLineHeight($lineHeight);
}
return $this;
}

/**
* Set the word wrap
*
Expand Down Expand Up @@ -333,6 +357,26 @@ public function getWrap()
return $this->wrap;
}

/**
* Get the word auto-wrap
*
* @return int
*/
public function getAutoWrap()
{
return $this->autoWrap;
}

/**
* Determine if the text object has auto-wrap
*
* @return boolean
*/
public function hasAutoWrap()
{
return ($this->autoWrap > 0);
}

/**
* Get the line height
*
Expand Down Expand Up @@ -412,9 +456,11 @@ public function startStream($fontReference, $x, $y)
* Get the partial text stream
*
* @param string $fontReference
* @param Font $fontObject
* @param int $wrapLength
* @return string
*/
public function getPartialStream($fontReference = null)
public function getPartialStream($fontReference = null, Font $fontObject = null, $wrapLength = null)
{
$stream = '';

Expand Down Expand Up @@ -462,7 +508,34 @@ public function getPartialStream($fontReference = null)
}
}
} else {
$stream .= " ({$this->string})Tj\n";
if ((null !== $fontObject) && (null !== $wrapLength)) {
$strings = [];
$curString = '';
$words = explode(' ', $this->string);
foreach ($words as $word) {
$newString = ($curString != '') ? $curString . ' ' . $word : $word;
if ($fontObject->getStringWidth($newString, $this->size) <= $wrapLength) {
$curString = $newString;
} else {
$strings[] = $curString;
$curString = $word;
}
}
if (!empty($curString)) {
$strings[] = $curString;
}
if ((int)$this->lineHeight == 0) {
$this->lineHeight = $this->size;
}
foreach ($strings as $i => $string) {
$stream .= " ({$string})Tj\n";
if ($i < count($strings)) {
$stream .= " 0 -" . $this->lineHeight . " Td\n";
}
}
} else {
$stream .= " ({$this->string})Tj\n";
}
}
}

Expand Down

0 comments on commit 7612e5b

Please sign in to comment.