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