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';
43 isa => enum([ qw/cm pt/ ]),
91 isa => 'LaTeX::TikZ::Point::Autocoerce',
104 my $origin = $tikz->origin;
105 if (defined $origin) {
106 my ($x, $y) = map $origin->$_, qw/x y/;
113 defined() ? "$_" : '(undef)';
114 } map($tikz->$_, qw/unit format scale width height/), $origin;
125 $find_mods_rec = do {
126 no warnings 'recursion';
128 Sub::Name::subname('find_mods_rec' => sub {
129 my ($set, $layers, $others) = @_;
133 next if $seen{$tag}++;
135 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
142 my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
144 : $set->isa('LaTeX::TikZ::Set::Path')
148 $find_mods_rec->($_, $layers, $others) for @subsets;
152 Sub::Name::subname('find_mods' => sub {
155 $find_mods_rec->(@_);
162 my ($tikz, @sets) = @_;
164 unless ($translate) {
165 require LaTeX::TikZ::Functor;
166 $translate = LaTeX::TikZ::Functor->new(
168 'LaTeX::TikZ::Set::Point' => sub {
169 my ($functor, $set, $v) = @_;
176 label => $set->label,
184 my $origin = $tikz->origin;
185 if (defined $origin) {
186 @sets = map $_->$translate($origin), @sets;
189 my (@layers, @other_mods);
190 my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
191 $find_mods->($seq, \@layers, \@other_mods);
193 my $w = $tikz->width;
194 my $h = $tikz->height;
196 if (defined $w and defined $h) {
197 require LaTeX::TikZ::Set::Rectangle;
199 $_->clip(LaTeX::TikZ::Set::Rectangle->new(
205 $_ = $tikz->len($_) for $w, $h;
206 $canvas = ",papersize={$w,$h},body={$w,$h}";
210 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
211 "\\usepackage{tikz}",
212 "\\usetikzlibrary{patterns}",
216 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
217 push @decls, $_->declare($tikz) for @other_mods;
220 "\\begin{tikzpicture}",
221 @{ $_->draw($tikz) },
222 "\\end{tikzpicture}",
225 return \@header, \@decls, @bodies;
233 my ($tikz, $len) = @_;
235 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
237 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
247 $a = ($a * 180) / CORE::atan2(0, -1);
248 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
251 sprintf $tikz->format, POSIX::ceil($a);
259 my ($tikz, $name, $pos) = @_;
261 my $scale = sprintf '%0.2f', $tikz->scale / 5;
263 "node[scale=$scale,$pos] {$name}";
271 my ($tikz, $width) = @_;
273 # width=1 is 0.4 points for a scale of 2.5
274 0.8 * $width * ($tikz->scale / 5);
277 LaTeX::TikZ::Interface->register(
281 __PACKAGE__->new(@_);
285 __PACKAGE__->meta->make_immutable;
293 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
295 You can contact me by mail or on C<irc.perl.org> (vincent).
299 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>.
300 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
304 You can find documentation for this module with the perldoc command.
308 =head1 COPYRIGHT & LICENSE
310 Copyright 2010 Vincent Pit, all rights reserved.
312 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
316 1; # End of LaTeX::TikZ::Formatter