1 package LaTeX::TikZ::Formatter;
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Tools;
23 use Any::Moose 'Util::TypeConstraints';
27 isa => enum([ qw/cm pt/ ]),
55 does => 'Maybe[LaTeX::TikZ::Point]',
58 my $lts_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set');
62 no warnings 'recursion';
64 Sub::Name::subname('find_mods' => sub {
65 my ($set, $layers, $others) = @_;
67 if ($set->isa('LaTeX::TikZ::Set::Mod')) {
69 if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
77 my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
79 : $set->isa('LaTeX::TikZ::Set::Path')
83 $find_mods->($_, $layers, $others) for @subsets;
90 $lts_tc->assert_valid($_) for @_;
92 my $seq = LaTeX::TikZ::Set::Sequence->new(
96 my (@layers, @other_mods);
97 $find_mods->($seq, \@layers, \@other_mods);
99 my $o = $tikz->origin;
100 $seq = $seq->translate($o) if defined $o;
102 my $w = $tikz->width;
103 my $h = $tikz->height;
105 if (defined $w and defined $h) {
106 $seq->clip(Tikz->rectangle(Tikz->point(0) => [ $w, $h ]));
107 $_ = $tikz->len($_) for $w, $h;
108 $canvas = ",papersize={$w,$h},body={$w,$h}";
112 "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
113 "\\usepackage{tikz}",
114 "\\usetikzlibrary{patterns}",
119 my $layers_decl = LaTeX::TikZ::Mod::Layer->declare(@layers);
120 if (defined $layers_decl) {
122 push @decls, $layers_decl;
126 my $decl = $_->declare($tikz);
134 "\\begin{tikzpicture}",
135 do { my $s = $seq->draw($tikz); chomp $s; $s },
136 "\\end{tikzpicture}",
139 return \@header, \@decls, \@content;
143 my ($tikz, $len) = @_;
145 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
147 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
153 $a = ($a * 180) / CORE::atan2(0, -1);
154 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
157 sprintf $tikz->format, POSIX::ceil($a);
161 my ($tikz, $name, $pos) = @_;
163 my $scale = sprintf '%0.2f', $tikz->scale / 5;
165 "node[scale=$scale,$pos] {\$$name\$}";
169 my ($tikz, $width) = @_;
171 # width=1 is 0.4 points for a scale of 2.5
172 0.8 * $width * ($tikz->scale / 5);
175 __PACKAGE__->meta->make_immutable;
179 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
181 You can contact me by mail or on C<irc.perl.org> (vincent).
185 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>.
186 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
190 You can find documentation for this module with the perldoc command.
194 =head1 COPYRIGHT & LICENSE
196 Copyright 2010 Vincent Pit, all rights reserved.
198 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
202 1; # End of LaTeX::TikZ::Formatter