xref: /webtrees/app/Report/ReportPdfCell.php (revision a25f0a04682c4c39c1947220c90af4118c713952)
1<?php
2namespace 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 ReportPdfCell
21 */
22class ReportPdfCell extends ReportBaseCell {
23	/**
24	 * PDF Cell renderer
25	 *
26	 * @param PDF $renderer
27	 *
28	 * @return void
29	 */
30	function render($renderer) {
31
32		// Set up the text style
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		$match = array();
40		// Indicates if the cell background must be painted (1) or transparent (0)
41		if ($this->fill == 1) {
42			if (!empty($this->bgcolor)) {
43				// HTML color to RGB
44				if (preg_match("/#?(..)(..)(..)/", $this->bgcolor, $match)) {
45					$r = hexdec($match[1]);
46					$g = hexdec($match[2]);
47					$b = hexdec($match[3]);
48					$renderer->SetFillColor($r, $g, $b);
49				}
50			} // If no color set then don't fill
51			else {
52				$this->fill = 0;
53			}
54		}
55		// Paint the Border color if set
56		if (!empty($this->bocolor)) {
57			// HTML color to RGB
58			if (preg_match("/#?(..)(..)(..)/", $this->bocolor, $match)) {
59				$r = hexdec($match[1]);
60				$g = hexdec($match[2]);
61				$b = hexdec($match[3]);
62				$renderer->SetDrawColor($r, $g, $b);
63			}
64		}
65		// Paint the text color or they might use inherited colors by the previous function
66		if (preg_match("/#?(..)(..)(..)/", $this->tcolor, $match)) {
67			$r = hexdec($match[1]);
68			$g = hexdec($match[2]);
69			$b = hexdec($match[3]);
70			$renderer->SetTextColor($r, $g, $b);
71		} else {
72			$renderer->SetTextColor(0, 0, 0);
73		}
74
75		// If current position (left)
76		if ($this->left == ".") {
77			$cX = $renderer->GetX();
78		} // For static position add margin (also updates X)
79		else {
80			$cX = $renderer->addMarginX($this->left);
81		}
82
83		// Check the width if set to page wide OR set by xml to larger then page wide
84		if ($this->width == 0 || $this->width > $renderer->getRemainingWidthPDF()) {
85			$this->width = $renderer->getRemainingWidthPDF();
86		}
87		// For current position
88		if ($this->top == ".") {
89			$this->top = $renderer->GetY();
90		} else {
91			$renderer->SetY($this->top);
92		}
93
94		// Check the last cell height and adjust the current cell height if needed
95		if ($renderer->lastCellHeight > $this->height) {
96			$this->height = $renderer->lastCellHeight;
97		}
98		// Check for pagebreak
99		if (!empty($temptext)) {
100			$cHT = $renderer->getNumLines($temptext, $this->width);
101			$cHT = $cHT * $renderer->getCellHeightRatio() * $renderer->getCurrentStyleHeight();
102			$cM = $renderer->getMargins();
103			// Add padding
104			if (is_array($cM['cell'])) {
105				$cHT += ($cM['padding_bottom'] + $cM['padding_top']);
106			} else {
107				$cHT += ($cM['cell'] * 2);
108			}
109			// Add a new page if needed
110			if ($renderer->checkPageBreakPDF($cHT)) {
111				$this->top = $renderer->GetY();
112			}
113			$temptext = spanLTRRTL($temptext, "BOTH");
114		}
115		// HTML ready - last value is true
116		$renderer->MultiCell(
117			$this->width,
118			$this->height,
119			$temptext,
120			$this->border,
121			$this->align,
122			$this->fill,
123			$this->newline,
124			$cX,
125			$this->top,
126			$this->reseth,
127			$this->stretch,
128			true
129		);
130		// Reset the last cell height for the next line
131		if ($this->newline >= 1) {
132			$renderer->lastCellHeight = 0;
133		} // OR save the last height if heigher then before
134		elseif ($renderer->lastCellHeight < $renderer->getLastH()) {
135			$renderer->lastCellHeight = $renderer->getLastH();
136		}
137
138		// Set up the url link if exists ontop of the cell
139		if (!empty($this->url)) {
140			$renderer->Link($cX, $this->top, $this->width, $this->height, $this->url);
141		}
142		// Reset the border and the text color to black or they will be inherited
143		$renderer->SetDrawColor(0, 0, 0);
144		$renderer->SetTextColor(0, 0, 0);
145	}
146}
147