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