xref: /webtrees/app/Report/ReportPdfText.php (revision adc8b18ac5746a1eec1eeed9e4e3f8bf9b0de428)
1<?php
2namespace Fisharebest\Webtrees\Report;
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 ReportTcpdf $renderer
27	 */
28	public function render($renderer) {
29		// Set up the style
30		if ($renderer->getCurrentStyle() != $this->styleName) {
31			$renderer->setCurrentStyle($this->styleName);
32		}
33		$temptext = str_replace("#PAGENUM#", $renderer->PageNo(), $this->text);
34		// underline «title» part of Source item
35		$temptext = str_replace(array('«', '»'), array('<u>', '</u>'), $temptext);
36
37		// Paint the text color or they might use inherited colors by the previous function
38		$match = array();
39		if (preg_match("/#?(..)(..)(..)/", $this->color, $match)) {
40			$r = hexdec($match[1]);
41			$g = hexdec($match[2]);
42			$b = hexdec($match[3]);
43			$renderer->SetTextColor($r, $g, $b);
44		} else {
45			$renderer->SetTextColor(0, 0, 0);
46		}
47		$temptext = spanLTRRTL($temptext, "BOTH");
48		$temptext = str_replace(
49			array('<br><span dir="rtl" >', '<br><span dir="ltr" >', '> ', ' <'),
50			array('<span dir="rtl" ><br>', '<span dir="ltr" ><br>', '>&nbsp;', '&nbsp;<'),
51			$temptext
52		);
53		$renderer->writeHTML(
54			$temptext,
55			false,
56			false,
57			true,
58			false,
59			""
60		); //change height - line break etc. - the form is mirror on rtl pages
61		// Reset the text color to black or it will be inherited
62		$renderer->SetTextColor(0, 0, 0);
63	}
64
65	/**
66	 * Returns the height in points of the text element
67	 *
68	 * The height is already calculated in getWidth()
69	 *
70	 * @param ReportTcpdf $pdf
71	 *
72	 * @return float 0
73	 */
74	public function getHeight($pdf) {
75		return 0;
76	}
77
78	/**
79	 * Splits the text into lines if necessary to fit into a giving cell
80	 *
81	 * @param ReportTcpdf $pdf
82	 *
83	 * @return array
84	 */
85	public function getWidth($pdf) {
86		// Setup the style name, a font must be selected to calculate the width
87		if ($pdf->getCurrentStyle() != $this->styleName) {
88			$pdf->setCurrentStyle($this->styleName);
89		}
90		// Check for the largest font size in the box
91		$fsize = $pdf->getCurrentStyleHeight();
92		if ($fsize > $pdf->largestFontHeight) {
93			$pdf->largestFontHeight = $fsize;
94		}
95
96		// Get the line width
97		$lw = $pdf->GetStringWidth($this->text);
98		// Line Feed counter - Number of lines in the text
99		$lfct = substr_count($this->text, "\n") + 1;
100		// If there is still remaining wrap width...
101		if ($this->wrapWidthRemaining > 0) {
102			// Check with line counter too!
103			// but floor the $wrapWidthRemaining first to keep it bugfree!
104			$wrapWidthRemaining = (int) ($this->wrapWidthRemaining);
105			if ($lw >= $wrapWidthRemaining || $lfct > 1) {
106				$newtext = "";
107				$lines   = explode("\n", $this->text);
108				// Go throught the text line by line
109				foreach ($lines as $line) {
110					// Line width in points + a little margin
111					$lw = $pdf->GetStringWidth($line);
112					// If the line has to be wraped
113					if ($lw >= $wrapWidthRemaining) {
114						$words    = explode(" ", $line);
115						$addspace = count($words);
116						$lw       = 0;
117						foreach ($words as $word) {
118							$addspace--;
119							$lw += $pdf->GetStringWidth($word . " ");
120							if ($lw <= $wrapWidthRemaining) {
121								$newtext .= $word;
122								if ($addspace != 0) {
123									$newtext .= " ";
124								}
125							} else {
126								$lw = $pdf->GetStringWidth($word . " ");
127								$newtext .= "\n$word";
128								if ($addspace != 0) {
129									$newtext .= " ";
130								}
131								// Reset the wrap width to the cell width
132								$wrapWidthRemaining = $this->wrapWidthCell;
133							}
134						}
135					} else {
136						$newtext .= $line;
137					}
138					// Check the Line Feed counter
139					if ($lfct > 1) {
140						// Add a new line as long as it’s not the last line
141						$newtext .= "\n";
142						// Reset the line width
143						$lw = 0;
144						// Reset the wrap width to the cell width
145						$wrapWidthRemaining = $this->wrapWidthCell;
146					}
147					$lfct--;
148				}
149				$this->text = $newtext;
150				$lfct       = substr_count($this->text, "\n");
151
152				return array($lw, 1, $lfct);
153			}
154		}
155		$l    = 0;
156		$lfct = substr_count($this->text, "\n");
157		if ($lfct > 0) {
158			$l = 2;
159		}
160
161		return array($lw, $l, $lfct);
162	}
163}
164