blib/lib/ICC/Profile/desc.pm

filter: Statementstmt 62.1% 59 / 95 Branchbran 17.6% 6 / 34 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.7% 11 / 16 Podpod 12.5% 1 / 8 totaltotal 47.8% 78 / 163 CC 21 SCAR 24.0
CC
21
Cov
49%
sdump
30.0
Unicode
20.0
ScriptCode
20.0
CRAP 11.0
« blib/lib/ICC/Profile/XYZ_.pm ↑ Summary blib/lib/ICC/Support/geo1.pm »
1 package ICC::Profile::desc;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = 0.22;
7  
8 # revised 2018-08-07
9 #
10 # Copyright © 2004-2018 by William B. Birkett
11  
12 # add development directory
13 2 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # inherit from Shared
16 2 ▶use parent qw(ICC::Shared);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # support modules
19 2 ▶use Encode; # Unicode module
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # create new desc tag object
22 # parameters: ()
23 # parameters: (ref_to_attribute_hash)
24 # returns: (ref_to_object)
25 sub new {
26  
27 # get object class
28 1 ▶ my $class = shift();
Subroutine new: called CC=3 SCAR=12.3
Pod undocumented
29
30 # create empty desc object
31 1 my $self = [
32 {}, # object header
33 '', # ASCII string
34 0, # Unicode language
35 '', # Unicode string
36 0, # ScriptCode code
37 '' # ScriptCode string
38 ];
39  
40 # if single parameter is a hash reference
41 1 ▶ if (@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH") 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
42  
43 # set object attributes
44 0 _newICCdesc($self, @_);
45  
46 }
47  
48 # bless object
49 1 bless($self, $class);
50  
51 # return object reference
52 1 return($self);
53  
54 }
55  
56 # create desc tag object from ICC profile
57 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
58 # returns: (ref_to_object)
59 sub new_fh {
60  
61 # get object class
62 1 ▶ my $class = shift();
Subroutine new_fh: called CC=2 SCAR=7.0
Pod undocumented
63  
64 # create empty desc object
65 1 my $self = [
66 {}, # object header
67 '', # ASCII string
68 0, # Unicode language
69 '', # Unicode string
70 0, # ScriptCode language
71 '' # ScriptCode string
72 ];
73  
74 # verify 3 parameters
75 1 ▶ (@_ == 3) or croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 1 1
76  
77 # read desc data from profile
78 1 _readICCdesc($self, @_);
79  
80 # bless object
81 1 bless($self, $class);
82  
83 # return object reference
84 1 return($self);
85  
86 }
87  
88 # writes desc tag object to ICC profile
89 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
90 sub write_fh {
91  
92 # verify 4 parameters
93 1 ▶ (@_ == 4) or croak('wrong number of parameters');
Subroutine write_fh: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or croak("wrong number of parameters") 0 1 1
94  
95 # write desc data to profile
96 1 goto &_writeICCdesc;
97  
98 }
99  
100 # get tag size (for writing to profile)
101 # returns: (tag_size)
102 sub size {
103
104 # get parameters
105 3 ▶ my ($self) = @_;
Subroutine size: called CC=2 SCAR=7.2
Pod undocumented
106
107 # return size
108 3 ▶ return(91 + length($self->[1]) + 2 * length($self->[3]) + (length($self->[3]) > 0 ? 2 : 0));
Branch: 1 branch
BranchTrueFalseTotal
length $self->[3] > 0 ? : 3 0 3
109
110 }
111  
112 # get ASCII desc string
113 # parameters: ([desc_string])
114 # returns: (desc_string)
115 sub ASCII {
116  
117 # get object reference
118 0 ▶ my $self = shift();
Subroutine ASCII: not called CC=2 SCAR=17.9
Pod undocumented
119
120 # if parameter supplied
121 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
122
123 # save desc string
124 0 $self->[1] = shift();
125
126 }
127
128 # return desc string
129 0 return($self->[1]);
130  
131 }
132  
133 # get Unicode desc string
134 # parameters: ([desc_string, [lang_code]])
135 # returns: (desc_string, [lang_code])
136 sub Unicode {
137  
138 # get object reference
139 0 ▶ my $self = shift();
Subroutine Unicode: not called CC=4 SCAR=30.0
Pod undocumented
140
141 # if parameter supplied
142 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
143
144 # save desc string
145 0 $self->[3] = shift();
146
147 # if parameter supplied
148 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
149
150 # save language code
151 0 $self->[2] = shift();
152
153 }
154
155 }
156
157 # if language code wanted
158 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
159
160 # return desc string and language code
161 0 return($self->[3], $self->[2]);
162
163 } else {
164
165 # return desc string
166 0 return($self->[3]);
167
168 }
169  
170 }
171  
172 # get ScriptCode desc string
173 # parameters: ([desc_string, [ScriptCode_code]])
174 # returns: (desc_string, [ScriptCode_code])
175 sub ScriptCode {
176  
177 # get object reference
178 0 ▶ my $self = shift();
Subroutine ScriptCode: not called CC=4 SCAR=30.0
Pod undocumented
179
180 # if parameter supplied
181 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
182
183 # save desc string
184 0 $self->[5] = shift();
185
186 # if parameter supplied
187 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
188
189 # save ScriptCode code
190 0 $self->[4] = shift();
191
192 }
193
194 }
195
196 # if ScriptCode code wanted
197 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
198
199 # return desc string and ScriptCode code
200 0 return($self->[5], $self->[4]);
201
202 } else {
203
204 # return desc string
205 0 return($self->[5]);
206
207 }
208
209 }
210  
211 # print object contents to string
212 # format is an array structure
213 # parameter: ([format])
214 # returns: (string)
215 sub sdump {
216  
217 # get parameters
218 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
219  
220 # local variables
221 0 my ($s, $fmt);
222  
223 # resolve parameter to an array reference
224 0 ▶ $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
Branch: 2 branches
BranchTrueFalseTotal
defined $p ? : 0 0 0
ref $p eq 'ARRAY' ? : 0 0 0
225  
226 # get format string
227 0 ▶ $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $p->[0] && !ref($p->[0]) ? : 0 0 0
Condition0%0/3
defined $p->[0] and not ref $p->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p->[0] and not ref $p->[0]
defined $p->[0] not ref $p->[0]
Truth table0%0/3
A and not(B)
A= defined $p->[0]B= not ref $p->[0]
ABresult
0X0
100
111
228  
229 # set string to object ID
230 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
231  
232 # return
233 0 return($s);
234  
235 }
236  
237 # set object attributes from parameter hash
238 # supported attributes: 'ascii', 'unicode_lang', 'unicode', 'scriptcode_lang', 'scriptcode'
239 # parameters: (ref_to_object, parameter_hash)
240 sub _newICCdesc {
241
242 # get parameters
243 0 ▶ my ($self, $pars) = @_;
Subroutine _newICCdesc: not called CC=3 SCAR=24.8
244
245 # local variables
246 0 my (%desc);
247
248 # hash of description strings
249 0 %desc = ('ascii' => 1, 'unicode_lang' => 2, 'unicode' => 3, 'scriptcode_lang' => 4, 'scriptcode' => 5);
250
251 # for each parameter key
252 0 for my $key (keys(%{$pars})) {
253
254 # if supported key
255 0 ▶ if (exists($desc{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $desc{$key}) 0 0 0
256
257 # save value
258 0 $self->[$desc{$key}] = $pars->{$key};
259
260 }
261
262 }
263
264 }
265  
266 # read desc tag from ICC profile
267 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
268 sub _readICCdesc {
269
270 # get parameters
271 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCdesc: called CC=1 SCAR=0.0
272
273 # local variables
274 1 my ($buf, $cnt);
275
276 # save tag signature
277 1 $self->[0]{'signature'} = $tag->[0];
278
279 # seek start of tag
280 1 seek($fh, $tag->[1], 0);
281
282 # read first 12 bytes
283 1 read($fh, $buf, 12);
284
285 # unpack ASCII string count
286 1 $cnt = unpack('x8 N', $buf);
287
288 # read ASCII string and Unicode language/count
289 1 read($fh, $buf, $cnt + 8);
290
291 # unpack ASCII string and Unicode language/count
292 1 ($self->[1], $self->[2], $cnt) = unpack("Z$cnt N2", $buf);
293
294 # doulbe Unicode count
295 1 $cnt *= 2;
296
297 # read Unicode string and ScriptCode language/count
298 1 read($fh, $buf, $cnt + 3);
299
300 # unpack Unicode string and ScriptCode language/count
301 1 ($self->[3], $self->[4], $cnt) = unpack("a$cnt nC", $buf);
302
303 # decode Unicode string
304 1 $self->[3] = decode('UTF-16BE', $self->[3]);
305
306 # chop null terminator
307 1 chop($self->[3]);
308
309 # read ScriptCode string
310 1 read($fh, $buf, 67);
311
312 # unpack ScriptCode string
313 1 $self->[5] = unpack("Z$cnt", $buf);
314
315 }
316  
317 # write desc tag to ICC profile
318 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
319 sub _writeICCdesc {
320  
321 # get parameters
322 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCdesc: called CC=3 SCAR=11.3
323  
324 # local variables
325 1 my ($cnt, $ufmt);
326  
327 # seek start of tag
328 1 seek($fh, $tag->[1], 0);
329  
330 # get ASCII count
331 1 $cnt = length($self->[1]) + 1;
332  
333 # write ASCII
334 1 print $fh pack("a4 x4 N Z$cnt", 'desc', $cnt, $self->[1]);
335  
336 # get Unicode count
337 1 $cnt = length($self->[3]) + 1;
338  
339 # if count > 1
340 1 ▶ if ($cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 1) { } 1 0 1
341
342 # make Unicode format string
343 1 $ufmt = 'a' . (2 * $cnt);
344
345 # write Unicode
346 1 print $fh pack("N N $ufmt", $self->[2], $cnt, encode('UTF-16BE', ($self->[3] . chr(0))));
347
348 } else {
349
350 # write nulls
351 0 print $fh pack('x8');
352
353 }
354
355 # get ScriptCode count
356 1 $cnt = length($self->[5]) + 1;
357
358 # if count > 1
359 1 ▶ if ($cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 1) { } 1 0 1
360
361 # write ScriptCode
362 1 print $fh pack('n C Z67', $self->[4], $cnt, $self->[5]);
363
364 } else {
365
366 # write nulls
367 0 print $fh pack('x70');
368
369 }
370
371 }
372  
373 2 1;
« blib/lib/ICC/Profile/XYZ_.pm ↑ Summary blib/lib/ICC/Support/geo1.pm »