Skip to content

Commit

Permalink
Refactor duplicate code in report renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Sep 8, 2021
1 parent 5213572 commit 345fcc8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 209 deletions.
101 changes: 72 additions & 29 deletions app/Report/AbstractRenderer.php
Expand Up @@ -51,68 +51,73 @@ abstract class AbstractRenderer
'US-Tabloid' => [11.0 * self::INCH_TO_POINTS, 17.0 * self::INCH_TO_POINTS],
];

/** @var float Left Margin */
public $left_margin = 18.0 * self::MM_TO_POINTS;
public float $left_margin = 18.0 * self::MM_TO_POINTS;

/** @var float Right Margin */
public $right_margin = 9.9 * self::MM_TO_POINTS;
public float $right_margin = 9.9 * self::MM_TO_POINTS;

/** @var float Top Margin */
public $top_margin = 26.8 * self::MM_TO_POINTS;
public float $top_margin = 26.8 * self::MM_TO_POINTS;

/** @var float Bottom Margin */
public $bottom_margin = 21.6 * self::MM_TO_POINTS;
public float $bottom_margin = 21.6 * self::MM_TO_POINTS;

/** @var float Header Margin */
public $header_margin = 4.9 * self::MM_TO_POINTS;
public float $header_margin = 4.9 * self::MM_TO_POINTS;

/** @var float Footer Margin */
public $footer_margin = 9.9 * self::MM_TO_POINTS;
public float $footer_margin = 9.9 * self::MM_TO_POINTS;

/** @var string Page orientation (portrait, landscape) */
public $orientation = 'portrait';
public string $orientation = 'portrait';

/** @var string Page format name */
public $page_format = 'A4';
public string $page_format = 'A4';

/** @var float Height of page format in points */
public $page_height = 0.0;
public float $page_height = 0.0;

/** @var float Width of page format in points */
public $page_width = 0.0;
public float $page_width = 0.0;

/** @var string[][] An array of the Styles elements found in the document */
public $styles = [];
/** @var array<array<string,string>> An array of the Styles elements found in the document */
public array $styles = [];

/** @var string The default Report font name */
public $default_font = 'dejavusans';
public string $default_font = 'dejavusans';

/** @var float The default Report font size */
public $default_font_size = 12.0;
public float $default_font_size = 12.0;

/** @var string Header (H), Body (B) or Footer (F) */
public $processing = 'H';
public string $processing = 'H';

/** @var bool RTL Language (false=LTR, true=RTL) */
public $rtl = false;
public bool $rtl = false;

/** @var bool Show the Generated by... (true=show the text) */
public $show_generated_by = true;

/** @var string Generated By... text */
public $generated_by = '';
public string $generated_by = '';

/** @var string The report title */
public $title = '';
public string $title = '';

/** @var string Author of the report, the users full name */
public $rauthor = Webtrees::NAME . ' ' . Webtrees::VERSION;
public string $rauthor = Webtrees::NAME . ' ' . Webtrees::VERSION;

/** @var string Keywords */
public $rkeywords = '';
public string $rkeywords = '';

/** @var string Report Description / Subject */
public $rsubject = '';
public string $rsubject = '';

/** @var array<ReportBaseElement|string> */
public array $headerElements = [];

/** @var array<ReportBaseElement|string> */
public $footerElements = [];

/** @var array<ReportBaseElement|string> */
public array $bodyElements = [];

public string $currentStyle = '';

/**
* Clear the Header.
Expand All @@ -121,14 +126,52 @@ abstract class AbstractRenderer
*/
abstract public function clearHeader(): void;


/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElement($element): void
{
if ($this->processing === 'B') {
$this->addElementToBody($element);
} elseif ($this->processing === 'H') {
$this->addElementToHeader($element);
} elseif ($this->processing === 'F') {
$this->addElementToFooter($element);
}
}

/**
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElementToHeader($element): void
{
$this->headerElements[] = $element;
}

/**
* Add an element.
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElementToBody($element): void
{
$this->bodyElements[] = $element;
}

/**
* @param ReportBaseElement|string $element
*
* @return void
*/
abstract public function addElement($element): void;
public function addElementToFooter($element): void
{
$this->footerElements[] = $element;
}

/**
* Run the report.
Expand Down
34 changes: 0 additions & 34 deletions app/Report/HtmlRenderer.php
Expand Up @@ -69,13 +69,6 @@ class HtmlRenderer extends AbstractRenderer
*/
public $Y = 0.0;

/**
* Currently used style name
*
* @var string
*/
public $currentStyle = '';

/**
* Page number counter
*
Expand Down Expand Up @@ -133,15 +126,6 @@ class HtmlRenderer extends AbstractRenderer
*/
public $maxY = 0;

/** @var ReportBaseElement[] Array of elements in the header */
public $headerElements = [];

/** @var ReportBaseElement[] Array of elements in the footer */
public $footerElements = [];

/** @var ReportBaseElement[] Array of elements in the body */
public $bodyElements = [];

