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