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