ButtonMaker.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ButtonMaker/ButtonClass.pm ↑ Summary ButtonMaker/Button.pm »
1 package Image::ButtonMaker;
2  
3 our $VERSION = '0.1.4';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Image::ButtonMaker::ButtonClass;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Image::ButtonMaker::ClassContainer;
9 use Image::ButtonMaker::Lexicon;
10 use locale;
11  
12 my @default = (
13 image_dirs => undef,
14 font_dirs => undef,
15 target_dir => undef,
16 lang_id => undef,
17  
18 classes => undef, ## Class container
19 buttons => undef, ## Button hash
20 lexicon => undef, ## Lexicon object
21 );
22  
23 sub new {
24 my $invocant = shift;
25 my $class = ref($invocant) || $invocant;
26  
27 my $object = {@default, @_};
28 $object->{image_dirs} = [];
29 $object->{font_dirs} = [];
30  
31  
32 $object->{classes} = Image::ButtonMaker::ClassContainer->new();
33 $object->{buttons} = [];
34  
35 bless $object, $class;
36 }
37  
38 #### Replace path specific properties
39 sub replace_properties {
40 my $self = shift;
41 my $butref = shift;
42 my $lexicon = $self->{lexicon};
43 my $lang_id = $self->{lang_id};
44  
45 my %buthash = (@$butref);
46 my $properties = $buthash{properties};
47  
48 my $noLex = $properties->{NoLexicon} ? 1:0;
49  
50 foreach my $key (keys(%$properties)) {
51 my $value = $properties->{$key};
52  
53 #### Lexicon lookup
54 if(!$noLex && ($key eq 'Text')) {
55 if($lexicon && length($lang_id)) {
56 my $replace = $lexicon->lookup($lang_id, $value);
57 if(length($replace)) {
58 $properties->{$key} = $replace;
59 } else {
60 print STDERR "WARNING: lexicon lookup($lang_id, $value) returned nothing\n";
61 }
62 }
63 }
64  
65 #### Path replacements ################################
66 elsif($key eq 'TextFont') {
67 my $filename = $self->find_font_file($value);
68 print STDERR "WARNING: could not find file for font: $value"
69 unless(defined $filename);
70 $properties->{$key} = $filename;
71 }
72 elsif($key eq 'CanvasTemplateImg') {
73 $properties->{$key} = $self->find_image_file($value);
74 }
75 elsif($key eq 'IconName') {
76 $properties->{$key} = $self->find_image_file($value);
77 }
78 }
79 }
80  
81  
82 ### Instance Specific Methods ########################################
83 sub read_classfile {
84 my $self = shift;
85 while(my $classfile = shift) {
86 my $classcontainer = $self->{classes};
87  
88 my $classList = do $classfile;
89 die "Error reading classfile $classfile: $@" if($@);
90 die "Error reading classfile $classfile: $!" if($!);
91  
92 foreach my $class (@$classList) {
93 $self->replace_properties($class);
94 my $class_obj = Image::ButtonMaker::ButtonClass->new(@$class);
95 die "CLASSERROR ($Image::ButtonMaker::ButtonClass::error) $Image::ButtonMaker::ButtonClass::errorstr"
96 unless($class_obj);
97 $classcontainer->add_class($class_obj);
98 }
99  
100 die "Could not read classfile $classfile because:\n $@" if($@);
101 }
102  
103 return 1;
104 }
105  
106  
107 sub read_buttonfile {
108 my $self = shift;
109 my $buttonfile = shift;
110  
111 my $classcontainer = $self->{classes};
112 my $buttoncontainer = $self->{buttons};
113  
114 my $buttonList = do $buttonfile;
115 die "Error reading buttonfile $buttonfile: $@" if($@);
116 die "Error reading buttonfile $buttonfile: $!" if($!);
117  
118 foreach my $button (@$buttonList) {
119 $self->replace_properties($button);
120 my $button_obj = Image::ButtonMaker::Button->new(@$button,
121 classcontainer => $classcontainer);
122 die "BUTTERR $Image::ButtonMaker::Button::errorstr" unless($button_obj);
123  
124 push @$buttoncontainer, $button_obj;
125 }
126 return 1;
127 }
128  
129  
130 sub clear_buttonlist {
131 my $self = shift;
132 $self->{buttons} = [];
133 return;
134 }
135  
136  
137 sub clear_classcontainer {
138 my $self = shift;
139 $self->{classes} = Image::ButtonMaker::ClassContainer->new;
140 return;
141 }
142  
143  
144 sub add_image_dir {
145 my $self = shift;
146 my $dir = shift;
147 die "add_image_dir: Dir $dir not found" unless(-d $dir);
148  
149 my $imagedirs = $self->{image_dirs};
150 push @$imagedirs, $dir;
151  
152 return;
153 }
154  
155  
156 sub get_image_dirs {
157 my $self = shift;
158 return (@{$self->{image_dirs}});
159 }
160  
161  
162 sub add_font_dir {
163 my $self = shift;
164 my $dir = shift;
165 die "add_font_dir: Dir $dir not found" unless(-d $dir);
166  
167 my $fontdirs = $self->{font_dirs};
168 push @$fontdirs, $dir;
169  
170 return;
171 }
172  
173  
174 sub get_font_dirs {
175 my $self = shift;
176 return (@{$self->{font_dirs}});
177 }
178  
179  
180 sub set_target_dir {
181 my $self = shift;
182 $self->{target_dir} = shift;
183 return;
184 }
185  
186 sub get_target_dir {
187 my $self = shift;
188 return $self->{target_dir};
189 }
190  
191  
192 sub set_lang_id {
193 my $self = shift;
194 $self->{lang_id} = shift;
195 return;
196 }
197  
198 sub get_lang_id {
199 my $self = shift;
200 return $self->{lang_id};
201 }
202  
203  
204 sub generate {
205 my $self = shift;
206 my $buttonlist = $self->{buttons};
207 my $target_dir = $self->{target_dir};
208
209 foreach my $but (@$buttonlist) {
210 my $img = $but->write($target_dir);
211 die "BUTERR: $Image::ButtonMaker::Button::errorstr"
212 unless($img);
213 #print "BUTFILENAME :",$but->lookup_filename, "\n"
214 }
215 return;
216 }
217  
218 sub find_font_file {
219 my $self = shift;
220 my $file = shift;
221 my $dirs = $self->{font_dirs};
222  
223 foreach my $d (@$dirs) {
224 return "$d/$file" if(-f "$d/$file");
225 }
226  
227 return undef;
228 }
229  
230 sub find_image_file {
231 my $self = shift;
232 my $file = shift;
233 my $dirs = $self->{image_dirs};
234 foreach my $d (@$dirs) {
235 return "$d/$file" if(-f "$d/$file");
236 }
237  
238 return undef;
239 }
240  
241 ############################################################
242 ## Private-ish methods
243  
244 sub reset_error {
245 my $self = shift;
246  
247 $self->{error} = 0;
248 $self->{errstr} = '';
249 return;
250 }
251  
252  
253 sub set_error {
254 my $self = shift;
255  
256 $self->{error} = shift;
257 $self->{errstr} = shift;
258 return;
259 }
260  
261 sub is_error {
262 my $self = shift;
263 return ($self->{error} != 0);
264 }
265  
266 #### Package Methods #######################################
267 sub find_file_in_dirs {
268 my $file = shift;
269 my $dirs = shift;
270  
271 foreach my $d (@$dirs) {
272 my $f = "$d/$file";
273 return $f if(-f $f);
274 }
275 return undef;
276 }
277  
278 1;
279 __END__
« ButtonMaker/ButtonClass.pm ↑ Summary ButtonMaker/Button.pm »