]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Pattern.pm
Complete patterns implementation, interface and tests
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Mod / Pattern.pm
1 package LaTeX::TikZ::Mod::Pattern;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Mod::Pattern - A modifier that fills a closed path with a pattern.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Any::Moose;
19
20 with 'LaTeX::TikZ::Mod';
21
22 has 'template' => (
23  is       => 'ro',
24  isa      => 'ArrayRef[Str]',
25  required => 1,
26 );
27
28 has '_cache' => (
29  is       => 'ro',
30  isa      => 'HashRef',
31  init_arg => undef,
32  default  => sub { +{ } },
33 );
34
35 sub name {
36  my ($pat, $tikz) = @_;
37
38  my $cache = $pat->_cache->{$tikz->id};
39  confess('Template not yet declared') unless defined $cache;
40
41  $cache->[0];
42 }
43
44 my $id = 'a';
45
46 my %handlers = (
47  name  => sub { $_[0]->name($_[1]) },
48  width => sub { sprintf '%0.1fpt', $_[1]->thickness($_[2]) },
49 );
50
51 sub tag { ref $_[0] }
52
53 sub cover { 1 }
54
55 sub declare {
56  my ($pat, $tikz) = @_;
57
58  my $tikz_id = $tikz->id;
59  my $cache   = $pat->_cache->{$tikz_id};
60  return @{$cache->[1]} if defined $cache;
61
62  $cache = $pat->_cache->{$tikz_id} = [ ];
63  $cache->[0] = 'pat' . $id++;
64
65  my $template = [ map $_, @{$pat->template} ];
66  s!#([^#]+)#!
67   my ($command, @opts) = split /=/, $1, 2;
68   @opts = split /,/, $opts[0] if @opts;
69   $handlers{lc $command}->($pat, $tikz, @opts);
70  !ge for @$template;
71  $cache->[1] = $template;
72
73  return @$template;
74 }
75
76 sub apply { 'fill', 'pattern=' . $_[0]->name($_[1]) }
77
78 use LaTeX::TikZ::Interface pattern => sub {
79  my $class = shift;
80
81  my %args = @_;
82  if (exists $args{class}) {
83   $class = delete $args{class};
84   $class = __PACKAGE__ . '::' . $class unless $class =~ /::/;
85   (my $pm = $class) =~ s{::}{/}g;
86   $pm .= '.pm';
87   require $pm;
88  }
89
90  $class->new(%args);
91 };
92
93 __PACKAGE__->meta->make_immutable;
94
95 =head1 AUTHOR
96
97 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
98
99 You can contact me by mail or on C<irc.perl.org> (vincent).
100
101 =head1 BUGS
102
103 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>.
104 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
105
106 =head1 SUPPORT
107
108 You can find documentation for this module with the perldoc command.
109
110     perldoc LaTeX::TikZ
111
112 =head1 COPYRIGHT & LICENSE
113
114 Copyright 2010 Vincent Pit, all rights reserved.
115
116 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
117
118 =cut
119
120 1; # End of LaTeX::TikZ::Mod::Pattern