Debian Codegraph @

src/ui/castlecontrols_uifont.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2010-2021 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 { Helper routine to get default font to draw UI.
20 @exclude }
21 function InternalGetUIFont: TCastleAbstractFont;
22
23 { Helper routine to set default font to draw UI.
24 @exclude }
25 procedure InternalSetUIFont(const Value: TCastleAbstractFont);
26
27 {$ifdef FPC}
28 (*The default font, used throughout the engine as a fallback.
29 By default, this is a modern sans-serif font hardcoded into the engine.
30
31 Better use FallbackFont now, or use your own font, or render text
32 using TCastleLabel.
33
34 You can assign your own font here, to make this the new default font.
35
36 Although in most cases it is better to leave @link(UIFont) unchanged
37 (treat it like "engine default font"), and instead set your default font by
38 setting @link(TCastleContainer.DefaultFont Window.Container.DefaultFont).
39 You can do it by code, or by editing CastleSettings.xml (this way editor
40 will be aware of this new default), see https://castle-engine.io/manual_castle_settings.php .
41
42 Note that assigning font here @italic(does not) make it automatically freed.
43 Use the ownership mechanism to make sure it is freed,
44 i.e. assign some owner to your font instance, like this:
45
46 @longCode(#
47 MyFont := TCastleFont.Create({ owner } Application);
48 MyFont.Load(TextureFont_Xxxx);
49 UIFont := MyFont;
50 #)
51 *)
52 property UIFont: TCastleAbstractFont
53 read InternalGetUIFont write InternalSetUIFont;
54 {$endif}
55 { @groupEnd }
56
57 { The default font, used throughout the engine as a fallback.
58 This is a modern sans-serif font hardcoded into the engine. }
59 function FallbackFont: TCastleFont;
60
61 { Force using FallbackFont (thus, ignore @link(TCastleContainer.DefaultFont))
62 and FallbackTheme (thus, disable any @link(TCastleTheme Theme) customizations) in this UI.
63 This guarantees the same UI look, ignoring user's UI font and theme customizations. }
64 procedure ForceFallbackLook(const Ui: TCastleUserInterface);
65
66 {$endif read_interface}
67
68 {$ifdef read_implementation}
69
70 { UIFont --------------------------------------------------------------------- }
71
72 type
73 { Component used as owner for fonts created on-demand in UIFont . }
74 TDefaultUIFont = class(TComponent)
75 public
76 Normal: TCastleFont;
77 end;
78
79 var
80 DefaultUIFont: TDefaultUIFont;
81 FUIFont: TCastleAbstractFont;
82
83 function FallbackFont: TCastleFont;
84 var
85 TimeStart: TCastleProfilerTime;
86 begin
87 if DefaultUIFont = nil then
88 DefaultUIFont := TDefaultUIFont.Create(nil);
89 if DefaultUIFont.Normal = nil then
90 begin
91 DefaultUIFont.Normal := TCastleFont.Create(DefaultUIFont);
92 TimeStart := Profiler.Start('Loading Font_DefaultUi');
93 DefaultUIFont.Normal.Load(Font_DefaultUi);
94 Profiler.Stop(TimeStart);
95 end;
96 Result := DefaultUIFont.Normal;
97 end;
98
99 function InternalGetUIFont: TCastleAbstractFont;
100 begin
101 if FUIFont = nil then
102 FUIFont := FallbackFont;
103 Result := FUIFont;
104 end;
105
106 procedure InternalSetUIFont(const Value: TCastleAbstractFont);
107 begin
108 FUIFont := Value;
109 end;
110
111 procedure FinalizationUIFonts;
112 begin
113 if DefaultUIFont <> nil then
114 begin
115 if (FUIFont <> nil) and
116 (FUIFont = DefaultUIFont.Normal) then
117 FUIFont := nil;
118 FreeAndNil(DefaultUIFont);
119 end;
120 end;
121
122 procedure ForceFallbackLook(const Ui: TCastleUserInterface);
123 var
124 Child: TCastleUserInterface;
125 begin
126 Ui.CustomTheme := FallbackTheme;
127 if Ui is TCastleUserInterfaceFont then
128 TCastleUserInterfaceFont(Ui).CustomFont := FallbackFont;
129 for Child in Ui do
130 ForceFallbackLook(Child);
131 end;
132
133 {$endif read_implementation}