1 package LaTeX::TikZ::Formatter;
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Point;
22 use LaTeX::TikZ::Interface;
24 use LaTeX::TikZ::Tools;
27 use Any::Moose 'Util::TypeConstraints';
31 isa => enum([ qw/cm pt/ ]),
59 isa => 'LaTeX::TikZ::Point::Autocoerce',
66 my $origin = $tikz->origin;
67 if (defined $origin) {
68 my ($x, $y) = map $origin->$_, qw/x y/;
75 defined() ? "$_" : '(undef)';
76 } map($tikz->$_, qw/unit format scale width height/), $origin;
84 no warnings 'recursion';
86 Sub::Name::subname('find_mods_rec' => sub {
87 my ($set, $layers, $others) = @_;
91 next if $seen{$tag}++;
93 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
100 my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
102 : $set->isa('LaTeX::TikZ::Set::Path')
106 $find_mods_rec->($_, $layers, $others) for @subsets;
110 Sub::Name::subname('find_mods' => sub {
113 $find_mods_rec->(@_);
120 my ($tikz, @sets) = @_;
122 unless ($translate) {
123 require LaTeX::TikZ::Functor;
124 $translate = LaTeX::TikZ::Functor->new(
126 'LaTeX::TikZ::Set::Point' => sub {
127 my ($functor, $set, $v) = @_;
134 label => $set->label,
142 my $origin = $tikz->origin;
143 if (defined $origin) {
144 @sets = map $_->$translate($origin), @sets;
147 my (@layers, @other_mods);
148 my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
149 $find_mods->($seq, \@layers, \@other_mods);
151 my $w = $tikz->width;
152 my $h = $tikz->height;
154 if (defined $w and defined $h) {
155 require LaTeX::TikZ::Set::Rectangle;
157 $_->clip(LaTeX::TikZ::Set::Rectangle->new(
163 $_ = $tikz->len($_) for $w, $h;
164 $canvas = ",papersize={$w,$h},body={$w,$h}";
168 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
169 "\\usepackage{tikz}",
170 "\\usetikzlibrary{patterns}",
174 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
175 push @decls, $_->declare($tikz) for @other_mods;
178 "\\begin{tikzpicture}",
179 @{ $_->draw($tikz) },
180 "\\end{tikzpicture}",
183 return \@header, \@decls, @bodies;
187 my ($tikz, $len) = @_;
189 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
191 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
197 $a = ($a * 180) / CORE::atan2(0, -1);
198 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
201 sprintf $tikz->format, POSIX::ceil($a);
205 my ($tikz, $name, $pos) = @_;
207 my $scale = sprintf '%0.2f', $tikz->scale / 5;
209 "node[scale=$scale,$pos] {$name}";
213 my ($tikz, $width) = @_;
215 # width=1 is 0.4 points for a scale of 2.5
216 0.8 * $width * ($tikz->scale / 5);
219 LaTeX::TikZ::Interface->register(
223 __PACKAGE__->new(@_);
227 __PACKAGE__->meta->make_immutable;
231 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
233 You can contact me by mail or on C<irc.perl.org> (vincent).
237 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>.
238 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
242 You can find documentation for this module with the perldoc command.
246 =head1 COPYRIGHT & LICENSE
248 Copyright 2010 Vincent Pit, all rights reserved.
250 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
254 1; # End of LaTeX::TikZ::Formatter