our $VERSION = '0.01';
+=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 Any::Moose;
use Any::Moose 'Util::TypeConstraints';
+=head1 ATTRIBUTES
+
+=head2 C<unit>
+
+=cut
+
has 'unit' => (
is => 'ro',
isa => enum([ qw/cm pt/ ]),
default => 'cm',
);
+=head2 C<format>
+
+=cut
+
has 'format' => (
is => 'ro',
isa => 'Str',
default => '%s',
);
+=head2 C<scale>
+
+=cut
+
has 'scale' => (
is => 'rw',
isa => 'Num',
default => 1,
);
+=head2 C<width>
+
+=cut
+
has 'width' => (
is => 'rw',
isa => 'Maybe[Num]',
);
+=head2 C<height>
+
+=cut
+
has 'height' => (
is => 'rw',
isa => 'Maybe[Num]',
);
+=head2 C<origin>
+
+=cut
+
has 'origin' => (
is => 'rw',
isa => 'LaTeX::TikZ::Point::Autocoerce',
coerce => 1,
);
+=head1 METHODS
+
+=head2 C<id>
+
+=cut
+
sub id {
my $tikz = shift;
} map($tikz->$_, qw/unit format scale width height/), $origin;
}
+=head2 C<render>
+
+=cut
+
my $find_mods = do {
our %seen;
my $translate;
sub render {
- my $tikz = shift;
-
- my $seq = LaTeX::TikZ::Set::Sequence->new(
- kids => \@_,
- );
+ my ($tikz, @sets) = @_;
unless ($translate) {
require LaTeX::TikZ::Functor;
}
my $origin = $tikz->origin;
- $seq = $seq->$translate($origin) if defined $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 $w = $tikz->width;
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>
+
+=cut
+
sub len {
my ($tikz, $len) = @_;
sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
}
+=head2 C<angle>
+
+=cut
+
sub angle {
my ($tikz, $a) = @_;
sprintf $tikz->format, POSIX::ceil($a);
}
+=head2 C<label>
+
+=cut
+
sub label {
my ($tikz, $name, $pos) = @_;
"node[scale=$scale,$pos] {$name}";
}
+=head2 C<thickness>
+
+=cut
+
sub thickness {
my ($tikz, $width) = @_;