File Coverage

blib/lib/Text/PDF/Name.pm
Criterion Covered Total %
statement 20 31 64.5
branch 2 4 50.0
condition 4 12 33.3
subroutine 6 8 75.0
pod 5 5 100.0
total 37 60 61.6


line stmt bran cond sub pod time code
1             package Text::PDF::Name;
2              
3 1     1   3 use strict;
  1         1  
  1         24  
4 1     1   3 use vars qw(@ISA);
  1         1  
  1         29  
5             # no warnings qw(uninitialized);
6              
7 1     1   3 use Text::PDF::String;
  1         1  
  1         281  
8             @ISA = qw(Text::PDF::String);
9              
10             =head1 NAME
11              
12             Text::PDF::Name - Inherits from L and stores PDF names (things
13             beginning with /)
14              
15             =head1 METHODS
16              
17             =head2 Text::PDF::Name->from_pdf($string)
18              
19             Creates a new string object (not a full object yet) from a given string.
20             The string is parsed according to input criteria with escaping working, particular
21             to Names.
22              
23             =cut
24              
25              
26             sub from_pdf
27             {
28 0     0 1 0 my ($class, $str, $pdf) = @_;
29 0         0 my ($self) = $class->SUPER::from_pdf($str);
30              
31 0         0 $self->{'val'} = name_to_string ($self->{'val'}, $pdf);
32 0         0 $self;
33             }
34              
35             =head2 $n->convert ($str, $pdf)
36              
37             Converts a name into a string by removing the / and converting any hex
38             munging unless $pdf is supplied and its version is less than 1.2.
39              
40             =cut
41              
42             sub convert
43             {
44 0     0 1 0 my ($self, $str, $pdf) = @_;
45              
46 0         0 $str = name_to_string ($str, $pdf);
47 0         0 return $str;
48             }
49              
50              
51             =head2 $s->as_pdf ($pdf)
52              
53             Returns a name formatted as PDF. $pdf is optional but should be the
54             PDF File object for which the name is intended if supplied.
55              
56             =cut
57              
58             sub as_pdf
59             {
60 9     9 1 6 my ($self, $pdf) = @_;
61 9         10 my ($str) = $self->{'val'};
62              
63 9         10 $str = string_to_name ($str, $pdf);
64 9         19 return ("/" . $str);
65             }
66              
67              
68             # Prior to PDF version 1.2, `#' was a literal character. Embedded
69             # spaces were implicitly allowed in names as well but it would be best
70             # to ignore that (PDF reference 2nd edition, Appendix H, section 3.2.4.3).
71              
72             =head2 Text::PDF::Name->string_to_name ($str, $pdf)
73              
74             Suitably encode the string $str for output in the File object $pdf
75             (the exact format may depend on the version of $pdf). Prinicipally,
76             encode certain characters in hex if the version is greater than 1.1.
77              
78             =cut
79              
80             sub string_to_name ($;$)
81             {
82 24     24 1 15 my ($str, $pdf) = @_;
83 24 50 33     66 if (!defined($pdf) || (defined $pdf->{' version'} && $pdf->{' version'} >= 2))
      33        
84 0         0 { $str =~ s|([\001-\040\177-\377%()\[\]{}<>#/])|"#".sprintf("%02X", ord($1))|oge; }
  0         0  
85 24         30 return $str;
86             }
87              
88             =head2 Text::PDF::Name->name_to_string ($str, $pdf)
89              
90             Suitably decode the string $str as read from the File object $pdf (the
91             exact decoding may depend on the version of $pdf). Principally, undo
92             the hex encoding for PDF versions > 1.1.
93              
94             =cut
95            
96             sub name_to_string ($;$)
97             {
98 3     3 1 6 my ($str, $pdf) = @_;
99 3         2 $str =~ s|^/||o;
100              
101 3 50 33     12 if (!defined($pdf) || (defined $pdf->{' version'} && $pdf->{' version'} >= 2))
      33        
102 0         0 { $str =~ s/#([0-9a-f]{2})/chr(hex($1))/oige; }
  0         0  
103 3         6 return $str;
104             }
105