Builder/Util.pm

filter: Statementstmt 49.1% 233 / 474 Branchbran 38.1% 77 / 202 Conditioncond 12.5% 6 / 48 MC/DCmcdc 7.4% 2 / 27 Subroutinesub 68.5% 37 / 54 Podpod 5.0% 2 / 40 totaltotal 42.2% 357 / 845 CC 124 SCAR 61.4
CC
124
Cov
44%
str2dim
1892.0
labHexValues
30.0
namecolor_cmyk
22.9
CRAP 466.0
« Builder/Content.pm ↑ Summary Builder.pm »
1 package PDF::Builder::Util;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 our $VERSION = '3.028'; # VERSION
7 39 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
8  
9 # note: $a and $b are "Magic variables" according to perlcritic, and so it
10 # has conniptions over using them as variable names (even with "my"). so, I
11 # changed most of the single letter names to double letters (r,g,b -> rr,gg,bb
12 # etc.)
13  
14 39 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 ▶ use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶ use Math::Trig; # CAUTION: deg2rad(0) = deg2rad(360) = 0!
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶ use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 39 ▶ use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 39 ▶ use PDF::Builder::Basic::PDF::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 39 ▶ use PDF::Builder::Resource::Colors;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 39 ▶ use PDF::Builder::Resource::Glyphs;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 39 ▶ use PDF::Builder::Resource::PaperSizes;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 39 ▶ use POSIX qw( HUGE_VAL floor );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 39 ▶ use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 @ISA
27 @EXPORT
28 @EXPORT_OK
29 %colors
30 $key_var
31 %u2n
32 %n2u
33 $pua
34 %PaperSizes
35 39 );
36  
37 39 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 39 @ISA = qw(Exporter);
39 39 @EXPORT = qw(
40 pdfkey
41 float floats floats5 intg intgs
42 mMin mMax
43 HSVtoRGB RGBtoHSV HSLtoRGB RGBtoHSL RGBtoLUM
44 namecolor namecolor_cmyk namecolor_lab optInvColor defineColor
45 dofilter unfilter
46 nameByUni uniByName initNameTable defineName
47 page_size
48 getPaperSizes
49 str2dim
50 );
51 39 @EXPORT_OK = qw(
52 pdfkey
53 digest digestx digest16 digest32
54 float floats floats5 intg intgs
55 mMin mMax
56 cRGB cRGB8 RGBasCMYK
57 HSVtoRGB RGBtoHSV HSLtoRGB RGBtoHSL RGBtoLUM
58 namecolor namecolor_cmyk namecolor_lab optInvColor defineColor
59 dofilter unfilter
60 nameByUni uniByName initNameTable defineName
61 page_size getPaperSizes
62 str2dim
63 );
64  
65 =head1 NAME
66  
67 PDF::Builder::Util - Utility package for often-used methods across the package
68  
69 =cut
70  
71 39 %colors = PDF::Builder::Resource::Colors->get_colors();
72 39 %PaperSizes = PDF::Builder::Resource::PaperSizes->get_paper_sizes();
73  
74 39 $key_var = 'CBA';
75  
76 39 $pua = 0xE000;
77  
78 39 %u2n = %{$PDF::Builder::Resource::Glyphs::u2n};
79 39 %n2u = %{$PDF::Builder::Resource::Glyphs::n2u};
80 }
81  
82 69 ▶sub pdfkey {
Subroutine pdfkey: called CC=1 SCAR=0.0
Pod undocumented
83 69 return $PDF::Builder::Util::key_var++;
84 }
85  
86 0 ▶sub digestx {
Subroutine digestx: not called CC=2 SCAR=17.9
Pod undocumented
87 0 my $len = shift;
88  
89 0 my $mask = $len - 1;
90 0 my $ddata = join('', @_);
91 0 my $mdkey = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789gT';
92 0 my $xdata = '0' x $len;
93 0 my $off = 0;
94 0 foreach my $set (0 .. (length($ddata) << 1)) {
95 0 $off += vec($ddata, $set, 4);
96 0 $off += vec($xdata, ($set & $mask), 8);
97 0 vec($xdata, ($set & ($mask << 1 | 1)), 4) = vec($mdkey, ($off & 0x7f), 4);
98 }
99  
100 # foreach $set (0 .. $mask) {
101 # vec($xdata, $set, 8) = (vec($xdata, $set, 8) & 0x7f) | 0x40;
102 # }
103  
104 # $off = 0;
105 # foreach $set (0 .. $mask) {
106 # $off += vec($xdata, $set, 8);
107 # vec($xdata, $set, 8) = vec($mdkey, ($off & 0x3f), 8);
108 # }
109  
110 0 return $xdata;
111 }
112  
113 0 ▶sub digest {
Subroutine digest: not called CC=1 SCAR=6.9
Pod undocumented
114 0 return digestx(32, @_);
115 }
116  
117 0 ▶sub digest16 {
Subroutine digest16: not called CC=1 SCAR=6.9
Pod undocumented
118 0 return digestx(16, @_);
119 }
120  
121 0 ▶sub digest32 {
Subroutine digest32: not called CC=1 SCAR=6.9
Pod undocumented
122 0 return digestx(32, @_);
123 }
124  
125 2930 ▶sub xlog10 {
Subroutine xlog10: called CC=2 SCAR=6.9
Pod undocumented
126 2930 my $n = shift;
127  
128 2930 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 1977 953 2930
129 1977 return log(abs($n)) / log(10);
130 } else {
131 953 return 0;
132 }
133 }
134  
135 2930 ▶sub float {
Subroutine float: called CC=5 SCAR=16.7
Pod undocumented
136 2930 my $f = shift;
137 2930 ▶ my $mxd = shift() || 4;
Condition100%2/2
shift() || 4
l!l
2012729
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
138  
139 2930 ▶ $f = 0 if abs($f) < 0.0000000000000001;
Branch: 1 branch
BranchTrueFalseTotal
if abs $f < "1e-16" 953 1977 2930
140 2930 my $ad = floor(xlog10($f) - $mxd);
141 2930 ▶ if (abs($f - int($f)) < (10 ** (-$mxd))) {
Branch: 2 branches
BranchTrueFalseTotal
if (abs $f - int($f) < 10 ** (-$mxd)) { } 2075 855 2930
elsif ($ad > 0) { } 0 855 855
142 # just in case we have an integer
143 2075 return sprintf('%i', $f);
144 } elsif ($ad > 0) {
145 0 my $value = sprintf('%f', $f);
146 # Remove trailing zeros
147 0 $value =~ s/(\.\d*?)0+$/$1/;
148 0 $value =~ s/\.$//;
149 0 return $value;
150 } else {
151 855 my $value = sprintf('%.*f', abs($ad), $f);
152 # Remove trailing zeros
153 855 $value =~ s/(\.\d*?)0+$/$1/;
154 855 $value =~ s/\.$//;
155 855 return $value;
156 }
157 }
158  
159 408 ▶sub floats { return map { float($_) } @_; }
Subroutine floats: called CC=1 SCAR=0.0
Pod undocumented
160 53 ▶sub floats5 { return map { float($_, 5) } @_; }
Subroutine floats5: called CC=1 SCAR=0.0
Pod undocumented
161  
162 1 ▶sub intg {
Subroutine intg: called CC=1 SCAR=0.0
Pod undocumented
163 1 my $f = shift;
164  
165 1 return sprintf('%i', $f);
166 }
167  
168 0 ▶sub intgs { return map { intg($_) } @_; }
Subroutine intgs: not called CC=1 SCAR=6.9
Pod undocumented
169  
170 2 ▶sub mMin {
Subroutine mMin: called CC=2 SCAR=6.9
Pod undocumented
171 2 my $n = HUGE_VAL();
172 2 ▶ map { $n = ($n > $_) ? $_ : $n } @_;
Branch: 1 branch
BranchTrueFalseTotal
$n > $_ ? : 2 4 6
173 2 return $n;
174 }
175  
176 2 ▶sub mMax {
Subroutine mMax: called CC=2 SCAR=6.9
Pod undocumented
177 2 my $n = -HUGE_VAL();
178 2 ▶ map { $n = ($n < $_) ? $_ : $n } @_;
Branch: 1 branch
BranchTrueFalseTotal
$n < $_ ? : 5 1 6
179 2 return $n;
180 }
181  
182 =head2 PREDEFINED COLORS
183  
184 See the source of L<PDF::Builder::Resource::Colors> for a complete list.
185  
186 B<Please Note:> This is an amalgamation of the X11, SGML and (X)HTML
187 specification sets.
188  
189 There are many color model conversion and input conversion routines
190 defined here.
191  
192 =cut
193  
194 0 ▶sub cRGB {
Subroutine cRGB: not called CC=1 SCAR=6.9
Pod undocumented
195 0 my @cmy = (map { 1 - $_ } @_);
196 0 my $k = mMin(@cmy);
197 0 return (map { $_ - $k } @cmy), $k;
198 }
199  
200 0 ▶sub cRGB8 {
Subroutine cRGB8: not called CC=1 SCAR=6.9
Pod undocumented
201 0 return cRGB(map { $_ / 255 } @_);
202 }
203  
204 0 ▶sub RGBtoLUM {
Subroutine RGBtoLUM: not called CC=1 SCAR=6.9
Pod undocumented
205 0 my ($rr, $gg, $bb) = @_;
206 0 return $rr * 0.299 + $gg * 0.587 + $bb * 0.114;
207 }
208  
209 0 ▶sub RGBasCMYK {
Subroutine RGBasCMYK: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my @rgb = @_;
211 0 my @cmy = map { 1 - $_ } @rgb;
212 0 my $k = mMin(@cmy) * 0.44;
213 0 return (map { $_ - $k } @cmy), $k;
214 }
215  
216 26 ▶sub HSVtoRGB {
Subroutine HSVtoRGB: called CC=7 SCAR=19.5
Pod undocumented
217 26 my ($h,$s,$v) = @_;
218 26 my ($rr,$gg,$bb, $i, $f, $p, $q, $t);
219  
220 26 ▶ if ($s == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0) 0 26 26
221 # achromatic (grey)
222 0 return ($v,$v,$v);
223 }
224  
225 26 $h %= 360;
226 26 $h /= 60; # sector 0 to 5
227 26 $i = POSIX::floor($h);
228 26 $f = $h - $i; # factorial part of h
229 26 $p = $v * (1 - $s);
230 26 $q = $v * (1 - $s * $f);
231 26 $t = $v * (1 - $s * ( 1 - $f ));
232  
233 26 ▶ if ($i < 1) {
Branch: 5 branches
BranchTrueFalseTotal
if ($i < 1) { } 5 21 26
elsif ($i < 2) { } 4 17 21
elsif ($i < 3) { } 3 14 17
elsif ($i < 4) { } 5 9 14
elsif ($i < 5) { } 3 6 9
234 5 $rr = $v;
235 5 $gg = $t;
236 5 $bb = $p;
237 } elsif ($i < 2) {
238 4 $rr = $q;
239 4 $gg = $v;
240 4 $bb = $p;
241 } elsif ($i < 3) {
242 3 $rr = $p;
243 3 $gg = $v;
244 3 $bb = $t;
245 } elsif ($i < 4) {
246 5 $rr = $p;
247 5 $gg = $q;
248 5 $bb = $v;
249 } elsif ($i < 5) {
250 3 $rr = $t;
251 3 $gg = $p;
252 3 $bb = $v;
253 } else {
254 6 $rr = $v;
255 6 $gg = $p;
256 6 $bb = $q;
257 }
258  
259 26 return ($rr, $gg, $bb);
260 }
261  
262 6 ▶sub RGBquant {
Subroutine RGBquant: called CC=5 SCAR=16.2
Pod undocumented
263 6 my ($q1, $q2, $h) = @_;
264 6 ▶ while ($h < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < 0) 0 6 6
265 0 $h += 360;
266 }
267 6 $h %= 360;
268 6 ▶ if ($h < 60) {
Branch: 3 branches
BranchTrueFalseTotal
if ($h < 60) { } 1 5 6
elsif ($h < 180) { } 2 3 5
elsif ($h < 240) { } 1 2 3
269 1 return $q1 + (($q2 - $q1) * $h / 60);
270 } elsif ($h < 180) {
271 2 return $q2;
272 } elsif ($h < 240) {
273 1 return $q1 + (($q2 - $q1) * (240 - $h) / 60);
274 } else {
275 2 return $q1;
276 }
277 }
278  
279 2 ▶sub RGBtoHSV {
Subroutine RGBtoHSV: called CC=5 SCAR=17.3
Pod undocumented
280 2 my ($rr,$gg,$bb) = @_;
281  
282 2 my ($h,$s,$v, $min, $max, $delta);
283  
284 2 $min = mMin($rr, $gg, $bb);
285 2 $max = mMax($rr, $gg, $bb);
286  
287 2 $v = $max;
288 2 $delta = $max - $min;
289  
290 2 ▶ if ($delta > 0.000000001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 1e-09) { } 2 0 2
291 2 $s = $delta / $max;
292 } else {
293 0 $s = 0;
294 0 $h = 0;
295 0 return ($h,$s,$v);
296 }
297  
298 2 ▶ if ( $rr == $max ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr == $max) { } 0 2 2
elsif ($gg == $max) { } 1 1 2
299 0 $h = ($gg - $bb) / $delta;
300 } elsif ( $gg == $max ) {
301 1 $h = 2 + ($bb - $rr) / $delta;
302 } else {
303 1 $h = 4 + ($rr - $gg) / $delta;
304 }
305 2 $h *= 60;
306 2 ▶ if ($h < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h < 0) 0 2 2
307 0 $h += 360;
308 }
309 2 return ($h,$s,$v);
310 }
311  
312 0 ▶sub RGBtoHSL {
Subroutine RGBtoHSL: not called CC=3 SCAR=24.8
Pod undocumented
313 0 my ($rr,$gg,$bb) = @_;
314  
315 0 my ($h,$s,$v, $l, $min, $max, $delta);
316  
317 0 $min = mMin($rr, $gg, $bb);
318 0 $max = mMax($rr, $gg, $bb);
319 0 ($h, $s, $v) = RGBtoHSV($rr, $gg, $bb);
320 0 $l = ($max + $min) / 2.0;
321 0 $delta = $max - $min;
322 0 ▶ if ($delta < 0.00000000001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 1e-11) { } 0 0 0
323 0 return (0, 0, $l);
324 } else {
325 0 ▶ if ($l <= 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= 0.5) { } 0 0 0
326 0 $s = $delta / ($max + $min);
327 } else {
328 0 $s = $delta / (2 - $max - $min);
329 }
330 }
331 0 return ($h, $s, $l);
332 }
333  
334 2 ▶sub HSLtoRGB {
Subroutine HSLtoRGB: called CC=3 SCAR=11.5
Pod undocumented
335 2 my($h,$s,$l, $rr,$gg,$bb, $p1, $p2) = @_;
336  
337 2 ▶ if ($l <= 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= 0.5) { } 0 2 2
338 0 $p2 = $l * (1 + $s);
339 } else {
340 2 $p2 = $l + $s - ($l * $s);
341 }
342 2 $p1 = 2 * $l - $p2;
343 2 ▶ if ($s < 0.0000000000001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < 1e-13) { } 0 2 2
344 0 $rr = $gg = $bb = $l;
345 } else {
346 2 $rr = RGBquant($p1, $p2, $h + 120);
347 2 $gg = RGBquant($p1, $p2, $h);
348 2 $bb = RGBquant($p1, $p2, $h - 120);
349 }
350 2 return ($rr,$gg,$bb);
351 }
352  
353 0 ▶sub optInvColor {
Subroutine optInvColor: not called CC=2 SCAR=17.9
Pod undocumented
354 0 my ($rr,$gg,$bb) = @_;
355  
356 0 my $ab = (0.2 * $rr) + (0.7 * $gg) + (0.1 * $bb);
357  
358 0 ▶ if ($ab > 0.45) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ab > 0.45) { } 0 0 0
359 0 return(0,0,0);
360 } else {
361 0 return(1,1,1);
362 }
363 }
364  
365 0 ▶sub defineColor {
Subroutine defineColor: not called CC=2 SCAR=17.9
Pod undocumented
366 0 my ($name, $mx, $rr,$gg,$bb) = @_;
367 0 ▶ $colors{$name} ||= [ map {$_ / $mx} ($rr,$gg,$bb) ];
Condition0%0/2
$PDF::Builder::Util::colors{$name} ||= [map({$_ / $mx;} $rr, $gg, $bb)]
l!l
00
MC/DC0%0/1
$PDF::Builder::Util::colors{$name} ||= [map({$_ / $mx;} $rr, $gg, $bb)]
$PDF::Builder::Util::colors{$name}
Truth table0%0/2
A
A= $PDF::Builder::Util::colors{$name}
Aresult
11
00
368 0 return $colors{$name};
369 }
370  
371 # convert 3n (n=1..4) hex digits to RGB 0-1 values
372 # returns a triplet of values 0.0..1.0
373 23 ▶sub rgbHexValues {
Subroutine rgbHexValues: called CC=5 SCAR=22.8
Pod undocumented
374 23 my $name = lc(shift()); # # plus 3n hex digits
375 # if <3 digits, pad with '0' (silent error)
376 # if not 3n digits, ignore extras (silent error)
377 # if >12 digits, ignore extras (silent error)
378 23 my ($rr,$gg,$bb);
379 0 ▶ while (length($name) < 4) { $name .= '0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $name < 4) 0 23 23
380 23 ▶ if (length($name) < 5) { # zb. #fa4, #cf0
Branch: 3 branches
BranchTrueFalseTotal
if (length $name < 5) { } 0 23 23
elsif (length $name < 8) { } 23 0 23
elsif (length $name < 11) { } 0 0 0
381 0 $rr = hex(substr($name, 1, 1)) / 0xf;
382 0 $gg = hex(substr($name, 2, 1)) / 0xf;
383 0 $bb = hex(substr($name, 3, 1)) / 0xf;
384 } elsif (length($name) < 8) { # zb. #ffaa44, #ccff00
385 23 $rr = hex(substr($name, 1, 2)) / 0xff;
386 23 $gg = hex(substr($name, 3, 2)) / 0xff;
387 23 $bb = hex(substr($name, 5, 2)) / 0xff;
388 } elsif (length($name) < 11) { # zb. #fffaaa444, #cccfff000
389 0 $rr = hex(substr($name, 1, 3)) / 0xfff;
390 0 $gg = hex(substr($name, 4, 3)) / 0xfff;
391 0 $bb = hex(substr($name, 7, 3)) / 0xfff;
392 } else { # zb. #ffffaaaa4444, #ccccffff0000
393 0 $rr = hex(substr($name, 1, 4)) / 0xffff;
394 0 $gg = hex(substr($name, 5, 4)) / 0xffff;
395 0 $bb = hex(substr($name, 9, 4)) / 0xffff;
396 }
397  
398 23 return ($rr,$gg,$bb);
399 }
400  
401 # convert 4n (n=1..4) hex digits to CMYK 0-1 values
402 # returns a quadruple of values 0.0..1.0
403 2 ▶sub cmykHexValues {
Subroutine cmykHexValues: called CC=5 SCAR=23.4
Pod undocumented
404 2 my $name = lc(shift()); # % plus 4n hex digits
405  
406 # if <4 digits, pad with '0' (silent error)
407 # if not 4n digits, ignore extras (silent error)
408 # if >16 digits, ignore extras (silent error)
409 2 my ($c,$m,$y,$k);
410 0 ▶ while (length($name) < 5) { $name .= '0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $name < 5) 0 2 2
411 2 ▶ if (length($name) < 6) { # zb. %cmyk
Branch: 3 branches
BranchTrueFalseTotal
if (length $name < 6) { } 0 2 2
elsif (length $name < 10) { } 2 0 2
elsif (length $name < 14) { } 0 0 0
412 0 $c = hex(substr($name, 1, 1)) / 0xf;
413 0 $m = hex(substr($name, 2, 1)) / 0xf;
414 0 $y = hex(substr($name, 3, 1)) / 0xf;
415 0 $k = hex(substr($name, 4, 1)) / 0xf;
416 } elsif (length($name) < 10) { # zb. %ccmmyykk
417 2 $c = hex(substr($name, 1, 2)) / 0xff;
418 2 $m = hex(substr($name, 3, 2)) / 0xff;
419 2 $y = hex(substr($name, 5, 2)) / 0xff;
420 2 $k = hex(substr($name, 7, 2)) / 0xff;
421 } elsif (length($name) < 14) { # zb. %cccmmmyyykkk
422 0 $c = hex(substr($name, 1, 3)) / 0xfff;
423 0 $m = hex(substr($name, 4, 3)) / 0xfff;
424 0 $y = hex(substr($name, 7, 3)) / 0xfff;
425 0 $k = hex(substr($name, 10, 3)) /0xfff;
426 } else { # zb. %ccccmmmmyyyykkkk
427 0 $c = hex(substr($name, 1, 4)) / 0xffff;
428 0 $m = hex(substr($name, 5, 4)) / 0xffff;
429 0 $y = hex(substr($name, 9, 4)) / 0xffff;
430 0 $k = hex(substr($name, 13, 4)) / 0xffff;
431 }
432  
433 2 return ($c,$m,$y,$k);
434 }
435  
436 # convert 3n (n=1..4) hex digits to HSV 0-360, 0-1 values
437 # returns a triplet of values 0.0..360.0, 2x0.0..1.0
438 28 ▶sub hsvHexValues {
Subroutine hsvHexValues: called CC=5 SCAR=16.7
Pod undocumented
439 28 my $name = lc(shift()); # ! plus 3n hex digits
440  
441 # if <3 digits, pad with '0' (silent error)
442 # if not 3n digits, ignore extras (silent error)
443 # if >12 digits, ignore extras (silent error)
444 28 my ($h,$s,$v);
445 0 ▶ while (length($name) < 4) { $name .= '0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $name < 4) 0 28 28
446 28 ▶ if (length($name) < 5) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $name < 5) { } 1 27 28
elsif (length $name < 8) { } 25 2 27
elsif (length $name < 11) { } 0 2 2
447 1 $h = 360 * hex(substr($name, 1, 1)) / 0x10;
448 1 $s = hex(substr($name, 2, 1)) / 0xf;
449 1 $v = hex(substr($name, 3, 1)) / 0xf;
450 } elsif (length($name) < 8) {
451 25 $h = 360 * hex(substr($name, 1, 2)) / 0x100;
452 25 $s = hex(substr($name, 3, 2)) / 0xff;
453 25 $v = hex(substr($name, 5, 2)) / 0xff;
454 } elsif (length($name) < 11) {
455 0 $h = 360 * hex(substr($name, 1, 3)) / 0x1000;
456 0 $s = hex(substr($name, 4, 3)) / 0xfff;
457 0 $v = hex(substr($name, 7, 3)) / 0xfff;
458 } else {
459 2 $h = 360 * hex(substr($name, 1, 4)) / 0x10000;
460 2 $s = hex(substr($name, 5, 4)) / 0xffff;
461 2 $v = hex(substr($name, 9, 4)) / 0xffff;
462 }
463  
464 28 return ($h,$s,$v);
465 }
466  
467 # convert 3n (n=1..4) hex digits to LAB 0-100, -100-100 values
468 # returns a triplet of values 0.0..100.0, 2x-100.0..100.0
469 0 ▶sub labHexValues {
Subroutine labHexValues: not called CC=5 SCAR=34.0
Pod undocumented
470 0 my $name = lc(shift()); # & plus 3n hex digits
471  
472 # if <3 digits, pad with '0' (silent error)
473 # if not 3n digits, ignore extras (silent error)
474 # if >12 digits, ignore extras (silent error)
475 0 my ($ll,$aa,$bb);
476 0 ▶ while (length($name) < 4) { $name .= '0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $name < 4) 0 0 0
477 0 ▶ if (length($name) < 5) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $name < 5) { } 0 0 0
elsif (length $name < 8) { } 0 0 0
elsif (length $name < 11) { } 0 0 0
478 0 $ll = 100*hex(substr($name, 1, 1)) / 0xf;
479 0 $aa = (200*hex(substr($name, 2, 1)) / 0xf) - 100;
480 0 $bb = (200*hex(substr($name, 3, 1)) / 0xf) - 100;
481 } elsif (length($name) < 8) {
482 0 $ll = 100*hex(substr($name, 1, 2)) / 0xff;
483 0 $aa = (200*hex(substr($name, 3, 2)) / 0xff) - 100;
484 0 $bb = (200*hex(substr($name, 5, 2)) / 0xff) - 100;
485 } elsif (length($name) < 11) {
486 0 $ll = 100*hex(substr($name, 1, 3)) / 0xfff;
487 0 $aa = (200*hex(substr($name, 4, 3)) / 0xfff) - 100;
488 0 $bb = (200*hex(substr($name, 7, 3)) / 0xfff) - 100;
489 } else {
490 0 $ll = 100*hex(substr($name, 1, 4)) / 0xffff;
491 0 $aa = (200*hex(substr($name, 5, 4)) / 0xffff) - 100;
492 0 $bb = (200*hex(substr($name, 9, 4)) / 0xffff) - 100;
493 }
494  
495 0 return ($ll,$aa,$bb);
496 }
497  
498 70 ▶sub namecolor {
Subroutine namecolor: called CC=8 SCAR=25.5
Pod undocumented
499 70 my $name = shift;
500  
501 70 ▶ unless (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $name) 70 0 70
502 70 $name = lc($name);
503 70 $name =~ s/[^\#!%\&\$a-z0-9]//g;
504 }
505  
506 70 ▶ if ($name =~ /^[a-z]/) { # name spec.
Branch: 5 branches
BranchTrueFalseTotal
if ($name =~ /^[a-z]/) { } 21 49 70
elsif ($name =~ /^#/) { } 23 26 49
elsif ($name =~ /^%/) { } 0 26 26
elsif ($name =~ /^!/) { } 26 0 26
elsif ($name =~ /^&/) { } 0 0 0
507 21 return namecolor($colors{$name});
508 } elsif ($name =~ /^#/) { # rgb spec.
509 23 return floats5(rgbHexValues($name));
510 } elsif ($name =~ /^%/) { # cmyk spec.
511 0 return floats5(cmykHexValues($name));
512 } elsif ($name =~ /^!/) { # hsv spec.
513 26 return floats5(HSVtoRGB(hsvHexValues($name)));
514 } elsif ($name =~ /^&/) { # hsl spec.
515 0 return floats5(HSLtoRGB(hsvHexValues($name)));
516 } else { # or it is a ref ?
517 0 ▶ return floats5(@{$name || [0.5,0.5,0.5]});
Condition0%0/2
$name || [0.5, 0.5, 0.5]
l!l
00
MC/DC0%0/1
$name || [0.5, 0.5, 0.5]
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
518 }
519 }
520  
521 2 ▶sub namecolor_cmyk {
Subroutine namecolor_cmyk: called CC=8 SCAR=31.3
Pod undocumented
522 2 my $name = shift;
523
524 2 ▶ unless (ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $name) 2 0 2
525 2 $name = lc($name);
526 2 $name =~ s/[^\#!%\&\$a-z0-9]//g;
527 }
528  
529 2 ▶ if ($name =~ /^[a-z]/) { # name spec.
Branch: 5 branches
BranchTrueFalseTotal
if ($name =~ /^[a-z]/) { } 0 2 2
elsif ($name =~ /^#/) { } 0 2 2
elsif ($name =~ /^%/) { } 2 0 2
elsif ($name =~ /^!/) { } 0 0 0
elsif ($name =~ /^&/) { } 0 0 0
530 0 return namecolor_cmyk($colors{$name});
531 } elsif ($name =~ /^#/) { # rgb spec.
532 0 return floats5(RGBasCMYK(rgbHexValues($name)));
533 } elsif ($name =~ /^%/) { # cmyk spec.
534 2 return floats5(cmykHexValues($name));
535 } elsif ($name =~ /^!/) { # hsv spec.
536 0 return floats5(RGBasCMYK(HSVtoRGB(hsvHexValues($name))));
537 } elsif ($name =~ /^&/) { # hsl spec.
538 0 return floats5(RGBasCMYK(HSLtoRGB(hsvHexValues($name))));
539 } else { # or it is a ref ?
540 0 ▶ return floats5(RGBasCMYK(@{$name || [0.5,0.5,0.5]}));
Condition0%0/2
$name || [0.5, 0.5, 0.5]
l!l
00
MC/DC0%0/1
$name || [0.5, 0.5, 0.5]
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
541 }
542 }
543  
544 # note that an angle of 360 degrees is treated as 0 radians by deg2rad.
545 2 ▶sub namecolor_lab {
Subroutine namecolor_lab: called CC=8 SCAR=31.0
Pod undocumented
546 2 my $name = shift;
547  
548 2 ▶ unless (ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $name) 2 0 2
549 2 $name = lc($name);
550 2 $name =~ s/[^\#!%\&\$a-z0-9]//g;
551 }
552  
553 2 ▶ if ($name =~ /^[a-z]/) { # name spec.
Branch: 5 branches
BranchTrueFalseTotal
if ($name =~ /^[a-z]/) { } 0 2 2
elsif ($name =~ /^\$/) { } 0 2 2
elsif ($name =~ /^#/) { } 0 2 2
elsif ($name =~ /^!/) { } 0 2 2
elsif ($name =~ /^&/) { } 2 0 2
554 0 return namecolor_lab($colors{$name});
555 } elsif ($name =~ /^\$/) { # lab spec.
556 0 return floats5(labHexValues($name));
557 } elsif ($name =~ /^#/) { # rgb spec.
558 0 my ($h,$s,$v) = RGBtoHSV(rgbHexValues($name));
559 0 my $aa = cos(deg2rad($h)) * $s * 100;
560 0 my $bb = sin(deg2rad($h)) * $s * 100;
561 0 my $ll = 100 * $v;
562 0 return floats5($ll,$aa,$bb);
563 } elsif ($name =~ /^!/) { # hsv spec.
564 # fake conversion
565 0 my ($h,$s,$v) = hsvHexValues($name);
566 0 my $aa = cos(deg2rad($h)) * $s * 100;
567 0 my $bb = sin(deg2rad($h)) * $s * 100;
568 0 my $ll = 100 * $v;
569 0 return floats5($ll,$aa,$bb);
570 } elsif ($name =~ /^&/) { # hsl spec.
571 2 my ($h,$s,$v) = hsvHexValues($name);
572 2 my $aa = cos(deg2rad($h)) * $s * 100;
573 2 my $bb = sin(deg2rad($h)) * $s * 100;
574 2 ($h,$s,$v) = RGBtoHSV(HSLtoRGB($h,$s,$v));
575 2 my $ll = 100 * $v;
576 2 return floats5($ll,$aa,$bb);
577 } else { # or it is a ref ?
578 0 ▶ my ($h,$s,$v) = RGBtoHSV(@{$name || [0.5,0.5,0.5]});
Condition0%0/2
$name || [0.5, 0.5, 0.5]
l!l
00
MC/DC0%0/1
$name || [0.5, 0.5, 0.5]
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
579 0 my $aa = cos(deg2rad($h)) * $s * 100;
580 0 my $bb = sin(deg2rad($h)) * $s * 100;
581 0 my $ll = 100 * $v;
582 0 return floats5($ll,$aa,$bb);
583 }
584 }
585  
586 =head2 STREAM FILTERS
587  
588 There are a number of functions here to handle stream filtering.
589  
590 =cut
591  
592 7 ▶sub unfilter {
Subroutine unfilter: called CC=4 SCAR=14.1
Pod undocumented
593 7 my ($filter, $stream) = @_;
594  
595 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 7 0 7
596 # we need to fix filter because it MAY be
597 # an array BUT IT COULD BE only a name
598 7 ▶ if (ref($filter) !~ /Array$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($filter) =~ /Array$/) 0 7 7
599 0 $filter = PDFArray($filter);
600 }
601 7 my @filts;
602 7 my ($hasflate) = -1;
603 7 my ($temp, $i, $temp1);
604  
605 7 @filts = map { ("PDF::Builder::Basic::PDF::Filter::" . $_->val())->new() } $filter->elements();
606  
607 7 foreach my $f (@filts) {
608 7 $stream = $f->infilt($stream, 1);
609 }
610 }
611  
612 7 return $stream;
613 }
614  
615 4 ▶sub dofilter {
Subroutine dofilter: called CC=4 SCAR=14.1
Pod undocumented
616 4 my ($filter, $stream) = @_;
617  
618 4 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 4 0 4
619 # we need to fix filter because it MAY be
620 # an array BUT IT COULD BE only a name
621 4 ▶ if (ref($filter) !~ /Array$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($filter) =~ /Array$/) 0 4 4
622 0 $filter = PDFArray($filter);
623 }
624 4 my @filts;
625 4 my $hasflate = -1;
626 4 my ($temp, $i, $temp1);
627  
628 4 @filts = map { ("PDF::Builder::Basic::PDF::Filter::" . $_->val())->new() } $filter->elements();
629  
630 4 foreach my $f (@filts) {
631 4 $stream = $f->outfilt($stream, 1);
632 }
633 }
634  
635 4 return $stream;
636 }
637  
638 =head2 PREDEFINED GLYPH-NAMES
639  
640 See the file C<uniglyph.txt> for a complete list.
641  
642 B<Please Note:> You may notice that apart from the 'AGL/WGL4', names
643 from the XML, (X)HTML and SGML specification sets have been included
644 to enable interoperability towards PDF.
645  
646 There are a number of functions here to handle various
647 aspects of glyph identification.
648  
649 =cut
650  
651 8704 ▶sub nameByUni {
Subroutine nameByUni: called CC=2 SCAR=7.1
Pod undocumented
652 8704 my $e = shift;
653  
654 8704 ▶ return $u2n{$e} || sprintf('uni%04X', $e);
Condition66%2/3
$PDF::Builder::Util::u2n{$e} || sprintf('uni%04X', $e)
l!l&&r!l&&!r
870040
MC/DC0%0/2
$PDF::Builder::Util::u2n{$e} || sprintf('uni%04X', $e)
$PDF::Builder::Util::u2n{$e} sprintf('uni%04X', $e)
Truth table66%2/3
A || B
A= $PDF::Builder::Util::u2n{$e}B= sprintf('uni%04X', $e)
ABresult
1X1
011
000
655 }
656  
657 9525 ▶sub uniByName {
Subroutine uniByName: called CC=3 SCAR=11.4
Pod undocumented
658 9525 my $e = shift;
659 9525 ▶ if ($e =~ /^uni([0-9A-F]{4})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^uni([0-9A-F]{4})$/) 0 9525 9525
660 0 return hex($1);
661 }
662 9525 ▶ return $n2u{$e} || undef;
Condition100%2/2
$PDF::Builder::Util::n2u{$e} || undef
l!l
82411284
MC/DC100%1/1
$PDF::Builder::Util::n2u{$e} || undef
$PDF::Builder::Util::n2u{$e}
Truth table100%2/2
A
A= $PDF::Builder::Util::n2u{$e}
Aresult
11
00
663 }
664  
665 0 ▶sub initNameTable {
Subroutine initNameTable: not called CC=1 SCAR=6.9
Pod undocumented
666 0 %u2n = %{$PDF::Builder::Resource::Glyphs::u2n};
667 0 %n2u = %{$PDF::Builder::Resource::Glyphs::n2u};
668 0 $pua = 0xE000;
669 0 return;
670 }
671  
672 0 ▶sub defineName {
Subroutine defineName: not called CC=3 SCAR=24.8
Pod undocumented
673 0 my $name = shift;
674  
675 0 ▶ return $n2u{$name} if defined $n2u{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDF::Builder::Util::n2u{$name} 0 0 0
676  
677 0 ▶ $pua++ while defined $u2n{$pua};
Branch: 1 branch
BranchTrueFalseTotal
defined $PDF::Builder::Util::u2n{$PDF::Builder::Util::pua} and do { ++$PDF::Builder::Util::pua } 0 0 0
678  
679 0 $u2n{$pua} = $name;
680 0 $n2u{$name} = $pua;
681  
682 0 return $pua;
683 }
684  
685 =head2 PREDEFINED PAPER SIZES
686  
687 Dimensions are in points.
688  
689 =head3 paper_size
690  
691 @box_corners = paper_size($x1,$y1, $x2,$y2);
692  
693 =over
694  
695 Returns an array ($x1,$y1, $x2,$y2) (full bounding box).
696  
697 =back
698  
699 @box_corners = paper_size($x1,$y1);
700  
701 =over
702  
703 Returns an array (0,0, $x1,$y1) (half bounding box).
704  
705 =back
706  
707 @box_corners = paper_size($media_name);
708  
709 =over
710  
711 Returns an array (0,0, paper_width,paper_height) for the named media.
712  
713 =back
714  
715 @box_corners = paper_size($x1);
716  
717 =over
718  
719 Returns an array (0,0, $x1,$x1) (single quadratic).
720  
721 Otherwise, array (0,0, 612,792) (US Letter dimensions) is returned.
722  
723 =back
724  
725 =cut
726  
727 305 ▶sub page_size {
Subroutine page_size: called CC=5 SCAR=16.2
Pod undocumented
728 305 my ($x1,$y1, $x2,$y2) = @_;
729  
730 305 ▶ if (defined $x2) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $x2) { } 27 278 305
elsif (defined $y1) { } 13 265 278
elsif (defined $PDF::Builder::Util::PaperSizes{lc $x1}) { } 264 1 265
elsif ($x1 =~ /^[\d\.]+$/) { } 0 1 1
731 # full bbox
732 27 return ($x1,$y1, $x2,$y2);
733 } elsif (defined $y1) {
734 # half bbox
735 13 return (0,0, $x1,$y1);
736 } elsif (defined $PaperSizes{lc $x1}) {
737 # textual spec.
738 264 return (0,0, @{$PaperSizes{lc $x1}});
739 } elsif ($x1 =~ /^[\d\.]+$/) {
740 # single quadratic
741 0 return(0,0, $x1,$x1);
742 } else {
743 # PDF default (US letter)
744 1 return (0,0, 612,792);
745 }
746 }
747  
748 =head3 getPaperSizes
749  
750 %sizes = getPaperSizes();
751  
752 =over
753  
754 Returns a hash containing the available paper size aliases as keys and
755 their dimensions as a two-element array reference.
756  
757 See the source of L<PDF::Builder::Resource::PaperSizes> for the complete list.
758  
759 =back
760  
761 =cut
762  
763 0 ▶sub getPaperSizes {
Subroutine getPaperSizes: not called CC=2 SCAR=17.9
Pod documented
764 0 my %sizes = ();
765 0 foreach my $type (keys %PaperSizes) {
766 0 $sizes{$type} = [@{$PaperSizes{$type}}];
767 }
768 0 return %sizes;
769 }
770  
771 =head2 STRING TO DIMENSION
772  
773 Convert a string "number [unit]" to the value in desired units. Units are
774 case-insensitive (the input is first folded to lower case).
775  
776 Supported units: mm, cm, in (inch), pt (Big point, 72/inch), ppt (printer's
777 point, 72.27/inch), pc (pica, 6/inch), dd (Didot point, 67.5532/inch), and
778 cc (Ciceros, 5.62943/inch). More can be added easily.
779 Invalid units are a fatal error.
780  
781 =head3 str2dim
782  
783 $value = str2dim($string, $type, $default_units);
784  
785 =over
786  
787 C<$string> contains a number and optionally, a unit. Space(s) between the number
788 and the unit are optional. E.g., '200', '35.2 mm', and '1.5in' are all allowable
789 input strings.
790  
791 C<$type> is for validation of the input $string's numeric value. The first
792 character is B<i> for an I<integer> is required (no decimal point), or B<f> for
793 other (floating point) numbers. Next is an optional B<c> to indicate that an
794 out-of-range input value is to be silently I<clamped> to be within the given
795 range (the default is to raise a fatal error). Finally, an optional I<range>
796 expression: {lower limit,upper limit}. The limits are either numbers or B<*> (to
797 indicate +/- infinity (no limit) on that end of the range). B<{> is B<[> to say
798 that the lower limit is I<included> in the range, while B<(> says that the
799 lower limit is I<excluded> from the range. Likewise, B<}> is B<]> for
800 I<included> upper limit, and B<)> for I<excluded>. The limits (and silent
801 clamping, or fatal error if the input is out of range) are against the input
802 value, before conversion to the output units.
803  
804 Example types:
805  
806 =over
807  
808 =item C<'f(*,*)'> no limits (the default) -- all values OK
809  
810 =item C<'i(0,*)'> integer greater than 0
811  
812 =item C<'fc[-3.2,7.86]'> a number between -3.2 and 7.86, with value clamped to
813 be within that range (including the endpoints)
814  
815 =back
816  
817 C<$default_units> is a required string, giving the units that the input is
818 converted to. For example, if the default units are 'pt', and the input string
819 '2 in', the output value would be '144'. If the input string has no explicit
820 units, it is assumed to be in the default units (no conversion is done).
821  
822 =back
823  
824 =cut
825  
826 # convert string to numeric, converting units to default unit
827 # recognized units are mm, cm, in, pt, ppt (printer's point, 72.27/inch), pc
828 # allow space between number and unit
829 # TBD for floats being clamped and limit is not-inclusive, what value to clamp?
830 # currently limit +/- 1.0
831 # if string is empty or all blank, return 0
832 0 ▶sub str2dim {
Subroutine str2dim: not called CC=43 SCAR=75.5
Pod documented
833 0 my ($string, $type, $defUnit) = @_;
834  
835 0 my ($defUnitIdx, $value, $unit, $unitIdx);
836 # unit names, divisor to get inches
837 # ppt = printer's (old) points, dd = didot ppoints, cc = ciceros
838 0 my @units = ( 'mm', 'cm', 'in', 'pt', 'ppt', 'pc',
839 'dd', 'cc' );
840 0 my @convert = ( 25.4, 2.54, 1, 72, 72.27, 6,
841 67.5532, 5.62943 );
842  
843 # validate default unit
844 0 $defUnit = lc($defUnit);
845 0 ▶ for ($defUnitIdx = 0; $defUnitIdx < @units; $defUnitIdx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defUnitIdx < @units) 0 0 0
846 0 ▶ if ($units[$defUnitIdx] eq $defUnit) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($units[$defUnitIdx] eq $defUnit) 0 0 0
847 }
848 # fell through? invalid default unit
849 0 ▶ if ($defUnitIdx >= @units) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defUnitIdx >= @units) 0 0 0
850 0 die "Error: Unknown default dimensional unit '$defUnit'\n";
851 }
852  
853 0 $string =~ s/\s//g; # remove all whitespace
854 0 ▶ if ($string eq '') { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 0 0 0
855  
856 0 ▶ if ($string =~ m/^([.0-9-]+)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ /^([.0-9-]+)$/i) { } 0 0 0
elsif ($string =~ /^([.0-9-]+)(.*)$/i) { } 0 0 0
857 0 $value = $1;
858 0 $unit = '';
859 } elsif ($string =~ m/^([.0-9-]+)(.*)$/i) {
860 0 $value = $1;
861 0 $unit = lc($2);
862 } else {
863 0 die "Error: Unable to decipher dimensional string '$string'\n";
864 }
865 # is unit good? leaves unitIdx as index into arrays
866 0 ▶ if ($unit ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit ne "") 0 0 0
867 0 ▶ for ($unitIdx = 0; $unitIdx < @units; $unitIdx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unitIdx < @units) 0 0 0
868 0 ▶ if ($units[$unitIdx] eq $unit) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($units[$unitIdx] eq $unit) 0 0 0
869 }
870 # fell through? invalid unit
871 0 ▶ if ($unitIdx >= @units) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unitIdx >= @units) 0 0 0
872 0 die "Error: Unknown dimensional unit '$unit' in '$string'\n";
873 }
874 } # else is bare number
875  
876 # validate number. if type = i (int), only integer permitted
877 # if type = f (float), any valid float OK (no E notation)
878 # in either case, must not be negative
879 # note: no range checking (might be overflow)
880 0 ▶ if ($value =~ m/^-/) { die "Error: Dimensional value '$value $unit' cannot be negative\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-/) 0 0 0
881  
882 0 $type = lc($type);
883 0 $type =~ s/\s//g;
884 0 ▶ if ($type =~ m/^[fi]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^[fi]/) { } 0 0 0
885 # OK type
886 } else {
887 0 die "Error: Invalid type for dimension. Must be 'f' or 'i'\n";
888 }
889 0 ▶ if ($type =~ m/^i/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^i/) { } 0 0 0
890 0 ▶ if (!($value =~ m/^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+$/) 0 0 0
891 0 die "Error: $value is not a valid integer\n";
892 }
893 } else { # presumably f (float)
894 0 ▶ if (!($value =~ m/^\.\d+$/ ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\.\d+$/ or $value =~ /^\d+\.\d+$/ or $value =~ /^\d+\.?$/) 0 0 0
Condition0%0/6
$value =~ /^\.\d+$/ or $value =~ /^\d+\.\d+$/ or $value =~ /^\d+\.?$/
l!l&&r!l&&!r
000
$value =~ /^\.\d+$/ or $value =~ /^\d+\.\d+$/
l!l&&r!l&&!r
000
MC/DC0%0/3
$value =~ /^\.\d+$/ or $value =~ /^\d+\.\d+$/ or $value =~ /^\d+\.?$/
$value =~ /^\.\d+$/ $value =~ /^\d+\.\d+$/ $value =~ /^\d+\.?$/
Truth table0%0/4
A or B or C
A= $value =~ /^\.\d+$/B= $value =~ /^\d+\.\d+$/C= $value =~ /^\d+\.?$/
ABCresult
1XX1
01X1
0011
0000
895 $value =~ m/^\d+\.\d+$/ ||
896 $value =~ m/^\d+\.?$/)) {
897 0 die "Error: $value is not a valid float\n";
898 }
899 }
900  
901 # $value is a legit number, $unit is OK unit. convert if unit different
902 # from default unit
903 0 ▶ if ($unit eq '' || $unit eq $defUnit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq '' or $unit eq $defUnit) { } 0 0 0
Condition0%0/3
$unit eq "" or $unit eq $defUnit
l!l&&r!l&&!r
000
MC/DC0%0/2
$unit eq "" or $unit eq $defUnit
$unit eq "" $unit eq $defUnit
Truth table0%0/3
A or B
A= $unit eq ""B= $unit eq $defUnit
ABresult
1X1
011
000
904 # assume bare number is default unit
905 } else {
906 # convert to inches, and back to defUnit
907 0 $value /= $convert[$unitIdx];
908 0 $value *= $convert[$defUnitIdx];
909 }
910  
911 # range check and optionally clamp: look at remainder of type
912 0 $type = substr($type, 1);
913 0 ▶ if ($type ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "") 0 0 0
914 # format is optional c (for clamp)
915 # [ or ( for lower value is included or excluded from range
916 # lower value or * (- infinity)
917 # comma ,
918 # upper value or * (+ infinity)
919 # ] or ) for upper value is included or excluded from range
920 0 my $clamp = 0; # default to False (error if out of range)
921 0 ▶ if ($type =~ m/^c/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^c/) 0 0 0
922 0 $clamp = 1;
923 0 $type = substr($type, 1); # MUST be at least 5 more char
924 }
925
926 # get lower and upper bounds
927 0 my $lbInf = 1; # * for value T
928 0 my $ubInf = 1; # * for value T
929 0 my ($lb,$ub); # non-* values
930 0 my $lbInc = 0; # [ include T, ( include F
931 0 my $ubInc = 0; # ] include T, ) include F
932 0 ▶ if ($type =~ m/^([\[\(])([^,]+),([^\]\)]+)([\]\)])$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^([\[\(])([^,]+),([^\]\)]+)([\]\)])$/) { } 0 0 0
933 0 $lbInc = ($1 eq '[');
934 0 $lbInf = ($2 eq '*');
935 0 $ubInf = ($3 eq '*');
936 0 $ubInc = ($4 eq ']');
937 0 ▶ if (!$lbInf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lbInf) 0 0 0
938 0 $lb = $2;
939 # must be numeric. don't care int/float
940 0 ▶ if ($lb =~ m/^-?\.\d+$/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($lb =~ /^-?\.\d+$/ or $lb =~ /^-?\d+\.\d+/ or $lb =~ /^-?\d+\.?$/) { } 0 0 0
Condition0%0/6
$lb =~ /^-?\.\d+$/ or $lb =~ /^-?\d+\.\d+/ or $lb =~ /^-?\d+\.?$/
l!l&&r!l&&!r
000
$lb =~ /^-?\.\d+$/ or $lb =~ /^-?\d+\.\d+/
l!l&&r!l&&!r
000
MC/DC0%0/3
$lb =~ /^-?\.\d+$/ or $lb =~ /^-?\d+\.\d+/ or $lb =~ /^-?\d+\.?$/
$lb =~ /^-?\.\d+$/ $lb =~ /^-?\d+\.\d+/ $lb =~ /^-?\d+\.?$/
Truth table0%0/4
A or B or C
A= $lb =~ /^-?\.\d+$/B= $lb =~ /^-?\d+\.\d+/C= $lb =~ /^-?\d+\.?$/
ABCresult
1XX1
01X1
0011
0000
941 $lb =~ m/^-?\d+\.\d+/ ||
942 $lb =~ m/^-?\d+\.?$/ ) {
943 # is numeric
944 0 ▶ if ($lbInc && $value < $lb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lbInc and $value < $lb) 0 0 0
Condition0%0/3
$lbInc and $value < $lb
!ll&&!rl&&r
000
MC/DC0%0/2
$lbInc and $value < $lb
$lbInc $value < $lb
Truth table0%0/3
A and B
A= $lbIncB= $value < $lb
ABresult
0X0
100
111
945 0 ▶ if ($clamp) { $value = $lb; }
Branch: 1 branch
BranchTrueFalseTotal
if ($clamp) { } 0 0 0
946 0 else { die "Error: Value $value is smaller than the limit $lb\n"; }
947 }
948 0 ▶ if (!$lbInc && $value <= $lb) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lbInc and $value <= $lb) 0 0 0
Condition0%0/3
not $lbInc and $value <= $lb
!ll&&!rl&&r
000
MC/DC0%0/2
not $lbInc and $value <= $lb
not $lbInc $value <= $lb
Truth table0%0/3
not(A) and B
A= not $lbIncB= $value <= $lb
ABresult
0X0
100
111
949 0 ▶ if ($clamp) { $value = $lb+1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($clamp) { } 0 0 0
950 0 else { die "Error: Value $value is smaller or equal to the limit $lb\n"; }
951 }
952 } else {
953 0 die "Error: Range lower bound '$lb' not * or number\n";
954 }
955 } # if lb is -inf, don't care what value is
956 0 ▶ if (!$ubInf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ubInf) 0 0 0
957 0 $ub = $3;
958 # must be numeric. don't care int/float
959 0 ▶ if ($ub =~ m/^-?\.\d+$/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($ub =~ /^-?\.\d+$/ or $ub =~ /^-?\d+\.\d+/ or $ub =~ /^-?\d+\.?$/) { } 0 0 0
Condition0%0/6
$ub =~ /^-?\.\d+$/ or $ub =~ /^-?\d+\.\d+/ or $ub =~ /^-?\d+\.?$/
l!l&&r!l&&!r
000
$ub =~ /^-?\.\d+$/ or $ub =~ /^-?\d+\.\d+/
l!l&&r!l&&!r
000
MC/DC0%0/3
$ub =~ /^-?\.\d+$/ or $ub =~ /^-?\d+\.\d+/ or $ub =~ /^-?\d+\.?$/
$ub =~ /^-?\.\d+$/ $ub =~ /^-?\d+\.\d+/ $ub =~ /^-?\d+\.?$/
Truth table0%0/4
A or B or C
A= $ub =~ /^-?\.\d+$/B= $ub =~ /^-?\d+\.\d+/C= $ub =~ /^-?\d+\.?$/
ABCresult
1XX1
01X1
0011
0000
960 $ub =~ m/^-?\d+\.\d+/ ||
961 $ub =~ m/^-?\d+\.?$/ ) {
962 # is numeric
963 0 ▶ if ($ubInc && $value > $ub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ubInc and $value > $ub) 0 0 0
Condition0%0/3
$ubInc and $value > $ub
!ll&&!rl&&r
000
MC/DC0%0/2
$ubInc and $value > $ub
$ubInc $value > $ub
Truth table0%0/3
A and B
A= $ubIncB= $value > $ub
ABresult
0X0
100
111
964 0 ▶ if ($clamp) { $value = $ub; }
Branch: 1 branch
BranchTrueFalseTotal
if ($clamp) { } 0 0 0
965 0 else { die "Error: Value $value is larger than the limit $ub\n"; }
966 }
967 0 ▶ if (!$ubInc && $value >= $ub) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ubInc and $value >= $ub) 0 0 0
Condition0%0/3
not $ubInc and $value >= $ub
!ll&&!rl&&r
000
MC/DC0%0/2
not $ubInc and $value >= $ub
not $ubInc $value >= $ub
Truth table0%0/3
not(A) and B
A= not $ubIncB= $value >= $ub
ABresult
0X0
100
111
968 0 ▶ if ($clamp) { $value = $ub-1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($clamp) { } 0 0 0
969 0 else { die "Error: Value $value is larger or equal to the limit $ub\n"; }
970 }
971 } else {
972 0 die "Error: Range upper bound '$ub' not * or number\n";
973 }
974 } # if ub is +inf, don't care what value is
975  
976 } else {
977 0 die "Error: Invalid range specification '$type'\n";
978 }
979 }
980
981 0 return $value;
982 } # end of str2dim()
983  
984 39 1;
985  
986 __END__
« Builder/Content.pm ↑ Summary Builder.pm »