API2/Basic/PDF/Name.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 5 / 5 totaltotal 75.0% 48 / 64 SCAR 20.2
CC
7
Cov
78%
string_to_name
5.6
name_to_string
4.4
BEGIN
1.0
CRAP 7.5
« API2/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary API2/Resource/XObject/Form/BarCode/code3of9.pm »
1 # Code in the PDF::API2::Basic::PDF namespace was originally copied from the
2 # Text::PDF distribution.
3 #
4 # Copyright Martin Hosken <Martin_Hosken@sil.org>
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 ▶use base 'PDF::API2::Basic::PDF::String';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 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 ▶ my ($class, $string, $pdf) = @_;
Subroutine from_pdf: called CC=1 SCAR=0.0
Pod documented
33 287 my ($self) = $class->SUPER::from_pdf($string);
34  
35 287 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
36 287 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 ▶ my ($self, $string, $pdf) = @_;
Subroutine convert: called CC=1 SCAR=0.0
Pod documented
48  
49 287 $string = name_to_string($string, $pdf);
50 287 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 ▶ my ($self, $pdf) = @_;
Subroutine as_pdf: called CC=1 SCAR=0.0
Pod documented
62 2477 my $string = $self->{'val'};
63  
64 2477 $string = string_to_name($string, $pdf);
65 2477 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 ▶ my ($string, $pdf) = @_;
Subroutine string_to_name: called CC=4 SCAR=17.2
Pod documented
82  
83 # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
84 5848 ▶ unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2") 5848 0 5848
Condition33%2/6
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
!ll&&!rl&&r
058480
$pdf and $pdf->{" version"}
!ll&&!rl&&r
005848
MC/DC0%0/3
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
$pdf $pdf->{" version"} $pdf->{" version"} < "1.2"
Truth table25%1/4
A and B and C
A= $pdfB= $pdf->{" version"}C= $pdf->{" version"} < "1.2"
ABCresult
0XX0
10X0
1100
1111
85 5848 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
86 }
87  
88 5848 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 ▶ my ($string, $pdf) = @_;
Subroutine name_to_string: called CC=4 SCAR=14.8
Pod documented
101 972 $string =~ s|^/||o;
102  
103 # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
104 972 ▶ unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2") 685 0 685
Condition66%4/6
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
!ll&&!rl&&r
2876850
$pdf and $pdf->{" version"}
!ll&&!rl&&r
2870685
MC/DC0%0/3
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
$pdf $pdf->{" version"} $pdf->{" version"} < "1.2"
Truth table50%2/4
A and B and C
A= $pdfB= $pdf->{" version"}C= $pdf->{" version"} < "1.2"
ABCresult
0XX0
10X0
1100
1111
105 972 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
106 }
107  
108 972 return $string;
109 }
110  
111 40 1;
« API2/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary API2/Resource/XObject/Form/BarCode/code3of9.pm »