Skip to content
Merged
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
21 changes: 15 additions & 6 deletions src/Writer/BackgroundWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,27 @@ public function writePatterns() // _putpatterns
$this->writer->write('/Font <<');

foreach ($this->mpdf->fonts as $font) {
if (!$font['used'] && $font['type'] === 'TTF') {
// The 'used' and 'type' keys are only reachable once a font is flagged, as a
// core font that has drawn nothing yet carries neither
if (!isset($font['fo']) || !$font['fo']) {
continue;
}
if (isset($font['fo']) && $font['fo']) {
if ($font['type'] === 'TTF' && ($font['sip'] || $font['smp'])) {

if (isset($font['type']) && $font['type'] === 'TTF') {
if (!$font['used']) {
continue;
}

if ($font['sip'] || $font['smp']) {
foreach ($font['n'] as $k => $fid) {
$this->writer->write('/F' . $font['subsetfontids'][$k] . ' ' . $font['n'][$k] . ' 0 R');
$this->writer->write('/F' . $font['subsetfontids'][$k] . ' ' . $fid . ' 0 R');
}
} else {
$this->writer->write('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');

continue;
}
}

$this->writer->write('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
}
$this->writer->write('>>');
} else {
Expand Down
72 changes: 72 additions & 0 deletions tests/Mpdf/BackgroundPatternResourcesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Mpdf;

class BackgroundPatternResourcesTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
{

const SVG = __DIR__ . '/../data/img/pattern-with-text.svg';

/**
* The page draws no text of its own, so its core font is registered but never marked used
* and carries no 'used' key at all. Reading that key before checking 'fo' is a warning,
* which PHPUnit turns into a test error.
*/
public function testPatternIsWrittenWithAnUnusedCoreFont()
{
$output = $this->render();

$this->assertStringContainsString(
'/PatternType 1',
$output,
'Expected the repeating SVG background to be written as a tiling pattern'
);
}

/**
* The pattern's resource dictionary still has to name the font its Form XObject draws with.
*/
public function testPatternResourcesNameTheSvgFont()
{
$output = $this->render();

$this->assertSame(1, preg_match('#/PatternType 1.*?/Resources (\d+) 0 R#s', $output, $pattern));

$this->assertMatchesRegularExpression(
'#/Font\s*<<\s*/F\d+ [1-9]\d* 0 R#',
$this->object($output, $pattern[1])
);
}

/**
* @param string $pdf
* @param string $number
* @return string
*/
private function object($pdf, $number)
{
preg_match('/(?:^|\s)' . $number . ' 0 obj\s*(.*?)endobj/s', $pdf, $matches);

return isset($matches[1]) ? $matches[1] : '';
}

/**
* @return string
*/
private function render()
{
$mpdf = new Mpdf(['mode' => 'c']);
$mpdf->compress = false;

$mpdf->WriteHTML(
'<div style="background-image: url(\'' . self::SVG . '\');'
. ' background-repeat: repeat; height: 40mm"></div>'
);

$output = $mpdf->OutputBinaryData();
$mpdf->cleanup();

return $output;
}

}
4 changes: 4 additions & 0 deletions tests/data/img/pattern-with-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading