]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Pattern/Dots.pm
Initial commit
[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
22 extends 'LaTeX::TikZ::Mod::Pattern';
23
24 has 'dot_width' => (
25  is      => 'ro',
26  isa     => 'Int',
27  default => 1,
28 );
29
30 has 'space_width' => (
31  is      => 'ro',
32  isa     => 'Int',
33  default => 1,
34 );
35
36 my $W = Sub::Name::subname('WIDTH' => sub { sprintf '#WIDTH=%0.1f#', @_ });
37
38 my $forge_template = Sub::Name::subname('forge_template' => sub {
39  my ($dot_width, $space_width) = @_;
40
41  my ($low_left, $up_right, $tile_size, $center);
42  my ($width, $half_width, $shadow_min, $shadow_max);
43
44  $width      = $W->($space_width);
45  $half_width = $W->($space_width / 2);
46
47  $shadow_min = $W->(- $dot_width);
48  $shadow_max = $W->($space_width + $dot_width);
49  $dot_width  = $W->($dot_width);
50
51  $low_left   = "\\pgfqpoint{$shadow_min}{$shadow_min}";
52  $up_right   = "\\pgfqpoint{$shadow_max}{$shadow_max}";
53  $center     = "\\pgfqpoint{$half_width}{$half_width}";
54  $tile_size  = "\\pgfqpoint{$width}{$width}";
55
56  <<" PATTERN";
57 \\pgfdeclarepatternformonly{#NAME#}{$low_left}{$up_right}{$tile_size}{%
58  \\pgfpathcircle{$center}{$dot_width}
59  \\pgfusepath{fill}
60 }
61  PATTERN
62 });
63
64 around 'BUILDARGS' => sub {
65  my ($orig, $class, %args);
66
67  confess('Can\'t specify an explicit template for a '. __PACKAGE__ .' pattern')
68                                                       if exists $args{template};
69
70  my @params = qw/dot_width space_width/;
71
72  my $meta = $class->meta;
73  for (@params) {
74   my $attr = $meta->find_attribute_by_name($_);
75   $args{$_} = $attr->default if $attr->has_default and not exists $args{$_};
76   $attr->type_constraint->assert_valid($args{$_});
77  }
78
79  $args{template} = $forge_template->(@args{@params});
80
81  $class->$orig(%args);
82 };
83
84 __PACKAGE__->meta->make_immutable;
85
86 =head1 AUTHOR
87
88 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
89
90 You can contact me by mail or on C<irc.perl.org> (vincent).
91
92 =head1 BUGS
93
94 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>.
95 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
96
97 =head1 SUPPORT
98
99 You can find documentation for this module with the perldoc command.
100
101     perldoc LaTeX::TikZ
102
103 =head1 COPYRIGHT & LICENSE
104
105 Copyright 2010 Vincent Pit, all rights reserved.
106
107 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
108
109 =cut
110
111 1; # End of LaTeX::TikZ::Mod::Pattern::Dots