Debian Codegraph @

src/fonts/castlefonts_fontfamily.inc

raw
1 {%MainUnit castlefonts.pas}
2 {
3 Copyright 2016-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 family, with possible different subfonts for
21 @link(Regular), @link(Bold), @link(Italic), @link(BoldItalic) variants.
22
23 It can be generally used everywhere where we allow TCastleAbstractFont,
24 and in most cases it will just act like the @link(Regular) variant.
25 But it is especialy useful together with @link(TCastleLabel.Html) = @true.
26 In such case, using it as @link(TCastleUserInterfaceFont.CustomFont TCastleLabel.CustomFont)
27 will allow to render HTML text with varying font styles, this way bold/italic requested
28 by HTML like @code(<b>something bold</b> <i>something italic</i>) will be visible.
29
30 Similar to TCustomizedFont, it can also change the subfont size.
31 Simply set the @code(Size) property of this instance to non-zero
32 to force the specific size of all the underlying subfonts.
33 You can also change the subfont outline, if CustomizeOutline is used.
34 The underlying font properties remain unchanged for subfonts
35 (so they can be still used for other purposes,
36 directly or by other TCustomizedFont or TCastleFontFamily wrappers). }
37 TCastleFontFamily = class(TCastleAbstractFont)
38 strict private
39 FRegular, FBold, FItalic, FBoldItalic: TCastleAbstractFont;
40 FInternalBold, FInternalItalic: boolean;
41 FCustomizeOutline: boolean;
42 procedure SetRegular(const Value: TCastleAbstractFont);
43 procedure SetBold(const Value: TCastleAbstractFont);
44 procedure SetItalic(const Value: TCastleAbstractFont);
45 procedure SetBoldItalic(const Value: TCastleAbstractFont);
46 procedure SubFontCustomizeBegin(const SF: TCastleAbstractFont);
47 procedure SubFontCustomizeEnd(const SF: TCastleAbstractFont);
48 procedure SubFontFontSizeChange(Sender: TObject);
49 private
50 { Font for given ABold/AItalic combination.
51 Does what it can to fallback on Regular font
52 (e.g. when you request ABold = AItalic = @true and BoldItalic is @nil)
53 but returns @nil if even Regular font is @nil. }
54 function SubFont(const ABold, AItalic: boolean): TCastleAbstractFont; overload;
55 function SubFont: TCastleAbstractFont; overload;
56 {$ifdef FPC}strict{$endif} protected
57 procedure GLContextClose; override;
58 procedure Measure(out AHeight, ACapHeight, ADescenderHeight: Single); override;
59 protected
60 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
61 public
62 constructor Create(AOwner: TComponent); override;
63 destructor Destroy; override;
64 function PropertySections(const PropertyName: String): TPropertySections; override;
65 function FontLoaded: Boolean; override;
66
67 property InternalBold: boolean read FInternalBold write FInternalBold default false;
68 property InternalItalic: boolean read FInternalItalic write FInternalItalic default false;
69
70 procedure PrepareResources; override;
71 procedure Print(const X, Y: Single; const Color: TCastleColor;
72 const S: string); override;
73 function TextWidth(const S: string): Single; override;
74 function TextHeight(const S: string): Single; override;
75 function TextHeightBase(const S: string): Single; override;
76 function TextMove(const S: string): TVector2; override;
77 function EffectiveSize: Single; override;
78
79 { Should we customize the outline of the underlying font. }
80 property CustomizeOutline: boolean read FCustomizeOutline write FCustomizeOutline default false;
81
82 {$ifdef FPC}
83 property RegularFont: TCastleAbstractFont read FRegular write SetRegular; deprecated 'use Regular';
84 property BoldFont: TCastleAbstractFont read FBold write SetBold; deprecated 'use Bold';
85 property ItalicFont: TCastleAbstractFont read FItalic write SetItalic; deprecated 'use Italic';
86 property BoldItalicFont: TCastleAbstractFont read FBoldItalic write SetBoldItalic; deprecated 'use BoldItalic';
87 {$endif FPC}
88 published
89 property Regular: TCastleAbstractFont read FRegular write SetRegular;
90 property Bold: TCastleAbstractFont read FBold write SetBold;
91 property Italic: TCastleAbstractFont read FItalic write SetItalic;
92 property BoldItalic: TCastleAbstractFont read FBoldItalic write SetBoldItalic;
93 end;
94
95 {$endif read_interface}
96
97 {$ifdef read_implementation}
98
99 { TCastleFontFamily ------------------------------------------------------------ }
100
101 constructor TCastleFontFamily.Create(AOwner: TComponent);
102 begin
103 inherited;
104 end;
105
106 destructor TCastleFontFamily.Destroy;
107 begin
108 // detach free notifications
109 Regular := nil;
110 Bold := nil;
111 Italic := nil;
112 BoldItalic := nil;
113 inherited;
114 end;
115
116 function TCastleFontFamily.PropertySections(const PropertyName: String): TPropertySections;
117 begin
118 if (PropertyName = 'Regular') or
119 (PropertyName = 'Bold') or
120 (PropertyName = 'Italic') or
121 (PropertyName = 'BoldItalic') then
122 Result := [psBasic]
123 else
124 Result := inherited PropertySections(PropertyName);
125 end;
126
127 procedure TCastleFontFamily.SetRegular(const Value: TCastleAbstractFont);
128 begin
129 if FRegular <> Value then
130 begin
131 if FRegular <> nil then
132 begin
133 FRegular.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
134 FRegular.RemoveFreeNotification(Self);
135 end;
136 FRegular := Value;
137 if FRegular <> nil then
138 begin
139 FRegular.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
140 FRegular.FreeNotification(Self);
141 end;
142 end;
143 end;
144
145 procedure TCastleFontFamily.SetBold(const Value: TCastleAbstractFont);
146 begin
147 if FBold <> Value then
148 begin
149 if FBold <> nil then
150 begin
151 FBold.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
152 FBold.RemoveFreeNotification(Self);
153 end;
154 FBold := Value;
155 if FBold <> nil then
156 begin
157 FBold.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
158 FBold.FreeNotification(Self);
159 end;
160 end;
161 end;
162
163 procedure TCastleFontFamily.SetItalic(const Value: TCastleAbstractFont);
164 begin
165 if FItalic <> Value then
166 begin
167 if FItalic <> nil then
168 begin
169 FItalic.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
170 FItalic.RemoveFreeNotification(Self);
171 end;
172 FItalic := Value;
173 if FItalic <> nil then
174 begin
175 FItalic.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
176 FItalic.FreeNotification(Self);
177 end;
178 end;
179 end;
180
181 procedure TCastleFontFamily.SetBoldItalic(const Value: TCastleAbstractFont);
182 begin
183 if FBoldItalic <> Value then
184 begin
185 if FBoldItalic <> nil then
186 begin
187 FBoldItalic.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
188 FBoldItalic.RemoveFreeNotification(Self);
189 end;
190 FBoldItalic := Value;
191 if FBoldItalic <> nil then
192 begin
193 FBoldItalic.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}SubFontFontSizeChange);
194 FBoldItalic.FreeNotification(Self);
195 end;
196 end;
197 end;
198
199 procedure TCastleFontFamily.Notification(AComponent: TComponent; Operation: TOperation);
200 begin
201 inherited;
202 { set to nil by setter to detach notifications }
203 if (Operation = opRemove) and (AComponent = FRegular) then
204 Regular := nil;
205 if (Operation = opRemove) and (AComponent = FBold) then
206 Bold := nil;
207 if (Operation = opRemove) and (AComponent = FItalic) then
208 Italic := nil;
209 if (Operation = opRemove) and (AComponent = FBoldItalic) then
210 BoldItalic := nil;
211 end;
212
213 procedure TCastleFontFamily.PrepareResources;
214 begin
215 if FRegular <> nil then
216 FRegular.PrepareResources;
217 if FBold <> nil then
218 FBold.PrepareResources;
219 if FItalic <> nil then
220 FItalic.PrepareResources;
221 if FBoldItalic <> nil then
222 FBoldItalic.PrepareResources;
223 end;
224
225 procedure TCastleFontFamily.GLContextClose;
226 begin
227 if FRegular <> nil then
228 FRegular.GLContextClose;
229 if FBold <> nil then
230 FBold.GLContextClose;
231 if FItalic <> nil then
232 FItalic.GLContextClose;
233 if FBoldItalic <> nil then
234 FBoldItalic.GLContextClose;
235 end;
236
237 procedure TCastleFontFamily.SubFontCustomizeBegin(const SF: TCastleAbstractFont);
238 begin
239 Assert(SF <> nil, 'TCastleFontFamily.SubFontCustomizeBegin should only be called with SF (from SubFont) <> nil');
240 if (Size <> 0) or CustomizeOutline then
241 begin
242 SF.PushProperties;
243 if Size <> 0 then
244 SF.Size := Size;
245 if CustomizeOutline then
246 begin
247 SF.Outline := Outline;
248 SF.OutlineColor := OutlineColor;
249 SF.OutlineHighQuality := OutlineHighQuality;
250 end;
251 end;
252 end;
253
254 procedure TCastleFontFamily.SubFontCustomizeEnd(const SF: TCastleAbstractFont);
255 begin
256 Assert(SF <> nil, 'TCastleFontFamily.SubFontCustomizeEnd should only be called with SF (from SubFont) <> nil');
257 if Size <> 0 then
258 SF.PopProperties;
259 end;
260
261 procedure TCastleFontFamily.Print(const X, Y: Single; const Color: TCastleColor;
262 const S: string);
263 var
264 SF: TCastleAbstractFont;
265 begin
266 SF := SubFont;
267 if SF = nil then
268 Exit; // subfonts not set yet
269 SubFontCustomizeBegin(SF);
270 SF.Print(X, Y, Color, S);
271 SubFontCustomizeEnd(SF);
272 end;
273
274 function TCastleFontFamily.TextWidth(const S: string): Single;
275 var
276 SF: TCastleAbstractFont;
277 begin
278 SF := SubFont;
279 if SF = nil then
280 Exit(0); // subfonts not set yet
281 SubFontCustomizeBegin(SF);
282 Result := SF.TextWidth(S);
283 SubFontCustomizeEnd(SF);
284 end;
285
286 function TCastleFontFamily.TextHeight(const S: string): Single;
287 var
288 SF: TCastleAbstractFont;
289 begin
290 SF := SubFont;
291 if SF = nil then
292 Exit(0); // subfonts not set yet
293 SubFontCustomizeBegin(SF);
294 Result := SF.TextHeight(S);
295 SubFontCustomizeEnd(SF);
296 end;
297
298 function TCastleFontFamily.TextHeightBase(const S: string): Single;
299 var
300 SF: TCastleAbstractFont;
301 begin
302 SF := SubFont;
303 if SF = nil then
304 Exit(0); // subfonts not set yet
305 SubFontCustomizeBegin(SF);
306 Result := SF.TextHeightBase(S);
307 SubFontCustomizeEnd(SF);
308 end;
309
310 function TCastleFontFamily.TextMove(const S: string): TVector2;
311 var
312 SF: TCastleAbstractFont;
313 begin
314 SF := SubFont;
315 if SF = nil then
316 Exit(TVector2.Zero); // subfonts not set yet
317 SubFontCustomizeBegin(SF);
318 Result := SF.TextMove(S);
319 SubFontCustomizeEnd(SF);
320 end;
321
322 function TCastleFontFamily.SubFont(const ABold, AItalic: boolean): TCastleAbstractFont;
323 begin
324 if ABold and AItalic and (BoldItalic <> nil) then
325 Result := BoldItalic
326 else
327 if ABold and (Bold <> nil) then
328 Result := Bold
329 else
330 if AItalic and (Italic <> nil) then
331 Result := Italic
332 else
333 if Regular <> nil then
334 Result := Regular
335 else
336 Result := nil;
337 // We have to gracefully accept unset font, it is a possible case esp. in editor but also at runtime
338 //raise Exception.Create('You must set at least Regular of TCastleFontFamily to use it for processing and rendering');
339 end;
340
341 function TCastleFontFamily.SubFont: TCastleAbstractFont;
342 begin
343 Result := SubFont(InternalBold, InternalItalic);
344 end;
345
346 function TCastleFontFamily.EffectiveSize: Single;
347 var
348 SF: TCastleAbstractFont;
349 begin
350 if Size <> 0 then
351 Result := Size
352 else
353 begin
354 SF := SubFont;
355 if SF = nil then
356 Exit(0); // subfonts not set yet
357 Result := SF.EffectiveSize;
358 end;
359 end;
360
361 procedure TCastleFontFamily.Measure(out AHeight, ACapHeight, ADescenderHeight: Single);
362 var
363 SF: TCastleAbstractFont;
364 begin
365 SF := SubFont;
366 if SF = nil then
367 begin
368 AHeight := 0;
369 ACapHeight := 0;
370 ADescenderHeight := 0;
371 Exit; // subfonts not set yet
372 end;
373
374 { See TCustomizedFont.Measure for explanation why we need to override
375 Measure. }
376 SubFontCustomizeBegin(SF);
377 SF.Measure(AHeight, ACapHeight, ADescenderHeight);
378 SubFontCustomizeEnd(SF);
379 end;
380
381 procedure TCastleFontFamily.SubFontFontSizeChange(Sender: TObject);
382 begin
383 FontSizesChanged; // invalidate our MakeMeasure calculations
384 end;
385
386 function TCastleFontFamily.FontLoaded: Boolean;
387 begin
388 Result := (Regular <> nil) and Regular.FontLoaded;
389 end;
390
391 {$endif read_implementation}