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