]> git.imager.perl.org - imager.git/blob - t/t30t1font.t
huge spelling update and spell checking patch
[imager.git] / t / t30t1font.t
1 #!perl -w
2 # Before `make install' is performed this script should be runnable with
3 # `make test'. After `make install' it should work as `perl test.pl'
4
5 ######################### We start with some black magic to print on failure.
6
7 # Change 1..1 below to 1..last_test_to_print .
8 # (It may become useful if the test is moved to ./t subdirectory.)
9 use strict;
10 use Test::More tests => 94;
11 BEGIN { use_ok(Imager => ':all') }
12 use Imager::Test qw(diff_text_with_nul is_color3);
13
14 #$Imager::DEBUG=1;
15
16 init_log("testout/t30t1font.log",1);
17
18 my $deffont = './fontfiles/dcr10.pfb';
19
20 my $fontname_pfb=$ENV{'T1FONTTESTPFB'}||$deffont;
21 my $fontname_afm=$ENV{'T1FONTTESTAFM'}||'./fontfiles/dcr10.afm';
22
23 SKIP:
24 {
25   if (!(i_has_format("t1")) ) {
26     skip("t1lib unavailable or disabled", 93);
27   }
28   elsif (! -f $fontname_pfb) {
29     skip("cannot find fontfile for type 1 test $fontname_pfb", 93);
30   }
31   elsif (! -f $fontname_afm) {
32     skip("cannot find fontfile for type 1 test $fontname_afm", 93);
33   }
34
35   print "# has t1\n";
36
37   #i_t1_set_aa(1);
38
39   unlink "t1lib.log"; # lose it if it exists
40   init(t1log=>0);
41   ok(!-e("t1lib.log"), "disable t1log");
42   init(t1log=>1);
43   ok(-e("t1lib.log"), "enable t1log");
44   init(t1log=>0);
45   unlink "t1lib.log";
46
47   my $fnum=Imager::i_t1_new($fontname_pfb,$fontname_afm); # this will load the pfb font
48   unless (ok($fnum >= 0, "load font $fontname_pfb")) {
49     skip("without the font I can't do a thing", 90);
50   }
51
52   my $bgcolor=Imager::Color->new(255,0,0,0);
53   my $overlay=Imager::ImgRaw::new(200,70,3);
54   
55   ok(i_t1_cp($overlay,5,50,1,$fnum,50.0,'XMCLH',5,1), "i_t1_cp");
56
57   i_line($overlay,0,50,100,50,$bgcolor,1);
58
59   my @bbox=i_t1_bbox(0,50.0,'XMCLH',5);
60   is(@bbox, 8, "i_t1_bbox");
61   print "# bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
62
63   open(FH,">testout/t30t1font.ppm") || die "cannot open testout/t35t1font.ppm\n";
64   binmode(FH); # for os2
65   my $IO = Imager::io_new_fd( fileno(FH) );
66   i_writeppm_wiol($overlay,$IO);
67   close(FH);
68
69   $bgcolor=Imager::Color::set($bgcolor,200,200,200,0);
70   my $backgr=Imager::ImgRaw::new(280,300,3);
71
72   i_t1_set_aa(2);
73   ok(i_t1_text($backgr,10,100,$bgcolor,$fnum,150.0,'test',4,1), "i_t1_text");
74
75   # "UTF8" tests
76   # for perl < 5.6 we can hand-encode text
77   # since T1 doesn't support over 256 chars in an encoding we just drop
78   # chars over \xFF
79   # the following is "A\xA1\x{2010}A"
80   # 
81   my $text = pack("C*", 0x41, 0xC2, 0xA1, 0xE2, 0x80, 0x90, 0x41);
82   my $alttext = "A\xA1A";
83   
84   my @utf8box = i_t1_bbox($fnum, 50.0, $text, length($text), 1);
85   is(@utf8box, 8, "utf8 bbox element count");
86   my @base = i_t1_bbox($fnum, 50.0, $alttext, length($alttext), 0);
87   is(@base, 8, "alt bbox element count");
88   my $maxdiff = $fontname_pfb eq $deffont ? 0 : $base[2] / 3;
89   print "# (@utf8box vs @base)\n";
90   ok(abs($utf8box[2] - $base[2]) <= $maxdiff, 
91       "compare box sizes $utf8box[2] vs $base[2] (maxerror $maxdiff)");
92
93   # hand-encoded UTF8 drawing
94   ok(i_t1_text($backgr, 10, 140, $bgcolor, $fnum, 32, $text, length($text), 1,1), "draw hand-encoded UTF8");
95
96   ok(i_t1_cp($backgr, 80, 140, 1, $fnum, 32, $text, length($text), 1, 1), 
97       "cp hand-encoded UTF8");
98
99   # ok, try native perl UTF8 if available
100  SKIP:
101   {
102     $] >= 5.006 or skip("perl too old to test native UTF8 support", 5);
103     my $text;
104     # we need to do this in eval to prevent compile time errors in older
105     # versions
106     eval q{$text = "A\xA1\x{2010}A"}; # A, a with ogonek, HYPHEN, A in our test font
107     #$text = "A".chr(0xA1).chr(0x2010)."A"; # this one works too
108     ok(i_t1_text($backgr, 10, 180, $bgcolor, $fnum, 32, $text, length($text), 1),
109         "draw UTF8");
110     ok(i_t1_cp($backgr, 80, 180, 1, $fnum, 32, $text, length($text), 1),
111         "cp UTF8");
112     @utf8box = i_t1_bbox($fnum, 50.0, $text, length($text), 0);
113     is(@utf8box, 8, "native utf8 bbox element count");
114     ok(abs($utf8box[2] - $base[2]) <= $maxdiff, 
115       "compare box sizes native $utf8box[2] vs $base[2] (maxerror $maxdiff)");
116     eval q{$text = "A\xA1\xA2\x01\x1F\x{0100}A"};
117     ok(i_t1_text($backgr, 10, 220, $bgcolor, $fnum, 32, $text, 0, 1, 0, "uso"),
118        "more complex output");
119   }
120
121   open(FH,">testout/t30t1font2.ppm") || die "cannot open testout/t35t1font.ppm\n";
122   binmode(FH);
123   $IO = Imager::io_new_fd( fileno(FH) );
124   i_writeppm_wiol($backgr, $IO);
125   close(FH);
126
127   my $rc=i_t1_destroy($fnum);
128   unless (ok($rc >= 0, "i_t1_destroy")) {
129     print "# i_t1_destroy failed: rc=$rc\n";
130   }
131
132   print "# debug: ",join(" x ",i_t1_bbox(0,50,"eses",4) ),"\n";
133   print "# debug: ",join(" x ",i_t1_bbox(0,50,"llll",4) ),"\n";
134
135   # character existance tests - uses the special ExistenceTest font
136   my $exists_font = 'fontfiles/ExistenceTest.pfb';
137   my $exists_afm = 'fontfiles/ExistenceText.afm';
138   
139   -e $exists_font or die;
140     
141   my $font_num = Imager::i_t1_new($exists_font, $exists_afm);
142   SKIP: {
143     ok($font_num >= 0, 'loading test font')
144       or skip('Could not load test font', 6);
145     # first the list interface
146     my @exists = Imager::i_t1_has_chars($font_num, "!A");
147     is(@exists, 2, "return count from has_chars");
148     ok($exists[0], "we have an exclamation mark");
149     ok(!$exists[1], "we have no uppercase A");
150
151     # then the scalar interface
152     my $exists = Imager::i_t1_has_chars($font_num, "!A");
153     is(length($exists), 2, "return scalar length");
154     ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
155     ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
156     i_t1_destroy($font_num);
157   }
158   
159   my $font = Imager::Font->new(file=>$exists_font, type=>'t1');
160   SKIP:
161   {
162     ok($font, "loaded OO font")
163       or skip("Could not load test font", 24);
164     my @exists = $font->has_chars(string=>"!A");
165     is(@exists, 2, "return count from has_chars");
166     ok($exists[0], "we have an exclamation mark");
167     ok(!$exists[1], "we have no uppercase A");
168     
169     # then the scalar interface
170     my $exists = $font->has_chars(string=>"!A");
171     is(length($exists), 2, "return scalar length");
172     ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
173     ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
174
175     # check the advance width
176     my @bbox = $font->bounding_box(string=>'/', size=>100);
177     print "# @bbox\n";
178     isnt($bbox[2], $bbox[5], "different advance to pos_width");
179
180     # names
181     my $face_name = Imager::i_t1_face_name($font->{id});
182     print "# face $face_name\n";
183     is($face_name, 'ExistenceTest', "face name");
184     $face_name = $font->face_name;
185     is($face_name, 'ExistenceTest', "face name");
186
187     my @glyph_names = $font->glyph_names(string=>"!J/");
188     is($glyph_names[0], 'exclam', "check exclam name OO");
189     ok(!defined($glyph_names[1]), "check for no J name OO");
190     is($glyph_names[2], 'slash', "check slash name OO");
191
192     # this character chosen since when it's truncated to one byte it
193     # becomes 0x21 or '!' which the font does define
194     my $text = pack("C*", 0xE2, 0x80, 0xA1); # "\x{2021}" as utf-8
195     @glyph_names = $font->glyph_names(string=>$text, utf8=>1);
196     is($glyph_names[0], undef, "expect no glyph_name for \\x{20A1}");
197
198     # make sure a missing string parameter is handled correctly
199     eval {
200       $font->glyph_names();
201     };
202     is($@, "", "correct error handling");
203     cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
204
205     # test extended bounding box results
206     # the test font is known to have a shorter advance width for that char
207     @bbox = $font->bounding_box(string=>"/", size=>100);
208     is(@bbox, 8, "should be 8 entries");
209     isnt($bbox[6], $bbox[2], "different advance width");
210     my $bbox = $font->bounding_box(string=>"/", size=>100);
211     cmp_ok($bbox->pos_width, '>', $bbox->advance_width, "OO check");
212
213     cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
214
215     cmp_ok($bbox->display_width, '>', $bbox->advance_width,
216            "check display width (roughly)");
217
218     # check with a char that fits inside the box
219     $bbox = $font->bounding_box(string=>"!", size=>100);
220     print "# pos width ", $bbox->pos_width, "\n";
221
222     # they aren't the same historically for the type 1 driver
223     isnt($bbox->pos_width, $bbox->advance_width, 
224        "check backwards compatibility");
225     cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
226     cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
227     cmp_ok($bbox->display_width, '<', $bbox->advance_width,
228            "display smaller than advance");
229   }
230
231  SKIP:
232   { print "# alignment tests\n";
233     my $font = Imager::Font->new(file=>$deffont, type=>'t1');
234     ok($font, "loaded deffont OO")
235       or skip("could not load font:".Imager->errstr, 4);
236     my $im = Imager->new(xsize=>140, ysize=>150);
237     my %common = 
238       (
239        font=>$font, 
240        size=>40, 
241        aa=>1,
242       );
243     $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
244     $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
245     $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
246     for my $args ([ x=>5,   text=>"A", color=>"white" ],
247                   [ x=>40,  text=>"y", color=>"white" ],
248                   [ x=>75,  text=>"A", channel=>1 ],
249                   [ x=>110, text=>"y", channel=>1 ]) {
250       ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
251       ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
252       ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
253     }
254     ok($im->write(file=>'testout/t30align.ppm'), "save align image");
255   }
256
257  SKIP:
258   {
259     # see http://rt.cpan.org/Ticket/Display.html?id=20555
260     print "# bounding box around spaces\n";
261     # SpaceTest contains 3 characters, space, ! and .undef
262     # only characters that define character zero seem to illustrate
263     # the problem we had with spaces
264     my $space_fontfile = "fontfiles/SpaceTest.pfb";
265     my $font = Imager::Font->new(file => $space_fontfile, type => 't1');
266     ok($font, "loaded $deffont")
267       or skip("failed to load $deffont" . Imager->errstr, 13);
268     my $bbox = $font->bounding_box(string => "", size => 36);
269     print "# empty string bbox: @$bbox\n";
270     is($bbox->start_offset, 0, "empty string start_offset");
271     is($bbox->end_offset, 0, "empty string end_offset");
272     is($bbox->advance_width, 0, "empty string advance_width");
273     is($bbox->ascent, 0, "empty string ascent");
274     is($bbox->descent, 0, "empty string descent");
275
276     # a single space
277     my $bbox_space = $font->bounding_box(string => " ", size => 36);
278     print "# space bbox: @$bbox_space\n";
279     is($bbox_space->start_offset, 0, "single space start_offset");
280     is($bbox_space->end_offset, $bbox_space->advance_width, 
281        "single space end_offset");
282     cmp_ok($bbox_space->ascent, '>=', $bbox_space->descent,
283            "single space ascent/descent");
284
285     my $bbox_bang = $font->bounding_box(string => "!", size => 36);
286     print "# '!' bbox: @$bbox_bang\n";
287
288     # space ! space
289     my $bbox_spbangsp = $font->bounding_box(string => " ! ", size => 36);
290     print "# ' ! ' bbox: @$bbox_spbangsp\n";
291     my $exp_advance = $bbox_bang->advance_width + 2 * $bbox_space->advance_width;
292     is($bbox_spbangsp->advance_width, $exp_advance, "sp ! sp advance_width");
293     is($bbox_spbangsp->start_offset, 0, "sp ! sp start_offset");
294     is($bbox_spbangsp->end_offset, $exp_advance, "sp ! sp end_offset");
295   }
296
297  SKIP:
298   { # http://rt.cpan.org/Ticket/Display.html?id=20554
299     # this is "A\xA1\x{2010}A"
300     # the t1 driver is meant to ignore any UTF8 characters over 0xff
301     print "# issue 20554\n";
302     my $text = pack("C*", 0x41, 0xC2, 0xA1, 0xE2, 0x80, 0x90, 0x41);
303     my $tran_text = "A\xA1A";
304     my $font = Imager::Font->new(file => 'fontfiles/dcr10.pfb', type => 't1');
305     $font
306       or skip("cannot load font fontfiles/fcr10.pfb:".Imager->errstr, 1);
307     my $bbox_utf8 = $font->bounding_box(string => $text, utf8 => 1, size => 36);
308     my $bbox_tran = $font->bounding_box(string => $tran_text, size => 36);
309     is($bbox_utf8->advance_width, $bbox_tran->advance_width,
310        "advance widths should match");
311   }
312   { # string output cut off at NUL ('\0')
313     # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
314     my $font = Imager::Font->new(file => 'fontfiles/dcr10.pfb', type => 't1');
315     ok($font, "loaded dcr10.pfb");
316
317     diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
318                        font => $font, color => '#FFFFFF');
319     diff_text_with_nul("a\\0b vs a", "a\0b", "a", 
320                        font => $font, channel => 1);
321
322     # UTF8 encoded \xBF
323     my $pound = pack("C*", 0xC2, 0xBF);
324     diff_text_with_nul("utf8 pound\0pound vs pound", "$pound\0$pound", $pound,
325                        font => $font, color => '#FFFFFF', utf8 => 1);
326     diff_text_with_nul("utf8 dash\0dash vs dash", "$pound\0$pound", $pound,
327                        font => $font, channel => 1, utf8 => 1);
328
329   }
330
331   { # RT 11972
332     # when rendering to a transparent image the coverage should be
333     # expressed in terms of the alpha channel rather than the color
334     my $font = Imager::Font->new(file=>'fontfiles/dcr10.pfb', type=>'t1');
335     my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
336     ok($im->string(string => "AB", size => 20, aa => 2, color => '#F00',
337                    x => 0, y => 15, font => $font),
338        "draw to transparent image");
339     my $im_noalpha = $im->convert(preset => 'noalpha');
340     my $im_pal = $im->to_paletted(make_colors => 'mediancut');
341     my @colors = $im_pal->getcolors;
342     is(@colors, 2, "should be only 2 colors");
343     @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
344     is_color3($colors[0], 0, 0, 0, "check we got black");
345     is_color3($colors[1], 255, 0, 0, "and red");
346   }
347 }
348
349 #malloc_state();