package LaTeX::TikZ::Formatter;

use strict;
use warnings;

=head1 NAME

LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

use Sub::Name ();

use LaTeX::TikZ::Tools;

use Any::Moose;
use Any::Moose 'Util::TypeConstraints';

has 'unit' => (
 is      => 'ro',
 isa     => enum([ qw/cm pt/ ]),
 default => 'cm',
);

has 'format' => (
 is      => 'ro',
 isa     => 'Str',
 default => '%s',
);

has 'scale' => (
 is      => 'ro',
 isa     => 'Num',
 default => 1,
);

has 'width' => (
 is  => 'ro',
 isa => 'Maybe[Num]',
);

has 'height' => (
 is  => 'ro',
 isa => 'Maybe[Num]',
);

has 'origin' => (
 is   => 'ro',
 does => 'Maybe[LaTeX::TikZ::Point]',
);

my $find_mods;
$find_mods = do {
 no warnings 'recursion';

 Sub::Name::subname('find_mods' => sub {
  my ($set, $layers, $others) = @_;

  for ($set->mods) {
   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
                  : ();

  $find_mods->($_, $layers, $others) for @subsets;
 })
};

sub render {
 my $tikz = shift;

 my $seq = LaTeX::TikZ::Set::Sequence->new(
  kids => \@_,
 );

 my (@layers, @other_mods);
 $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) {
  $seq->clip(Tikz->rectangle(Tikz->point(0) => [ $w, $h ]));
  $_ = $tikz->len($_) for $w, $h;
  $canvas = ",papersize={$w,$h},body={$w,$h}";
 }

 my @header = (
  "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
  "\\usepackage{tikz}",
  "\\usetikzlibrary{patterns}",
 );

 my @decls;
 push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if  @layers;
 push @decls, $_->declare($tikz)                        for @other_mods;

 my @content = (
  "\\begin{tikzpicture}",
  @{ $seq->draw($tikz) },
  "\\end{tikzpicture}",
 );

 return \@header, \@decls, \@content;
}

sub len {
 my ($tikz, $len) = @_;

 $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);

 sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
}

sub angle {
 my ($tikz, $a) = @_;

 $a = ($a * 180) / CORE::atan2(0, -1);
 $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;

 require POSIX;
 sprintf $tikz->format, POSIX::ceil($a);
}

sub label {
 my ($tikz, $name, $pos) = @_;

 my $scale = sprintf '%0.2f', $tikz->scale / 5;

 "node[scale=$scale,$pos] {\$$name\$}";
}

sub thickness {
 my ($tikz, $width) = @_;

 # width=1 is 0.4 points for a scale of 2.5
 0.8 * $width * ($tikz->scale / 5);
}

__PACKAGE__->meta->make_immutable;

=head1 AUTHOR

Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.

You can contact me by mail or on C<irc.perl.org> (vincent).

=head1 BUGS

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>.
I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc LaTeX::TikZ

=head1 COPYRIGHT & LICENSE

Copyright 2010 Vincent Pit, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1; # End of LaTeX::TikZ::Formatter
