File Coverage

blib/lib/PDF/API2/ViewerPreferences.pm
Criterion Covered Total %
statement 47 113 41.5
branch 16 86 18.6
condition n/a
subroutine 9 12 75.0
pod 4 4 100.0
total 76 215 35.3


line stmt bran cond sub pod time code
1             package PDF::API2::ViewerPreferences;
2              
3 30     30   1706 use strict;
  30         82  
  30         984  
4 30     30   172 use warnings;
  30         67  
  30         1298  
5              
6             our $VERSION = '2.044'; # VERSION
7              
8 30     30   183 use Carp;
  30         67  
  30         1838  
9 30     30   221 use PDF::API2::Basic::PDF::Utils;
  30         77  
  30         2899  
10 30     30   220 use Scalar::Util qw(weaken);
  30         93  
  30         52011  
11              
12             our @CARP_NOT;
13              
14             my @booleans = qw(HideToolbar HideMenubar HideWindowUI FitWindow CenterWindow
15             DisplayDocTitle PickTrayByPDFSize);
16              
17             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     0   0 my $name = shift();
32 0         0 $name =~ s/^([A-Z]+)/lc($1)/e;
  0         0  
33 0         0 $name =~ s/([A-Z]+)/'_' . lc($1)/ge;
  0         0  
