]> git.imager.perl.org - imager.git/blobdiff - t/t35ttfont.t
avoid a probably unwarranted compiler warning
[imager.git] / t / t35ttfont.t
index fba92435091e989a6fb16192ccb6286227d4c426..27c6a42e8de8833330e1a88e274dd906287b7e79 100644 (file)
@@ -1,17 +1,20 @@
 #!perl -w
 use strict;
-use lib 't';
-use Test::More tests => 63;
+use Test::More tests => 97;
+
+$|=1;
 
 BEGIN { use_ok(Imager => ':all') }
-require "t/testtools.pl";
+use Imager::Test qw(diff_text_with_nul is_color3 is_image);
+
+-d "testout" or mkdir "testout";
 
 init_log("testout/t35ttfont.log",2);
 
 SKIP:
 {
-  skip("freetype 1.x unavailable or disabled", 62
-    unless i_has_format("tt");
+  skip("freetype 1.x unavailable or disabled", 96
+    unless $Imager::formats{"tt"};
   print "# has tt\n";
   
   my $deffont = './fontfiles/dodge.ttf';
@@ -19,23 +22,25 @@ SKIP:
 
   if (!ok(-f $fontname, "check test font file exists")) {
     print "# cannot find fontfile for truetype test $fontname\n";
-    skip('Cannot load test font', 61);
+    skip('Cannot load test font', 89);
   }
 
-  i_init_fonts();
+  #i_init_fonts();
   #     i_tt_set_aa(1);
   
   my $bgcolor = i_color_new(255,0,0,0);
-  my $overlay = Imager::ImgRaw::new(200,70,3);
+  my $overlay = Imager::ImgRaw::new(320,140,3);
+  i_box_filled($overlay, 0, 0, 319, 139, i_color_new(128, 128, 128));
   
   my $ttraw = Imager::i_tt_new($fontname);
   ok($ttraw, "create font");
 
-  my @bbox = i_tt_bbox($ttraw,50.0,'XMCLH',5,0);
+  my @bbox = i_tt_bbox($ttraw,50.0,'XMCLH',6,0);
   is(@bbox, 8, "bounding box");
   print "#bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
 
-  ok(i_tt_cp($ttraw,$overlay,5,50,1,50.0,'XMCLH',5,1,0), "cp output");
+  ok(i_tt_cp($ttraw,$overlay,5,50,1,50.0,'XM CLH',6,1,0), "cp output");
+  ok(i_tt_cp($ttraw,$overlay,5,120,1,50.0,'XM CLH',6,0,0), "cp output (non AA)");
   i_line($overlay,0,50,100,50,$bgcolor,1);
 
   open(FH,">testout/t35ttfont.ppm") || die "cannot open testout/t35ttfont.ppm\n";
@@ -49,15 +54,17 @@ SKIP:
   
   #     i_tt_set_aa(2);
   
-  ok(i_tt_text($ttraw,$backgr,100,120,$bgcolor,50.0,'test',4,1,0),
+  ok(i_tt_text($ttraw,$backgr,100,120,$bgcolor,50.0,'te st',5,1,0),
       "normal output");
+  ok(i_tt_text($ttraw,$backgr,100,200,$bgcolor,50.0,'te st',5,0,0),
+      "normal output (non AA)");
 
   my $ugly = Imager::i_tt_new("./fontfiles/ImUgly.ttf");
   ok($ugly, "create ugly font");
   # older versions were dropping the bottom of g and the right of a
   ok(i_tt_text($ugly, $backgr,100, 80, $bgcolor, 14, 'g%g', 3, 1, 0), 
      "draw g%g");
-  ok(i_tt_text($ugly, $backgr,150, 80, $bgcolor, 14, 'delta', 5, 1, 0),
+  ok(i_tt_text($ugly, $backgr,150, 80, $bgcolor, 14, 'delta', 6, 1, 0),
       "draw delta");
   i_line($backgr,0,20,499,20,i_color_new(0,127,0,0),1);
   ok(i_tt_text($ttraw, $backgr, 20, 20, $bgcolor, 14, 'abcdefghijklmnopqrstuvwxyz{|}', 29, 1, 0), "alphabet");
@@ -135,15 +142,15 @@ SKIP:
     
     my $face_name = Imager::i_tt_face_name($hcfont->{id});
     print "# face $face_name\n";
-    ok($face_name eq 'ExistenceTest', "face name");
+    is($face_name, 'ExistenceTest', "face name (function)");
     $face_name = $hcfont->face_name;
-    ok($face_name eq 'ExistenceTest', "face name");
+    is($face_name, 'ExistenceTest', "face name (OO)");
     
     # FT 1.x cheats and gives names even if the font doesn't have them
     my @glyph_names = $hcfont->glyph_names(string=>"!J/");
-    ok($glyph_names[0] eq 'exclam', "check exclam name OO");
+    is($glyph_names[0], 'exclam', "check exclam name OO");
     ok(!defined($glyph_names[1]), "check for no J name OO");
-    ok($glyph_names[2] eq 'slash', "check slash name OO");
+    is($glyph_names[2], 'slash', "check slash name OO");
     
     print "# ** name table of the test font **\n";
     Imager::i_tt_dump_names($hcfont->{id});
@@ -220,5 +227,108 @@ SKIP:
     ok($im->write(file=>'testout/t35align.ppm'), "save align image");
   }
 
+  { # Ticket #14804 Imager::Font->new() doesn't report error details
+    # when using freetype 1
+    # make sure we're using C locale for messages
+    use POSIX qw(setlocale LC_ALL);
+    setlocale(LC_ALL, "C");
+
+    my $font = Imager::Font->new(file=>'t/t35ttfont.t', type=>'tt');
+    ok(!$font, "font creation should have failed for invalid file");
+    cmp_ok(Imager->errstr, 'eq', 'Invalid file format.',
+         "test error message");
+
+    setlocale(LC_ALL, "");
+  }
+
+  { # check errstr set correctly
+    my $font = Imager::Font->new(file=>$fontname, type=>'tt',
+                               size => undef);
+    ok($font, "made size error test font");
+    my $im = Imager->new(xsize=>100, ysize=>100);
+    ok($im, "made size error test image");
+    ok(!$im->string(font=>$font, x=>10, 'y'=>50, string=>"Hello"),
+       "drawing should fail with no size");
+    is($im->errstr, "No font size provided", "check error message");
+
+    # try no string
+    ok(!$im->string(font=>$font, x=>10, 'y'=>50, size=>15),
+       "drawing should fail with no string");
+    is($im->errstr, "missing required parameter 'string'",
+       "check error message");
+  }
+
+  { # introduced in 0.46 - outputting just space crashes
+    my $im = Imager->new(xsize=>100, ysize=>100);
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', size=>14);
+    ok($im->string(font=>$font, x=> 5, 'y' => 50, string=>' '),
+      "outputting just a space was crashing");
+  }
+
+  { # 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=>'tt');
+    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);
+  }
+
+ SKIP:
+  { # 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=>'tt');
+    ok($font, "loaded fontfiles/ImUgly.ttf")
+      or skip("Could not load test font: ".Imager->errstr, 4);
+    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");
+  }
+
+ SKIP:
+  { # RT 71564
+    my $noalpha = Imager::Color->new(255, 255, 255, 0);
+    my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'tt',
+                                color => $noalpha);
+    ok($font, "loaded fontfiles/ImUgly.ttf")
+      or skip("Could not load test font: ".Imager->errstr, 4);
+    {
+      my $im = Imager->new(xsize => 40, ysize => 20);
+      my $copy = $im->copy;
+      ok($im->string(string => "AB", size => 20, aa => 1,
+                    x => 0, y => 15, font => $font),
+        "draw with transparent color, aa");
+      is_image($im, $copy, "should draw nothing");
+    }
+    {
+      my $im = Imager->new(xsize => 40, ysize => 20);
+      my $copy = $im->copy;
+      ok($im->string(string => "AB", size => 20, aa => 0,
+                    x => 0, y => 15, font => $font),
+        "draw with transparent color, non-aa");
+      local $TODO = "RT 73359 - non-AA text isn't normal mode rendered";
+      is_image($im, $copy, "should draw nothing");
+    }
+  }
+
   ok(1, "end of code");
 }