Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/bench/one.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
exit(2);
}

// Untimed setup a scenario may need, such as writing its fixtures.
$prepare = "bench_prepare_{$scenario}";
if (function_exists($prepare)) {
$prepare();
}

$start = hrtime(true);
$bytes = $fn();
$ms = (hrtime(true) - $start) / 1e6;
Expand Down
8 changes: 5 additions & 3 deletions scripts/bench/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'invoice' => ['label' => '100-page invoice (50 rows/page)'],
'images' => ['label' => 'Image grid (20 pages × 4 JPEGs)'],
'html' => ['label' => 'HTML → PDF article (~5 pages)'],
'svg' => ['label' => 'SVG dashboards (10 pages, ~1,200 shapes each)'],
];

$libs = ['phppdf', 'mpdf', 'tcpdf', 'dompdf', 'fpdf'];
Expand Down Expand Up @@ -244,7 +245,7 @@ function summary_table(array $scenarios, array $results): string
{
$out = "| Scenario | dskripchenko/php-pdf | mpdf | tcpdf | dompdf | FPDF |\n";
$out .= "|---|---:|---:|---:|---:|---:|\n";
foreach (['html', 'invoice', 'images', 'hello'] as $key) {
foreach (['html', 'invoice', 'images', 'svg', 'hello'] as $key) {
if (! isset($scenarios[$key])) {
continue;
}
Expand Down Expand Up @@ -322,9 +323,10 @@ function emit_benchmarks_doc(array $scenarios, array $results, array $meta): str
| `invoice` | 100 pages × 50 rows; each page has an `<h1>` heading and a 4-column table. |
| `images` | 20 pages × 4 JPEG thumbnails (250 × 180 pt each). |
| `html` | One `<h1>`, two `<h2>` sections, 24 paragraphs of Lorem with bold/italic/link plus bullet lists. ~5 pages. |
| `svg` | 10 pages, each a different metrics-dashboard SVG: 12 panels of line, bar, scatter and area charts with gridlines and labels, ~1,200 shapes. |

FPDF has no HTML support and is therefore skipped on the `html`
scenario.
FPDF has no HTML or SVG support and is therefore skipped on the
`html` and `svg` scenarios.

## How to reproduce

Expand Down
175 changes: 175 additions & 0 deletions scripts/bench/scenarios.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,178 @@ function bench_article_html(): string
. '<h2>Section B</h2>' . $body
. '</body></html>';
}

// ---------- SVG dashboards ----------
//
// Ten metrics dashboards, one per page: a 4 × 3 grid of panels with
// gridlines and tick labels, holding two-series line charts (300 points
// each), 48-bar charts, 250-point translucent scatters and filled area
// paths — about 1,200 shapes per page. Each page is a different file, so a
// library that caches images by name still draws all ten. Every library
// reads the files inside the timed run. FPDF has no SVG.

const BENCH_SVG_PAGES = 10;
const BENCH_SVG_WIDTH_PT = 523.0;
const BENCH_SVG_HEIGHT_PT = 523.0 * 800 / 1200;

function bench_prepare_svg(): void
{
$dir = bench_svg_dir();
if (! is_dir($dir)) {
mkdir($dir, 0777, true);
}
foreach (bench_svg_files() as $i => $file) {
if (! is_file($file)) {
file_put_contents($file, bench_svg_dashboard($i + 1));
}
}
}

function bench_svg_dir(): string
{
// The version keeps files of an older generator from being reused.
return sys_get_temp_dir().'/php-pdf-bench-svg-v1';
}

/** @return list<string> */
function bench_svg_files(): array
{
$files = [];
for ($p = 1; $p <= BENCH_SVG_PAGES; $p++) {
$files[] = bench_svg_dir()."/dashboard-$p.svg";
}

return $files;
}

function bench_svg_dashboard(int $seed): string
{
mt_srand($seed);
$W = 1200; $H = 800; $cols = 4; $rows = 3; $pad = 12;
$pw = ($W - $pad * ($cols + 1)) / $cols;
$ph = ($H - $pad * ($rows + 1)) / $rows;
$f = fn (float $v): string => rtrim(rtrim(sprintf('%.2f', $v), '0'), '.');
$o = [sprintf('<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d">', $W, $H, $W, $H)];
$o[] = sprintf('<rect x="0" y="0" width="%d" height="%d" fill="#f4f5f7"/>', $W, $H);
$kinds = ['line', 'bar', 'scatter', 'area'];
for ($r = 0; $r < $rows; $r++) {
for ($c = 0; $c < $cols; $c++) {
$kind = $kinds[($r * $cols + $c) % 4];
$o[] = sprintf('<g transform="translate(%s,%s)">', $f($pad + $c * ($pw + $pad)), $f($pad + $r * ($ph + $pad)));
$o[] = sprintf('<rect x="0" y="0" width="%s" height="%s" rx="4" fill="#ffffff" stroke="#d0d4da" stroke-width="1"/>', $f($pw), $f($ph));
$o[] = sprintf('<text x="10" y="18" font-family="Helvetica" font-size="11" fill="#222222">%s metric %d</text>', ucfirst($kind), $r * $cols + $c + 1);
$cx0 = 34; $cy0 = 28; $cw = $pw - 44; $ch = $ph - 48;
for ($g = 0; $g <= 5; $g++) {
$gy = $cy0 + $ch - $g * $ch / 5;
$o[] = sprintf('<line x1="%s" y1="%s" x2="%s" y2="%s" stroke="#e5e7eb" stroke-width="0.5"/>', $cx0, $f($gy), $f($cx0 + $cw), $f($gy));
$o[] = sprintf('<text x="%s" y="%s" font-family="Helvetica" font-size="7" fill="#6b7280" text-anchor="end">%d</text>', $cx0 - 4, $f($gy + 2.5), $g * 20);
}
for ($t = 0; $t <= 6; $t++) {
$o[] = sprintf('<text x="%s" y="%s" font-family="Helvetica" font-size="7" fill="#6b7280" text-anchor="middle">%02d:00</text>', $f($cx0 + $t * $cw / 6), $f($cy0 + $ch + 11), $t * 4);
}
$sy = fn (float $v): float => $cy0 + $ch - $v * $ch;
switch ($kind) {
case 'line':
foreach (['#2563eb', '#dc2626'] as $color) {
$v = mt_rand(30, 70) / 100;
$pts = [];
for ($i = 0; $i < 300; $i++) {
$v = max(0.02, min(0.98, $v + mt_rand(-100, 100) / 2500));
$pts[] = $f($cx0 + $i * $cw / 299).','.$f($sy($v));
}
$o[] = sprintf('<polyline fill="none" stroke="%s" stroke-width="1" points="%s"/>', $color, implode(' ', $pts));
}
break;
case 'bar':
$bw = $cw / 48;
for ($i = 0; $i < 48; $i++) {
$v = mt_rand(5, 95) / 100;
$o[] = sprintf('<rect x="%s" y="%s" width="%s" height="%s" fill="#10b981"/>', $f($cx0 + $i * $bw + 0.5), $f($sy($v)), $f($bw - 1), $f($v * $ch));
}
break;
case 'scatter':
for ($i = 0; $i < 250; $i++) {
$a = mt_rand(0, 1000) / 1000;
$b = max(0.02, min(0.98, $a * 0.8 + mt_rand(-150, 150) / 1000 + 0.1));
$o[] = sprintf('<circle cx="%s" cy="%s" r="1.8" fill="#7c3aed" fill-opacity="0.6"/>', $f($cx0 + $a * $cw), $f($sy($b)));
}
break;
case 'area':
$v = 0.4;
$area = sprintf('M%s,%s', $cx0, $f($cy0 + $ch));
$line = '';
for ($i = 0; $i < 200; $i++) {
$v = max(0.05, min(0.95, $v + mt_rand(-100, 100) / 2000));
$pt = $f($cx0 + $i * $cw / 199).','.$f($sy($v));
$area .= ' L'.$pt;
$line .= ($i === 0 ? 'M' : ' L').$pt;
}
$area .= sprintf(' L%s,%s Z', $f($cx0 + $cw), $f($cy0 + $ch));
$o[] = sprintf('<path d="%s" fill="#f59e0b" fill-opacity="0.35" stroke="none"/>', $area);
$o[] = sprintf('<path d="%s" fill="none" stroke="#d97706" stroke-width="1"/>', $line);
break;
}
$o[] = '</g>';
}
}
$o[] = '</svg>';

return implode("\n", $o)."\n";
}

function bench_phppdf_svg(): string
{
$b = DocumentBuilder::new();
foreach (bench_svg_files() as $i => $file) {
if ($i > 0) {
$b->pageBreak();
}
$b->block(new \Dskripchenko\PhpPdf\Element\SvgElement(
(string) file_get_contents($file), BENCH_SVG_WIDTH_PT, BENCH_SVG_HEIGHT_PT,
));
}

return $b->toBytes();
}

function bench_mpdf_svg(): string
{
$pdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
foreach (bench_svg_files() as $i => $file) {
if ($i > 0) {
$pdf->AddPage();
}
$pdf->WriteHTML(sprintf('<img src="%s" style="width:%spt;height:%spt"/>', $file, BENCH_SVG_WIDTH_PT, BENCH_SVG_HEIGHT_PT));
}

return $pdf->Output('', 'S');
}

function bench_tcpdf_svg(): string
{
$pdf = new \TCPDF('P', 'pt', 'A4', true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
foreach (bench_svg_files() as $file) {
$pdf->AddPage();
$pdf->ImageSVG('@'.file_get_contents($file), 36, 36, BENCH_SVG_WIDTH_PT, BENCH_SVG_HEIGHT_PT);
}

return $pdf->Output('', 'S');
}

function bench_dompdf_svg(): string
{
$pdf = new \Dompdf\Dompdf(['chroot' => bench_svg_dir(), 'isRemoteEnabled' => false]);
$html = '<html><body>';
foreach (bench_svg_files() as $i => $file) {
if ($i > 0) {
$html .= '<div style="page-break-after: always;"></div>';
}
$html .= sprintf('<img src="%s" style="width:%spt;height:%spt"/>', $file, BENCH_SVG_WIDTH_PT, BENCH_SVG_HEIGHT_PT);
}
$pdf->loadHtml($html.'</body></html>');
$pdf->render();

return $pdf->output();
}
2 changes: 1 addition & 1 deletion src/Build/CellBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class CellBuilder

public function __construct()
{
$this->style = new CellStyle;
$this->style = CellStyle::default();
}

public static function new(): self
Expand Down
6 changes: 3 additions & 3 deletions src/Build/ParagraphBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class ParagraphBuilder

public function __construct()
{
$this->style = new ParagraphStyle;
$this->defaultRunStyle = new RunStyle;
$this->style = ParagraphStyle::default();
$this->defaultRunStyle = RunStyle::default();
}

public static function new(): self
Expand All @@ -53,7 +53,7 @@ public static function new(): self

public function text(string $text, ?RunStyle $style = null): self
{
$this->children[] = new Run($text, $style ?? new RunStyle);
$this->children[] = new Run($text, $style);

return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Element/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
final readonly class Cell
{
public CellStyle $style;

/**
* @param list<BlockElement> $children
*/
public function __construct(
public array $children = [],
public CellStyle $style = new CellStyle,
?CellStyle $style = null,
public int $columnSpan = 1,
public int $rowSpan = 1,
) {
$this->style = $style ?? CellStyle::default();
if ($columnSpan < 1) {
throw new \InvalidArgumentException("columnSpan must be ≥ 1, got $columnSpan");
}
Expand Down
18 changes: 11 additions & 7 deletions src/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,36 @@

public const MERGEFIELD = 'MERGEFIELD';

public RunStyle $style;

public function __construct(
public string $instruction,
public RunStyle $style = new RunStyle,
) {}
?RunStyle $style = null,
) {
$this->style = $style ?? RunStyle::default();
}

public static function page(RunStyle $style = new RunStyle): self
public static function page(?RunStyle $style = null): self
{
return new self(self::PAGE, $style);
}

public static function totalPages(RunStyle $style = new RunStyle): self
public static function totalPages(?RunStyle $style = null): self
{
return new self(self::NUMPAGES, $style);
}

public static function date(string $format = 'dd.MM.yyyy', RunStyle $style = new RunStyle): self
public static function date(string $format = 'dd.MM.yyyy', ?RunStyle $style = null): self
{
return new self(self::DATE.':'.$format, $style);
}

public static function time(string $format = 'HH:mm', RunStyle $style = new RunStyle): self
public static function time(string $format = 'HH:mm', ?RunStyle $style = null): self
{
return new self(self::TIME.':'.$format, $style);
}

public static function mergeField(string $name, RunStyle $style = new RunStyle): self
public static function mergeField(string $name, ?RunStyle $style = null): self
{
return new self(self::MERGEFIELD.':'.$name, $style);
}
Expand Down
13 changes: 10 additions & 3 deletions src/Element/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@
*/
final readonly class Paragraph implements BlockElement
{
public ParagraphStyle $style;

public RunStyle $defaultRunStyle;

/**
* @param list<InlineElement> $children
*/
public function __construct(
public array $children = [],
public ParagraphStyle $style = new ParagraphStyle,
?ParagraphStyle $style = null,
public ?int $headingLevel = null,
public RunStyle $defaultRunStyle = new RunStyle,
) {}
?RunStyle $defaultRunStyle = null,
) {
$this->style = $style ?? ParagraphStyle::default();
$this->defaultRunStyle = $defaultRunStyle ?? RunStyle::default();
}

public function isEmpty(): bool
{
Expand Down
8 changes: 6 additions & 2 deletions src/Element/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
final readonly class Run implements InlineElement
{
public RunStyle $style;

public function __construct(
public string $text,
public RunStyle $style = new RunStyle,
) {}
?RunStyle $style = null,
) {
$this->style = $style ?? RunStyle::default();
}
}
19 changes: 19 additions & 0 deletions src/Font/Ttf/KerningTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ public function lookup(int $leftGid, int $rightGid): int
return $this->memo[$leftGid][$rightGid] ??= $this->find($leftGid, $rightGid);
}

/**
* lookup() for each neighbouring pair of a glyph run: element $i is the
* adjustment between $gids[$i - 1] and $gids[$i] (element 0 is 0).
*
* @param list<int> $gids
* @return list<int>
*/
public function lookupRun(array $gids): array
{
$out = [];
$prev = null;
foreach ($gids as $gid) {
$out[] = $prev === null ? 0 : ($this->memo[$prev][$gid] ??= $this->find($prev, $gid));
$prev = $gid;
}

return $out;
}

private function find(int $leftGid, int $rightGid): int
{
for ($i = count($this->layers) - 1; $i >= 0; $i--) {
Expand Down
Loading
Loading