From 220768dbcbab6f65910dc2691e9dae8aec659d69 Mon Sep 17 00:00:00 2001 From: mertz Date: Mon, 24 May 2004 19:55:03 +0000 Subject: adding three tests for text bounding boxes (far from complete!) --- Perl/t/Bbox.t | 90 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 19 deletions(-) (limited to 'Perl/t') diff --git a/Perl/t/Bbox.t b/Perl/t/Bbox.t index c2d08cc..238da00 100644 --- a/Perl/t/Bbox.t +++ b/Perl/t/Bbox.t @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # -# $Id: Bbox.t,v 1.1 2004-05-18 15:39:38 mertz Exp $ +# $Id: Bbox.t,v 1.2 2004-05-24 19:55:03 mertz Exp $ # Author: Christophe Mertz # @@ -9,7 +9,7 @@ BEGIN { if (!eval q{ - use Test::More tests => 2; + use Test::More tests => 5; 1; }) { print "# tests only work properly with installed Test::More module\n"; @@ -40,27 +40,77 @@ BEGIN { $mw = MainWindow->new(); -$zinc = $mw->Zinc(-width => 100, -height => 100); +$zinc = $mw->Zinc(-width => 400, -height => 400)->pack; like ($zinc, qr/^Tk::Zinc=HASH/ , "zinc has been created"); my $coords = [ [10,10], [40, 40] ]; my $font = $zinc->fontCreate('font20pixels', -size => -20); -my $t = $zinc->add('text', 1, - -font => 'font20pixels', - -alignment => 'center', - #-text => 'text', # an empty text - -position => [30,25], - ); - -ok(&similarFlatArray ([$zinc->bbox($t)], - [30, 25, 30, 25+20+3], - [4, 4, 4, 4], +#my @metrics = $zinc->fontMetrics('font20pixels'); +#print "metrics = @metrics\n"; +my $linespace = $zinc->fontMetrics('font20pixels', -linespace); + +my $txt1 = $zinc->add('text', 1, + -font => 'font20pixels', + -alignment => 'center', + #-text => 'text', # an empty text + -position => [30,25], + ); +#print "bbox=(", join(',', $zinc->bbox($txt1)),")\n"; + +ok(&similarFlatArray ([$zinc->bbox($txt1)], + [30,25, 30,25+$linespace], + [4, 4, 4, 4], ), "bbox of empty text"); -# print "bbox=(", join(',', $zinc->bbox($t)),")\n"; +my $width = $zinc->fontMeasure('font20pixels', 'dummy'); +print "width = $width\n"; +my $txt2 = $zinc->add('text', 1, + -font => 'font20pixels', + -alignment => 'left', + -text => 'dummy', + -position => [200,100], + ); +# print "bbox=(", join(',', $zinc->bbox($txt2)),")\n"; + +ok(&similarFlatArray ([$zinc->bbox($txt2)], + [200,100, 200+$width, 100+$linespace], + [4,4, 4,4 ], + ), + "bbox of 'dummy' text"); + +my $txt3 = $zinc->add('text', 1, + -font => 'font20pixels', + -alignment => 'center', + -text => 'dummy', + -position => [200,200], + ); +# print "bbox=(", join(',', $zinc->bbox($txt3)),")\n"; + +ok(&similarFlatArray ([$zinc->bbox($txt3)], + [200,200, 200+$width, 200+$linespace], + [4,4, 4,4 ], + ), + "bbox of 'dummy' aligned-centered text"); + +my $txt4 = $zinc->add('text', 1, + -font => 'font20pixels', + -anchor => 'center', + -text => 'dummy', + -position => [200,100], + ); +# print "bbox=(", join(',', $zinc->bbox($txt4)),")\n"; + +ok(&similarFlatArray ([$zinc->bbox($txt4)], + [200-$width/2,100-$linespace/2, 200+$width/2, 100+$linespace/2], + [4,4, 4,4 ], + ), + "bbox of 'dummy' centered text"); + + + sub similarPoints { my ($ref1, $ref2)= @_; @@ -94,6 +144,8 @@ sub similarPoints { return 1; } +## ref1 is the obtained array +## ref2 is the expected array sub similarFlatArray { my ($ref1, $ref2, $deltaref)= @_; diag ("waiting a reference for \$ref1"), return 0 unless ref ($ref1) eq 'ARRAY'; @@ -103,20 +155,20 @@ sub similarFlatArray { my @array1 = @{$ref1}; my @array2 = @{$ref2}; my @deltaarray = @{$deltaref}; - diag ("arrays for \$ref1 and \$ref2 and \$deltaref are not of same length,".$#array1.",".$#array2.",".$#deltaarray), return 0 + diag ("arrays obtained, expected and deltas are not of same length,".$#array1.",".$#array2.",".$#deltaarray), return 0 unless ($#array1 == $#array2) and ($#array2 == $#deltaarray); for my $i (0.. $#array1) { my $a = $array1[$i]; my $b = $array2[$i]; my $delta = $deltaarray[$i]; - diag ("waiting a numeric value for elt $i of \$ref1"), return 0 + diag ("waiting a numeric value for elt $i of obtained array"), return 0 unless &numerical($a); - diag ("waiting a numeric value for elt $i of \$ref2"), return 0 + diag ("waiting a numeric value for elt $i of expected array"), return 0 unless &numerical($b); - diag ("waiting a numeric value for elt $i of \$deltaref"), return 0 + diag ("waiting a numeric value for elt $i of deltas array"), return 0 unless &numerical($delta); - diag ("delta > $delta between elt $i of \$ref1 ($a) and \$ref2 ($b)"), return 0 + diag ("delta > $delta between elt $i of obtained array ($a) and expected array ($b)"), return 0 if (abs($a-$b) > $delta) ; } return 1; -- cgit v1.1