[rt #73359] draw non-aa text in alpha combining mode for FT1
authorTony Cook <tony@develop-help.com>
Mon, 10 Jun 2013 03:41:40 +0000 (13:41 +1000)
committerTony Cook <tony@develop-help.com>
Mon, 10 Jun 2013 03:41:40 +0000 (13:41 +1000)
Changes
fontft1.c
t/350-font/030-ttoo.t

diff --git a/Changes b/Changes
index c9228e6ad911d27e40669d2dfe17f2d6c8e90fa7..195ce792e63c4ac510dfab146bc831fd69da31dc 100644 (file)
--- a/Changes
+++ b/Changes
@@ -34,6 +34,9 @@ Imager release history.  Older releases can be found in Changes.old
 
    - some XS code formatting
 
+ - TT (Freetype 1.x) and FT2 (Freetype 2.x) non-antialiased rendering
+   now renders in alpha-combining mode, to match antialiased output.
+   https://rt.cpan.org/Ticket/Display.html?id=73359
 
 Imager 0.96 - 19 May 2013
 ===========
index df05e86fc166de6c9c5cd5c0e66a02c1c55f61d9..b09fb564e2520b3c43d69ce83eec0e2075bbff8a 100644 (file)
--- a/fontft1.c
+++ b/fontft1.c
@@ -871,22 +871,30 @@ i_tt_dump_raster_map2( i_img* im, TT_Raster_Map* bit, i_img_dim xb, i_img_dim yb
     }
     i_render_done(&r);
   } else {
+    unsigned char *bmp = mymalloc(bit->width);
+    i_render r;
+
+    i_render_init(&r, im, bit->width);
+
     for(y=0;y<bit->rows;y++) {
       unsigned mask = 0x80;
       unsigned char *p = bmap + y * bit->cols;
+      unsigned char *pout = bmp;
 
       for(x = 0; x < bit->width; x++) {
-       if (*p & mask) {
-         i_ppix(im, x+xb, y+yb, cl);
-       }
+       *pout++ = (*p & mask) ? 0xFF : 0;
        mask >>= 1;
        if (!mask) {
          mask = 0x80;
          ++p;
        }
       }
+
+      i_render_color(&r, xb, yb+y, bit->cols, bmp, cl);
     }
 
+    i_render_done(&r);
+    myfree(bmp);
   }
 }
 
index 2982681531c56397ad7e7abcb3e4543e5775a8af..c3c9df4990781bd2038a5258455ce94f949d2480 100644 (file)
@@ -10,19 +10,18 @@ use strict;
 
 # Change 1..1 below to 1..last_test_to_print .
 # (It may become useful if the test is moved to ./t subdirectory.)
-use Test::More tests => 16;
+use Test::More tests => 26;
 
-BEGIN { use_ok('Imager') };
+use Imager;
 
-BEGIN {
-  require Imager::Test;
-  Imager::Test->import(qw(isnt_image));
-}
+use Imager::Test qw(isnt_image is_image);
 
 -d "testout" or mkdir "testout";
 
 Imager->open_log(log => "testout/t36oofont.log");
 
+my @test_output;
+
 my $fontname_tt=$ENV{'TTFONTTEST'}||'./fontfiles/dodge.ttf';
 
 my $green=Imager::Color->new(92,205,92,128);
@@ -33,7 +32,7 @@ die $Imager::ERRSTR unless $red;
 SKIP:
 {
   $Imager::formats{"tt"} && -f $fontname_tt
-    or skip("FT1.x missing or disabled", 14);
+    or skip("FT1.x missing or disabled", 24);
 
   my $img=Imager->new(xsize=>300, ysize=>100) or die "$Imager::ERRSTR\n";
 
@@ -97,6 +96,59 @@ SKIP:
       isnt_image($work, $cmp, "make sure something was drawn");
     }
   }
+
+  { # RT 73359
+    # non-AA font drawing isn't normal mode
+
+    Imager->log("testing no-aa normal output\n");
+
+    my $font = Imager::Font->new(file => "fontfiles/ImUgly.ttf", type => "tt");
+
+    ok($font, "make a work font");
+
+    my %common =
+      (
+       x => 10,
+       font => $font,
+       size => 25,
+       aa => 0,
+       align => 0,
+      );
+
+    # build our comparison image
+    my $cmp = Imager->new(xsize => 120, ysize => 100);
+    my $layer = Imager->new(xsize => 120, ysize => 100, channels => 4);
+    ok($layer->string(%common, y => 10, text => "full", color => "#8080FF"),
+       "draw non-aa text at full coverage to layer image");
+    ok($layer->string(%common, y => 40, text => "half", color => "#FF808080"),
+       "draw non-aa text at half coverage to layer image");
+    ok($layer->string(%common, y => 70, text => "quarter", color => "#80FF8040"),
+       "draw non-aa text at zero coverage to layer image");
+    ok($cmp->rubthrough(src => $layer), "rub layer onto comparison image");
+
+    my $im = Imager->new(xsize => 120, ysize => 100);
+    ok($im->string(%common, y => 10, text => "full", color => "#8080FF"),
+       "draw non-aa text at full coverage");
+    ok($im->string(%common, y => 40, text => "half", color => "#FF808080"),
+       "draw non-aa text at half coverage");
+    ok($im->string(%common, y => 70, text => "quarter", color => "#80FF8040"),
+       "draw non-aa text at zero coverage");
+    is_image($im, $cmp, "check the result");
+
+    push @test_output, "ttaanorm.ppm", "ttaacmp.ppm";
+    ok($cmp->write(file => "testout/ttaacmp.ppm"), "save cmp image")
+      or diag "Saving cmp image: ", $cmp->errstr;
+    ok($im->write(file => "testout/ttaanorm.ppm"), "save test image")
+      or diag "Saving result image: ", $im->errstr;
+  }
+}
+
+Imager->close_log;
+
+END {
+  unless ($ENV{IMAGER_KEEP_FILES}) {
+    unlink map "testout/$_", @test_output;
+  }
 }
 
 ok(1, "end");