| 1 |
{ |
| 2 |
Copyright 2014-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Data for a 2D font initialized from a FreeType font file (TTextureFontData). } |
| 17 |
unit CastleTextureFontData; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
{ Workaround a convoluted bug in Delphi < 10.4. |
| 22 |
Doesn't affect new Delphi versions, doesn't affect FPC. |
| 23 |
|
| 24 |
More precisely, we confirmed |
| 25 |
- it occurs in Delphi 10.2.3 |
| 26 |
- it is fixed in Delphi 10.4.2 and all newer Delphi versions we tested (11.x, 12.x). |
| 27 |
|
| 28 |
Details: |
| 29 |
|
| 30 |
In these older Delphi versions, |
| 31 |
using "inherited", "inherited Create" or "inherited Create(nil)" |
| 32 |
in TTextureFontData.TGlyphDictionary.Create |
| 33 |
leaves the created instance in state when internal FComparer is nil |
| 34 |
(as if TDictionary<TKey,TValue>.Create constructor wasn't called). |
| 35 |
Looks like having own constructor TGlyphDictionary.Create confuses |
| 36 |
these early Delphi versions. |
| 37 |
|
| 38 |
Workaround is to create and use own comparer, instance of |
| 39 |
TUnicodeCharEqualityComparer. |
| 40 |
|
| 41 |
This isn't related to whether this constructor has "reintroduce" or not, |
| 42 |
tested. |
| 43 |
|
| 44 |
In effect, |
| 45 |
- Hash method causes Access Violation, |
| 46 |
- and in effect all other routines (Add, AddOrSetValue, our SetItems) |
| 47 |
cause Access Violation. |
| 48 |
|
| 49 |
In the past, we even applied this change to any Delphi version, because |
| 50 |
- The TUnicodeCharEqualityComparer makes sense anyway, the default |
| 51 |
comparer from Generics.Collections for Cardinal wouldn't do anything |
| 52 |
substantially different or more optimal. |
| 53 |
- This way TUnicodeCharEqualityComparer will be tested even when we run |
| 54 |
through latest Delphi, like 11. |
| 55 |
- This way we don't care about carefully testing at which Delphi version |
| 56 |
(10.3.x, 10.4.x?) the bug is fixed. |
| 57 |
|
| 58 |
However, we later found out the workaround is not really applicable |
| 59 |
to all Delphi versions. Freeing our comparer ("FreeAndNil(FComparer)") |
| 60 |
makes a crash with Delphi 12.0, only in RELEASE mode (goes OK in DEBUG...). |
| 61 |
Looks like in newer Delphis, we are not supposed to free the comparer. |
| 62 |
But not freeing it means we most likely have a memory leak in older Delphis. |
| 63 |
So part of the workaround would have to be conditional on Delphi version anyway. |
| 64 |
|
| 65 |
So we apply the workaround selectively, only to older Delphis. |
| 66 |
} |
| 67 |
{$ifndef FPC} |
| 68 |
{$if CompilerVersion < 34} |
| 69 |
{$define CASTLE_WORKAROUND_GLYPH_COMPARER} |
| 70 |
{$endif} |
| 71 |
{$endif} |
| 72 |
|
| 73 |
interface |
| 74 |
|
| 75 |
uses Generics.Collections, Generics.Defaults, |
| 76 |
CastleVectors, CastleUnicode, CastleStringUtils, CastleImages, |
| 77 |
CastleInternalFreeType; |
| 78 |
|
| 79 |
type |
| 80 |
{ Raised by |
| 81 |
@link(TTextureFontData.Create) or @link(TCastleFont.Load) when |
| 82 |
the freetype library cannot be found, and thus font files cannot be read. } |
| 83 |
EFreeTypeLibraryNotFound = CastleInternalFreeType.EFreeTypeLibraryNotFound; |
| 84 |
|
| 85 |
{ Data for a 2D font initialized from a FreeType font file, like ttf. } |
| 86 |
TTextureFontData = class |
| 87 |
public |
| 88 |
type |
| 89 |
{ Information about a particular font glyph. } |
| 90 |
TGlyph = class |
| 91 |
public |
| 92 |
{ How to shift the glyph with respect |
| 93 |
to the starting position when drawing. } |
| 94 |
X, Y: Integer; |
| 95 |
{ How to advance the position for next glyph. } |
| 96 |
AdvanceX, AdvanceY: Integer; |
| 97 |
{ Size of the glyph. |
| 98 |
Always Width and Height >= 0 (they are Cardinal type after all), |
| 99 |
but note that it is possible that Width = Height = 0 |
| 100 |
(it commonly happens for space ' ' character). } |
| 101 |
Width, Height: Cardinal; |
| 102 |
{ Position of the glyph on the image in TTextureFontData.Image. } |
| 103 |
ImageX, ImageY: Cardinal; |
| 104 |
end; |
| 105 |
|
| 106 |
{ Map Unicode code to a TGlyph representation. } |
| 107 |
TGlyphDictionary = class({$ifdef FPC}specialize{$endif} TDictionary<TUnicodeChar, TGlyph>) |
| 108 |
strict private |
| 109 |
{$ifdef CASTLE_WORKAROUND_GLYPH_COMPARER} |
| 110 |
type |
| 111 |
TUnicodeCharEqualityComparer = class(TCustomComparer<TUnicodeChar>) |
| 112 |
function Compare(const Left, Right: TUnicodeChar): Integer; override; |
| 113 |
function Equals(const Left, Right: TUnicodeChar): Boolean; override; |
| 114 |
function GetHashCode(const Value: TUnicodeChar): Integer; override; |
| 115 |
end; |
| 116 |
var |
| 117 |
FComparer: TUnicodeCharEqualityComparer; |
| 118 |
{$endif} |
| 119 |
|
| 120 |
FOwnsGlyphs: boolean; |
| 121 |
function GetItems(const AKey: TUnicodeChar): TGlyph; |
| 122 |
procedure SetItems(const AKey: TUnicodeChar; const AValue: TGlyph); |
| 123 |
public |
| 124 |
property OwnsGlyphs: boolean read FOwnsGlyphs write FOwnsGlyphs default true; |
| 125 |
{ Access dictionary items. |
| 126 |
Setting this is allowed regardless if the key previously existed or not, |
| 127 |
in other words: setting this does AddOrSetValue, contrary to the ancestor TDictionary |
| 128 |
that only allows setting when the key already exists. } |
| 129 |
property Items [const AKey: TUnicodeChar]: TGlyph read GetItems write SetItems; default; |
| 130 |
constructor Create; reintroduce; |
| 131 |
destructor Destroy; override; |
| 132 |
end; |
| 133 |
private |
| 134 |
type |
| 135 |
TGlyphCharDictionary = array [Byte] of TGlyph; |
| 136 |
const |
| 137 |
MaxFallbackGlyphWarnings = 10; |
| 138 |
var |
| 139 |
FUrl: String; |
| 140 |
FAntiAliased: Boolean; |
| 141 |
FSize: Cardinal; |
| 142 |
{ For optimization of rendering normal 8-bit fonts (like standard ASCII |
| 143 |
text), we keep glyphs with index < 256 listed in TGlyphCharDictionary. |
| 144 |
Only the glyphs with index >= 256 are kept on extra TGlyphDictionary. } |
| 145 |
{ Non-nil only for filled glyphs. } |
| 146 |
FGlyphsByte: TGlyphCharDictionary; |
| 147 |
FGlyphsExtra: TGlyphDictionary; |
| 148 |
FImage: TGrayscaleImage; |
| 149 |
FFirstExistingGlyph: TGlyph; |
| 150 |
FFirstExistingGlyphChar: TUnicodeChar; |
| 151 |
{ If the requested glyph doesn't exit, @link(Glyph) will use this one |
| 152 |
as a fallback. } |
| 153 |
FFallbackGlyph: TGlyph; |
| 154 |
FFallbackGlyphChar: TUnicodeChar; |
| 155 |
FUseFallbackGlyph: Boolean; |
| 156 |
FallbackGlyphWarnings: Integer; |
| 157 |
|
| 158 |
procedure CalculateFallbackGlyph; |
| 159 |
procedure MakeFallbackWarning(const C: TUnicodeChar); |
| 160 |
public |
| 161 |
{ Create by reading a FreeType font file, like ttf. |
| 162 |
|
| 163 |
Providing charaters list as @nil means that we only create glyphs |
| 164 |
for SimpleAsciiCharacters, which includes only the basic ASCII characters. |
| 165 |
The ACharacters instance @italic(does not) become owned by this object, |
| 166 |
so remember to free it after calling this constructor. |
| 167 |
|
| 168 |
@raises EFreeTypeLibraryNotFound If the freetype library is not installed. } |
| 169 |
constructor Create(const AUrl: String; |
| 170 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 171 |
ACharacters: TUnicodeCharList = nil); |
| 172 |
|
| 173 |
{ Create from a ready data for glyphs and image. |
| 174 |
Useful when font data is embedded inside the Pascal source code. |
| 175 |
AGlyphs instance, and AImage instance, become owned by this class. } |
| 176 |
constructor CreateFromData(const AGlyphs: TGlyphDictionary; |
| 177 |
const AImage: TGrayscaleImage; |
| 178 |
const ASize: Cardinal; const AnAntiAliased: Boolean); |
| 179 |
destructor Destroy; override; |
| 180 |
|
| 181 |
property Url: String read FUrl; |
| 182 |
property Size: Cardinal read FSize; |
| 183 |
property AntiAliased: Boolean read FAntiAliased; |
| 184 |
|
| 185 |
{ Read-only information about a glyph for given character. |
| 186 |
|
| 187 |
When AllowUsingFallbackGlyph and UseFallbackGlyph (both are @true |
| 188 |
by default) then we always return non-nil glyph. |
| 189 |
If the desired glyph was not really present, we make a warning |
| 190 |
(using WritelnWarning) and return a fallback glyph. |
| 191 |
|
| 192 |
When not (AllowUsingFallbackGlyph and UseFallbackGlyph) |
| 193 |
then we return @nil for a missing glyph. |
| 194 |
Glyph may be missing because it was not requested at constructor, |
| 195 |
or because it doesn't exist in the font data. } |
| 196 |
function Glyph(const C: TUnicodeChar; |
| 197 |
const AllowUsingFallbackGlyph: Boolean = true): TGlyph; |
| 198 |
|
| 199 |
{ When a glyph (picture of a particular character) in a font doesn't exist, |
| 200 |
by default we make a warning (using WritelnWarning) and use a fallback |
| 201 |
glyph, like "?". This lets user know that some character is there. |
| 202 |
|
| 203 |
Set this to @false to just silently omit a missing glyph. |
| 204 |
The @link(Glyph) method will just return (silently) @nil in this case. } |
| 205 |
property UseFallbackGlyph: Boolean |
| 206 |
read FUseFallbackGlyph write FUseFallbackGlyph default true; |
| 207 |
|
| 208 |
property Image: TGrayscaleImage read FImage; |
| 209 |
|
| 210 |
{ List all characters for which glyphs are actually loaded. |
| 211 |
@link(Glyph) will answer non-nil exactly for these characters. |
| 212 |
The resulting list instance is owned by caller, so take care to free it. } |
| 213 |
function LoadedGlyphs: TUnicodeCharList; |
| 214 |
|
| 215 |
function TextWidth(const S: string): Integer; |
| 216 |
function TextHeight(const S: string): Integer; |
| 217 |
{ The height (above the baseline) of the text. |
| 218 |
This doesn't take into account height of the text below the baseline |
| 219 |
(for example letter "y" has the tail below the baseline in most fonts). } |
| 220 |
function TextHeightBase(const S: string): Integer; |
| 221 |
function TextMove(const S: string): TVector2Integer; |
| 222 |
end; |
| 223 |
|
| 224 |
implementation |
| 225 |
|
| 226 |
uses Classes, SysUtils, Character, |
| 227 |
CastleLog, CastleUtils, CastleUriUtils, CastleFilesUtils, CastleDownload; |
| 228 |
|
| 229 |
{ TUnicodeCharEqualityComparer ----------------------------------------------- } |
| 230 |
|
| 231 |
{$ifdef CASTLE_WORKAROUND_GLYPH_COMPARER} |
| 232 |
|
| 233 |
function TTextureFontData.TGlyphDictionary.TUnicodeCharEqualityComparer. |
| 234 |
Compare(const Left, Right: TUnicodeChar): Integer; |
| 235 |
begin |
| 236 |
Result := Left - Right; |
| 237 |
end; |
| 238 |
|
| 239 |
function TTextureFontData.TGlyphDictionary.TUnicodeCharEqualityComparer. |
| 240 |
Equals(const Left, Right: TUnicodeChar): Boolean; |
| 241 |
begin |
| 242 |
Result := Left = Right; |
| 243 |
end; |
| 244 |
|
| 245 |
function TTextureFontData.TGlyphDictionary.TUnicodeCharEqualityComparer. |
| 246 |
GetHashCode(const Value: TUnicodeChar): Integer; |
| 247 |
begin |
| 248 |
Result := Value; |
| 249 |
end; |
| 250 |
|
| 251 |
{$endif} |
| 252 |
|
| 253 |
{ TTextureFontData.TGlyphDictionary ------------------------------------------ } |
| 254 |
|
| 255 |
constructor TTextureFontData.TGlyphDictionary.Create; |
| 256 |
begin |
| 257 |
{$ifdef CASTLE_WORKAROUND_GLYPH_COMPARER} |
| 258 |
FComparer := TUnicodeCharEqualityComparer.Create; |
| 259 |
inherited Create(FComparer); |
| 260 |
{$else} |
| 261 |
inherited; |
| 262 |
{$endif} |
| 263 |
|
| 264 |
FOwnsGlyphs := true; |
| 265 |
end; |
| 266 |
|
| 267 |
destructor TTextureFontData.TGlyphDictionary.Destroy; |
| 268 |
var |
| 269 |
G: TGlyph; |
| 270 |
begin |
| 271 |
if OwnsGlyphs then |
| 272 |
for G in Values do |
| 273 |
G.Free; |
| 274 |
Clear; |
| 275 |
inherited; |
| 276 |
{$ifdef CASTLE_WORKAROUND_GLYPH_COMPARER} |
| 277 |
{ Don't do this on new Delphis. |
| 278 |
This makes a crash with Delphi 12 when compiled |
| 279 |
in RELEASE (but not DEBUG) mode. |
| 280 |
|
| 281 |
Now CASTLE_WORKAROUND_GLYPH_COMPARER is only defined for older Delphis, |
| 282 |
should we free or not? |
| 283 |
|
| 284 |
Decision: Let eventual memory leaks happen with older Delphis. |
| 285 |
Better memory leaks, than crash. |
| 286 |
Everything is great (no crash, no leak) with Delphis >= 10.4. } |
| 287 |
//FreeAndNil(FComparer); |
| 288 |
{$endif} |
| 289 |
end; |
| 290 |
|
| 291 |
function TTextureFontData.TGlyphDictionary.GetItems(const AKey: TUnicodeChar): TGlyph; |
| 292 |
begin |
| 293 |
Result := inherited Items[AKey]; |
| 294 |
end; |
| 295 |
|
| 296 |
procedure TTextureFontData.TGlyphDictionary.SetItems(const AKey: TUnicodeChar; const AValue: TGlyph); |
| 297 |
begin |
| 298 |
AddOrSetValue(AKey, AValue); |
| 299 |
end; |
| 300 |
|
| 301 |
{ TTextureFontData ----------------------------------------------------------------- } |
| 302 |
|
| 303 |
constructor TTextureFontData.Create(const AUrl: String; |
| 304 |
const ASize: Cardinal; const AnAntiAliased: Boolean; |
| 305 |
ACharacters: TUnicodeCharList); |
| 306 |
var |
| 307 |
FontId: Integer; |
| 308 |
|
| 309 |
function GetGlyphInfo(const C: TUnicodeChar): TGlyph; |
| 310 |
var |
| 311 |
Bitmaps: TStringBitmaps; |
| 312 |
Bitmap: PFontBitmap; |
| 313 |
begin |
| 314 |
if AntiAliased then |
| 315 |
Bitmaps := FontMgr.GetStringGray(FontId, UnicodeCharToString(C), Size) else |
| 316 |
Bitmaps := FontMgr.GetString(FontId, UnicodeCharToString(C), Size); |
| 317 |
|
| 318 |
try |
| 319 |
if Bitmaps.Count = 0 then |
| 320 |
begin |
| 321 |
WritelnWarning('Font', Format('Font "%s" does not contain glyph for character "%s" (index %d)', |
| 322 |
[Url, C, Ord(C)])); |
| 323 |
Exit(nil); |
| 324 |
end; |
| 325 |
|
| 326 |
Bitmap := Bitmaps.Bitmaps[0]; |
| 327 |
if Bitmaps.Count > 1 then |
| 328 |
WritelnWarning('Font', Format('Font "%s" contains a sequence of glyphs (more than a single glyph) for a single character "%s" (index %d)', |
| 329 |
[Url, C, Ord(C)])); |
| 330 |
if (Bitmap^.Width < 0) or (Bitmap^.Height < 0) then |
| 331 |
begin |
| 332 |
WritelnWarning('Font', Format('Font "%s" contains a glyphs with Width or Height < 0 for character "%s" (index %d)', |
| 333 |
[Url, C, Ord(C)])); |
| 334 |
Exit(nil); |
| 335 |
end; |
| 336 |
|
| 337 |
Result := TGlyph.Create; |
| 338 |
Result.Width := Bitmap^.Width; |
| 339 |
Result.Height := Bitmap^.Height; |
| 340 |
Result.X := -Bitmap^.X; |
| 341 |
Result.Y := Bitmap^.Height - 1 + Bitmap^.Y; |
| 342 |
Result.AdvanceX := Bitmap^.AdvanceX shr 10; // 64 * 16, looks like this is just magic for freetype |
| 343 |
Result.AdvanceY := Bitmap^.AdvanceY shr 10; // 64 * 16, looks like this is just magic for freetype |
| 344 |
finally FreeAndNil(Bitmaps) end; |
| 345 |
end; |
| 346 |
|
| 347 |
{ Copy glyph data for character C (assuming it is Ok, that is GetGlyphInfo |
| 348 |
returned non-nil for this) to the Image (at position ImageX, ImageY). } |
| 349 |
procedure GetGlyphData(const C: TUnicodeChar; const ImageX, ImageY: Cardinal); |
| 350 |
var |
| 351 |
Bitmaps: TStringBitmaps; |
| 352 |
Bitmap: PFontBitmap; |
| 353 |
|
| 354 |
{ Extracting data from glyph with Pitch, like in TFreeTypeFont.DrawChar. } |
| 355 |
procedure DrawChar; |
| 356 |
var |
| 357 |
B, RX, RY: Integer; |
| 358 |
begin |
| 359 |
B := 0; |
| 360 |
for RY := 0 to Bitmap^.Height - 1 do |
| 361 |
begin |
| 362 |
for RX := 0 to Bitmap^.Width - 1 do |
| 363 |
Image.PixelPtr(ImageX + RX, ImageY + Bitmap^.Height - 1 - RY)^ := Bitmap^.Data^[B + RX]; |
| 364 |
Inc(B, Bitmap^.Pitch); |
| 365 |
end; |
| 366 |
end; |
| 367 |
|
| 368 |
{ Extracting data with Pitch, like in TFreeTypeFont.DrawCharBW. } |
| 369 |
procedure DrawCharBW; |
| 370 |
const |
| 371 |
Bits: array [0..7] of Byte = (128,64,32,16,8,4,2,1); |
| 372 |
var |
| 373 |
RB: Byte; |
| 374 |
RX, RY, B, L: Integer; |
| 375 |
begin |
| 376 |
B := 0; |
| 377 |
for RY := 0 to Bitmap^.Height - 1 do |
| 378 |
begin |
| 379 |
L := 0; |
| 380 |
for RX := 0 to Bitmap^.Width - 1 do |
| 381 |
begin |
| 382 |
RB := RX mod 8; |
| 383 |
if (Bitmap^.Data^[B + L] and Bits[RB]) <> 0 then |
| 384 |
Image.PixelPtr(ImageX + RX, ImageY + Bitmap^.Height - 1 - RY)^ := 255; |
| 385 |
if RB = 7 then |
| 386 |
Inc(L); |
| 387 |
end; |
| 388 |
Inc(B, Bitmap^.Pitch); |
| 389 |
end; |
| 390 |
end; |
| 391 |
|
| 392 |
begin |
| 393 |
if AntiAliased then |
| 394 |
Bitmaps := FontMgr.GetStringGray(FontId, UnicodeCharToString(C), Size) else |
| 395 |
Bitmaps := FontMgr.GetString(FontId, UnicodeCharToString(C), Size); |
| 396 |
try |
| 397 |
Bitmap := Bitmaps.Bitmaps[0]; |
| 398 |
if (Bitmap^.Pitch < 0) then |
| 399 |
begin |
| 400 |
WritelnWarning('Font', Format('Font "%s" contains a glyphs with Pitch < 0 for character "%s" (index %d)', |
| 401 |
[Url, C, Ord(C)])); |
| 402 |
Exit; |
| 403 |
end; |
| 404 |
if AntiAliased then |
| 405 |
DrawChar else |
| 406 |
DrawCharBW; |
| 407 |
finally FreeAndNil(Bitmaps) end; |
| 408 |
end; |
| 409 |
|
| 410 |
const |
| 411 |
{ Separate the glyphs for safety, to avoid pulling in colors |
| 412 |
from neighboring letters when drawing (floating point errors could in theory |
| 413 |
make small errors moving us outside of the desired pixel). } |
| 414 |
GlyphPadding = 2; |
| 415 |
|
| 416 |
var |
| 417 |
GlyphInfo: TGlyph; |
| 418 |
GlyphsCount, ImageSize: Cardinal; |
| 419 |
MaxWidth, MaxHeight, ImageX, ImageY: Cardinal; |
| 420 |
C: TUnicodeChar; |
| 421 |
TemporaryCharacters: boolean; |
| 422 |
begin |
| 423 |
inherited Create; |
| 424 |
FUrl := AUrl; |
| 425 |
FSize := ASize; |
| 426 |
FAntiAliased := AnAntiAliased; |
| 427 |
FUseFallbackGlyph := true; |
| 428 |
|
| 429 |
InitFontMgr; |
| 430 |
FontId := FontMgr.RequestFont(Url); |
| 431 |
|
| 432 |
TemporaryCharacters := ACharacters = nil; |
| 433 |
if TemporaryCharacters then |
| 434 |
begin |
| 435 |
ACharacters := TUnicodeCharList.Create; |
| 436 |
ACharacters.Add(SimpleAsciiCharacters); |
| 437 |
end; |
| 438 |
|
| 439 |
try |
| 440 |
FGlyphsExtra := TGlyphDictionary.Create; |
| 441 |
|
| 442 |
GlyphsCount := 0; |
| 443 |
MaxWidth := 0; |
| 444 |
MaxHeight := 0; |
| 445 |
for C in ACharacters do |
| 446 |
begin |
| 447 |
GlyphInfo := GetGlyphInfo(C); |
| 448 |
if C <= High(FGlyphsByte) then |
| 449 |
FGlyphsByte[C] := GlyphInfo |
| 450 |
else |
| 451 |
FGlyphsExtra[C] := GlyphInfo; |
| 452 |
if GlyphInfo <> nil then |
| 453 |
begin |
| 454 |
if FFirstExistingGlyph = nil then |
| 455 |
begin |
| 456 |
FFirstExistingGlyph := GlyphInfo; |
| 457 |
FFirstExistingGlyphChar := C; |
| 458 |
end; |
| 459 |
Inc(GlyphsCount); |
| 460 |
MaxVar(MaxWidth , GlyphInfo.Width); |
| 461 |
MaxVar(MaxHeight, GlyphInfo.Height); |
| 462 |
end else |
| 463 |
WritelnWarning('Font "%s" does not contain requested character %s (Unicode number %d)', |
| 464 |
[UriDisplay(Url), UnicodeCharToString(C), C]); |
| 465 |
end; |
| 466 |
|
| 467 |
if GlyphsCount = 0 then |
| 468 |
raise Exception.Create('Cannot create a font with no glyphs'); |
| 469 |
|
| 470 |
MaxWidth := MaxWidth + GlyphPadding; |
| 471 |
MaxHeight := MaxHeight + GlyphPadding; |
| 472 |
|
| 473 |
ImageSize := 8; |
| 474 |
while (ImageSize div MaxHeight) * (ImageSize div MaxWidth) < GlyphsCount do |
| 475 |
ImageSize := ImageSize * 2; |
| 476 |
|
| 477 |
WritelnLog('Font', 'Creating image %dx%d to store glyphs of font "%s" (%d glyphs, max glyph size (including %d pixel padding) is %dx%d)', |
| 478 |
[ImageSize, ImageSize, Url, GlyphsCount, GlyphPadding, MaxWidth, MaxHeight]); |
| 479 |
|
| 480 |
FImage := TGrayscaleImage.Create(ImageSize, ImageSize); |
| 481 |
Image.Clear(0); |
| 482 |
Image.TreatAsAlpha := true; |
| 483 |
// Image.Url doesn't change image contents, it is only information for profiler |
| 484 |
Image.Url := Url + Format('[font converted to a texture, size: %d, anti-aliased: %s]', [ |
| 485 |
Size, |
| 486 |
BoolToStr(AntiAliased, true) |
| 487 |
]); |
| 488 |
|
| 489 |
ImageX := 0; |
| 490 |
ImageY := 0; |
| 491 |
for C in ACharacters do |
| 492 |
begin |
| 493 |
GlyphInfo := Glyph(C, false); |
| 494 |
if GlyphInfo <> nil then |
| 495 |
begin |
| 496 |
GlyphInfo.ImageX := ImageX; |
| 497 |
GlyphInfo.ImageY := ImageY; |
| 498 |
|
| 499 |
GetGlyphData(C, ImageX, ImageY); |
| 500 |
|
| 501 |
ImageX := ImageX + MaxWidth; |
| 502 |
if ImageX + MaxWidth >= ImageSize then |
| 503 |
begin |
| 504 |
ImageX := 0; |
| 505 |
ImageY := ImageY + MaxHeight; |
| 506 |
end; |
| 507 |
end; |
| 508 |
end; |
| 509 |
|
| 510 |
// Debug: SaveImage(Image, '/tmp/a.png'); |
| 511 |
finally |
| 512 |
if TemporaryCharacters then |
| 513 |
FreeAndNil(ACharacters); |
| 514 |
end; |
| 515 |
|
| 516 |
CalculateFallbackGlyph; |
| 517 |
end; |
| 518 |
|
| 519 |
constructor TTextureFontData.CreateFromData(const AGlyphs: TGlyphDictionary; |
| 520 |
const AImage: TGrayscaleImage; |
| 521 |
const ASize: Cardinal; const AnAntiAliased: Boolean); |
| 522 |
var |
| 523 |
C: TUnicodeChar; |
| 524 |
GlyphPair: {$ifdef FPC}TGlyphDictionary.TDictionaryPair{$else}TPair<TUnicodeChar, TGlyph>{$endif}; |
| 525 |
begin |
| 526 |
inherited Create; |
| 527 |
FUrl := AImage.Url; // this is only for debug purposes now (to potentially display in debug, profiler etc.) |
| 528 |
FSize := ASize; |
| 529 |
FAntiAliased := AnAntiAliased; |
| 530 |
FUseFallbackGlyph := true; |
| 531 |
|
| 532 |
// WritelnLog('Creating font from %s with %d glyphs', [ |
| 533 |
// AImage.Url, |
| 534 |
// AGlyphs.Count |
| 535 |
// ]); |
| 536 |
|
| 537 |
{ split AGlyphs into FGlyphsByte and FGlyphsExtra } |
| 538 |
FGlyphsExtra := TGlyphDictionary.Create; |
| 539 |
for GlyphPair in AGlyphs do |
| 540 |
begin |
| 541 |
C := GlyphPair.Key; |
| 542 |
if C <= High(FGlyphsByte) then |
| 543 |
FGlyphsByte[C] := GlyphPair.Value |
| 544 |
else |
| 545 |
FGlyphsExtra[C] := GlyphPair.Value; |
| 546 |
|
| 547 |
if FFirstExistingGlyph = nil then |
| 548 |
begin |
| 549 |
FFirstExistingGlyph := GlyphPair.Value; |
| 550 |
FFirstExistingGlyphChar := C; |
| 551 |
end; |
| 552 |
end; |
| 553 |
AGlyphs.OwnsGlyphs := false; |
| 554 |
AGlyphs.Free; // we own AGlyphs, for now we just free them |
| 555 |
|
| 556 |
FImage := AImage; |
| 557 |
|
| 558 |
CalculateFallbackGlyph; |
| 559 |
end; |
| 560 |
|
| 561 |
destructor TTextureFontData.Destroy; |
| 562 |
var |
| 563 |
C: Byte; |
| 564 |
begin |
| 565 |
FreeAndNil(FGlyphsExtra); |
| 566 |
for C := Low(Byte) to High(Byte) do |
| 567 |
FreeAndNil(FGlyphsByte[C]); |
| 568 |
FreeAndNil(FImage); |
| 569 |
inherited; |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TTextureFontData.CalculateFallbackGlyph; |
| 573 |
|
| 574 |
function TryFallback(const C: TUnicodeChar): Boolean; |
| 575 |
begin |
| 576 |
FFallbackGlyph := Glyph(C, false); |
| 577 |
FFallbackGlyphChar := C; |
| 578 |
Result := FFallbackGlyph <> nil; |
| 579 |
end; |
| 580 |
|
| 581 |
begin |
| 582 |
if not TryFallback(Ord('?')) then |
| 583 |
if not TryFallback(Ord('_')) then |
| 584 |
if not TryFallback(Ord('.')) then |
| 585 |
begin |
| 586 |
FFallbackGlyph := FFirstExistingGlyph; |
| 587 |
FFallbackGlyphChar := FFirstExistingGlyphChar; |
| 588 |
end; |
| 589 |
end; |
| 590 |
|
| 591 |
function TTextureFontData.Glyph(const C: TUnicodeChar; |
| 592 |
const AllowUsingFallbackGlyph: Boolean): TGlyph; |
| 593 |
begin |
| 594 |
if C <= High(FGlyphsByte) then |
| 595 |
Result := FGlyphsByte[C] |
| 596 |
else |
| 597 |
if not FGlyphsExtra.TryGetValue(C, Result) then |
| 598 |
Result := nil; |
| 599 |
|
| 600 |
if (Result = nil) and AllowUsingFallbackGlyph and UseFallbackGlyph then |
| 601 |
begin |
| 602 |
Result := FFallbackGlyph; |
| 603 |
MakeFallbackWarning(C); |
| 604 |
end; |
| 605 |
end; |
| 606 |
|
| 607 |
procedure TTextureFontData.MakeFallbackWarning(const C: TUnicodeChar); |
| 608 |
begin |
| 609 |
if FallbackGlyphWarnings < MaxFallbackGlyphWarnings then |
| 610 |
begin |
| 611 |
Inc(FallbackGlyphWarnings); |
| 612 |
WritelnWarning('Font is missing glyph for character %s (Unicode number %d)', [ |
| 613 |
UnicodeCharToString(C), |
| 614 |
C |
| 615 |
]); |
| 616 |
if FallbackGlyphWarnings = MaxFallbackGlyphWarnings then |
| 617 |
WritelnWarning('No further warnings about missing glyphs will be reported for this font (to avoid slowing down the application by flooding the log with warnings)'); |
| 618 |
end; |
| 619 |
end; |
| 620 |
|
| 621 |
function TTextureFontData.LoadedGlyphs: TUnicodeCharList; |
| 622 |
var |
| 623 |
C: TUnicodeChar; |
| 624 |
begin |
| 625 |
Result := TUnicodeCharList.Create; |
| 626 |
for C := 0 to High(FGlyphsByte) do |
| 627 |
if FGlyphsByte[C] <> nil then |
| 628 |
Result.Add(C); |
| 629 |
for C in FGlyphsExtra.Keys do |
| 630 |
Result.Add(C); |
| 631 |
end; |
| 632 |
|
| 633 |
function TTextureFontData.TextWidth(const S: string): Integer; |
| 634 |
var |
| 635 |
Iter: TCastleStringIterator; |
| 636 |
G: TTextureFontData.TGlyph; |
| 637 |
begin |
| 638 |
Result := 0; |
| 639 |
Iter.Start(S); |
| 640 |
while Iter.GetNext do |
| 641 |
begin |
| 642 |
G := Glyph(Iter.Current); |
| 643 |
if G <> nil then |
| 644 |
Result := Result + G.AdvanceX; |
| 645 |
end; |
| 646 |
end; |
| 647 |
|
| 648 |
function TTextureFontData.TextHeight(const S: string): Integer; |
| 649 |
var |
| 650 |
Iter: TCastleStringIterator; |
| 651 |
MinY, MaxY, YOrigin: Integer; |
| 652 |
G: TTextureFontData.TGlyph; |
| 653 |
begin |
| 654 |
MinY := 0; |
| 655 |
MaxY := 0; |
| 656 |
|
| 657 |
Iter.Start(S); |
| 658 |
while Iter.GetNext do |
| 659 |
begin |
| 660 |
G := Glyph(Iter.Current); |
| 661 |
if G <> nil then |
| 662 |
begin |
| 663 |
YOrigin := G.Y; |
| 664 |
MinVar(MinY, -YOrigin); |
| 665 |
MaxVar(MaxY, G.Height - YOrigin); |
| 666 |
end; |
| 667 |
end; |
| 668 |
Result := MaxY - MinY; |
| 669 |
end; |
| 670 |
|
| 671 |
function TTextureFontData.TextMove(const S: string): TVector2Integer; |
| 672 |
var |
| 673 |
Iter: TCastleStringIterator; |
| 674 |
G: TTextureFontData.TGlyph; |
| 675 |
begin |
| 676 |
Result := TVector2Integer.Zero; |
| 677 |
|
| 678 |
Iter.Start(S); |
| 679 |
while Iter.GetNext do |
| 680 |
begin |
| 681 |
G := Glyph(Iter.Current); |
| 682 |
if G <> nil then |
| 683 |
begin |
| 684 |
Result.X := Result.X + G.AdvanceX; |
| 685 |
Result.Y := Result.Y + G.AdvanceY; |
| 686 |
end; |
| 687 |
end; |
| 688 |
end; |
| 689 |
|
| 690 |
function TTextureFontData.TextHeightBase(const S: string): Integer; |
| 691 |
var |
| 692 |
Iter: TCastleStringIterator; |
| 693 |
G: TTextureFontData.TGlyph; |
| 694 |
begin |
| 695 |
Result := 0; |
| 696 |
{ This is just like TextHeight implementation, except we only |
| 697 |
calculate (as Result) the MaxY value (assuming that MinY is zero). } |
| 698 |
Iter.Start(S); |
| 699 |
while Iter.GetNext do |
| 700 |
begin |
| 701 |
G := Glyph(Iter.Current); |
| 702 |
if G <> nil then |
| 703 |
MaxVar(Result, G.Height - G.Y); |
| 704 |
end; |
| 705 |
end; |
| 706 |
|
| 707 |
end. |