1 package LaTeX::TikZ::Mod::Pattern::Lines;
8 LaTeX::TikZ::Mod::Pattern::Lines - An hatched pattern modifier.
16 our $VERSION = '0.01';
20 use LaTeX::TikZ::Tools;
23 use Any::Moose 'Util::TypeConstraints';
27 This class inherits the L<LaTeX::TikZ::Mod::Pattern> class and its L</tag>, L</covers>, L<LaTeX::TikZ::Mod::Pattern/declare> and L<LaTeX::TikZ::Mod::Pattern/apply> methods.
31 extends 'LaTeX::TikZ::Mod::Pattern';
39 enum 'LaTeX::TikZ::Mod::Pattern::Direction' => (
40 'horizontal', 'vertical', 'north east', 'north west',
45 isa => 'LaTeX::TikZ::Mod::Pattern::Direction',
46 default => 'horizontal',
55 isa => subtype('Num' => where { LaTeX::TikZ::Tools::numcmp($_, 0) >= 0 }),
63 has 'space_width' => (
65 isa => subtype('Num' => where { LaTeX::TikZ::Tools::numcmp($_, 0) >= 0 }),
69 my $W = Sub::Name::subname('WIDTH' => sub { sprintf '#WIDTH=%0.1f#', @_ });
71 my $forge_template = Sub::Name::subname('forge_template' => sub {
72 my ($direction, $line_width, $space_width) = @_;
74 my ($low_left, $up_right, $tile_size, $line_begin, $line_end);
75 my ($width, $half_width, $shadow_min, $shadow_max);
77 $width = $W->($space_width);
78 $half_width = $W->($space_width / 2);
80 $shadow_min = $W->(- $line_width);
81 $shadow_max = $W->($space_width + $line_width);
82 $line_width = $W->($line_width);
84 $low_left = "\\pgfqpoint{$shadow_min}{$shadow_min}";
85 $up_right = "\\pgfqpoint{$shadow_max}{$shadow_max}";
86 $tile_size = "\\pgfqpoint{$width}{$width}";
88 if ($direction =~ /^(?:horizontal|vertical)$/) {
90 if ($direction eq 'horizontal') {
91 $line_begin = "\\pgfqpoint{$shadow_min}{$half_width}";
92 $line_end = "\\pgfqpoint{$shadow_max}{$half_width}";
94 $line_begin = "\\pgfqpoint{$half_width}{$shadow_min}";
95 $line_end = "\\pgfqpoint{$half_width}{$shadow_max}";
98 } elsif ($direction =~ /^north (?:east|west)$/) {
100 if ($direction eq 'north east') {
101 $line_begin = "\\pgfqpoint{$shadow_min}{$shadow_min}";
102 $line_end = "\\pgfqpoint{$shadow_max}{$shadow_max}";
104 $line_begin = "\\pgfqpoint{$shadow_min}{$shadow_max}";
105 $line_end = "\\pgfqpoint{$shadow_max}{$shadow_min}";
113 "\\pgfdeclarepatternformonly{#NAME#}{$low_left}{$up_right}{$tile_size}{",
114 "\\pgfsetlinewidth{$line_width}",
115 "\\pgfpathmoveto{$line_begin}",
116 "\\pgfpathlineto{$line_end}",
117 "\\pgfusepath{stroke}",
122 around 'BUILDARGS' => sub {
123 my ($orig, $class, %args) = @_;
125 confess('Can\'t specify an explicit template for a '. __PACKAGE__ .' pattern')
126 if exists $args{template};
128 my @params = qw/direction line_width space_width/;
130 my $meta = $class->meta;
132 my $attr = $meta->find_attribute_by_name($_);
133 $args{$_} = $attr->default if $attr->has_default and not exists $args{$_};
134 $attr->type_constraint->assert_valid($args{$_});
137 $args{template} = $forge_template->(@args{@params});
139 $class->$orig(%args);
148 sub tag { join '/', ref $_[0], $_[0]->direction }
155 my ($this, $other) = @_;
157 LaTeX::TikZ::Tools::numeq($this->line_width, $other->line_width) or return 0;
159 my $ratio = $other->space_width / $this->space_width;
161 return LaTeX::TikZ::Tools::numeq($ratio, int $ratio);
164 __PACKAGE__->meta->make_immutable;
168 L<LaTeX::TikZ>, L<LaTeX::TikZ::Mod::Pattern>.
172 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
174 You can contact me by mail or on C<irc.perl.org> (vincent).
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.
183 You can find documentation for this module with the perldoc command.
187 =head1 COPYRIGHT & LICENSE
189 Copyright 2010 Vincent Pit, all rights reserved.
191 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
195 1; # End of LaTeX::TikZ::Mod::Pattern::Lines