]> git.imager.perl.org - imager.git/commitdiff
- the FT1.x driver now supports the align parameter correctly.
authorTony Cook <tony@develop=help.com>
Mon, 2 May 2005 14:11:26 +0000 (14:11 +0000)
committerTony Cook <tony@develop=help.com>
Mon, 2 May 2005 14:11:26 +0000 (14:11 +0000)
  Tests were added to each driver to check correct handling of the align
  parameter.

Changes
Imager.xs
font.c
image.h
lib/Imager/Font/Truetype.pm
t/t30t1font.t
t/t35ttfont.t
t/t38ft2font.t

diff --git a/Changes b/Changes
index 0baa90a3ee9a2d59ba9df4940d83eeb979060853..7ce98d5cf7c5ba453f3aaf54c5b4d1f48bbc3fe1 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1076,6 +1076,9 @@ Revision history for Perl extension Imager.
   centre point if both 'x' and 'y' were non-zero.
 - the internal i_tags_get_string() function now acts correctly for
   integer only tags.
+- the FT1.x driver now supports the align parameter correctly.
+  Tests were added to each driver to check correct handling of the align
+  parameter.
 
 =================================================================
 
index 711dcc75ce28a9c56f246285610647e8941cb97f..8e8d87399075f949bfbcfeaf215ae1bcd6ebdc8c 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1910,7 +1910,7 @@ MODULE = Imager         PACKAGE = Imager
 
 
 undef_int
-i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
+i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8,align=1)
   Imager::Font::TT     handle
     Imager::ImgRaw     im
               int     xb
@@ -1921,6 +1921,7 @@ i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
               int     len_ignored
               int     smooth
                int     utf8
+               int     align
              PREINIT:
                char *str;
                STRLEN len;
@@ -1931,13 +1932,13 @@ i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8)
 #endif
                str = SvPV(str_sv, len);
                RETVAL = i_tt_text(handle, im, xb, yb, cl, points, str, 
-                                  len, smooth, utf8);
+                                  len, smooth, utf8, align);
              OUTPUT:
                RETVAL                
 
 
 undef_int
-i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
+i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
   Imager::Font::TT     handle
     Imager::ImgRaw     im
               int     xb
@@ -1948,6 +1949,7 @@ i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
               int     len_ignored
               int     smooth
                int     utf8
+               int     align
              PREINIT:
                char *str;
                STRLEN len;
@@ -1958,7 +1960,7 @@ i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8)
 #endif
                str = SvPV(str_sv, len);
                RETVAL = i_tt_cp(handle, im, xb, yb, channel, points, str, len,
-                                smooth, utf8);
+                                smooth, utf8, align);
              OUTPUT:
                 RETVAL
 
diff --git a/font.c b/font.c
index a979e7ad318b4d27d62b1462d856ddc8648737bf..fffc0d47c5a6e6d1e9e6e48929978cdc0957d1fd 100644 (file)
--- a/font.c
+++ b/font.c
@@ -1562,8 +1562,6 @@ i_tt_rasterize( TT_Fonthandle *handle, TT_Raster_Map *bit, int cords[6], float p
     return 0;
   }
 
-  /*  ascent = ( handle->properties.horizontal->Ascender  * handle->instanceh[inst].imetrics.y_ppem ) / handle->properties.header->Units_Per_EM; */
-  
   if ( smooth ) i_tt_done_raster_map( &small_bit );
   return 1;
 }
@@ -1593,19 +1591,20 @@ Interface to text rendering into a single channel in an image
 */
 
 undef_int
