Skip to content
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

Pr fixes #177

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/Cezpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ public function ezTable(&$data, $cols = '', $title = '', $options = '')
$lines = [];
}
$this->y -= $options['rowGap'];
foreach ($lines as $line) {
foreach ($lines as $i => $line) {
$line = $this->ezProcessText($line);
// set the text color
// grab the defined colors for this cell
Expand Down Expand Up @@ -1720,6 +1720,14 @@ public function ezTable(&$data, $cols = '', $title = '', $options = '')
} else {
if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['justification'])) {
$just = $options['cols'][$colName]['justification'];

if ($just == 'full') {
// do not fully justify if its the absolute last line (taking line breaks into account)
$tmp = $this->addText($pos[$colName], $this->y, $options['fontSize'], $line, $maxWidth[$colName], $just, 0,0,1);
if (!strlen($tmp)) {
$just = "left";
}
}
} else {
$just = 'left';
}
Expand Down Expand Up @@ -1969,7 +1977,7 @@ public function ezText($text, $size = 0, $options = [], $test = 0)
$right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right'])) ? $options['right'] : 0);
}

if ($just == 'full' && (empty($lines[$i + 1]) || $c == $i + 1)) {
if ($just == 'full') {
// do not fully justify if its the absolute last line (taking line breaks into account)
$tmp = $this->addText($left, $this->y, $size, $line, $right - $left, $just, 0, 0, 1);
if (!strlen($tmp)) {
Expand Down
103 changes: 96 additions & 7 deletions src/Cpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,52 @@ protected function filterText($text, $bom = true, $convert_encoding = true)

return $text;
}



/**
* Prepares the text with directives for later processing in text addition.
*
* This function prepares text with directives at the specified coordinates and
* with the specified font size and style. The function also allows for text justification and
* rotation, and returns an array of text and formatting information.
*
* @param string &$text The text to add to the PDF document
* @param float $x The x coordinate of the starting point of the text
* @param float $y The y coordinate of the starting point of the text
* @param float $size The font size of the text
* @param float &$width The maximum width available for the text
* @param string $justification The justification of the text: "left", "center", or "right"
* @param float $angle The angle of rotation for the text
* @param float $wordSpaceAdjust The space between words in the text
*
* @return array An array of text and formatting information
*
* Example of returned array for:
* $text = "Hello<b>World</b>! extra text"
* $width = 100
*
* [
* 0 => [
* 'text' => "Hello ",
* 'nspaces' => 1,
* 'callback' => ['func' => 'b', 'status' => 'start', ... ]
* ],
* 1 => [
* 'text': "World",
* 'nspaces': 0,
* 'callback': ['func' => 'b', 'status' => 'end', ... ]
* ],
* 2 => [
* 'text' => "!",
* 'nspaces' => 1,
* 'callback' => null
* ]
* ]
*
* With the next resulting output values:
* $text = "extra text" (will be processed in next call)
* $width = 17.489
*/
private function addTextWithDirectives(&$text, $x, $y, $size, &$width, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0)
{
$result = [];
Expand All @@ -2839,6 +2884,7 @@ private function addTextWithDirectives(&$text, $x, $y, $size, &$width, $justific
$x += $textLength[0];
$y += $textLength[1];

// text must be truncated
if ($textLength[2] >= 0) {
if (isset($result[count($result) - 1])) {
$prev = &$result[count($result) - 1];
Expand All @@ -2863,7 +2909,9 @@ private function addTextWithDirectives(&$text, $x, $y, $size, &$width, $justific

$text = mb_substr($text, $offset, null, 'UTF-8');
} else {
$text = mb_substr($text, $offset + $textLength[2] + $textLength[3], null, 'UTF-8');
// make sure we advance at least 1 character
$textOff = max(1, $offset + $textLength[2] + $textLength[3]);
$text = mb_substr($text, $textOff, null, 'UTF-8');
array_push($result, ['text' => mb_substr($part, 0, $textLength[2], 'UTF-8'), 'nspaces' => $textLength[4], 'callback' => $info]);
}

Expand Down Expand Up @@ -2980,7 +3028,17 @@ protected function defaultFormatting($info)
}

/**
* add text to the document, at a specified location, size and angle on the page.
* Add text to the document, at a specified location, size and angle on the page.
* @param float $x The x-coordinate of the starting point of the text
* @param float $y The y-coordinate of the starting point of the text
* @param float $size The size of the text
* @param string $text The text to be added
* @param float $width The maximum width allowed for the text (default: 0)
* @param string $justification The type of text justification ('left', 'right', 'center', 'full') (default: 'left')
* @param float $angle The angle in degrees by which the text should be rotated (default: 0)
* @param float $wordSpaceAdjust The amount of space between words (default: 0)
* @param bool $test Flag indicating whether the function is being used for testing purposes (default: 0)
* @return string The text that was added to the document
*/
public function addText($x, $y, $size, $text, $width = 0, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0, $test = 0)
{
Expand All @@ -3005,7 +3063,7 @@ public function addText($x, $y, $size, $text, $width = 0, $justification = 'left
return $v['text'];
}, $parts));

$this->adjustWrapText($parsedText, $orgWidth - $width, $orgWidth, $x, $wordSpaceAdjust, $justification);
$this->adjustWrapText($parsedText, $orgWidth - $width, $orgWidth, $x, $wordSpaceAdjust, $justification);

if ($test) {
return $text;
Expand Down Expand Up @@ -3077,8 +3135,22 @@ public function addText($x, $y, $size, $text, $width = 0, $justification = 'left
return $text;
}

/**
* Add wrapped text to the document at the given coordinates.
* @param float $x The x-coordinate of the starting point of the text.
* @param float $y The y-coordinate of the starting point of the text.
* @param int $size The font size to use for the text.
* @param string $text The text to add to the document.
* @param float $width The width of the area in which the text should wrap.
* @param string $justification The type of justification to use for the text. Can be 'left', 'right', 'center', or 'full'.
* @param int $angle The angle at which to draw the text, measured in degrees.
* @param float $wordSpaceAdjust The space between words in the text
* @param bool $test Whether to just return the calculated height without actually adding the text to the PDF.
* @return void
*/
public function addTextWrap($x, $y, $size, $text, $width = 0, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0, $test = 0)
{
// Split the input text into an array of lines based on line breaks and wrap the lines as necessary
$parts = preg_split('/$\R?^/m', $text);
foreach ($parts as $v) {
$text = $this->addText($x, $y, $size, $v, $width, $justification, $angle, $wordSpaceAdjust, $test);
Expand Down Expand Up @@ -3122,6 +3194,23 @@ public function getTextWidth($size, $text)
return $tmp[0];
}

/**
* Calculates the width and height of a given text string in the current
* font and size, taking into account any rotation angle and a maximum width
* (in page units) for line wrapping
*
* @param float $size The font size to use for the text.
* @param string $text The text string for which to calculate the length.
* @param float $maxWidth The maximum width of the text string. If the text string exceeds this width, it will be truncated.
* @param float $angle The angle at which to draw the text, in degrees.
* @param float $wa word spacing (0 will use default spacing)
* @return array an array with five elements:
* [0] => The width of the text string in the current font and size, taking into account the font size and angle.
* [1] => The height of the text string in the current font and size, taking into account the font size and angle.
* [2] => The index of the character at which the text string should be truncated, if it exceeds the maximum width, or -1 if no line wrapping occurs.
* [3] => A boolean value indicating whether a space character should be added at the truncation point.
* [4] => The number of spaces in the text string.
*/
private function getTextLength($size, $text, $maxWidth = 0, $angle = 0, $wa = 0)
{
if (!$this->numFonts) {
Expand All @@ -3132,9 +3221,9 @@ private function getTextLength($size, $text, $maxWidth = 0, $angle = 0, $wa = 0)
// get length of its unicode string
$len = mb_strlen($text, 'UTF-8');
$cf = $this->currentFont;
$tw = $maxWidth / $size * 1000;
$tw = $maxWidth / $size * 1000; // maximum width of the text in glyph units
$break = -1;
$w = 0;
$w = 0; // accumulated total length of the string
$nspaces = 0;

for ($i = 0; $i < $len; ++$i) {
Expand All @@ -3144,7 +3233,7 @@ private function getTextLength($size, $text, $maxWidth = 0, $angle = 0, $wa = 0)
continue;
}

// verify if the charactor is a valid space (unicode supported, see $this->spaces)
// verify if the character is a valid space (unicode supported, see $this->spaces)
$isSpace = in_array($cOrd, $this->spaces);

if (isset($this->fonts[$cf]['differences'][$cOrd])) {
Expand Down
Loading