1 package LaTeX::TikZ::Formatter;
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Interface;
22 use LaTeX::TikZ::Tools;
25 use Any::Moose 'Util::TypeConstraints';
29 isa => enum([ qw/cm pt/ ]),
57 isa => 'Maybe[LaTeX::TikZ::Point::Autocoerce]',
66 defined($val) ? "$val" : '(undef)';
67 } qw/unit format scale width height origin/;
75 no warnings 'recursion';
77 Sub::Name::subname('find_mods_rec' => sub {
78 my ($set, $layers, $others) = @_;
82 next if $seen{$tag}++;
84 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
91 my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
93 : $set->isa('LaTeX::TikZ::Set::Path')
97 $find_mods_rec->($_, $layers, $others) for @subsets;
101 Sub::Name::subname('find_mods' => sub {
104 $find_mods_rec->(@_);
111 my $seq = LaTeX::TikZ::Set::Sequence->new(
115 my (@layers, @other_mods);
116 $find_mods->($seq, \@layers, \@other_mods);
118 my $o = $tikz->origin;
119 $seq = $seq->translate($o) if defined $o;
121 my $w = $tikz->width;
122 my $h = $tikz->height;
124 if (defined $w and defined $h) {
125 require LaTeX::TikZ::Set::Rectangle;
126 $seq->clip(LaTeX::TikZ::Set::Rectangle->new(
131 $_ = $tikz->len($_) for $w, $h;
132 $canvas = ",papersize={$w,$h},body={$w,$h}";
136 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
137 "\\usepackage{tikz}",
138 "\\usetikzlibrary{patterns}",
142 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
143 push @decls, $_->declare($tikz) for @other_mods;
146 "\\begin{tikzpicture}",
147 @{ $seq->draw($tikz) },
148 "\\end{tikzpicture}",
151 return \@header, \@decls, \@content;
155 my ($tikz, $len) = @_;
157 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
159 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
165 $a = ($a * 180) / CORE::atan2(0, -1);
166 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
169 sprintf $tikz->format, POSIX::ceil($a);
173 my ($tikz, $name, $pos) = @_;
175 my $scale = sprintf '%0.2f', $tikz->scale / 5;
177 "node[scale=$scale,$pos] {\$$name\$}";
181 my ($tikz, $width) = @_;
183 # width=1 is 0.4 points for a scale of 2.5
184 0.8 * $width * ($tikz->scale / 5);
187 LaTeX::TikZ::Interface->register(
191 __PACKAGE__->new(@_);
195 __PACKAGE__->meta->make_immutable;
199 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
201 You can contact me by mail or on C<irc.perl.org> (vincent).
205 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>.
206 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
210 You can find documentation for this module with the perldoc command.
214 =head1 COPYRIGHT & LICENSE
216 Copyright 2010 Vincent Pit, all rights reserved.
218 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
222 1; # End of LaTeX::TikZ::Formatter