]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Pattern/Dots.pm
Enforce the non-negativity of some attributes with anonymous types
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Mod / Pattern / Dots.pm
1 package LaTeX::TikZ::Mod::Pattern::Dots;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Mod::Pattern::Dots - A dotted pattern modifier.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Sub::Name ();
19
20 use Any::Moose;
21 use Any::Moose 'Util::TypeConstraints';
22
23 extends 'LaTeX::TikZ::Mod::Pattern';
24
25 has 'dot_width' => (
26  is      => 'ro',
27  isa     => subtype('Num' => where { LaTeX::TikZ::Tools::numcmp($_, 0) >= 0 }),
28  default => 1,
29 );
30
31 has 'space_width' => (
32  is      => 'ro',
33  isa     => subtype('Num' => where { LaTeX::TikZ::Tools::numcmp($_, 0) >= 0 }),
34  default => 1,
35 );
36
37 my $W = Sub::Name::subname('WIDTH' => sub { sprintf '#WIDTH=%0.1f#', @_ });
38
39 my $forge_template = Sub::Name::subname('forge_template' => sub {
40  my ($dot_width, $space_width) = @_;
41
42  my ($low_left, $up_right, $tile_size, $center);
43  my ($width, $half_width, $shadow_min, $shadow_max);
44
45  $width      = $W->($space_width);
46  $half_width = $W->($space_width / 2);
47
48  $shadow_min = $W->(- $dot_width);
49  $shadow_max = $W->($space_width + $dot_width);
50  $dot_width  = $W->($dot_width);
51
52  $low_left   = "\\pgfqpoint{$shadow_min}{$shadow_min}";
53  $up_right   = "\\pgfqpoint{$shadow_max}{$shadow_max}";
54  $center     = "\\pgfqpoint{$half_width}{$half_width}";
55  $tile_size  = "\\pgfqpoint{$width}{$width}";
56
57  return [
58   "\\pgfdeclarepatternformonly{#NAME#}{$low_left}{$up_right}{$tile_size}{",
59   "\\pgfpathcircle{$center}{$dot_width}",
60   "\\pgfusepath{fill}",
61   '}',
62  ];
63 });
64
65 around 'BUILDARGS' => sub {
66  my ($orig, $class, %args) = @_;
67
68  confess('Can\'t specify an explicit template for a '. __PACKAGE__ .' pattern')
69                                                       if exists $args{template};
70
71  my @params = qw/dot_width space_width/;
72
73  my $meta = $class->meta;
74  for (@params) {
75   my $attr = $meta->find_attribute_by_name($_);
76   $args{$_} = $attr->default if $attr->has_default and not exists $args{$_};
77   $attr->type_constraint->assert_valid($args{$_});
78  }
79
80  $args{template} = $forge_template->(@args{@params});
81
82  $class->$orig(%args);
83 };
84
85 __PACKAGE__->meta->make_immutable;
86
87 =head1 AUTHOR
88
89 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
90
91 You can contact me by mail or on C<irc.perl.org> (vincent).
92
93 =head1 BUGS
94
95 Please report any bugs or feature requests to C<bug-latex-tikz at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ>.
96 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
97
98 =head1 SUPPORT
99
100 You can find documentation for this module with the perldoc command.
101
102     perldoc LaTeX::TikZ
103
104 =head1 COPYRIGHT & LICENSE
105
106 Copyright 2010 Vincent Pit, all rights reserved.
107
108 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
109
110 =cut
111
112 1; # End of LaTeX::TikZ::Mod::Pattern::Dots