xref: /webtrees/app/Report/ReportPdfText.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class ReportPdfText
21 */
22class ReportPdfText extends ReportBaseText {
23	/**
24	 * PDF Text renderer
25	 *
26	 * @param PDF $renderer
27	 *
28	 * @return void
29	 */
30	function render($renderer) {
31		// Set up the style
32		if ($renderer->getCurrentStyle() != $this->styleName) {
33			$renderer->setCurrentStyle($this->styleName);
34		}
35		$temptext = str_replace("#PAGENUM#", $renderer->PageNo(), $this->text);
36		// underline «title» part of Source item
37		$temptext = str_replace(array('«', '»'), array('<u>', '</u>'), $temptext);
38
39		// Paint the text color or they might use inherited colors by the previous function
40		$match = array();
41		if (preg_match("/#?(..)(..)(..)/", $this->color, $match)) {
42			$r = hexdec($match[1]);
43			$g = hexdec($match[2]);
44			$b = hexdec($match[3]);
45			$renderer->SetTextColor($r, $g, $b);
46		} else {
47			$renderer->SetTextColor(0, 0, 0);
48		}
49		$temptext = spanLTRRTL($temptext, "BOTH");
50		$temptext = str_replace(
51			array('<br><span dir="rtl" >', '<br><span dir="ltr" >', '> ', ' <'),
52			array('<span dir="rtl" ><br>', '<span dir="ltr" ><br>', '>&nbsp;', '&nbsp;<'),
53			$temptext
54		);
55		$renderer->writeHTML(
56			$temptext,
57			false,
58			false,
59			true,
60			false,
61			""
62		); //change height - line break etc. - the form is mirror on rtl pages
63		// Reset the text color to black or it will be inherited
64		$renderer->SetTextColor(0, 0, 0);
65	}
66
67	/**
68	 * Returns the height in points of the text element
69	 *
70	 * The height is already calculated in getWidth()
71	 *
72	 * @param PDF $pdf
73	 *
74	 * @return float 0
75	 */
76	function getHeight($pdf) {
77		return 0;
78	}
79
80	/**
81	 * Splits the text into lines if necessary to fit into a giving cell
82	 *
83	 * @param PDF $pdf
84	 *
85	 * @return array
86	 */
87	function getWidth($pdf) {
88		// Setup the style name, a font must be selected to calculate the width
89		if ($pdf->getCurrentStyle() != $this->styleName) {
90			$pdf->setCurrentStyle($this->styleName);
91		}
92		// Check for the largest font size in the box
93		$fsize = $pdf->getCurrentStyleHeight();
94		if ($fsize > $pdf->largestFontHeight) {
95			$pdf->largestFontHeight = $fsize;
96		}
97
98		// Get the line width
99		$lw = $pdf->GetStringWidth($this->text);
100		// Line Feed counter - Number of lines in the text
101		$lfct = substr_count($this->text, "\n") + 1;
102		// If there is still remaining wrap width...
103		if ($this->wrapWidthRemaining > 0) {
104			// Check with line counter too!
105			// but floor the $wrapWidthRemaining first to keep it bugfree!
106			$wrapWidthRemaining = (int) ($this->wrapWidthRemaining);
107			if (($lw >= ($wrapWidthRemaining)) or ($lfct > 1)) {
108				$newtext = "";
109				$lines = explode("\n", $this->text);
110				// Go throught the text line by line
111				foreach ($lines as $line) {
112					// Line width in points + a little margin
113					$lw = $pdf->GetStringWidth($line);
114					// If the line has to be wraped
115					if ($lw >= $wrapWidthRemaining) {
116						$words = explode(" ", $line);
117						$addspace = count($words);
118						$lw = 0;
119						foreach ($words as $word) {
120							$addspace--;
121							$lw += $pdf->GetStringWidth($word . " ");
122							if ($lw <= $wrapWidthRemaining) {
123								$newtext .= $word;
124								if ($addspace != 0) {
125									$newtext .= " ";
126								}
127							} else {
128								$lw = $pdf->GetStringWidth($word . " ");
129								$newtext .= "\n$word";
130								if ($addspace != 0) {
131									$newtext .= " ";
132								}
133								// Reset the wrap width to the cell width
134								$wrapWidthRemaining = $this->wrapWidthCell;
135							}
136						}
137					} else {
138						$newtext .= $line;
139					}
140					// Check the Line Feed counter
141					if ($lfct > 1) {
142						// Add a new line as long as it’s not the last line
143						$newtext .= "\n";
144						// Reset the line width
145						$lw = 0;
146						// Reset the wrap width to the cell width
147						$wrapWidthRemaining = $this->wrapWidthCell;
148					}
149					$lfct--;
150				}
151				$this->text = $newtext;
152				$lfct = substr_count($this->text, "\n");
153
154				return array($lw, 1, $lfct);
155			}
156		}
157		$l = 0;
158		$lfct = substr_count($this->text, "\n");
159		if ($lfct > 0) {
160			$l = 2;
161		}
162
163		return array($lw, $l, $lfct);
164	}
165}
166