Skip to content

Commit 11ce795

Browse files
jakejackson1claude
andcommitted
Leave out an image title in left-out content, and share the page stream parser
An image's title annotation now takes the visibility of its span, like the other objects a tag puts straight into the text buffer. The visibility groups reuse OptionalContentWriter's table rather than repeat its bits, the per-object drop check no longer re-tests the block's visibility each time, and IndexEntry() drops a guard the object buffer already covers. The page content parser moves into PageStreams as pageContents(). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 6b2f932 commit 11ce795

5 files changed

Lines changed: 49 additions & 44 deletions

File tree

‎src/Mpdf.php‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Mpdf\Utils\NumericString;
2020
use Mpdf\Utils\UtfString;
2121
use Mpdf\Utils\Path;
22+
use Mpdf\Writer\OptionalContentWriter;
2223
use Psr\Log\NullLogger;
2324
use Mpdf\Unicode\Ucdn;
2425
use Mpdf\Unicode\Bidi;
@@ -51,9 +52,9 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface
5152
const OBJECT_IDENTIFIER = "\xbb\xa4\xac";
5253

5354
/**
54-
* The optional content group each visibility draws in: OC1 to OC3, marked used by bits 1, 2 and 4 of $hasOC
55+
* The optional content group each visibility draws in, by its resource name in OptionalContentWriter
5556
*/
56-
const VISIBILITY_GROUPS = ['printonly' => 1, 'screenonly' => 2, 'hidden' => 3];
57+
const VISIBILITY_GROUPS = ['printonly' => 'OC1', 'screenonly' => 'OC2', 'hidden' => 'OC3'];
5758

5859
var $useFixedNormalLineHeight; // mPDF 6
5960
var $useFixedTextBaseline; // mPDF 6
@@ -2168,7 +2169,7 @@ function SetVisibility($v)
21682169
$this->writer->write('___DROPPED___START' . $this->uniqstr);
21692170
} elseif ($v != 'visible') {
21702171
$this->writer->write($this->visibilityBegins($v));
2171-
$this->hasOC = ($this->hasOC | 1 << (self::VISIBILITY_GROUPS[$v] - 1));
2172+
$this->hasOC = ($this->hasOC | OptionalContentWriter::VISIBILITY_GROUPS[self::VISIBILITY_GROUPS[$v]][0]);
21722173
}
21732174
$this->visibility = $v;
21742175
}
@@ -2204,7 +2205,7 @@ private function visibilityBegins($v)
22042205
throw new \Mpdf\MpdfException('Incorrect visibility: ' . $v);
22052206
}
22062207

2207-
return '/OC /OC' . self::VISIBILITY_GROUPS[$v] . ' BDC';
2208+
return '/OC /' . self::VISIBILITY_GROUPS[$v] . ' BDC';
22082209
}
22092210

22102211
/**
@@ -7621,10 +7622,12 @@ function printobjectbuffer($is_table = false, $blockdir = false)
76217622
$rtlalign = 'L';
76227623
}
76237624

7625+
// An object left out is not drawn, and registers no link, field, bookmark or entry
7626+
$blockDropped = $this->visibilityDropped($this->visibility);
7627+
76247628
foreach ($this->objectbuffer as $ib => $objattr) {
76257629

7626-
// An object left out is not drawn, and registers no link, field, bookmark or entry
7627-
if ($this->visibilityDropped($this->visibility) || (isset($objattr['visibility']) && $this->visibilityDropped($objattr['visibility']))) {
7630+
if ($blockDropped || (isset($objattr['visibility']) && $this->visibilityDropped($objattr['visibility']))) {
76287631
continue;
76297632
}
76307633

@@ -24474,10 +24477,6 @@ function DeletePages($start_page, $end_page = -1)
2447424477

2447524478
function IndexEntry($txt, $xref = '')
2447624479
{
24477-
if ($this->visibilityDropped($this->visibility)) {
24478-
return;
24479-
}
24480-
2448124480
if ($xref) {
2448224481
$this->IndexEntrySee($txt, $xref);
2448324482
return;

‎src/Tag/Img.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public function open($attr, &$ahtml, &$ihtml)
461461
$objattr['SUBJECT'] = '';
462462
$objattr['OPACITY'] = $this->mpdf->annotOpacity;
463463
$objattr['COLOR'] = $this->colorConverter->convert('yellow', $this->mpdf->PDFAXwarnings);
464-
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
464+
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
465465
if ($this->mpdf->tableLevel) { // *TABLES*
466466
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e]; // *TABLES*
467467
} // *TABLES*

‎src/Writer/PageWriter.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function writePages() // _putpages
8585
// Remove the content that visibility leaves out of a PDF/A or PDF/X document, then any marker left unpaired
8686
if (strpos($thispage, '___DROPPED___') !== false) {
8787
$thispage = preg_replace('/___DROPPED___START' . $this->mpdf->uniqstr . '.*?___DROPPED___END' . $this->mpdf->uniqstr . '/s', '', $thispage);
88-
$thispage = preg_replace('/___DROPPED___(START|END)' . $this->mpdf->uniqstr . '/', '', $thispage);
88+
$thispage = str_replace(['___DROPPED___START' . $this->mpdf->uniqstr, '___DROPPED___END' . $this->mpdf->uniqstr], '', $thispage);
8989
}
9090

9191
// Clean up repeated /GS1 gs statements

‎tests/Mpdf/AutoVisibilityTest.php‎

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

‎tests/Mpdf/PageStreams.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ private function object($pdf, $number)
114114
return $match[1];
115115
}
116116

117+
/**
118+
* The content stream of each page, in order. Unlike pages(), no other stream is included, such as an embedded
119+
* font's ToUnicode map.
120+
*
121+
* @param string $pdf
122+
*
123+
* @return string[]
124+
*/
125+
private function pageContents($pdf)
126+
{
127+
$contents = [];
128+
foreach ($this->pageObjects($pdf) as $number) {
129+
preg_match('#/Contents (\d+) 0 R#', $this->object($pdf, $number), $ref);
130+
preg_match('/stream\n(.*?)\nendstream/s', $this->object($pdf, $ref[1]), $stream);
131+
$contents[] = $stream[1];
132+
}
133+
134+
return $contents;
135+
}
136+
117137
/**
118138
* The object numbers each page lists in its /Annots array, as one array of numbers per page
119139
*

0 commit comments

Comments
 (0)