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