blib/lib/ICC/Profile/mluc.pm

filter: Statementstmt 68.1% 75 / 110 Branchbran 15.0% 6 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 84.6% 11 / 13 Podpod 16.6% 1 / 6 totaltotal 50.5% 93 / 184 CC 29 SCAR 39.4
CC
29
Cov
51%
text
156.0
sdump
30.0
new
7.0
CRAP 51.3
« blib/lib/ICC/Support/nNET.pm ↑ Summary blib/lib/ICC/Profile/mAB_.pm »
1 package ICC::Profile::mluc;
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.12;
7  
8 # revised 2015-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 mluc tag object
22 # use 'text' method to add additional entries
23 # parameters: ()
24 # parameters: (language_code, country_code, text)
25 # returns: (ref_to_object)
26 sub new {
27  
28 # get object class
29 1 ▶ my $class = shift();
Subroutine new: called CC=4 SCAR=19.4
Pod undocumented
30
31 # create empty mluc object
32 1 my $self = [
33 {}, # object header
34 12, # name record size
35 [] # array of name records
36 ];
37
38 # if three parameters
39 1 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 0 1 1
40
41 # verify country and language codes
42 0 ▶ (length($_[0]) == 2 && length($_[1]) == 2) || croak('country or language code wrong length');
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 2 and length $_[1] == 2 or croak("country or language code wrong length") 0 0 0
Condition0%0/3
length $_[0] == 2 and length $_[1] == 2
!ll&&!rl&&r
000
MC/DC0%0/2
length $_[0] == 2 and length $_[1] == 2
length $_[0] == 2 length $_[1] == 2
Truth table0%0/3
A and B
A= length $_[0] == 2B= length $_[1] == 2
ABresult
0X0
100
111
43
44 # add name record
45 0 $self->[2][0] = [$_[0], $_[1], 0, 0, $_[2]];
46
47 }
48  
49 # bless object
50 1 bless($self, $class);
51  
52 # return object reference
53 1 return($self);
54  
55 }
56  
57 # create mluc tag object from ICC profile
58 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
59 # returns: (ref_to_object)
60 sub new_fh {
61  
62 # get object class
63 1 ▶ my $class = shift();
Subroutine new_fh: called CC=2 SCAR=7.0
Pod undocumented
64  
65 # create empty mluc object
66 1 my $self = [
67 {}, # object header
68 0, # name record size
69 [] # array of name records
70 ];
71  
72 # verify 3 parameters
73 1 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 1 1
74  
75 # read mluc data from profile
76 1 _readICCmluc($self, @_);
77  
78 # bless object
79 1 bless($self, $class);
80  
81 # return object reference
82 1 return($self);
83  
84 }
85  
86 # writes mluc tag object to ICC profile
87 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
88 sub write_fh {
89  
90 # get tag reference
91 1 ▶ my $self = shift();
Subroutine write_fh: called CC=2 SCAR=7.1
Pod undocumented
92  
93 # verify 3 parameters
94 1 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 1 1
95  
96 # write mluc data to profile
97 1 _writeICCmluc($self, @_);
98  
99 }
100  
101 # get tag size (for writing to profile)
102 # returns: (tag_size)
103 sub size {
104
105 # get parameters
106 3 ▶ my ($self) = @_;
Subroutine size: called CC=3 SCAR=11.1
Pod undocumented
107
108 # local variables
109 3 my ($text);
110
111 # if count > 0
112 3 ▶ if (@{$self->[2]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[2];}) { } 3 0 3
113
114 # initialize text size
115 3 $text = 0;
116
117 # for each name record
118 3 for my $rec (@{$self->[2]}) {
119
120 # add text string size
121 3 $text += length($rec->[4]);
122
123 }
124
125 # return size (after Unicode encoding)
126 3 return(16 + @{$self->[2]} * 12 + $text * 2);
127
128 } else {
129
130 # return size
131 0 return(12);
132
133 }
134
135 }
136  
137 # get/set Unicode mluc string
138 # updates text if language/country found
139 # otherwise, adds new table entry
140 # parameters: (language_code, country_code, [text])
141 # returns: (mluc_string)
142 sub text {
143  
144 # get parameters
145 0 ▶ my $self = shift();
Subroutine text: not called CC=12 SCAR=50.5
Pod undocumented
146
147 # local variables
148 0 my (@match);
149
150 # if two parameters (get)
151 0 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
152
153 # return if name record count = 0
154 0 ▶ return if (@{$self->[2]} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->[2];} == 0 0 0 0
155
156 # match country and language codes
157 0 ▶ @match = grep {$_->[1] eq $_[1] && $_->[0] eq $_[0]} @{$self->[2]};
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] eq $_[1] and $_->[0] eq $_[0] 0 0 0
158
159 # match country code
160 0 ▶ @match = grep {$_->[1] eq $_[1]} @{$self->[2]} if (@match == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @match == 0 0 0 0
161  
162 # match language code
163 0 ▶ @match = grep {$_->[0] eq $_[0]} @{$self->[2]} if (@match == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @match == 0 0 0 0
164
165 # use first name record
166 0 ▶ @match = ($self->[2][0]) if (@match == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @match == 0 0 0 0
167
168 # return name record string
169 0 return($match[0][4]);
170
171 # if three parameters (set)
172 } elsif (@_ == 3) {
173
174 # match country and language codes
175 0 ▶ @match = grep {($_->[1] eq $_[1]) && ($_->[0] eq $_[0])} @{$self->[2]};
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] eq $_[1] and $_->[0] eq $_[0] 0 0 0
176
177 # if match found
178 0 ▶ if (@match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@match) { } 0 0 0
179
180 # set name record text
181 0 $match[0][4] = $_[2];
182
183 } else {
184
185 # verify country and language codes
186 0 ▶ (length($_[0]) == 2 && length($_[1]) == 2) || croak('country or language code wrong length');
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 2 and length $_[1] == 2 or croak("country or language code wrong length") 0 0 0
Condition0%0/3
length $_[0] == 2 and length $_[1] == 2
!ll&&!rl&&r
000
MC/DC0%0/2
length $_[0] == 2 and length $_[1] == 2
length $_[0] == 2 length $_[1] == 2
Truth table0%0/3
A and B
A= length $_[0] == 2B= length $_[1] == 2
ABresult
0X0
100
111
187
188 # add new name record
189 0 push(@{$self->[2]}, [$_[0], $_[1], 0, 0, $_[2]]);
190
191 }
192
193 # return name record string
194 0 return($_[2]);
195
196 } else {
197
198 # warning message
199 0 carp('wrong number of parameters');
200
201 }
202
203 }
204  
205 # print object contents to string
206 # format is an array structure
207 # parameter: ([format])
208 # returns: (string)
209 sub sdump {
210  
211 # get parameters
212 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
213  
214 # local variables
215 0 my ($s, $fmt);
216  
217 # resolve parameter to an array reference
218 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
219  
220 # get format string
221 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
222  
223 # set string to object ID
224 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
225  
226 # return
227 0 return($s);
228  
229 }
230  
231 # read mluc tag from ICC profile
232 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
233 sub _readICCmluc {
234
235 # get parameters
236 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _readICCmluc: called CC=4 SCAR=13.9
237
238 # local variables
239 1 my ($buf, $cnt);
240
241 # save tag signature
242 1 $self->[0]{'signature'} = $tag->[0];
243
244 # seek start of tag
245 1 seek($fh, $tag->[1], 0);
246
247 # read type sig and count
248 1 read($fh, $buf, 12);
249
250 # unpack name record count
251 1 $cnt = unpack('x8 N', $buf);
252
253 # return if count = 0
254 1 ▶ return if ($cnt == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 1 1
255
256 # read name record size
257 1 read($fh, $buf, 4);
258
259 # unpack name record size
260 1 $self->[1] = unpack('N', $buf);
261
262 # for each name record
263 1 for my $i (0 .. $cnt - 1) {
264
265 # read name record
266 1 read($fh, $buf, $self->[1]);
267
268 # unpack language/country codes, length and offset
269 1 $self->[2][$i] = [unpack('a2 a2 N N', $buf)];
270
271 }
272
273 # for each name record
274 1 for my $rec (@{$self->[2]}) {
275
276 # seek text string
277 1 seek($fh, $tag->[1] + $rec->[3], 0);
278
279 # read text string
280 1 read($fh, $buf, $rec->[2]);
281
282 # save decoded Unicode data
283 1 $rec->[4] = decode('UTF-16BE', $buf);
284
285 }
286
287 }
288  
289 # write mluc tag to ICC profile
290 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
291 sub _writeICCmluc {
292  
293 # get parameters
294 1 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCmluc: called CC=4 SCAR=13.9
295  
296 # local variables
297 1 my ($cnt, $offset);
298  
299 # get name record count
300 1 $cnt = @{$self->[2]};
301  
302 # seek start of tag
303 1 seek($fh, $tag->[1], 0);
304  
305 # write type sig and count
306 1 print $fh pack('a4 x4 N', 'mluc', $cnt);
307  
308 # return if count = 0
309 1 ▶ return if ($cnt == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 1 1
310  
311 # write name record size
312 1 print $fh pack('N', $self->[1]);
313  
314 # compute initial text string offset
315 1 $offset = 16 + $cnt * 12;
316  
317 # for each name record
318 1 for my $rec (@{$self->[2]}) {
319
320 # write language/country codes, length and offset
321 1 print $fh pack('a2 a2 N N', @{$rec}[0 .. 1], length($rec->[4]) * 2, $offset);
322
323 # update offset
324 1 $offset += length($rec->[4]) * 2;
325
326 }
327
328 # for each name record
329 1 for my $rec (@{$self->[2]}) {
330
331 # write the Unicode string
332 1 print $fh encode('UTF-16BE', $rec->[4]);
333
334 }
335
336 }
337  
338 2 1;
« blib/lib/ICC/Support/nNET.pm ↑ Summary blib/lib/ICC/Profile/mAB_.pm »