5 use Imager::Test qw(diff_text_with_nul is_color3 is_image isnt_image);
7 use Cwd qw(getcwd abs_path);
11 ok($Imager::formats{t1}, "must have t1");
13 -d "testout" or mkdir "testout";
14 ok(-d "testout", "make output directory");
16 init_log("testout/t10type1.log",1);
18 my $deffont = 'fontfiles/dcr10.pfb';
20 my $fontname_pfb=$ENV{'T1FONTTESTPFB'}||$deffont;
21 my $fontname_afm=$ENV{'T1FONTTESTAFM'}||'./fontfiles/dcr10.afm';
24 or skip_all("cannot find fontfile for type 1 test $fontname_pfb");
26 or skip_all("cannot find fontfile for type 1 test $fontname_afm");
34 unlink "t1lib.log"; # lose it if it exists
36 ok(!-e("t1lib.log"), "disable t1log");
38 ok(-e("t1lib.log"), "enable t1log");
42 my $fnum=Imager::Font::T1xs->new($fontname_pfb,$fontname_afm); # this will load the pfb font
43 unless (ok($fnum >= 0, "load font $fontname_pfb")) {
44 skip("without the font I can't do a thing", 90);
47 my $bgcolor=Imager::Color->new(255,0,0,255);
48 my $overlay=Imager::ImgRaw::new(200,70,3);
50 ok($fnum->cp($overlay,5,50,1,50.0,'XMCLH',1), "i_t1_cp");
52 i_line($overlay,0,50,100,50,$bgcolor,1);
54 my @bbox=$fnum->bbox(50.0,'XMCLH');
55 is(@bbox, 8, "i_t1_bbox");
56 print "# bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
58 open(FH,">testout/t30t1font.ppm") || die "cannot open testout/t35t1font.ppm\n";
59 binmode(FH); # for os2
60 my $IO = Imager::io_new_fd( fileno(FH) );
61 i_writeppm_wiol($overlay,$IO);
64 $bgcolor=Imager::Color::set($bgcolor,200,200,200,255);
65 my $backgr=Imager::ImgRaw::new(280,300,3);
67 ok($fnum->text($backgr,10,100,$bgcolor,150.0,'test',1,2), "i_t1_text");
70 # for perl < 5.6 we can hand-encode text
71 # since T1 doesn't support over 256 chars in an encoding we just drop
73 # the following is "A\xA1\x{2010}A"
75 my $text = pack("C*", 0x41, 0xC2, 0xA1, 0xE2, 0x80, 0x90, 0x41);
76 my $alttext = "A\xA1A";
78 my @utf8box = $fnum->bbox(50.0, $text, 1);
79 is(@utf8box, 8, "utf8 bbox element count");
80 my @base = $fnum->bbox(50.0, $alttext, 0);
81 is(@base, 8, "alt bbox element count");
82 my $maxdiff = $fontname_pfb eq $deffont ? 0 : $base[2] / 3;
83 print "# (@utf8box vs @base)\n";
84 ok(abs($utf8box[2] - $base[2]) <= $maxdiff,
85 "compare box sizes $utf8box[2] vs $base[2] (maxerror $maxdiff)");
87 # hand-encoded UTF8 drawing
88 ok($fnum->text($backgr, 10, 140, $bgcolor, 32, $text, 1,1), "draw hand-encoded UTF8");
90 ok($fnum->cp($backgr, 80, 140, 1, 32, $text, 1, 1),
91 "cp hand-encoded UTF8");
94 my $text = pack("C", 0xC0);
95 ok(!$fnum->text($backgr, 10, 140, $bgcolor, 32, $text, 1, 1),
96 "attempt to draw invalid utf8");
97 is(Imager->_error_as_msg, "invalid UTF8 character",
101 # ok, try native perl UTF8 if available
104 $] >= 5.006 or skip("perl too old to test native UTF8 support", 5);
106 # we need to do this in eval to prevent compile time errors in older
108 eval q{$text = "A\xA1\x{2010}A"}; # A, a with ogonek, HYPHEN, A in our test font
109 #$text = "A".chr(0xA1).chr(0x2010)."A"; # this one works too
110 Imager->log("draw UTF8\n");
111 ok($fnum->text($backgr, 10, 180, $bgcolor, 32, $text, 1),
113 ok($fnum->cp($backgr, 80, 180, 1, 32, $text, 1),
115 @utf8box = $fnum->bbox(50.0, $text, 0);
116 is(@utf8box, 8, "native utf8 bbox element count");
117 ok(abs($utf8box[2] - $base[2]) <= $maxdiff,
118 "compare box sizes native $utf8box[2] vs $base[2] (maxerror $maxdiff)");
119 eval q{$text = "A\xA1\xA2\x01\x1F\x{0100}A"};
120 ok($fnum->text($backgr, 10, 220, $bgcolor, 32, $text, 0, 1, "uso"),
121 "more complex output");
124 open(FH,">testout/t30t1font2.ppm") || die "cannot open testout/t35t1font.ppm\n";
126 $IO = Imager::io_new_fd( fileno(FH) );
127 i_writeppm_wiol($backgr, $IO);
132 # character existance tests - uses the special ExistenceTest font
133 my $exists_font = 'fontfiles/ExistenceTest.pfb';
134 my $exists_afm = 'fontfiles/ExistenceText.afm';
136 -e $exists_font or die "$exists_font not found";
138 my $font_num = Imager::Font::T1xs->new($exists_font, $exists_afm);
140 ok($font_num >= 0, 'loading test font')
141 or skip('Could not load test font', 6);
142 # first the list interface
143 my @exists = $font_num->has_chars("!A");
144 is(@exists, 2, "return count from has_chars");
145 ok($exists[0], "we have an exclamation mark");
146 ok(!$exists[1], "we have no uppercase A");
148 # then the scalar interface
149 my $exists = $font_num->has_chars("!A");
150 is(length($exists), 2, "return scalar length");
151 ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
152 ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
156 my $font = Imager::Font->new(file=>$exists_font, type=>'t1');
159 ok($font, "loaded OO font")
160 or skip("Could not load test font", 24);
161 my @exists = $font->has_chars(string=>"!A");
162 is(@exists, 2, "return count from has_chars");
163 ok($exists[0], "we have an exclamation mark");
164 ok(!$exists[1], "we have no uppercase A");
166 # then the scalar interface
167 my $exists = $font->has_chars(string=>"!A");
168 is(length($exists), 2, "return scalar length");
169 ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
170 ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
172 # check the advance width
173 my @bbox = $font->bounding_box(string=>'/', size=>100);
175 isnt($bbox[2], $bbox[5], "different advance to pos_width");
178 my $face_name = $font->{t1font}->face_name();
179 print "# face $face_name\n";
180 is($face_name, 'ExistenceTest', "face name");
181 $face_name = $font->face_name;
182 is($face_name, 'ExistenceTest', "face name");
184 my @glyph_names = $font->glyph_names(string=>"!J/");
185 is($glyph_names[0], 'exclam', "check exclam name OO");
186 ok(!defined($glyph_names[1]), "check for no J name OO");
187 is($glyph_names[2], 'slash', "check slash name OO");
189 # this character chosen since when it's truncated to one byte it
190 # becomes 0x21 or '!' which the font does define
191 my $text = pack("C*", 0xE2, 0x80, 0xA1); # "\x{2021}" as utf-8
192 @glyph_names = $font->glyph_names(string=>$text, utf8=>1);
193 is($glyph_names[0], undef, "expect no glyph_name for \\x{20A1}");
195 my @bad = $font->glyph_names(string => "ab\xC0", utf8 => 1);
196 is(@bad, 0, "should be no results for bad utf8");
198 # make sure a missing string parameter is handled correctly
200 $font->glyph_names();
202 is($@, "", "correct error handling");
203 cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
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");
213 cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
215 cmp_ok($bbox->display_width, '>', $bbox->advance_width,
216 "check display width (roughly)");
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";
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");
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);
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");
254 ok($im->write(file=>'testout/t30align.ppm'), "save align image");
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 $space_fontfile")
267 or skip("failed to load $space_fontfile" . 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");
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");
285 my $bbox_bang = $font->bounding_box(string => "!", size => 36);
286 print "# '!' bbox: @$bbox_bang\n";
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");
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');
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");
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");
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);
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);
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");
350 # checks that a c:foo or c:\foo path is handled correctly on win32
352 $^O eq "MSWin32" || $^O eq "cygwin"
353 or skip("only for win32", 2);
355 or skip("Cannot get cwd", 2);
356 if ($^O eq "cygwin") {
357 $dir = Cygwin::posix_to_win_path($dir);
359 my $abs_path = abs_path($deffont);
360 my $font = Imager::Font->new(file => $abs_path, type => $type);
361 ok($font, "found font by absolute path")
362 or print "# path $abs_path\n";
366 and skip("cygwin doesn't support drive relative DOSsish paths", 1);
367 my ($drive) = $dir =~ /^([a-z]:)/i
368 or skip("cwd has no drive letter", 2);
369 my $drive_path = $drive . $deffont;
370 $font = Imager::Font->new(file => $drive_path, type => $type);
371 ok($font, "found font by drive relative path")
372 or print "# path $drive_path\n";
376 Imager->log("Testing aa levels", 1);
377 my $f1 = Imager::Font->new(file => $deffont, type => "t1");
378 is($f1->{t1aa}, 2, "should have default aa level");
379 my $imbase = Imager->new(xsize => 100, ysize => 20);
380 ok($imbase->string(text => "test", size => 18, x => 5, y => 18,
381 color => "#FFF", font => $f1, aa => 1),
382 "draw text with def aa level");
383 ok(Imager::Font::T1->set_aa_level(1), "set aa level to 1");
384 my $f2 = Imager::Font->new(file => $deffont, type => "t1");
385 is($f2->{t1aa}, 1, "new font has new aa level");
386 my $imaa1 = Imager->new(xsize => 100, ysize => 20);
387 ok($imaa1->string(text => "test", size => 18, x => 5, y => 18,
388 color => "#FFF", font => $f2, aa => 1),
389 "draw text with non-def aa level");
390 isnt_image($imbase, $imaa1, "images should differ");
391 ok($f2->set_aa_level(2), "set aa level of font");
392 is($f2->{t1aa}, 2, "check new aa level");
393 my $imaa2 = Imager->new(xsize => 100, ysize => 20);
394 ok($imaa2->string(text => "test", size => 18, x => 5, y => 18,
395 color => "#FFF", font => $f2, aa => 1),
396 "draw text with non-def but 2 aa level");
397 is_image($imbase, $imaa2, "check images match");
400 { # error handling check
401 my $im = Imager->new(xsize => 100, ysize => 20);
402 my $fnum = Imager::Font->new(file => $deffont, type => "t1");
403 ok(!$im->string(font => $fnum, string => "text", size => -10),
405 is($im->errstr, "i_t1_text(): T1_AASetString failed: Invalid Argument in Function Call",
406 "check error message");