]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Formatter.pm
More docs for LaTeX::TikZ::Formatter
[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 =head1 DESCRIPTION
19
20 A formatter object turns a L<LaTeX::TikZ::Set> tree into the actual TikZ code, depending on some parameters such as the scale, the unit or the origin.
21
22 =cut
23
24 use Sub::Name ();
25
26 use LaTeX::TikZ::Point;
27
28 use LaTeX::TikZ::Interface;
29
30 use LaTeX::TikZ::Tools;
31
32 use Any::Moose;
33 use Any::Moose 'Util::TypeConstraints';
34
35 =head1 ATTRIBUTES
36
37 =head2 C<unit>
38
39 The unit in which lengths are printed.
40 Valid units are C<cm> for centimeters and C<pt> for points.
41
42 Defaults to C<cm>.
43
44 =cut
45
46 has 'unit' => (
47  is      => 'ro',
48  isa     => enum([ qw/cm pt/ ]),
49  default => 'cm',
50 );
51
52 =head2 C<format>
53
54 The format used to print the numbers.
55
56 Defaults to C<%s>.
57
58 =cut
59
60 has 'format' => (
61  is      => 'ro',
62  isa     => 'Str',
63  default => '%s',
64 );
65
66 =head2 C<scale>
67
68 The scale of the drawing.
69
70 Defaults to C<1>.
71
72 =cut
73
74 has 'scale' => (
75  is      => 'rw',
76  isa     => 'Num',
77  default => 1,
78 );
79
80 =head2 C<width>
81
82 The width of the drawing area.
83
84 Defaults to C<undef> for none.
85
86 =cut
87
88 has 'width' => (
89  is  => 'rw',
90  isa => 'Maybe[Num]',
91 );
92
93 =head2 C<height>
94
95 The height of the drawing area.
96
97 Defaults to C<undef> for none.
98
99 =cut
100
101 has 'height' => (
102  is  => 'rw',
103  isa => 'Maybe[Num]',
104 );
105
106 =head2 C<origin>
107
108 A point coerced into a L<LaTeX::TikZ::Point> object that represents the logical origin of the printed area.
109 If L</width> and L</height> are set, the canvas will be equivalent to a rectangle whose lower left corner at C<-$origin> and of given width and length.
110
111 Defaults to C<(0, 0)>, meaning that the drawing area goes from C<(0, 0)> to C<($width, $height)>.
112
113 =cut
114
115 has 'origin' => (
116  is     => 'rw',
117  isa    => 'LaTeX::TikZ::Point::Autocoerce',
118  coerce => 1,
119 );
120
121 =head1 METHODS
122
123 =head2 C<id>
124
125 An unique identifier of the formatter object.
126
127 =cut
128
129 sub id {
130  my $tikz = shift;
131
132  my $origin = $tikz->origin;
133  if (defined $origin) {
134   my ($x, $y) = map $origin->$_, qw/x y/;
135   $origin = "($x;$y)";
136  } else {
137   $origin = "(0;0)";
138  }
139
140  join $;, map {
141   defined() ? "$_" : '(undef)';
142  } map($tikz->$_, qw/unit format scale width height/), $origin;
143 }
144
145 =head2 C<render @sets>
146
147 Processes all the L<LaTeX::TikZ::Set> objects given in C<@sets> to produce the actual TikZ code to insert in the LaTeX file.
148 First, all the mods applied to the sets and their subsets are collected, and a declaration is emitted if needed for each of them by calling L<LaTeX::TikZ::Mod/declare>.
149 Then, the image code is generated for each set.
150
151 This method returns a list of array references :
152
153 =over 4
154
155 =item *
156
157 The first one contains the header lines to include between the C<\documentclass> and the C<\begin{document}>.
158
159 =item *
160
161 The second one contains the mod declaration lines to put inside the document, between C<\begin{document}> and C<\end{document}>.
162
163 =item *
164
165 Finally, there's one array reference for each given TikZ set, which contain the lines for the actual TikZ pictures.
166
167 =back
168
169     my ($header, $declarations, $seq1_body, $seq2_body) = $formatter->render($set1, $set2);
170
171 =cut
172
173 my $find_mods = do {
174  our %seen;
175
176  my $find_mods_rec;
177  $find_mods_rec = do {
178   no warnings 'recursion';
179
180   Sub::Name::subname('find_mods_rec' => sub {
181    my ($set, $layers, $others) = @_;
182
183    for ($set->mods) {
184     my $tag = $_->tag;
185     next if $seen{$tag}++;
186
187     if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
188      push @$layers, $_;
189     } else {
190      push @$others, $_;
191     }
192    }
193
194    my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
195                  ? $set->kids
196                  : $set->isa('LaTeX::TikZ::Set::Path')
197                    ? $set->ops
198                    : ();
199
200    $find_mods_rec->($_, $layers, $others) for @subsets;
201   });
202  };
203
204  Sub::Name::subname('find_mods' => sub {
205   local %seen = ();
206
207   $find_mods_rec->(@_);
208  });
209 };
210
211 my $translate;
212
213 sub render {
214  my ($tikz, @sets) = @_;
215
216  unless ($translate) {
217   require LaTeX::TikZ::Functor;
218   $translate = LaTeX::TikZ::Functor->new(
219    rules => [
220     'LaTeX::TikZ::Set::Point' => sub {
221      my ($functor, $set, $v) = @_;
222
223      $set->new(
224       point => [
225        $set->x + $v->x,
226        $set->y + $v->y,
227       ],
228       label => $set->label,
229       pos   => $set->pos,
230      );
231     },
232    ],
233   );
234  }
235
236  my $origin = $tikz->origin;
237  if (defined $origin) {
238   @sets = map $_->$translate($origin), @sets;
239  }
240
241  my (@layers, @other_mods);
242  my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
243  $find_mods->($seq, \@layers, \@other_mods);
244
245  my $w = $tikz->width;
246  my $h = $tikz->height;
247  my $canvas = '';
248  if (defined $w and defined $h) {
249   require LaTeX::TikZ::Set::Rectangle;
250   for (@sets) {
251    $_->clip(LaTeX::TikZ::Set::Rectangle->new(
252     from   => 0,
253     width  => $w,
254     height => $h,
255    ));
256   }
257   $_ = $tikz->len($_) for $w, $h;
258   $canvas = ",papersize={$w,$h},body={$w,$h}";
259  }
260
261  my @header = (
262   "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
263   "\\usepackage{tikz}",
264   "\\usetikzlibrary{patterns}",
265  );
266
267  my @decls;
268  push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if  @layers;
269  push @decls, $_->declare($tikz)                        for @other_mods;
270
271  my @bodies = map [
272   "\\begin{tikzpicture}",
273   @{ $_->draw($tikz) },
274   "\\end{tikzpicture}",
275  ], @sets;
276
277  return \@header, \@decls, @bodies;
278 }
279
280 =head2 C<len $len>
281
282 Format the given length according to the formatter options.
283
284 =cut
285
286 sub len {
287  my ($tikz, $len) = @_;
288
289  $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
290
291  sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
292 }
293
294 =head2 C<angle $theta>
295
296 Format the given angle (in radians) according to the formatter options.
297
298 =cut
299
300 sub angle {
301  my ($tikz, $a) = @_;
302
303  $a = ($a * 180) / CORE::atan2(0, -1);
304  $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
305
306  require POSIX;
307  sprintf $tikz->format, POSIX::ceil($a);
308 }
309
310 =head2 C<label $name, $pos>
311
312 Returns the TikZ code for a point labeled C<$name> at position C<$pos> according to the formatter options.
313
314 =cut
315
316 sub label {
317  my ($tikz, $name, $pos) = @_;
318
319  my $scale = sprintf '%0.2f', $tikz->scale / 5;
320
321  "node[scale=$scale,$pos] {$name}";
322 }
323
324 =head2 C<thickness>
325
326 Format the given line thickness according to the formatter options.
327
328 =cut
329
330 sub thickness {
331  my ($tikz, $width) = @_;
332
333  # width=1 is 0.4 points for a scale of 2.5
334  0.8 * $width * ($tikz->scale / 5);
335 }
336
337 LaTeX::TikZ::Interface->register(
338  formatter => sub {
339   shift;
340
341   __PACKAGE__->new(@_);
342  },
343 );
344
345 __PACKAGE__->meta->make_immutable;
346
347 =head1 SEE ALSO
348
349 L<LaTeX::TikZ>.
350
351 =head1 AUTHOR
352
353 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
354
355 You can contact me by mail or on C<irc.perl.org> (vincent).
356
357 =head1 BUGS
358
359 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>.
360 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
361
362 =head1 SUPPORT
363
364 You can find documentation for this module with the perldoc command.
365
366     perldoc LaTeX::TikZ
367
368 =head1 COPYRIGHT & LICENSE
369
370 Copyright 2010 Vincent Pit, all rights reserved.
371
372 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
373
374 =cut
375
376 1; # End of LaTeX::TikZ::Formatter