]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Formatter.pm
44af1c8bdc1d634542755f8a066ed9df5d770de4
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Formatter.pm
1 package LaTeX::TikZ::Formatter;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Formatter - LaTeX::TikZ formatter object.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 use Sub::Name ();
19
20 use LaTeX::TikZ::Tools;
21
22 use Any::Moose;
23 use Any::Moose 'Util::TypeConstraints';
24
25 has 'unit' => (
26  is      => 'ro',
27  isa     => enum([ qw/cm pt/ ]),
28  default => 'cm',
29 );
30
31 has 'format' => (
32  is      => 'ro',
33  isa     => 'Str',
34  default => '%s',
35 );
36
37 has 'scale' => (
38  is      => 'ro',
39  isa     => 'Num',
40  default => 1,
41 );
42
43 has 'width' => (
44  is  => 'ro',
45  isa => 'Maybe[Num]',
46 );
47
48 has 'height' => (
49  is  => 'ro',
50  isa => 'Maybe[Num]',
51 );
52
53 has 'origin' => (
54  is   => 'ro',
55  does => 'Maybe[LaTeX::TikZ::Point]',
56 );
57
58 my $find_mods;
59 $find_mods = do {
60  no warnings 'recursion';
61
62  Sub::Name::subname('find_mods' => sub {
63   my ($set, $layers, $others) = @_;
64
65   for ($set->mods) {
66    if ($_->isa('LaTeX::TikZ::Mod::Layer')) {
67     push @$layers, $_;
68    } else {
69     push @$others, $_;
70    }
71   }
72
73   my @subsets = $set->isa('LaTeX::TikZ::Set::Sequence')
74                 ? $set->kids
75                 : $set->isa('LaTeX::TikZ::Set::Path')
76                   ? $set->ops
77                   : ();
78
79   $find_mods->($_, $layers, $others) for @subsets;
80  })
81 };
82
83 sub render {
84  my $tikz = shift;
85
86  my $seq = LaTeX::TikZ::Set::Sequence->new(
87   kids => \@_,
88  );
89
90  my (@layers, @other_mods);
91  $find_mods->($seq, \@layers, \@other_mods);
92
93  my $o = $tikz->origin;
94  $seq  = $seq->translate($o) if defined $o;
95
96  my $w = $tikz->width;
97  my $h = $tikz->height;
98  my $canvas = '';
99  if (defined $w and defined $h) {
100   require LaTeX::TikZ::Set::Rectangle;
101   $seq->clip(LaTeX::TikZ::Set::Rectangle->new(
102    from   => 0,
103    width  => $w,
104    height => $h,
105   ));
106   $_ = $tikz->len($_) for $w, $h;
107   $canvas = ",papersize={$w,$h},body={$w,$h}";
108  }
109
110  my @header = (
111   "\\usepackage[pdftex,hcentering,vcentering$canvas]{geometry}",
112   "\\usepackage{tikz}",
113   "\\usetikzlibrary{patterns}",
114  );
115
116  my @decls;
117  push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if  @layers;
118  push @decls, $_->declare($tikz)                        for @other_mods;
119
120  my @content = (
121   "\\begin{tikzpicture}",
122   @{ $seq->draw($tikz) },
123   "\\end{tikzpicture}",
124  );
125
126  return \@header, \@decls, \@content;
127 }
128
129 sub len {
130  my ($tikz, $len) = @_;
131
132  $len = 0 if LaTeX::TikZ::Tools::numeq($len, 0);
133
134  sprintf $tikz->format . $tikz->unit, $len * $tikz->scale;
135 }
136
137 sub angle {
138  my ($tikz, $a) = @_;
139
140  $a = ($a * 180) / CORE::atan2(0, -1);
141  $a += 360 if LaTeX::TikZ::Tools::numcmp($a, 0) < 0;
142
143  require POSIX;
144  sprintf $tikz->format, POSIX::ceil($a);
145 }
146
147 sub label {
148  my ($tikz, $name, $pos) = @_;
149
150  my $scale = sprintf '%0.2f', $tikz->scale / 5;
151
152  "node[scale=$scale,$pos] {\$$name\$}";
153 }
154
155 sub thickness {
156  my ($tikz, $width) = @_;
157
158  # width=1 is 0.4 points for a scale of 2.5
159  0.8 * $width * ($tikz->scale / 5);
160 }
161
162 use LaTeX::TikZ::Interface formatter => sub {
163  shift;
164
165  __PACKAGE__->new(@_);
166 };
167
168 __PACKAGE__->meta->make_immutable;
169
170 =head1 AUTHOR
171
172 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
173
174 You can contact me by mail or on C<irc.perl.org> (vincent).
175
176 =head1 BUGS
177
178 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>.
179 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
180
181 =head1 SUPPORT
182
183 You can find documentation for this module with the perldoc command.
184
185     perldoc LaTeX::TikZ
186
187 =head1 COPYRIGHT & LICENSE
188
189 Copyright 2010 Vincent Pit, all rights reserved.
190
191 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
192
193 =cut
194
195 1; # End of LaTeX::TikZ::Formatter