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