From: Vincent Pit Date: Mon, 31 Jan 2011 15:38:45 +0000 (+0100) Subject: Introduce the ->begin and ->end path methods X-Git-Tag: rt87282~16 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=6832c809e7591e1c3e2809654f814c298d28d2ef;p=perl%2Fmodules%2FLaTeX-TikZ.git Introduce the ->begin and ->end path methods --- diff --git a/lib/LaTeX/TikZ/Set/Chain.pm b/lib/LaTeX/TikZ/Set/Chain.pm index 8795de1..e12587e 100644 --- a/lib/LaTeX/TikZ/Set/Chain.pm +++ b/lib/LaTeX/TikZ/Set/Chain.pm @@ -195,6 +195,32 @@ sub path { return $path; } +=head2 C + +=cut + +sub begin { + my $set = shift; + + my @kids = $set->kids; + return undef unless @kids; + + $kids[0]->begin; +} + +=head2 C + +=cut + +sub end { + my $set = shift; + + my @kids = $set->kids; + return undef unless @kids; + + $kids[-1]->end; +} + LaTeX::TikZ::Interface->register( join => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Circle.pm b/lib/LaTeX/TikZ/Set/Circle.pm index 54c1540..0de14df 100644 --- a/lib/LaTeX/TikZ/Set/Circle.pm +++ b/lib/LaTeX/TikZ/Set/Circle.pm @@ -73,6 +73,18 @@ sub path { $set->center->path(@_) . ' circle (' . $tikz->len($set->radius) . ')'; } +=head2 C + +=cut + +sub begin { $_[0]->center->begin } + +=head2 C + +=cut + +sub end { $_[0]->center->end } + LaTeX::TikZ::Interface->register( circle => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Line.pm b/lib/LaTeX/TikZ/Set/Line.pm index 4054bfd..faa4ccb 100644 --- a/lib/LaTeX/TikZ/Set/Line.pm +++ b/lib/LaTeX/TikZ/Set/Line.pm @@ -70,6 +70,18 @@ sub path { $set->from->path(@_) . ' -- ' . $set->to->path(@_); } +=head2 C + +=cut + +sub begin { $_[0]->from->begin } + +=head2 C + +=cut + +sub end { $_[0]->to->end } + LaTeX::TikZ::Interface->register( line => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Path.pm b/lib/LaTeX/TikZ/Set/Path.pm index f7a5fb2..081f35b 100644 --- a/lib/LaTeX/TikZ/Set/Path.pm +++ b/lib/LaTeX/TikZ/Set/Path.pm @@ -33,7 +33,7 @@ with 'LaTeX::TikZ::Set'; =head1 METHODS -This method is required by the interface : +These methods are required by the interface : =over 4 @@ -43,12 +43,26 @@ C Returns the TikZ code that builds a path out of the current set object as a string formatted by the L object C<$formatter>. +=item * + +C + +Returns a L object pointing to the beginning of the path, or C if this path has no beginning. + +=item * + +C + +A L object pointing to the end of the path, or C if this path has no end. + =back =cut requires qw< path + begin + end >; =head2 C diff --git a/lib/LaTeX/TikZ/Set/Point.pm b/lib/LaTeX/TikZ/Set/Point.pm index d5761d1..4cf7992 100644 --- a/lib/LaTeX/TikZ/Set/Point.pm +++ b/lib/LaTeX/TikZ/Set/Point.pm @@ -114,6 +114,18 @@ sub path { $path; } +=head2 C + +=cut + +sub begin { $_[0]->point } + +=head2 C + +=cut + +sub end { $_[0]->point } + LaTeX::TikZ::Interface->register( point => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Polyline.pm b/lib/LaTeX/TikZ/Set/Polyline.pm index a1992a5..987c321 100644 --- a/lib/LaTeX/TikZ/Set/Polyline.pm +++ b/lib/LaTeX/TikZ/Set/Polyline.pm @@ -83,6 +83,32 @@ sub path { ($set->closed ? 'cycle' : ()); } +=head2 C + +=cut + +sub begin { + my $set = shift; + + my @points = $set->points; + return undef unless @points; + + $points[0]->begin; +} + +=head2 C + +=cut + +sub end { + my $set = shift; + + my @points = $set->points; + return undef unless @points; + + $points[-1]->end; +} + LaTeX::TikZ::Interface->register( polyline => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Raw.pm b/lib/LaTeX/TikZ/Set/Raw.pm index 68f4025..8641287 100644 --- a/lib/LaTeX/TikZ/Set/Raw.pm +++ b/lib/LaTeX/TikZ/Set/Raw.pm @@ -50,6 +50,18 @@ has 'content' => ( sub path { $_[0]->content } +=head2 C + +=cut + +sub begin { undef } + +=head2 C + +=cut + +sub end { undef } + LaTeX::TikZ::Interface->register( raw => sub { shift; diff --git a/lib/LaTeX/TikZ/Set/Rectangle.pm b/lib/LaTeX/TikZ/Set/Rectangle.pm index 2e290db..838ff95 100644 --- a/lib/LaTeX/TikZ/Set/Rectangle.pm +++ b/lib/LaTeX/TikZ/Set/Rectangle.pm @@ -92,6 +92,18 @@ sub path { $set->from->path(@_) . ' rectangle ' . $set->to->path(@_); } +=head2 C + +=cut + +sub begin { $_[0]->from->begin } + +=head2 C + +=cut + +sub end { $_[0]->to->end } + my $meta = __PACKAGE__->meta; my $tc1 = $meta->find_attribute_by_name('from')->type_constraint; my $tc2 = $meta->find_attribute_by_name('to')->type_constraint; diff --git a/lib/LaTeX/TikZ/Set/Union.pm b/lib/LaTeX/TikZ/Set/Union.pm index 228a6ff..13b5dc6 100644 --- a/lib/LaTeX/TikZ/Set/Union.pm +++ b/lib/LaTeX/TikZ/Set/Union.pm @@ -78,6 +78,32 @@ sub path { join ' ', map $_->path(@_), $set->kids; } +=head2 C + +=cut + +sub begin { + my $set = shift; + + my @kids = $set->kids; + return undef unless @kids; + + $kids[0]->begin; +} + +=head2 C + +=cut + +sub end { + my $set = shift; + + my @kids = $set->kids; + return undef unless @kids; + + $kids[-1]->end; +} + LaTeX::TikZ::Interface->register( union => sub { shift; diff --git a/t/11-point.t b/t/11-point.t index a8481ec..4b31a50 100644 --- a/t/11-point.t +++ b/t/11-point.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 8 + 2 * 8; +use Test::More tests => 8 + 2 * 8 + 2 * (1 + 2 * 3); use Math::Complex; @@ -97,3 +97,25 @@ is $@, '', check $p, 'a labeled positioned point', <<'RES'; \draw (2cm,-2cm) [fill] circle (0.4pt) node[scale=0.20,below right] {bar} ; RES + +my $union = eval { + Tikz->union( + Tikz->point([ 0, -1 ]), + Tikz->raw("foo"), + Tikz->point(9) + ); +}; +is $@, '', 'creating a simple union path doesn\'t croak'; +is_point_ok $union->begin, 0, -1, 'beginning of a simple union path'; +is_point_ok $union->end, 9, 0, 'end of a simple union path'; + +my $path = eval { + Tikz->union( + Tikz->join('--' => 1, 2, 3), + $union, + Tikz->chain(5 => '--' => [ 6, 1 ]), + ); +}; +is $@, '', 'creating a complex union path doesn\'t croak'; +is_point_ok $path->begin, 1, 0, 'beginning of a complex union path'; +is_point_ok $path->end, 6, 1, 'end of a complex union path'; diff --git a/t/lib/LaTeX/TikZ/TestHelper.pm b/t/lib/LaTeX/TikZ/TestHelper.pm index deb1dff..040fe82 100644 --- a/t/lib/LaTeX/TikZ/TestHelper.pm +++ b/t/lib/LaTeX/TikZ/TestHelper.pm @@ -8,7 +8,7 @@ use Test::More (); use Any::Moose 'Exporter'; any_moose('Exporter')->setup_import_methods( - as_is => [ qw ], + as_is => [ qw ], ); my $tikz; @@ -40,4 +40,16 @@ sub check { return $head, $decl, $body; } +sub is_point_ok { + my ($p, $x, $y, $desc) = @_; + + my $ok = Test::More::isa_ok($p, 'LaTeX::TikZ::Point', "$desc isa point"); + if ($ok) { + Test::More::cmp_ok($p->x, '==', $x, "$desc x coordinate is right"); + Test::More::cmp_ok($p->y, '==', $y, "$desc y coordinate is right"); + } else { + Test::More::fail("$desc placeholder $_") for 1, 2; + } +} + 1;