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 => 'Maybe[LaTeX::TikZ::Point::Autocoerce]',
68 defined($val) ? "$val" : '(undef)';
69 } qw/unit format scale width height origin/;
77 no warnings 'recursion';
79 Sub::Name::subname('find_mods_rec' => sub {
80 my ($set, $layers, $others) = @_;
84 next if $seen{$tag}++;
86 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
93 my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
95 : $set->isa('LaTeX::TikZ::Set::Path')
99 $find_mods_rec->($_, $layers, $others) for @subsets;
103 Sub::Name::subname('find_mods' => sub {
106 $find_mods_rec->(@_);
113 my $seq = LaTeX::TikZ::Set::Sequence->new(
117 my (@layers, @other_mods);
118 $find_mods->($seq, \@layers, \@other_mods);
120 my $w = $tikz->width;
121 my $h = $tikz->height;
123 if (defined $w and defined $h) {
124 require LaTeX::TikZ::Set::Rectangle;
125 $seq->clip(LaTeX::TikZ::Set::Rectangle->new(
130 $_ = $tikz->len($_) for $w, $h;
131 $canvas = ",papersize={$w,$h},body={$w,$h}";
135 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
136 "\\usepackage{tikz}",
137 "\\usetikzlibrary{patterns}",
141 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
142 push @decls, $_->declare($tikz) for @other_mods;
145 "\\begin{tikzpicture}",
146 @{ $seq->draw($tikz) },
147 "\\end{tikzpicture}",
150 return \@header, \@decls, \@content;
154 my ($tikz, $len) = @_;
156 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
158 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
164 $a = ($a * 180) / CORE::atan2(0, -1);
165 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
168 sprintf $tikz->format, POSIX::ceil($a);
172 my ($tikz, $name, $pos) = @_;
174 my $scale = sprintf '%0.2f', $tikz->scale / 5;
176 "node[scale=$scale,$pos] {\$$name\$}";
180 my ($tikz, $width) = @_;
182 # width=1 is 0.4 points for a scale of 2.5
183 0.8 * $width * ($tikz->scale / 5);
186 LaTeX::TikZ::Interface->register(
190 __PACKAGE__->new(@_);
194 __PACKAGE__->meta->make_immutable;
198 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
200 You can contact me by mail or on C<irc.perl.org> (vincent).
204 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>.
205 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
209 You can find documentation for this module with the perldoc command.
213 =head1 COPYRIGHT & LICENSE
215 Copyright 2010 Vincent Pit, all rights reserved.
217 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
221 1; # End of LaTeX::TikZ::Formatter