Permalink
Browse files

use the standard font tests for Freetype 1.x (TT) too

and fix the code to pass them
  • Loading branch information...
1 parent d6f02a5 commit 7e3298ec06ae0d1794cc6c006439ecc26f0ce7ed @tonycoz committed Feb 23, 2013
Showing with 87 additions and 20 deletions.
  1. +14 −9 Imager.xs
  2. +1 −0 MANIFEST
  3. +39 −7 lib/Imager/Font/Truetype.pm
  4. +4 −4 t/t35ttfont.t
  5. +29 −0 t/t37std.t
View
@@ -2289,7 +2289,7 @@ i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
void
-i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
+i_tt_bbox(handle,point,str_sv,utf8)
Imager::Font::TT handle
double point
SV* str_sv
@@ -2301,11 +2301,11 @@ i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
STRLEN len;
int i;
PPCODE:
+ str = SvPV(str_sv, len);
#ifdef SvUTF8
if (SvUTF8(ST(2)))
utf8 = 1;
#endif
- str = SvPV(str_sv, len);
if ((rc=i_tt_bbox(handle,point,str,len,cords, utf8))) {
EXTEND(SP, rc);
for (i = 0; i < rc; ++i) {
@@ -2325,11 +2325,12 @@ i_tt_has_chars(handle, text_sv, utf8)
size_t count;
size_t i;
PPCODE:
+ i_clear_error();
+ text = SvPV(text_sv, len);
#ifdef SvUTF8
if (SvUTF8(text_sv))
utf8 = 1;
#endif
- text = SvPV(text_sv, len);
work = mymalloc(len);
count = i_tt_has_chars(handle, text, len, utf8, work);
if (GIMME_V == G_ARRAY) {
@@ -2372,34 +2373,38 @@ i_tt_glyph_name(handle, text_sv, utf8 = 0)
size_t len;
size_t outsize;
char name[255];
+ SSize_t count = 0;
PPCODE:
+ i_clear_error();
+ text = SvPV(text_sv, work_len);
#ifdef SvUTF8
if (SvUTF8(text_sv))
utf8 = 1;
#endif
- text = SvPV(text_sv, work_len);
len = work_len;
while (len) {
unsigned long ch;
if (utf8) {
ch = i_utf8_advance(&text, &len);
if (ch == ~0UL) {
i_push_error(0, "invalid UTF8 character");
- break;
+ XSRETURN_EMPTY;
}
}
else {
ch = *text++;
--len;
}
- EXTEND(SP, 1);
+ EXTEND(SP, count);
if ((outsize = i_tt_glyph_name(handle, ch, name, sizeof(name))) != 0) {
- PUSHs(sv_2mortal(newSVpv(name, 0)));
+ ST(count) = sv_2mortal(newSVpv(name, 0));
}
else {
- PUSHs(&PL_sv_undef);
- }
+ ST(count) = &PL_sv_undef;
+ }
+ ++count;
}
+ XSRETURN(count);
#endif
View
@@ -343,6 +343,7 @@ t/t22flood.t Flood fill tests
t/t31font.t General font interface tests
t/t35ttfont.t
t/t36oofont.t
+t/t37std.t Standard font tests for TT
t/t40scale.t
t/t50basicoo.t
t/t55trans.t
@@ -66,9 +66,14 @@ sub _draw {
sub _bounding_box {
my $self = shift;
my %input = @_;
- return Imager::i_tt_bbox($self->{id}, $input{size},
- $input{string}, length($input{string}),
- $input{utf8});
+ my @result =
+ Imager::i_tt_bbox($self->{id}, $input{size}, $input{string}, $input{utf8});
+ unless (@result) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+
+ return @result;
}
sub utf8 { 1 }
@@ -77,12 +82,29 @@ sub utf8 { 1 }
sub has_chars {
my ($self, %hsh) = @_;
- unless (defined $hsh{string} && length $hsh{string}) {
+ unless (defined $hsh{string}) {
$Imager::ERRSTR = "No string supplied to \$font->has_chars()";
return;
}
- return Imager::i_tt_has_chars($self->{id}, $hsh{string},
- _first($hsh{'utf8'}, $self->{utf8}, 0));
+ if (wantarray) {
+ my @result = Imager::i_tt_has_chars($self->{id}, $hsh{string},
+ _first($hsh{'utf8'}, $self->{utf8}, 0));
+ unless (@result) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+ return @result;
+ }
+ else {
+ my $result = Imager::i_tt_has_chars($self->{id}, $hsh{string},
+ _first($hsh{'utf8'}, $self->{utf8}, 0));
+ unless (defined $result) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+
+ return $result;
+ }
}
sub face_name {
@@ -91,6 +113,10 @@ sub face_name {
Imager::i_tt_face_name($self->{id});
}
+sub can_glyph_names {
+ 1;
+}
+
sub glyph_names {
my ($self, %input) = @_;
@@ -99,7 +125,13 @@ sub glyph_names {
or return Imager->_set_error("no string parameter passed to glyph_names");
my $utf8 = _first($input{utf8} || 0);
- Imager::i_tt_glyph_name($self->{id}, $string, $utf8);
+ my @names = Imager::i_tt_glyph_name($self->{id}, $string, $utf8);
+ unless (@names) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+
+ return @names;
}
1;
View
@@ -35,7 +35,7 @@ SKIP:
my $ttraw = Imager::i_tt_new($fontname);
ok($ttraw, "create font");
- my @bbox = i_tt_bbox($ttraw,50.0,'XMCLH',6,0);
+ my @bbox = i_tt_bbox($ttraw,50.0,'XMCLH',0);
is(@bbox, 8, "bounding box");
print "#bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
@@ -77,9 +77,9 @@ SKIP:
my $text = pack("C*", 0x41, 0xE2, 0x80, 0x90, 0x41);
my $alttext = "A-A";
- my @utf8box = i_tt_bbox($ttraw, 50.0, $text, length($text), 1);
+ my @utf8box = i_tt_bbox($ttraw, 50.0, $text, 1);
is(@utf8box, 8, "utf8 bbox element count");
- my @base = i_tt_bbox($ttraw, 50.0, $alttext, length($alttext), 0);
+ my @base = i_tt_bbox($ttraw, 50.0, $alttext, 0);
is(@base, 8, "alt bbox element count");
my $maxdiff = $fontname eq $deffont ? 0 : $base[2] / 3;
print "# (@utf8box vs @base)\n";
@@ -106,7 +106,7 @@ SKIP:
"draw UTF8");
ok(i_tt_cp($ttraw, $backgr, 350, 80, 0, 14, $text, 0, 1, 0),
"cp UTF8");
- @utf8box = i_tt_bbox($ttraw, 50.0, $text, length($text), 0);
+ @utf8box = i_tt_bbox($ttraw, 50.0, $text, 0);
is(@utf8box, 8, "native utf8 bbox element count");
ok(abs($utf8box[2] - $base[2]) <= $maxdiff,
"compare box sizes native $utf8box[2] vs $base[2] (maxerror $maxdiff)");
View
@@ -0,0 +1,29 @@
+#!perl -w
+use strict;
+use Imager::Test qw(std_font_tests std_font_test_count);
+use Imager::Font;
+use Test::More;
+
+$Imager::formats{tt}
+ or plan skip_all => "No tt available";
+
+plan tests => std_font_test_count();
+
+my $font = Imager::Font->new(file => "fontfiles/dodge.ttf",
+ type => "tt");
+my $name_font =
+ Imager::Font->new(file => "fontfiles/ImUgly.ttf",
+ type => "tt");
+
+SKIP:
+{
+ $font
+ or skip "Cannot load font", std_font_test_count();
+ std_font_tests
+ ({
+ font => $font,
+ has_chars => [ 1, 1, 1 ],
+ glyph_name_font => $name_font,
+ glyph_names => [ qw(A uni2010 A) ],
+ });
+}

0 comments on commit 7e3298e

Please sign in to comment.