]> git.imager.perl.org - imager.git/blobdiff - t/t38ft2font.t
sanity test new ft2 rendering
[imager.git] / t / t38ft2font.t
index 59118309e4d88255ef7525c1c886fb49e6854d15..722cc133a0f54de67f57644b639d1d690f34e2b5 100644 (file)
@@ -1,6 +1,7 @@
 #!perl -w
 use strict;
-use Test::More tests => 138;
+use Test::More tests => 182;
+++$|;
 # Before `make install' is performed this script should be runnable with
 # `make test'. After `make install' it should work as `perl test.pl'
 
@@ -11,19 +12,21 @@ use Test::More tests => 138;
 
 BEGIN { use_ok(Imager => ':all') }
 
+use Imager::Test qw(diff_text_with_nul is_color3);
+
 init_log("testout/t38ft2font.log",2);
 
 my @base_color = (64, 255, 64);
 
 SKIP:
 {
-  i_has_format("ft2") or skip("no freetype2 library found", 137);
+  i_has_format("ft2") or skip("no freetype2 library found", 181);
 
   print "# has ft2\n";
   
   my $fontname=$ENV{'TTFONTTEST'}||'./fontfiles/dodge.ttf';
 
-  -f $fontname or skip("cannot find fontfile $fontname", 137);
+  -f $fontname or skip("cannot find fontfile $fontname", 181);
 
 
   my $bgcolor=i_color_new(255,0,0,0);
@@ -37,7 +40,7 @@ SKIP:
   my @bbox=Imager::Font::FreeType2::i_ft2_bbox($ttraw, 50.0, 0, 'XMCLH', 0);
   print "#bbox @bbox\n";
   
-  is(@bbox, 7, "i_ft2_bbox() returns 7 values");
+  is(@bbox, 8, "i_ft2_bbox() returns 8 values");
 
   ok(Imager::Font::FreeType2::i_ft2_cp($ttraw,$overlay,5,50,1,50.0,50, 'XMCLH',1,1, 0, 0), "drawn to channel");
   i_line($overlay,0,50,100,50,$bgcolor,1);
@@ -180,14 +183,14 @@ SKIP:
   ok(@got == 2, "has_chars returned 2 items");
   ok(!$got[0], "have no chr(1)");
   ok($got[1], "have 'H'");
-  ok($oof->has_chars(string=>"H\x01") eq "\x01\x00",
+  is($oof->has_chars(string=>"H\x01"), "\x01\x00",
      "scalar has_chars()");
 
   print "# OO bounding boxes\n";
   @bbox = $oof->bounding_box(string=>"hello", size=>30);
   my $bbox = $oof->bounding_box(string=>"hello", size=>30);
 
-  is(@bbox, 7, "list bbox returned 7 items");
+  is(@bbox, 8, "list bbox returned 8 items");
   ok($bbox->isa('Imager::Font::BBox'), "scalar bbox returned right class");
   ok($bbox->start_offset == $bbox[0], "start_offset");
   ok($bbox->end_offset == $bbox[2], "end_offset");
@@ -223,22 +226,38 @@ SKIP:
   SKIP:
   {
     ok($exfont, "loaded existence font") or
-      skip("couldn't load test font", 5);
+      skip("couldn't load test font", 11);
+
     # the test font is known to have a shorter advance width for that char
     my @bbox = $exfont->bounding_box(string=>"/", size=>100);
-    is(@bbox, 7, "should be 7 entries");
+    is(@bbox, 8, "should be 8 entries");
     isnt($bbox[6], $bbox[2], "different advance width");
     my $bbox = $exfont->bounding_box(string=>"/", size=>100);
     ok($bbox->pos_width != $bbox->advance_width, "OO check");
 
+    cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
+
+    cmp_ok($bbox->display_width, '>', $bbox->advance_width,
+           "check display width (roughly)");
+
+    # check with a char that fits inside the box
+    $bbox = $exfont->bounding_box(string=>"!", size=>100);
+    print "# pos width ", $bbox->pos_width, "\n";
+    is($bbox->pos_width, $bbox->advance_width, 
+       "check backwards compatibility");
+    cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
+    cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
+    cmp_ok($bbox->display_width, '<', $bbox->advance_width,
+           "display smaller than advance");
+
     # name tests
     # make sure the number of tests on each branch match
     if (Imager::Font::FreeType2::i_ft2_can_face_name()) {
       my $facename = Imager::Font::FreeType2::i_ft2_face_name($exfont->{id});
       print "# face name '$facename'\n";
-      ok($facename eq 'ExistenceTest', "test face name");
+      is($facename, 'ExistenceTest', "test face name");
       $facename = $exfont->face_name;
-      ok($facename eq 'ExistenceTest', "test face name OO");
+      is($facename, 'ExistenceTest', "test face name OO");
     }
     else {
       # make sure we get the error we expect
@@ -355,6 +374,99 @@ SKIP:
   }
 
   ok($mmim->write(file=>"testout/t38mm.ppm"), "save MM output");
+
+ SKIP:
+  { print "# alignment tests\n";
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
+    ok($font, "loaded deffont OO")
+      or skip("could not load font:".Imager->errstr, 4);
+    my $im = Imager->new(xsize=>140, ysize=>150);
+    my %common = 
+      (
+       font=>$font, 
+       size=>40, 
+       aa=>1,
+      );
+    $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
+    $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
+    $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
+    for my $args ([ x=>5,   text=>"A", color=>"white" ],
+                  [ x=>40,  text=>"y", color=>"white" ],
+                  [ x=>75,  text=>"A", channel=>1 ],
+                  [ x=>110, text=>"y", channel=>1 ]) {
+      ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
+      ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
+      ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
+    }
+    ok($im->write(file=>'testout/t38align.ppm'), "save align image");
+  }
+
+
+  { # outputting a space in non-AA could either crash 
+    # or fail (ft 2.2+)
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
+    my $im = Imager->new(xsize => 100, ysize => 100);
+    ok($im->string(x => 10, y => 10, string => "test space", aa => 0,
+                  color => '#FFF', size => 8, font => $font),
+       "draw space non-antialiased (color)");
+    ok($im->string(x => 10, y => 50, string => "test space", aa => 0,
+                  channel => 0, size => 8, font => $font),
+       "draw space non-antialiased (channel)");
+  }
+
+  { # cannot output "0"
+    # https://rt.cpan.org/Ticket/Display.html?id=21770
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
+    ok($font, "loaded imugly");
+    my $imbase = Imager->new(xsize => 100, ysize => 100);
+    my $im = $imbase->copy;
+    ok($im->string(x => 10, y => 50, string => "0", aa => 0,
+                  color => '#FFF', size => 20, font => $font),
+       "draw '0'");
+    ok(Imager::i_img_diff($im->{IMG}, $imbase->{IMG}),
+       "make sure we actually drew it");
+    $im = $imbase->copy;
+    ok($im->string(x => 10, y => 50, string => 0.0, aa => 0,
+                  color => '#FFF', size => 20, font => $font),
+       "draw 0.0");
+    ok(Imager::i_img_diff($im->{IMG}, $imbase->{IMG}),
+       "make sure we actually drew it");
+  }
+  { # string output cut off at NUL ('\0')
+    # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
+    ok($font, "loaded imugly");
+
+    diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
+                      font => $font, color => '#FFFFFF');
+    diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
+                      font => $font, channel => 1);
+
+    # UTF8 encoded \x{2010}
+    my $dash = pack("C*", 0xE2, 0x80, 0x90);
+    diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
+                      font => $font, color => '#FFFFFF', utf8 => 1);
+    diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
+                      font => $font, channel => 1, utf8 => 1);
+  }
+
+  { # RT 11972
+    # when rendering to a transparent image the coverage should be
+    # expressed in terms of the alpha channel rather than the color
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'ft2');
+    my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
+    ok($im->string(string => "AB", size => 20, aa => 1, color => '#F00',
+                  x => 0, y => 15, font => $font),
+       "draw to transparent image");
+    $im->write(file => "foo.png");
+    my $im_noalpha = $im->convert(preset => 'noalpha');
+    my $im_pal = $im->to_paletted(make_colors => 'mediancut');
+    my @colors = $im_pal->getcolors;
+    is(@colors, 2, "should be only 2 colors");
+    @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
+    is_color3($colors[0], 0, 0, 0, "check we got black");
+    is_color3($colors[1], 255, 0, 0, "and red");
+  }
 }
 
 sub align_test {
@@ -363,8 +475,8 @@ sub align_test {
   my @pos = $f->align(halign=>$h, valign=>$v, 'x'=>$x, 'y'=>$y,
                       image=>$img, size=>15, color=>'FFFFFF',
                       string=>"x$h ${v}y", channel=>1, aa=>1);
-  @pos = $f->align(halign=>$h, valign=>$v, 'x'=>$x, 'y'=>$y,
-                      image=>$img, size=>15, color=>'FF99FF',
+  @pos = $img->align_string(halign=>$h, valign=>$v, 'x'=>$x, 'y'=>$y,
+                      font=>$f, size=>15, color=>'FF99FF',
                       string=>"x$h ${v}y", aa=>1);
   if (ok(@pos == 4, "$h $v aligned output")) {
     # checking corners
@@ -394,7 +506,7 @@ sub align_test {
     cross($img, $pos[2], $cy, '0000FF');
   }
   else {
-    SKIP: { skip("couldn't draw text", 8) };
+    SKIP: { skip("couldn't draw text", 7) };
   }
 }