34 0         0 $name =~ s/pdfsize/pdf_size/;
35 0         0 return $name;
36             }
37              
38             sub _camel_case {
39 172     172   310 my $name = shift();
40 172         579 $name = ucfirst($name);
41 172         1293 $name =~ s/_([a-z]+)/ucfirst($1)/ge;
  676         2459  
42 172         435 $name =~ s/Ui$/UI/;
43 172         333 $name =~ s/Pdf/PDF/;
44 172         466 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     172 1 389 my ($class, $pdf) = @_;
55 172         491 my $self = bless { pdf => $pdf }, $class;
56 172         800 weaken $self->{'pdf'};
57 172         483 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     0 1 0 my $self = shift();
69 0         0 my $prefs = $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
70 0 0       0 return unless $prefs;
71 0         0 $prefs->realise();
72              
73 0         0 my %values;
74 0         0 foreach my $pref (@booleans) {
75 0 0       0 next unless $prefs->{$pref};
76 0 0       0 $values{_snake_case($pref)} = $prefs->{$pref}->val() eq 'true' ? 1 : 0;
77             }
78 0         0 foreach my $pref (@names) {
79 0 0       0 next unless $prefs->{$pref};
80 0 0       0 if ($pref eq 'Direction') {
    0          
    0          
81 0         0 $values{'direction'} = lc($prefs->{$pref}->val());
82             }
83             elsif ($pref eq 'Duplex') {
84 0         0 my $value = $prefs->{$pref}->val();
85 0         0 $value =~ s/Flip//;
86 0         0 $value =~ s/Edge//;
87 0         0 $values{'duplex'} = _snake_case($value);
88             }
89             elsif ($pref eq 'NonFullScreenPageMode') {
90 0         0 my $value = _snake_case($prefs->{$pref}->val());
91 0         0 $value =~ s/^use_//;
92 0 0       0 $value = 'optional_content' if $value eq 'oc';
93 0         0 $values{'non_full_screen_page_mode'} = $value;
94             }
95             else {
96 0         0 $values{_snake_case($pref)} = _snake_case($prefs->{$pref}->val());
97             }
98             }
99 0 0       0 if ($prefs->{'PrintPageRange'}) {
100 0         0 my @ranges = map { $_->val() } @{$prefs->{'PrintPageRange'}};
  0         0  
  0         0  
101 0         0 $values{'print_page_range'} = \@ranges;
102             }
103 0 0       0 if ($prefs->{'NumCopies'}) {
104 0         0 $values{'num_copies'} = $prefs->{'NumCopies'}->val();
105             }
106              
107 0         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 if
113             not.
114              
115             =cut
116              
117             sub get_preference {
118 0     0 1 0 my ($self, $name) = @_;
119 0         0 my %values = $self->get_preferences();
120 0         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     172   294 my $self = shift();
132 172 100       480 if ($self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}) {
133 8         27 $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}->realise();
134             }
135             else {
136 164         431 $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'} = PDFDict();
137             }
138 172         658 $self->{'pdf'}->{'pdf'}->out_obj($self->{'pdf'}->{'catalog'});
139 172         347 return $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
140             }
141              
142             sub set_preferences {
143 172     172 1 591 my ($self, %values) = @_;
144 172         453 my $prefs = $self->_init_preferences();
145 172         746 local @CARP_NOT = qw(PDF::API2);
146 172         578 foreach my $snake (keys %values) {
147 172         419 my $camel = _camel_case($snake);
148 172 100       507 if ($camel eq 'NonFullScreenPageMode') {
    50          
    50          
    50          
    50          
    0          
    0          
    0          
149 169         350 my $value = $values{$snake};
150 169 0       404 my $name = ($value eq 'none' ? 'UseNone' :
    0          
    0          
    50          
151             $value eq 'outlines' ? 'UseOutlines' :
152             $value eq 'thumbnails' ? 'UseThumbs' :
153             $value eq 'optional_content' ? 'UseOC' :
154             '');
155 169 50       396 croak "Invalid value for $snake: $values{$snake}" unless $name;
156 169         473 $prefs->{$camel} = PDFName($name);
157             }
158             elsif ($camel eq 'Direction') {
159 0         0 my $name = $values{$snake};
160 0 0       0 unless ($name =~ /^(?:L2R|R2L)$/i) {
161 0         0 croak "Invalid value for $snake: $name";
162             }
163 0         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 0       0 $values{$snake} eq 'art_box' ? 'ArtBox' : '');
    0          
    0          
    0          
    0          
171 0 0       0 croak "Invalid value for $snake: $name" unless $name;
172 0         0 $prefs->{$camel} = PDFName($name);
173             }
174             elsif ($camel eq 'PrintScaling') {
175 0         0 my $value = $values{$snake};
176 0 0       0 my $name = ($value eq 'none' ? 'None' :
    0          
177             $value eq 'app_default' ? 'AppDefault' : '');
178 0 0       0 croak "Invalid value for $snake: $name" unless $name;
179 0         0 $prefs->{$camel} = PDFName($name);
180             }
181             elsif ($camel eq 'Duplex') {
182 3         6 my $value = $values{$snake};
183 3 50       11 my $name = ($value eq 'simplex' ? 'Simplex' :
    100          
    100          
184             $value eq 'duplex_short' ? 'DuplexFlipShortEdge' :
185             $value eq 'duplex_long' ? 'DuplexFlipLongEdge' : '');
186 3 50       8 croak "Invalid value for $snake: $value" unless $name;
187 3         7 $prefs->{$camel} = PDFName($name);
188             }
189             elsif ($camel eq 'PrintPageRange') {
190 0 0       0 unless (ref($values{$snake}) eq 'ARRAY') {
191 0         0 croak "The value for $snake must be a reference to an array";
192             }
193 0         0 my @range = @{$values{$snake}};
  0         0  
194 0 0       0 unless (@range % 2 == 0) {
195 0         0 croak "The value for $snake must contain pairs of page numbers";
196             }
197 0 0       0 if (join('', @range) =~ /\D/) {
198 0         0 croak "The value for $snake may only contain page numbers";
199             }
200 0         0 $prefs->{$camel} = PDFArray(map { PDFNum($_) } @range);
  0         0  
201             }
202             elsif ($camel eq 'NumCopies') {
203 0 0       0 unless ($values{$snake} =~ /^\d+$/) {
204 0         0 croak "$snake: $values{$snake} is not an integer";
205             }
206 0         0 $prefs->{$camel} = PDFNum($values{$snake});
207             }
208 0         0 elsif (grep { $camel eq $_ } @booleans) {
209 0 0       0 $prefs->{$camel} = PDFBool($values{$snake} ? 1 : 0);
210             }
211             else {
212 0         0 croak "Unrecognized viewer preference '$snake'";
213             }
214             }
215 172         884 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, C,
228             C, C, or C.
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 in L except
267             that the C and C 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 represents no page scaling, and C
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             1;