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   300 use base 'PDF::API2::Basic::PDF::String';
  40         131  
  40         4231  
12              
13 40     40   310 use strict;
  40         133  
  40         19272  
14              
15             our $VERSION = '2.044'; # 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 287     287 1 622 my ($class, $string, $pdf) = @_;
33 287         742 my ($self) = $class->SUPER::from_pdf($string);
34              
35 287         601 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
36 287         683 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 287     287 1 548 my ($self, $string, $pdf) = @_;
48              
49 287         513 $string = name_to_string($string, $pdf);
50 287         917 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 2477     2477 1 3825 my ($self, $pdf) = @_;
62 2477         4855 my $string = $self->{'val'};
63              
64 2477         3862 $string = string_to_name($string, $pdf);
65 2477         7657 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 5848     5848 1 9629 my ($string, $pdf) = @_;
82              
83             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
84 5848 50 33     23418 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
85 5848         10726 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
  0         0  
86             }
87              
88 5848         16588 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 972     972 1 1889 my ($string, $pdf) = @_;
101 972         1577 $string =~ s|^/||o;
102              
103             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
104 972 50 66     3689 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
105 972         1660 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
  0         0  
106             }
107              
108 972         2193 return $string;
109             }
110              
111             1;