File Coverage

blib/lib/PDF/API3/Compat/API2/Basic/PDF/String.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 12 0.0
condition n/a
subroutine 4 11 36.3
pod 6 7 85.7
total 22 89 24.7


line stmt bran cond sub pod time code
1             #=======================================================================
2             # ____ ____ _____ _ ____ ___ ____
3             # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4             # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5             # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6             # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7             #
8             # A Perl Module Chain to faciliate the Creation and Modification
9             # of High-Quality "Portable Document Format (PDF)" Files.
10             #
11             #=======================================================================
12             #
13             # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
14             #
15             #
16             # Copyright Martin Hosken
17             #
18             # No warranty or expression of effectiveness, least of all regarding
19             # anyone's safety, is implied in this software or documentation.
20             #
21             # This specific module is licensed under the Perl Artistic License.
22             #
23             #
24             # $Id: String.pm,v 2.1 2006/06/15 20:27:06 areibens Exp $
25             #
26             #=======================================================================
27             package PDF::API3::Compat::API2::Basic::PDF::String;
28            
29             =head1 NAME
30            
31             PDF::API3::Compat::API2::Basic::PDF::String - PDF String type objects and superclass for simple objects
32             that are basically stringlike (Number, Name, etc.)
33            
34             =head1 METHODS
35            
36             =cut
37            
38 1     1   5 use strict;
  1         2  
  1         39  
39 1     1   5 use vars qw(@ISA %trans %out_trans);
  1         3  
  1         1428  
40 1     1   7 no warnings qw[ deprecated recursion uninitialized ];
  1         1  
  1         32  
41            
42 1     1   5 use PDF::API3::Compat::API2::Basic::PDF::Objind;
  1         1  
  1         913  
43             @ISA = qw(PDF::API3::Compat::API2::Basic::PDF::Objind);
44            
45             %trans = (
46             "n" => "\n",
47             "r" => "\r",
48             "t" => "\t",
49             "b" => "\b",
50             "f" => "\f",
51             "\\" => "\\",
52             "(" => "(",
53             ")" => ")"
54             );
55            
56             %out_trans = (
57             "\n" => "n",
58             "\r" => "r",
59             "\t" => "t",
60             "\b" => "b",
61             "\f" => "f",
62             "\\" => "\\",
63             "(" => "(",
64             ")" => ")"
65             );
66            
67            
68             =head2 PDF::API3::Compat::API2::Basic::PDF::String->from_pdf($string)
69            
70             Creates a new string object (not a full object yet) from a given string.
71             The string is parsed according to input criteria with escaping working.
72            
73             =cut
74            
75             sub from_pdf
76             {
77 0     0 1   my ($class, $str) = @_;
78 0           my ($self) = {};
79            
80 0           bless $self, $class;
81 0           $self->{'val'} = $self->convert($str);
82 0           $self->{' realised'} = 1;
83 0           return $self;
84             }
85            
86            
87             =head2 PDF::API3::Compat::API2::Basic::PDF::String->new($string)
88            
89             Creates a new string object (not a full object yet) from a given string.
90             The string is parsed according to input criteria with escaping working.
91            
92             =cut
93            
94             sub new
95             {
96 0     0 1   my ($class, $str) = @_;
97 0           my ($self) = {};
98            
99 0           bless $self, $class;
100 0           $self->{'val'} = $str;
101 0           $self->{' realised'} = 1;
102 0           return $self;
103             }
104            
105            
106             =head2 $s->convert($str)
107            
108             Returns $str converted as per criteria for input from PDF file
109            
110             =cut
111            
112             sub convert
113             {
114 0     0 1   my ($self, $str) = @_;
115            
116 0 0         if($str=~m|^\s*\<|o)
117             {
118             # cleaning up hex-strings, since spec is very loose,
119             # at least openoffice exporter needs this ! - fredo
120 0           $str=~s|[^0-9a-f]+||gio;
121 0           $str="<$str>";
122 0           $self->{' ishex'}=1;
123            
124 0           1 while $str =~ s/\<([0-9a-f]{2})/chr(hex($1))."\<"/oige;
  0            
125 0           $str =~ s/\<([0-9a-f]?)\>/chr(hex($1."0"))/oige;
  0            
126 0           $str =~ s/\<\>//og;
127             }
128             else
129             {
130             # if we import binary escapes,
131             # let it be hex on output -- fredo
132 0 0         if($str =~ s/\\([nrtbf\\()])/$trans{$1}/ogi)
133             {
134 0           $self->{' ishex'}=1;
135             }
136 0 0         if($str =~ s/\\([0-7]{1,3})/chr(oct($1))/oeg)
  0            
137             {
138 0           $self->{' ishex'}=1;
139             }
140             }
141            
142 0           return $str;
143             }
144            
145            
146             =head2 $s->val
147            
148             Returns the value of this string (the string itself).
149            
150             =cut
151            
152             sub val
153 0     0 1   { $_[0]->{'val'}; }
154            
155            
156             =head2 $->as_pdf
157            
158             Returns the string formatted for output as PDF for PDF File object $pdf.
159            
160             =cut
161            
162             sub as_pdf
163             {
164 0     0 1   my ($self) = @_;
165 0           my ($str) = $self->{'val'};
166            
167 0 0         if($self->{' isutf'}) {
    0          
168 0           $str = join( '', map { sprintf('%04X',$_) } unpack('U*',$str) );
  0            
169 0           return "";
170             } elsif($self->{' ishex'}) { # imported as hex ?
171 0           $str = unpack('H*',$str);
172 0           return "<$str>";
173             } else {
174 0 0         if ($str =~ m/[^\n\r\t\b\f\040-\176\200-\377]/oi)
175             {
176 0           $str =~ s/(.)/sprintf("%02X", ord($1))/oge;
  0            
177 0           return "<$str>";
178             } else
179             {
180 0           $str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/ogi;
181 0           return "($str)";
182             }
183             }
184             }
185            
186            
187             =head2 $s->outobjdeep
188            
189             Outputs the string in PDF format, complete with necessary conversions
190            
191             =cut
192            
193             sub outobjdeep
194             {
195 0     0 1   my ($self, $fh, $pdf, %opts) = @_;
196            
197 0           $fh->print($self->as_pdf ($pdf));
198             }
199            
200             sub outxmldeep
201             {
202 0     0 0   my ($self, $fh, $pdf, %opts) = @_;
203            
204 0           $opts{-xmlfh}->print("".$self->val."\n");
205             }
206