1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# $Id: align.t,v 1.19 2003/02/21 03:07:10 mgjv Exp $
use lib ".", "..";
BEGIN{ require "t/lib.pl" }
use Test::More tests => 21;
use GD;
BEGIN { use_ok "GD::Text::Align" };
use constant PI => 4 * atan2(1,1);
# Create an image
$gd = GD::Image->new(200,200);
ok (defined $gd, "GD returns an object");
$gd->colorAllocate(255,255,255);
$black = $gd->colorAllocate(0,0,0);
is ($gd->colorsTotal, 2, "Colors allocation");
# Test the default setup
$t = GD::Text::Align->new($gd);
ok (defined $t, "GD::Text::Align returns object")
or diag($@);
$t->set_text('A string');
($w, $h, $cu, $cd) = $t->get(qw(width height char_up char_down));
ok ($w==48 && $h==13 && $cu==13 && $cd==0, "string returns right dim");
# Some alignments
$t->set_align('top', 'left');
$t->draw(100,10);
($x, $y) = $t->get(qw(x y));
ok ($x==100 && $y==10, "top left");
$t->set_align('center', 'right');
$t->draw(100,10);
($x, $y) = $t->get(qw(x y));
ok ($x==52 && $y==3.5, "center right");
$t->set_align('bottom','center');
$t->draw(100,20);
($x, $y) = $t->get(qw(x y));
ok ($x==76 && $y==7, "bottom center");
# Test loading of other builtin font
$t->set_font(gdGiantFont);
$t->set_align('bottom', 'right');
$t->draw(100,40);
($x, $y) = $t->get(qw(x y));
ok ($x==28 && $y==25, "builtin font");
# Test some angles, this method is not meant to be used by anyone but
# me :)
# This first test used to check for PI/4 exactly, but some installations
# of Perl on some platforms, with some flags would round that just
# slightly differently from what I expect and want.
$t->draw(100,40,PI/4 - 0.0001);
ok (!$t->_builtin_up, "angles test");
$t->draw(100,40,PI/4 + 0.0001);
ok ($t->_builtin_up, "angles test 2");
# And some bounding boxes
$t->set_align('bottom', 'left');
@bb = $t->bounding_box(100,100);
is ("@bb", "100 100 172 100 172 85 100 85", "bounding boxes");
@bb = $t->bounding_box(100,100,PI/2);
is ("@bb", "100 100 100 28 85 28 85 100", "bounding boxes 2");
# Constructor test
$t = GD::Text::Align->new($gd,
valign => 'top',
halign => 'left',
text => 'Banana Boat',
colour => $black,
font => gdGiantFont,
);
@bb = $t->draw(10,10);
is ("@bb", "10 25 109 25 109 10 10 10", "constructor test");
# Test a '0' string
$t = GD::Text::Align->new($gd,
text => '0',
font => gdLargeFont,
valign => 'bottom',
halign => 'center',
colour => $black);
@bb = $t->draw(100, 200);
is ("@bb", "96 200 104 200 104 184 96 184", "false string");
# TTF fonts
SKIP:
{
# skip
skip "No ttf", 6 unless ($t->can_do_ttf);
$t = GD::Text::Align->new($gd,
valign => 'top',
halign => 'left',
text => 'Banana Boat',
colour => 1,
ptsize => 18,
);
ok ($t->set_font('Dustismo_Sans'), "ttf font Dustismo Sans")
or diag($t->error);
skip "Some TTF tests disabled: Freetype inconsistent", 5;
@bb = $t->draw(10,40);
ok (aeq(\@bb, [qw"11 57 137 57 137 41 11 41"], 1), "drawing")
or diag("bb = @bb");
ok ($t->set_font('Dustismo_Sans', 12), "ttf Dustismo Sans 12pt")
or diag($t->error);
$t->set_align('bottom', 'left');
@bb = $t->bounding_box(100,100);
ok (aeq(\@bb, [qw"101 97 190 97 190 86 101 86"], 1), "bottom left align")
or diag("bb = @bb");
$t->set_align('top', 'center');
@bb = $t->bounding_box(100,100, 4*PI/3);
ok (aeq(\@bb, [qw"111 55 67 132 77 137 120 60"], 1), "top center align")
or diag("bb = @bb");
@bb = $t->draw(140,100,4*PI/3);
ok (aeq(\@bb, [qw"151 55 107 132 117 137 160 60"], 1), "last drawing")
or diag("bb = @bb");
}
__END__
# only during testing of the test scripts
$gd->colorAllocate(127,127,127);
$gd->line(100,0,100,200,2);
$gd->line(0,100,200,100,2);
open(GD, ">/tmp/align.png") or die $!;
binmode GD;
print GD $gd->png;
close(GD)
|