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'
5 ######################### We start with some black magic to print on failure.
7 # Change 1..1 below to 1..last_test_to_print .
8 # (It may become useful if the test is moved to ./t subdirectory.)
10 use Test::More tests => 94;
11 BEGIN { use_ok(Imager => ':all') }
12 use Imager::Test qw(diff_text_with_nul is_color3);
16 init_log("testout/t30t1font.log",1);
18 my $deffont = './fontfiles/dcr10.pfb';
20 my $fontname_pfb=$ENV{'T1FONTTESTPFB'}||$deffont;
21 my $fontname_afm=$ENV{'T1FONTTESTAFM'}||'./fontfiles/dcr10.afm';
25 if (!(i_has_format("t1")) ) {
26 skip("t1lib unavailable or disabled", 93);
28 elsif (! -f $fontname_pfb) {
29 skip("cannot find fontfile for type 1 test $fontname_pfb", 93);
31 elsif (! -f $fontname_afm) {
32 skip("cannot find fontfile for type 1 test $fontname_afm", 93);
39 unlink "t1lib.log"; # lose it if it exists
41 ok(!-e("t1lib.log"), "disable t1log");
43 ok(-e("t1lib.log"), "enable t1log");
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);
52 my $bgcolor=Imager::Color->new(255,0,0,0);
53 my $overlay=Imager::ImgRaw::new(200,70,3);
55 ok(i_t1_cp($overlay,5,50,1,$fnum,50.0,'XMCLH',5,1), "i_t1_cp");
57 i_line($overlay,0,50,100,50,$bgcolor,1);
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";
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);
69 $bgcolor=Imager::Color::set($bgcolor,200,200,200,0);
70 my $backgr=Imager::ImgRaw::new(280,300,3);
73 ok(i_t1_text($backgr,10,100,$bgcolor,$fnum,150.0,'test',4,1), "i_t1_text");
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
79 # the following is "A\xA1\x{2010}A"
81 my $text = pack("C*", 0x41, 0xC2, 0xA1, 0xE2, 0x80, 0x90, 0x41);
82 my $alttext = "A\xA1A";
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)");
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");
96 ok(i_t1_cp($backgr, 80, 140, 1, $fnum, 32, $text, length($text), 1, 1),
97 "cp hand-encoded UTF8");
99 # ok, try native perl UTF8 if available
102 $] >= 5.006 or skip("perl too old to test native UTF8 support", 5);
104 # we need to do this in eval to prevent compile time errors in older
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),
110 ok(i_t1_cp($backgr, 80, 180, 1, $fnum, 32, $text, length($text), 1),
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");
121 open(FH,">testout/t30t1font2.ppm") || die "cannot open testout/t35t1font.ppm\n";
123 $IO = Imager::io_new_fd( fileno(FH) );
124 i_writeppm_wiol($backgr, $IO);
127 my $rc=i_t1_destroy($fnum);
128 unless (ok($rc >= 0, "i_t1_destroy")) {
129 print "# i_t1_destroy failed: rc=$rc\n";
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";
135 # character existance tests - uses the special ExistenceTest font
136 my $exists_font = 'fontfiles/ExistenceTest.pfb';
137 my $exists_afm = 'fontfiles/ExistenceText.afm';
139 -e $exists_font or die;
141 my $font_num = Imager::i_t1_new($exists_font, $exists_afm);
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");
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");
158 my $font = Imager::Font->new(file=>$exists_font, type=>'t1');
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");
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");
174 # check the advance width
175 my @bbox = $font->bounding_box(string=>'/', size=>100);
177 isnt($bbox[2], $bbox[5], "different advance to pos_width");
180 my $face_name = Imager::i_t1_face_name($font->{id});
181 print "# face $face_name\n";
182 is($face_name, 'ExistenceTest', "face name");
183 $face_name = $font->face_name;
184 is($face_name, 'ExistenceTest', "face name");
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");
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}");
197 # make sure a missing string parameter is handled correctly
199 $font->glyph_names();
201 is($@, "", "correct error handling");
202 cmp_ok(Imager->errstr, '=~', qr/no string parameter/, "error message");
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");
212 cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
214 cmp_ok($bbox->display_width, '>', $bbox->advance_width,
215 "check display width (roughly)");
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";
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");
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);
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");
253 ok($im->write(file=>'testout/t30align.ppm'), "save align image");
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");
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");
284 my $bbox_bang = $font->bounding_box(string => "!", size => 36);
285 print "# '!' bbox: @$bbox_bang\n";
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");
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');
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");
311 { # string output cut off at NUL ('\0')
312 # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
313 my $font = Imager::Font->new(file => 'fontfiles/dcr10.pfb', type => 't1');
314 ok($font, "loaded dcr10.pfb");
316 diff_text_with_nul("a\\0b vs a", "a\0b", "a",
317 font => $font, color => '#FFFFFF');
318 diff_text_with_nul("a\\0b vs a", "a\0b", "a",
319 font => $font, channel => 1);
322 my $pound = pack("C*", 0xC2, 0xBF);
323 diff_text_with_nul("utf8 pound\0pound vs pound", "$pound\0$pound", $pound,
324 font => $font, color => '#FFFFFF', utf8 => 1);
325 diff_text_with_nul("utf8 dash\0dash vs dash", "$pound\0$pound", $pound,
326 font => $font, channel => 1, utf8 => 1);
331 # when rendering to a transparent image the coverage should be
332 # expressed in terms of the alpha channel rather than the color
333 my $font = Imager::Font->new(file=>'fontfiles/dcr10.pfb', type=>'t1');
334 my $im = Imager->new(xsize => 40, ysize => 20, channels => 4);
335 ok($im->string(string => "AB", size => 20, aa => 2, color => '#F00',
336 x => 0, y => 15, font => $font),
337 "draw to transparent image");
338 my $im_noalpha = $im->convert(preset => 'noalpha');
339 my $im_pal = $im->to_paletted(make_colors => 'mediancut');
340 my @colors = $im_pal->getcolors;
341 is(@colors, 2, "should be only 2 colors");
342 @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
343 is_color3($colors[0], 0, 0, 0, "check we got black");
344 is_color3($colors[1], 255, 0, 0, "and red");