]> git.imager.perl.org - imager.git/blob - t/t30t1font.t
Removed the query asking if GIF support should be disabled, since
[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 lib 't';
11 use Test::More tests => 77;
12 BEGIN { use_ok(Imager => ':all') }
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", 76);
27   }
28   elsif (! -f $fontname_pfb) {
29     skip("cannot find fontfile for type 1 test $fontname_pfb", 76);
30   }
31   elsif (! -f $fontname_afm) {
32     skip("cannot find fontfile for type 1 test $fontname_afm", 76);
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", 48);
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   }
157   
158   my $font = Imager::Font->new(file=>$exists_font, type=>'t1');
159   SKIP:
160   {
161     ok($font, "loaded OO font")
162       or skip("Could not load test font", 24);
163     my @exists = $font->has_chars(string=>"!A");
164     is(@exists, 2, "return count from has_chars");
165     ok($exists[0], "we have an exclamation mark");
166     ok(!$exists[1], "we have no uppercase A");
167     
168     # then the scalar interface
169     my $exists = $font->has_chars(string=>"!A");
170     is(length($exists), 2, "return scalar length");
171     ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
172     ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
173
174     # check the advance width
175     my @bbox = $font->bounding_box(string=>'/', size=>100);
176     print "# @bbox\n";
177     isnt($bbox[2], $bbox[5], "different advance to pos_width");
178
179     # names
180     my $face_name = Imager::i_t1_face_name($font->{id});
181     print "# face $face_name\n";
182     ok($face_name eq 'ExistenceTest', "face name");
183     $face_name = $font->face_name;
184     ok($face_name eq 'ExistenceTest', "face name");
185
186     my @glyph_names = $font->glyph_names(string=>"!J/");
187     is($glyph_names[0], 'exclam', "check exclam name OO");
188     ok(!defined($glyph_names[1]), "check for no J name OO");
189     is($glyph_names[2], 'slash', "check slash name OO");
190
191     # this character chosen since when it's truncated to one byte it
192     # becomes 0x21 or '!' which the font does define
193     my $text = pack("C*", 0xE2, 0x80, 0xA1); # "\x{2021}" as utf-8
194     @glyph_names = $font->glyph_names(string=>$text, utf8=>1);
195     is($glyph_names[0], undef, "expect no glyph_name for \\x{20A1}");
196
197     # make sure a missing string parameter is handled correctly
198     eval {
199       $font->glyph_names();
200     };
201     is($@, "", "correct error handling");
202     cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
203
204     # test extended bounding box results
205     # the test font is known to have a shorter advance width for that char
206     @bbox = $font->bounding_box(string=>"/", size=>100);
207     is(@bbox, 8, "should be 8 entries");
208     isnt($bbox[6], $bbox[2], "different advance width");
209     my $bbox = $font->bounding_box(string=>"/", size=>100);
210     cmp_ok($bbox->pos_width, '>', $bbox->advance_width, "OO check");
211
212     cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
213
214     cmp_ok($bbox->display_width, '>', $bbox->advance_width,
215            "check display width (roughly)");
216
217     # check with a char that fits inside the box
218     $bbox = $font->bounding_box(string=>"!", size=>100);
219     print "# pos width ", $bbox->pos_width, "\n";
220
221     # they aren't the same historically for the type 1 driver
222     isnt($bbox->pos_width, $bbox->advance_width, 
223        "check backwards compatibility");
224     cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
225     cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
226     cmp_ok($bbox->display_width, '<', $bbox->advance_width,
227            "display smaller than advance");
228   }
229
230  SKIP:
231   { print "# alignment tests\n";
232     my $font = Imager::Font->new(file=>$deffont, type=>'t1');
233     ok($font, "loaded deffont OO")
234       or skip("could not load font:".Imager->errstr, 4);
235     my $im = Imager->new(xsize=>140, ysize=>150);
236     my %common = 
237       (
238        font=>$font, 
239        size=>40, 
240        aa=>1,
241       );
242     $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
243     $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
244     $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
245     for my $args ([ x=>5,   text=>"A", color=>"white" ],
246                   [ x=>40,  text=>"y", color=>"white" ],
247                   [ x=>75,  text=>"A", channel=>1 ],
248                   [ x=>110, text=>"y", channel=>1 ]) {
249       ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
250       ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
251       ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
252     }
253     ok($im->write(file=>'testout/t30align.ppm'), "save align image");
254   }
255
256  SKIP:
257   {
258     # see http://rt.cpan.org/Ticket/Display.html?id=20555
259     print "# bounding box around spaces\n";
260     # SpaceTest contains 3 characters, space, ! and .undef
261     # only characters that define character zero seem to illustrate
262     # the problem we had with spaces
263     my $space_fontfile = "fontfiles/SpaceTest.pfb";
264     my $font = Imager::Font->new(file => $space_fontfile, type => 't1');
265     ok($font, "loaded $deffont")
266       or skip("failed to load $deffont" . Imager->errstr, 13);
267     my $bbox = $font->bounding_box(string => "", size => 36);
268     print "# empty string bbox: @$bbox\n";
269     is($bbox->start_offset, 0, "empty string start_offset");
270     is($bbox->end_offset, 0, "empty string end_offset");
271     is($bbox->advance_width, 0, "empty string advance_width");
272     is($bbox->ascent, 0, "empty string ascent");
273     is($bbox->descent, 0, "empty string descent");
274
275     # a single space
276     my $bbox_space = $font->bounding_box(string => " ", size => 36);
277     print "# space bbox: @$bbox_space\n";
278     is($bbox_space->start_offset, 0, "single space start_offset");
279     is($bbox_space->end_offset, $bbox_space->advance_width, 
280        "single space end_offset");
281     cmp_ok($bbox_space->ascent, '>=', $bbox_space->descent,
282            "single space ascent/descent");
283
284     my $bbox_bang = $font->bounding_box(string => "!", size => 36);
285     print "# '!' bbox: @$bbox_bang\n";
286
287     # space ! space
288     my $bbox_spbangsp = $font->bounding_box(string => " ! ", size => 36);
289     print "# ' ! ' bbox: @$bbox_spbangsp\n";
290     my $exp_advance = $bbox_bang->advance_width + 2 * $bbox_space->advance_width;
291     is($bbox_spbangsp->advance_width, $exp_advance, "sp ! sp advance_width");
292     is($bbox_spbangsp->start_offset, 0, "sp ! sp start_offset");
293     is($bbox_spbangsp->end_offset, $exp_advance, "sp ! sp end_offset");
294   }
295
296  SKIP:
297   { # http://rt.cpan.org/Ticket/Display.html?id=20554
298     # this is "A\xA1\x{2010}A"
299     # the t1 driver is meant to ignore any UTF8 characters over 0xff
300     print "# issue 20554\n";
301     my $text = pack("C*", 0x41, 0xC2, 0xA1, 0xE2, 0x80, 0x90, 0x41);
302     my $tran_text = "A\xA1A";
303     my $font = Imager::Font->new(file => 'fontfiles/dcr10.pfb', type => 't1');
304     $font
305       or skip("cannot load font fontfiles/fcr10.pfb:".Imager->errstr, 1);
306     my $bbox_utf8 = $font->bounding_box(string => $text, utf8 => 1, size => 36);
307     my $bbox_tran = $font->bounding_box(string => $tran_text, size => 36);
308     is($bbox_utf8->advance_width, $bbox_tran->advance_width,
309        "advance widths should match");
310   }
311 }
312
313 #malloc_state();