/** @var ReportHtmlFootnote[] Array of elements in the footer notes */
public $printedfootnotes = [];

Expand Down Expand Up @@ -179,24 +163,6 @@ public function setup(): void
}
}

/**
* Add an element.
*
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElement($element): void
{
if ($this->processing === 'B') {
$this->bodyElements[] = $element;
} elseif ($this->processing === 'H') {
$this->headerElements[] = $element;
} elseif ($this->processing === 'F') {
$this->footerElements[] = $element;
}
}

/**
* Generate footnotes
*
Expand Down
119 changes: 16 additions & 103 deletions app/Report/PdfRenderer.php
Expand Up @@ -51,42 +51,22 @@ class PdfRenderer extends AbstractRenderer
*/
private const UNICODE = true;

/**
* false means that the full font is embedded, true means only the used chars
* in TCPDF v5.9 font subsetting is a very slow process, this leads to larger files
*
* @var bool const
*/
// Font sub-setting in TCPDF is slow.
private const SUBSETTING = false;

/**
* @var TcpdfWrapper
*/
public $tcpdf;

/** @var ReportBaseElement[] Array of elements in the header */
public $headerElements = [];
public TcpdfWrapper $tcpdf;

/** @var ReportBaseElement[] Array of elements in the footer */
public $footerElements = [];

/** @var ReportBaseElement[] Array of elements in the body */
public $bodyElements = [];

/** @var ReportPdfFootnote[] Array of elements in the footer notes */
public $printedfootnotes = [];

/** @var string Currently used style name */
public $currentStyle = '';
/** @var array<ReportPdfFootnote> Array of elements in the footer notes */
public array $printedfootnotes = [];

/** @var float The last cell height */
public $lastCellHeight = 0;
public float $lastCellHeight = 0.0;

/** @var float The largest font size within a TextBox to calculate the height */
public $largestFontHeight = 0;
public float $largestFontHeight = 0.0;

/** @var int The last pictures page number */
public $lastpicpage = 0;
public int $lastpicpage = 0;

/** @var PdfRenderer The current report. */
public $wt_report;
Expand Down Expand Up @@ -167,42 +147,6 @@ public function footer(): void
}
}

/**
* Add an element to the Header -PDF
*
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addHeader($element): void
{
$this->headerElements[] = $element;
}

/**
* Add an element to the Body -PDF
*
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addBody($element): void
{
$this->bodyElements[] = $element;
}

/**
* Add an element to the Footer -PDF
*
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addFooter($element): void
{
$this->footerElements[] = $element;
}

/**
* Remove the header.
*
Expand Down Expand Up @@ -444,25 +388,22 @@ public function setup(): void
{
parent::setup();

// Setup the PDF class with custom size pages because WT supports more page sizes. If WT sends an unknown size name then the default would be A4
$this->tcpdf = new TcpdfWrapper($this->orientation, parent::UNITS, [
$this->page_width,
$this->page_height,
], self::UNICODE, 'UTF-8', self::DISK_CACHE);
$this->tcpdf = new TcpdfWrapper(
$this->orientation,
self::UNITS,
[$this->page_width, $this->page_height],
self::UNICODE,
'UTF-8',
self::DISK_CACHE
);

// Setup the PDF margins
$this->tcpdf->SetMargins($this->left_margin, $this->top_margin, $this->right_margin);
$this->tcpdf->setHeaderMargin($this->header_margin);
$this->tcpdf->setFooterMargin($this->footer_margin);
//Set auto page breaks
$this->tcpdf->SetAutoPageBreak(true, $this->bottom_margin);
// Set font subsetting
$this->tcpdf->setFontSubsetting(self::SUBSETTING);
// Setup PDF compression
$this->tcpdf->SetCompression(self::COMPRESSION);
// Setup RTL support
$this->tcpdf->setRTL($this->rtl);
// Set the document information
$this->tcpdf->SetCreator(Webtrees::NAME . ' ' . Webtrees::VERSION);
$this->tcpdf->SetAuthor($this->rauthor);
$this->tcpdf->SetTitle($this->title);
Expand All @@ -478,35 +419,7 @@ public function setup(): void
$element = new ReportPdfCell(0, 10, 0, 'C', '', 'genby', 1, ReportBaseElement::CURRENT_POSITION, ReportBaseElement::CURRENT_POSITION, 0, 0, '', '', true);
$element->addText($this->generated_by);
$element->setUrl(Webtrees::URL);
$this->addFooter($element);
}
}

/**
* Add an element.
*
* @param ReportBaseElement|string $element
*
* @return void
*/
public function addElement($element): void
{
if ($this->processing === 'B') {
$this->addBody($element);

return;
}

if ($this->processing === 'H') {
$this->addHeader($element);

return;
}

if ($this->processing === 'F') {
$this->addFooter($element);

return;
$this->addElementToFooter($element);
}
}

Expand Down

0 comments on commit 345fcc8

Please sign in to comment.