File Coverage

blib/lib/PDF/API2/Basic/PDF/Name.pm
Criterion Covered Total %
statement 26 28 92.8
branch 2 4 50.0
condition 5 12 41.6
subroutine 7 7 100.0
pod 5 5 100.0
total 45 56 80.3


line stmt bran cond sub pod time code
1             # Code in the PDF::API2::Basic::PDF namespace was originally copied from the
2             # Text::PDF distribution.
3             #
4             # Copyright Martin Hosken
5             #
6             # Martin Hosken's code may be used under the terms of the MIT license.
7             # Subsequent versions of the code have the same license as PDF::API2.
8              
9             package PDF::API2::Basic::PDF::Name;
10              
11 40     40   305 use base 'PDF::API2::Basic::PDF::String';
  40         128  
  40         4494  
12              
13 40     40   260 use strict;
  40         90  
  40         19180  
14              
15             our $VERSION = '2.043'; # VERSION
16              
17             =head1 NAME
18              
19             PDF::API2::Basic::PDF::Name - Low-level PDF name object
20              
21             =head1 METHODS
22              
23             =head2 PDF::API2::Basic::PDF::Name->from_pdf($string)
24              
25             Creates a new string object (not a full object yet) from a given
26             string. The string is parsed according to input criteria with
27             escaping working, particular to Names.
28              
29             =cut
30              
31             sub from_pdf {
32 276     276 1 590 my ($class, $string, $pdf) = @_;
33 276         730 my ($self) = $class->SUPER::from_pdf($string);
34              
35 276         530 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
36 276         654 return $self;
37             }
38              
39             =head2 $n->convert ($string, $pdf)
40              
41             Converts a name into a string by removing the / and converting any hex
42             munging.
43              
44             =cut
45              
46             sub convert {
47 276     276 1 523 my ($self, $string, $pdf) = @_;
48              
49 276         500 $string = name_to_string($string, $pdf);
50 276         875 return $string;
51             }
52              
53             =head2 $s->as_pdf ($pdf)
54              
55             Returns a name formatted as PDF. $pdf is optional but should be the
56             PDF File object for which the name is intended if supplied.
57              
58             =cut
59              
60             sub as_pdf {
61 2337     2337 1 3310 my ($self, $pdf) = @_;
62 2337         3664 my $string = $self->{'val'};
63              
64 2337         3488 $string = string_to_name($string, $pdf);
65 2337         7037 return '/' . $string;
66             }
67              
68              
69             # Prior to PDF version 1.2, '#' was a literal character. Embedded
70             # spaces were implicitly allowed in names as well but it would be best
71             # to ignore that (PDF 1.3, section H.3.2.4.3).
72              
73             =head2 PDF::API2::Basic::PDF::Name->string_to_name ($string, $pdf)
74              
75             Suitably encode the string $string for output in the File object $pdf
76             (the exact format may depend on the version of $pdf).
77              
78             =cut
79              
80             sub string_to_name {
81 5568     5568 1 8779 my ($string, $pdf) = @_;
82              
83             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
84 5568 50 33     21459 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
85 5568         9875 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
  0         0  
86             }
87              
88 5568         15708 return $string;
89             }
90              
91             =head2 PDF::API2::Basic::PDF::Name->name_to_string ($string, $pdf)
92              
93             Suitably decode the string $string as read from the File object $pdf
94             (the exact decoding may depend on the version of $pdf). Principally,
95             undo the hex encoding for PDF versions > 1.1.
96              
97             =cut
98              
99             sub name_to_string {
100 939     939 1 1794 my ($string, $pdf) = @_;
101 939         1397 $string =~ s|^/||o;
102              
103             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
104 939 50 66     3562 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
105 939         1550 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
  0         0  
106             }
107              
108 939         2024 return $string;
109             }
110              
111             1;