lib/PostScript/Resources.pm

filter: Statementstmt 74.8% 137 / 183 Branchbran 40.3% 46 / 114 Conditioncond 42.1% 8 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 94.1% 16 / 17 Podpod 75.0% 3 / 4 totaltotal 60.1% 210 / 349 CC 67 SCAR 37.4
CC
67
Cov
60%
new
112.4
x_loadFontFamily
56.0
_loadFile
14.9
CRAP 42.2
« lib/PostScript/Font.pm ↑ Summary blib/lib/PostScript/PseudoISO.pm »
1 # RCS Status : $Id: Resources.pm,v 1.12 2004/12/18 16:40:52 jv Exp $# Author : Johan Vromans
2 # Created On : Januari 1999
3 # Last Modified By: Johan Vromans
4 # Last Modified On: Thu Oct 23 14:11:52 2003
5 # Update Count : 187
6 # Status : Released
7  
8 ################ Module Preamble ################
9  
10 package PostScript::Resources;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶BEGIN { require 5.005; }
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use IO qw(File);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 $VERSION = "1.03";
22  
23 1 my $ResourcePath = "."; # default standard resource path
24 1 my $defupr = "PSref.upr"; # principal resource file
25 1 my $fn; # file currently being loaded
26 my $fh; # handle of file currently being loaded
27 1 my $section; # section currently being loaded
28 1 my $exclusive; # last loaded file was exclusive
29 1 my $rscidx; # current resource index
30  
31 1 my $trace;
32 1 my $debug;
33 1 my $verbose;
34 1 my $error;
35  
36 1 ▶sub new {
Subroutine new: called CC=24 SCAR=47.2
Pod documented
37 1 my $class = shift;
38 1 ▶ my (%atts) = (path => $ENV{"PSRESOURCEPATH"} || "::",
Condition50%1/2
$ENV{'PSRESOURCEPATH'} || '::'
l!l
01
MC/DC0%0/1
$ENV{'PSRESOURCEPATH'} || '::'
$ENV{'PSRESOURCEPATH'}
Truth table50%1/2
A
A= $ENV{'PSRESOURCEPATH'}
Aresult
11
00
39 stdpath => $ResourcePath,
40 error => 'die', # 'die', 'warn' or 'ignore'
41 verbose => 0,
42 trace => 0,
43 debug => 0,
44 @_);
45  
46 1 $debug = lc($atts{debug});
47 1 ▶ $trace = $debug || lc($atts{trace});
Condition33%1/3
$debug || lc $atts{'trace'}
l!l&&r!l&&!r
001
MC/DC0%0/2
$debug || lc $atts{'trace'}
$debug lc $atts{'trace'}
Truth table33%1/3
A || B
A= $debugB= lc $atts{'trace'}
ABresult
1X1
011
000
48 1 ▶ $verbose = $trace || lc($atts{verbose});
Condition33%1/3
$trace || lc $atts{'verbose'}
l!l&&r!l&&!r
001
MC/DC0%0/2
$trace || lc $atts{'verbose'}
$trace lc $atts{'verbose'}
Truth table33%1/3
A || B
A= $traceB= lc $atts{'verbose'}
ABresult
1X1
011
000
49 1 $error = lc($atts{error});
50  
51 1 my $self = {};
52 1 bless $self, $class;
53  
54 # Get the resource paths.
55 1 my $path = $atts{path};
56 1 $path =~ s|::|:$atts{stdpath}:|g;
57 1 $path =~ s|^:||;
58  
59 # According to the specs, the file names are either literal,
60 # absolute or relative. In the latter case, the current prefix
61 # (which defaults to the directory of the .upr file) must be
62 # appended.
63 # To avoid lots of unnecessary file name parsing, each prefix
64 # will be stored in a prefix array, and the index in this array
65 # will be prepended to each file name entry.
66 # The costly filename manipulation will only be done when a
67 # filename needs to be returned (sub _buildfilename).
68  
69 # Create the prefix array and reset the index.
70 1 $self->{prefix} = [];
71 1 $rscidx = 0;
72  
73 # Process the entries in the list.
74 1 foreach my $rsc ( split (":", $path) ) {
75  
76 1 ▶ print STDERR ("rsc#$rscidx: $rsc <file>\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
77  
78 1 ▶ if ( -d $rsc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $rsc) { } 1 0 1
79 1 ▶ print STDERR ("rsc#$rscidx: $rsc <dir>\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
80  
81 # Directory.
82 1 $exclusive = 0;
83  
84 # First check for a PSres.upr, and load it if possible.
85 1 $fn = File::Spec->catfile ($rsc, "PSres.upr");
86 1 ▶ if ( -f $fn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fn) 0 1 1
87 0 ▶ print STDERR ("rsc#$rscidx: load $fn\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
88 0 $rscidx++;
89 0 eval { _loadFile ($self) };
90 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
91 0 ▶ die ($@) if $error eq "die";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "die" 0 0 0
92 0 ▶ warn ($@) if $error eq "warn";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "warn" 0 0 0
93 0 next;
94 }
95 }
96  
97 # Unless PSres.upr was an exclusive resource, load all
98 # files with .upr extension.
99 1 ▶ unless ( $exclusive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exclusive) 1 0 1
100 1 my $dh = do { local *DH };
101 1 opendir ($dh, $rsc);
102 1 my @files = grep (/\.upr$/, readdir ($dh));
103 1 closedir ($dh);
104 1 foreach my $file ( @files ) {
105 # Skip the PSres.upr. It is already loaded.
106 2 ▶ next if $file eq "PSres.upr";
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "PSres.upr" 0 2 2
107  
108 2 $fn = File::Spec->catfile ($rsc, $file);
109 2 ▶ print STDERR ("rsc#$rscidx: load $fn\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
110 2 $rscidx++;
111 2 eval { _loadFile ($self) };
112 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
113 0 ▶ die ($@) if $error eq "die";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "die" 0 0 0
114 0 ▶ warn ($@) if $error eq "warn";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "warn" 0 0 0
115 0 next;
116 }
117 }
118 }
119 }
120 else {
121  
122 # File. This is _not_ defined in the specs.
123  
124 0 $fn = $rsc;
125 0 ▶ print STDERR ("rsc#$rscidx: load $fn\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
126 0 $rscidx++;
127 0 eval { _loadFile ($self) };
128 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
129 0 ▶ die ($@) if $error eq "die";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "die" 0 0 0
130 0 ▶ warn ($@) if $error eq "warn";
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "warn" 0 0 0
131 0 next;
132 }
133 }
134 }
135  
136 1 $self;
137 }
138  
139 1 ▶sub FontAFM ($$) {
Subroutine FontAFM: called CC=2 SCAR=8.1
Pod documented
140 1 my ($self, $font) = @_;
141 return _buildfilename ($self, $font)
142 1 ▶ if defined ($font = $self->{FontAFM}->{$font});
Branch: 1 branch
BranchTrueFalseTotal
if defined($font = $self->{"FontAFM"}{$font}) 1 0 1
143 0 undef;
144 }
145  
146 1 ▶sub FontOutline ($$) {
Subroutine FontOutline: called CC=2 SCAR=8.1
Pod documented
147 1 my ($self, $font) = @_;
148 return _buildfilename ($self, $font)
149 1 ▶ if defined ($font = $self->{FontOutline}->{$font});
Branch: 1 branch
BranchTrueFalseTotal
if defined($font = $self->{"FontOutline"}{$font}) 1 0 1
150 0 undef;
151 }
152  
153 2 ▶sub _buildfilename ($$) {
Subroutine _buildfilename: called CC=3 SCAR=11.2
154 2 my ($self, $name) = @_;
155 2 my $i;
156 2 ($i, $name) = unpack ("IA*", $name);
157 2 ▶ return $1 if $name =~ /^=(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^=(.*)$/ 0 2 2
158 2 ▶ return $name if File::Spec->file_name_is_absolute ($name);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($name) 0 2 2
159 2 File::Spec->canonpath (File::Spec->catfile ($self->{prefix}->[$i], $name));
160 }
161  
162 2 ▶sub _loadFile ($) {
Subroutine _loadFile: called CC=12 SCAR=27.0
163  
164 2 my ($self) = @_;
165  
166 2 my $data; # data
167  
168 2 eval { # so we can use die
169  
170 2 $fh = new IO::File; # font file
171 2 my $sz = -s $fn; # file size
172  
173 2 ▶ $fh->open ($fn) || die ("$fn: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($fn) or die "${fn}: $!\n" 2 0 2
174 2 ▶ print STDERR ($fn, ": Loading Resources\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
175  
176 # Read in the data.
177 2 my $line = <$fh>;
178 2 ▶ die ($fn."[$.]: Unrecognized file format\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^PS-Resources-(Exclusive-)?([\d.]+)/ 0 2 2
179 unless $line =~ /^PS-Resources-(Exclusive-)?([\d.]+)/;
180 2 $exclusive = defined $1;
181 2 my $version = $2;
182  
183 # The resources file is organised in sections, each starting
184 # with the section name and terminated by a line with just
185 # a period.
186 # Following the first PS-Resources line, the sections are
187 # enumerated, e.g.:
188 #
189 # PS-Resources-1.0
190 # FontAFM
191 # FontOutlines
192 # FontFamily
193 # FontPrebuilt
194 # FontBDF
195 # FontBDFSizes
196 # .
197 #
198 # Optionally, the name of the resource directory follows. It
199 # is preceded with a slash, e.g.:
200 #
201 # //usr/share/psresources
202 #
203 # This is then followed by each of the sections, e.g.:
204 #
205 # FontAFM
206 # ... afm info ...
207 # .
208 # FontOutlines
209 # ... outlines info ...
210 # .
211 # FontFamily
212 # ... family info ...
213 # .
214 #
215 # Backslash escapes are NOT handled, except for continuation.
216 #
217 # We have a _loadXXX subroutine for each section, where XXX is
218 # the section name. Flexible and extensible.
219 #
220 # The current approach is to ignore the first section.
221  
222 2 $self->_skipSection ($fh);
223  
224 # Then, load the sections from the file, skipping unknown ones.
225  
226 2 my $checkdir = 1;
227 2 ▶ while ( defined ($section = _readLine ($self, $fh)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($section = &_readLine($self, $fh))) 4 2 6
228 4 chomp ($section);
229 4 ▶ if ( $checkdir && $section =~ /^\/(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkdir and $section =~ m[^/(.*)]) 0 4 4
Condition66%2/3
$checkdir and $section =~ m[^/(.*)]
!ll&&!rl&&r
220
MC/DC0%0/2
$checkdir and $section =~ m[^/(.*)]
$checkdir $section =~ m[^/(.*)]
Truth table66%2/3
A and B
A= $checkdirB= $section =~ m[^/(.*)]
ABresult
0X0
100
111
230 0 $self->{prefix}->[$rscidx] = $1;
231 0 $checkdir = 0;
232 0 next;
233 }
234 4 $checkdir = 0;
235 4 ▶ my $loader = defined &{"_load$section"} ? "_load$section" :
Branch: 1 branch
BranchTrueFalseTotal
defined &{"_load$section";} ? : 4 0 4
236 "_skipSection";
237 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 4 ▶ die ($fn."[$.]: Premature end of $section section\n")
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof or not &$loader($self) 0 4 4
Condition33%1/3
$fh->eof or not &$loader($self)
l!l&&r!l&&!r
004
MC/DC0%0/2
$fh->eof or not &$loader($self)
$fh->eof not &$loader($self)
Truth table33%1/3
A or not(B)
A= $fh->eofB= not &$loader($self)
ABresult
1X1
011
000
239 if $fh->eof || !$loader->($self);
240 }
241  
242 };
243  
244 # Set the dfeault value for the directory prefix, if necessary.
245 2 ▶ $self->{prefix}->[$rscidx] ||= dirname ($fn);
Condition33%1/3
$self->{"prefix"}[$rscidx] ||= dirname($fn)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"prefix"}[$rscidx] ||= dirname($fn)
$self->{"prefix"}[$rscidx] dirname($fn)
Truth table33%1/3
A ||= B
A= $self->{"prefix"}[$rscidx]B= dirname($fn)
ABresult
1X1
011
000
246  
247 2 $fh->close;
248 2 ▶ die ($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
249 2 $self;
250 }
251  
252 22 ▶sub _readLine () {
Subroutine _readLine: called CC=5 SCAR=17.8
253 # Read a line, handling continuation lines.
254 22 my $line;
255 22 while ( 1 ) {
256 22 ▶ return undef if $fh->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 2 20 22
257 20 $line .= <$fh>;
258 20 ▶ if ( $line =~ /^(.*)\\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.*)\\$/) 0 20 20
259 0 $line = $1;
260 0 redo;
261 }
262 20 ▶ $line = $1 if $line =~ /^(.*)%/; # remove comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(.*)%/ 0 20 20
263 20 $line =~ s/\s+$//; # remove trailing blanks
264 20 ▶ next unless $line =~ /\S/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 20 20
265 20 return $line;
266 }
267 0 continue { $line = "" }
268 0 undef;
269 }
270  
271 2 ▶sub _loadFontAFM ($) {
Subroutine _loadFontAFM: called CC=8 SCAR=22.7
272 2 my ($self) = @_;
273 2 ▶ print STDERR ($fn, "[$.]: Loading section $section\n") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 2 2
274  
275 2 my $afm;
276 $afm = $self->{FontAFM} = {}
277 2 ▶ unless defined ($afm = $self->{FontAFM});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($afm = $self->{"FontAFM"}) 1 1 2
278  
279 2 my $line;
280 2 my $rscidx = pack ("I", $rscidx);
281 2 ▶ while ( defined ($line = _readLine ()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = &_readLine())) 6 0 6
282 6 ▶ return 1 if $line =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.$/ 2 4 6
283  
284 # PostScriptName=the/file.afm
285 4 ▶ if ( $line =~ /^([^=]+)=(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^=]+)=(.*)$/) 4 0 4
286 4 ▶ $afm->{$1} = $rscidx.$2 unless $afm->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $afm->{$1} 4 0 4
287 4 next;
288 }
289 0 ▶ warn ($fn, "[$.]: Invalid FontAFM entry\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $error eq "ignore" 0 0 0
290 unless $error eq "ignore";
291 }
292 0 return 1;
293 }
294  
295 0 ▶sub x_loadFontFamily ($) {
Subroutine x_loadFontFamily: not called CC=7 SCAR=40.3
Pod undocumented
296 0 my ($self) = @_;
297 0 ▶ print STDERR ($fn, "[$.]: Loading section $section\n") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 0 0
298  
299 0 my $fam;
300 $fam = $self->{FontFamily} = {}
301 0 ▶ unless defined ($fam = $self->{FontFamily});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($fam = $self->{"FontFamily"}) 0 0 0
302  
303 0 my $line;
304 0 ▶ while ( defined ($line = _readLine ()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = &_readLine())) 0 0 0
305 0 ▶ return 1 if $line =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.$/ 0 0 0
306  
307 # Familiyname=Type1,PostScriptName1,Type2,PostScriptName2,...
308 0 ▶ if ( $line =~ /^([^=]+)==?(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^=]+)==?(.*)$/) 0 0 0
309 0 $fam->{$1} = { split (',', $2) };
310 0 next;
311 }
312 0 ▶ warn ($fn, "[$.]: Invalid FontFamily entry\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $error eq "ignore" 0 0 0
313 unless $error eq "ignore";
314 }
315 0 return 1;
316 }
317  
318 2 ▶sub _loadFontOutline ($) {
Subroutine _loadFontOutline: called CC=8 SCAR=22.7
319 2 my ($self) = @_;
320 2 ▶ print STDERR ($fn, "[$.]: Loading section $section\n") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 2 2
321  
322 2 my $pfa;
323 $pfa = $self->{FontOutline} = {}
324 2 ▶ unless defined ($pfa = $self->{FontOutline});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($pfa = $self->{"FontOutline"}) 1 1 2
325  
326 2 my $line;
327 2 my $rscidx = pack ("I", $rscidx);
328 2 ▶ while ( defined ($line = _readLine ()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = &_readLine())) 4 0 4
329 4 ▶ return 1 if $line =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.$/ 2 2 4
330  
331 # PostScriptName=the/file.pfa
332 2 ▶ if ( $line =~ /^([^=]+)=(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^=]+)=(.*)$/) 2 0 2
333 2 ▶ $pfa->{$1} = $rscidx.$2 unless $pfa->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $pfa->{$1} 2 0 2
334 2 next;
335 }
336 0 ▶ warn ($fn, "[$.]: Invalid FontOutline entry\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $error eq "ignore" 0 0 0
337 unless $error eq "ignore";
338 }
339 0 return 1;
340 }
341  
342 2 ▶sub _skipSection ($) {
Subroutine _skipSection: called CC=5 SCAR=17.0
343 2 my ($self) = (@_);
344 2 ▶ $section ||= "list";
Condition50%1/2
$section ||= "list"
l!l
02
MC/DC0%0/1
$section ||= "list"
$section
Truth table50%1/2
A
A= $section
Aresult
11
00
345 2 ▶ print STDERR ($fn, "[$.]: Skipping section $section\n") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 2 2
346  
347 2 my $line;
348 2 ▶ while ( defined ($line = _readLine ()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = &_readLine())) 6 0 6
349 6 ▶ return 1 if $line =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.$/ 2 4 6
350 }
351 0 return 1;
352 }
353  
354 1 1;
355  
356 __END__
« lib/PostScript/Font.pm ↑ Summary blib/lib/PostScript/PseudoISO.pm »