1 package LaTeX::TikZ::Formatter;
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
16 our $VERSION = '0.01';
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 Any::Moose '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->isa('LaTeX::TikZ::Set::Sequence')
217 : $set->isa('LaTeX::TikZ::Set::Path')
221 $find_mods_rec->($_, $layers, $others) for @subsets;
225 Sub::Name::subname('find_mods' => sub {
228 $find_mods_rec->(@_);
235 my ($tikz, @sets) = @_;
237 unless ($translate) {
238 require LaTeX::TikZ::Functor;
239 $translate = LaTeX::TikZ::Functor->new(
241 'LaTeX::TikZ::Set::Point' => sub {
242 my ($functor, $set, $v) = @_;
249 label => $set->label,
257 my $origin = $tikz->origin;
258 if (defined $origin) {
259 @sets = map $_->$translate($origin), @sets;
262 my (@layers, @other_mods);
263 my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
264 $find_mods->($seq, \@layers, \@other_mods);
266 my $w = $tikz->width;
267 my $h = $tikz->height;
269 if (defined $w and defined $h) {
270 require LaTeX::TikZ::Set::Rectangle;
272 $_->clip(LaTeX::TikZ::Set::Rectangle->new(
278 $_ = $tikz->len($_) for $w, $h;
279 $canvas = ",papersize={$w,$h},body={$w,$h}";
283 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
284 "\\usepackage{tikz}",
285 "\\usetikzlibrary{patterns}",
289 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
290 push @decls, $_->declare($tikz) for @other_mods;
293 "\\begin{tikzpicture}",
294 @{ $_->draw($tikz) },
295 "\\end{tikzpicture}",
298 return \@header, \@decls, @bodies;
303 Format the given length according to the formatter options.
308 my ($tikz, $len) = @_;
310 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
312 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
315 =head2 C<angle $theta>
317 Format the given angle (in radians) according to the formatter options.
324 $a = ($a * 180) / CORE::atan2(0, -1);
325 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
328 sprintf $tikz->format, POSIX::ceil($a);
331 =head2 C<label $name, $pos>
333 Returns the TikZ code for a point labeled C<$name> at position C<$pos> according to the formatter options.
338 my ($tikz, $name, $pos) = @_;
340 my $scale = sprintf '%0.2f', $tikz->scale / 5;
342 "node[scale=$scale,$pos] {$name}";
347 Format the given line thickness according to the formatter options.
352 my ($tikz, $width) = @_;
354 # width=1 is 0.4 points for a scale of 2.5
355 0.8 * $width * ($tikz->scale / 5);
358 LaTeX::TikZ::Interface->register(
362 __PACKAGE__->new(@_);
366 __PACKAGE__->meta->make_immutable;
374 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
376 You can contact me by mail or on C<irc.perl.org> (vincent).
380 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>.
381 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
385 You can find documentation for this module with the perldoc command.
389 =head1 COPYRIGHT & LICENSE
391 Copyright 2010 Vincent Pit, all rights reserved.
393 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
397 1; # End of LaTeX::TikZ::Formatter