]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Pattern.pm
Initial commit
[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      => '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  $cache = $pat->_cache->{$tikz_id} = [ ];
62
63  $cache->[0] = 'pat' . $id++;
64
65  my $template = $pat->template;
66  $template =~ s!#([^#]+)#!
67   my ($command, @opts) = split /=/, $1, 2;
68   @opts = split /,/, $opts[0] if @opts;
69   $handlers{lc $command}->($pat, $tikz, @opts);
70  !ge;
71  $cache->[1] = $template;
72 }
73
74 sub apply { 'fill', 'pattern=' . $_[0]->name($_[1]) }
75
76 __PACKAGE__->meta->make_immutable;
77
78 =head1 AUTHOR
79
80 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
81
82 You can contact me by mail or on C<irc.perl.org> (vincent).
83
84 =head1 BUGS
85
86 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>.
87 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
88
89 =head1 SUPPORT
90
91 You can find documentation for this module with the perldoc command.
92
93     perldoc LaTeX::TikZ
94
95 =head1 COPYRIGHT & LICENSE
96
97 Copyright 2010 Vincent Pit, all rights reserved.
98
99 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
100
101 =cut
102
103 1; # End of LaTeX::TikZ::Mod::Pattern