| 1 | package PDF::Make::Linearization; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use PDF::Make; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.15'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =encoding utf8 | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | PDF::Make::Linearization - PDF Linearization (Fast Web View) support | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use PDF::Make; | ||||||||||||||||||||||||||||
| 18 | use PDF::Make::Linearization; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Check if a PDF is linearized | ||||||||||||||||||||||||||||
| 21 | my $doc = PDF::Make->open('document.pdf'); | ||||||||||||||||||||||||||||
| 22 | if ($doc->is_linearized) { | ||||||||||||||||||||||||||||
| 23 | my $params = $doc->linear_params; | ||||||||||||||||||||||||||||
| 24 | say "Fast Web View: Yes"; | ||||||||||||||||||||||||||||
| 25 | say "Pages: $params->{page_count}"; | ||||||||||||||||||||||||||||
| 26 | say "First page ends at byte: $params->{first_page_end}"; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Create a linearized PDF | ||||||||||||||||||||||||||||
| 30 | my $pdf = PDF::Make->new; | ||||||||||||||||||||||||||||
| 31 | $pdf->page->text(100, 700, "Page 1"); | ||||||||||||||||||||||||||||
| 32 | $pdf->page->text(100, 700, "Page 2"); | ||||||||||||||||||||||||||||
| 33 | $pdf->finalize; | ||||||||||||||||||||||||||||
| 34 | $pdf->write_linearized('optimized.pdf'); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Streaming reader for HTTP byte-range requests | ||||||||||||||||||||||||||||
| 37 | my $reader = PDF::Make::StreamReader->new( | ||||||||||||||||||||||||||||
| 38 | fetch => sub { | ||||||||||||||||||||||||||||
| 39 | my ($offset, $length) = @_; | ||||||||||||||||||||||||||||
| 40 | return http_range_request($url, $offset, $length); | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | $reader->read_header; | ||||||||||||||||||||||||||||
| 44 | say "Pages: ", $reader->page_count; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Load pages on demand | ||||||||||||||||||||||||||||
| 47 | $reader->read_page(0); # First page (usually pre-loaded) | ||||||||||||||||||||||||||||
| 48 | $reader->read_page(5); # Triggers fetch for page 6 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | This module provides PDF linearization support, enabling "Fast Web View" | ||||||||||||||||||||||||||||
| 53 | functionality per Annex F of ISO 32000-2:2020. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Linearization reorganizes a PDF file so that: | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =over 4 | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item * The first page can display before the entire file downloads | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item * Subsequent pages load on demand via HTTP byte-range requests | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * Hint tables enable efficient page offset calculation | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =back | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 METHODS ADDED TO PDF::Make | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 is_linearized | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $bool = $doc->is_linearized; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Returns true if the document is linearized (has Fast Web View). | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 linear_params | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | my $params = $doc->linear_params; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Returns a hashref with linearization parameters: | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | version => 1, # Linearized version | ||||||||||||||||||||||||||||
| 83 | file_length => 123456, # Total file size | ||||||||||||||||||||||||||||
| 84 | hint_offset => 1234, # Hint stream offset | ||||||||||||||||||||||||||||
| 85 | hint_length => 567, # Hint stream length | ||||||||||||||||||||||||||||
| 86 | first_page_obj => 7, # First page object number | ||||||||||||||||||||||||||||
| 87 | first_page_end => 12345, # End of first page section | ||||||||||||||||||||||||||||
| 88 | page_count => 10, # Number of pages | ||||||||||||||||||||||||||||
| 89 | main_xref_offset => 98765, # Main xref table offset | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Returns undef if document is not linearized. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 linearize | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | $doc->linearize; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Prepares the document for linearized output. This analyzes page dependencies | ||||||||||||||||||||||||||||
| 99 | and computes the optimal object ordering. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head2 write_linearized | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | $doc->write_linearized($path); | ||||||||||||||||||||||||||||
| 104 | my $bytes = $doc->write_linearized; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Writes the document in linearized format. If a path is provided, writes to | ||||||||||||||||||||||||||||
| 107 | that file. Otherwise returns the PDF bytes. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Storage for linearization state (inside-out pattern for XS objects) | ||||||||||||||||||||||||||||
| 112 | 3 | my %_linearize_state; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Add methods to PDF::Make::Document - XS provides these when available | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # is_linearized - wraps XS _xs_is_linearized | ||||||||||||||||||||||||||||
| 119 | 3 | ▶ | unless (defined &PDF::Make::Document::is_linearized) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 7 | ▶ | *PDF::Make::Document::is_linearized = sub { $_[0]->_xs_is_linearized }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # linear_params - wraps XS _xs_linear_params; returns undef when not linearized | ||||||||||||||||||||||||||||
| 124 | 3 | ▶ | unless (defined &PDF::Make::Document::linear_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 4 | ▶ | *PDF::Make::Document::linear_params = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 126 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 127 | 4 | ▶ | return undef unless $self->is_linearized; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | return $self->_xs_linear_params; | |||||||||||||||||||||||||||
| 129 | 3 | }; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # linearize - wraps XS _xs_linearize, records state for write_linearized | ||||||||||||||||||||||||||||
| 133 | 3 | ▶ | unless (defined &PDF::Make::Document::linearize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | *PDF::Make::Document::linearize = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 4 | $self->_xs_linearize; | |||||||||||||||||||||||||||
| 137 | 4 | $_linearize_state{"$self"} = 1; | |||||||||||||||||||||||||||
| 138 | 4 | return $self; | |||||||||||||||||||||||||||
| 139 | 3 | }; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # write_linearized - wraps XS _xs_write_linearized_to_path, or returns bytes | ||||||||||||||||||||||||||||
| 143 | 3 | ▶ | unless (defined &PDF::Make::Document::write_linearized) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 6 | ▶ | *PDF::Make::Document::write_linearized = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 145 | 6 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 146 | 6 | $_linearize_state{"$self"} = 1; | |||||||||||||||||||||||||||
| 147 | 6 | ▶ | return $self->_xs_write_linearized_to_path($path) if defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 4 | return $self->_write_linearized_bytes; | |||||||||||||||||||||||||||
| 149 | 3 | }; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # _write_linearized_bytes - use LinearContext to produce linearized output | ||||||||||||||||||||||||||||
| 153 | 3 | ▶ | unless (defined &PDF::Make::Document::_write_linearized_bytes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 4 | ▶ | *PDF::Make::Document::_write_linearized_bytes = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Use LinearContext pipeline | ||||||||||||||||||||||||||||
| 158 | 4 | my $ctx = PDF::Make::LinearContext->_new($self); | |||||||||||||||||||||||||||
| 159 | 4 | $ctx->analyze; | |||||||||||||||||||||||||||
| 160 | 4 | $ctx->build_hints; | |||||||||||||||||||||||||||
| 161 | 4 | return $ctx->write; | |||||||||||||||||||||||||||
| 162 | 3 | }; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 PDF::Make::StreamReader | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Streaming reader for linearized PDFs, enabling page-on-demand loading. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 new | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | my $reader = PDF::Make::StreamReader->new( | ||||||||||||||||||||||||||||
| 173 | fetch => sub { | ||||||||||||||||||||||||||||
| 174 | my ($offset, $length) = @_; | ||||||||||||||||||||||||||||
| 175 | # Return $length bytes starting at $offset | ||||||||||||||||||||||||||||
| 176 | return $data; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Creates a new streaming reader with the given fetch callback. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 read_header | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | $reader->read_header; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Reads and parses the PDF header and linearization dictionary. | ||||||||||||||||||||||||||||
| 187 | This is the first operation to perform. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 is_linearized | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | if ($reader->is_linearized) { ... } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Returns true if the PDF is linearized. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 page_count | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | my $count = $reader->page_count; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Returns the total number of pages. Available after C<read_header>. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 page_available | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | if ($reader->page_available($page_num)) { ... } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Returns true if the given page (0-based) is loaded. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 read_page | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | $reader->read_page($page_num); | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Fetches and parses the given page's data. May trigger HTTP range request. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 page_range | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | my ($offset, $length) = $reader->page_range($page_num); | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Returns the byte offset and length for the given page. | ||||||||||||||||||||||||||||
| 218 | Useful for HTTP Range header construction. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | package PDF::Make::StreamReader; | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 225 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 226 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 229 | 14 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 14 | ▶ | croak "fetch callback required" unless $args{fetch}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 12 | ▶ | croak "fetch must be a code reference" unless ref($args{fetch}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | my $self = bless { | ||||||||||||||||||||||||||||
| 235 | fetch => $args{fetch}, | ||||||||||||||||||||||||||||
| 236 | 10 | is_linearized => 0, | |||||||||||||||||||||||||||
| 237 | page_count => 0, | ||||||||||||||||||||||||||||
| 238 | params => {}, | ||||||||||||||||||||||||||||
| 239 | hints_loaded => 0, | ||||||||||||||||||||||||||||
| 240 | page_hints => [], | ||||||||||||||||||||||||||||
| 241 | shared_hints => [], | ||||||||||||||||||||||||||||
| 242 | page_loaded => {}, # page_num => 1 | ||||||||||||||||||||||||||||
| 243 | header_data => undef, | ||||||||||||||||||||||||||||
| 244 | _doc => undef, | ||||||||||||||||||||||||||||
| 245 | }, $class; | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 10 | return $self; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 8 | ▶ | sub read_header { | ||||||||||||||||||||||||||
|
Subroutine
read_header: called CC=13 SCAR=26.0
| |||||||||||||||||||||||||||||
| 251 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # Fetch first 4KB | ||||||||||||||||||||||||||||
| 254 | 8 | my $header_size = 4096; | |||||||||||||||||||||||||||
| 255 | 8 | my $data = $self->{fetch}->(0, $header_size); | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 8 | ▶ | croak "Failed to fetch header" unless defined $data && length($data) > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data and length $data > 0
MC/DC0%0/2
defined $data and length $data > 0
defined $data
length $data > 0
Truth table33%1/3
A and B
A= defined $dataB= length $data > 0
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 8 | $self->{header_data} = $data; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Check for linearization | ||||||||||||||||||||||||||||
| 262 | 8 | ▶ | if ($data =~ m{/Linearized\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 6 | $self->{is_linearized} = 1; | |||||||||||||||||||||||||||
| 264 | 6 | $self->{params}{version} = $1; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Extract linearization parameters | ||||||||||||||||||||||||||||
| 268 | 8 | ▶ | if ($self->{is_linearized}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | # /L - file length | ||||||||||||||||||||||||||||
| 270 | 6 | ▶ | if ($data =~ m{/L\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 6 | $self->{params}{file_length} = $1; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # /N - page count | ||||||||||||||||||||||||||||
| 275 | 6 | ▶ | if ($data =~ m{/N\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 6 | $self->{page_count} = $1; | |||||||||||||||||||||||||||
| 277 | 6 | $self->{params}{page_count} = $1; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # /O - first page object | ||||||||||||||||||||||||||||
| 281 | 6 | ▶ | if ($data =~ m{/O\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 5 | $self->{params}{first_page_obj} = $1; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # /E - end of first page | ||||||||||||||||||||||||||||
| 286 | 6 | ▶ | if ($data =~ m{/E\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 5 | $self->{params}{first_page_end} = $1; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # /H - hint stream [offset length] | ||||||||||||||||||||||||||||
| 291 | 6 | ▶ | if ($data =~ m{/H\s*\[\s*(\d+)\s+(\d+)\s*(?:(\d+)\s+(\d+)\s*)?\]}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 5 | $self->{params}{hint_offset} = $1; | |||||||||||||||||||||||||||
| 293 | 5 | $self->{params}{hint_length} = $2; | |||||||||||||||||||||||||||
| 294 | 5 | ▶ | $self->{params}{overflow_offset} = $3 if defined $3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 5 | ▶ | $self->{params}{overflow_length} = $4 if defined $4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # /T - main xref offset | ||||||||||||||||||||||||||||
| 299 | 6 | ▶ | if ($data =~ m{/T\s+(\d+)}s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 5 | $self->{params}{main_xref_offset} = $1; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # Mark first page as loaded (it's in the header section) | ||||||||||||||||||||||||||||
| 304 | 6 | $self->{page_loaded}{0} = 1; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 8 | return $self; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 5 | ▶ | sub is_linearized { | ||||||||||||||||||||||||||
|
Subroutine
is_linearized: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 311 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 312 | 5 | return $self->{is_linearized}; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 4 | ▶ | sub page_count { | ||||||||||||||||||||||||||
|
Subroutine
page_count: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 316 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 317 | 4 | return $self->{page_count}; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 2 | ▶ | sub params { | ||||||||||||||||||||||||||
|
Subroutine
params: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 321 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 322 | 2 | return { %{$self->{params}} }; | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 7 | ▶ | sub page_available { | ||||||||||||||||||||||||||
|
Subroutine
page_available: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 326 | 7 | my ($self, $page_num) = @_; | |||||||||||||||||||||||||||
| 327 | 7 | ▶ | return $self->{page_loaded}{$page_num} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 4 | ▶ | sub load_hints { | ||||||||||||||||||||||||||
|
Subroutine
load_hints: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||
| 331 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 4 | ▶ | return if $self->{hints_loaded}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 3 | ▶ | croak "Not linearized" unless $self->{is_linearized}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 2 | my $offset = $self->{params}{hint_offset}; | |||||||||||||||||||||||||||
| 337 | 2 | my $length = $self->{params}{hint_length}; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 2 | ▶ | croak "Hint offset/length not available" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $offset and defined $length
MC/DC0%0/2
defined $offset and defined $length
defined $offset
defined $length
Truth table33%1/3
A and B
A= defined $offsetB= defined $length
| |||||||||||||||||||||||||||||
| 340 | unless defined $offset && defined $length; | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Fetch hint stream | ||||||||||||||||||||||||||||
| 343 | 2 | my $hint_data = $self->{fetch}->($offset, $length); | |||||||||||||||||||||||||||
| 344 | 2 | ▶ | croak "Failed to fetch hint stream" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $hint_data and length $hint_data >= $length
MC/DC0%0/2
defined $hint_data and length $hint_data >= $length
defined $hint_data
length $hint_data >= $length
Truth table33%1/3
A and B
A= defined $hint_dataB= length $hint_data >= $length
| |||||||||||||||||||||||||||||
| 345 | unless defined $hint_data && length($hint_data) >= $length; | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # Parse hint stream | ||||||||||||||||||||||||||||
| 348 | 2 | $self->_parse_hint_stream($hint_data); | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 2 | $self->{hints_loaded} = 1; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 2 | return $self; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 2 | ▶ | sub _parse_hint_stream { | ||||||||||||||||||||||||||
|
Subroutine
_parse_hint_stream: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 356 | 2 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Find stream content (skip object header and dictionary) | ||||||||||||||||||||||||||||
| 359 | 2 | my $stream_start = index($data, "stream"); | |||||||||||||||||||||||||||
| 360 | 2 | ▶ | return unless $stream_start >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 2 | $stream_start += 6; # Skip "stream" | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Skip newline after "stream" | ||||||||||||||||||||||||||||
| 364 | 2 | ▶ | $stream_start++ if substr($data, $stream_start, 1) eq "\r"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 2 | ▶ | $stream_start++ if substr($data, $stream_start, 1) eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 2 | my $stream_end = rindex($data, "endstream"); | |||||||||||||||||||||||||||
| 368 | 2 | ▶ | return unless $stream_end > $stream_start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 2 | my $stream_content = substr($data, $stream_start, $stream_end - $stream_start); | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # Parse page offset hint table header (§F.4.2) | ||||||||||||||||||||||||||||
| 373 | # First 40 bytes contain header fields | ||||||||||||||||||||||||||||
| 374 | 2 | ▶ | return unless length($stream_content) >= 40; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 2 | my @bytes = unpack("C*", $stream_content); | |||||||||||||||||||||||||||
| 377 | 2 | my $pos = 0; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # Item 1: Min objects per page (4 bytes) | ||||||||||||||||||||||||||||
| 380 | 2 | my $min_obj = ($bytes[$pos] << 24) | ($bytes[$pos+1] << 16) | | |||||||||||||||||||||||||||
| 381 | ($bytes[$pos+2] << 8) | $bytes[$pos+3]; | ||||||||||||||||||||||||||||
| 382 | 2 | $pos += 4; | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # Item 2: First page location (4 bytes) | ||||||||||||||||||||||||||||
| 385 | 2 | my $first_loc = ($bytes[$pos] << 24) | ($bytes[$pos+1] << 16) | | |||||||||||||||||||||||||||
| 386 | ($bytes[$pos+2] << 8) | $bytes[$pos+3]; | ||||||||||||||||||||||||||||
| 387 | 2 | $pos += 4; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # Item 3: Bits for obj count (2 bytes) | ||||||||||||||||||||||||||||
| 390 | 2 | my $bits_obj = ($bytes[$pos] << 8) | $bytes[$pos+1]; | |||||||||||||||||||||||||||
| 391 | 2 | $pos += 2; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # Item 4: Min page length (4 bytes) | ||||||||||||||||||||||||||||
| 394 | 2 | my $min_len = ($bytes[$pos] << 24) | ($bytes[$pos+1] << 16) | | |||||||||||||||||||||||||||
| 395 | ($bytes[$pos+2] << 8) | $bytes[$pos+3]; | ||||||||||||||||||||||||||||
| 396 | 2 | $pos += 4; | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | # Item 5: Bits for page length (2 bytes) | ||||||||||||||||||||||||||||
| 399 | 2 | my $bits_len = ($bytes[$pos] << 8) | $bytes[$pos+1]; | |||||||||||||||||||||||||||
| 400 | 2 | $pos += 2; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | # Store parsed values | ||||||||||||||||||||||||||||
| 403 | $self->{hint_header} = { | ||||||||||||||||||||||||||||
| 404 | 2 | min_obj_count => $min_obj, | |||||||||||||||||||||||||||
| 405 | first_page_loc => $first_loc, | ||||||||||||||||||||||||||||
| 406 | bits_obj_count => $bits_obj, | ||||||||||||||||||||||||||||
| 407 | min_page_length => $min_len, | ||||||||||||||||||||||||||||
| 408 | bits_page_len => $bits_len, | ||||||||||||||||||||||||||||
| 409 | }; | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | # Continue parsing per-page data... | ||||||||||||||||||||||||||||
| 412 | # (Simplified for now) | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 2 | return 1; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 5 | ▶ | sub read_page { | ||||||||||||||||||||||||||
|
Subroutine
read_page: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 418 | 5 | my ($self, $page_num) = @_; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | croak "Invalid page number" | ||||||||||||||||||||||||||||
| 421 | 5 | ▶ | if $page_num < 0 || $page_num >= $self->{page_count}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$page_num < 0 or $page_num >= $self->{"page_count"}
MC/DC100%2/2
$page_num < 0 or $page_num >= $self->{"page_count"}
$page_num < 0
$page_num >= $self->{"page_count"}
Truth table100%3/3
A or B
A= $page_num < 0B= $page_num >= $self->{"page_count"}
| |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | # Already loaded? | ||||||||||||||||||||||||||||
| 424 | 2 | ▶ | return $self if $self->{page_loaded}{$page_num}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | # Need hints for page ranges | ||||||||||||||||||||||||||||
| 427 | 1 | ▶ | $self->load_hints unless $self->{hints_loaded}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # Get page byte range | ||||||||||||||||||||||||||||
| 430 | 1 | my ($offset, $length) = $self->page_range($page_num); | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # Fetch page data | ||||||||||||||||||||||||||||
| 433 | 1 | my $page_data = $self->{fetch}->($offset, $length); | |||||||||||||||||||||||||||
| 434 | 1 | ▶ | croak "Failed to fetch page $page_num" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $page_data and length $page_data > 0
MC/DC0%0/2
defined $page_data and length $page_data > 0
defined $page_data
length $page_data > 0
Truth table33%1/3
A and B
A= defined $page_dataB= length $page_data > 0
| |||||||||||||||||||||||||||||
| 435 | unless defined $page_data && length($page_data) > 0; | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | # Parse page objects | ||||||||||||||||||||||||||||
| 438 | # (In real implementation, would parse and add to document) | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | # Mark page as loaded | ||||||||||||||||||||||||||||
| 441 | 1 | $self->{page_loaded}{$page_num} = 1; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 1 | return $self; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 4 | ▶ | sub page_range { | ||||||||||||||||||||||||||
|
Subroutine
page_range: called CC=8 SCAR=21.9
| |||||||||||||||||||||||||||||
| 447 | 4 | my ($self, $page_num) = @_; | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | croak "Invalid page number" | ||||||||||||||||||||||||||||
| 450 | 4 | ▶ | if $page_num < 0 || $page_num >= $self->{page_count}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$page_num < 0 or $page_num >= $self->{"page_count"}
MC/DC50%1/2
$page_num < 0 or $page_num >= $self->{"page_count"}
$page_num < 0
$page_num >= $self->{"page_count"}
Truth table66%2/3
A or B
A= $page_num < 0B= $page_num >= $self->{"page_count"}
| |||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | # Load hints if needed | ||||||||||||||||||||||||||||
| 453 | 3 | ▶ | $self->load_hints unless $self->{hints_loaded}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # Calculate offset from hint data | ||||||||||||||||||||||||||||
| 456 | 3 | my $header = $self->{hint_header}; | |||||||||||||||||||||||||||
| 457 | 3 | ▶ | return (0, 0) unless $header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 3 | my $offset = $header->{first_page_loc}; | |||||||||||||||||||||||||||
| 460 | 3 | my $length = $header->{min_page_length}; | |||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | # Add deltas for pages before this one | ||||||||||||||||||||||||||||
| 463 | 3 | for my $i (0 .. $page_num - 1) { | |||||||||||||||||||||||||||
| 464 | 3 | my $hint = $self->{page_hints}[$i]; | |||||||||||||||||||||||||||
| 465 | 3 | ▶ | if ($hint) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 466 | 0 | $offset += $hint->{page_length}; | |||||||||||||||||||||||||||
| 467 | } else { | ||||||||||||||||||||||||||||
| 468 | 3 | $offset += $length; # Use min length as estimate | |||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | # Get this page's length | ||||||||||||||||||||||||||||
| 473 | 3 | my $page_hint = $self->{page_hints}[$page_num]; | |||||||||||||||||||||||||||
| 474 | 3 | ▶ | if ($page_hint) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | 0 | $length = $page_hint->{page_length}; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 3 | return ($offset, $length); | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 1 | ▶ | sub doc { | ||||||||||||||||||||||||||
|
Subroutine
doc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 482 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 483 | 1 | return $self->{_doc}; | |||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =head1 LINEARIZATION STRUCTURE | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | A linearized PDF has this structure: | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | ┌─────────────────────────────────────┐ | ||||||||||||||||||||||||||||
| 491 | │ Header (%PDF-2.0) │ | ||||||||||||||||||||||||||||
| 492 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 493 | │ Linearization dictionary (obj 1) │ | ||||||||||||||||||||||||||||
| 494 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 495 | │ First page xref (partial) │ | ||||||||||||||||||||||||||||
| 496 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 497 | │ Document catalog, pages tree root │ | ||||||||||||||||||||||||||||
| 498 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 499 | │ First page objects │ | ||||||||||||||||||||||||||||
| 500 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 501 | │ Hint stream │ | ||||||||||||||||||||||||||||
| 502 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 503 | │ Remaining pages (2..N) │ | ||||||||||||||||||||||||||||
| 504 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 505 | │ Shared objects │ | ||||||||||||||||||||||||||||
| 506 | ├─────────────────────────────────────┤ | ||||||||||||||||||||||||||||
| 507 | │ Main xref + trailer │ | ||||||||||||||||||||||||||||
| 508 | └─────────────────────────────────────┘ | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | L<PDF::Make>, ISO 32000-2:2020 Annex F (Linearized PDF) | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =cut | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 3 | 1; | |||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | __END__ | ||||||||||||||||||||||||||||