xref: /webtrees/app/Report/ReportHtmlText.php (revision d32a32162970e6bfef5abe5397602a75311438d3)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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
18/**
19 * Class ReportHtmlText
20 */
21class ReportHtmlText extends ReportBaseText {
22	/**
23	 * Render the elements.
24	 *
25	 * @param ReportHtml $renderer
26	 * @param int        $curx
27	 * @param bool       $attrib Is is called from a different element?
28	 */
29	public function render($renderer, $curx = 0, $attrib = true) {
30
31		// Setup the style name
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(['«', '»'], ['<u>', '</u>'], $temptext);
38
39		// If any text at all
40		if (!empty($temptext)) {
41			// If called by an other element
42			if (!$attrib) {
43				$renderer->write($temptext, $this->color);
44			} else {
45				// Save the start positions
46				$startX = $renderer->getX();
47				$startY = $renderer->getY();
48				$width  = $renderer->getRemainingWidth();
49				// If text is wider then page width then wrap it
50				if ($renderer->getStringWidth($temptext) > $width) {
51					$lines = explode("\n", $temptext);
52					foreach ($lines as $line) {
53						echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">';
54						$line = $renderer->textWrap($line, $width);
55						$startY += $renderer->getTextCellHeight($line);
56						$renderer->setY($startY);
57						$renderer->write($line, $this->color);
58						echo "</div>\n";
59					}
60				} else {
61					echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">';
62					$renderer->write($temptext, $this->color);
63					echo "</div>\n";
64					$renderer->setX($startX + $renderer->getStringWidth($temptext));
65					if ($renderer->countLines($temptext) != 1) {
66						$renderer->setXy(0, ($startY + $renderer->getTextCellHeight($temptext)));
67					}
68				}
69			}
70		}
71	}
72
73	/**
74	 * Returns the height in points of the text element
75	 * The height is already calculated in getWidth()
76	 *
77	 * @param ReportHtml $html
78	 *
79	 * @return float
80	 */
81	public function getHeight($html) {
82		$ct = substr_count($this->text, "\n");
83		if ($ct > 0) {
84			$ct += 1;
85		}
86		$style = $html->getStyle($this->styleName);
87
88		return ($style['size'] * $ct) * $html->cellHeightRatio;
89	}
90
91	/**
92	 * Get the width of text and wrap it too
93	 *
94	 * @param ReportHtml $html
95	 *
96	 * @return array
97	 */
98	public function getWidth($html) {
99		// Setup the style name
100		if ($html->getCurrentStyle() != $this->styleName) {
101			$html->setCurrentStyle($this->styleName);
102		}
103
104		// Check for the largest font size in the box
105		$fsize = $html->getCurrentStyleHeight();
106		if ($fsize > $html->largestFontHeight) {
107			$html->largestFontHeight = $fsize;
108		}
109
110		// Get the line width for the text in points
111		$lw = $html->getStringWidth($this->text);
112		// Line Feed counter - Number of lines in the text
113		$lfct = $html->countLines($this->text);
114		// If there is still remaining wrap width...
115		if ($this->wrapWidthRemaining > 0) {
116			// Check with line counter too!
117			if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
118				$newtext            = '';
119				$wrapWidthRemaining = $this->wrapWidthRemaining;
120				$lines              = explode("\n", $this->text);
121				// Go throught the text line by line
122				foreach ($lines as $line) {
123					// Line width in points + a little margin
124					$lw = $html->getStringWidth($line);
125					// If the line has to be wraped
126					if ($lw > $wrapWidthRemaining) {
127						$words    = explode(' ', $line);
128						$addspace = count($words);
129						$lw       = 0;
130						foreach ($words as $word) {
131							$addspace--;
132							$lw += $html->getStringWidth($word . ' ');
133							if ($lw <= $wrapWidthRemaining) {
134								$newtext .= $word;
135								if ($addspace != 0) {
136									$newtext .= ' ';
137								}
138							} else {
139								$lw = $html->getStringWidth($word . ' ');
140								$newtext .= "\n$word";
141								if ($addspace != 0) {
142									$newtext .= ' ';
143								}
144								// Reset the wrap width to the cell width
145								$wrapWidthRemaining = $this->wrapWidthCell;
146							}
147						}
148					} else {
149						$newtext .= $line;
150					}
151					// Check the Line Feed counter
152					if ($lfct > 1) {
153						// Add a new line feed as long as it’s not the last line
154						$newtext .= "\n";
155						// Reset the line width
156						$lw = 0;
157						// Reset the wrap width to the cell width
158						$wrapWidthRemaining = $this->wrapWidthCell;
159					}
160					$lfct--;
161				}
162				$this->text = $newtext;
163				$lfct       = substr_count($this->text, "\n");
164
165				return [$lw, 1, $lfct];
166			}
167		}
168		$l    = 0;
169		$lfct = substr_count($this->text, "\n");
170		if ($lfct > 0) {
171			$l = 2;
172		}
173
174		return [$lw, $l, $lfct];
175	}
176}
177