Debian Codegraph @

t/font.t

raw
1 BEGIN { $|=1; $^W=1; }
2 use strict;
3
4 BEGIN {
5 if (!eval q{
6 use Test::More;
7 1;
8 }) {
9 print "1..0 # skip: no Test::More module\n";
10 exit;
11 }
12 }
13
14 use Tk;
15 use Tk::Font;
16 use Tk::Config;
17 use Getopt::Long;
18 use Data::Dumper;
19
20 plan tests => 34;
21
22 my $v;
23 GetOptions("v" => \$v)
24 or die "usage: $0 [-v]";
25
26 my $Xft = $Tk::Config::xlib =~ /-lXft\b/;
27
28 my $mw = Tk::MainWindow->new;
29 $mw->geometry("+10+10");
30
31 ##
32 ## if there's only one (fixed) or no font family
33 ## then something is wrong. Probably the environment
34 ## and not perl/Tks fault.
35 ##
36 {
37 my @fam = ();
38 eval { @fam = $mw->fontFamilies; };
39 is($@, "", "fontFamilies");
40 cmp_ok(@fam,">=",1, "Num. of font families=".scalar @fam);
41 }
42 ##
43 ## Tk800.003 writes 'ont ...' in warning instead of 'font ...'
44 ## fontActual expects one argument
45 ## opps, looks like fault of ptksh
46 {
47 eval { $mw->fontActual; };
48 like($@, qr/^wrong # args: should be "font/,
49 "fontActual without font error");
50 }
51 ##
52 ## Stephen O. Lidie reported that Tk800.003
53 ## fontMeasure() and fontMeasure(fontname) gives
54 ## SEGV on linux and AIX.
55 ##
56 {
57 my $fontname = ($^O eq 'MSWin32') ? 'ansifixed': 'fixed';
58 eval { $mw->fontMeasure; };
59 isnt($@, "", "fontMeasure documented to require two args, not zero");
60 eval { $mw->fontMeasure($fontname); };
61 isnt($@, "", "fontMeasure documented to require two args, not one");
62 my $num = undef;
63 eval { $num = $mw->fontMeasure($fontname, 'Hi'); };
64 is($@, "", "fontMeasure works with fixed font and a string");
65 ok(defined $num);
66 cmp_ok($num, ">=", 2, "fontMeasure value is large enough");
67 my $l = $mw->Label(-font => $fontname);
68 my $name;
69 eval { $name = $l->cget('-font') };
70 is("$name", $fontname, "cget(-font) returns same fontname");
71 }
72
73 my @fam = $mw->fontFamilies;
74 if ($v)
75 {
76 diag "Font families:";
77 foreach my $fam (@fam)
78 {
79 diag "* $fam";
80 }
81 }
82
83 my $times_tests = 4;
84 SKIP:
85 {
86 skip("Times not available", $times_tests)
87 if !grep { /^times$/i } @fam;
88
89 $mw->optionAdd('*Listbox.font','Times -12 bold');
90 my $lb = $mw->Listbox()->pack;
91 $lb->insert(end => '0',"\xff","\x{20ac}","\x{0289}");
92 $lb->update;
93 my $lf = $lb->cget('-font');
94 diag "Font attributes: $$lf:" . join(',',$mw->fontActual($lf))
95 if $v;
96
97 my %fa = ($mw->fontActual($lf), $mw->fontMetrics($lf));
98
99 skip("Times requested, but got $fa{-family}", $times_tests)
100 if lc $fa{-family} !~ /^times$/i;
101
102 my %expect = (-family => 'Times',
103 -size => -12, -weight => 'bold',
104 -slant => 'roman');
105 foreach my $key (sort keys %expect)
106 {
107 my $val = $fa{$key};
108 like($val, qr{\Q$expect{$key}}i, "Value of $key from fontActual (Times font)");
109 }
110
111 my @subfonts = $mw->fontSubfonts($lf);
112 if ($v)
113 {
114 diag "Subfonts of $$lf:";
115 foreach my $sf (@subfonts)
116 {
117 diag '* ' . join(',',@$sf);
118 }
119 }
120 }
121
122 # This is really only needed for checking the -ascent and -descent values
123 # if Perl/Tk was built with XFT=1 and by requesting helvetica really
124 # the X11 helvetica was returned.
125 my $font_metric_tests = 10;
126 SKIP: {
127 skip("Test only work on X11", $font_metric_tests)
128 if $Tk::platform ne 'unix';
129
130 my $l = $mw->Label(-font => '-adobe-helvetica-bold-r-normal--*-180-*-*-*-*-*-*');
131 my $f = $l->cget(-font);
132 my @subfonts = $mw->font('subfonts', $f);
133
134 if ($Xft)
135 {
136 my $subfont_file = $subfonts[0]->[4];
137 skip("Unexpected subfont file $subfont_file", $font_metric_tests)
138 if $subfont_file !~ m{75dpi/helvB18(-ISO8859-1)?\.pcf(.gz)?$};
139 if ($v)
140 {
141 diag("Subfont file is $subfont_file");
142 }
143 }
144
145 my %fa = ($mw->fontActual($f), $mw->fontMetrics($f));
146
147 skip("Helvetica requested, but got $fa{-family}", $font_metric_tests)
148 if lc $fa{-family} ne 'helvetica';
149 skip("18 pixels requested, but got " . -$fa{-size}, $font_metric_tests)
150 if lc $fa{-size} != -18;
151
152 my $font_dump_shown = 0;
153 my %expected = (
154 "-weight" => "bold",
155 "-underline" => 0,
156 "-family" => "helvetica",
157 "-slant" => "roman",
158 "-size" => -18,
159 "-overstrike" => 0,
160 "-ascent" => 16,
161 "-descent" => 5,
162 "-linespace" => 21,
163 "-fixed" => 0,
164 );
165 while(my($key,$val) = each %expected)
166 {
167 if (!is(lc $fa{$key}, $val, "Expected $key value (Helvetica font)"))
168 {
169 diag(Dumper(\@subfonts)) if !$font_dump_shown;
170 $font_dump_shown++;
171 }
172 }
173 $l->destroy;
174 }
175
176 {
177 # This caused core dumps with Xft version of Perl/Tk
178 my $l = $mw->Label(-font => '-*-*-bold-r-*--12-*-*-*-*-*-*-*');
179 $l->destroy;
180 pass("Core dump check (especially for XFT)");
181 }
182
183 {
184 # This caused core dumps with Perl/Tk 804.027
185 eval { $mw->fontMeasure(undef, -ascent)};
186 like($@, qr{Cannot use undef as font object},
187 "Core dump check with undef font object");
188
189 eval { $mw->fontConfigure(undef) };
190 like($@, qr{Cannot use undef as font object});
191
192 eval { $mw->fontActual(undef) };
193 like($@, qr{Cannot use undef as font object});
194 }
195
196 {
197 # This caused core dumps with Perl/Tk 804.027
198 my $l = $mw->Label(-font => "helvetica 10");
199 my $f = $l->cget(-font);
200 eval { $l->configure(-font => undef) };
201 like($@, qr{\QCannot use undef value for object of type 'font'},
202 "Core dump check with undef font in configure");
203 # This used to be:
204 # is($l->cget(-font), $f, "Font stays unchanged");
205 # but there's a bug in some Test::More versions, so I do have to do it
206 # differently:
207 my $got = "@{[ $l->cget(-font) ]}";
208 my $expected = "$f";
209 is($got, $expected, "Font stays unchanged");
210 $l->destroy;
211
212 eval { $mw->Label(-font => undef) };
213 like($@, qr{\QCannot use undef value for object of type 'font'},
214 "Core dump check with undef font in initial setup (label)");
215
216 require Tk::TextList; # TextList seems to be special
217 eval { $mw->TextList(-font => undef) };
218 like($@, qr{\QCannot use undef value for object of type 'font'},
219 "Core dump check with undef font in initial setup (textlist)");
220 # FIXME XXX
221 # The above failure causes a font to be left in the cache. The message:
222 # Font -*-helvetica-medium-r-normal--14-*-*-*-*-iso8859-1 still in cache.
223 }
224
225 {
226 my $l = $mw->Label;
227 my $f = $l->cget(-font);
228 isa_ok($f, 'Tk::Font');
229 my $ascent = $f->measure(-ascent);
230 ok(defined $ascent, "Got ascent from measure");
231 eval { $f->actual(undef,undef) };
232 like($@, qr{\Qwrong # args: should be "font actual font},
233 "Does not get error about undef font object");
234 }
235
236 __END__