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 41     41   300 use base 'PDF::API2::Basic::PDF::String';
  41         108  
  41         4030  
12              
13 41     41   267 use strict;
  41         87  
  41         18517  
14              
15             our $VERSION = '2.045'; # 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 576 my ($class, $string, $pdf) = @_;
33 287         677 my ($self) = $class->SUPER::from_pdf($string);
34              
35 287         519 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
36 287         632 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 488 my ($self, $string, $pdf) = @_;
48              
49 287         486 $string = name_to_string($string, $pdf);
50 287         792 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 3575 my ($self, $pdf) = @_;
62 2477         3764 my $string = $self->{'val'};
63              
64 2477         3703 $string = string_to_name($string, $pdf);
65 2477         7169 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 9208 my ($string, $pdf) = @_;
82              
83             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
84 5848 50 33     22541 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
85 5848         10251 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
  0         0  
86             }
87              
88 5848         15880 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 1767 my ($string, $pdf) = @_;
101 972         1318 $string =~ s|^/||o;
102              
103             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
104 972 50 66     3446 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
105 972         1416 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
  0         0  
106             }
107              
108 972         1925 return $string;
109             }
110              
111             1;