File Coverage

blib/lib/ICC/Profile/text.pm
Criterion Covered Total %
statement 41 52 78.8
branch 4 16 25.0
condition 0 3 0.0
subroutine 10 12 83.3
pod 1 6 16.6
total 56 89 62.9


line stmt bran cond sub pod time code
1             package ICC::Profile::text;
2              
3 2     2   122932 use strict;
  2         13  
  2         54  
4 2     2   9 use Carp;
  2         3  
  2         127  
5              
6             our $VERSION = 0.12;
7              
8             # revised 2018-08-07
9             #
10             # Copyright © 2004-2019 by William B. Birkett
11              
12             # add development directory
13 2     2   482 use lib 'lib';
  2         660  
  2         10  
14              
15             # inherit from Shared
16 2     2   244 use parent qw(ICC::Shared);
  2         6  
  2         24  
17              
18             # create new text tag object
19             # parameters: ([text_string])
20             # returns: (ref_to_object)
21             sub new {
22              
23             # get object class
24 1     1 0 750 my $class = shift();
25            
26             # create empty text object
27 1         3 my $self = [
28             {}, # object header
29             '' # text string
30             ];
31              
32             # if parameter supplied
33 1 50       5 if (@_) {
34            
35             # save it
36 0         0 $self->[1] = shift();
37            
38             }
39              
40             # bless object
41 1         2 bless($self, $class);
42            
43             # return object reference
44 1         3 return($self);
45              
46             }
47              
48             # create text tag object from ICC profile
49             # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
50             # returns: (ref_to_object)
51             sub new_fh {
52              
53             # get object class
54 1     1 0 663 my $class = shift();
55              
56             # create empty text object
57 1         3 my $self = [
58             {}, # object header
59             '' # text string
60             ];
61              
62             # verify 3 parameters
63 1 50       5 (@_ == 3) or croak('wrong number of parameters');
64              
65             # read text data from profile
66 1         5 _readICCtext($self, @_);
67              
68             # bless object
69 1         3 bless($self, $class);
70              
71             # return object reference
72 1         8 return($self);
73              
74             }
75              
76             # writes text tag object to ICC profile
77             # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
78             sub write_fh {
79              
80             # verify 4 parameters
81 1 50   1 0 1152 (@_ == 4) or croak('wrong number of parameters');
82              
83             # write text data to profile
84 1         6 goto &_writeICCtext;
85              
86             }
87              
88             # get tag size (for writing to profile)
89             # returns: (tag_size)
90             sub size {
91            
92             # get parameters
93 3     3 0 483 my ($self) = @_;
94            
95             # get text string
96 3         11 my $txt = $self->[1];
97            
98             # strip out non-ASCII characters
99 3         12 $txt =~ s/[^\x00-\x7F]//g;
100            
101             # return size (string is null terminated)
102 3         24 return(8 + length($txt) + 1);
103            
104             }
105              
106             # get/set text string
107             # parameters: ([text_string])
108             # returns: (text_string)
109             sub text {
110              
111             # get object reference
112 0     0 0 0 my $self = shift();
113            
114             # if parameter supplied
115 0 0       0 if (@_) {
116            
117             # save it
118 0         0 $self->[1] = shift();
119            
120             }
121            
122             # return text string
123 0         0 return($self->[1]);
124              
125             }
126              
127             # print object contents to string
128             # format is an array structure
129             # parameter: ([format])
130             # returns: (string)
131             sub sdump {
132              
133             # get parameters
134 0     0 1 0 my ($self, $p) = @_;
135              
136             # local variables
137 0         0 my ($s, $fmt);
138              
139             # resolve parameter to an array reference
140 0 0       0 $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
    0          
141              
142             # get format string
143 0 0 0     0 $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef';
144              
145             # set string to object ID
146 0         0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
147              
148             # return
149 0         0 return($s);
150              
151             }
152              
153             # read text tag from ICC profile
154             # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
155             sub _readICCtext {
156            
157             # get parameters
158 1     1   3 my ($self, $parent, $fh, $tag) = @_;
159            
160             # local variables
161 1         1 my ($buf);
162            
163             # save tag signature
164 1         3 $self->[0]{'signature'} = $tag->[0];
165            
166             # seek start of tag
167 1         12 seek($fh, $tag->[1], 0);
168            
169             # read tag
170 1         10 read($fh, $buf, $tag->[2]);
171            
172             # unpack text string (null terminated)
173 1         6 $self->[1] = unpack('x8 Z*', $buf);
174            
175             }
176              
177             # write text tag to ICC profile
178             # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
179             sub _writeICCtext {
180              
181             # get parameters
182 1     1   4 my ($self, $parent, $fh, $tag) = @_;
183              
184             # local variables
185 1         2 my ($txt);
186              
187             # seek start of tag
188 1         9 seek($fh, $tag->[1], 0);
189              
190             # get text string
191 1         5 $txt = $self->[1];
192              
193             # strip out non-ASCII characters and warn
194 1 50       7 ($txt =~ s/[^\x00-\x7F]//g) && carp('non-ASCII character(s) removed from \'text\' tag');
195              
196             # write tag
197 1         28 print $fh pack('a4 x4 Z*', 'text', $txt);
198              
199             }
200              
201             1;