Coordinates.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« fakenum.pl ↑ Summary
1 package Math::Grid::Coordinates;
2  
3 0 ▶use Mojo::Util qw/dumper/;
Subroutine BEGIN: called CC=1 SCAR=2.6
4  
5 use Moo;
6 use Scalar::Util qw/looks_like_number/;
7 use List::MoreUtils qw/uniq/;
8 use Carp;
9  
10 my $NUM = sub { local $_ = shift; !$_ || (looks_like_number($_) && $_ >= 0) };
11  
12 has [ qw/grid_width grid_height/ ] => ( is => 'rwp', isa => sub { local $_ = shift; looks_like_number($_) && int($_) == $_ && $_ > 0 } );
13  
14 has [ qw/page_width page_height/ ] => ( is => 'rwp', isa => $NUM );
15  
16 # item size
17  
18 has [ qw/item_width item_height/ ] => ( is => 'rwp', isa => $NUM );
19  
20 has [ qw/gutter_h gutter_v border_l border_r border_t border_b/ ] => ( is => 'rwp', isa => $NUM );
21  
22 has "gutter" => (is => 'rwp', isa => $NUM, trigger => sub {
23 my ($self, $g) = @_;
24 if (defined $g) { for (qw/gutter_h gutter_v/) { my $set = "_set_$_"; $self->$set($g) } }
25 if ($self->gutter_v != $self->gutter_h) { carp "Multiple gutters"; return undef }
26 });
27  
28 has "border" => (is => 'rwp',
29 isa => $NUM,
30 trigger => sub {
31 my ($self, $b) = @_;
32 if (defined $b) { for (qw/border_l border_r border_t border_b/) { my $set = "_set_$_"; $self->$set($b) } }
33 if (1 < scalar uniq map { $self->$_ } qw/border_l border_r border_t border_b/) {
34 carp "Multiple borders"; return undef
35 }
36 });
37  
38  
39 has arrange => ( is => 'rw', isa => sub { !$_ || /^h$/i || /^v$/i } , default => sub { 'h' } );
40  
41 # grid and page sizes
42 # total items and sizes
43 # gutter and border as percentage
44  
45 # 10pw
46 # 10iw
47  
48 around BUILDARGS => sub {
49 my $orig = shift;
50 my $class = shift;
51 my $opts;
52  
53 if ( !ref $_[0] ) {
54 return $class->build_simple($orig, @_);
55 }
56 elsif ($_[0]->{page_height} && $_[0]->{page_width}) {
57 return $class->build_from_page($orig, @_);
58 }
59 else {
60 return $class->build_from_grid($orig, @_);
61 }
62 };
63  
64 sub build_simple {
65 my $class = shift;
66 my $orig = shift;
67  
68 my %h;
69 my $mode = "grid";
70 if ($_[0] eq 'page' || $_[0] eq 'grid') { $mode = shift @_ }
71 @h{$mode . "_width", $mode . "_height", qw/item_width item_height gutter border/} = grep { /[^a-z]/i } @_;
72 ($h{arrange}) = (grep { /^[h,v]i/ } @_) || ('h');
73  
74 my $opts = \%h;
75  
76 $opts = _handle_percentages($opts);
77 $opts = _handle_shortcuts($opts);
78  
79 my $obj = $class->$orig($opts);
80 return $obj;
81 }
82  
83 sub _handle_percentages {
84 my $opts = shift;
85 for (grep { (/^gutter/ || /^border/) && ($opts->{$_} || "") =~ /(\d+)([pi])([wh])$/ } keys $opts->%*) {
86 $opts->{$_} =~ /(\d+)([pi])([wh])$/;
87 my $ref = join "_", ($2 eq "p" ? "page" : "item"), ($3 eq "w" ? "width" : "height");
88 croak "$ref not defined yet: cannot calculate $_" unless defined $opts->{$ref};
89 $opts->{$_} = ($1 / 100) * $opts->{$ref};
90 }
91 return $opts;
92 }
93  
94 sub _handle_shortcuts {
95 my $opts = shift;
96  
97 for (qw/border_l border_r border_t border_b/) { $opts->{$_} //= $opts->{border} }
98 for (qw/gutter_h gutter_v/) { $opts->{$_} //= $opts->{gutter} }
99 for (qw/gutter gutter_h gutter_v border border_l border_r border_t border_b/) { $opts->{$_} //= 0 }
100  
101 return $opts;
102 }
103  
104 sub _handle_conflicts {
105 my $opts = shift;
106 croak "cannot set both page_width and item_width at grid creation stage" if $opts->{page_width} && $opts->{item_width};
107 croak "cannot set both page_height and item_height at grid creation stage" if $opts->{page_height} && $opts->{item_height};
108 }
109  
110 sub build_from_grid {
111 my $class = shift;
112 my $orig = shift;
113 my $opts = shift;
114  
115 $opts = _handle_percentages($opts);
116 $opts = _handle_shortcuts($opts);
117  
118 $opts->{page_height} = ($opts->{border_t} + $opts->{gutter_v} * ($opts->{grid_height} - 1) + $opts->{item_height} * $opts->{grid_height} + $opts->{border_b});
119 $opts->{page_width} = ($opts->{border_l} + $opts->{gutter_h} * ($opts->{grid_width} - 1) + $opts->{item_width} * $opts->{grid_width} + $opts->{border_r});
120
121 return $class->$orig($opts);
122 }
123  
124 sub build_from_page {
125 my $class = shift;
126 my $orig = shift;
127 my $opts = shift;
128  
129 $opts = _handle_percentages($opts);
130 $opts = _handle_shortcuts($opts);
131  
132 my $avail_v = $opts->{page_height} - ($opts->{border_t} + $opts->{gutter_v} * ($opts->{grid_height} - 1) + $opts->{border_b});
133 my $avail_h = $opts->{page_width} - ($opts->{border_l} + $opts->{gutter_h} * ($opts->{grid_width} - 1) + $opts->{border_r});
134  
135 # print $avail_h, $avail_v;
136  
137 $opts->{item_width} = $avail_h / $opts->{grid_width};
138 $opts->{item_height} = $avail_v / $opts->{grid_height};
139  
140 return $class->$orig($opts);
141 }
142  
143  
144 around [ qw/page_width page_height/ ] => sub {
145 my $orig = shift;
146 my $self = shift;
147 croak "Page size can only be set at grid creation" if @_;
148 return $orig->($self, @_);
149 };
150  
151  
152 sub total_height {
153 my $self = shift;
154 return $self->border_t + $self->item_height * $self->grid_height + $self->gutter_v * ($self->grid_height - 1) + $self->border_b;
155 };
156  
157 sub total_width {
158 my $self = shift;
159 return $self->border_l + $self->item_width * $self->grid_width + $self->gutter_h * ($self->grid_width - 1) + $self->border_r
160 };
161  
162 sub bbox {
163 my $self = shift;
164 my ($w, $h) = ($self->total_width, $self->total_height);
165 return wantarray ? ($w, $h) : [ $w, $h ];
166 }
167  
168 sub sequence {
169 my $self = shift;
170 my ($gw, $gh) = map { $self->$_ } qw/grid_width grid_height/;
171 my @sequence;
172  
173 if (lc($self->arrange) eq 'v') {
174 for my $x (0..$gw-1) { for my $y (0..$gh-1) { push @sequence, [$x, $y] } }
175 } else {
176 for my $y (0..$gh-1) { for my $x (0..$gw-1) { push @sequence, [$x, $y] } }
177 }
178 return @sequence;
179 }
180  
181 sub position {
182 my $self = shift;
183  
184 my ($x, $y) = @_;
185 my ($iw, $ih, $gt_h, $gt_v, $bl, $bt) = map { $self->$_ } qw/item_width item_height gutter_h gutter_v border_l border_t/;
186 # print ($x, $y, $iw, $ih, $gt_h, $gt_v, $bl, $bt);
187 return (
188 ($bl + $iw * $x + $gt_h * $x),
189 ($bt + $ih * $y + $gt_v * $y)
190 )
191 }
192  
193 sub positions {
194 my $self = shift;
195  
196 # first assign positions in terms of page coordinates
197 my @grid = $self->sequence;
198  
199 # then calculate and assign the actual position
200 my @pos = map {
201 [ $self->position(@$_) ]
202 } @grid;
203  
204 return @pos;
205 }
206  
207 sub block {
208 my $self = shift;
209 my ($x, $y, $w, $h) = @_;
210  
211 my ($iw, $ih, $gt_h, $gt_v, $bl, $bt) = map { $self->$_ } qw/item_width item_height gutter_h gutter_v border_l border_t/;
212  
213 my ($x_pos, $y_pos) = (
214 $bl + $iw * $x + $gt_h * $x,
215 $bt + $ih * $y + $gt_v * $y,
216 );
217  
218 my ($width, $height) = (
219 $iw * $w + $gt_h * ($w - 1),
220 $ih * $h + $gt_v * ($h - 1),
221 );
222  
223 return ($x_pos, $y_pos, $width, $height)
224 }
225  
226 sub guides {
227 my $self = shift;
228 my @guides;
229 my ($h, $w, $ih, $iw) = map { $self->$_ } qw/page_height page_width item_height item_width/;
230  
231 for (0..$self->grid_width-1) {
232 my $p = [ $self->position($_, 0) ]->[0];
233 push @guides, [ [ $p, 0 ], [ $p, $h ] ];
234 push @guides, [ [ $p + $iw, 0 ], [ $p + $iw, $h ] ];
235 }
236 for (0..$self->grid_height-1) {
237 my $p = [ $self->position(0, $_) ]->[1];
238 push @guides, [ [ 0, $p ], [ $w, $p ] ];
239 push @guides, [ [ 0, $p + $ih ], [ $w, $p + $ih ] ];
240 }
241 return @guides;
242 }
243  
244 sub marks {
245 my $self = shift;
246 my $l = shift || 12;
247  
248 my @marks = $self->guides;
249  
250 for my $m (@marks) {
251 if ($m->[0][0] == $m->[1][0]) {
252 $m->[0][1] -= $l;
253 $m->[1][1] += $l;
254 }
255 elsif ($m->[0][1] == $m->[1][1]) {
256 $m->[0][0] -= $l;
257 $m->[1][0] += $l;
258 }
259 }
260 return @marks
261 }
262  
263  
264 sub calculate {
265 my $self = shift;
266  
267 my $avail_v = $self->page_height - ($self->border_t + $self->gutter_v * ($self->grid_height - 1) + $self->border_b);
268 my $avail_h = $self->page_width - ($self->border_l + $self->gutter_h * ($self->grid_width - 1) + $self->border_r);
269  
270 $self->item_width($avail_h / $self->grid_width);
271 $self->item_height($avail_v / $self->grid_height);
272  
273 return $self;
274 }
275  
276  
277 sub numbers {
278 my $self = shift;
279 return (1..($self->grid_width * $self->grid_height))
280 }
281  
282 sub repage {
283 my $self = shift;
284 my $opts = shift;
285 my $prev = $self->to_hash;
286  
287 for (grep { !(defined $opts->{$_}) } keys $opts->%*) { delete $prev->{$_} }
288 for (grep { (defined $opts->{$_}) } keys $opts->%*) { $prev->{$_} = $opts->{$_} }
289  
290 my $clone = $self = Math::Grid::Coordinates->new($prev);
291 for (qw/item_height border_r gutter grid_width border_t border_l page_width border page_height gutter_v grid_height gutter_h border_b item_width/) {
292 my $setter = "_set_$_";
293 $self->$setter($clone->$_)
294 }
295 $self->arrange($clone->arrange);
296 return $self;
297 }
298  
299  
300 sub to_hash {
301 my $self = shift;
302 my @keys = qw/grid_width grid_height border_r border_b border_t border_l gutter_v gutter_h item_height item_width page_height page_width arrange/;
303 return { map { $_ =>($self->$_ || 0) } @keys };
304 return { $self->%* };
305 }
306  
307  
308 1;
309  
310 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
311  
312 =head1 NAME
313  
314 Math::Grid::Coordinates - create geometric grids
315  
316 =head1 SYNOPSYS
317  
318 use Math::Grid::Coordinates;
319  
320 my $grid = Math::Grid::Coordinates->new($grid_width, $grid_height, $item_width, $item_height, $gutter, $border, $arrangement);
321  
322 =head1 DESCRIPTION
323  
324 Math::Grid::Coordinates creates an array of x-y positions for items of a given height and width arranged in a grid. This is used to create grid layouts on a page, or repeate items on a number of pages of the same size.
325  
326 =head1 REQUIRES
327  
328 L<Moo>
329  
330 =head1 INITIALIZING THE GRID
331  
332 The grid can be initialized by setting page size, the number of the items and the gutters and borders, like so:
333  
334 my $grid = Math::Grid::Coordinates->new({ page_width => 210, page_height => 297, grid_width => 2, grid_height => 3, gutter => 6, border => 12 });
335  
336 in which case the item size is calculated.
337  
338 It can also be initialized by setting item size, the number of the items and the gutters and borders, like so:
339  
340 my $grid = Math::Grid::Coordinates->new({ item_width => 210, item_height => 297, grid_width => 2, grid_height => 3, gutter => 6, border => 12 });
341  
342 Gutters and borders can be percentages of page width or height like so:
343  
344 my $grid = Math::Grid::Coordinates->new({ item_width => 210, item_height => 297, grid_width => 2, grid_height => 3, gutter => 2pw, border => 4pw });
345  
346  
347 =head1 METHODS
348  
349 =head2 bbox
350  
351 $grid->bbox();
352  
353 Returns the total bounding box of the grid
354  
355 =head2 numbers
356  
357 $grid->numbers();
358  
359 Returns the sequence item numbers, with the top left item as item 1.
360  
361 +---------+---------+---------+---------+
362 | | | | |
363 | 1 | 2 | 3 | 4 |
364 | | | | |
365 +---------+---------+---------+---------+
366 | | | | |
367 | 5 | 6 | 7 | 8 |
368 | | | | |
369 +---------+---------+---------+---------+
370 | | | | |
371 | 9 | 10 | 11 | 12 |
372 | | | | |
373 +---------+---------+---------+---------+
374  
375 =head2 sequence
376  
377 $grid->sequence();
378  
379 Returns the sequence of x-y grid item coordinates, with the top left item as item C<[0, 0]>, the next one (assuming a horizontal arrangement) being C<[1, 0]> etc.
380  
381 +---------+---------+---------+---------+
382 | | | | |
383 | [0, 0] | [0, 1] | [0, 2] | [0, 3] |
384 | | | | |
385 +---------+---------+---------+---------+
386 | | | | |
387 | [1, 0] | [1, 1] | [1, 2] | [1, 3] |
388 | | | | |
389 +---------+---------+---------+---------+
390 | | | | |
391 | [2, 0] | [2, 1] | [2, 2] | [2, 3] |
392 | | | | |
393 +---------+---------+---------+---------+
394  
395 =head2 position
396  
397 $grid->position(0, 0);
398  
399 Returns the position of item as an array of x and y coordinates.
400  
401 =head2 block
402  
403 $grid->block($x, $y, $width, $height);
404  
405 Returns the position and size of item as an array of x and y coordinates, and width and height.
406  
407 =head2 positions
408  
409 $grid->positions();
410  
411 Returns the sequence of x-y grid coordinates.
412  
413 =head2 total_height
414  
415 $grid->total_height();
416  
417 The total height of the grid
418  
419 =head2 total_width
420  
421 $grid->total_width();
422  
423 The total width of the grid
424  
425 =head2 calculate
426  
427 $grid->calculate();
428  
429 Calculates item width and height based on page size, borders, gutters and item count
430  
431 =head2 guides
432  
433 $grid->guides();
434  
435 Returns start and end coordinates of layout guides
436  
437 =head2 marks
438  
439 $grid->marks($length);
440  
441 Returns start and end coordinates of layout marks (short lines outside page)
442  
443 =head1 To do
444  
445 =over 4
446  
447 =item *
448  
449 Allow for bottom or top start of grid
450  
451 =back
452  
453 =cut
« fakenum.pl ↑ Summary