]> git.vpit.fr Git - perl/modules/LaTeX-TikZ.git/blob - lib/LaTeX/TikZ/Mod/Pattern/Lines.pm
Initial commit
[perl/modules/LaTeX-TikZ.git] / lib / LaTeX / TikZ / Mod / Pattern / Lines.pm
1 package LaTeX::TikZ::Mod::Pattern::Lines;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 LaTeX::TikZ::Mod::Pattern::Lines - An hatched pattern modifier.
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 extends 'LaTeX::TikZ::Mod::Pattern';
26
27 enum 'LaTeX::TikZ::Mod::Pattern::Direction' => (
28  'horizontal', 'vertical', 'north east', 'north west',
29 );
30
31 has 'direction' => (
32  is      => 'ro',
33  isa     => 'LaTeX::TikZ::Mod::Pattern::Direction',
34  default => 'horizontal',
35 );
36
37 has 'line_width' => (
38  is      => 'ro',
39  isa     => 'Int',
40  default => 1,
41 );
42
43 has 'space_width' => (
44  is      => 'ro',
45  isa     => 'Int',
46  default => 1,
47 );
48
49 my $W = Sub::Name::subname('WIDTH' => sub { sprintf '#WIDTH=%0.1f#', @_ });
50
51 my $forge_template = Sub::Name::subname('forge_template' => sub {
52  my ($direction, $line_width, $space_width) = @_;
53
54  my ($low_left, $up_right, $tile_size, $line_begin, $line_end);
55  my ($width, $half_width, $shadow_min, $shadow_max);
56
57  $width      = $W->($space_width);
58  $half_width = $W->($space_width / 2);
59
60  $shadow_min = $W->(- $line_width);
61  $shadow_max = $W->($space_width + $line_width);
62  $line_width = $W->($line_width);
63
64  $low_left   = "\\pgfqpoint{$shadow_min}{$shadow_min}";
65  $up_right   = "\\pgfqpoint{$shadow_max}{$shadow_max}";
66  $tile_size  = "\\pgfqpoint{$width}{$width}";
67
68  if ($direction =~ /^(?:horizontal|vertical)$/) {
69
70   if ($direction eq 'horizontal') {
71    $line_begin = "\\pgfqpoint{$shadow_min}{$half_width}";
72    $line_end   = "\\pgfqpoint{$shadow_max}{$half_width}";
73   } else {
74    $line_begin = "\\pgfqpoint{$half_width}{$shadow_min}";
75    $line_end   = "\\pgfqpoint{$half_width}{$shadow_max}";
76   }
77
78  } elsif ($direction =~ /^north (?:east|west)$/) {
79
80   if ($direction eq 'north east') {
81    $line_begin = "\\pgfqpoint{$shadow_min}{$shadow_min}";
82    $line_end   = "\\pgfqpoint{$shadow_max}{$shadow_max}";
83   } else {
84    $line_begin = "\\pgfqpoint{$shadow_min}{$shadow_max}";
85    $line_end   = "\\pgfqpoint{$shadow_max}{$shadow_min}";
86   }
87
88  } else {
89   return;
90  }
91
92  <<" PATTERN";
93 \\pgfdeclarepatternformonly{#NAME#}{$low_left}{$up_right}{$tile_size}{%
94  \\pgfsetlinewidth{$line_width}
95  \\pgfpathmoveto{$line_begin}
96  \\pgfpathlineto{$line_end}
97  \\pgfusepath{stroke}
98 }
99  PATTERN
100 });
101
102 around 'BUILDARGS' => sub {
103  my ($orig, $class, %args);
104
105  confess('Can\'t specify an explicit template for a '. __PACKAGE__ .' pattern')
106                                                       if exists $args{template};
107
108  my @params = qw/direction line_width space_width/;
109
110  my $meta = $class->meta;
111  for (@params) {
112   my $attr = $meta->find_attribute_by_name($_);
113   $args{$_} = $attr->default if $attr->has_default and not exists $args{$_};
114   $attr->type_constraint->assert_valid($args{$_});
115  }
116
117  $args{template} = $forge_template->(@args{@params});
118
119  $class->$orig(%args);
120 };
121
122 sub tag { join '/', ref $_[0], $_[0]->direction }
123
124 sub cover {
125  my ($this, $other) = @_;
126
127  LaTeX::TikZ::Tools::numeq($this->line_width, $other->line_width) or return 0;
128
129  my $ratio = $other->space_width / $this->space_width;
130
131  return LaTeX::TikZ::Tools::numeq($ratio, int $ratio);
132 }
133
134 __PACKAGE__->meta->make_immutable;
135
136 =head1 AUTHOR
137
138 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
139
140 You can contact me by mail or on C<irc.perl.org> (vincent).
141
142 =head1 BUGS
143
144 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>.
145 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
146
147 =head1 SUPPORT
148
149 You can find documentation for this module with the perldoc command.
150
151     perldoc LaTeX::TikZ
152
153 =head1 COPYRIGHT & LICENSE
154
155 Copyright 2010 Vincent Pit, all rights reserved.
156
157 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
158
159 =cut
160
161 1; # End of LaTeX::TikZ::Mod::Pattern::Lines