| 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::String; | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use base 'PDF::API2::Basic::PDF::Objind'; | ||||||||||||||||||||||||||||||||
|
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::String - Low-level PDF string object | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 40 | our %trans = ( | |||||||||||||||||||||||||||||||||
| 26 | 'n' => "\n", | ||||||||||||||||||||||||||||||||||
| 27 | 'r' => "\r", | ||||||||||||||||||||||||||||||||||
| 28 | 't' => "\t", | ||||||||||||||||||||||||||||||||||
| 29 | 'b' => "\b", | ||||||||||||||||||||||||||||||||||
| 30 | 'f' => "\f", | ||||||||||||||||||||||||||||||||||
| 31 | "\\" => "\\", | ||||||||||||||||||||||||||||||||||
| 32 | '(' => '(', | ||||||||||||||||||||||||||||||||||
| 33 | ')' => ')', | ||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 40 | our %out_trans = ( | |||||||||||||||||||||||||||||||||
| 37 | "\n" => 'n', | ||||||||||||||||||||||||||||||||||
| 38 | "\r" => 'r', | ||||||||||||||||||||||||||||||||||
| 39 | "\t" => 't', | ||||||||||||||||||||||||||||||||||
| 40 | "\b" => 'b', | ||||||||||||||||||||||||||||||||||
| 41 | "\f" => 'f', | ||||||||||||||||||||||||||||||||||
| 42 | "\\" => "\\", | ||||||||||||||||||||||||||||||||||
| 43 | '(' => '(', | ||||||||||||||||||||||||||||||||||
| 44 | ')' => ')', | ||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | =head2 PDF::API2::Basic::PDF::String->from_pdf($string) | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | Creates a new string object (not a full object yet) from a given | ||||||||||||||||||||||||||||||||||
| 50 | string. The string is parsed according to input criteria with | ||||||||||||||||||||||||||||||||||
| 51 | escaping working. | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | sub from_pdf { | ||||||||||||||||||||||||||||||||||
| 56 | 399 | ▶ | my ($class, $str) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
from_pdf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 57 | 399 | my $self = {}; | |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 399 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 60 | 399 | $self->{'val'} = $self->convert($str); | |||||||||||||||||||||||||||||||||
| 61 | 399 | $self->{' realised'} = 1; | |||||||||||||||||||||||||||||||||
| 62 | 399 | return $self; | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | =head2 PDF::API2::Basic::PDF::String->new($string) | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | Creates a new string object (not a full object yet) from a given | ||||||||||||||||||||||||||||||||||
| 68 | string. The string is parsed according to input criteria with | ||||||||||||||||||||||||||||||||||
| 69 | escaping working. | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | sub new { | ||||||||||||||||||||||||||||||||||
| 74 | 30503 | ▶ | my ($class, $str) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 75 | 30503 | my $self = {}; | |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 30503 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 78 | 30503 | $self->{'val'} = $str; | |||||||||||||||||||||||||||||||||
| 79 | 30503 | $self->{' realised'} = 1; | |||||||||||||||||||||||||||||||||
| 80 | 30503 | return $self; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | =head2 $s->convert($str) | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | Returns $str converted as per criteria for input from PDF file | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | sub convert { | ||||||||||||||||||||||||||||||||||
| 90 | 30 | ▶ | my ($self, $input) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
convert: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 91 | 30 | my $output = ''; | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | # Hexadecimal Strings (PDF 1.7 section 7.3.4.3) | ||||||||||||||||||||||||||||||||||
| 94 | 30 | ▶ | if ($input =~ m|^\s*\<|o) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 3 | $self->{' ishex'} = 1; | |||||||||||||||||||||||||||||||||
| 96 | 3 | $output = $input; | |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | # Remove any extraneous characters to simplify processing | ||||||||||||||||||||||||||||||||||
| 99 | 3 | $output =~ s/[^0-9a-f]+//gio; | |||||||||||||||||||||||||||||||||
| 100 | 3 | $output = "<$output>"; | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | # Convert each sequence of two hexadecimal characters into a byte | ||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | 1 while $output =~ s/\<([0-9a-f]{2})/chr(hex($1)) . '<'/oige; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | # If a single hexadecimal character remains, append 0 and | ||||||||||||||||||||||||||||||||||
| 106 | # convert it into a byte. | ||||||||||||||||||||||||||||||||||
| 107 | 3 | $output =~ s/\<([0-9a-f])\>/chr(hex($1 . '0'))/oige; | |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | # Remove surrounding angle brackets | ||||||||||||||||||||||||||||||||||
| 110 | 3 | $output =~ s/\<\>//og; | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | # Literal Strings (PDF 1.7 section 7.3.4.2) | ||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||
| 115 | # Remove surrounding parentheses | ||||||||||||||||||||||||||||||||||
| 116 | 27 | $input =~ s/^\s*\((.*)\)\s*$/$1/os; | |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 27 | my $cr = '(?:\015\012|\015|\012)'; | |||||||||||||||||||||||||||||||||
| 119 | 27 | my $prev_input; | |||||||||||||||||||||||||||||||||
| 120 | 27 | ▶ | while ($input) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 52 | ▶ | if (defined $prev_input and $input eq $prev_input) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $prev_input and $input eq $prev_input
MC/DC0%0/2
defined $prev_input and $input eq $prev_input
defined $prev_input
$input eq $prev_input
Truth table66%2/3
A and B
A= defined $prev_inputB= $input eq $prev_input
| |||||||||||||||||||||||||||||||||||
| 122 | 0 | die "Infinite loop while parsing literal string"; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | 52 | $prev_input = $input; | |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | # Convert bachslash followed by up to three octal digits | ||||||||||||||||||||||||||||||||||
| 127 | # into that binary byte | ||||||||||||||||||||||||||||||||||
| 128 | 52 | ▶ | if ($input =~ /^\\([0-7]{1,3})(.*)/os) { | ||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||
| 129 | 13 | $output .= chr(oct($1)); | |||||||||||||||||||||||||||||||||
| 130 | 13 | $input = $2; | |||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||
| 132 | # Convert backslash followed by an escaped character into that | ||||||||||||||||||||||||||||||||||
| 133 | # character | ||||||||||||||||||||||||||||||||||
| 134 | elsif ($input =~ /^\\([nrtbf\\\(\)])(.*)/osi) { | ||||||||||||||||||||||||||||||||||
| 135 | 9 | $output .= $trans{$1}; | |||||||||||||||||||||||||||||||||
| 136 | 9 | $input = $2; | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | # Ignore backslash followed by an end-of-line marker | ||||||||||||||||||||||||||||||||||
| 139 | elsif ($input =~ /^\\$cr(.*)/os) { | ||||||||||||||||||||||||||||||||||
| 140 | 5 | $input = $1; | |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | # Convert an unescaped end-of-line marker to a line-feed | ||||||||||||||||||||||||||||||||||
| 143 | elsif ($input =~ /^\015\012?(.*)/os) { | ||||||||||||||||||||||||||||||||||
| 144 | 2 | $output .= "\012"; | |||||||||||||||||||||||||||||||||
| 145 | 2 | $input = $1; | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | # Check to see if there are any other special sequences | ||||||||||||||||||||||||||||||||||
| 148 | elsif ($input =~ /^(.*?)((?:\\(?:[nrtbf\\\(\)0-7]|$cr)|\015\012?).*)/os) { | ||||||||||||||||||||||||||||||||||
| 149 | 10 | $output .= $1; | |||||||||||||||||||||||||||||||||
| 150 | 10 | $input = $2; | |||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||||||||
| 153 | 13 | $output .= $input; | |||||||||||||||||||||||||||||||||
| 154 | 13 | $input = undef; | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 30 | return $output; | |||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | =head2 $s->val | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | Returns the value of this string (the string itself). | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | sub val { | ||||||||||||||||||||||||||||||||||
| 170 | 1050 | ▶ | return $_[0]->{'val'}; | ||||||||||||||||||||||||||||||||
|
Subroutine
val: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | =head2 $->as_pdf | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | Returns the string formatted for output as PDF for PDF File object $pdf. | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | sub as_pdf { | ||||||||||||||||||||||||||||||||||
| 181 | 164 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
as_pdf: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 182 | 164 | my $str = $self->{'val'}; | |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 164 | ▶ | if ($self->{' ishex'}) { # imported as hex ? | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 185 | 1 | $str = unpack('H*', $str); | |||||||||||||||||||||||||||||||||
| 186 | 1 | return "<$str>"; | |||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||
| 188 | ▶ | elsif ($self->{' isutf'} or (utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/)) { | |||||||||||||||||||||||||||||||||
|
Condition66%4/6
$self->{" isutf"} or utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
MC/DC33%1/3
$self->{" isutf"} or utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
$self->{" isutf"}
utf8::is_utf8($str)
$str =~ /[^[:ascii:]]/
Truth table50%2/4
A or B and C
A= $self->{" isutf"}B= utf8::is_utf8($str)C= $str =~ /[^[:ascii:]]/
| |||||||||||||||||||||||||||||||||||
| 189 | 1 | $str = join('', map { sprintf('%04X' , $_) } unpack('U*', $str) ); | |||||||||||||||||||||||||||||||||
| 190 | 1 | return "<FEFF$str>"; | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||||||||
| 193 | 162 | ▶ | if ($str =~ m/[^\n\r\t\b\f\040-\176\200-\377]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 194 | 3 | $str =~ s/(.)/sprintf('%02X', ord($1))/sge; | |||||||||||||||||||||||||||||||||
| 195 | 3 | return "<$str>"; | |||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||||||||
| 198 | 159 | $str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/g; | |||||||||||||||||||||||||||||||||
| 199 | 159 | return "($str)"; | |||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | =head2 $s->outobjdeep | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | Outputs the string in PDF format, complete with necessary conversions | ||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||
| 210 | sub outobjdeep { | ||||||||||||||||||||||||||||||||||
| 211 | 3218 | ▶ | my ($self, $fh, $pdf) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
outobjdeep: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | 3218 | $fh->print($self->as_pdf($pdf)); | |||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||
| 216 | 40 | 1; | |||||||||||||||||||||||||||||||||