~ubuntu-branches/ubuntu/maverick/libtext-pdf-perl/maverick

« back to all changes in this revision

Viewing changes to lib/Text/PDF/String.pm

  • Committer: Bazaar Package Importer
  • Author(s): Gunnar Wolf
  • Date: 2003-06-19 13:37:58 UTC
  • Revision ID: james.westby@ubuntu.com-20030619133758-6qa8nc1qryh9fv33
Tags: upstream-0.25
ImportĀ upstreamĀ versionĀ 0.25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
use strict;
 
13
use vars qw(@ISA %trans %out_trans);
 
14
# no warnings qw(uninitialized);
 
15
 
 
16
use Text::PDF::Objind;
 
17
@ISA = qw(Text::PDF::Objind);
 
18
 
 
19
%trans = (
 
20
    "n" => "\n",
 
21
    "r" => "\r",
 
22
    "t" => "\t",
 
23
    "b" => "\b",
 
24
    "f" => "\f",
 
25
    "\\" => "\\",
 
26
    "(" => "(",
 
27
    ")" => ")"
 
28
        );
 
29
 
 
30
%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
    my ($class, $str) = @_;
 
52
    my ($self) = {};
 
53
 
 
54
    bless $self, $class;
 
55
    $self->{'val'} = $self->convert($str);
 
56
    $self->{' realised'} = 1;
 
57
    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
    my ($class, $str) = @_;
 
71
    my ($self) = {};
 
72
 
 
73
    bless $self, $class;
 
74
    $self->{'val'} = $str;
 
75
    $self->{' realised'} = 1;
 
76
    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
    my ($self, $str) = @_;
 
89
 
 
90
    $str =~ s/\\([nrtbf\\()]|[0-7]+)/defined $trans{$1} ? $trans{$1} : chr(oct($1))/oegi;
 
91
#    $str =~ s/\\([0-7]+)/chr(oct($1))/oeg;              # thanks to kundrat@kundrat.sk
 
92
    1 while $str =~ s/\<([0-9a-f]{2})[\r\n]*/chr(hex($1))."\<"/oige;
 
93
    $str =~ s/\<([0-9a-f]?)\>/chr(hex($1."0"))/oige;
 
94
    $str =~ s/\<\>//og;
 
95
    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
{ $_[0]->{'val'}; }
 
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
    my ($self) = @_;
 
118
    my ($str) = $self->{'val'};
 
119
 
 
120
    if ($str =~ m/[^\n\r\t\b\f\040-\176\200-\377]/oi)
 
121
    {
 
122
        $str =~ s/(.)/sprintf("%02X", ord($1))/oge;
 
123
        return "<$str>";
 
124
    } else
 
125
    {
 
126
        $str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/ogi;
 
127
        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
    my ($self, $fh, $pdf, %opts) = @_;
 
140
 
 
141
    $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
    my ($self, $inpdf, $res, $unique, $outpdf, %opts) = @_;
 
154
    my ($i);
 
155
 
 
156
    $res = $self->SUPER::copy($inpdf, $res, $unique, $outpdf, %opts);
 
157
    $res->{'val'} = $self->{'val'};
 
158
    $res->{' realised'} = 1;
 
159
    $res;
 
160
}