| 1 |
{%MainUnit castlefonts.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2024 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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Font loaded from a font file, like ttf or otf. |
| 21 |
This class is typically used for outline (scalable, vector) fonts in ttf or otf formats. |
| 22 |
But it can really deal with any font supported by |
| 23 |
the @url(https://www.freetype.org/ FreeType library), even bitmap fonts, |
| 24 |
see @url(https://www.freetype.org/freetype2/docs/ft2faq.html#general-what the summary of font formats supported by FreeType). |
| 25 |
|
| 26 |
This can load a font file, or it can use ready data in TTextureFontData. |
| 27 |
The latter allows to use this for fonts embedded in a Pascal source code, |
| 28 |
since our texture-font-to-pascal can convert a font file to a unit that defines |
| 29 |
ready TTextureFontData instance. } |
| 30 |
TCastleFont = class(TCastleAbstractFont) |
| 31 |
strict private |
| 32 |
FFont: TTextureFontData; //< nil when no font loaded successfully |
| 33 |
FOwnsFont: boolean; |
| 34 |
DrawableImage: TDrawableImage; |
| 35 |
GlyphsScreenRects, GlyphsImageRects: TFloatRectangleList; |
| 36 |
FUrl: String; |
| 37 |
FOptimalSize: Cardinal; |
| 38 |
FAntiAliased: Boolean; |
| 39 |
FLoadBasicCharacters: Boolean; |
| 40 |
FLoadCharacters: String; |
| 41 |
function GetScale: Single; |
| 42 |
procedure SetScale(const AValue: Single); |
| 43 |
procedure SetUrl(const Value: String); |
| 44 |
procedure SetOptimalSize(const Value: Cardinal); |
| 45 |
procedure SetAntiAliased(const Value: Boolean); |
| 46 |
procedure SetLoadBasicCharacters(const Value: Boolean); |
| 47 |
procedure SetLoadCharacters(const Value: String); |
| 48 |
{ Free stuff loaded by @link(Load). } |
| 49 |
procedure Unload; |
| 50 |
procedure Reload; |
| 51 |
strict protected |
| 52 |
procedure SetSize(const Value: Single); override; |
| 53 |
procedure GLContextClose; override; |
| 54 |
public |
| 55 |
const |
| 56 |
DefaultOptimalSize = 20; |
| 57 |
|
| 58 |
constructor Create(AOwner: TComponent); overload; override; |
| 59 |
destructor Destroy; override; |
| 60 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 61 |
function FontLoaded: Boolean; overload; override; |
| 62 |
|
| 63 |
constructor Create(const Url: String; |
| 64 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 65 |
const ACharacters: TUnicodeCharList = nil); reintroduce; overload; deprecated 'use Create(Owner: TComponent), then assign properties to load font: OptimalSize, LoadCharacters, AntiAliased, Url'; |
| 66 |
constructor Create(const Url: String; |
| 67 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 68 |
const ACharacters: TSetOfChars); reintroduce; overload; deprecated 'use Create(Owner: TComponent), then assign properties to load font: OptimalSize, LoadCharacters, AntiAliased, Url'; |
| 69 |
|
| 70 |
{ Load by reading a FreeType font file, like ttf. |
| 71 |
|
| 72 |
Providing charaters list as @nil means that we only create glyphs |
| 73 |
for SimpleAsciiCharacters, which includes only the basic ASCII characters. |
| 74 |
The ACharacters instance @italic(does not) become owned by this object, |
| 75 |
so remember to free it after calling this constructor. |
| 76 |
|
| 77 |
Loading a font data also changes @link(Size) to the underlying |
| 78 |
(optimal to render) font data size. } |
| 79 |
procedure Load(const Url: String; |
| 80 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 81 |
const ACharacters: TUnicodeCharList = nil; |
| 82 |
const AdjustProperties: Boolean = true); overload; deprecated 'assign properties to load font: OptimalSize, LoadCharacters, AntiAliased, Url'; |
| 83 |
|
| 84 |
{ Load from a ready TTextureFontData instance. |
| 85 |
@param(Data TTextureFontData instance containing loaded image |
| 86 |
and glyphs parameters.) |
| 87 |
@param(OwnsData If @true, the Data instance becomes owned |
| 88 |
by this class (will be freed in our constructor). |
| 89 |
Usually you @italic(do not) want this, since usually you pass Data |
| 90 |
from a unit generated by texture-font-to-pascal. In this case, |
| 91 |
the finalization of CastleTextureFont_Xxx unit will already free |
| 92 |
the TTextureFontData instance.) |
| 93 |
@param(AdjustProperties Adjust Url, OptimalSize, Size, AntiAliased |
| 94 |
to reflect loaded Data. Or reset them to reflect empty Url |
| 95 |
(and the rest at defaults) when Data = nil.) } |
| 96 |
procedure Load(const Data: TTextureFontData; |
| 97 |
const OwnsData: Boolean = false; |
| 98 |
const AdjustProperties: Boolean = true); overload; |
| 99 |
|
| 100 |
procedure PrepareResources; override; |
| 101 |
procedure Print(const X, Y: Single; const Color: TCastleColor; |
| 102 |
const S: string); override; |
| 103 |
function TextWidth(const S: string): Single; override; |
| 104 |
function TextHeight(const S: string): Single; override; |
| 105 |
function TextHeightBase(const S: string): Single; override; |
| 106 |
function TextMove(const S: string): TVector2; override; |
| 107 |
|
| 108 |
{ Underlying font data. } |
| 109 |
property FontData: TTextureFontData read FFont; {$ifdef FPC}deprecated 'you should not need to use this directly';{$endif} // can't be removed from delphi because x3dnodes_standard_text.inc uses that |
| 110 |
|
| 111 |
{ Scale applied to the rendered font, compared to @link(FontData).Size. |
| 112 |
Changing this is equivalent to changing the Size property. } |
| 113 |
property Scale: Single read GetScale write SetScale; |
| 114 |
published |
| 115 |
{ Loaded font file. |
| 116 |
Typically this is used for outline (scalable, vector) fonts in ttf or otf formats. |
| 117 |
But it can really deal with any font format supported by |
| 118 |
the @url(https://www.freetype.org/ FreeType library), even bitmap fonts, |
| 119 |
see @url(https://www.freetype.org/freetype2/docs/ft2faq.html#general-what the summary of font formats supported by FreeType). } |
| 120 |
property Url: String read FUrl write SetUrl; |
| 121 |
|
| 122 |
{ Optimal font size (in real device pixels), |
| 123 |
the font will be scaled when other size is actually needed. |
| 124 |
This also sets default @link(Size) used for rendering this font. } |
| 125 |
property OptimalSize: Cardinal read FOptimalSize write SetOptimalSize default DefaultOptimalSize; |
| 126 |
|
| 127 |
{ Anti-aliased font has smooth edges and is rendered using blending. |
| 128 |
Usually this is much better for quality. |
| 129 |
Non-anti-aliased means that font uses simple yes/no transparency |
| 130 |
and is rendered using alpha testing. } |
| 131 |
property AntiAliased: Boolean read FAntiAliased write SetAntiAliased default true; |
| 132 |
|
| 133 |
{ Load from font all "basic" characters, which include digits, |
| 134 |
English letters and standard ASCII symbols. |
| 135 |
See @link(SimpleAsciiCharacters) for exact definition. |
| 136 |
These are loaded in addition to characters listed on LoadCharacters. } |
| 137 |
property LoadBasicCharacters: Boolean read FLoadBasicCharacters write SetLoadBasicCharacters default true; |
| 138 |
|
| 139 |
{ Load from font all characters listed here. As everywhere in CGE |
| 140 |
(same as in Lazarus LCL), this is an string composed from UTF-8 characters. } |
| 141 |
property LoadCharacters: String read FLoadCharacters write SetLoadCharacters; |
| 142 |
end; |
| 143 |
|
| 144 |
TGLBitmapFont = TCastleFont deprecated 'use TCastleFont'; |
| 145 |
TTextureFont = TCastleFont deprecated 'use TCastleFont'; |
| 146 |
|
| 147 |
{$endif read_interface} |
| 148 |
|
| 149 |
{$ifdef read_implementation} |
| 150 |
|
| 151 |
{ TCastleFont --------------------------------------------------------------- } |
| 152 |
|
| 153 |
constructor TCastleFont.Create(AOwner: TComponent); |
| 154 |
begin |
| 155 |
inherited; |
| 156 |
GlyphsScreenRects := TFloatRectangleList.Create; |
| 157 |
GlyphsImageRects := TFloatRectangleList.Create; |
| 158 |
FLoadBasicCharacters := true; |
| 159 |
FOptimalSize := DefaultOptimalSize; |
| 160 |
FAntiAliased := true; |
| 161 |
end; |
| 162 |
|
| 163 |
destructor TCastleFont.Destroy; |
| 164 |
begin |
| 165 |
Unload; |
| 166 |
FreeAndNil(GlyphsScreenRects); |
| 167 |
FreeAndNil(GlyphsImageRects); |
| 168 |
inherited; |
| 169 |
end; |
| 170 |
|
| 171 |
procedure TCastleFont.Unload; |
| 172 |
begin |
| 173 |
// free previous FFont data |
| 174 |
Load(nil, { OwnsData doesn't matter } false, { AdjustParameters } false); |
| 175 |
end; |
| 176 |
|
| 177 |
constructor TCastleFont.Create(const Url: String; |
| 178 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 179 |
const ACharacters: TUnicodeCharList); |
| 180 |
begin |
| 181 |
Create(nil); |
| 182 |
{$warnings off} // calling deprecated from deprecated |
| 183 |
Load(Url, ASize, AnAntiAliased, ACharacters, true); |
| 184 |
{$warnings on} |
| 185 |
end; |
| 186 |
|
| 187 |
procedure TCastleFont.Load(const Url: String; |
| 188 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 189 |
const ACharacters: TUnicodeCharList = nil; |
| 190 |
const AdjustProperties: Boolean = true); |
| 191 |
var |
| 192 |
NewFontData: TTextureFontData; |
| 193 |
begin |
| 194 |
{ Although FontSizesChanged is also called by Load, |
| 195 |
make sure it gets called even when TTextureFontData.Create fails |
| 196 |
(e.g. loading file fails). } |
| 197 |
FontSizesChanged; |
| 198 |
if Url = '' then |
| 199 |
Unload |
| 200 |
else |
| 201 |
begin |
| 202 |
NewFontData := TTextureFontData.Create(Url, ASize, AnAntiAliased, ACharacters); |
| 203 |
Load(NewFontData, true, AdjustProperties); |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
constructor TCastleFont.Create(const Url: String; |
| 208 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 209 |
const ACharacters: TSetOfChars); |
| 210 |
var |
| 211 |
Chars: TUnicodeCharList; |
| 212 |
C: char; |
| 213 |
begin |
| 214 |
Chars := TUnicodeCharList.Create; |
| 215 |
try |
| 216 |
for C in ACharacters do |
| 217 |
Chars.Add(Ord(C)); |
| 218 |
{$warnings off} // calling deprecated from deprecated |
| 219 |
Create(Url, ASize, AnAntiAliased, Chars); |
| 220 |
{$warnings on} |
| 221 |
finally FreeAndNil(Chars) end; |
| 222 |
end; |
| 223 |
|
| 224 |
procedure TCastleFont.Load(const Data: TTextureFontData; const OwnsData: Boolean = false; |
| 225 |
const AdjustProperties: Boolean = true); |
| 226 |
begin |
| 227 |
GLContextClose; |
| 228 |
|
| 229 |
if FOwnsFont then |
| 230 |
FreeAndNil(FFont) |
| 231 |
else |
| 232 |
FFont := nil; |
| 233 |
|
| 234 |
FOwnsFont := OwnsData; |
| 235 |
FFont := Data; |
| 236 |
|
| 237 |
if AdjustProperties then |
| 238 |
begin |
| 239 |
if FFont <> nil then |
| 240 |
begin |
| 241 |
FUrl := FFont.Url; |
| 242 |
FOptimalSize := FFont.Size; |
| 243 |
FAntiAliased := FFont.AntiAliased; |
| 244 |
end else |
| 245 |
begin |
| 246 |
FUrl := ''; |
| 247 |
{ for consistency, adjust all properties always, even when FFont = nil } |
| 248 |
FOptimalSize := DefaultOptimalSize; |
| 249 |
FAntiAliased := true; |
| 250 |
end; |
| 251 |
// synchronize Size with OptimalSize |
| 252 |
Size := OptimalSize; |
| 253 |
end; |
| 254 |
|
| 255 |
// call this at the end, when our state is consistent |
| 256 |
FontSizesChanged; |
| 257 |
end; |
| 258 |
|
| 259 |
function TCastleFont.PropertySections(const PropertyName: String): TPropertySections; |
| 260 |
begin |
| 261 |
if (PropertyName = 'Url') or |
| 262 |
(PropertyName = 'OptimalSize') or |
| 263 |
(PropertyName = 'AntiAliased') or |
| 264 |
(PropertyName = 'LoadCharacters') or |
| 265 |
(PropertyName = 'LoadBasicCharacters') then |
| 266 |
Result := [psBasic] |
| 267 |
else |
| 268 |
Result := inherited PropertySections(PropertyName); |
| 269 |
end; |
| 270 |
|
| 271 |
function TCastleFont.GetScale: Single; |
| 272 |
begin |
| 273 |
if FFont <> nil then |
| 274 |
Result := Size / FFont.Size |
| 275 |
else |
| 276 |
Result := Size; |
| 277 |
end; |
| 278 |
|
| 279 |
procedure TCastleFont.SetScale(const AValue: Single); |
| 280 |
begin |
| 281 |
if FFont <> nil then |
| 282 |
Size := FFont.Size * AValue |
| 283 |
else |
| 284 |
Size := AValue; |
| 285 |
end; |
| 286 |
|
| 287 |
procedure TCastleFont.SetSize(const Value: Single); |
| 288 |
begin |
| 289 |
inherited SetSize(Value); |
| 290 |
|
| 291 |
Assert((FFont = nil) or (FFont.Size <> 0)); |
| 292 |
Assert(not IsInfinite(Value)); |
| 293 |
|
| 294 |
{ In the past, we had "smart" code here adjusting |
| 295 |
DrawableImage.SmoothScaling := Size <> FFont.Size |
| 296 |
in each SetSize (if DrawableImage <> nil), |
| 297 |
based on the idea that when desired size equals optimal |
| 298 |
size then there's no need for smooth scaling. |
| 299 |
|
| 300 |
But this causes often toggling of DrawableImage.SmoothScaling. |
| 301 |
It is particularly visible when using multiple TCastleLabel, |
| 302 |
and/or with multiple lines. In this case we switch between original/customized |
| 303 |
font size quickly, since TCastleUserInterfaceFont.Font |
| 304 |
adjusts Size, TCastleFontFamily.SubFontCustomizeBegin does SF.PushProperties. |
| 305 |
But actually all rendering is done with equal Size... |
| 306 |
|
| 307 |
So constant switching between smooth scaling and non-smooth scaling |
| 308 |
does not serve anything, and it: |
| 309 |
|
| 310 |
- makes unnnecessary OpenGL calls to toggle texture filtering back and forth, |
| 311 |
slowing down rendering |
| 312 |
- prevents labels batching when using TDrawableImage.BatchingBegin/End, |
| 313 |
Container.UserInterfaceBatching |
| 314 |
} |
| 315 |
end; |
| 316 |
|
| 317 |
procedure TCastleFont.PrepareResources; |
| 318 |
begin |
| 319 |
inherited; |
| 320 |
if (FFont <> nil) and (DrawableImage = nil) then |
| 321 |
DrawableImage := TDrawableImage.Create(FFont.Image, |
| 322 |
{ Smooth scaling always true, ready for any Size, |
| 323 |
see TCastleFont.SetSize comments. } |
| 324 |
true, false); |
| 325 |
end; |
| 326 |
|
| 327 |
procedure TCastleFont.GLContextClose; |
| 328 |
begin |
| 329 |
FreeAndNil(DrawableImage); |
| 330 |
inherited; |
| 331 |
end; |
| 332 |
|
| 333 |
procedure TCastleFont.Print(const X, Y: Single; const Color: TCastleColor; |
| 334 |
const S: string); |
| 335 |
var |
| 336 |
ScreenX, ScreenY: Single; |
| 337 |
G: TTextureFontData.TGlyph; |
| 338 |
GlyphsToRender: Integer; |
| 339 |
|
| 340 |
procedure GlyphDraw(const OutlineMoveX, OutlineMoveY: Integer); |
| 341 |
var |
| 342 |
ScreenRect, ImageRect: PFloatRectangle; |
| 343 |
begin |
| 344 |
if TargetImage <> nil then |
| 345 |
begin |
| 346 |
TargetImage.DrawFrom(FFont.Image, |
| 347 |
Round(ScreenX - G.X * Scale + OutlineMoveX * Outline), |
| 348 |
Round(ScreenY - G.Y * Scale + OutlineMoveY * Outline), |
| 349 |
G.ImageX, |
| 350 |
G.ImageY, |
| 351 |
G.Width, |
| 352 |
G.Height); |
| 353 |
end else |
| 354 |
begin |
| 355 |
Assert(GlyphsToRender < GlyphsScreenRects.Count); |
| 356 |
|
| 357 |
{ Use a small margin around every glyph to allow bilinear |
| 358 |
filtering to smoothly go from opaque to fully transparent |
| 359 |
at glyph border. This prevents glyph border from ending suddenly, |
| 360 |
it looks much better in the case of blending. |
| 361 |
|
| 362 |
This cooperates with TTextureFontData.Create (used by |
| 363 |
texture-font-to-pascal) that makes sure that each letter is |
| 364 |
surrounded with a padding that allows such border, |
| 365 |
see GlyphPadding in castletexturefontdata.pas. } |
| 366 |
{ TODO; Not used now. While there's a visible improvement in some cases |
| 367 |
(when optimal font size mismatches final font size), |
| 368 |
there's also regression in castle-model-viewer |
| 369 |
(when optimal font size matches final font size closely): |
| 370 |
the "A" in "Animations" button in castle-model-viewer gets weird artifact. } |
| 371 |
{.$define EXTRA_GLYPH_SPACE} |
| 372 |
|
| 373 |
ScreenRect := PFloatRectangle(GlyphsScreenRects.Ptr(GlyphsToRender)); |
| 374 |
ScreenRect^.Left := ScreenX - (G.X {$ifdef EXTRA_GLYPH_SPACE} + 0.5 {$endif}) * Scale + OutlineMoveX * Outline; |
| 375 |
ScreenRect^.Bottom := ScreenY - (G.Y {$ifdef EXTRA_GLYPH_SPACE} + 0.5 {$endif}) * Scale + OutlineMoveY * Outline; |
| 376 |
ScreenRect^.Width := (G.Width {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}) * Scale; |
| 377 |
ScreenRect^.Height := (G.Height {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}) * Scale; |
| 378 |
|
| 379 |
ImageRect := PFloatRectangle(GlyphsImageRects.Ptr(GlyphsToRender)); |
| 380 |
ImageRect^.Left := G.ImageX {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}; |
| 381 |
ImageRect^.Bottom := G.ImageY {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}; |
| 382 |
ImageRect^.Width := G.Width {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}; |
| 383 |
ImageRect^.Height := G.Height {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}; |
| 384 |
|
| 385 |
Inc(GlyphsToRender); |
| 386 |
end; |
| 387 |
end; |
| 388 |
|
| 389 |
var |
| 390 |
Iter: TCastleStringIterator; |
| 391 |
GlyphsPerChar: Integer; |
| 392 |
begin |
| 393 |
if FFont = nil then |
| 394 |
Exit; // font not loaded |
| 395 |
|
| 396 |
if TargetImage = nil then |
| 397 |
begin |
| 398 |
PrepareResources; |
| 399 |
|
| 400 |
{ allocate the necessary glyphs at start. |
| 401 |
This allows to quickly fill them later. |
| 402 |
Note that we possibly allocate too much, because Length(S) may be > StringLength(S) |
| 403 |
(because of multi-byte characters), and also because some characters do not have glyphs. |
| 404 |
That's OK, we'll calculate real GlyphsToRender when iterating. } |
| 405 |
if Outline = 0 then |
| 406 |
GlyphsPerChar := 1 else |
| 407 |
if OutlineHighQuality then |
| 408 |
GlyphsPerChar := 8 else |
| 409 |
GlyphsPerChar := 4; |
| 410 |
GlyphsScreenRects.Count := Max(MinimumGlyphsAllocated, GlyphsPerChar * Length(S)); |
| 411 |
GlyphsImageRects .Count := Max(MinimumGlyphsAllocated, GlyphsPerChar * Length(S)); |
| 412 |
end; |
| 413 |
|
| 414 |
{ first pass, to render Outline. |
| 415 |
|
| 416 |
This could be done better by being done together with non-outline pass, |
| 417 |
by filling the alternative place in Glyph arrays, such that outline and non-outline data |
| 418 |
don't collide. |
| 419 |
It would be 1. faster (don't iterate over S two times), 2. less code duplication. } |
| 420 |
if Outline <> 0 then |
| 421 |
begin |
| 422 |
GlyphsToRender := 0; |
| 423 |
{ While Round() below is not needed, it improves the quality of rendered |
| 424 |
text. Compare e.g. castle-model-viewer button captions. } |
| 425 |
ScreenX := Round(X); |
| 426 |
ScreenY := Round(Y); |
| 427 |
if TargetImage <> nil then |
| 428 |
FFont.Image.ColorWhenTreatedAsAlpha := Vector3Byte(OutlineColor.XYZ); // ignore OutlineColor[3] for now |
| 429 |
|
| 430 |
Iter.Start(S); |
| 431 |
while Iter.GetNext do |
| 432 |
begin |
| 433 |
G := FFont.Glyph(Iter.Current); |
| 434 |
if G <> nil then |
| 435 |
begin |
| 436 |
if (G.Width <> 0) and (G.Height <> 0) then |
| 437 |
begin |
| 438 |
GlyphDraw(0, 0); |
| 439 |
GlyphDraw(0, 2); |
| 440 |
GlyphDraw(2, 2); |
| 441 |
GlyphDraw(2, 0); |
| 442 |
|
| 443 |
if OutlineHighQuality then |
| 444 |
begin |
| 445 |
GlyphDraw(1, 0); |
| 446 |
GlyphDraw(1, 2); |
| 447 |
GlyphDraw(0, 1); |
| 448 |
GlyphDraw(2, 1); |
| 449 |
end; |
| 450 |
end; |
| 451 |
ScreenX := ScreenX + G.AdvanceX * Scale + Outline * 2; |
| 452 |
ScreenY := ScreenY + G.AdvanceY * Scale; |
| 453 |
end; |
| 454 |
end; |
| 455 |
|
| 456 |
if TargetImage = nil then |
| 457 |
begin |
| 458 |
DrawableImage.Color := OutlineColor; |
| 459 |
DrawableImage.Draw( |
| 460 |
PFloatRectangleArray(GlyphsScreenRects.L), |
| 461 |
PFloatRectangleArray(GlyphsImageRects.L), GlyphsToRender); |
| 462 |
end; |
| 463 |
end; |
| 464 |
|
| 465 |
GlyphsToRender := 0; |
| 466 |
{ While Round() below is not needed, it improves the quality of rendered |
| 467 |
text. } |
| 468 |
ScreenX := Round(X); |
| 469 |
ScreenY := Round(Y); |
| 470 |
if TargetImage <> nil then |
| 471 |
FFont.Image.ColorWhenTreatedAsAlpha := Vector3Byte(Color.XYZ); // ignore Color[3] for now |
| 472 |
|
| 473 |
Iter.Start(S); |
| 474 |
while Iter.GetNext do |
| 475 |
begin |
| 476 |
G := FFont.Glyph(Iter.Current); |
| 477 |
if G <> nil then |
| 478 |
begin |
| 479 |
if (G.Width <> 0) and (G.Height <> 0) then |
| 480 |
if Outline <> 0 then |
| 481 |
GlyphDraw(1, 1) |
| 482 |
else |
| 483 |
GlyphDraw(0, 0); |
| 484 |
ScreenX := ScreenX + G.AdvanceX * Scale + Outline * 2; |
| 485 |
ScreenY := ScreenY + G.AdvanceY * Scale; |
| 486 |
end; |
| 487 |
end; |
| 488 |
|
| 489 |
if TargetImage = nil then |
| 490 |
begin |
| 491 |
DrawableImage.Color := Color; |
| 492 |
DrawableImage.Draw( |
| 493 |
PFloatRectangleArray(GlyphsScreenRects.L), |
| 494 |
PFloatRectangleArray(GlyphsImageRects.L), GlyphsToRender); |
| 495 |
end; |
| 496 |
end; |
| 497 |
|
| 498 |
function TCastleFont.TextWidth(const S: string): Single; |
| 499 |
begin |
| 500 |
if FFont = nil then |
| 501 |
Exit(0); // font not loaded |
| 502 |
Result := FFont.TextWidth(S) * Scale; |
| 503 |
if Outline <> 0 then |
| 504 |
Result := Result + Outline * 2 * StringLength(S); |
| 505 |
end; |
| 506 |
|
| 507 |
function TCastleFont.TextHeight(const S: string): Single; |
| 508 |
begin |
| 509 |
if FFont = nil then |
| 510 |
Exit(0); // font not loaded |
| 511 |
Result := FFont.TextHeight(S) * Scale + Outline * 2; |
| 512 |
end; |
| 513 |
|
| 514 |
function TCastleFont.TextHeightBase(const S: string): Single; |
| 515 |
begin |
| 516 |
if FFont = nil then |
| 517 |
Exit(0); // font not loaded |
| 518 |
Result := FFont.TextHeightBase(S) * Scale + Outline * 2; |
| 519 |
end; |
| 520 |
|
| 521 |
function TCastleFont.TextMove(const S: string): TVector2; |
| 522 |
var |
| 523 |
M: TVector2Integer; |
| 524 |
begin |
| 525 |
if FFont = nil then |
| 526 |
Exit(TVector2.Zero); // font not loaded |
| 527 |
M := FFont.TextMove(S); |
| 528 |
Result := Vector2(M.X, M.Y); |
| 529 |
Result.X := Result.X * Scale; |
| 530 |
if Outline <> 0 then |
| 531 |
Result.X := Result.X + Outline * 2 * StringLength(S); |
| 532 |
Result.Y := Result.Y * Scale; |
| 533 |
end; |
| 534 |
|
| 535 |
procedure TCastleFont.SetUrl(const Value: String); |
| 536 |
begin |
| 537 |
if FUrl <> Value then |
| 538 |
begin |
| 539 |
{ We deliberately change URL even when loading of font file could fail. |
| 540 |
This way URL property is always correctly deserialized in CGE editor, |
| 541 |
even when loading the image failed. } |
| 542 |
FUrl := Value; |
| 543 |
Reload; |
| 544 |
end; |
| 545 |
end; |
| 546 |
|
| 547 |
procedure TCastleFont.SetOptimalSize(const Value: Cardinal); |
| 548 |
begin |
| 549 |
if FOptimalSize <> Value then |
| 550 |
begin |
| 551 |
{ As with SetUrl: we deliberately change current property, even when Reload fails. } |
| 552 |
FOptimalSize := Value; |
| 553 |
Reload; |
| 554 |
end; |
| 555 |
end; |
| 556 |
|
| 557 |
procedure TCastleFont.SetAntiAliased(const Value: Boolean); |
| 558 |
begin |
| 559 |
if FAntiAliased <> Value then |
| 560 |
begin |
| 561 |
{ As with SetUrl: we deliberately change current property, even when Reload fails. } |
| 562 |
FAntiAliased := Value; |
| 563 |
Reload; |
| 564 |
end; |
| 565 |
end; |
| 566 |
|
| 567 |
procedure TCastleFont.SetLoadBasicCharacters(const Value: Boolean); |
| 568 |
begin |
| 569 |
if FLoadBasicCharacters <> Value then |
| 570 |
begin |
| 571 |
{ As with SetUrl: we deliberately change current property, even when Reload fails. } |
| 572 |
FLoadBasicCharacters := Value; |
| 573 |
Reload; |
| 574 |
end; |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TCastleFont.SetLoadCharacters(const Value: String); |
| 578 |
begin |
| 579 |
if FLoadCharacters <> Value then |
| 580 |
begin |
| 581 |
{ As with SetUrl: we deliberately change current property, even when Reload fails. } |
| 582 |
FLoadCharacters := Value; |
| 583 |
Reload; |
| 584 |
end; |
| 585 |
end; |
| 586 |
|
| 587 |
procedure TCastleFont.Reload; |
| 588 |
|
| 589 |
function GetCharactersList: TUnicodeCharList; |
| 590 |
begin |
| 591 |
if (LoadCharacters = '') and LoadBasicCharacters then |
| 592 |
begin |
| 593 |
{ Default, and fast case. TTextureFontData.Create interprets characters list = nil |
| 594 |
just like that: load only SimpleAsciiCharacters. |
| 595 |
No need to create TUnicodeCharList instance to express it explicitly. } |
| 596 |
Result := nil; |
| 597 |
end else |
| 598 |
begin |
| 599 |
Result := TUnicodeCharList.Create; |
| 600 |
if LoadBasicCharacters then |
| 601 |
Result.Add(SimpleAsciiCharacters); |
| 602 |
Result.Add(LoadCharacters); |
| 603 |
end; |
| 604 |
end; |
| 605 |
|
| 606 |
// Load, without catching any exceptions |
| 607 |
procedure LoadCore; |
| 608 |
var |
| 609 |
CharactersList: TUnicodeCharList; |
| 610 |
begin |
| 611 |
CharactersList := GetCharactersList; |
| 612 |
try |
| 613 |
{$warnings off} // this Load is deprecated; actually it should be internal, nested here |
| 614 |
Load(Url, OptimalSize, AntiAliased, CharactersList); |
| 615 |
{$warnings on} |
| 616 |
finally FreeAndNil(CharactersList) end; |
| 617 |
end; |
| 618 |
|
| 619 |
begin |
| 620 |
if CastleDesignMode then |
| 621 |
begin |
| 622 |
{ If loading file failed, and we're inside CGE editor, |
| 623 |
merely report a warning. This allows deserializing in CGE editor |
| 624 |
designs with broken URLs. } |
| 625 |
try |
| 626 |
LoadCore; |
| 627 |
except |
| 628 |
on E: Exception do |
| 629 |
begin |
| 630 |
{ We unload, otherwise we would keep the font loaded, not correctly reflecting |
| 631 |
some new properties. |
| 632 |
Testcase: in editor load TTF to some TCastleFont, |
| 633 |
use it with some TCastleLabel.CustomFont, |
| 634 |
then set TCastlFont.LoadBasicCharacters to false -> font should revert |
| 635 |
to default UIFont (with warning "cannot load font with no glyphs"). } |
| 636 |
Unload; |
| 637 |
WritelnWarning('TCastleFont', 'Failed to load font "%s": %s', |
| 638 |
[UriDisplay(Url), ExceptMessage(E)]); |
| 639 |
end; |
| 640 |
end; |
| 641 |
end else |
| 642 |
begin |
| 643 |
{ When not in editor, most exceptions are just passed upward. |
| 644 |
But we handle EFreeTypeLibraryNotFound. } |
| 645 |
try |
| 646 |
LoadCore; |
| 647 |
except |
| 648 |
on E: EFreeTypeLibraryNotFound do |
| 649 |
begin |
| 650 |
Unload; |
| 651 |
WritelnWarning('TCastleFont', 'Failed to load font "%s": %s', |
| 652 |
[UriDisplay(Url), E.Message]); |
| 653 |
end; |
| 654 |
end; |
| 655 |
end; |
| 656 |
end; |
| 657 |
|
| 658 |
function TCastleFont.FontLoaded: Boolean; |
| 659 |
begin |
| 660 |
Result := FFont <> nil; |
| 661 |
end; |
| 662 |
|
| 663 |
{$endif read_implementation} |