File Coverage

blib/lib/PDF/Builder/ViewerPreferences.pm
Criterion Covered Total %
statement 15 113 13.2
branch 0 86 0.0
condition n/a
subroutine 5 12 41.6
pod 4 4 100.0
total 24 215 11.1


line stmt bran cond sub pod time code
1             package PDF::Builder::ViewerPreferences;
2              
3 1     1   1549 use strict;
  1         3  
  1         31  
4 1     1   6 use warnings;
  1         1  
  1         61  
5              
6             our $VERSION = '3.025'; # VERSION
7             our $LAST_UPDATE = '3.025'; # manually update whenever code is changed
8              
9 1     1   6 use Carp;
  1         2  
  1         53  
10 1     1   7 use PDF::Builder::Basic::PDF::Utils;
  1         2  
  1         99  
11 1     1   8 use Scalar::Util qw(weaken);
  1         3  
  1         1783  
12              
13             our @CARP_NOT;
14              
15             my @booleans = qw(HideToolbar HideMenubar HideWindowUI FitWindow CenterWindow
16             DisplayDocTitle PickTrayByPDFSize);
17              
18             my @names = qw(NonFullScreenPageMode Direction Duplex PrintScaling
19             ViewArea ViewClip PrintArea PrintClip);
20              
21             =head1 NAME
22              
23             PDF::Builder::ViewerPreferences - How the PDF should be displayed or printed
24              
25             =head1 METHODS
26              
27             This has been split out from C in L.
28              
29             =over
30              
31             =cut
32              
33             sub _snake_case {
34 0     0     my $name = shift();
35 0           $name =~ s/^([A-Z]+)/lc($1)/e;
  0            
36 0           $name =~ s/([A-Z]+)/'_' . lc($1)/ge;
  0            
37 0           $name =~ s/pdfsize/pdf_size/;
38 0           return $name;
39             }
40              
41             sub _camel_case {
42 0     0     my $name = shift();
43 0           $name = ucfirst($name);
44 0           $name =~ s/_([a-z]+)/ucfirst($1)/ge;
  0            
45 0           $name =~ s/Ui$/UI/;
46 0           $name =~ s/Pdf/PDF/;
47 0           return $name;
48             }
49              
50             =item $self = $class->new($pdf)
51              
52             Creates a new ViewerPreferences object from a PDF::Builder object.
53              
54             =cut
55              
56             sub new {
57 0     0 1   my ($class, $pdf) = @_;
58 0           my $self = bless { pdf => $pdf }, $class;
59 0           weaken $self->{'pdf'};
60 0           return $self;
61             }
62              
63             =item %preferences = $self->get_preferences()
64              
65             Returns a hash containing all of the viewer preferences that are defined in the
66             PDF.
67              
68             =cut
69              
70             sub get_preferences {
71 0     0 1   my $self = shift();
72 0           my $prefs = $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
73 0 0         return unless $prefs;
74 0           $prefs->realise();
75              
76 0           my %values;
77 0           foreach my $pref (@booleans) {
78 0 0         next unless $prefs->{$pref};
79 0 0         $values{_snake_case($pref)} = $prefs->{$pref}->val() eq 'true' ? 1 : 0;
80             }
81 0           foreach my $pref (@names) {
82 0 0         next unless $prefs->{$pref};
83 0 0         if ($pref eq 'Direction') {
    0          
    0          
84 0           $values{'direction'} = lc($prefs->{$pref}->val());
85             } elsif ($pref eq 'Duplex') {
86 0           my $value = $prefs->{$pref}->val();
87 0           $value =~ s/Flip//;
88 0           $value =~ s/Edge//;
89 0           $values{'duplex'} = _snake_case($value);
90             } elsif ($pref eq 'NonFullScreenPageMode') {
91 0           my $value = _snake_case($prefs->{$pref}->val());
92 0           $value =~ s/^use_//;
93 0 0         $value = 'optional_content' if $value eq 'oc';
94 0           $values{'non_full_screen_page_mode'} = $value;
95             } else {
96 0           $values{_snake_case($pref)} = _snake_case($prefs->{$pref}->val());
97             }
98             }
99 0 0         if ($prefs->{'PrintPageRange'}) {
100 0           my @ranges = map { $_->val() } @{$prefs->{'PrintPageRange'}};
  0            
  0            
101 0           $values{'print_page_range'} = \@ranges;
102             }
103 0 0         if ($prefs->{'NumCopies'}) {
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 if
113             not.
114              
115             =cut
116              
117             sub get_preference {
118 0     0 1   my ($self, $name) = @_;
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 0     0     my $self = shift();
132 0 0         if ($self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}) {
133 0           $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'}->realise();
134             }
135             else {
136 0           $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'} = PDFDict();
137             }
138 0           $self->{'pdf'}->{'pdf'}->out_obj($self->{'pdf'}->{'catalog'});
139 0           return $self->{'pdf'}->{'catalog'}->{'ViewerPreferences'};
140             }
141              
142             sub set_preferences {
143 0     0 1   my ($self, %values) = @_;
144 0           my $prefs = $self->_init_preferences();
145 0           local @CARP_NOT = qw(PDF::Builder);
146 0           foreach my $snake (keys %values) {
147 0           my $camel = _camel_case($snake);
148 0 0         if ($camel eq 'NonFullScreenPageMode') {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
149 0           my $value = $values{$snake};
150 0 0         my $name = ($value eq 'none' ? 'UseNone' :
    0          
    0          
    0          
151             $value eq 'outlines' ? 'UseOutlines' :
152             $value eq 'thumbnails' ? 'UseThumbs' :
153             $value eq 'optional_content' ? 'UseOC' :
154             '');
155 0 0         croak "Invalid value for $snake: $values{$snake}" unless $name;
156 0           $prefs->{$camel} = PDFName($name);
157             }
158             elsif ($camel eq 'Direction') {
159 0           my $name = $values{$snake};
160 0 0         unless ($name =~ /^(?:L2R|R2L)$/i) {
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 0         $values{$snake} eq 'art_box' ? 'ArtBox' : '');
    0          
    0          
    0          
    0          
171 0 0         croak "Invalid value for $snake: $name" unless $name;
172 0           $prefs->{$camel} = PDFName($name);
173             }
174             elsif ($camel eq 'PrintScaling') {
175 0           my $value = $values{$snake};
176 0 0         my $name = ($value eq 'none' ? 'None' :
    0          
177             $value eq 'app_default' ? 'AppDefault' : '');
178 0 0         croak "Invalid value for $snake: $name" unless $name;
179 0           $prefs->{$camel} = PDFName($name);
180             }
181             elsif ($camel eq 'Duplex') {
182 0           my $value = $values{$snake};
183 0 0         my $name = ($value eq 'simplex' ? 'Simplex' :
    0          
    0          
184             $value eq 'duplex_short' ? 'DuplexFlipShortEdge' :
185             $value eq 'duplex_long' ? 'DuplexFlipLongEdge' : '');
186 0 0         croak "Invalid value for $snake: $value" unless $name;
187 0           $prefs->{$camel} = PDFName($name);
188             }
189             elsif ($camel eq 'PrintPageRange') {
190 0 0         unless (ref($values{$snake}) eq 'ARRAY') {
191 0           croak "The value for $snake must be a reference to an array";
192             }
193 0           my @range = @{$values{$snake}};
  0            
194 0 0         unless (@range % 2 == 0) {
195 0           croak "The value for $snake must contain pairs of page numbers";
196             }
197 0 0         if (join('', @range) =~ /\D/) {
198 0           croak "The value for $snake may only contain page numbers";
199             }
200 0           $prefs->{$camel} = PDFArray(map { PDFNum($_) } @range);
  0            
201             }
202             elsif ($camel eq 'NumCopies') {
203 0 0         unless ($values{$snake} =~ /^\d+$/) {
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 0         $prefs->{$camel} = PDFBool($values{$snake} ? 1 : 0);
210             }
211             else {
212 0           croak "Unrecognized viewer preference '$snake'";
213             }
214             }
215 0           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,
267             except 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;