=head1 VERSION
-Version 0.01
+Version 0.02
=cut
-our $VERSION = '0.01';
+our $VERSION = '0.02';
+
+=head1 DESCRIPTION
+
+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.
+
+=cut
use Sub::Name ();
+use LaTeX::TikZ::Point;
+
+use LaTeX::TikZ::Interface;
+
use LaTeX::TikZ::Tools;
use Any::Moose;
use Any::Moose 'Util::TypeConstraints';
+=head1 ATTRIBUTES
+
+=head2 C<unit>
+
+The unit in which lengths are printed.
+Valid units are C<cm> for centimeters and C<pt> for points.
+
+Defaults to C<cm>.
+
+=cut
+
has 'unit' => (
is => 'ro',
isa => enum([ qw/cm pt/ ]),
default => 'cm',
);
+=head2 C<format>
+
+The format used to print the numbers.
+
+Defaults to C<%s>.
+
+=cut
+
has 'format' => (
is => 'ro',
isa => 'Str',
default => '%s',
);
+=head2 C<scale>
+
+The scale of the drawing.
+
+Defaults to C<1>.
+
+=cut
+
has 'scale' => (
- is => 'ro',
+ is => 'rw',
isa => 'Num',
default => 1,
);
+=head2 C<width>
+
+The width of the drawing area.
+
+Defaults to C<undef> for none.
+
+=cut
+
has 'width' => (
- is => 'ro',
+ is => 'rw',
isa => 'Maybe[Num]',
);
+=head2 C<height>
+
+The height of the drawing area.
+
+Defaults to C<undef> for none.
+
+=cut
+
has 'height' => (
- is => 'ro',
+ is => 'rw',
isa => 'Maybe[Num]',
);
+=head2 C<origin>
+
+A point coerced into a L<LaTeX::TikZ::Point> object that represents the logical origin of the printed area.
+If L</width> and L</height> are set, the canvas will be equivalent to a rectangle whose lower left corner at C<-$origin> and of given width and length.
+
+Defaults to C<(0, 0)>, meaning that the drawing area goes from C<(0, 0)> to C<($width, $height)>.
+
+=cut
+
has 'origin' => (
- is => 'ro',
- does => 'Maybe[LaTeX::TikZ::Point]',
+ is => 'rw',
+ isa => 'LaTeX::TikZ::Point::Autocoerce',
+ coerce => 1,
);
-my $find_mods;
-$find_mods = do {
- no warnings 'recursion';
+=head1 METHODS
- Sub::Name::subname('find_mods' => sub {
- my ($set, $layers, $others) = @_;
+=head2 C<id>
+
+An unique identifier of the formatter object.
+
+=cut
+
+sub id {
+ my $tikz = shift;
+
+ my $origin = $tikz->origin;
+ if (defined $origin) {
+ my ($x, $y) = map $origin->$_, qw/x y/;
+ $origin = "($x;$y)";
+ } else {
+ $origin = "(0;0)";
+ }
+
+ join $;, map {
+ defined() ? "$_" : '(undef)';
+ } map($tikz->$_, qw/unit format scale width height/), $origin;
+}
+
+=head2 C<render @sets>
+
+Processes all the L<LaTeX::TikZ::Set> objects given in C<@sets> to produce the actual TikZ code to insert in the LaTeX file.
+First, all the mods applied to the sets and their subsets are collected, and a declaration is emitted if needed for each of them by calling L<LaTeX::TikZ::Mod/declare>.
+Then, the image code is generated for each set.
+
+This method returns a list of array references :
- for ($set->mods) {
- if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
- push @$layers, $_;
- } else {
- push @$others, $_;
+=over 4
+
+=item *
+
+The first one contains the header lines to include between the C<\documentclass> and the C<\begin{document}>.
+
+=item *
+
+The second one contains the mod declaration lines to put inside the document, between C<\begin{document}> and C<\end{document}>.
+
+=item *
+
+Finally, there's one array reference for each given TikZ set, which contain the lines for the actual TikZ pictures.
+
+=back
+
+The lines returned by L</render> don't end with a line feed.
+
+ my ($header, $declarations, $set1_body, $set2_body) = $formatter->render($set1, $set2);
+
+ open my $tex, '>', 'test.tex' or die "open('>test.tex'): $!";
+
+ print $tex "$_\n" for (
+ "\\documentclass[12pt]{article}",
+ @$header,
+ "\\begin{document}",
+ "\\pagestyle{empty}",
+ @$declarations,
+ "First set :"
+ "\\begin{center}",
+ @$set1_body,
+ "\\end{center}",
+ "Second set :"
+ "\\begin{center}",
+ @$set2_body,
+ "\\end{center}",
+ "\\end{document}",
+ );
+
+=cut
+
+my $find_mods = do {
+ our %seen;
+
+ my $find_mods_rec;
+ $find_mods_rec = do {
+ no warnings 'recursion';
+
+ Sub::Name::subname('find_mods_rec' => sub {
+ my ($set, $layers, $others) = @_;
+
+ for ($set->mods) {
+ my $tag = $_->tag;
+ next if $seen{$tag}++;
+
+ if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
+ push @$layers, $_;
+ } else {
+ push @$others, $_;
+ }
}
- }
- my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
- ? $set->kids
- : $set->isa('LaTeX::TikZ::Set::Path')
- ? $set->ops
- : ();
+ my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
+ ? $set->kids
+ : $set->isa('LaTeX::TikZ::Set::Path')
+ ? $set->ops
+ : ();
- $find_mods->($_, $layers, $others) for @subsets;
- })
+ $find_mods_rec->($_, $layers, $others) for @subsets;
+ });
+ };
+
+ Sub::Name::subname('find_mods' => sub {
+ local %seen = ();
+
+ $find_mods_rec->(@_);
+ });
};
+my $translate;
+
sub render {
- my $tikz = shift;
+ my ($tikz, @sets) = @_;
+
+ unless ($translate) {
+ require LaTeX::TikZ::Functor;
+ $translate = LaTeX::TikZ::Functor->new(
+ rules => [
+ 'LaTeX::TikZ::Set::Point' => sub {
+ my ($functor, $set, $v) = @_;
+
+ $set->new(
+ point => [
+ $set->x + $v->x,
+ $set->y + $v->y,
+ ],
+ label => $set->label,
+ pos => $set->pos,
+ );
+ },
+ ],
+ );
+ }
- my $seq = LaTeX::TikZ::Set::Sequence->new(
- kids => \@_,
- );
+ my $origin = $tikz->origin;
+ if (defined $origin) {
+ @sets = map $_->$translate($origin), @sets;
+ }
my (@layers, @other_mods);
+ my $seq = LaTeX::TikZ::Set::Sequence->new(kids => \@sets);
$find_mods->($seq, \@layers, \@other_mods);
- my $o = $tikz->origin;
- $seq = $seq->translate($o) if defined $o;
-
my $w = $tikz->width;
my $h = $tikz->height;
my $canvas = '';
if (defined $w and defined $h) {
require LaTeX::TikZ::Set::Rectangle;
- $seq->clip(LaTeX::TikZ::Set::Rectangle->new(
- from => 0,
- width => $w,
- height => $h,
- ));
+ for (@sets) {
+ $_->clip(LaTeX::TikZ::Set::Rectangle->new(
+ from => 0,
+ width => $w,
+ height => $h,
+ ));
+ }
$_ = $tikz->len($_) for $w, $h;
$canvas = ",papersize={$w,$h},body={$w,$h}";
}
push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
push @decls, $_->declare($tikz) for @other_mods;
- my @content = (
+ my @bodies = map [
"\\begin{tikzpicture}",
- @{ $seq->draw($tikz) },
+ @{ $_->draw($tikz) },
"\\end{tikzpicture}",
- );
+ ], @sets;
- return \@header, \@decls, \@content;
+ return \@header, \@decls, @bodies;
}
+=head2 C<len $len>
+
+Format the given length according to the formatter options.
+
+=cut
+
sub len {
my ($tikz, $len) = @_;
sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
}
+=head2 C<angle $theta>
+
+Format the given angle (in radians) according to the formatter options.
+
+=cut
+
sub angle {
my ($tikz, $a) = @_;
sprintf $tikz->format, POSIX::ceil($a);
}
+=head2 C<label $name, $pos>
+
+Returns the TikZ code for a point labeled C<$name> at position C<$pos> according to the formatter options.
+
+=cut
+
sub label {
my ($tikz, $name, $pos) = @_;
my $scale = sprintf '%0.2f', $tikz->scale / 5;
- "node[scale=$scale,$pos] {\$$name\$}";
+ "node[scale=$scale,$pos] {$name}";
}
+=head2 C<thickness>
+
+Format the given line thickness according to the formatter options.
+
+=cut
+
sub thickness {
my ($tikz, $width) = @_;
0.8 * $width * ($tikz->scale / 5);
}
-use LaTeX::TikZ::Interface formatter => sub {
- shift;
+LaTeX::TikZ::Interface->register(
+ formatter => sub {
+ shift;
- __PACKAGE__->new(@_);
-};
+ __PACKAGE__->new(@_);
+ },
+);
__PACKAGE__->meta->make_immutable;
+=head1 SEE ALSO
+
+L<LaTeX::TikZ>.
+
=head1 AUTHOR
Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.