| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleFonts" -*- |
| 2 |
{ |
| 3 |
Copyright 2011-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
unit TestCastleFonts; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses CastleTester; |
| 22 |
|
| 23 |
type |
| 24 |
TTestCastleFonts = class(TCastleTestCase) |
| 25 |
strict private |
| 26 |
procedure FailIfFreeTypeMissing; |
| 27 |
published |
| 28 |
procedure TestMaxTextWidthHtml; |
| 29 |
procedure TestMaxTextWidthHtmlInWindow; |
| 30 |
procedure TestSizeFontFamily; |
| 31 |
procedure TestOverrideFont; |
| 32 |
procedure TestSizeChangeNotificationFontFamily; |
| 33 |
procedure TestSizeChangeNotificationCustomized; |
| 34 |
end; |
| 35 |
|
| 36 |
implementation |
| 37 |
|
| 38 |
uses SysUtils, Classes, |
| 39 |
CastleWindow, CastleFonts, CastleTextureFont_Default3d_Sans, CastleLog, |
| 40 |
Font_LatoRegular_300, CastleInternalFreeTypeH; |
| 41 |
|
| 42 |
procedure TTestCastleFonts.TestMaxTextWidthHtml; |
| 43 |
var |
| 44 |
F: TCastleFont; |
| 45 |
SList: TStringList; |
| 46 |
W1, W2: Single; |
| 47 |
begin |
| 48 |
F := TCastleFont.Create(nil); |
| 49 |
try |
| 50 |
F.Load(Font_Default3d_Sans); |
| 51 |
|
| 52 |
SList := TStringList.Create; |
| 53 |
try |
| 54 |
SList.Append('blah'); |
| 55 |
W1 := F.MaxTextWidth(SList); |
| 56 |
|
| 57 |
SList.Clear; |
| 58 |
SList.Append('<font color="#aabbcc">blah</font>'); |
| 59 |
W2 := F.MaxTextWidth(SList, true); |
| 60 |
|
| 61 |
AssertTrue(W1 > 0); |
| 62 |
AssertTrue(W1 = W2); |
| 63 |
finally FreeAndNil(SList) end; |
| 64 |
finally FreeAndNil(F) end; |
| 65 |
end; |
| 66 |
|
| 67 |
procedure TTestCastleFonts.TestMaxTextWidthHtmlInWindow; |
| 68 |
var |
| 69 |
Window: TCastleWindow; |
| 70 |
begin |
| 71 |
if not CanCreateWindowForTest then |
| 72 |
Exit; |
| 73 |
|
| 74 |
// should work with OpenGL context too, actually it doesn't matter now |
| 75 |
Window := CreateWindowForTest; |
| 76 |
try |
| 77 |
Window.Visible := false; |
| 78 |
Window.Open; |
| 79 |
TestMaxTextWidthHtml; |
| 80 |
Window.Close; |
| 81 |
finally DestroyWindowForTest(Window) end; |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TTestCastleFonts.TestSizeFontFamily; |
| 85 |
var |
| 86 |
Font: TCastleFont; |
| 87 |
Family: TCastleFontFamily; |
| 88 |
Customized: TCustomizedFont; |
| 89 |
begin |
| 90 |
Font := TCastleFont.Create(nil); |
| 91 |
try |
| 92 |
Font.Load(Font_Default3d_Sans); |
| 93 |
|
| 94 |
AssertEquals(25, Font.Size); |
| 95 |
AssertEquals(25, Font.EffectiveSize); |
| 96 |
AssertEquals(23, Font.Height); |
| 97 |
|
| 98 |
Family := TCastleFontFamily.Create(nil); |
| 99 |
try |
| 100 |
Family.Regular := Font; |
| 101 |
|
| 102 |
AssertEquals(0, Family.Size); |
| 103 |
AssertEquals(25, Family.EffectiveSize); |
| 104 |
AssertEquals(23, Family.Height); |
| 105 |
|
| 106 |
Family.Size := 30; |
| 107 |
AssertEquals(30, Family.Size); |
| 108 |
AssertEquals(30, Family.EffectiveSize); |
| 109 |
AssertEquals(27.6, Family.Height); |
| 110 |
finally FreeAndNil(Family) end; |
| 111 |
|
| 112 |
Customized := TCustomizedFont.Create(nil); |
| 113 |
try |
| 114 |
Customized.SourceFont := Font; |
| 115 |
|
| 116 |
AssertEquals(0, Customized.Size); |
| 117 |
AssertEquals(25, Customized.EffectiveSize); |
| 118 |
AssertEquals(23, Customized.Height); |
| 119 |
|
| 120 |
Customized.Size := 30; |
| 121 |
AssertEquals(30, Customized.Size); |
| 122 |
AssertEquals(30, Customized.EffectiveSize); |
| 123 |
AssertEquals(27.6, Customized.Height); |
| 124 |
finally FreeAndNil(Customized) end; |
| 125 |
|
| 126 |
Font.Size := 60; |
| 127 |
AssertEquals(60, Font.Size); |
| 128 |
AssertEquals(60, Font.EffectiveSize); |
| 129 |
AssertEquals(55.2, Font.Height); |
| 130 |
|
| 131 |
Family := TCastleFontFamily.Create(nil); |
| 132 |
try |
| 133 |
Family.Regular := Font; |
| 134 |
|
| 135 |
AssertEquals(0, Family.Size); |
| 136 |
AssertEquals(60, Family.EffectiveSize); |
| 137 |
AssertEquals(55.2, Family.Height); |
| 138 |
|
| 139 |
Family.Size := 30; |
| 140 |
AssertEquals(30, Family.Size); |
| 141 |
AssertEquals(30, Family.EffectiveSize); |
| 142 |
AssertEquals(27.6, Family.Height); |
| 143 |
finally FreeAndNil(Family) end; |
| 144 |
|
| 145 |
Customized := TCustomizedFont.Create(nil); |
| 146 |
try |
| 147 |
Customized.SourceFont := Font; |
| 148 |
|
| 149 |
AssertEquals(0, Customized.Size); |
| 150 |
AssertEquals(60, Customized.EffectiveSize); |
| 151 |
AssertEquals(55.2, Customized.Height); |
| 152 |
|
| 153 |
Customized.Size := 30; |
| 154 |
AssertEquals(30, Customized.Size); |
| 155 |
AssertEquals(30, Customized.EffectiveSize); |
| 156 |
AssertEquals(27.6, Customized.Height); |
| 157 |
finally FreeAndNil(Customized) end; |
| 158 |
finally FreeAndNil(Font) end; |
| 159 |
end; |
| 160 |
|
| 161 |
procedure TTestCastleFonts.TestOverrideFont; |
| 162 |
var |
| 163 |
LargeDigitsFont: TCastleFont; |
| 164 |
CustomizedFont: TCustomizedFont; |
| 165 |
FontFamily: TCastleFontFamily; |
| 166 |
begin |
| 167 |
LargeDigitsFont := TCastleFont.Create(nil); |
| 168 |
try |
| 169 |
LargeDigitsFont.Load(TextureFont_LatoRegular_300); |
| 170 |
LargeDigitsFont.MeasureHeight := '123'; |
| 171 |
LargeDigitsFont.MeasureCapHeight := '123'; |
| 172 |
LargeDigitsFont.MeasureDescenderHeight := ''; |
| 173 |
LargeDigitsFont.FontData.UseFallbackGlyph := false; |
| 174 |
AssertEquals(300, TextureFont_LatoRegular_300.Size); |
| 175 |
|
| 176 |
AssertSameValue(300, LargeDigitsFont.Size); |
| 177 |
AssertSameValue(522, LargeDigitsFont.TextWidth('123'), 1); |
| 178 |
AssertSameValue(221, LargeDigitsFont.TextHeight('123'), 1); |
| 179 |
AssertSameValue(0, LargeDigitsFont.TextWidth('abc')); |
| 180 |
AssertSameValue(0, LargeDigitsFont.TextHeight('abc')); |
| 181 |
AssertSameValue(221, LargeDigitsFont.Height); |
| 182 |
AssertSameValue(219, LargeDigitsFont.CapHeight); |
| 183 |
AssertSameValue(0, LargeDigitsFont.DescenderHeight); |
| 184 |
|
| 185 |
CustomizedFont := TCustomizedFont.Create(nil); |
| 186 |
try |
| 187 |
CustomizedFont.SourceFont := LargeDigitsFont; |
| 188 |
AssertSameValue(0, CustomizedFont.Size); // not customized yet |
| 189 |
AssertSameValue(522, CustomizedFont.TextWidth('123'), 1); |
| 190 |
AssertSameValue(221, CustomizedFont.TextHeight('123'), 1); |
| 191 |
AssertSameValue(0, CustomizedFont.TextWidth('abc')); |
| 192 |
AssertSameValue(0, CustomizedFont.TextHeight('abc')); |
| 193 |
AssertSameValue(221, CustomizedFont.Height); |
| 194 |
AssertSameValue(219, CustomizedFont.CapHeight); |
| 195 |
AssertSameValue(0, CustomizedFont.DescenderHeight); |
| 196 |
finally FreeAndNil(CustomizedFont) end; |
| 197 |
|
| 198 |
FontFamily := TCastleFontFamily.Create(nil); |
| 199 |
try |
| 200 |
FontFamily.Regular := LargeDigitsFont; |
| 201 |
AssertSameValue(0, FontFamily.Size); // not customized yet |
| 202 |
AssertSameValue(522, FontFamily.TextWidth('123'), 1); |
| 203 |
AssertSameValue(221, FontFamily.TextHeight('123'), 1); |
| 204 |
AssertSameValue(0, FontFamily.TextWidth('abc')); |
| 205 |
AssertSameValue(0, FontFamily.TextHeight('abc')); |
| 206 |
AssertSameValue(221, FontFamily.Height); |
| 207 |
AssertSameValue(219, FontFamily.CapHeight); |
| 208 |
AssertSameValue(0, FontFamily.DescenderHeight); |
| 209 |
finally FreeAndNil(FontFamily) end; |
| 210 |
|
| 211 |
LargeDigitsFont.Size := 1000; |
| 212 |
AssertSameValue(1000, LargeDigitsFont.Size); |
| 213 |
AssertSameValue(522 * 10/3, LargeDigitsFont.TextWidth('123'), 10/3); |
| 214 |
AssertSameValue(221 * 10/3, LargeDigitsFont.TextHeight('123'), 10/3); |
| 215 |
AssertSameValue(0, LargeDigitsFont.TextWidth('abc')); |
| 216 |
AssertSameValue(0, LargeDigitsFont.TextHeight('abc')); |
| 217 |
AssertSameValue(221 * 10/3, LargeDigitsFont.Height, 10/3); |
| 218 |
AssertSameValue(219 * 10/3, LargeDigitsFont.CapHeight, 10/3); |
| 219 |
AssertSameValue(0, LargeDigitsFont.DescenderHeight); |
| 220 |
|
| 221 |
CustomizedFont := TCustomizedFont.Create(nil); |
| 222 |
try |
| 223 |
CustomizedFont.SourceFont := LargeDigitsFont; |
| 224 |
CustomizedFont.Size := 2000; |
| 225 |
AssertSameValue(2000, CustomizedFont.Size); // not customized yet |
| 226 |
AssertSameValue(522 * 20/3, CustomizedFont.TextWidth('123'), 20/3); |
| 227 |
AssertSameValue(221 * 20/3, CustomizedFont.TextHeight('123'), 20/3); |
| 228 |
AssertSameValue(0, CustomizedFont.TextWidth('abc')); |
| 229 |
AssertSameValue(0, CustomizedFont.TextHeight('abc')); |
| 230 |
AssertSameValue(221 * 20/3, CustomizedFont.Height, 20/3); |
| 231 |
AssertSameValue(219 * 20/3, CustomizedFont.CapHeight, 20/3); |
| 232 |
AssertSameValue(0, CustomizedFont.DescenderHeight); |
| 233 |
finally FreeAndNil(CustomizedFont) end; |
| 234 |
|
| 235 |
FontFamily := TCastleFontFamily.Create(nil); |
| 236 |
try |
| 237 |
FontFamily.Regular := LargeDigitsFont; |
| 238 |
FontFamily.Size := 2000; |
| 239 |
AssertSameValue(2000, FontFamily.Size); // not customized yet |
| 240 |
AssertSameValue(522 * 20/3, FontFamily.TextWidth('123'), 20/3); |
| 241 |
AssertSameValue(221 * 20/3, FontFamily.TextHeight('123'), 20/3); |
| 242 |
AssertSameValue(0, FontFamily.TextWidth('abc')); |
| 243 |
AssertSameValue(0, FontFamily.TextHeight('abc')); |
| 244 |
AssertSameValue(221 * 20/3, FontFamily.Height, 20/3); |
| 245 |
AssertSameValue(219 * 20/3, FontFamily.CapHeight, 20/3); |
| 246 |
AssertSameValue(0, FontFamily.DescenderHeight); |
| 247 |
finally FreeAndNil(FontFamily) end; |
| 248 |
finally FreeAndNil(LargeDigitsFont) end; |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TTestCastleFonts.FailIfFreeTypeMissing; |
| 252 |
begin |
| 253 |
{ Without FreeType, we cannot load .ttf so some tests have to fail. |
| 254 |
We don't want to ignore this problem and turn it into hard-to-notice |
| 255 |
warning (it's better to force you to setup proper DLLs to have clean tests output), |
| 256 |
but we do this check to generate exception with more obvious error message. |
| 257 |
This way users of this test on Windows (where FreeType most often may be missing) |
| 258 |
will know what's going on. } |
| 259 |
if not FreeTypeLibraryInitialized then |
| 260 |
begin |
| 261 |
Fail('FreeType library not available, so TTestCastleFonts.TestSizeChangeNotificationFontFamily has to fail.' |
| 262 |
{$ifdef MSWINDOWS} |
| 263 |
+ ' On Windows, be sure to place proper DLL files alongside EXE. It is easiest to build using CGE editor that will place proper DLLs automatically.' |
| 264 |
{$endif} |
| 265 |
); |
| 266 |
end; |
| 267 |
end; |
| 268 |
|
| 269 |
procedure TTestCastleFonts.TestSizeChangeNotificationFontFamily; |
| 270 |
var |
| 271 |
F: TCastleFont; |
| 272 |
FF: TCastleFontFamily; |
| 273 |
begin |
| 274 |
FailIfFreeTypeMissing; |
| 275 |
|
| 276 |
F := TCastleFont.Create(nil); |
| 277 |
AssertEquals(0, F.Height); |
| 278 |
|
| 279 |
FF := TCastleFontFamily.Create(nil); |
| 280 |
AssertEquals(0, FF.Height); |
| 281 |
FF.Regular := F; |
| 282 |
AssertEquals(0, FF.Height); |
| 283 |
|
| 284 |
F.Url := 'castle-data:/fonts/PARPG.ttf'; |
| 285 |
AssertSameValue(22, F.Height); |
| 286 |
AssertSameValue(22, FF.Height); |
| 287 |
// writeln(F.Height:1:2); |
| 288 |
// writeln(FF.Height:1:2); |
| 289 |
|
| 290 |
FreeAndNil(F); |
| 291 |
FreeAndNil(FF); |
| 292 |
end; |
| 293 |
|
| 294 |
procedure TTestCastleFonts.TestSizeChangeNotificationCustomized; |
| 295 |
var |
| 296 |
F: TCastleFont; |
| 297 |
CF: TCastleFontFamily; |
| 298 |
begin |
| 299 |
FailIfFreeTypeMissing; |
| 300 |
|
| 301 |
F := TCastleFont.Create(nil); |
| 302 |
AssertEquals(0, F.Height); |
| 303 |
|
| 304 |
CF := TCastleFontFamily.Create(nil); |
| 305 |
AssertEquals(0, CF.Height); |
| 306 |
CF.Regular := F; |
| 307 |
AssertEquals(0, CF.Height); |
| 308 |
|
| 309 |
F.Url := 'castle-data:/fonts/PARPG.ttf'; |
| 310 |
AssertSameValue(22, F.Height); |
| 311 |
AssertSameValue(22, CF.Height); |
| 312 |
// writeln(F.Height:1:2); |
| 313 |
// writeln(CF.Height:1:2); |
| 314 |
|
| 315 |
FreeAndNil(F); |
| 316 |
FreeAndNil(CF); |
| 317 |
end; |
| 318 |
|
| 319 |
initialization |
| 320 |
RegisterTest(TTestCastleFonts); |
| 321 |
end. |