API2/ViewerPreferences.pm

filter: Statementstmt 44.0% 52 / 118 Branchbran 18.6% 16 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 36.8% 81 / 220 SCAR 65.5
CC
47
Cov
33%
set_preferences
477.1
get_preferences
182.0
_snake_case
2.0
CRAP 701.5
« API2/Util.pm ↑ Summary API2/Resource/XObject/Image/PNM.pm »
1 package PDF::API2::ViewerPreferences;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 our $VERSION = '2.044'; # VERSION
7  
8 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 our @CARP_NOT;
13  
14 30 my @booleans = qw(HideToolbar HideMenubar HideWindowUI FitWindow CenterWindow
15 DisplayDocTitle PickTrayByPDFSize);
16  
17 30 my @names = qw(NonFullScreenPageMode Direction Duplex PrintScaling
18 ViewArea ViewClip PrintArea PrintClip);
19  
20 =head1 NAME
21  
22 PDF::API2::ViewerPreferences - How the PDF should be displayed or printed
23  
24 =head1 METHODS
25  
26 =over
27  
28 =cut
29  
30 sub _snake_case {
31 0 ▶ my $name = shift();
Subroutine _snake_case: not called CC=1 SCAR=6.9
32 0 $name =~ s/^([A-Z]+)/lc($1)/e;
33 0 $name =~ s/([A-Z]+)/'_' . lc($1)/ge;
34 0 $name =~ s/pdfsize/pdf_size/;
35 0 return $name;
36 }
37  
38 sub _camel_case {
39 172 ▶ my $name = shift();
Subroutine _camel_case: called CC=1 SCAR=0.0
40 172 $name = ucfirst($name);
41 172 $name =~ s/_([a-z]+)/ucfirst($1)/ge;
42 172 $name =~ s/Ui$/UI/;
43 172 $name =~ s/Pdf/PDF/;
44 172 return $name;
45 }
46  
47 =item $self = $class->new($pdf)
48  
49 Creates a new ViewerPreferences object from a PDF::API2 object.
50  
51 =cut
52  
53 sub new {
54 172 ▶ my ($class, $pdf) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
55 172 my $self = bless { pdf => $pdf }, $class;
56 172 weaken $self->{'pdf'};
57 172 return $self;
58 }
59  
60 =item %preferences = $self->get_preferences()
61  
62 Returns a hash containing all of the viewer preferences that are defined in the
63 PDF.
64  
65 =cut
66  
67 sub get_preferences {
68 0 ▶ my $self = shift();
Subroutine get_preferences: not called CC=13 SCAR=52.0
Pod documented
69 0 my $prefs = $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
70 0 ▶ return unless $prefs;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefs 0 0 0
71 0 $prefs->realise();
72  
73 0 my %values;
74 0 foreach my $pref (@booleans) {
75 0 ▶ next unless $prefs->{$pref};
Branch: 1 branch
BranchTrueFalseTotal
unless $prefs->{$pref} 0 0 0
76 0 ▶ $values{_snake_case($pref)} = $prefs->{$pref}->val() eq 'true' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$prefs->{$pref}->val eq 'true' ? : 0 0 0
77 }
78 0 foreach my $pref (@names) {
79 0 ▶ next unless $prefs->{$pref};
Branch: 1 branch
BranchTrueFalseTotal
unless $prefs->{$pref} 0 0 0
80 0 ▶ if ($pref eq 'Direction') {
Branch: 3 branches
BranchTrueFalseTotal
if ($pref eq 'Direction') { } 0 0 0
elsif ($pref eq 'Duplex') { } 0 0 0
elsif ($pref eq 'NonFullScreenPageMode') { } 0 0 0
81 0 $values{'direction'} = lc($prefs->{$pref}->val());
82 }
83 elsif ($pref eq 'Duplex') {
84 0 my $value = $prefs->{$pref}->val();
85 0 $value =~ s/Flip//;
86 0 $value =~ s/Edge//;
87 0 $values{'duplex'} = _snake_case($value);
88 }
89 elsif ($pref eq 'NonFullScreenPageMode') {
90 0 my $value = _snake_case($prefs->{$pref}->val());
91 0 $value =~ s/^use_//;
92 0 ▶ $value = 'optional_content' if $value eq 'oc';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "oc" 0 0 0
93 0 $values{'non_full_screen_page_mode'} = $value;
94 }
95 else {
96 0 $values{_snake_case($pref)} = _snake_case($prefs->{$pref}->val());
97 }
98 }
99 0 ▶ if ($prefs->{'PrintPageRange'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefs->{"PrintPageRange"}) 0 0 0
100 0 my @ranges = map { $_->val() } @{$prefs->{'PrintPageRange'}};
101 0 $values{'print_page_range'} = \@ranges;
102 }
103 0 ▶ if ($prefs->{'NumCopies'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefs->{"NumCopies"}) 0 0 0
104 0 $values{'num_copies'} = $prefs->{'NumCopies'}->val();
105 }
106  
107 0 return %values;
108 }
109  
110 =item $value = $self->get_preference($name)
111  
112 Returns the value of the specified viewer preference if present, or C<undef> if
113 not.
114  
115 =cut
116  
117 sub get_preference {
118 0 ▶ my ($self, $name) = @_;
Subroutine get_preference: not called CC=1 SCAR=6.9
Pod documented
119 0 my %values = $self->get_preferences();
120 0 return $values{$name};
121 }
122  
123 =item $self->set_preferences(%values)
124  
125 Sets one or more viewer preferences, as described in the preferences section
126 below.
127  
128 =cut
129  
130 sub _init_preferences {
131 172 ▶ my $self = shift();
Subroutine _init_preferences: called CC=2 SCAR=6.9
132 172 ▶ if ($self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pdf'}{'catalog'}{'ViewerPreferences'}) { } 8 164 172
133 8 $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}->realise();
134 }
135 else {
136 164 $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'} = PDFDict();
137 }
138 172 $self->{'pdf'}->{'pdf'}->out_obj($self->{'pdf'}->{'catalog'});
139 172 return $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
140 }
141  
142 sub set_preferences {
143 172 ▶ my ($self, %values) = @_;
Subroutine set_preferences: called CC=34 SCAR=61.7
Pod documented
144 172 my $prefs = $self->_init_preferences();
145 172 local @CARP_NOT = qw(PDF::API2);
146 172 foreach my $snake (keys %values) {
147 172 my $camel = _camel_case($snake);
148 172 ▶ if ($camel eq 'NonFullScreenPageMode') {
Branch: 8 branches
BranchTrueFalseTotal
if ($camel eq 'NonFullScreenPageMode') { } 169 3 172
elsif ($camel eq 'Direction') { } 0 3 3
elsif ($camel =~ /^(?:View|Print)(?:Area|Clip)$/) { } 0 3 3
elsif ($camel eq 'PrintScaling') { } 0 3 3
elsif ($camel eq 'Duplex') { } 3 0 3
elsif ($camel eq 'PrintPageRange') { } 0 0 0
elsif ($camel eq 'NumCopies') { } 0 0 0
elsif (grep {$camel eq $_;} @booleans) { } 0 0 0
149 169 my $value = $values{$snake};
150 169 ▶ my $name = ($value eq 'none' ? 'UseNone' :
Branch: 4 branches
BranchTrueFalseTotal
$value eq 'none' ? : 169 0 169
$value eq 'outlines' ? : 0 0 0
$value eq 'thumbnails' ? : 0 0 0
$value eq 'optional_content' ? : 0 0 0
151 $value eq 'outlines' ? 'UseOutlines' :
152 $value eq 'thumbnails' ? 'UseThumbs' :
153 $value eq 'optional_content' ? 'UseOC' :
154 '');
155 169 ▶ croak "Invalid value for $snake: $values{$snake}" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 169 169
156 169 $prefs->{$camel} = PDFName($name);
157 }
158 elsif ($camel eq 'Direction') {
159 0 my $name = $values{$snake};
160 0 ▶ unless ($name =~ /^(?:L2R|R2L)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^(?:L2R|R2L)$/i) 0 0 0
161 0 croak "Invalid value for $snake: $name";
162 }
163 0 $prefs->{$camel} = PDFName(uc $name);
164 }
165 elsif ($camel =~ /^(?:View|Print)(?:Area|Clip)$/) {
166 my $name = ($values{$snake} eq 'media_box' ? 'MediaBox' :
167 $values{$snake} eq 'crop_box' ? 'CropBox' :
168 $values{$snake} eq 'bleed_box' ? 'BleedBox' :
169 $values{$snake} eq 'trim_box' ? 'TrimBox' :
170 0 ▶ $values{$snake} eq 'art_box' ? 'ArtBox' : '');
Branch: 5 branches
BranchTrueFalseTotal
$values{$snake} eq 'media_box' ? : 0 0 0
$values{$snake} eq 'crop_box' ? : 0 0 0
$values{$snake} eq 'bleed_box' ? : 0 0 0
$values{$snake} eq 'trim_box' ? : 0 0 0
$values{$snake} eq 'art_box' ? : 0 0 0
171 0 ▶ croak "Invalid value for $snake: $name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
172 0 $prefs->{$camel} = PDFName($name);
173 }
174 elsif ($camel eq 'PrintScaling') {
175 0 my $value = $values{$snake};
176 0 ▶ my $name = ($value eq 'none' ? 'None' :
Branch: 2 branches
BranchTrueFalseTotal
$value eq 'none' ? : 0 0 0
$value eq 'app_default' ? : 0 0 0
177 $value eq 'app_default' ? 'AppDefault' : '');
178 0 ▶ croak "Invalid value for $snake: $name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
179 0 $prefs->{$camel} = PDFName($name);
180 }
181 elsif ($camel eq 'Duplex') {
182 3 my $value = $values{$snake};
183 3 ▶ my $name = ($value eq 'simplex' ? 'Simplex' :
Branch: 3 branches
BranchTrueFalseTotal
$value eq 'simplex' ? : 1 2 3
$value eq 'duplex_short' ? : 1 1 2
$value eq 'duplex_long' ? : 1 0 1
184 $value eq 'duplex_short' ? 'DuplexFlipShortEdge' :
185 $value eq 'duplex_long' ? 'DuplexFlipLongEdge' : '');
186 3 ▶ croak "Invalid value for $snake: $value" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
187 3 $prefs->{$camel} = PDFName($name);
188 }
189 elsif ($camel eq 'PrintPageRange') {
190 0 ▶ unless (ref($values{$snake}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $values{$snake} eq "ARRAY") 0 0 0
191 0 croak "The value for $snake must be a reference to an array";
192 }
193 0 my @range = @{$values{$snake}};
194 0 ▶ unless (@range % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@range % 2 == 0) 0 0 0
195 0 croak "The value for $snake must contain pairs of page numbers";
196 }
197 0 ▶ if (join('', @range) =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (join("", @range) =~ /\D/) 0 0 0
198 0 croak "The value for $snake may only contain page numbers";
199 }
200 0 $prefs->{$camel} = PDFArray(map { PDFNum($_) } @range);
201 }
202 elsif ($camel eq 'NumCopies') {
203 0 ▶ unless ($values{$snake} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($values{$snake} =~ /^\d+$/) 0 0 0
204 0 croak "$snake: $values{$snake} is not an integer";
205 }
206 0 $prefs->{$camel} = PDFNum($values{$snake});
207 }
208 0 elsif (grep { $camel eq $_ } @booleans) {
209 0 ▶ $prefs->{$camel} = PDFBool($values{$snake} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$values{$snake} ? : 0 0 0
210 }
211 else {
212 0 croak "Unrecognized viewer preference '$snake'";
213 }
214 }
215 172 return $self;
216 }
217  
218 =back
219  
220 =head1 PREFERENCES
221  
222 Viewer Preferences describe how the document should be presented on screen or in
223 print. Not all PDF viewers will respect these preferences.
224  
225 Boolean preferences default to false and take (or return) 0 or 1 as arguments.
226  
227 Bounding Box preferences take (or return) one of C<media_box>, C<crop_box>,
228 C<bleed_box>, C<trim_box>, or C<art_box>.
229  
230 =over
231  
232 =item hide_toolbar (boolean)
233  
234 A flag specifying whether to hide the tool bars when the document is active.
235  
236 =item hide_menubar (boolean)
237  
238 A flag specifying whether to hide the menu bar when the document is active.
239  
240 =item hide_window_ui (boolean)
241  
242 A flag specifying whether to hide the user interface elements in the document's
243 window (such as scroll bars and navigation controls), leaving only the
244 document's contents displayed.
245  
246 =item fit_window (boolean)
247  
248 A flag specifying whether to resize the document's window to fit the size of the
249 first displayed page.
250  
251 =item center_window (boolean)
252  
253 A flag specifying whether to position the document's window in the center of the
254 screen.
255  
256 =item display_doc_title (boolean)
257  
258 A flag specifying whether the window's title bar should display the document
259 title taken from the Title entry of the document information directory. If
260 false, the title bar should instead display the name of the PDF file containing
261 the document.
262  
263 =item non_full_screen_page_mode (name)
264  
265 The document's page mode, specifying how to display the document on exiting
266 full-screen mode. Options are the same as C<page_mode> in L<PDF::API2> except
267 that the C<attachments> and C<full_screen> options aren't supported.
268  
269 =item direction ('l2r' or 'r2l')
270  
271 The predominant reading order for text (left-to-right or right-to-left).
272  
273 This entry has no direct effect on the document's contents or page numbering but
274 may be used to determine the relative positioning of pages when displayed
275 side-by-side or printed n-up.
276  
277 =item view_area (bounding box)
278  
279 The name of the page boundary representing the area of a page that shall be
280 displayed when viewing the document on the screen.
281  
282 =item view_clip (bounding box)
283  
284 The name of the page boundary to which the contents of a page shall be clipped
285 when viewing the document on the screen.
286  
287 =item print_area (bounding box)
288  
289 The name of the page boundary representing the area of a page that shall be
290 rendered when printing the document.
291  
292 =item print_clip (bounding box)
293  
294 The name of the page boundary to which the contents of a page shall be clipped
295 when printing the document.
296  
297 =item print_scaling ('none' or 'app_default')
298  
299 The page scaling option that shall be selected when a print dialog is displayed
300 for this document. C<none> represents no page scaling, and C<app_default>
301 represents the reader's default print scaling.
302  
303 =item duplex ('simplex', 'duplex_short', or 'duplex_long')
304  
305 The paper handling option that shall be used when printing the file from the
306 print dialog. The duplex values represent whether the page should be flipped on
307 its short edge or long edge, respectively.
308  
309 =item pick_tray_by_pdf_size (boolean)
310  
311 A flag specifying whether the PDF page size shall be used to select the input
312 paper tray. This setting influences only the preset values used to populate the
313 print dialog presented by the reader.
314  
315 =item print_page_rage (an array of integer pairs)
316  
317 The page numbers used to initialize the print dialog box when the file is
318 printed. The array shall contain an even number of integers to be interpreted
319 in pairs, with each pair specifying the first and last pages in a sub-range of
320 pages to be printed. The first page of the PDF file shall be denoted by 1.
321  
322 =item num_copies (integer)
323  
324 The number of copies that shall be printed when the print dialog is opened for
325 this file.
326  
327 =back
328  
329 =cut
330  
331 30 1;
« API2/Util.pm ↑ Summary API2/Resource/XObject/Image/PNM.pm »