xref: /webtrees/app/Report/ReportHtmlText.php (revision 1e71bdc0ba6fc5add8fed9a3beb51cfca09e47dd)
1<?php
2namespace Fisharebest\Webtrees;
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 ReportHtmlText
21 */
22class ReportHtmlText extends ReportBaseText {
23	/**
24	 * @param ReportHtml $renderer
25	 * @param integer        $curx
26	 * @param boolean        $attrib Is is called from a different element?
27	 *
28	 * @return void
29	 */
30	function render($renderer, $curx = 0, $attrib = true) {
31
32		// Setup the style name
33		if ($renderer->getCurrentStyle() != $this->styleName) {
34			$renderer->setCurrentStyle($this->styleName);
35		}
36		$temptext = str_replace("#PAGENUM#", $renderer->pageNo(), $this->text);
37		// underline «title» part of Source item
38		$temptext = str_replace(array('«', '»'), array('<u>', '</u>'), $temptext);
39
40		// If any text at all
41		if (!empty($temptext)) {
42			// If called by an other element
43			if (!$attrib) {
44				$renderer->write($temptext, $this->color);
45			} else {
46				// Save the start positions
47				$startX = $renderer->getX();
48				$startY = $renderer->getY();
49				$width = $renderer->getRemainingWidth();
50				// If text is wider then page width then wrap it
51				if ($renderer->GetStringWidth($temptext) > $width) {
52					$lines = explode("\n", $temptext);
53					foreach ($lines as $line) {
54						echo "<div style=\"position:absolute;top:", $startY, "pt;", $renderer->alignRTL, ":", $startX, "pt;width:", $width, "pt;\">";
55						$line = $renderer->textWrap($line, $width);
56						$startY += $renderer->getTextCellHeight($line);
57						$renderer->setY($startY);
58						$renderer->write($line, $this->color);
59						echo "</div>\n";
60					}
61				} else {
62					echo "<div style=\"position:absolute;top:", $startY, "pt;", $renderer->alignRTL, ":", $startX, "pt;width:", $width, "pt;\">";
63					$renderer->write($temptext, $this->color);
64					echo "</div>\n";
65					$renderer->setX($startX + $renderer->GetStringWidth($temptext));
66					if ($renderer->countLines($temptext) != 1) {
67						$renderer->setXy(0, ($startY + $renderer->getTextCellHeight($temptext)));
68					}
69				}
70			}
71		}
72	}
73
74	/**
75	 * Returns the height in points of the text element
76	 * The height is already calculated in getWidth()
77	 *
78	 * @param ReportHtml $html
79	 *
80	 * @return float
81	 */
82	function getHeight($html) {
83		$ct = substr_count($this->text, "\n");
84		if ($ct > 0) {
85			$ct += 1;
86		}
87		$style = $html->getStyle($this->styleName);
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	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) or ($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				return array($lw, 1, $lfct);
165			}
166		}
167		$l = 0;
168		$lfct = substr_count($this->text, "\n");
169		if ($lfct > 0) {
170			$l = 2;
171		}
172		return array($lw, $l, $lfct);
173	}
174}
175