]> git.imager.perl.org - imager.git/blob - t/t35ttfont.t
0.61 release
[imager.git] / t / t35ttfont.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 91;
4
5 $|=1;
6
7 BEGIN { use_ok(Imager => ':all') }
8 require "t/testtools.pl";
9 use Imager::Test qw(diff_text_with_nul is_color3);
10
11 init_log("testout/t35ttfont.log",2);
12
13 SKIP:
14 {
15   skip("freetype 1.x unavailable or disabled", 90) 
16     unless i_has_format("tt");
17   print "# has tt\n";
18   
19   my $deffont = './fontfiles/dodge.ttf';
20   my $fontname=$ENV{'TTFONTTEST'} || $deffont;
21
22   if (!ok(-f $fontname, "check test font file exists")) {
23     print "# cannot find fontfile for truetype test $fontname\n";
24     skip('Cannot load test font', 89);
25   }
26
27   i_init_fonts();
28   #     i_tt_set_aa(1);
29   
30   my $bgcolor = i_color_new(255,0,0,0);
31   my $overlay = Imager::ImgRaw::new(320,140,3);
32   i_box_filled($overlay, 0, 0, 319, 139, i_color_new(128, 128, 128));
33   
34   my $ttraw = Imager::i_tt_new($fontname);
35   ok($ttraw, "create font");
36
37   my @bbox = i_tt_bbox($ttraw,50.0,'XMCLH',6,0);
38   is(@bbox, 8, "bounding box");
39   print "#bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
40
41   ok(i_tt_cp($ttraw,$overlay,5,50,1,50.0,'XM CLH',6,1,0), "cp output");
42   ok(i_tt_cp($ttraw,$overlay,5,120,1,50.0,'XM CLH',6,0,0), "cp output (non AA)");
43   i_line($overlay,0,50,100,50,$bgcolor,1);
44
45   open(FH,">testout/t35ttfont.ppm") || die "cannot open testout/t35ttfont.ppm\n";
46   binmode(FH);
47   my $IO = Imager::io_new_fd( fileno(FH) );
48   ok(i_writeppm_wiol($overlay, $IO), "save t35ttfont.ppm");
49   close(FH);
50
51   $bgcolor=i_color_set($bgcolor,200,200,200,0);
52   my $backgr=Imager::ImgRaw::new(500,300,3);
53   
54   #     i_tt_set_aa(2);
55   
56   ok(i_tt_text($ttraw,$backgr,100,120,$bgcolor,50.0,'te st',5,1,0),
57       "normal output");
58   ok(i_tt_text($ttraw,$backgr,100,200,$bgcolor,50.0,'te st',5,0,0),
59       "normal output (non AA)");
60
61   my $ugly = Imager::i_tt_new("./fontfiles/ImUgly.ttf");
62   ok($ugly, "create ugly font");
63   # older versions were dropping the bottom of g and the right of a
64   ok(i_tt_text($ugly, $backgr,100, 80, $bgcolor, 14, 'g%g', 3, 1, 0), 
65      "draw g%g");
66   ok(i_tt_text($ugly, $backgr,150, 80, $bgcolor, 14, 'delta', 6, 1, 0),
67       "draw delta");
68   i_line($backgr,0,20,499,20,i_color_new(0,127,0,0),1);
69   ok(i_tt_text($ttraw, $backgr, 20, 20, $bgcolor, 14, 'abcdefghijklmnopqrstuvwxyz{|}', 29, 1, 0), "alphabet");
70   ok(i_tt_text($ttraw, $backgr, 20, 50, $bgcolor, 14, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 26, 1, 0), "ALPHABET");
71   
72   # UTF8 tests
73   # for perl < 5.6 we can hand-encode text
74   # the following is "A\x{2010}A"
75   # 
76   my $text = pack("C*", 0x41, 0xE2, 0x80, 0x90, 0x41);
77   my $alttext = "A-A";
78   
79   my @utf8box = i_tt_bbox($ttraw, 50.0, $text, length($text), 1);
80   is(@utf8box, 8, "utf8 bbox element count");
81   my @base = i_tt_bbox($ttraw, 50.0, $alttext, length($alttext), 0);
82   is(@base, 8, "alt bbox element count");
83   my $maxdiff = $fontname eq $deffont ? 0 : $base[2] / 3;
84   print "# (@utf8box vs @base)\n";
85   ok(abs($utf8box[2] - $base[2]) <= $maxdiff, 
86      "compare box sizes $utf8box[2] vs $base[2] (maxerror $maxdiff)");
87   
88   # hand-encoded UTF8 drawing
89   ok(i_tt_text($ttraw, $backgr, 200, 80, $bgcolor, 14, $text, length($text), 1, 1), "draw hand-encoded UTF8");
90
91   ok(i_tt_cp($ttraw, $backgr, 250, 80, 1, 14, $text, length($text), 1, 1), 
92       "cp hand-encoded UTF8");
93
94   # ok, try native perl UTF8 if available
95  SKIP:
96   {
97     skip("perl too old to test native UTF8 support", 5) unless $] >= 5.006;
98
99     my $text;
100     # we need to do this in eval to prevent compile time errors in older
101     # versions
102     eval q{$text = "A\x{2010}A"}; # A, HYPHEN, A in our test font
103     #$text = "A".chr(0x2010)."A"; # this one works too
104     ok(i_tt_text($ttraw, $backgr, 300, 80, $bgcolor, 14, $text, 0, 1, 0),
105        "draw UTF8");
106     ok(i_tt_cp($ttraw, $backgr, 350, 80, 0, 14, $text, 0, 1, 0),
107        "cp UTF8");
108     @utf8box = i_tt_bbox($ttraw, 50.0, $text, length($text), 0);
109     is(@utf8box, 8, "native utf8 bbox element count");
110     ok(abs($utf8box[2] - $base[2]) <= $maxdiff, 
111        "compare box sizes native $utf8box[2] vs $base[2] (maxerror $maxdiff)");
112     eval q{$text = "A\x{0905}\x{0906}\x{0103}A"}; # Devanagari
113     ok(i_tt_text($ugly, $backgr, 100, 160, $bgcolor, 36, $text, 0, 1, 0),
114        "more complex output");
115   }
116
117   open(FH,">testout/t35ttfont2.ppm") || die "cannot open testout/t35ttfont.ppm\n";
118   binmode(FH);
119   $IO = Imager::io_new_fd( fileno(FH) );
120   ok(i_writeppm_wiol($backgr, $IO), "save t35ttfont2.ppm");
121   close(FH);
122   
123   my $exists_font = "fontfiles/ExistenceTest.ttf";
124   my $hcfont = Imager::Font->new(file=>$exists_font, type=>'tt');
125  SKIP:
126   {
127     ok($hcfont, "loading existence test font")
128       or skip("could not load test font", 20);
129
130     # list interface
131     my @exists = $hcfont->has_chars(string=>'!A');
132     ok(@exists == 2, "check return count");
133     ok($exists[0], "we have an exclamation mark");
134     ok(!$exists[1], "we have no exclamation mark");
135     
136     # scalar interface
137     my $exists = $hcfont->has_chars(string=>'!A');
138     ok(length($exists) == 2, "check return length");
139     ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
140     ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
141     
142     my $face_name = Imager::i_tt_face_name($hcfont->{id});
143     print "# face $face_name\n";
144     is($face_name, 'ExistenceTest', "face name (function)");
145     $face_name = $hcfont->face_name;
146     is($face_name, 'ExistenceTest', "face name (OO)");
147     
148     # FT 1.x cheats and gives names even if the font doesn't have them
149     my @glyph_names = $hcfont->glyph_names(string=>"!J/");
150     is($glyph_names[0], 'exclam', "check exclam name OO");
151     ok(!defined($glyph_names[1]), "check for no J name OO");
152     is($glyph_names[2], 'slash', "check slash name OO");
153     
154     print "# ** name table of the test font **\n";
155     Imager::i_tt_dump_names($hcfont->{id});
156
157     # the test font is known to have a shorter advance width for that char
158     my @bbox = $hcfont->bounding_box(string=>"/", size=>100);
159     is(@bbox, 8, "should be 8 entries");
160     isnt($bbox[6], $bbox[2], "different advance width from pos width");
161     print "# @bbox\n";
162     my $bbox = $hcfont->bounding_box(string=>"/", size=>100);
163     isnt($bbox->pos_width, $bbox->advance_width, "OO check");
164
165     cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
166
167     cmp_ok($bbox->display_width, '>', $bbox->advance_width,
168            "check display width (roughly)");
169
170     # check with a char that fits inside the box
171     $bbox = $hcfont->bounding_box(string=>"!", size=>100);
172     print "# @$bbox\n";
173     print "# pos width ", $bbox->pos_width, "\n";
174     is($bbox->pos_width, $bbox->advance_width, 
175        "check backwards compatibility");
176     cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
177     cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
178     cmp_ok($bbox->display_width, '<', $bbox->advance_width,
179            "display smaller than advance");
180   }
181   undef $hcfont;
182   
183   my $name_font = "fontfiles/NameTest.ttf";
184   $hcfont = Imager::Font->new(file=>$name_font, type=>'tt');
185  SKIP:
186   {
187     ok($hcfont, "loading name font")
188       or skip("could not load name font $name_font", 3);
189     # make sure a missing string parameter is handled correctly
190     eval {
191       $hcfont->glyph_names();
192     };
193     is($@, "", "correct error handling");
194     cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
195     
196     my $text = pack("C*", 0xE2, 0x80, 0x90); # "\x{2010}" as utf-8
197     my @names = $hcfont->glyph_names(string=>$text, utf8=>1);
198     is($names[0], "hyphentwo", "check utf8 glyph name");
199   }
200
201   undef $hcfont;
202   
203  SKIP:
204   { print "# alignment tests\n";
205     my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'tt');
206     ok($font, "loaded deffont OO")
207       or skip("could not load font:".Imager->errstr, 4);
208     my $im = Imager->new(xsize=>140, ysize=>150);
209     my %common = 
210       (
211        font=>$font, 
212        size=>40, 
213        aa=>1,
214       );
215     $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
216     $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
217     $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
218     for my $args ([ x=>5,   text=>"A", color=>"white" ],
219                   [ x=>40,  text=>"y", color=>"white" ],
220                   [ x=>75,  text=>"A", channel=>1 ],
221                   [ x=>110, text=>"y", channel=>1 ]) {
222       ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
223       ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
224       ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
225     }
226     ok($im->write(file=>'testout/t35align.ppm'), "save align image");
227   }
228
229   { # Ticket #14804 Imager::Font->new() doesn't report error details
230     # when using freetype 1
231     # make sure we're using C locale for messages
232     use POSIX qw(setlocale LC_ALL);
233     setlocale(LC_ALL, "C");
234
235     my $font = Imager::Font->new(file=>'t/t35ttfont.t', type=>'tt');
236     ok(!$font, "font creation should have failed for invalid file");
237     cmp_ok(Imager->errstr, 'eq', 'Invalid file format.',
238           "test error message");
239
240     setlocale(LC_ALL, "");
241   }
242
243   { # check errstr set correctly
244     my $font = Imager::Font->new(file=>$fontname, type=>'tt',
245                                 size => undef);
246     ok($font, "made size error test font");
247     my $im = Imager->new(xsize=>100, ysize=>100);
248     ok($im, "made size error test image");
249     ok(!$im->string(font=>$font, x=>10, 'y'=>50, string=>"Hello"),
250        "drawing should fail with no size");
251     is($im->errstr, "No font size provided", "check error message");
252
253     # try no string
254     ok(!$im->string(font=>$font, x=>10, 'y'=>50, size=>15),
255        "drawing should fail with no string");
256     is($im->errstr, "missing required parameter 'string'",
257        "check error message");
258   }
259
260   { # introduced in 0.46 - outputting just space crashes
261     my $im = Imager->new(xsize=>100, ysize=>100);
262     my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', size=>14);
263     ok($im->string(font=>$font, x=> 5, 'y' => 50, string=>' '),
264       "outputting just a space was crashing");
265   }
266
267   { # string output cut off at NUL ('\0')
268     # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
269     my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'tt');
270     ok($font, "loaded imugly");
271
272     diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
273                        font => $font, color => '#FFFFFF');
274     diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
275                        font => $font, channel => 1);
276
277     # UTF8 encoded \x{2010}
278     my $dash = pack("C*", 0xE2, 0x80, 0x90);
279     diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
280                        font => $font, color => '#FFFFFF', utf8 => 1);
281     diff_text_with_nul("utf8 dash\0dash vs dash", "$dash\0$dash", $dash,
282                        font => $font, channel => 1, utf8 => 1);
283   }
284
285   { # RT 11972
286     # when rendering to a transparent image the coverage should be
287     # expressed in terms of the alpha channel rather than the color
288     my $font = Imager::Font->new(file=>'fontfiles/ImUgly.ttf', type=>'tt');
289     my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
290     ok($im->string(string => "AB", size => 20, aa => 1, color => '#F00',
291                    x => 0, y => 15, font => $font),
292        "draw to transparent image");
293     #$im->write(file => "foo.png");
294     my $im_noalpha = $im->convert(preset => 'noalpha');
295     my $im_pal = $im->to_paletted(make_colors => 'mediancut');
296     my @colors = $im_pal->getcolors;
297     is(@colors, 2, "should be only 2 colors");
298     @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
299     is_color3($colors[0], 0, 0, 0, "check we got black");
300     is_color3($colors[1], 255, 0, 0, "and red");
301   }
302
303   ok(1, "end of code");
304 }