3 use Test::More tests => 187;
5 # Before `make install' is performed this script should be runnable with
6 # `make test'. After `make install' it should work as `perl test.pl'
8 ######################### We start with some black magic to print on failure.
10 # Change 1..1 below to 1..last_test_to_print .
11 # (It may become useful if the test is moved to ./t subdirectory.)
13 BEGIN { use_ok(Imager => ':all') }
15 use Imager::Test qw(diff_text_with_nul is_color3);
17 init_log("testout/t38ft2font.log",2);
19 my @base_color = (64, 255, 64);
23 i_has_format("ft2") or skip("no freetype2 library found", 186);
27 my $fontname=$ENV{'TTFONTTEST'}||'./fontfiles/dodge.ttf';
29 -f $fontname or skip("cannot find fontfile $fontname", 186);
32 my $bgcolor=i_color_new(255,0,0,0);
33 my $overlay=Imager::ImgRaw::new(200,70,3);
35 my $ttraw=Imager::Font::FreeType2::i_ft2_new($fontname, 0);
37 $ttraw or print Imager::_error_as_msg(),"\n";
38 ok($ttraw, "loaded raw font");
40 my @bbox=Imager::Font::FreeType2::i_ft2_bbox($ttraw, 50.0, 0, 'XMCLH', 0);
41 print "#bbox @bbox\n";
43 is(@bbox, 8, "i_ft2_bbox() returns 8 values");
45 ok(Imager::Font::FreeType2::i_ft2_cp($ttraw,$overlay,5,50,1,50.0,50, 'XMCLH',1,1, 0, 0), "drawn to channel");
46 i_line($overlay,0,50,100,50,$bgcolor,1);
48 open(FH,">testout/t38ft2font.ppm") || die "cannot open testout/t38ft2font.ppm\n";
50 my $IO = Imager::io_new_fd(fileno(FH));
51 ok(i_writeppm_wiol($overlay, $IO), "saved image");
54 $bgcolor=i_color_set($bgcolor,200,200,200,0);
55 my $backgr=Imager::ImgRaw::new(500,300,3);
58 ok(Imager::Font::FreeType2::i_ft2_text($ttraw,$backgr,100,150,NC(255, 64, 64),200.0,50, 'MAW',1,1,0, 0), "drew MAW");
59 Imager::Font::FreeType2::i_ft2_settransform($ttraw, [0.9659, 0.2588, 0, -0.2588, 0.9659, 0 ]);
60 ok(Imager::Font::FreeType2::i_ft2_text($ttraw,$backgr,100,150,NC(0, 128, 0),200.0,50, 'MAW',0,1, 0, 0), "drew rotated MAW");
61 i_line($backgr, 0,150, 499, 150, NC(0, 0, 255),1);
63 open(FH,">testout/t38ft2font2.ppm") || die "cannot open testout/t38ft2font.ppm\n";
65 $IO = Imager::io_new_fd(fileno(FH));
66 ok(i_writeppm_wiol($backgr,$IO), "saved second image");
69 my $oof = Imager::Font->new(file=>$fontname, type=>'ft2', 'index'=>0);
71 ok($oof, "loaded OO font");
73 my $im = Imager->new(xsize=>400, ysize=>250);
75 ok($im->string(font=>$oof,
80 color=>NC(255, 128, 255),
82 align=>0), "drawn through OO interface");
83 ok($oof->transform(matrix=>[1, 0.1, 0, 0, 1, 0]),
84 "set matrix via OO interface");
85 ok($im->string(font=>$oof,
93 align=>1), "drawn transformed through OO");
94 use Imager::Matrix2d ':handy';
95 ok($oof->transform(matrix=>m2d_rotate(degrees=>-30)),
96 "set transform from m2d module");
97 ok($im->string(font=>$oof,
103 color=>NC(255,255,0),
105 align=>0, vlayout=>0), "drawn first rotated");
107 ok($im->string(font=>$oof,
115 align=>0, vlayout=>0), "drawn second rotated");
117 $oof->transform(matrix=>m2d_identity());
118 $oof->hinting(hinting=>1);
121 # the test font (dodge.ttf) only supports one character above 0xFF that
122 # I can see, 0x2010 HYPHEN (which renders the same as 0x002D HYPHEN MINUS)
123 # an attempt at utf8 support
124 # first attempt to use native perl UTF8
127 skip("no native UTF8 support in this version of perl", 1)
130 # we need to do this in eval to prevent compile time errors in older
132 eval q{$text = "A\x{2010}A"}; # A, HYPHEN, A in our test font
133 #$text = "A".chr(0x2010)."A"; # this one works too
134 unless (ok($im->string(font=>$oof,
140 aa=>1), "drawn UTF natively")) {
141 print "# ",$im->errstr,"\n";
145 # an attempt using emulation of UTF8
146 my $text = pack("C*", 0x41, 0xE2, 0x80, 0x90, 0x41);
147 #my $text = "A\xE2\x80\x90\x41\x{2010}";
148 #substr($text, -1, 0) = '';
149 unless (ok($im->string(font=>$oof,
154 color=>NC(255,128,0),
156 utf8=>1), "drawn UTF emulated")) {
157 print "# ",$im->errstr,"\n";
161 # well it was - it demostrates what happens when you combine
162 # transformations and font hinting
163 for my $steps (0..39) {
164 $oof->transform(matrix=>m2d_rotate(degrees=>-$steps+5));
165 # demonstrates why we disable hinting on a doing a transform
166 # if the following line is enabled then the 0 degrees output sticks
168 # $oof->hinting(hinting=>1);
169 $im->string(font=>$oof,
174 color=>NC(255, $steps * 5, 200-$steps * 5),
179 $im->write(file=>'testout/t38_oo.ppm')
180 or print "# could not save OO output: ",$im->errstr,"\n";
182 my (@got) = $oof->has_chars(string=>"\x01H");
183 ok(@got == 2, "has_chars returned 2 items");
184 ok(!$got[0], "have no chr(1)");
185 ok($got[1], "have 'H'");
186 is($oof->has_chars(string=>"H\x01"), "\x01\x00",
187 "scalar has_chars()");
189 print "# OO bounding boxes\n";
190 @bbox = $oof->bounding_box(string=>"hello", size=>30);
191 my $bbox = $oof->bounding_box(string=>"hello", size=>30);
193 is(@bbox, 8, "list bbox returned 8 items");
194 ok($bbox->isa('Imager::Font::BBox'), "scalar bbox returned right class");
195 ok($bbox->start_offset == $bbox[0], "start_offset");
196 ok($bbox->end_offset == $bbox[2], "end_offset");
197 ok($bbox->global_ascent == $bbox[3], "global_ascent");
198 ok($bbox->global_descent == $bbox[1], "global_descent");
199 ok($bbox->ascent == $bbox[5], "ascent");
200 ok($bbox->descent == $bbox[4], "descent");
201 ok($bbox->advance_width == $bbox[6], "advance_width");
203 print "# aligned text output\n";
204 my $alimg = Imager->new(xsize=>300, ysize=>300);
205 $alimg->box(color=>'40FF40', filled=>1);
207 $oof->transform(matrix=>m2d_identity());
208 $oof->hinting(hinting=>1);
210 align_test('left', 'top', 10, 10, $oof, $alimg);
211 align_test('start', 'top', 10, 40, $oof, $alimg);
212 align_test('center', 'top', 150, 70, $oof, $alimg);
213 align_test('end', 'top', 290, 100, $oof, $alimg);
214 align_test('right', 'top', 290, 130, $oof, $alimg);
216 align_test('center', 'top', 150, 160, $oof, $alimg);
217 align_test('center', 'center', 150, 190, $oof, $alimg);
218 align_test('center', 'bottom', 150, 220, $oof, $alimg);
219 align_test('center', 'baseline', 150, 250, $oof, $alimg);
221 ok($alimg->write(file=>'testout/t38aligned.ppm'),
222 "saving aligned output image");
224 my $exfont = Imager::Font->new(file=>'fontfiles/ExistenceTest.ttf',
228 ok($exfont, "loaded existence font") or
229 skip("couldn't load test font", 11);
231 # the test font is known to have a shorter advance width for that char
232 my @bbox = $exfont->bounding_box(string=>"/", size=>100);
233 is(@bbox, 8, "should be 8 entries");
234 isnt($bbox[6], $bbox[2], "different advance width");
235 my $bbox = $exfont->bounding_box(string=>"/", size=>100);
236 ok($bbox->pos_width != $bbox->advance_width, "OO check");
238 cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
240 cmp_ok($bbox->display_width, '>', $bbox->advance_width,
241 "check display width (roughly)");
243 # check with a char that fits inside the box
244 $bbox = $exfont->bounding_box(string=>"!", size=>100);
245 print "# pos width ", $bbox->pos_width, "\n";
246 is($bbox->pos_width, $bbox->advance_width,
247 "check backwards compatibility");
248 cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
249 cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
250 cmp_ok($bbox->display_width, '<', $bbox->advance_width,
251 "display smaller than advance");
254 # make sure the number of tests on each branch match
255 if (Imager::Font::FreeType2::i_ft2_can_face_name()) {
256 my $facename = Imager::Font::FreeType2::i_ft2_face_name($exfont->{id});
257 print "# face name '$facename'\n";
258 is($facename, 'ExistenceTest', "test face name");
259 $facename = $exfont->face_name;
260 is($facename, 'ExistenceTest', "test face name OO");
263 # make sure we get the error we expect
264 my $facename = Imager::Font::FreeType2::i_ft2_face_name($exfont->{id});
265 my ($msg) = Imager::_error_as_msg();
266 ok(!defined($facename), "test face name not supported");
268 ok(scalar($msg =~ /or later required/), "test face name not supported");
274 Imager::Font::FreeType2->can_glyph_names
275 or skip("FT2 compiled without glyph names support", 9);
277 # FT2 considers POST tables in TTF fonts unreliable, so use
278 # a type 1 font, see below for TTF test
279 my $exfont = Imager::Font->new(file=>'fontfiles/ExistenceTest.pfb',
283 ok($exfont, "load Type 1 via FT2")
284 or skip("couldn't load type 1 with FT2", 8);
286 Imager::Font::FreeType2::i_ft2_glyph_name($exfont->{id}, "!J/");
288 #print Dumper \@glyph_names;
289 is($glyph_names[0], 'exclam', "check exclam name");
290 ok(!defined($glyph_names[1]), "check for no J name");
291 is($glyph_names[2], 'slash', "check slash name");
294 @glyph_names = $exfont->glyph_names(string=>"!J/");
295 is($glyph_names[0], 'exclam', "check exclam name OO");
296 ok(!defined($glyph_names[1]), "check for no J name OO");
297 is($glyph_names[2], 'slash', "check slash name OO");
299 # make sure a missing string parameter is handled correctly
301 $exfont->glyph_names();
303 is($@, "", "correct error handling");
304 cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
307 # freetype 2 considers truetype glyph name tables unreliable
308 # due to some specific fonts, supplying reliable_only=>0 bypasses
309 # that check and lets us get the glyph names even for truetype fonts
310 # so we can test this stuff <sigh>
311 # we can't use ExistenceTest.ttf since that's generated with
312 # AppleStandardEncoding since the same .sfd needs to generate
313 # a .pfb file, NameTest.ttf uses a Unicode encoding
315 # we were using an unsigned char to store a unicode character
316 # https://rt.cpan.org/Ticket/Display.html?id=7949
317 $exfont = Imager::Font->new(file=>'fontfiles/NameTest.ttf',
321 ok($exfont, "load TTF via FT2")
322 or skip("could not load TTF with FT2", 1);
323 my $text = pack("C*", 0xE2, 0x80, 0x90); # "\x{2010}" as utf-8
324 my @names = $exfont->glyph_names(string=>$text,
325 utf8=>1, reliable_only=>0);
326 is($names[0], "hyphentwo", "check utf8 glyph name");
330 # check that error codes are translated correctly
331 my $errfont = Imager::Font->new(file=>"t/t38ft2font.t", type=>"ft2");
332 is($errfont, undef, "new font vs non font");
333 cmp_ok(Imager->errstr, '=~', qr/unknown file format/, "check error message");
335 # Multiple Master tests
336 # we check a non-MM font errors correctly
337 print "# check that the methods act correctly for a non-MM font\n";
338 ok(!$exfont->is_mm, "exfont not MM");
339 ok((() = $exfont->mm_axes) == 0, "exfont has no MM axes");
340 cmp_ok(Imager->errstr, '=~', qr/no multiple masters/,
341 "and returns correct error when we ask");
342 ok(!$exfont->set_mm_coords(coords=>[0, 0]), "fail setting axis on exfont");
343 cmp_ok(Imager->errstr, '=~', qr/no multiple masters/,
344 "and returns correct error when we ask");
346 # try a MM font now - test font only has A defined
347 print "# Try a multiple master font\n";
348 my $mmfont = Imager::Font->new(file=>"fontfiles/MMOne.pfb", type=>"ft2",
349 color=>"white", aa=>1, size=>60);
350 ok($mmfont, "loaded MM font");
351 ok($mmfont->is_mm, "font is multiple master");
352 my @axes = $mmfont->mm_axes;
353 is(@axes, 2, "check we got both axes");
354 is($axes[0][0], "Weight", "name of first axis");
355 is($axes[0][1], 50, "min for first axis");
356 is($axes[0][2], 999, "max for first axis");
357 is($axes[1][0], "Slant", "name of second axis");
358 is($axes[1][1], 0, "min for second axis");
359 is($axes[1][2], 999, "max for second axis");
360 my $mmim = Imager->new(xsize=>200, ysize=>200);
361 $mmim->string(font=>$mmfont, x=>0, 'y'=>50, text=>"A");
362 ok($mmfont->set_mm_coords(coords=>[ 700, 0 ]), "set to bold, unsloped");
363 $mmim->string(font=>$mmfont, x=>0, 'y'=>100, text=>"A", color=>'blue');
364 my @weights = qw(50 260 525 760 999);
365 my @slants = qw(0 333 666 999);
366 for my $windex (0 .. $#weights) {
367 my $weight = $weights[$windex];
368 for my $sindex (0 .. $#slants) {
369 my $slant = $slants[$sindex];
370 $mmfont->set_mm_coords(coords=>[ $weight, $slant ]);
371 $mmim->string(font=>$mmfont, x=>30+32*$windex, 'y'=>50+45*$sindex,
376 ok($mmim->write(file=>"testout/t38mm.ppm"), "save MM output");
379 { print "# alignment tests\n";
380 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
381 ok($font, "loaded deffont OO")
382 or skip("could not load font:".Imager->errstr, 4);
383 my $im = Imager->new(xsize=>140, ysize=>150);
390 $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
391 $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
392 $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
393 for my $args ([ x=>5, text=>"A", color=>"white" ],
394 [ x=>40, text=>"y", color=>"white" ],
395 [ x=>75, text=>"A", channel=>1 ],
396 [ x=>110, text=>"y", channel=>1 ]) {
397 ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
398 ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
399 ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
401 ok($im->write(file=>'testout/t38align.ppm'), "save align image");
405 { # outputting a space in non-AA could either crash
407 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
408 my $im = Imager->new(xsize => 100, ysize => 100);
409 ok($im->string(x => 10, y => 10, string => "test space", aa => 0,
410 color => '#FFF', size => 8, font => $font),
411 "draw space non-antialiased (color)");
412 ok($im->string(x => 10, y => 50, string => "test space", aa => 0,
413 channel => 0, size => 8, font => $font),
414 "draw space non-antialiased (channel)");
417 { # cannot output "0"
418 # https://rt.cpan.org/Ticket/Display.html?id=21770
419 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
420 ok($font, "loaded imugly");
421 my $imbase = Imager->new(xsize => 100, ysize => 100);
422 my $im = $imbase->copy;
423 ok($im->string(x => 10, y => 50, string => "0", aa => 0,
424 color => '#FFF', size => 20, font => $font),
426 ok(Imager::i_img_diff($im->{IMG}, $imbase->{IMG}),
427 "make sure we actually drew it");
429 ok($im->string(x => 10, y => 50, string => 0.0, aa => 0,
430 color => '#FFF', size => 20, font => $font),
432 ok(Imager::i_img_diff($im->{IMG}, $imbase->{IMG}),
433 "make sure we actually drew it");
435 { # string output cut off at NUL ('\0')
436 # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
437 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
438 ok($font, "loaded imugly");
440 diff_text_with_nul("a\\0b vs a", "a\0b", "a",
441 font => $font, color => '#FFFFFF');
442 diff_text_with_nul("a\\0b vs a", "a\0b", "a",
443 font => $font, channel => 1);
445 # UTF8 encoded \x{2010}
446 my $dash = pack("C*", 0xE2, 0x80, 0x90);
447 diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
448 font => $font, color => '#FFFFFF', utf8 => 1);
449 diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
450 font => $font, channel => 1, utf8 => 1);
454 # when rendering to a transparent image the coverage should be
455 # expressed in terms of the alpha channel rather than the color
456 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
457 my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
458 ok($im->string(string => "AB", size => 20, aa => 1, color => '#F00',
459 x => 0, y => 15, font => $font),
460 "draw to transparent image");
461 my $im_noalpha = $im->convert(preset => 'noalpha');
462 my $im_pal = $im->to_paletted(make_colors => 'mediancut');
463 my @colors = $im_pal->getcolors;
464 is(@colors, 2, "should be only 2 colors");
465 @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
466 is_color3($colors[0], 0, 0, 0, "check we got black");
467 is_color3($colors[1], 255, 0, 0, "and red");
471 my $im = Imager->new(xsize => 100, ysize => 100, channels => 4);
472 $im->box(filled => 1, color => '#ff0000FF');
473 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
474 ok($im->string(x => 0, 'y' => 40, text => 'test',
475 size => 11, sizew => 11, font => $font, aa => 1),
476 'draw on translucent image')
480 # not ft2 specific, but Imager
481 my $im = Imager->new(xsize => 100, ysize => 100);
482 my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
483 my $imcopy = $im->copy;
484 ok($im, "make test image");
485 ok($font, "make test font");
486 ok($im->align_string(valign => "center", halign => "center",
487 x => 50, y => 50, string => "0", color => "#FFF",
490 ok(Imager::i_img_diff($im->{IMG}, $imcopy->{IMG}),
491 "make sure we drew the '0'");
496 my ($h, $v, $x, $y, $f, $img) = @_;
498 my @pos = $f->align(halign=>$h, valign=>$v, 'x'=>$x, 'y'=>$y,
499 image=>$img, size=>15, color=>'FFFFFF',
500 string=>"x$h ${v}y", channel=>1, aa=>1);
501 @pos = $img->align_string(halign=>$h, valign=>$v, 'x'=>$x, 'y'=>$y,
502 font=>$f, size=>15, color=>'FF99FF',
503 string=>"x$h ${v}y", aa=>1);
504 if (ok(@pos == 4, "$h $v aligned output")) {
506 my $cx = int(($pos[0] + $pos[2]) / 2);
507 my $cy = int(($pos[1] + $pos[3]) / 2);
509 print "# @pos cx $cx cy $cy\n";
510 okmatchcolor($img, $cx, $pos[1]-1, @base_color, "outer top edge");
511 okmatchcolor($img, $cx, $pos[3], @base_color, "outer bottom edge");
512 okmatchcolor($img, $pos[0]-1, $cy, @base_color, "outer left edge");
513 okmatchcolor($img, $pos[2], $cy, @base_color, "outer right edge");
515 okmismatchcolor($img, $cx, $pos[1], @base_color, "inner top edge");
516 okmismatchcolor($img, $cx, $pos[3]-1, @base_color, "inner bottom edge");
517 okmismatchcolor($img, $pos[0], $cy, @base_color, "inner left edge");
518 # okmismatchcolor($img, $pos[2]-1, $cy, @base_color, "inner right edge");
519 # XXX: This gets triggered by a freetype2 bug I think
520 # $ rpm -qa | grep freetype
525 cross($img, $x, $y, 'FF0000');
526 cross($img, $cx, $pos[1]-1, '0000FF');
527 cross($img, $cx, $pos[3], '0000FF');
528 cross($img, $pos[0]-1, $cy, '0000FF');
529 cross($img, $pos[2], $cy, '0000FF');
532 SKIP: { skip("couldn't draw text", 7) };
537 my ($img, $x, $y, $r, $g, $b, $about) = @_;
539 my $c = $img->getpixel('x'=>$x, 'y'=>$y);
540 my ($fr, $fg, $fb) = $c->rgba;
541 ok($fr == $r && $fg == $g && $fb == $b,
542 "want ($r,$g,$b) found ($fr,$fg,$fb)\@($x,$y) $about");
545 sub okmismatchcolor {
546 my ($img, $x, $y, $r, $g, $b, $about) = @_;
548 my $c = $img->getpixel('x'=>$x, 'y'=>$y);
549 my ($fr, $fg, $fb) = $c->rgba;
550 ok($fr != $r || $fg != $g || $fb != $b,
551 "don't want ($r,$g,$b) found ($fr,$fg,$fb)\@($x,$y) $about");
555 my ($img, $x, $y, $color) = @_;
557 $img->setpixel('x'=>[$x, $x, $x, $x, $x, $x-2, $x-1, $x+1, $x+2],
558 'y'=>[$y-2, $y-1, $y, $y+1, $y+2, $y, $y, $y, $y],