blib/lib/SVG/Grid.pm

filter: Statementstmt 93.5% 130 / 139 Branchbran 78.5% 11 / 14 Conditioncond 54.1% 33 / 61 MC/DCmcdc 16.6% 5 / 30 Subroutinesub 73.0% 19 / 26 Podpod 77.7% 7 / 9 totaltotal 73.4% 205 / 279 CC 32 SCAR 22.5
CC
32
Cov
81%
_get_defaults
26.6
grid
7.0
rectangle_link
5.8
CRAP 9.5
↑ Summary
1 package SVG::Grid;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings qw(FATAL utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use File::Slurper 'write_text';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Types::Standard qw/Any Int HashRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has cell_height =>
16 (
17 0 ▶ default => sub {return 40},
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 2 is => 'rw',
19 isa => Int,
20 required => 0,
21 );
22  
23 has cell_width =>
24 (
25 0 ▶ default => sub {return 40},
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 2 is => 'rw',
27 isa => Int,
28 required => 0,
29 );
30  
31 has colors =>
32 (
33 1 ▶ default => sub {return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 is => 'rw',
35 isa => HashRef,
36 required => 0,
37 );
38  
39 2 has height =>
40 (
41 is => 'rw',
42 isa => Int,
43 required => 0,
44 );
45  
46 has output_file_name =>
47 (
48 1 ▶ default => sub{return ''},
Subroutine __ANON__: called CC=1 SCAR=0.0
49 2 is => 'rw',
50 isa => Str,
51 required => 0,
52 );
53  
54 has style =>
55 (
56 1 ▶ default => sub {return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 2 is => 'rw',
58 isa => HashRef,
59 required => 0,
60 );
61  
62 2 has svg =>
63 (
64 is => 'rw',
65 isa => Any,
66 required => 0,
67 );
68  
69 has x_cell_count =>
70 (
71 0 ▶ default => sub {return 30},
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 2 is => 'rw',
73 isa => Int,
74 required => 0,
75 );
76  
77 has x_offset =>
78 (
79 0 ▶ default => sub {return 40},
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 2 is => 'rw',
81 isa => Int,
82 required => 0,
83 );
84  
85 2 has width =>
86 (
87 is => 'rw',
88 isa => Int,
89 required => 0,
90 );
91  
92 has y_cell_count =>
93 (
94 0 ▶ default => sub {return 30},
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 2 is => 'rw',
96 isa => Int,
97 required => 0,
98 );
99  
100 has y_offset =>
101 (
102 0 ▶ default => sub {return 40},
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 2 is => 'rw',
104 isa => Int,
105 required => 0,
106 );
107  
108 2 our $VERSION = '1.12';
109  
110 # ------------------------------------------------
111  
112 sub BUILD
113 1 ▶{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
114 1 my($self) = @_;
115  
116 1 $self -> colors
117 ({
118 black => 'rgb( 0, 0, 0)',
119 blue => 'rgb( 0, 0, 255)',
120 dimgray => 'rgb(105, 105, 105)',
121 indianred => 'rgb(205, 92, 92)',
122 red => 'rgb(255, 0, 0)',
123 silver => 'rgb(192, 192, 192)',
124 white => 'rgb(255, 255, 255)',
125 });
126 1 $self -> style
127 ({
128 'fill-opacity' => 0,
129 font => 'Arial',
130 'font-size' => 14,
131 'font-weight' => 'normal',
132 stroke => 'rgb(0, 0, 0)',
133 'stroke-width' => 1,
134 });
135 1 $self -> width
136 (
137 $self -> x_cell_count * $self -> cell_width
138 + 2 * $self -> x_offset
139 + 2 * $self -> cell_width
140 );
141 1 $self -> height
142 (
143 $self -> y_cell_count * $self -> cell_height
144 + 2 * $self -> y_offset
145 + 2 * $self -> cell_height
146 );
147 1 $self -> svg(SVG -> new(width => $self -> width, height => $self -> height) );
148  
149 } # End of BUILD.
150  
151 # ----------------------------------------------
152  
153 sub frame
154 1 ▶{
Subroutine frame: called CC=1 SCAR=0.0
Pod documented
155 1 my($self, %options) = @_;
156 1 my($frame_x) = [0, $self -> width - 1, $self -> width - 1, 0, 0];
157 1 my($frame_y) = [0, 0, $self -> height - 1, $self -> height - 1, 0];
158 1 my($points) = $self -> svg -> get_path
159 (
160 -type => 'polyline',
161 x => $frame_x,
162 y => $frame_y,
163 );
164 1 my($defaults) = $self -> _get_defaults(%options);
165 1 my($id) = 'frame_' . $$frame_x[2] . '_' . $$frame_y[2]; # Try to make it unique.
166  
167 $self -> svg -> polyline
168 (
169 %$points,
170 id => $id,
171 style =>
172 {
173 1 %{$self -> style},
174 'fill-opacity' => $$defaults{fill_opacity},
175 stroke => $$defaults{stroke},
176 'stroke-width' => $$defaults{stroke_width},
177 }
178 1 );
179  
180 } # End of frame.
181  
182 # ----------------------------------------------
183  
184 sub _get_defaults
185 18 ▶{
Subroutine _get_defaults: called CC=15 SCAR=32.8
186 18 my($self, %options) = @_;
187  
188 return
189 {
190 18 ▶ fill => $options{fill} || ${$self -> style}{fill} || 'rgb(205, 92, 92)', # Aka indianred.
Condition40%2/5
$options{'fill-opacity'} || do { $self->style }->{'fill-opacity'} || 0
l!l
018
$options{"fill-opacity"} or do { $self->style }->{"fill-opacity"}
l!l&&r!l&&!r
0018
MC/DC0%0/2
$options{'fill-opacity'} || do { $self->style }->{'fill-opacity'} || 0
$options{"fill-opacity"} do { $self->style }->{"fill-opacity"}
Truth table33%1/3
A or B
A= $options{"fill-opacity"}B= do { $self->style }->{"fill-opacity"}
ABresult
1X1
011
000
191 18 ▶ fill_opacity => $options{'fill-opacity'} || ${$self -> style}{'fill-opacity'} || 0,
Condition60%3/5
$options{'font-size'} || do { $self->style }->{'font-size'} || 14
l!l
180
$options{"font-size"} or do { $self->style }->{"font-size"}
l!l&&r!l&&!r
2160
MC/DC0%0/2
$options{'font-size'} || do { $self->style }->{'font-size'} || 14
$options{"font-size"} do { $self->style }->{"font-size"}
Truth table66%2/3
A or B
A= $options{"font-size"}B= do { $self->style }->{"font-size"}
ABresult
1X1
011
000
192 16 ▶ font_size => $options{'font-size'} || ${$self -> style}{'font-size'} || 14,
Condition70%7/10
$options{'font-weight'} || $options{'style'}{'font-weight'} || 'normal'
l!l
216
$options{"font-weight"} or $options{"style"}{"font-weight"}
l!l&&r!l&&!r
2016
$options{'stroke'} || do { $self->colors }->{'dimgray'} || 'rgb(105, 105, 105)'
l!l
180
$options{"stroke"} or do { $self->colors }->{"dimgray"}
l!l&&r!l&&!r
1440
MC/DC50%1/2
$options{'font-weight'} || $options{'style'}{'font-weight'} || 'normal'
$options{"font-weight"} $options{"style"}{"font-weight"}
MC/DC0%0/2
$options{'stroke'} || do { $self->colors }->{'dimgray'} || 'rgb(105, 105, 105)'
$options{"stroke"} do { $self->colors }->{"dimgray"}
Truth table66%2/3
A or B
A= $options{"font-weight"}B= $options{"style"}{"font-weight"}
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $options{"stroke"}B= do { $self->colors }->{"dimgray"}
ABresult
1X1
011
000
193 font_weight => $options{'font-weight'} || $options{style}{'font-weight'} || 'normal',
194 4 ▶ stroke => $options{stroke} || ${$self -> colors}{dimgray} || 'rgb(105, 105, 105)', # Aka dimgray.
Condition60%3/5
$options{'stroke-width'} || do { $self->style }->{'stroke-width'} || 1
l!l
180
$options{"stroke-width"} or do { $self->style }->{"stroke-width"}
l!l&&r!l&&!r
1170
MC/DC0%0/2
$options{'stroke-width'} || do { $self->style }->{'stroke-width'} || 1
$options{"stroke-width"} do { $self->style }->{"stroke-width"}
Truth table66%2/3
A or B
A= $options{"stroke-width"}B= do { $self->style }->{"stroke-width"}
ABresult
1X1
011
000
195 17 ▶ stroke_width => $options{'stroke-width'} || ${$self -> style}{'stroke-width'} || 1,
Condition40%2/5
$options{'text_color'} || do { $self->colors }->{'blue'} || 'rgb( 0, 0, 255)'
l!l
180
$options{"text_color"} or do { $self->colors }->{"blue"}
l!l&&r!l&&!r
0180
MC/DC0%0/2
$options{'text_color'} || do { $self->colors }->{'blue'} || 'rgb( 0, 0, 255)'
$options{"text_color"} do { $self->colors }->{"blue"}
Truth table33%1/3
A or B
A= $options{"text_color"}B= do { $self->colors }->{"blue"}
ABresult
1X1
011
000
196 18 ▶ text_color => $options{text_color} || ${$self -> colors}{blue} || 'rgb( 0, 0, 255)', # Aka blue.
Condition40%2/5
$options{'fill'} || do { $self->style }->{'fill'} || 'rgb(205, 92, 92)'
l!l
018
$options{"fill"} or do { $self->style }->{"fill"}
l!l&&r!l&&!r
0018
MC/DC0%0/2
$options{'fill'} || do { $self->style }->{'fill'} || 'rgb(205, 92, 92)'
$options{"fill"} do { $self->style }->{"fill"}
Truth table33%1/3
A or B
A= $options{"fill"}B= do { $self->style }->{"fill"}
ABresult
1X1
011
000
197 };
198  
199 } # End of _get_defaults.
200  
201 # ----------------------------------------------
202  
203 sub grid
204 1 ▶{
Subroutine grid: called CC=7 SCAR=19.5
Pod documented
205 1 my($self, %options) = @_;
206 1 my($count) = 0;
207 1 my($defaults) = $self -> _get_defaults(%options);
208 1 my($limit) = int( ($self -> width - 2 * $self -> x_offset) / $self -> cell_width);
209  
210 1 my(%opts);
211  
212 1 ▶ for (my $i = $self -> x_offset; $i <= ($self -> width - $self -> cell_width); $i += $self -> cell_width)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->width - $self->cell_width) 6 1 7
213 {
214 6 $count++;
215  
216 # Draw vertical lines.
217  
218 $self -> svg -> line
219 (
220 id => "grid_x_$i", # Try to make it unique.
221 x1 => $i,
222 y1 => $self -> cell_height,
223 x2 => $i,
224 y2 => $self -> height - $self -> y_offset - 1,
225 style =>
226 {
227 6 %{$self -> style},
228 stroke => $$defaults{stroke},
229 'stroke-width' => $$defaults{stroke_width},
230 }
231 6 );
232  
233 # This 'if' stops the x-axis labels appearing on top/bottom of the y-axis labels.
234  
235 6 ▶ if ( ($count > 1) && ($count < $limit) )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 1 and $count < $limit) 3 3 6
Condition100%3/3
$count > 1 and $count < $limit
!ll&&!rl&&r
123
MC/DC100%2/2
$count > 1 and $count < $limit
$count > 1 $count < $limit
Truth table100%3/3
A and B
A= $count > 1B= $count < $limit
ABresult
0X0
100
111
236 {
237 # Add x-axis labels across the top.
238  
239 3 %opts = ();
240 3 $opts{x} = $i + $$defaults{font_size};
241 3 $opts{y} = $self -> y_offset + 2 * $$defaults{font_size};
242 3 $opts{stroke} = $$defaults{text_color};
243 3 $opts{text} = $count - 1;
244  
245 3 $self -> text(%opts);
246  
247 # Add x-axis labels across the bottom.
248  
249 3 %opts = ();
250 3 $opts{x} = $i + $$defaults{font_size};
251 3 $opts{y} = $self -> height - $self -> y_offset - $$defaults{font_size};
252 3 $opts{stroke} = $$defaults{text_color};
253 3 $opts{text} = $count - 1;
254  
255 3 $self -> text(%opts);
256 }
257 }
258  
259 1 $count = 0;
260 1 $limit = int( ($self -> height - 2 * $self -> y_offset) / $self -> cell_height);
261  
262 1 ▶ for (my $i = $self -> y_offset; $i <= ($self -> height - $self -> cell_height); $i += $self -> cell_height)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->height - $self->cell_height) 6 1 7
263 {
264 6 $count++;
265  
266 # Draw horizontal lines.
267  
268 $self -> svg -> line
269 (
270 id => "grid_y_$i", # Try to make it unique.
271 x1 => $self -> x_offset,
272 y1 => $i,
273 x2 => $self -> width - $self -> x_offset - 1,
274 y2 => $i,
275 style =>
276 {
277 6 %{$self -> style},
278 stroke => $$defaults{stroke},
279 'stroke-width' => $$defaults{stroke_width},
280 }
281 6 );
282  
283 # This 'if' stops the y-axis labels appearing to the left/right of the x-axis labels.
284  
285 6 ▶ if ( ($count > 1) && ($count < $limit) )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 1 and $count < $limit) 3 3 6
Condition100%3/3
$count > 1 and $count < $limit
!ll&&!rl&&r
123
MC/DC100%2/2
$count > 1 and $count < $limit
$count > 1 $count < $limit
Truth table100%3/3
A and B
A= $count > 1B= $count < $limit
ABresult
0X0
100
111
286 {
287 # Add y-axis labels down the left.
288  
289 3 %opts = ();
290 3 $opts{x} = $self -> x_offset + $$defaults{font_size};
291 3 $opts{y} = $i + 2 * $$defaults{font_size};
292 3 $opts{stroke} = $$defaults{text_color};
293 3 $opts{text} = $count - 1;
294  
295 3 $self -> text(%opts);
296  
297 # Add y-axis labels down the right.
298  
299 3 %opts = ();
300 3 $opts{x} = $self -> width - $self -> x_offset - 2 * $$defaults{font_size};
301 3 $opts{y} = $i + 2 * $$defaults{font_size};
302 3 $opts{stroke} = $$defaults{text_color};
303 3 $opts{text} = $count - 1;
304  
305 3 $self -> text(%opts);
306 }
307 }
308  
309 } # End of grid.
310  
311 # ----------------------------------------------
312  
313 sub image_link
314 1 ▶{
Subroutine image_link: called CC=4 SCAR=15.0
Pod documented
315 1 my($self, %options) = @_;
316 1 my($image_id) = "image_$options{x}_$options{y}"; # Try to make it unique.
317 my(%anchor_options) =
318 (
319 -href => $options{href},
320 id => "anchor_$options{x}_$options{y}", # Try to make it unique.
321 1 ▶ -show => $options{show} || 'new',
Condition50%1/2
$options{'show'} || 'new'
l!l
01
MC/DC0%0/1
$options{'show'} || 'new'
$options{'show'}
Truth table50%1/2
A
A= $options{'show'}
Aresult
11
00
322 );
323 1 ▶ $anchor_options{-title} = $options{title} if ($options{title} && (length($options{title}) > 0) );
Branch: 1 branch
BranchTrueFalseTotal
if $options{"title"} and length $options{"title"} > 0 1 0 1
Condition33%1/3
$options{"title"} and length $options{"title"} > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$options{"title"} and length $options{"title"} > 0
$options{"title"} length $options{"title"} > 0
Truth table33%1/3
A and B
A= $options{"title"}B= length $options{"title"} > 0
ABresult
0X0
100
111
324  
325 $self -> svg -> anchor(%anchor_options) -> image
326 (
327 -href => $options{image},
328 id => $image_id,
329 width => $self -> cell_width,
330 height => $self -> cell_height,
331 x => $self -> x_offset + $self -> cell_width * $options{x},
332 y => $self -> y_offset + $self -> cell_height * $options{y},
333 1 );
334  
335 1 return $image_id;
336  
337 } # End of image_link.
338  
339 # ------------------------------------------------
340  
341 sub report
342 0 ▶{
Subroutine report: not called CC=1 SCAR=6.9
Pod undocumented
343 0 my($self) = @_;
344  
345 0 print sprintf("x_cell_count: %d. cell_width: %d. x_offset: %d. width: %d. \n",
346 $self -> x_cell_count, $self -> cell_width, $self -> x_offset, $self -> width);
347 0 print sprintf("y_cell_count: %d. cell_height: %d. y_offset: %d. height: %d. \n",
348 $self -> y_cell_count, $self -> cell_height, $self -> y_offset, $self -> height);
349  
350 } # End of report.
351  
352 # ----------------------------------------------
353  
354 sub rectangle_link
355 1 ▶{
Subroutine rectangle_link: called CC=5 SCAR=17.5
Pod documented
356 1 my($self, %options) = @_;
357 1 my($defaults) = $self -> _get_defaults(%options);
358 my(%anchor_options) =
359 (
360 -href => $options{href},
361 id => "anchor_$options{x}_$options{y}", # Try to make it unique.
362 1 ▶ -show => $options{show} || 'new',
Condition50%1/2
$options{'show'} || 'new'
l!l
01
MC/DC0%0/1
$options{'show'} || 'new'
$options{'show'}
Truth table50%1/2
A
A= $options{'show'}
Aresult
11
00
363 );
364 1 ▶ $anchor_options{-title} = $options{title} if ($options{title} && (length($options{title}) > 0) );
Branch: 1 branch
BranchTrueFalseTotal
if $options{"title"} and length $options{"title"} > 0 1 0 1
Condition33%1/3
$options{"title"} and length $options{"title"} > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$options{"title"} and length $options{"title"} > 0
$options{"title"} length $options{"title"} > 0
Truth table33%1/3
A and B
A= $options{"title"}B= length $options{"title"} > 0
ABresult
0X0
100
111
365 1 my($rectangle_id) = "rectangle_$options{x}_$options{y}"; # Try to make it unique.
366  
367 $self -> svg -> anchor(%anchor_options) -> rectangle
368 (
369 fill => $$defaults{fill},
370 'fill-opacity' => $$defaults{fill_opacity} || 0.5, # We use 0.5 since the default is 0.
371 id => $rectangle_id,
372 stroke => $$defaults{stroke},
373 'stroke-width' => $$defaults{stroke_width},
374 width => $self -> cell_width,
375 height => $self -> cell_height,
376 x => $self -> x_offset + $self -> cell_width * $options{x},
377 y => $self -> y_offset + $self -> cell_height * $options{y},
378 1 ▶ );
Condition50%1/2
$defaults->{'fill_opacity'} || 0.5
l!l
01
MC/DC0%0/1
$defaults->{'fill_opacity'} || 0.5
$defaults->{'fill_opacity'}
Truth table50%1/2
A
A= $defaults->{'fill_opacity'}
Aresult
11
00
379  
380 1 return $rectangle_id;
381  
382 } # End of rectangle_link.
383  
384 # ----------------------------------------------
385  
386 sub text
387 14 ▶{
Subroutine text: called CC=1 SCAR=0.0
Pod documented
388 14 my($self, %options) = @_;
389 14 my($defaults) = $self -> _get_defaults(%options);
390  
391 $self -> svg -> text
392 (
393 id => "note_$options{x}_$options{y}", # Try to make it unique.
394 x => $options{x},
395 y => $options{y},
396 style =>
397 {
398 14 %{$self -> style},
399 'fill-opacity' => $$defaults{fill_opacity},
400 'font-size' => $$defaults{font_size},
401 'font-weight' => $$defaults{font_weight},
402 stroke => $$defaults{stroke},
403 }
404 14 ) -> cdata($options{text});
405  
406 } # End of text.
407  
408 # ----------------------------------------------
409  
410 sub text_link
411 1 ▶{
Subroutine text_link: called CC=4 SCAR=14.5
Pod documented
412 1 my($self, %options) = @_;
413 1 my($defaults) = $self -> _get_defaults(%options);
414 1 my($half_font) = int($$defaults{font_size} / 2);
415 my(%anchor_options) =
416 (
417 -href => $options{href},
418 id => "anchor_$options{x}_$options{y}", # Try to make it unique.
419 1 ▶ -show => $options{show} || 'new',
Condition50%1/2
$options{'show'} || 'new'
l!l
01
MC/DC0%0/1
$options{'show'} || 'new'
$options{'show'}
Truth table50%1/2
A
A= $options{'show'}
Aresult
11
00
420 );
421 1 ▶ $anchor_options{-title} = $options{title} if ($options{title} && (length($options{title}) > 0) );
Branch: 1 branch
BranchTrueFalseTotal
if $options{"title"} and length $options{"title"} > 0 1 0 1
Condition33%1/3
$options{"title"} and length $options{"title"} > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$options{"title"} and length $options{"title"} > 0
$options{"title"} length $options{"title"} > 0
Truth table33%1/3
A and B
A= $options{"title"}B= length $options{"title"} > 0
ABresult
0X0
100
111
422 1 my($text_id) = "text_$options{x}_$options{y}"; # Try to make it unique.
423  
424 $self -> svg -> anchor(%anchor_options) -> text
425 (
426 id => $text_id,
427 x => $self -> x_offset + $self -> cell_width * $options{x} + $$defaults{font_size} - $half_font,
428 y => $self -> y_offset + $self -> cell_height * $options{y} + $$defaults{font_size} + $half_font,
429 style =>
430 {
431 1 %{$self -> style},
432 'fill-opacity' => $$defaults{fill_opacity},
433 'font-size' => $$defaults{font_size},
434 'font-weight' => $$defaults{font_weight},
435 stroke => $$defaults{stroke},
436 'stroke-width' => $$defaults{stroke_width},
437  
438 }
439 1 ) -> cdata($options{text});
440  
441 1 return $text_id;
442  
443 } # End of text_link.
444  
445 # ------------------------------------------------
446  
447 sub write
448 1 ▶{
Subroutine write: called CC=2 SCAR=7.6
Pod documented
449 1 my($self, %options) = @_;
450 1 ▶ my($file_name) = $options{output_file_name} || $self -> output_file_name;
Condition33%1/3
$options{'output_file_name'} || $self->output_file_name
l!l&&r!l&&!r
100
MC/DC0%0/2
$options{'output_file_name'} || $self->output_file_name
$options{'output_file_name'} $self->output_file_name
Truth table33%1/3
A || B
A= $options{'output_file_name'}B= $self->output_file_name
ABresult
1X1
011
000
451  
452 1 write_text($file_name, $self -> svg -> xmlify);
453  
454 } # End of write.
455  
456 # ------------------------------------------------
457  
458 2 1;
459  
460 =pod
461  
462 =encoding utf8
463  
464 =head1 NAME
465  
466 C<SVG::Grid> - Address SVG images using cells of $n1 x $n2 pixels
467  
468 =head1 Synopsis
469  
470 This is scripts/synopsis.pl:
471  
472 #!/usr/bin/env perl
473  
474 use strict;
475 use utf8;
476 use warnings;
477  
478 use SVG::Grid;
479  
480 # ------------
481  
482 my($cell_width) = 40;
483 my($cell_height) = 40;
484 my($x_cell_count) = 3;
485 my($y_cell_count) = 3;
486 my($x_offset) = 40;
487 my($y_offset) = 40;
488 my($svg) = SVG::Grid -> new
489 (
490 cell_width => $cell_width,
491 cell_height => $cell_height,
492 x_cell_count => $x_cell_count,
493 y_cell_count => $y_cell_count,
494 x_offset => $x_offset,
495 y_offset => $y_offset,
496 );
497  
498 $svg -> frame('stroke-width' => 3);
499 $svg -> text
500 (
501 'font-size' => 20,
502 'font-weight' => '400',
503 text => 'Front Garden',
504 x => $svg -> x_offset, # Pixel co-ord.
505 y => $svg -> y_offset / 2, # Pixel co-ord.
506 );
507 $svg -> text
508 (
509 'font-size' => 14,
510 'font-weight' => '400',
511 text => '--> N',
512 x => $svg -> width - 2 * $svg -> cell_width, # Pixel co-ord.
513 y => $svg -> y_offset / 2, # Pixel co-ord.
514 );
515 $svg -> grid(stroke => 'blue');
516 $svg -> image_link
517 (
518 href => 'http://savage.net.au/Flowers/Chorizema.cordatum.html',
519 image => 'http://savage.net.au/Flowers/images/Chorizema.cordatum.0.jpg',
520 show => 'new',
521 title => 'MouseOver® an image',
522 x => 1, # Cell co-ord.
523 y => 2, # Cell co-ord.
524 );
525 $svg -> rectangle_link
526 (
527 href => 'http://savage.net.au/Flowers/Alyogyne.huegelii.html',
528 show => 'new',
529 title => 'MouseOverâ„¢ a rectangle',
530 x => 2, # Cell co-ord.
531 y => 3, # Cell co-ord.
532 );
533 $svg -> text_link
534 (
535 href => 'http://savage.net.au/Flowers/Aquilegia.McKana.html',
536 stroke => 'rgb(255, 0, 0)',
537 show => 'new',
538 text => '3,1',
539 title => 'MouseOvér some text',
540 x => 3, # Cell co-ord.
541 y => 1, # Cell co-ord.
542 );
543 $svg -> write(output_file_name => 'data/synopsis.svg');
544  
545 Output: L<http://savage.net.au/assets/images/articles/synopsis.svg>
546  
547 See also scripts/*.pl.
548  
549 =head1 Description
550  
551 C<SVG::Grid> allows you to I<mostly> use cell co-ordinates (like a spreadsheet) to place items on
552 an L<SVG> image. These co-ordinates are in the form (x, y) = (integer, integer), where x and y
553 refer to the position of a cell within a row and a column. You define these rows and columns when
554 you call the L</new(%options)> method. Cell co-ordinates are numbered 1 .. N.
555  
556 Here, I<mostly> means all method calls except adding text via the L</text(%options)]> method. With
557 C<text()>, you use pixels locations so that the text can be placed anywhere. Pixel co-ordinates are
558 numbered 0 .. N.
559  
560 Note: Objects of type C<SVG::Grid> are not daughters of L<SVG>. They are stand-alone objects.
561  
562 =head1 Distributions
563  
564 This module is available as a Unix-style distro (*.tgz).
565  
566 See L<http://savage.net.au/Perl-modules/html/installing-a-module.html>
567 for help on unpacking and installing distros.
568  
569 =head1 Installation
570  
571 Install L<SVG::Grid> as you would any C<Perl> module:
572  
573 Run:
574  
575 cpanm SVG::Grid
576  
577 or run:
578  
579 sudo cpan SVG::Grid
580  
581 And then:
582  
583 perl Makefile.PL
584 make (or dmake or nmake)
585 make test
586 make install
587  
588 =head1 Constructor and Initialization
589  
590 C<new()> is called as C<< my($svg) = SVG::Grid -> new(k1 => v1, k2 => v2, ...) >>.
591  
592 It returns a new object of type C<SVG::Grid>.
593  
594 Key-value pairs accepted in the parameter list (see corresponding methods for details
595 [e.g. L</text(%options)>]:
596  
597 =over 4
598  
599 =item o cell_height => $integer
600  
601 The height of each cell, in pixels.
602  
603 Default: 40.
604  
605 =item o cell_width => $integer
606  
607 The width of each cell, in pixels.
608  
609 Default: 40.
610  
611 =item o colors => $hashref
612  
613 The set of default colors, so you don't have to provide a C<color> parameter to various methods.
614  
615 It also means you can refer to colors by their names, rather than the awkward C<'rgb($R, $G, $B)'>
616 structures that the L<SVG> module uses.
617  
618 Default:
619  
620 $self -> colors
621 ({
622 black => 'rgb( 0, 0, 0)',
623 blue => 'rgb( 0, 0, 255)',
624 dimgray => 'rgb(105, 105, 105)',
625 indianred => 'rgb(205, 92, 92)',
626 red => 'rgb(255, 0, 0)',
627 silver => 'rgb(192, 192, 192)',
628 white => 'rgb(255, 255, 255)',
629 });
630  
631 =item o output_file_name =>
632  
633 The name of the SVG file to write, if the L</write(%options)> method is called.
634  
635 Default: ''.
636  
637 =item o style => $hashref
638  
639 The default style to use, so you don't have to provide a C<style> parameter to various methods.
640  
641 Default:
642  
643 $self -> style
644 ({
645 'fill-opacity' => 0,
646 font => 'Arial',
647 'font-size' => 14,
648 'font-weight' => 'normal',
649 stroke => 'rgb(0, 0, 0)',
650 'stroke-width' => 1,
651 });
652  
653 =item o x_cell_count => $integer
654  
655 The number of cells aross the SVG.
656  
657 Each cell will be C<cell_width> pixels wide.
658  
659 Default: 30.
660  
661 =item o x_offset => $integer
662  
663 The distance between the left and right sides of the SVG and the co-ordinate grid, in pixels.
664  
665 Default: 40.
666  
667 =item o y_cell_count => $integer
668  
669 The number of cells down the SVG.
670  
671 Each cell will be C<cell_height> pixels high.
672  
673 Default: 30.
674  
675 =item o y_offset => $integer
676  
677 The distance between the top and bottom sides of the SVG and the co-ordinate grid, in pixels.
678  
679 =back
680  
681 =head1 Methods
682  
683 =head2 cell_height()
684  
685 Gets the height of each cell, in pixels.
686  
687 C<cell_height> is a parameter to L</new()>.
688  
689 =head2 cell_width()
690  
691 Gets the width of each cell, in pixels.
692  
693 C<cell_width> is a parameter to L</new()>.
694  
695 =head2 colors([$hashref])
696  
697 Here, [] indicates an optional parameter.
698  
699 Gets or sets the default hashref of colors.
700  
701 C<colors> is a parameter to L</new()>.
702  
703 =head2 frame([%options])
704  
705 Draws the frame.
706  
707 This method uses these keys in C<%options>:
708  
709 =over 4
710  
711 =item o fill-opacity
712  
713 Default: 0.
714  
715 =item o stroke
716  
717 Default: 'rgb(105, 105, 105)' aka dimgray.
718  
719 =item o stroke-width
720  
721 Default: 1.
722  
723 =back
724  
725 =head2 grid(%options)
726  
727 Draws a grid onto the SVG.
728  
729 This method uses these keys in C<%options>:
730  
731 =over 4
732  
733 =item o font-size
734  
735 Default: 14.
736  
737 =item o stroke
738  
739 Default: 'rgb(105, 105, 105)' aka dimgray.
740  
741 =item o stroke-width
742  
743 Default: 1;
744  
745 =item o text_color
746  
747 Default: 'rgb(0, 0, 255)' aka blue.
748  
749 =back
750  
751 =head2 height()
752  
753 Returns the calculated height, in pixels, of the SVG.
754  
755 =head2 image_link(%options)
756  
757 Places an image onto the SVG and makes it clickable.
758  
759 This method uses these keys in C<%options>:
760  
761 =over 4
762  
763 =item o href => $url
764  
765 This is the link you are taken to if you click in the specified C<image>. Sample:
766  
767 href => 'http://savage.net.au/Flowers/Chorizema.cordatum.html'
768  
769 =item o image => $url
770  
771 This is the image which appears on the SVG, and which is made clickable. Sample:
772  
773 image => 'http://savage.net.au/Flowers/images/Chorizema.cordatum.0.jpg'
774  
775 =item o show => $string
776  
777 For $string you must choose one of the SVG specification values: embed|new|none|other|replace.
778  
779 L<The SVG specification for Behavior Attributes|https://www.w3.org/TR/xlink/#link-behaviors>.
780  
781 Note: The parameter passed to L<SVG> is actually called C<-show>.
782  
783 Default: 'new'
784  
785 'new' is similar to the effect achieved by the following HTML fragment:
786  
787 <A HREF="http://www.example.org" target="_blank">...</A>
788  
789 =item o title => $string.
790  
791 This string, if not empty, is passed to L<SVG> as the value of the C<-title> parameter.
792  
793 The effect is to activate a tooltip when you MouseOver the image.
794  
795 =item o x => $integer
796  
797 This is the cell # across the SVG.
798  
799 Cell co-ordinates are numbered 1 .. N.
800  
801 =item o y => $integer
802  
803 This is the cell # down the SVG.
804  
805 Cell co-ordinates are numbered 1 .. N.
806  
807 =back
808  
809 =head2 output_file_name($string)
810  
811 Here, [] indicates an optional parameter.
812  
813 Gets or sets the name of the output file.
814  
815 C<output_file_name> is a parameter to L</new()>.
816  
817 =head2 rectangle_link(%options)
818  
819 Places a rectangle (which fills a cell) onto the SVG and makes it clickable.
820  
821 This method uses these keys in C<%options>:
822  
823 =over 4
824  
825 =item o fill
826  
827 Default: 'rgb(205, 92, 92)' aka indianred.
828  
829 =item o fill-opacity
830  
831 Default: 0.5.
832  
833 =item o href => $url
834  
835 This is the link you are taken to if you click in the rectangle specified by (x, y). Sample:
836  
837 href => 'http://savage.net.au/Flowers/Alyogyne.huegelii.html'
838  
839 =item o stroke
840  
841 Default: 'rgb(105, 105, 105)' aka dimgray.
842  
843 =item o show => $string
844  
845 For $string you must choose one of the SVG specification values: embed|new|none|other|replace.
846  
847 L<The SVG specification for Behavior Attributes|https://www.w3.org/TR/xlink/#link-behaviors>.
848  
849 Note: The parameter passed to L<SVG> is actually called C<-show>.
850  
851 Default: 'new'
852  
853 'new' is similar to the effect achieved by the following HTML fragment:
854  
855 <A HREF="http://www.example.org" target="_blank">...</A>
856  
857 =item o title => $string.
858  
859 This string, if not empty, is passed to L<SVG> as the value of the C<-title> parameter.
860  
861 The effect is to activate a tooltip when you MouseOver the rectangle.
862  
863 =item o x => $integer
864  
865 This is the cell # across the SVG.
866  
867 Cell co-ordinates are numbered 1 .. N.
868  
869 =item o y => $integer
870  
871 This is the cell # down the SVG.
872  
873 Cell co-ordinates are numbered 1 .. N.
874  
875 =back
876  
877 =head2 style([$hashref])
878  
879 Here, [] indicates an optional parameter.
880  
881 Gets or sets the default hashref of styles.
882  
883 C<style> is a parameter to L</new()>.
884  
885 =head2 svg()
886  
887 Returns the internal L<SVG> object.
888  
889 =head2 text(%options)
890  
891 Places a text string onto the SVG.
892  
893 Warning: This method uses (x, y) in pixels.
894  
895 This method uses these keys in C<%options>:
896  
897 =over 4
898  
899 =item o fill-opacity
900  
901 Default: 0.
902  
903 =item o font-size
904  
905 Default: 14.
906  
907 =item o font-weight
908  
909 Default: 'normal'.
910  
911 =item o stroke
912  
913 Default: 'rgb(105, 105, 105)' aka dimgray.
914  
915 =item o x => $integer
916  
917 This is the pixel # across the SVG.
918  
919 Pixel co-ordinates are numbered 0 .. N.
920  
921 =item o y => $integer
922  
923 This is the pixel # down the SVG.
924  
925 Pixel co-ordinates are numbered 0 .. N.
926  
927 =back
928  
929 =head2 text_link(%options)
930  
931 Places a text string onto the SVG and makes it clickable.
932  
933 The clickable area is just the text. The remainer of the cell does not respond to the click.
934  
935 This method uses these keys in C<%options>:
936  
937 =over 4
938  
939 =item o fill-opacity
940  
941 Default: 0.
942  
943 =item o font-size
944  
945 Default: 14.
946  
947 =item o font-weight
948  
949 Default: 'normal'.
950  
951 =item o href => $url
952  
953 This is the link you are taken to if you click in the C<text> in the cell specified by (x, y).
954 Sample:
955  
956 href => 'http://savage.net.au/Flowers/Aquilegia.McKana.html'
957  
958 =item o stroke
959  
960 Default: 'rgb(105, 105, 105)' aka dimgray.
961  
962 =item o stroke-width
963  
964 Default: 1.
965  
966 =item o show => $string
967  
968 For $string you must choose one of the SVG specification values: embed|new|none|other|replace.
969  
970 L<The SVG specification for Behavior Attributes|https://www.w3.org/TR/xlink/#link-behaviors>.
971  
972 Note: The parameter passed to L<SVG> is actually called C<-show>.
973  
974 Default: 'new'
975  
976 'new' is similar to the effect achieved by the following HTML fragment:
977  
978 <A HREF="http://www.example.org" target="_blank">...</A>
979  
980 =item o text => $string
981  
982 This is the text which will be written into the cell and made clickable.
983  
984 =item o title => $string.
985  
986 This string, if not empty, is passed to L<SVG> as the value of the C<-title> parameter.
987  
988 The effect is to activate a tooltip when you MouseOver the rectangle.
989  
990 =item o x => $integer
991  
992 This is the cell # across the SVG.
993  
994 Cell co-ordinates are numbered 1 .. N.
995  
996 =item o y => $integer
997  
998 This is the cell # down the SVG.
999  
1000 Cell co-ordinates are numbered 1 .. N.
1001  
1002 =back
1003  
1004 =head2 width()
1005  
1006 Returns the calculated width, in pixels, of the SVG.
1007  
1008 =head2 write(%options)
1009  
1010 Writes the SVG to the file name passed to L</new(%options)> or passed to C<write()>. The latter
1011 value has priority.
1012  
1013 =head2 x_cell_count()
1014  
1015 Gets the count of cells horizontally.
1016  
1017 C<x_cell_count> is a parameter to L</new()>.
1018  
1019 =head2 x_offset()
1020  
1021 Gets the horizontal gap between the edges of the SVG and the grid.
1022  
1023 C<x_offset> is a parameter to L</new()>.
1024  
1025 =head2 y_cell_count()
1026  
1027 Gets the count of cells vertically.
1028  
1029 C<y_cell_count> is a parameter to L</new()>.
1030  
1031 =head2 y_offset()
1032  
1033 Gets the vertical gap between the edges of the SVG and the grid.
1034  
1035 C<y_offset> is a parameter to L</new()>.
1036  
1037 =head1 FAQ
1038  
1039 =head2 Does this module support Unicode?
1040  
1041 Yes. The L</write(%options)> method uses an encoding of UTF-8 on the output file handle.
1042  
1043 Note: To use Unicode, you must include 'use utf8;' in your programs. See scripts/synopsis.pl.
1044  
1045 =head2 Does this module support tootips via MouseOver?
1046  
1047 Yes. Just search this document for 'MouseOver'.
1048  
1049 =head2 Does this module use the SVG 'g' element?
1050  
1051 No. This means there is no grouping done by default. Nevertheless, you can call L</svg()> to get
1052 the internal SVG object, and use 'g' yourself at any time.
1053  
1054 See L<https://www.w3.org/TR/SVG11/struct.html#Groups> for details of the 'g' element.
1055  
1056 =head2 How does this module handle duplicate element ids?
1057  
1058 By using method parameters to generate a hopefully-unique id. This line copied from the
1059 L</image_link(%options)> method shows the general technique I've used:
1060  
1061 id => "image_$options{x}_$options{y}", # Try to make it unique.
1062  
1063 =head2 Is there any difference between C<fill> and C<stroke> for text?
1064  
1065 I don't think so, but I have had some odd results. Ultimately, you need to read the docs for the
1066 L<SVG> module to see what it expects.
1067  
1068 =head2 Is there any way to hide the coordinate numbering system?
1069  
1070 Not in V 1.00. However, it is on the TODO list.
1071  
1072 =head1 See Also
1073  
1074 L<GD>
1075  
1076 L<Imager>
1077  
1078 L<Image::Magick>
1079  
1080 L<Image::Magick::Chart>
1081  
1082 L<Image::Magick::PolyText>
1083  
1084 L<Image::Magick::Tiler>
1085  
1086 L<SVG>
1087  
1088 L<https://www.w3.org/Graphics/SVG/>
1089  
1090 =head1 Machine-Readable Change Log
1091  
1092 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
1093  
1094 =head1 Version Numbers
1095  
1096 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
1097  
1098 =head1 Repository
1099  
1100 L<https://github.com/ronsavage/SVG-Grid>
1101  
1102 =head1 Support
1103  
1104 Email the author, or log a bug on RT:
1105  
1106 L<https://rt.cpan.org/Public/Dist/Display.html?Name=SVG::Grid>.
1107  
1108 =head1 Author
1109  
1110 L<SVG::Grid> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2016.
1111  
1112 My homepage: L<http://savage.net.au/>
1113  
1114 =head1 Copyright and Licence
1115  
1116 Australian copyright (c) 2016, Ron Savage.
1117  
1118 All Programs of mine are 'OSI Certified Open Source Software';
1119 you can redistribute them and/or modify them under the terms of
1120 The Perl License, a copy of which is available at:
1121 http://dev.perl.org/licenses/
1122  
1123 =cut
↑ Summary