blib/lib/ICC/Profile/desc.pm

filter: Statementstmt 62.5% 60 / 96 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 48.1% 79 / 164 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 2016-05-17
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) || 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 # get tag reference
93 1 ▶ my $self = shift();
Subroutine write_fh: called CC=2 SCAR=7.1
Pod undocumented
94  
95 # verify 3 parameters
96 1 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 1 1
97  
98 # write desc data to profile
99 1 _writeICCdesc($self, @_);
100  
101 }
102  
103 # get tag size (for writing to profile)
104 # returns: (tag_size)
105 sub size {
106
107 # get parameters
108 3 ▶ my ($self) = @_;
Subroutine size: called CC=2 SCAR=7.2
Pod undocumented
109
110 # return size
111 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
112
113 }
114  
115 # get ASCII desc string
116 # parameters: ([desc_string])
117 # returns: (desc_string)
118 sub ASCII {
119  
120 # get object reference
121 0 ▶ my $self = shift();
Subroutine ASCII: not called CC=2 SCAR=17.9
Pod undocumented
122
123 # if parameter supplied
124 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
125
126 # save desc string
127 0 $self->[1] = shift();
128
129 }
130
131 # return desc string
132 0 return($self->[1]);
133  
134 }
135  
136 # get Unicode desc string
137 # parameters: ([desc_string, [lang_code]])
138 # returns: (desc_string, [lang_code])
139 sub Unicode {
140  
141 # get object reference
142 0 ▶ my $self = shift();
Subroutine Unicode: not called CC=4 SCAR=30.0
Pod undocumented
143
144 # if parameter supplied
145 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
146
147 # save desc string
148 0 $self->[3] = shift();
149
150 # if parameter supplied
151 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
152
153 # save language code
154 0 $self->[2] = shift();
155
156 }
157
158 }
159
160 # if language code wanted
161 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
162
163 # return desc string and language code
164 0 return($self->[3], $self->[2]);
165
166 } else {
167
168 # return desc string
169 0 return($self->[3]);
170
171 }
172  
173 }
174  
175 # get ScriptCode desc string
176 # parameters: ([desc_string, [ScriptCode_code]])
177 # returns: (desc_string, [ScriptCode_code])
178 sub ScriptCode {
179  
180 # get object reference
181 0 ▶ my $self = shift();
Subroutine ScriptCode: not called CC=4 SCAR=30.0
Pod undocumented
182
183 # if parameter supplied
184 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
185
186 # save desc string
187 0 $self->[5] = shift();
188
189 # if parameter supplied
190 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
191
192 # save ScriptCode code
193 0 $self->[4] = shift();
194
195 }
196
197 }
198
199 # if ScriptCode code wanted
200 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
201
202 # return desc string and ScriptCode code
203 0 return($self->[5], $self->[4]);
204
205 } else {
206
207 # return desc string
208 0 return($self->[5]);
209
210 }
211
212 }
213  
214 # print object contents to string
215 # format is an array structure
216 # parameter: ([format])
217 # returns: (string)
218 sub sdump {
219  
220 # get parameters
221 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
222  
223 # local variables
224 0 my ($s, $fmt);
225  
226 # resolve parameter to an array reference
227 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
228  
229 # get format string
230 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
231  
232 # set string to object ID
233 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
234  
235 # return
236 0 return($s);
237  
238 }
239  
240 # set object attributes from parameter hash
241 # supported attributes: 'ascii', 'unicode_lang', 'unicode', 'scriptcode_lang', 'scriptcode'
242 # parameters: (ref_to_object, parameter_hash)
243 sub _newICCdesc {
244
245 # get parameters
246 0 ▶ my ($self, $pars) = @_;
Subroutine _newICCdesc: not called CC=3 SCAR=24.8
247
248 # local variables
249 0 my (%desc);
250
251 # hash of description strings
252 0 %desc = ('ascii' => 1, 'unicode_lang' => 2, 'unicode' => 3, 'scriptcode_lang' => 4, 'scriptcode' => 5);
253
254 # for each parameter key
255 0 for my $key (keys(%{$pars})) {
256
257 # if supported key
258 0 ▶ if (exists($desc{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $desc{$key}) 0 0 0
259
260 # save value
261 0 $self->[$desc{$key}] = $pars->{$key};
262
263 }
264
265 }
266
267 }
268  
269 # read desc tag from ICC profile
270 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
271 sub _readICCdesc {
272
273 # get parameters
274 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCdesc: called CC=1 SCAR=0.0
275
276 # local variables
277 1 my ($buf, $cnt);
278
279 # save tag signature
280 1 $self->[0]{'signature'} = $tag->[0];
281
282 # seek start of tag
283 1 seek($fh, $tag->[1], 0);
284
285 # read first 12 bytes
286 1 read($fh, $buf, 12);
287
288 # unpack ASCII string count
289 1 $cnt = unpack('x8 N', $buf);
290
291 # read ASCII string and Unicode language/count
292 1 read($fh, $buf, $cnt + 8);
293
294 # unpack ASCII string and Unicode language/count
295 1 ($self->[1], $self->[2], $cnt) = unpack("Z$cnt N2", $buf);
296
297 # doulbe Unicode count
298 1 $cnt *= 2;
299
300 # read Unicode string and ScriptCode language/count
301 1 read($fh, $buf, $cnt + 3);
302
303 # unpack Unicode string and ScriptCode language/count
304 1 ($self->[3], $self->[4], $cnt) = unpack("a$cnt nC", $buf);
305
306 # decode Unicode string
307 1 $self->[3] = decode('UTF-16BE', $self->[3]);
308
309 # chop null terminator
310 1 chop($self->[3]);
311
312 # read ScriptCode string
313 1 read($fh, $buf, 67);
314
315 # unpack ScriptCode string
316 1 $self->[5] = unpack("Z$cnt", $buf);
317
318 }
319  
320 # write desc tag to ICC profile
321 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
322 sub _writeICCdesc {
323  
324 # get parameters
325 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCdesc: called CC=3 SCAR=11.3
326  
327 # local variables
328 1 my ($cnt, $ufmt);
329  
330 # seek start of tag
331 1 seek($fh, $tag->[1], 0);
332  
333 # get ASCII count
334 1 $cnt = length($self->[1]) + 1;
335  
336 # write ASCII
337 1 print $fh pack("a4 x4 N Z$cnt", 'desc', $cnt, $self->[1]);
338  
339 # get Unicode count
340 1 $cnt = length($self->[3]) + 1;
341  
342 # if count > 1
343 1 ▶ if ($cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 1) { } 1 0 1
344
345 # make Unicode format string
346 1 $ufmt = 'a' . (2 * $cnt);
347
348 # write Unicode
349 1 print $fh pack("N N $ufmt", $self->[2], $cnt, encode('UTF-16BE', ($self->[3] . chr(0))));
350
351 } else {
352
353 # write nulls
354 0 print $fh pack('x8');
355
356 }
357
358 # get ScriptCode count
359 1 $cnt = length($self->[5]) + 1;
360
361 # if count > 1
362 1 ▶ if ($cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 1) { } 1 0 1
363
364 # write ScriptCode
365 1 print $fh pack('n C Z67', $self->[4], $cnt, $self->[5]);
366
367 } else {
368
369 # write nulls
370 0 print $fh pack('x70');
371
372 }
373
374 }
375  
376 2 1;
« blib/lib/ICC/Profile/XYZ_.pm ↑ Summary blib/lib/ICC/Support/geo1.pm »