]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Formatter.pm
Complete patterns implementation, interface and tests
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Formatter.pm
1 package LaTeX::TikZ::Formatter;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
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 LaTeX::TikZ::Tools;
21
22 use Any::Moose;
23 use Any::Moose 'Util::TypeConstraints';
24
25 has 'unit' => (
26  is      => 'ro',
27  isa     => enum([ qw/cm pt/ ]),
28  default => 'cm',
29 );
30
31 has 'format' => (
32  is      => 'ro',
33  isa     => 'Str',
34  default => '%s',
35 );
36
37 has 'scale' => (
38  is      => 'ro',
39  isa     => 'Num',
40  default => 1,
41 );
42
43 has 'width' => (
44  is  => 'ro',
45  isa => 'Maybe[Num]',
46 );
47
48 has 'height' => (
49  is  => 'ro',
50  isa => 'Maybe[Num]',
51 );
52
53 has 'origin' => (
54  is     => 'ro',
55  isa    => 'Maybe[LaTeX::TikZ::Point::Autocoerce]',
56  coerce => 1,
57 );
58
59 sub id {
60  my $tikz = shift;
61
62  join $;, map {
63   my $val = $tikz->$_;
64   defined($val) ? "$val" : '(undef)';
65  } qw/unit format scale width height origin/;
66 }
67
68 my $find_mods;
69 $find_mods = do {
70  no warnings 'recursion';
71
72  Sub::Name::subname('find_mods' => sub {
73   my ($set, $layers, $others) = @_;
74
75   for ($set->mods) {
76    if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
77     push @$layers, $_;
78    } else {
79     push @$others, $_;
80    }
81   }
82
83   my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
84                 ? $set->kids
85                 : $set->isa('LaTeX::TikZ::Set::Path')
86                   ? $set->ops
87                   : ();
88
89   $find_mods->($_, $layers, $others) for @subsets;
90  })
91 };
92
93 sub render {
94  my $tikz = shift;
95
96  my $seq = LaTeX::TikZ::Set::Sequence->new(
97   kids => \@_,
98  );
99
100  my (@layers, @other_mods);
101  $find_mods->($seq, \@layers, \@other_mods);
102
103  my $o = $tikz->origin;
104  $seq  = $seq->translate($o) if defined $o;
105
106  my $w = $tikz->width;
107  my $h = $tikz->height;
108  my $canvas = '';
109  if (defined $w and defined $h) {
110   require LaTeX::TikZ::Set::Rectangle;
111   $seq->clip(LaTeX::TikZ::Set::Rectangle->new(
112    from   => 0,
113    width  => $w,
114    height => $h,
115   ));
116   $_ = $tikz->len($_) for $w, $h;
117   $canvas = ",papersize={$w,$h},body={$w,$h}";
118  }
119
120  my @header = (
121   "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
122   "\\usepackage{tikz}",
123   "\\usetikzlibrary{patterns}",
124  );
125
126  my @decls;
127  push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if  @layers;
128  push @decls, $_->declare($tikz)                        for @other_mods;
129
130  my @content = (
131   "\\begin{tikzpicture}",
132   @{ $seq->draw($tikz) },
133   "\\end{tikzpicture}",
134  );
135
136  return \@header, \@decls, \@content;
137 }
138
139 sub len {
140  my ($tikz, $len) = @_;
141
142  $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
143
144  sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
145 }
146
147 sub angle {
148  my ($tikz, $a) = @_;
149
150  $a = ($a * 180) / CORE::atan2(0, -1);
151  $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
152
153  require POSIX;
154  sprintf $tikz->format, POSIX::ceil($a);
155 }
156
157 sub label {
158  my ($tikz, $name, $pos) = @_;
159
160  my $scale = sprintf '%0.2f', $tikz->scale / 5;
161
162  "node[scale=$scale,$pos] {\$$name\$}";
163 }
164
165 sub thickness {
166  my ($tikz, $width) = @_;
167
168  # width=1 is 0.4 points for a scale of 2.5
169  0.8 * $width * ($tikz->scale / 5);
170 }
171
172 use LaTeX::TikZ::Interface formatter => sub {
173  shift;
174
175  __PACKAGE__->new(@_);
176 };
177
178 __PACKAGE__->meta->make_immutable;
179
180 =head1 AUTHOR
181
182 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
183
184 You can contact me by mail or on C<irc.perl.org> (vincent).
185
186 =head1 BUGS
187
188 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>.
189 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
190
191 =head1 SUPPORT
192
193 You can find documentation for this module with the perldoc command.
194
195     perldoc LaTeX::TikZ
196
197 =head1 COPYRIGHT & LICENSE
198
199 Copyright 2010 Vincent Pit, all rights reserved.
200
201 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
202
203 =cut
204
205 1; # End of LaTeX::TikZ::Formatter