-i_tt_cp( TT_Fonthandle *handle, i_img *im, int xb, int yb, int channel, float points, char const* txt, int len, int smooth, int utf8 ) {
+i_tt_cp( TT_Fonthandle *handle, i_img *im, int xb, int yb, int channel, float points, char const* txt, int len, int smooth, int utf8, int align ) {
 
   int cords[BOUNDING_BOX_COUNT];
-  int ascent, st_offset;
+  int ascent, st_offset, y;
   TT_Raster_Map bit;
   
   i_clear_error();
   if (! i_tt_rasterize( handle, &bit, cords, points, txt, len, smooth, utf8 ) ) return 0;
   
-  ascent=cords[5];
-  st_offset=cords[0];
+  ascent=cords[BBOX_ASCENT];
+  st_offset=cords[BBOX_NEG_WIDTH];
+  y = align ? yb-ascent : yb;
 
-  i_tt_dump_raster_map_channel( im, &bit, xb-st_offset , yb-ascent, channel, smooth );
+  i_tt_dump_raster_map_channel( im, &bit, xb-st_offset , y, channel, smooth );
   i_tt_done_raster_map( &bit );
 
   return 1;
@@ -1630,19 +1629,20 @@ Interface to text rendering in a single color onto an image
 */
 
 undef_int
-i_tt_text( TT_Fonthandle *handle, i_img *im, int xb, int yb, i_color *cl, float points, char const* txt, int len, int smooth, int utf8) {
+i_tt_text( TT_Fonthandle *handle, i_img *im, int xb, int yb, i_color *cl, float points, char const* txt, int len, int smooth, int utf8, int align) {
   int cords[BOUNDING_BOX_COUNT];
-  int ascent, st_offset;
+  int ascent, st_offset, y;
   TT_Raster_Map bit;
 
   i_clear_error();
   
   if (! i_tt_rasterize( handle, &bit, cords, points, txt, len, smooth, utf8 ) ) return 0;
   
-  ascent=cords[5];
-  st_offset=cords[0];
+  ascent=cords[BBOX_ASCENT];
+  st_offset=cords[BBOX_NEG_WIDTH];
+  y = align ? yb-ascent : yb;
 
-  i_tt_dump_raster_map2( im, &bit, xb+st_offset, yb-ascent, cl, smooth ); 
+  i_tt_dump_raster_map2( im, &bit, xb+st_offset, y, cl, smooth ); 
   i_tt_done_raster_map( &bit );
 
   return 1;
diff --git a/image.h b/image.h
index e908c99271a20c361558d1d2e2727119e61a1e74..1de0b5cba253d612af239aad0a793369b432edf3 100644 (file)
--- a/image.h
+++ b/image.h
@@ -296,8 +296,8 @@ typedef struct TT_Fonthandle_ TT_Fonthandle;
 undef_int i_init_tt( void );
 TT_Fonthandle* i_tt_new(char *fontname);
 void i_tt_destroy( TT_Fonthandle *handle );
-undef_int i_tt_cp( TT_Fonthandle *handle,i_img *im,int xb,int yb,int channel,float points,char const* txt,int len,int smooth, int utf8);
-undef_int i_tt_text( TT_Fonthandle *handle, i_img *im, int xb, int yb, i_color *cl, float points, char const* txt, int len, int smooth, int utf8);
+undef_int i_tt_cp( TT_Fonthandle *handle,i_img *im,int xb,int yb,int channel,float points,char const* txt,int len,int smooth, int utf8, int align);
+undef_int i_tt_text( TT_Fonthandle *handle, i_img *im, int xb, int yb, i_color *cl, float points, char const* txt, int len, int smooth, int utf8, int align);
 undef_int i_tt_bbox( TT_Fonthandle *handle, float points,char *txt,int len,int cords[6], int utf8);
 int i_tt_has_chars(TT_Fonthandle *handle, char const *text, int len, int utf8, char *out);
 void i_tt_dump_names(TT_Fonthandle *handle);
index ca147c4c441b0c49bc2a83c526cdc025c9e27fb0..283fa958ed2563be7bbcaca6d899324a3d087ad0 100644 (file)
@@ -51,12 +51,13 @@ sub _draw {
     Imager::i_tt_cp($self->{id},$input{image}{IMG},
                    $input{'x'}, $input{'y'}, $input{channel}, $input{size},
                    $input{string}, length($input{string}),$input{aa},
-                    $input{utf8}); 
+                    $input{utf8}, $input{align}); 
   } else {
     Imager::i_tt_text($self->{id}, $input{image}{IMG}, 
                      $input{'x'}, $input{'y'}, $input{color},
                      $input{size}, $input{string}, 
-                     length($input{string}), $input{aa}, $input{utf8}); 
+                     length($input{string}), $input{aa}, $input{utf8},
+                      $input{align}); 
   }
 }
 
index b0cfbc759c2aba9561d7dd8e2be079957288b8ea..2a9788464ba41824be31e02760f8d153c743e3dd 100644 (file)
@@ -7,7 +7,7 @@
 # Change 1..1 below to 1..last_test_to_print .
 # (It may become useful if the test is moved to ./t subdirectory.)
 use strict;
-use Test::More tests => 58;
+use Test::More tests => 64;
 BEGIN { use_ok(Imager => ':all') }
 
 #$Imager::DEBUG=1;
@@ -22,13 +22,13 @@ my $fontname_afm=$ENV{'T1FONTTESTAFM'}||'./fontfiles/dcr10.afm';
 SKIP:
 {
   if (!(i_has_format("t1")) ) {
-    skip("t1lib unavailable or disabled", 57);
+    skip("t1lib unavailable or disabled", 63);
   }
   elsif (! -f $fontname_pfb) {
-    skip("cannot find fontfile for type 1 test $fontname_pfb", 57);
+    skip("cannot find fontfile for type 1 test $fontname_pfb", 63);
   }
   elsif (! -f $fontname_afm) {
-    skip("cannot find fontfile for type 1 test $fontname_afm", 57);
+    skip("cannot find fontfile for type 1 test $fontname_afm", 63);
   }
 
   print "# has t1\n";
@@ -231,34 +231,24 @@ SKIP:
     my $font = Imager::Font->new(file=>$deffont, type=>'t1');
     ok($font, "loaded deffont OO")
       or skip("could not load font:".Imager->errstr, 4);
-    my $im = Imager->new(xsize=>70, ysize=>150);
-    my %common1 = 
+    my $im = Imager->new(xsize=>140, ysize=>150);
+    my %common = 
       (
        font=>$font, 
-       text=>'A', 
        size=>40, 
-       color=>'white',
-       x=>5,
        aa=>1,
       );
-    my %common2 = 
-      (
-       font=>$font, 
-       text=>'y', 
-       size=>40, 
-       color=>'white',
-       x=>40,
-       aa=>1,
-      );
-    $im->line(x1=>0, y1=>40, x2=>69, y2=>40, color=>'blue');
-    $im->line(x1=>0, y1=>90, x2=>69, y2=>90, color=>'blue');
-    $im->line(x1=>0, y1=>110, x2=>69, y2=>110, color=>'blue');
-    ok($im->string(%common1, 'y'=>40), "no alignment A");
-    ok($im->string(%common2, 'y'=>40), "no alignment y");
-    ok($im->string(%common1, 'y'=>90, align=>1), "align=1");
-    ok($im->string(%common2, 'y'=>90, align=>1), "align=1");
-    ok($im->string(%common1, 'y'=>110, align=>0), "align=0");
-    ok($im->string(%common2, 'y'=>110, align=>0), "align=0");
+    $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", cp=>1 ],
+                  [ x=>110, text=>"y", cp=>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/t30align.ppm'), "save align image");
   }
 }
index b4ec97f8a7a46d532cdb3520c5a6d557b395dc34..9c06d54633d93cae894d1c8783712931631504ce 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use lib 't';
-use Test::More tests => 57;
+use Test::More tests => 63;
 
 BEGIN { use_ok(Imager => ':all') }
 require "t/testtools.pl";
@@ -10,7 +10,7 @@ init_log("testout/t35ttfont.log",2);
 
 SKIP:
 {
-  skip("freetype 1.x unavailable or disabled", 56
+  skip("freetype 1.x unavailable or disabled", 62
     unless i_has_format("tt");
   print "# has tt\n";
   
@@ -19,7 +19,7 @@ SKIP:
 
   if (!ok(-f $fontname, "check test font file exists")) {
     print "# cannot find fontfile for truetype test $fontname\n";
-    skip('Cannot load test font', 55);
+    skip('Cannot load test font', 61);
   }
 
   i_init_fonts();
@@ -199,34 +199,24 @@ SKIP:
     my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'tt');
     ok($font, "loaded deffont OO")
       or skip("could not load font:".Imager->errstr, 4);
-    my $im = Imager->new(xsize=>70, ysize=>150);
-    my %common1 = 
+    my $im = Imager->new(xsize=>140, ysize=>150);
+    my %common = 
       (
        font=>$font, 
-       text=>'A', 
        size=>40, 
-       color=>'white',
-       x=>5,
        aa=>1,
       );
-    my %common2 = 
-      (
-       font=>$font, 
-       text=>'y', 
-       size=>40, 
-       color=>'white',
-       x=>40,
-       aa=>1,
-      );
-    $im->line(x1=>0, y1=>40, x2=>69, y2=>40, color=>'blue');
-    $im->line(x1=>0, y1=>90, x2=>69, y2=>90, color=>'blue');
-    $im->line(x1=>0, y1=>110, x2=>69, y2=>110, color=>'blue');
-    ok($im->string(%common1, 'y'=>40), "A no alignment");
-    ok($im->string(%common2, 'y'=>40), "y no alignment");
-    ok($im->string(%common1, 'y'=>90, align=>1), "A align=1");
-    ok($im->string(%common2, 'y'=>90, align=>1), "y align=1");
-    ok($im->string(%common1, 'y'=>110, align=>0), "A align=0");
-    ok($im->string(%common2, 'y'=>110, align=>0), "y align=0");
+    $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", cp=>1 ],
+                  [ x=>110, text=>"y", cp=>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/t35align.ppm'), "save align image");
   }
 
index eaadb340b1dc9b4d887fc8be56664aa5f7b5b142..7c5580f2acf2d1e5d9daf7f3c7aa49b3162a0825 100644 (file)
@@ -1,6 +1,6 @@
 #!perl -w
 use strict;
-use Test::More tests => 152;
+use Test::More tests => 158;
 ++$|;
 # Before `make install' is performed this script should be runnable with
 # `make test'. After `make install' it should work as `perl test.pl'
@@ -18,13 +18,13 @@ my @base_color = (64, 255, 64);
 
 SKIP:
 {
-  i_has_format("ft2") or skip("no freetype2 library found", 151);
+  i_has_format("ft2") or skip("no freetype2 library found", 157);
 
   print "# has ft2\n";
   
   my $fontname=$ENV{'TTFONTTEST'}||'./fontfiles/dodge.ttf';
 
-  -f $fontname or skip("cannot find fontfile $fontname", 151);
+  -f $fontname or skip("cannot find fontfile $fontname", 157);
 
 
   my $bgcolor=i_color_new(255,0,0,0);
@@ -378,34 +378,24 @@ SKIP:
     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=>70, ysize=>150);
-    my %common1 = 
+    my $im = Imager->new(xsize=>140, ysize=>150);
+    my %common = 
       (
        font=>$font, 
-       text=>'A', 
        size=>40, 
-       color=>'white',
-       x=>5,
        aa=>1,
       );
-    my %common2 = 
-      (
-       font=>$font, 
-       text=>'y', 
-       size=>40, 
-       color=>'white',
-       x=>40,
-       aa=>1,
-      );
-    $im->line(x1=>0, y1=>40, x2=>69, y2=>40, color=>'blue');
-    $im->line(x1=>0, y1=>90, x2=>69, y2=>90, color=>'blue');
-    $im->line(x1=>0, y1=>110, x2=>69, y2=>110, color=>'blue');
-    ok($im->string(%common1, 'y'=>40), "A no alignment");
-    ok($im->string(%common2, 'y'=>40), "y no alignment");
-    ok($im->string(%common1, 'y'=>90, align=>1), "A align=1");
-    ok($im->string(%common2, 'y'=>90, align=>1), "y align=1");
-    ok($im->string(%common1, 'y'=>110, align=>0), "A align=0");
-    ok($im->string(%common2, 'y'=>110, align=>0), "y align=0");
+    $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", cp=>1 ],
+                  [ x=>110, text=>"y", cp=>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");
   }
 }