@@ -11,6 +11,11 @@ class AutoVisibilityTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
1111
1212 use PageStreams;
1313
14+ /**
15+ * PDF/A-1b under auto, which allows no optional content
16+ */
17+ const PDFA1_AUTO = ['PDFA ' => true , 'PDFAauto ' => true , 'PDFAversion ' => '1-B ' ];
18+
1419 /**
1520 * A block and a span with each visibility are in the page content or not, and the text around them is
1621 *
@@ -23,7 +28,7 @@ public function testVisibilityUnderAuto($config, $visibility, $drawn)
2328 '<p>Before</p><div style="visibility: ' . $ visibility . '; border: 1px solid #000; background: #ccc">Block</div> '
2429 . '<p>Line <span style="visibility: ' . $ visibility . '">Span</span> After</p> '
2530 );
26- $ pages = implode ('' , $ this ->contents ($ this ->output ($ mpdf )));
31+ $ pages = implode ('' , $ this ->pageContents ($ this ->output ($ mpdf )));
2732
2833 $ this ->assertTrue ($ this ->drawn ($ pages , 'Before ' ));
2934 $ this ->assertTrue ($ this ->drawn ($ pages , 'After ' ));
@@ -41,7 +46,7 @@ public function testVisibilityUnderAuto($config, $visibility, $drawn)
4146 public function autoVisibilities ()
4247 {
4348 $ configs = [
44- 'PDF/A-1b ' => [[ ' PDFA ' => true , ' PDFAauto ' => true , ' PDFAversion ' => ' 1-B ' ] , false ],
49+ 'PDF/A-1b ' => [self :: PDFA1_AUTO , false ],
4550 'PDF/A-2b ' => [['PDFA ' => true , 'PDFAauto ' => true , 'PDFAversion ' => '2-B ' ], true ],
4651 'PDF/X-1a ' => [['PDFX ' => true , 'PDFXauto ' => true ], false ],
4752 ];
@@ -99,23 +104,23 @@ public function refusedVisibilities()
99104 }
100105
101106 /**
102- * A link, form field, annotation, bookmark and index entry register when their block or span is drawn, and not
103- * when it is left out
107+ * A link, form field, annotation, image title, bookmark and index entry register when their block or span is
108+ * drawn, and not when it is left out
104109 *
105110 * @dataProvider registeringElements
106111 */
107112 public function testLeftOutContentRegistersNothing ($ tag , $ visibility , $ registered )
108113 {
109- $ mpdf = $ this ->document ([ ' PDFA ' => true , ' PDFAauto ' => true , 'PDFAversion ' => ' 1-B ' , ' useActiveForms ' => true ]);
114+ $ mpdf = $ this ->document (self :: PDFA1_AUTO + [ ' useActiveForms ' => true , 'title2annots ' => true ]);
110115 $ mpdf ->WriteHTML (
111116 '<p>Before</p>< ' . $ tag . ' style="visibility: ' . $ visibility . '">Content <a href="https://example.com">link</a> '
112117 . ' <input type="text" name="field" value="Value" /> <annotation content="Note" /> <bookmark content="Mark" /> '
113- . ' <indexentry content="Entry" /></ ' . $ tag . '><p>After</p> '
118+ . ' <indexentry content="Entry" /> <img src=" ' . $ this -> pngImage () . ' " title="Title" /> </ ' . $ tag . '><p>After</p> '
114119 );
115120 $ pdf = $ this ->output ($ mpdf );
116121
117122 $ this ->assertCount ($ registered , isset ($ mpdf ->PageLinks [1 ]) ? $ mpdf ->PageLinks [1 ] : []);
118- $ this ->assertCount ($ registered , isset ($ mpdf ->PageAnnots [1 ]) ? $ mpdf ->PageAnnots [1 ] : []);
123+ $ this ->assertCount (2 * $ registered , isset ($ mpdf ->PageAnnots [1 ]) ? $ mpdf ->PageAnnots [1 ] : []);
119124 $ this ->assertCount ($ registered , $ mpdf ->BMoutlines );
120125 $ this ->assertCount ($ registered , $ mpdf ->Reference );
121126 $ this ->assertSame ($ registered , substr_count ($ pdf , '/Subtype /Widget ' ));
@@ -146,13 +151,13 @@ public function testLeftOutContentAcrossPageBreaks()
146151 $ html = '<p>Before</p><div style="visibility: %s; border: 1px solid #000"> '
147152 . str_repeat ('<p>Screen</p> ' , 60 ) . '</div><p>After</p> ' ;
148153
149- $ mpdf = $ this ->document ([ ' PDFA ' => true , ' PDFAauto ' => true , ' PDFAversion ' => ' 1-B ' ] );
154+ $ mpdf = $ this ->document (self :: PDFA1_AUTO );
150155 $ mpdf ->WriteHTML (sprintf ($ html , 'screenonly ' ));
151- $ pages = $ this ->contents ($ this ->output ($ mpdf ));
156+ $ pages = $ this ->pageContents ($ this ->output ($ mpdf ));
152157
153- $ visible = $ this ->document ([ ' PDFA ' => true , ' PDFAauto ' => true , ' PDFAversion ' => ' 1-B ' ] );
158+ $ visible = $ this ->document (self :: PDFA1_AUTO );
154159 $ visible ->WriteHTML (sprintf ($ html , 'visible ' ));
155- $ visiblePages = $ this ->contents ($ this ->output ($ visible ));
160+ $ visiblePages = $ this ->pageContents ($ this ->output ($ visible ));
156161
157162 $ this ->assertGreaterThan (2 , count ($ pages ));
158163 $ this ->assertCount (count ($ visiblePages ), $ pages );
@@ -168,7 +173,7 @@ public function testLeftOutContentAcrossPageBreaks()
168173 */
169174 public function testDrawingResumesWithTheStateSet ()
170175 {
171- $ mpdf = $ this ->document ([ ' PDFA ' => true , ' PDFAauto ' => true , ' PDFAversion ' => ' 1-B ' ] );
176+ $ mpdf = $ this ->document (self :: PDFA1_AUTO );
172177 $ mpdf ->AddPage ();
173178 $ mpdf ->SetDrawColor (255 , 0 , 0 );
174179 $ mpdf ->SetLineWidth (1 );
@@ -180,7 +185,7 @@ public function testDrawingResumesWithTheStateSet()
180185 $ mpdf ->SetDrawColor (0 , 0 , 255 );
181186 $ mpdf ->SetLineWidth (2 );
182187 $ mpdf ->Line (10 , 20 , 50 , 20 );
183- $ page = $ this ->contents ($ this ->output ($ mpdf ))[0 ];
188+ $ page = $ this ->pageContents ($ this ->output ($ mpdf ))[0 ];
184189
185190 $ before = substr ($ page , 0 , strrpos ($ page , ' m ' ));
186191 preg_match_all ('/([\d.]+ [\d.]+ [\d.]+) RG/ ' , $ before , $ colours );
@@ -203,25 +208,6 @@ private function document($config)
203208 return $ this ->mpdf ($ config + ['mode ' => 'utf-8 ' ]);
204209 }
205210
206- /**
207- * The content stream of each page, in order. Other streams, such as an embedded font's, are left out.
208- *
209- * @param string $pdf
210- *
211- * @return string[]
212- */
213- private function contents ($ pdf )
214- {
215- $ contents = [];
216- foreach ($ this ->pageObjects ($ pdf ) as $ number ) {
217- preg_match ('#/Contents (\d+) 0 R# ' , $ this ->object ($ pdf , $ number ), $ ref );
218- preg_match ('/\n ' . $ ref [1 ] . ' 0 obj\s*<<\/Length \d+>>\s*stream\n(.*?)\nendstream/s ' , $ pdf , $ stream );
219- $ contents [] = $ stream [1 ];
220- }
221-
222- return $ contents ;
223- }
224-
225211 /**
226212 * Whether text is drawn in a content stream, where mPDF writes it as UTF-16BE
227213 *
0 commit comments