| 1 | package Text::PDF::String; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Text::PDF::String - PDF String type objects and superclass for simple objects | ||||||||||
| 6 | that are basically stringlike (Number, Name, etc.) | ||||||||||
| 7 | |||||||||||
| 8 | =head1 METHODS | ||||||||||
| 9 | |||||||||||
| 10 | =cut | ||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use vars qw(@ISA %trans %out_trans); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | # no warnings qw(uninitialized); | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Text::PDF::Objind; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | @ISA = qw(Text::PDF::Objind); | |||||||||
| 18 | |||||||||||
| 19 | 1 | %trans = ( | |||||||||
| 20 | "n" => "\n", | ||||||||||
| 21 | "r" => "\r", | ||||||||||
| 22 | "t" => "\t", | ||||||||||
| 23 | "b" => "\b", | ||||||||||
| 24 | "f" => "\f", | ||||||||||
| 25 | "\\" => "\\", | ||||||||||
| 26 | "(" => "(", | ||||||||||
| 27 | ")" => ")" | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 1 | %out_trans = ( | |||||||||
| 31 | "\n" => "n", | ||||||||||
| 32 | "\r" => "r", | ||||||||||
| 33 | "\t" => "t", | ||||||||||
| 34 | "\b" => "b", | ||||||||||
| 35 | "\f" => "f", | ||||||||||
| 36 | "\\" => "\\", | ||||||||||
| 37 | "(" => "(", | ||||||||||
| 38 | ")" => ")" | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | =head2 Text::PDF::String->from_pdf($string) | ||||||||||
| 43 | |||||||||||
| 44 | Creates a new string object (not a full object yet) from a given string. | ||||||||||
| 45 | The string is parsed according to input criteria with escaping working. | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | sub from_pdf | ||||||||||
| 50 | { | ||||||||||
| 51 | 2 | ▶ | my ($class, $str) = @_; | ||||||||
|
Subroutine
from_pdf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 2 | my ($self) = {}; | |||||||||
| 53 | |||||||||||
| 54 | 2 | bless $self, $class; | |||||||||
| 55 | 2 | $self->{'val'} = $self->convert($str); | |||||||||
| 56 | 2 | $self->{' realised'} = 1; | |||||||||
| 57 | 2 | return $self; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | =head2 Text::PDF::String->new($string) | ||||||||||
| 62 | |||||||||||
| 63 | Creates a new string object (not a full object yet) from a given string. | ||||||||||
| 64 | The string is parsed according to input criteria with escaping working. | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | sub new | ||||||||||
| 69 | { | ||||||||||
| 70 | 19 | ▶ | my ($class, $str) = @_; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 71 | 19 | my ($self) = {}; | |||||||||
| 72 | |||||||||||
| 73 | 19 | bless $self, $class; | |||||||||
| 74 | 19 | $self->{'val'} = $str; | |||||||||
| 75 | 19 | $self->{' realised'} = 1; | |||||||||
| 76 | 19 | return $self; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | =head2 $s->convert($str) | ||||||||||
| 81 | |||||||||||
| 82 | Returns $str converted as per criteria for input from PDF file | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | sub convert | ||||||||||
| 87 | { | ||||||||||
| 88 | 0 | ▶ | my ($self, $str) = @_; | ||||||||
|
Subroutine
convert: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | $str =~ s/\\([nrtbf\\()]|[0-7]+)/defined $trans{$1} ? $trans{$1} : chr(oct($1))/oegi; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | # $str =~ s/\\([0-7]+)/chr(oct($1))/oeg; # thanks to kundrat@kundrat.sk | ||||||||||
| 92 | 0 | ▶ | 1 while $str =~ s/\<([0-9a-f]{2})[\r\n]*/chr(hex($1))."\<"/oige; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $str =~ s/\<([0-9a-f]?)\>/chr(hex($1."0"))/oige; | |||||||||
| 94 | 0 | $str =~ s/\<\>//og; | |||||||||
| 95 | 0 | return $str; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | =head2 $s->val | ||||||||||
| 100 | |||||||||||
| 101 | Returns the value of this string (the string itself). | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | sub val | ||||||||||
| 106 | 6 | ▶ | { $_[0]->{'val'}; } | ||||||||
|
Subroutine
val: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | =head2 $->as_pdf | ||||||||||
| 110 | |||||||||||
| 111 | Returns the string formatted for output as PDF for PDF File object $pdf. | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | sub as_pdf | ||||||||||
| 116 | { | ||||||||||
| 117 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
as_pdf: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 118 | 0 | my ($str) = $self->{'val'}; | |||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | if ($str =~ m/[^\n\r\t\b\f\040-\176\200-\377]/oi) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | { | ||||||||||
| 122 | 0 | $str =~ s/(.)/sprintf("%02X", ord($1))/oge; | |||||||||
| 123 | 0 | return "<$str>"; | |||||||||
| 124 | } else | ||||||||||
| 125 | { | ||||||||||
| 126 | 0 | $str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/ogi; | |||||||||
| 127 | 0 | return "($str)"; | |||||||||
| 128 | } | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | =head2 $s->outobjdeep | ||||||||||
| 132 | |||||||||||
| 133 | Outputs the string in PDF format, complete with necessary conversions | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | sub outobjdeep | ||||||||||
| 138 | { | ||||||||||
| 139 | 16 | ▶ | my ($self, $fh, $pdf, %opts) = @_; | ||||||||
|
Subroutine
outobjdeep: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 140 | |||||||||||
| 141 | 16 | $fh->print($self->as_pdf ($pdf)); | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | =head2 $s->copy($inpdf, $res, $unique, $outpdf, %opts) | ||||||||||
| 146 | |||||||||||
| 147 | Copies an object. See Text::PDF::Objind::Copy() for details | ||||||||||
| 148 | |||||||||||
| 149 | =cut | ||||||||||
| 150 | |||||||||||
| 151 | sub copy | ||||||||||
| 152 | { | ||||||||||
| 153 | 0 | ▶ | my ($self, $inpdf, $res, $unique, $outpdf, %opts) = @_; | ||||||||
|
Subroutine
copy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 154 | 0 | my ($i); | |||||||||
| 155 | |||||||||||
| 156 | 0 | $res = $self->SUPER::copy($inpdf, $res, $unique, $outpdf, %opts); | |||||||||
| 157 | 0 | $res->{'val'} = $self->{'val'}; | |||||||||
| 158 | 0 | $res->{' realised'} = 1; | |||||||||
| 159 | 0 | $res; | |||||||||
| 160 | } | ||||||||||