Skip to content

Commit 864533b

Browse files
authored
Paint SVG gradient stops opaque under PDF/A-1b, and write only the visibility groups the catalog lists (#346, #351) (#386)
1 parent fe769ef commit 864533b

9 files changed

Lines changed: 192 additions & 24 deletions

‎src/Color/ColorSpaceRestrictor.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ private function restrictRgbaColorSpace($c, $color, &$PDFAXwarnings = [])
165165
}
166166
$c = $this->withAllowedAlpha($this->colorModeConverter->rgb2cmyk($c));
167167
} elseif ($this->mpdf->PDFA && $this->mpdf->restrictColorSpace != 3) {
168-
if (!$this->mpdf->PDFAauto && !$this->mpdf->transparencyAllowed()) {
168+
// An opaque rgba() loses nothing by dropping its alpha
169+
if (!$this->mpdf->PDFAauto && !$this->mpdf->transparencyAllowed() && $c[4] < 100) {
169170
$PDFAXwarnings[] = "RGB color with transparency specified '" . $color . "' (converted to RGB without transparency)";
170171
}
171172
$c = $this->withAllowedAlpha($c);
@@ -193,7 +194,7 @@ private function restrictCmykaColorSpace($c, $color, &$PDFAXwarnings = [])
193194
}
194195
$c = $this->withAllowedAlpha($this->colorModeConverter->cmyk2rgb($c));
195196
} elseif ($this->mpdf->PDFX || ($this->mpdf->PDFA && $this->mpdf->restrictColorSpace == 3)) {
196-
if (!$this->mpdf->transparencyAllowed() && (($this->mpdf->PDFA && !$this->mpdf->PDFAauto) || ($this->mpdf->PDFX && !$this->mpdf->PDFXauto))) {
197+
if (!$this->mpdf->transparencyAllowed() && $c[5] < 100 && (($this->mpdf->PDFA && !$this->mpdf->PDFAauto) || ($this->mpdf->PDFX && !$this->mpdf->PDFXauto))) {
197198
$PDFAXwarnings[] = "CMYK color with transparency specified '" . $color . "' (converted to CMYK without transparency)";
198199
}
199200
$c = $this->withAllowedAlpha($c);

‎src/Image/Svg.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3578,7 +3578,7 @@ function xml_svg2pdf_start($parser, $name, $attribs)
35783578
$tmp_color = [
35793579
'color' => $color_final,
35803580
'offset' => (isset($attribs['offset']) ? $attribs['offset'] : ''),
3581-
'opacity' => $stop_opacity
3581+
'opacity' => $this->mpdf->allowedAlpha($stop_opacity)
35823582
];
35833583
array_push($this->svg_gradient[$last_gradid]['color'], $tmp_color);
35843584

‎src/Writer/MetadataWriter.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,15 @@ public function writeCatalog() //_putcatalog
489489

490490
if ($this->mpdf->hasOC) {
491491

492-
if (($this->mpdf->hasOC & 1) === 1) {
492+
if ($this->mpdf->n_ocg_print) {
493493
$p = $this->mpdf->n_ocg_print . ' 0 R';
494494
}
495495

496-
if (($this->mpdf->hasOC & 2) === 2) {
496+
if ($this->mpdf->n_ocg_view) {
497497
$v = $this->mpdf->n_ocg_view . ' 0 R';
498498
}
499499

500-
if (($this->mpdf->hasOC & 4) === 4) {
500+
if ($this->mpdf->n_ocg_hidden) {
501501
$h = $this->mpdf->n_ocg_hidden . ' 0 R';
502502
}
503503

‎src/Writer/OptionalContentWriter.php‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ final class OptionalContentWriter
1010

1111
use Strict;
1212

13+
/**
14+
* The groups the visibility property draws in, by resource name: the bit of Mpdf::$hasOC that marks each one used,
15+
* the property holding its object number, its name, and its print and view states
16+
*/
17+
const VISIBILITY_GROUPS = [
18+
'OC1' => [1, 'n_ocg_print', 'Print only', 'ON', 'OFF'],
19+
'OC2' => [2, 'n_ocg_view', 'Screen only', 'OFF', 'ON'],
20+
'OC3' => [4, 'n_ocg_hidden', 'Hidden', 'OFF', 'OFF'],
21+
];
22+
1323
/**
1424
* @var \Mpdf\Mpdf
1525
*/
@@ -28,24 +38,19 @@ public function __construct(Mpdf $mpdf, BaseWriter $writer)
2838

2939
public function writeOptionalContentGroups() // _putocg Optional Content Groups
3040
{
31-
if ($this->mpdf->hasOC) {
41+
foreach (self::VISIBILITY_GROUPS as $group) {
42+
list($bit, $property, $name, $print, $view) = $group;
3243

33-
$this->writer->object();
34-
$this->mpdf->n_ocg_print = $this->mpdf->n;
35-
$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Print only'));
36-
$this->writer->write('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>>');
37-
$this->writer->write('endobj');
38-
39-
$this->writer->object();
40-
$this->mpdf->n_ocg_view = $this->mpdf->n;
41-
$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Screen only'));
42-
$this->writer->write('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>>');
43-
$this->writer->write('endobj');
44+
// Only the groups the document uses are written, as the catalog lists only those
45+
$this->mpdf->$property = null;
46+
if (!($this->mpdf->hasOC & $bit)) {
47+
continue;
48+
}
4449

4550
$this->writer->object();
46-
$this->mpdf->n_ocg_hidden = $this->mpdf->n;
47-
$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Hidden'));
48-
$this->writer->write('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /OFF>>>>>>');
51+
$this->mpdf->$property = $this->mpdf->n;
52+
$this->writer->write('<</Type /OCG /Name ' . $this->writer->string($name));
53+
$this->writer->write('/Usage <</Print <</PrintState /' . $print . '>> /View <</ViewState /' . $view . '>>>>>>');
4954
$this->writer->write('endobj');
5055
}
5156

@@ -68,4 +73,21 @@ public function writeOptionalContentGroups() // _putocg Optional Content Groups
6873
}
6974
}
7075

76+
/**
77+
* The page resource entries naming each visibility group written
78+
*
79+
* @return string
80+
*/
81+
public function visibilityProperties()
82+
{
83+
$properties = '';
84+
foreach (self::VISIBILITY_GROUPS as $resource => $group) {
85+
if ($this->mpdf->{$group[1]}) {
86+
$properties .= '/' . $resource . ' ' . $this->mpdf->{$group[1]} . ' 0 R ';
87+
}
88+
}
89+
90+
return $properties;
91+
}
92+
7193
}

‎src/Writer/ResourceWriter.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ public function writeResources() // _putresources
214214

215215
if ($this->mpdf->hasOC || count($this->mpdf->layers)) {
216216
$this->writer->write('/Properties <<');
217-
if ($this->mpdf->hasOC) {
218-
$this->writer->write('/OC1 ' . $this->mpdf->n_ocg_print . ' 0 R /OC2 ' . $this->mpdf->n_ocg_view . ' 0 R /OC3 ' . $this->mpdf->n_ocg_hidden . ' 0 R ');
219-
}
217+
$this->writer->write($this->optionalContentWriter->visibilityProperties());
220218
if (count($this->mpdf->layers)) {
221219
foreach ($this->mpdf->layers as $id => $layer) {
222220
$this->writer->write('/ZI' . $id . ' ' . $layer['n'] . ' 0 R');

‎tests/Mpdf/Color/ColorSpaceRestrictorTest.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,36 @@ public function testPdfa2ReportsTheColorSpaceItChanges()
6161
$this->assertSame(["CMYK color with transparency specified 'cmyka(0, 100, 100, 0, 0.5)' (converted to RGB)"], $warnings);
6262
}
6363

64+
/**
65+
* Without PDFAauto, PDF/A-1 reports the alpha it drops from a translucent colour, but not from an opaque one
66+
*
67+
* @dataProvider opaqueAlphaColors
68+
*/
69+
public function testOpaqueAlphaIsNotReported($config, $opaque, $translucent)
70+
{
71+
$mpdf = new Mpdf($config + ['PDFA' => true, 'PDFAversion' => '1-B']);
72+
$restrictor = new ColorSpaceRestrictor($mpdf, new ColorModeConverter());
73+
74+
$warnings = [];
75+
$restrictor->restrictColorSpace($opaque, 'opaque', $warnings);
76+
$this->assertSame([], $warnings);
77+
78+
$restrictor->restrictColorSpace($translucent, 'translucent', $warnings);
79+
$this->assertCount(1, $warnings);
80+
}
81+
82+
/**
83+
* Configurations with an opaque and a translucent colour in the colour space each keeps: rgba() for an RGB output
84+
* intent, cmyka() for a CMYK one
85+
*
86+
* @return mixed[][]
87+
*/
88+
public function opaqueAlphaColors()
89+
{
90+
return [
91+
'rgba()' => [[], [5, 255, 0, 0, 100], [5, 255, 0, 0, 50]],
92+
'cmyka()' => [['restrictColorSpace' => 3], [6, 0, 100, 100, 0, 100], [6, 0, 100, 100, 0, 50]],
93+
];
94+
}
95+
6496
}

‎tests/Mpdf/PDFA1Test.php‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,69 @@ public function testSvgOpacityIsReported()
3838
$this->assertContains('Image opacity must be 100% (Opacity changed to 100%)', $mpdf->PDFAXwarnings);
3939
}
4040

41+
/**
42+
* An SVG gradient stop at part opacity fades through a soft mask where transparency is allowed, and is painted
43+
* opaque under PDF/A-1 and PDF/X-1a
44+
*
45+
* @dataProvider gradientDocuments
46+
*/
47+
public function testSvgGradientStopOpacity($config, $gradient, $translucent)
48+
{
49+
$pdf = $this->render($this->translucentGradient($gradient), $config + ['mode' => '']);
50+
51+
$this->assertSame($translucent, strpos($pdf, '/SMask') !== false);
52+
}
53+
54+
/**
55+
* A PDF/A-1 document that is not to fix itself reports the gradient stop opacity it could not keep
56+
*
57+
* @dataProvider gradients
58+
*/
59+
public function testSvgGradientStopOpacityIsReported($gradient)
60+
{
61+
$mpdf = $this->pdfa('1-B', ['PDFAauto' => false]);
62+
$mpdf->WriteHTML($this->translucentGradient($gradient));
63+
64+
$this->assertContains('Image opacity must be 100% (Opacity changed to 100%)', $mpdf->PDFAXwarnings);
65+
}
66+
67+
/**
68+
* Each SVG gradient under PDF/A-1b, PDF/A-2b and PDF/X-1a, with whether its stop keeps its opacity
69+
*
70+
* @return mixed[][]
71+
*/
72+
public function gradientDocuments()
73+
{
74+
$documents = [
75+
'PDF/A-1b' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '1-B'], false],
76+
'PDF/A-2b' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B'], true],
77+
'PDF/X-1a' => [['PDFX' => true, 'PDFXauto' => true], false],
78+
];
79+
80+
$cases = [];
81+
foreach ($documents as $document => $case) {
82+
list($config, $translucent) = $case;
83+
foreach ($this->gradients() as $gradient => $args) {
84+
$cases[$document . ', ' . $gradient] = [$config, $args[0], $translucent];
85+
}
86+
}
87+
88+
return $cases;
89+
}
90+
91+
/**
92+
* The SVG gradient elements
93+
*
94+
* @return string[][]
95+
*/
96+
public function gradients()
97+
{
98+
return [
99+
'linear' => ['linearGradient'],
100+
'radial' => ['radialGradient'],
101+
];
102+
}
103+
41104
/**
42105
* A blurred box-shadow fades out through a soft mask under PDF/A-2, and under PDF/A-1 keeps a hard edge instead
43106
*
@@ -165,6 +228,19 @@ private function translucentSvg()
165228
. ' stroke-opacity="0.4" opacity="0.5" /></svg>';
166229
}
167230

231+
/**
232+
* An SVG shape filled with a gradient whose first stop is at part opacity
233+
*
234+
* @param string $gradient linearGradient or radialGradient
235+
*
236+
* @return string
237+
*/
238+
private function translucentGradient($gradient)
239+
{
240+
return '<svg width="100" height="50"><' . $gradient . ' id="g"><stop offset="0" stop-color="red" stop-opacity="0.2" />'
241+
. '<stop offset="1" stop-color="blue" /></' . $gradient . '><rect width="80" height="40" fill="url(#g)" /></svg>';
242+
}
243+
168244
/**
169245
* A block whose shadow is blurred
170246
*

‎tests/Mpdf/PdfaConformanceTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function testDocumentConforms($version, $config)
5959
. '<img src="' . $img . 'demo.svg" width="40" />'
6060
. '<svg width="100" height="50"><rect width="80" height="40" fill="red" fill-opacity="0.3" stroke="blue"'
6161
. ' stroke-opacity="0.4" opacity="0.5" /><text x="5" y="30" fill-opacity="0.5">SVG</text></svg>'
62+
. '<svg width="100" height="50"><linearGradient id="l"><stop offset="0" stop-color="red" stop-opacity="0.2" />'
63+
. '<stop offset="1" stop-color="blue" /></linearGradient><radialGradient id="r"><stop offset="0" stop-color="red"'
64+
. ' stop-opacity="0.2" /><stop offset="1" stop-color="blue" /></radialGradient><rect width="40" height="40"'
65+
. ' fill="url(#l)" /><rect x="50" width="40" height="40" fill="url(#r)" /></svg>'
6266
);
6367

6468
$this->assertConforms($this->write($mpdf), $this->flavour($mpdf));

‎tests/Mpdf/PdfaOptionalContentTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,41 @@ public function testPdfa1RefusesOptionalContent()
122122
$this->assertStringNotContainsString('/Type /OCG', $pdf);
123123
}
124124

125+
/**
126+
* The catalog lists every optional content group written, and the page resources name only those groups
127+
*
128+
* @dataProvider visibilityDocuments
129+
*/
130+
public function testEveryGroupWrittenIsListed($config, $html)
131+
{
132+
$pdf = $this->render($html, $config);
133+
134+
preg_match_all('#(\d+) 0 obj\s*<</Type /OCG #', $pdf, $written);
135+
$written = array_map(static function ($number) {
136+
return $number . ' 0 R';
137+
}, $written[1]);
138+
preg_match('#/OCGs \[([^\]]*)\]#', $this->optionalContentProperties($pdf), $listed);
139+
preg_match('#/Properties <<(.*?)>>#s', $pdf, $properties);
140+
141+
$this->assertEqualsCanonicalizing($written, $this->references($listed[1]));
142+
$this->assertEqualsCanonicalizing($written, $this->references($properties[1]));
143+
}
144+
145+
/**
146+
* Documents using some of the visibility groups: a PDF/A-2 one with layers and hidden content, and plain ones
147+
* with print-only content, and with print-only and hidden content
148+
*
149+
* @return mixed[][]
150+
*/
151+
public function visibilityDocuments()
152+
{
153+
return [
154+
'PDF/A-2b, layers and hidden' => [['mode' => '', 'PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B'], $this->layeredContent()],
155+
'plain, print-only' => [[], '<div style="visibility: printonly">Print</div>'],
156+
'plain, print-only and hidden' => [[], '<div style="visibility: printonly">Print</div><div style="visibility: hidden">Hidden</div>'],
157+
];
158+
}
159+
125160
/**
126161
* The PDF/A versions that allow optional content
127162
*

0 commit comments

Comments
 (0)