File Coverage

blib/lib/PDF/API3/Compat/API2/Basic/PDF/Name.pm
Criterion Covered Total %
statement 12 35 34.2
branch 0 2 0.0
condition 0 6 0.0
subroutine 4 10 40.0
pod 5 6 83.3
total 21 59 35.5


line stmt bran cond sub pod time code
1             #=======================================================================
2             # ____ ____ _____ _ ____ ___ ____
3             # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4             # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5             # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6             # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7             #
8             # A Perl Module Chain to faciliate the Creation and Modification
9             # of High-Quality "Portable Document Format (PDF)" Files.
10             #
11             #=======================================================================
12             #
13             # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
14             #
15             #
16             # Copyright Martin Hosken
17             #
18             # No warranty or expression of effectiveness, least of all regarding
19             # anyone's safety, is implied in this software or documentation.
20             #
21             # This specific module is licensed under the Perl Artistic License.
22             #
23             #
24             # $Id: Name.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25             #
26             #=======================================================================
27             package PDF::API3::Compat::API2::Basic::PDF::Name;
28            
29 1     1   7 use strict;
  1         2  
  1         52  
30 1     1   5 use vars qw(@ISA);
  1         3  
  1         46  
31 1     1   6 no warnings qw[ deprecated recursion uninitialized ];
  1         2  
  1         37  
32            
33 1     1   634 use PDF::API3::Compat::API2::Basic::PDF::String;
  1         3  
  1         396  
34             @ISA = qw(PDF::API3::Compat::API2::Basic::PDF::String);
35            
36             =head1 NAME
37            
38             PDF::API3::Compat::API2::Basic::PDF::Name - Inherits from L and stores PDF names (things
39             beginning with /)
40            
41             =head1 METHODS
42            
43             =head2 PDF::API3::Compat::API2::Basic::PDF::Name->from_pdf($string)
44            
45             Creates a new string object (not a full object yet) from a given string.
46             The string is parsed according to input criteria with escaping working, particular
47             to Names.
48            
49             =cut
50            
51            
52             sub from_pdf
53             {
54 0     0 1   my ($class, $str, $pdf) = @_;
55 0           my ($self) = $class->SUPER::from_pdf($str);
56            
57 0           $self->{'val'} = name_to_string ($self->{'val'}, $pdf);
58 0           $self;
59             }
60            
61             =head2 $n->convert ($str, $pdf)
62            
63             Converts a name into a string by removing the / and converting any hex
64             munging unless $pdf is supplied and its version is less than 1.2.
65            
66             =cut
67            
68             sub convert
69             {
70 0     0 1   my ($self, $str, $pdf) = @_;
71            
72 0           $str = name_to_string ($str, $pdf);
73 0           return $str;
74             }
75            
76            
77             =head2 $s->as_pdf ($pdf)
78            
79             Returns a name formatted as PDF. $pdf is optional but should be the
80             PDF File object for which the name is intended if supplied.
81            
82             =cut
83            
84             sub as_pdf
85             {
86 0     0 1   my ($self, $pdf) = @_;
87 0           my ($str) = $self->{'val'};
88            
89 0           $str = string_to_name ($str, $pdf);
90 0           return ("/" . $str);
91             }
92            
93            
94             # Prior to PDF version 1.2, `#' was a literal character. Embedded
95             # spaces were implicitly allowed in names as well but it would be best
96             # to ignore that (PDF reference 2nd edition, Appendix H, section 3.2.4.3).
97            
98             =head2 PDF::API3::Compat::API2::Basic::PDF::Name->string_to_name ($str, $pdf)
99            
100             Suitably encode the string $str for output in the File object $pdf
101             (the exact format may depend on the version of $pdf). Prinicipally,
102             encode certain characters in hex if the version is greater than 1.1.
103            
104             =cut
105            
106             sub string_to_name ($;$)
107             {
108 0     0 1   my ($str, $pdf) = @_;
109             # if (!(defined ($pdf) && $pdf->{' version'} < 2))
110             # {
111 0           $str =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|"#".sprintf("%02X", ord($1))|oge;
  0            
112             # }
113 0           return $str;
114             }
115            
116             =head2 PDF::API3::Compat::API2::Basic::PDF::Name->name_to_string ($str, $pdf)
117            
118             Suitably decode the string $str as read from the File object $pdf (the
119             exact decoding may depend on the version of $pdf). Principally, undo
120             the hex encoding for PDF versions > 1.1.
121            
122             =cut
123            
124             sub name_to_string ($;$)
125             {
126 0     0 1   my ($str, $pdf) = @_;
127 0           $str =~ s|^/||o;
128            
129 0 0 0       if (!(defined ($pdf) && $pdf->{' version'} && $pdf->{' version'} < 2))
      0        
130 0           { $str =~ s/#([0-9a-f]{2})/chr(hex($1))/oige; }
  0            
131 0           return $str;
132             }
133            
134             sub outxmldeep
135             {
136 0     0 0   my ($self, $fh, $pdf, %opts) = @_;
137            
138 0           $opts{-xmlfh}->print("".$self->val."\n");
139             }