| 1 |
{%MainUnit castlefonts.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Font that uses @italic(another) TCastleAbstractFont for rendering and sizing, |
| 21 |
but modifies the underlying font size. |
| 22 |
Simply set the @code(Size) property of this instance to non-zero |
| 23 |
to force the specific size. |
| 24 |
|
| 25 |
The underlying font properties remain unchanged |
| 26 |
(so it can be still used for other purposes, |
| 27 |
directly or by other TCustomizedFont wrappers). } |
| 28 |
TCustomizedFont = class(TCastleAbstractFont) |
| 29 |
strict private |
| 30 |
FSourceFont: TCastleAbstractFont; |
| 31 |
FAlternativeSizes: TComponentList; |
| 32 |
{ Font for current Size, from BestSourceFont. |
| 33 |
May be @nil if no font alternative yet loaded. } |
| 34 |
SubFont: TCastleAbstractFont; |
| 35 |
{ Set SubFont to one of SourceFont of FAlternativeSizes, |
| 36 |
depending on the current Size. } |
| 37 |
procedure UpdateSubFont; |
| 38 |
procedure SubFontCustomizeBegin; |
| 39 |
procedure SubFontCustomizeEnd; |
| 40 |
procedure SetSourceFont(const Value: TCastleAbstractFont); |
| 41 |
procedure SubFontFontSizeChange(Sender: TObject); |
| 42 |
strict protected |
| 43 |
procedure Measure(out AHeight, ACapHeight, ADescenderHeight: Single); override; |
| 44 |
procedure GLContextClose; override; |
| 45 |
procedure SetSize(const Value: Single); override; |
| 46 |
protected |
| 47 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 48 |
public |
| 49 |
constructor Create(AOwner: TComponent); override; |
| 50 |
destructor Destroy; override; |
| 51 |
|
| 52 |
property SourceFont: TCastleAbstractFont read FSourceFont write SetSourceFont; |
| 53 |
|
| 54 |
procedure PrepareResources; override; |
| 55 |
procedure Print(const X, Y: Single; const Color: TCastleColor; |
| 56 |
const S: string); override; |
| 57 |
function TextWidth(const S: string): Single; override; |
| 58 |
function TextHeight(const S: string): Single; override; |
| 59 |
function TextHeightBase(const S: string): Single; override; |
| 60 |
function TextMove(const S: string): TVector2; override; |
| 61 |
function EffectiveSize: Single; override; |
| 62 |
function FontLoaded: Boolean; override; |
| 63 |
|
| 64 |
{ Add any number of alternative source fonts. |
| 65 |
Before actually using them for rendering, |
| 66 |
we always choose the one with size most matching our desired @link(Size). |
| 67 |
This way you can e.g. load the same font in sizes 10, 50, 100, |
| 68 |
and have good quality font rendering in various sizes. } |
| 69 |
procedure AddAlternativeSourceFont(const ASourceFont: TCastleAbstractFont); |
| 70 |
|
| 71 |
{ Load the same font to a number of textures with different sizes. |
| 72 |
At rendering, we will automatically use the best size. |
| 73 |
This sets @link(SourceFont) and @link(AddAlternativeSourceFont). |
| 74 |
|
| 75 |
This allows to achieve better look than TTexturedFont with one size. } |
| 76 |
procedure Load(const Url: String; |
| 77 |
const ASizes: array of Integer; const AnAntiAliased: boolean; |
| 78 |
const ACharacters: TUnicodeCharList = nil); |
| 79 |
|
| 80 |
{ Return SourceFont or one of the fonts added by @link(AddAlternativeSourceFont), |
| 81 |
to have the font with @link(TCastleAbstractFont.Size) closest to the given ASize. } |
| 82 |
function BestSourceFont(const ASize: Single): TCastleAbstractFont; |
| 83 |
end; |
| 84 |
|
| 85 |
{$endif read_interface} |
| 86 |
|
| 87 |
{$ifdef read_implementation} |
| 88 |
|
| 89 |
{ TCustomizedFont ------------------------------------------------------------ } |
| 90 |
|
| 91 |
constructor TCustomizedFont.Create(AOwner: TComponent); |
| 92 |
begin |
| 93 |
inherited; |
| 94 |
end; |
| 95 |
|
| 96 |
destructor TCustomizedFont.Destroy; |
| 97 |
begin |
| 98 |
SourceFont := nil; // this will free FSourceFont if needed |
| 99 |
FreeAndNil(FAlternativeSizes); |
| 100 |
inherited; |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TCustomizedFont.SubFontFontSizeChange(Sender: TObject); |
| 104 |
begin |
| 105 |
FontSizesChanged; // invalidate our MakeMeasure calculations |
| 106 |
end; |
| 107 |
|
| 108 |
procedure TCustomizedFont.SetSourceFont(const Value: TCastleAbstractFont); |
| 109 |
begin |
| 110 |
if FSourceFont <> Value then |
| 111 |
begin |
| 112 |
if FSourceFont <> nil then |
| 113 |
begin |
| 114 |
FSourceFont.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange); |
| 115 |
FSourceFont.RemoveFreeNotification(Self); |
| 116 |
end; |
| 117 |
FSourceFont := Value; |
| 118 |
if FSourceFont <> nil then |
| 119 |
begin |
| 120 |
FSourceFont.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange); |
| 121 |
FSourceFont.FreeNotification(Self); |
| 122 |
end; |
| 123 |
|
| 124 |
{ Recalculate SubFont, since fonts changed. |
| 125 |
|
| 126 |
TODO: This will not fire when a font on FAlternativeSizes list is freed, |
| 127 |
SubFont may be left pointing to one of freed fonts. |
| 128 |
|
| 129 |
TODO: Also SubFontFontSizeChange is not fired when something on FAlternativeSizes |
| 130 |
changes size. } |
| 131 |
UpdateSubFont; |
| 132 |
end; |
| 133 |
end; |
| 134 |
|
| 135 |
procedure TCustomizedFont.Notification(AComponent: TComponent; Operation: TOperation); |
| 136 |
begin |
| 137 |
inherited; |
| 138 |
|
| 139 |
if (Operation = opRemove) and (AComponent = FSourceFont) then |
| 140 |
begin |
| 141 |
{ set to nil by SetSourceFont to clean nicely } |
| 142 |
SourceFont := nil; |
| 143 |
end; |
| 144 |
end; |
| 145 |
|
| 146 |
procedure TCustomizedFont.PrepareResources; |
| 147 |
var |
| 148 |
I: Integer; |
| 149 |
begin |
| 150 |
if FSourceFont <> nil then |
| 151 |
FSourceFont.PrepareResources; |
| 152 |
|
| 153 |
if FAlternativeSizes <> nil then |
| 154 |
begin |
| 155 |
for I := 0 to FAlternativeSizes.Count - 1 do |
| 156 |
TCastleAbstractFont(FAlternativeSizes[I]).PrepareResources; |
| 157 |
end; |
| 158 |
end; |
| 159 |
|
| 160 |
procedure TCustomizedFont.GLContextClose; |
| 161 |
var |
| 162 |
I: Integer; |
| 163 |
begin |
| 164 |
if FSourceFont <> nil then |
| 165 |
FSourceFont.GLContextClose; |
| 166 |
|
| 167 |
if FAlternativeSizes <> nil then |
| 168 |
begin |
| 169 |
for I := 0 to FAlternativeSizes.Count - 1 do |
| 170 |
TCastleAbstractFont(FAlternativeSizes[I]).GLContextClose; |
| 171 |
end; |
| 172 |
end; |
| 173 |
|
| 174 |
procedure TCustomizedFont.SetSize(const Value: Single); |
| 175 |
begin |
| 176 |
if Size <> Value then |
| 177 |
begin |
| 178 |
inherited SetSize(Value); |
| 179 |
UpdateSubFont; |
| 180 |
end; |
| 181 |
end; |
| 182 |
|
| 183 |
function TCustomizedFont.BestSourceFont(const ASize: Single): TCastleAbstractFont; |
| 184 |
var |
| 185 |
SizeDist, NewSizeDist: Single; |
| 186 |
AltFont: TCastleAbstractFont; |
| 187 |
I: Integer; |
| 188 |
begin |
| 189 |
Result := SourceFont; |
| 190 |
|
| 191 |
if (FAlternativeSizes <> nil) and (ASize <> 0) then |
| 192 |
begin |
| 193 |
{ At destruction it can happen that SourceFont is nil, |
| 194 |
while FAlternativeSizes is non-nil. } |
| 195 |
if SourceFont <> nil then |
| 196 |
SizeDist := Abs(SourceFont.Size - ASize) |
| 197 |
else |
| 198 |
SizeDist := MaxSingle; |
| 199 |
|
| 200 |
for I := 0 to FAlternativeSizes.Count - 1 do |
| 201 |
begin |
| 202 |
AltFont := TCastleAbstractFont(FAlternativeSizes[I]); |
| 203 |
NewSizeDist := Abs(AltFont.Size - ASize); |
| 204 |
if NewSizeDist < SizeDist then |
| 205 |
begin |
| 206 |
Result := AltFont; |
| 207 |
SizeDist := NewSizeDist; |
| 208 |
end; |
| 209 |
end; |
| 210 |
end; |
| 211 |
|
| 212 |
// Writeln('for size ', Size:1:2, ' using font with size ', Result.Size:1:2); |
| 213 |
end; |
| 214 |
|
| 215 |
procedure TCustomizedFont.UpdateSubFont; |
| 216 |
begin |
| 217 |
SubFont := BestSourceFont(Size); |
| 218 |
end; |
| 219 |
|
| 220 |
procedure TCustomizedFont.SubFontCustomizeBegin; |
| 221 |
begin |
| 222 |
Assert(SubFont <> nil, 'TCustomizedFont.SubFontCustomizeBegin should only be called when SubFont <> nil'); |
| 223 |
if Size <> 0 then |
| 224 |
begin |
| 225 |
SubFont.PushProperties; |
| 226 |
SubFont.Size := Size; |
| 227 |
end; |
| 228 |
end; |
| 229 |
|
| 230 |
procedure TCustomizedFont.SubFontCustomizeEnd; |
| 231 |
begin |
| 232 |
Assert(SubFont <> nil, 'TCustomizedFont.SubFontCustomizeEnd should only be called when SubFont <> nil'); |
| 233 |
if Size <> 0 then |
| 234 |
SubFont.PopProperties; |
| 235 |
end; |
| 236 |
|
| 237 |
procedure TCustomizedFont.Print(const X, Y: Single; const Color: TCastleColor; |
| 238 |
const S: string); |
| 239 |
begin |
| 240 |
if SubFont = nil then |
| 241 |
Exit; // subfonts not set yet |
| 242 |
SubFontCustomizeBegin; |
| 243 |
SubFont.Print(X, Y, Color, S); |
| 244 |
SubFontCustomizeEnd; |
| 245 |
end; |
| 246 |
|
| 247 |
function TCustomizedFont.TextWidth(const S: string): Single; |
| 248 |
begin |
| 249 |
{ One may think that only for rendering (Print) we have to use SubFont, |
| 250 |
for sizing it's enough to use SourceFont (since all alternatives should |
| 251 |
be the same font, just scaled). |
| 252 |
|
| 253 |
In practice it's important to use best font (always SubFont), |
| 254 |
since the underlying fonts data have integer sizes, |
| 255 |
so taking sizes of a different font always introduces some imprecision, |
| 256 |
this is visible by slightly misaligned labels etc. } |
| 257 |
|
| 258 |
if SubFont = nil then |
| 259 |
Exit(0); // subfonts not set yet |
| 260 |
SubFontCustomizeBegin; |
| 261 |
Result := SubFont.TextWidth(S); |
| 262 |
SubFontCustomizeEnd; |
| 263 |
end; |
| 264 |
|
| 265 |
function TCustomizedFont.TextHeight(const S: string): Single; |
| 266 |
begin |
| 267 |
if SubFont = nil then |
| 268 |
Exit(0); // subfonts not set yet |
| 269 |
SubFontCustomizeBegin; |
| 270 |
Result := SubFont.TextHeight(S); |
| 271 |
SubFontCustomizeEnd; |
| 272 |
end; |
| 273 |
|
| 274 |
function TCustomizedFont.TextHeightBase(const S: string): Single; |
| 275 |
begin |
| 276 |
if SubFont = nil then |
| 277 |
Exit(0); // subfonts not set yet |
| 278 |
SubFontCustomizeBegin; |
| 279 |
Result := SubFont.TextHeightBase(S); |
| 280 |
SubFontCustomizeEnd; |
| 281 |
end; |
| 282 |
|
| 283 |
function TCustomizedFont.TextMove(const S: string): TVector2; |
| 284 |
begin |
| 285 |
if SubFont = nil then |
| 286 |
Exit(TVector2.Zero); // subfonts not set yet |
| 287 |
SubFontCustomizeBegin; |
| 288 |
Result := SubFont.TextMove(S); |
| 289 |
SubFontCustomizeEnd; |
| 290 |
end; |
| 291 |
|
| 292 |
function TCustomizedFont.EffectiveSize: Single; |
| 293 |
begin |
| 294 |
if Size <> 0 then |
| 295 |
Result := Size |
| 296 |
else |
| 297 |
begin |
| 298 |
if SubFont = nil then |
| 299 |
Exit(0); // subfonts not set yet |
| 300 |
Result := SourceFont.EffectiveSize; |
| 301 |
end; |
| 302 |
end; |
| 303 |
|
| 304 |
procedure TCustomizedFont.Measure(out AHeight, ACapHeight, ADescenderHeight: Single); |
| 305 |
begin |
| 306 |
{ In usual circumstances, overriding Measure in TCustomizedFont is not needed. |
| 307 |
The default implementation of ancestor would work OK, |
| 308 |
by calling TextHeight to measure "Wy" for Height, |
| 309 |
and TCustomizedFont.TextHeight refers to FSourceFont.TextHeight already. |
| 310 |
|
| 311 |
But if the FSourceFont has some custom override for Measure, |
| 312 |
like TLargeDigitsFont.Measure in testcastlefonts.pas |
| 313 |
that returns constant instead of measuring "Wy" height for Height |
| 314 |
(because TLargeDigitsFont doesn't have "Wy" letters) |
| 315 |
then it would be ignored. |
| 316 |
This would cause problems when wrapping a font in TCustomizedFont |
| 317 |
(which is what e.g. TCastleLabel does when it has some size), |
| 318 |
wrapping TLargeDigitsFont in TCustomizedFont would ignore the |
| 319 |
TLargeDigitsFont.Measure override. |
| 320 |
|
| 321 |
So instead we implement our Measure by calling FSourceFont.Measure. } |
| 322 |
|
| 323 |
if SubFont = nil then |
| 324 |
begin |
| 325 |
AHeight := 0; |
| 326 |
ACapHeight := 0; |
| 327 |
ADescenderHeight := 0; |
| 328 |
Exit; // subfonts not set yet |
| 329 |
end; |
| 330 |
|
| 331 |
SubFontCustomizeBegin; |
| 332 |
SubFont.Measure(AHeight, ACapHeight, ADescenderHeight); |
| 333 |
SubFontCustomizeEnd; |
| 334 |
end; |
| 335 |
|
| 336 |
procedure TCustomizedFont.AddAlternativeSourceFont(const ASourceFont: TCastleAbstractFont); |
| 337 |
begin |
| 338 |
if FAlternativeSizes = nil then |
| 339 |
FAlternativeSizes := TComponentList.Create(false); |
| 340 |
FAlternativeSizes.Add(ASourceFont); |
| 341 |
UpdateSubFont; |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TCustomizedFont.Load(const Url: String; |
| 345 |
const ASizes: array of Integer; const AnAntiAliased: boolean; |
| 346 |
const ACharacters: TUnicodeCharList); |
| 347 |
var |
| 348 |
F: TCastleFont; |
| 349 |
I: Integer; |
| 350 |
begin |
| 351 |
Assert(Length(ASizes) > 0); |
| 352 |
|
| 353 |
// clear previous |
| 354 |
SourceFont := nil; |
| 355 |
if FAlternativeSizes <> nil then |
| 356 |
FAlternativeSizes.Clear; |
| 357 |
|
| 358 |
for I := 0 to Length(ASizes) - 1 do |
| 359 |
begin |
| 360 |
F := TCastleFont.Create(Self); |
| 361 |
{$warnings off} |
| 362 |
{ Using deprecated, as we have to better alternative to pass ACharacters: TUnicodeCharList. |
| 363 |
In the future TCustomizedFont may be deprecated, we need to have alternative sizes |
| 364 |
at TCastleFont probably. } |
| 365 |
F.Load(Url, ASizes[I], AnAntiAliased, ACharacters); |
| 366 |
{$warnings on} |
| 367 |
if SourceFont = nil then |
| 368 |
SourceFont := F |
| 369 |
else |
| 370 |
AddAlternativeSourceFont(F); |
| 371 |
end; |
| 372 |
end; |
| 373 |
|
| 374 |
function TCustomizedFont.FontLoaded: Boolean; |
| 375 |
begin |
| 376 |
Result := (SourceFont <> nil) and SourceFont.FontLoaded; |
| 377 |
end; |
| 378 |
|
| 379 |
{$endif read_implementation} |