]> git.imager.perl.org - imager.git/blob - t/t37w32font.t
allow Imager to be loaded on Windows 98
[imager.git] / t / t37w32font.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 54;
4 BEGIN { use_ok(Imager => ':all') }
5 use Imager::Test qw(diff_text_with_nul);
6 ++$|;
7
8 init_log("testout/t37w32font.log",1);
9
10 SKIP:
11 {
12   i_has_format('w32') or skip("no MS Windows", 53);
13   print "# has w32\n";
14
15   my $fontname=$ENV{'TTFONTTEST'} || 'Times New Roman Bold';
16   
17   # i_init_fonts(); # unnecessary for Win32 font support
18
19   my $bgcolor=i_color_new(255,0,0,0);
20   my $overlay=Imager::ImgRaw::new(200,70,3);
21   
22   my @bbox=Imager::i_wf_bbox($fontname, 50.0,'XMCLH');
23   print "#bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
24   
25   ok(Imager::i_wf_cp($fontname,$overlay,5,50,1,50.0,'XMCLH',1,1),
26      "i_wf_cp smoke test");
27   i_line($overlay,0,50,100,50,$bgcolor, 1);
28   
29   open(FH,">testout/t37w32font.ppm") || die "cannot open testout/t37w32font.ppm\n";
30   binmode(FH);
31   my $io = Imager::io_new_fd(fileno(FH));
32   i_writeppm_wiol($overlay,$io);
33   close(FH);
34   
35   $bgcolor=i_color_set($bgcolor,200,200,200,0);
36   my $backgr=Imager::ImgRaw::new(500,300,3);
37   
38   ok(Imager::i_wf_text($fontname,$backgr,100,100,$bgcolor,100,'MAW.',1, 1),
39      "i_wf_text smoke test");
40   i_line($backgr,0, 100, 499, 100, NC(0, 0, 255), 1);
41   
42   open(FH,">testout/t37w32font2.ppm") || die "cannot open testout/t37w32font2.ppm\n";
43   binmode(FH);
44   $io = Imager::io_new_fd(fileno(FH));
45   i_writeppm_wiol($backgr,$io);
46   close(FH);
47   
48   my $img = Imager->new(xsize=>200, ysize=>200);
49   my $font = Imager::Font->new(face=>$fontname, size=>20);
50   ok($img->string('x'=>30, 'y'=>30, string=>"Imager", color=>NC(255, 0, 0), 
51                font=>$font),
52      "string with win32 smoke test")
53     or print "# ",$img->errstr,"\n";
54   $img->write(file=>'testout/t37_oo.ppm') or print "not ";
55   my @bbox2 = $font->bounding_box(string=>'Imager');
56   is(@bbox2, 8, "got 8 values from bounding_box");
57
58   # this only applies while the Win32 driver returns 6 values
59   # at this point we don't return the advance width from the low level
60   # bounding box function, so the Imager::Font::BBox advance method should
61   # return end_offset, check it does
62   my $bbox = $font->bounding_box(string=>"some text");
63   ok($bbox, "got the bounding box object");
64   is($bbox->advance_width, $bbox->end_offset, 
65      "check advance_width fallback correct");
66
67  SKIP:
68   {
69     $^O eq 'cygwin' and skip("Too hard to get correct directory for test font on cygwin", 13);
70     ok(Imager::i_wf_addfont("fontfiles/ExistenceTest.ttf"), "add test font")
71       or print "# ",Imager::_error_as_msg(),"\n";
72     
73     my $namefont = Imager::Font->new(face=>"ExistenceTest");
74     ok($namefont, "create font based on added font");
75     
76     # the test font is known to have a shorter advance width for that char
77     @bbox = $namefont->bounding_box(string=>"/", size=>100);
78     print "# / box: @bbox\n";
79     is(@bbox, 8, "should be 8 entries");
80     isnt($bbox[6], $bbox[2], "different advance width");
81     $bbox = $namefont->bounding_box(string=>"/", size=>100);
82     ok($bbox->pos_width != $bbox->advance_width, "OO check");
83     
84     cmp_ok($bbox->right_bearing, '<', 0, "check right bearing");
85   
86     cmp_ok($bbox->display_width, '>', $bbox->advance_width,
87            "check display width (roughly)");
88     
89     my $im = Imager->new(xsize=>200, ysize=>200);
90     $im->box(filled => 1, color => '#202020');
91     $im->box(box => [ 20 + $bbox->neg_width, 100-$bbox->ascent,
92                       20+$bbox->advance_width-$bbox->right_bearing, 100-$bbox->descent ],
93              color => '#101010', filled => 1);
94     $im->line(color=>'blue', x1=>20, y1=>0, x2=>20, y2=>199);
95     my $right = 20 + $bbox->advance_width;
96     $im->line(color=>'blue', x1=>$right, y1=>0, x2=>$right, y2=>199);
97     $im->line(color=>'blue', x1=>0, y1 => 100, x2=>199, y2 => 100);
98     ok($im->string(font=>$namefont, text=>"/", x=>20, y=>100, color=>'white', size=>100),
99        "draw / from ExistenceText")
100         or print "# ", $im->errstr, "\n";
101     $im->setpixel(x => 20+$bbox->neg_width, y => 100-$bbox->ascent, color => 'red');
102     $im->setpixel(x => 20+$bbox->advance_width - $bbox->right_bearing, y => 100-$bbox->descent, color => 'red');
103     $im->write(file=>'testout/t37w32_slash.ppm');
104     
105     # check with a char that fits inside the box
106     $bbox = $namefont->bounding_box(string=>"!", size=>100);
107     print "# pos width ", $bbox->pos_width, "\n";
108     print "# ! box: @$bbox\n";
109     is($bbox->pos_width, $bbox->advance_width, 
110      "check backwards compatibility");
111     cmp_ok($bbox->left_bearing, '>', 0, "left bearing positive");
112     cmp_ok($bbox->right_bearing, '>', 0, "right bearing positive");
113     cmp_ok($bbox->display_width, '<', $bbox->advance_width,
114            "display smaller than advance");
115
116     $im = Imager->new(xsize=>200, ysize=>200);
117     $im->box(filled => 1, color => '#202020');
118     $im->box(box => [ 20 + $bbox->neg_width, 100-$bbox->ascent,
119                       20+$bbox->advance_width-$bbox->right_bearing, 100-$bbox->descent ],
120              color => '#101010', filled => 1);
121     $im->line(color=>'blue', x1=>20, y1=>0, x2=>20, y2=>199);
122     $right = 20 + $bbox->advance_width;
123     $im->line(color=>'blue', x1=>$right, y1=>0, x2=>$right, y2=>199);
124     $im->line(color=>'blue', x1=>0, y1 => 100, x2=>199, y2 => 100);
125     ok($im->string(font=>$namefont, text=>"!", x=>20, y=>100, color=>'white', size=>100),
126        "draw / from ExistenceText")
127         or print "# ", $im->errstr, "\n";
128     $im->setpixel(x => 20+$bbox->neg_width, y => 100-$bbox->ascent, color => 'red');
129     $im->setpixel(x => 20+$bbox->advance_width - $bbox->right_bearing, y => 100-$bbox->descent, color => 'red');
130     $im->write(file=>'testout/t37w32_bang.ppm');
131
132     Imager::i_wf_delfont("fontfiles/ExistenceTest.ttf");
133   }
134
135  SKIP:
136   { print "# alignment tests\n";
137     my $font = Imager::Font->new(face=>"Arial");
138     ok($font, "loaded Arial OO")
139       or skip("could not load font:".Imager->errstr, 4);
140     my $im = Imager->new(xsize=>140, ysize=>150);
141     my %common = 
142       (
143        font=>$font, 
144        size=>40, 
145        aa=>1,
146       );
147     $im->line(x1=>0, y1=>40, x2=>139, y2=>40, color=>'blue');
148     $im->line(x1=>0, y1=>90, x2=>139, y2=>90, color=>'blue');
149     $im->line(x1=>0, y1=>110, x2=>139, y2=>110, color=>'blue');
150     for my $args ([ x=>5,   text=>"A", color=>"white" ],
151                   [ x=>40,  text=>"y", color=>"white" ],
152                   [ x=>75,  text=>"A", channel=>1 ],
153                   [ x=>110, text=>"y", channel=>1 ]) {
154       ok($im->string(%common, @$args, 'y'=>40), "A no alignment");
155       ok($im->string(%common, @$args, 'y'=>90, align=>1), "A align=1");
156       ok($im->string(%common, @$args, 'y'=>110, align=>0), "A align=0");
157     }
158     ok($im->write(file=>'testout/t37align.ppm'), "save align image");
159   }
160   { print "# utf 8 support\n";
161     my $font = Imager::Font->new(face => "Arial");
162     ok($font, "created font");
163     my $im = Imager->new(xsize => 100, ysize => 100);
164     ok($im->string(string => "\xE2\x98\xBA", size => 80, aa => 1, utf8 => 1, 
165                    color => "white", font => $font, x => 5, y => 80),
166        "draw in utf8 (hand encoded)")
167         or print "# ", $im->errstr, "\n";
168     ok($im->write(file=>'testout/t37utf8.ppm'), "save utf8 image");
169
170     # native perl utf8
171     # Win32 only supported on 5.6+
172     # since this gets compiled even on older perls we need to be careful 
173     # creating the string
174     my $text;
175     eval q{$text = "\x{263A}"}; # A, HYPHEN, A in our test font
176     my $im2 = Imager->new(xsize => 100, ysize => 100);
177     ok($im2->string(string => $text, size => 80, aa => 1,
178                     color => 'white', font => $font, x => 5, y => 80),
179        "draw in utf8 (perl utf8)")
180         or print "# ", $im->errstr, "\n";
181     ok($im2->write(file=>'testout/t37utf8b.ppm'), "save utf8 image");
182     is(Imager::i_img_diff($im->{IMG}, $im2->{IMG}), 0,
183        "check result is the same");
184
185     # bounding box
186     cmp_ok($font->bounding_box(string=>$text, size => 80)->advance_width, '<', 100,
187            "check we only get width of single char rather than 3");
188   }
189
190   { # string output cut off at NUL ('\0')
191     # https://rt.cpan.org/Ticket/Display.html?id=21770 cont'd
192     my $font = Imager::Font->new(face=>'Arial', type=>'w32');
193     ok($font, "loaded Arial");
194
195     diff_text_with_nul("a\\0b vs a", "a\0b - color", "a", 
196                        font => $font, color => '#FFFFFF');
197     diff_text_with_nul("a\\0b vs a", "a\0b - channel", "a", 
198                        font => $font, channel => 1);
199
200     # UTF8 encoded \x{2010}
201     my $dash = pack("C*", 0xE2, 0x80, 0x90);
202     diff_text_with_nul("utf8 dash\0dash vs dash - color", "$dash\0$dash", $dash,
203                        font => $font, color => '#FFFFFF', utf8 => 1);
204     diff_text_with_nul("utf8 dash\0dash vs dash - channel", "$dash\0$dash", $dash,
205                        font => $font, channel => 1, utf8 => 1);
206   }
207 }