1 package LaTeX::TikZ::Formatter;
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
16 our $VERSION = '0.02';
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.
26 use LaTeX::TikZ::Point;
28 use LaTeX::TikZ::Interface;
30 use LaTeX::TikZ::Tools;
33 use Mouse::Util::TypeConstraints;
39 The unit in which lengths are printed.
40 Valid units are C<cm> for centimeters and C<pt> for points.
48 isa => enum([ qw<cm pt> ]),
54 The format used to print the numbers.
68 The scale of the drawing.
82 The width of the drawing area.
84 Defaults to C<undef> for none.
95 The height of the drawing area.
97 Defaults to C<undef> for none.
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.
111 Defaults to C<(0, 0)>, meaning that the drawing area goes from C<(0, 0)> to C<($width, $height)>.
117 isa => 'LaTeX::TikZ::Point::Autocoerce',
125 An unique identifier of the formatter object.
132 my $origin = $tikz->origin;
133 if (defined $origin) {
134 my ($x, $y) = map $origin->$_, qw<x y>;
141 defined() ? "$_" : '(undef)';
142 } map($tikz->$_, qw<unit format scale width height>), $origin;
145 =head2 C<render @sets>
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.
151 This method returns a list of array references :
157 The first one contains the header lines to include between the C<\documentclass> and the C<\begin{document}>.
161 The second one contains the mod declaration lines to put inside the document, between C<\begin{document}> and C<\end{document}>.
165 Finally, there's one array reference for each given TikZ set, which contain the lines for the actual TikZ pictures.
169 The lines returned by L</render> don't end with a line feed.
171 my ($header, $declarations, $set1_body, $set2_body) = $formatter->render($set1, $set2);
173 open my $tex, '>', 'test.tex' or die "open('>test.tex'): $!";
175 print $tex "$_\n" for (
176 "\\documentclass[12pt]{article}",
179 "\\pagestyle{empty}",
198 $find_mods_rec = do {
199 no warnings 'recursion';
201 Sub::Name::subname('find_mods_rec' => sub {
202 my ($set, $layers, $others) = @_;
206 next if $seen{$tag}++;
208 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
215 my @subsets = $set->does('LaTeX::TikZ::Set::Container')
219 $find_mods_rec->($_, $layers, $others) for @subsets;
223 Sub::Name::subname('find_mods' => sub {
226 $find_mods_rec->(@_);
233 my ($tikz, @sets) = @_;
235 unless ($translate) {
236 require LaTeX::TikZ::Functor;
237 $translate = LaTeX::TikZ::Functor->new(
239 'LaTeX::TikZ::Set::Point' => sub {
240 my ($functor, $set, $v) = @_;
247 label => $set->label,
255 my $origin = $tikz->origin;
256 if (defined $origin) {
257 @sets = map $_->$translate($origin), @sets;
260 my (@layers, @other_mods);
261 my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
262 $find_mods->($seq, \@layers, \@other_mods);
264 my $w = $tikz->width;
265 my $h = $tikz->height;
267 if (defined $w and defined $h) {
268 require LaTeX::TikZ::Set::Rectangle;
270 $_->clip(LaTeX::TikZ::Set::Rectangle->new(
276 $_ = $tikz->len($_) for $w, $h;
277 $canvas = ",papersize={$w,$h},body={$w,$h}";
281 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
282 "\\usepackage{tikz}",
283 "\\usetikzlibrary{patterns}",
287 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
288 push @decls, $_->declare($tikz) for @other_mods;
291 "\\begin{tikzpicture}",
292 @{ $_->draw($tikz) },
293 "\\end{tikzpicture}",
296 return \@header, \@decls, @bodies;
301 Format the given length according to the formatter options.
306 my ($tikz, $len) = @_;
308 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
310 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
313 =head2 C<angle $theta>
315 Format the given angle (in radians) according to the formatter options.
322 $a = ($a * 180) / CORE::atan2(0, -1);
323 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
326 sprintf $tikz->format, POSIX::ceil($a);
329 =head2 C<label $name, $pos>
331 Returns the TikZ code for a point labeled C<$name> at position C<$pos> according to the formatter options.
336 my ($tikz, $name, $pos) = @_;
338 my $scale = sprintf '%0.2f', $tikz->scale / 5;
340 "node[scale=$scale,$pos] {$name}";
345 Format the given line thickness according to the formatter options.
350 my ($tikz, $width) = @_;
352 # width=1 is 0.4 points for a scale of 2.5
353 0.8 * $width * ($tikz->scale / 5);
356 LaTeX::TikZ::Interface->register(
360 __PACKAGE__->new(@_);
364 __PACKAGE__->meta->make_immutable;
372 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
374 You can contact me by mail or on C<irc.perl.org> (vincent).
378 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>.
379 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
383 You can find documentation for this module with the perldoc command.
387 =head1 COPYRIGHT & LICENSE
389 Copyright 2010 Vincent Pit, all rights reserved.
391 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
395 1; # End of LaTeX::TikZ::Formatter