Debian Codegraph @

src/ui/castlecontrols_userinterfacefont.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2010-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 TCastleImageControl = class;
20
21 { Base class for all user interface controls using a font.
22 Allows to customize font and font size per-control, or use defaults. }
23 TCastleUserInterfaceFont = class(TCastleUserInterface)
24 strict private
25 FTooltip: string;
26 TooltipLabelContainer: TCastleImageControl;
27 TooltipLabel: TCastleLabel;
28 FCustomFont: TCastleAbstractFont;
29 FOwnsCustomFont: boolean;
30 FLastSeenUIFont, FLastSeenDefaultFont: TCastleAbstractFont; //< remembered only to call FontChanged
31 FFontSize, FFontScale: Single;
32 FCustomizedFont: TCastleFontFamily; //< used only when some properties make it necessary
33 FSmallFont: boolean;
34 FOutline: Cardinal;
35 FOutlineColor: TCastleColor;
36 FOutlineHighQuality: boolean;
37 procedure SetCustomFont(const Value: TCastleAbstractFont);
38 procedure SetFontSize(const Value: Single);
39 procedure SetFontScale(const Value: Single);
40 procedure SetSmallFont(const Value: boolean);
41 procedure SetOutline(const Value: Cardinal);
42 procedure SetOutlineColor(const Value: TCastleColor);
43 procedure SetOutlineHighQuality(const Value: boolean);
44 procedure CustomFontSizeChanged(Sender: TObject);
45 protected
46 procedure InternalSetTooltipExists(const Value: Boolean); override;
47 procedure UIScaleChanged; override;
48 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
49 public
50 constructor Create(AOwner: TComponent); override;
51 destructor Destroy; override;
52 function TooltipExists: boolean; override;
53 procedure TooltipRender(const TooltipPosition: TVector2); override;
54 procedure Render; override;
55 function PropertySections(const PropertyName: String): TPropertySections; override;
56
57 { Copy font properties from another control. }
58 procedure AssignFont(const Source: TCastleUserInterfaceFont);
59
60 { Font used for rendering and measuring this control.
61
62 Depending on which one is set, this method returns
63 @link(CustomFont) or @link(TCastleContainer.DefaultFont) or global @link(UIFont).
64 It may also return a TCastleFontFamily instance that wraps (and scales)
65 one of them. }
66 function Font: TCastleAbstractFont;
67
68 { Check does currently used font (see @link(Font)) changed,
69 and eventually call FontChanged method @italic(now).
70
71 You only need to explicitly call this in very specific circumstances,
72 when you just changed @link(UIFont) or @link(TCastleContainer.DefaultFont)
73 and you want control size to be updated immediately
74 (for example, you need @link(EffectiveHeight)
75 to be immediately valid). Without calling this,
76 it could be updated only at next Render call.
77
78 Changing @link(CustomFont) automatically calls this. }
79 procedure CheckFontChanged;
80
81 { Force considering font changed (font instance, or glyphs, or sizes changed).
82
83 @italic(Usually, you don't need to call this explicitly from the outside.)
84 Changing font, like @link(CustomFont)
85 or @link(TCastleContainer.DefaultFont) or @link(UIFont),
86 or any property that may affect the font is automatically applied.
87
88 But there are exceptions: right now, if you call @link(TCastleFont.Load Load)
89 on a font, it's measurements will change but some controls using it will
90 not recalculate sizes automatically. In this case, call this method. }
91 procedure FontChanged; virtual;
92
93 {$ifdef FPC}
94 property OwnsCustomFont: boolean
95 read FOwnsCustomFont write FOwnsCustomFont default false;
96 deprecated 'use TCastleAbstractFont (inherited from TComponent) owner mechanism';
97
98 { Use smaller font.
99 Right now this just says to scale font by 0.5. }
100 property SmallFont: boolean read FSmallFont write SetSmallFont default false;
101 deprecated 'use "FontScale := 0.5" or "CustomFont := MySmalFont" instead of "SmallFont := true", this is more flexible';
102 {$endif}
103
104 { Outline color, used only if Outline <> 0. Default is black.
105 @seealso Outline }
106 property OutlineColor: TCastleColor read FOutlineColor write SetOutlineColor;
107 published
108 { Tooltip string, displayed when user hovers the mouse over a control.
109
110 Note that you can override TCastleUserInterface.TooltipExists and
111 TCastleUserInterface.TooltipRender to customize the tooltip drawing. }
112 property Tooltip: string read FTooltip write FTooltip;
113
114 { Font used to render text of this control.
115 When this is @nil then the @link(TCastleContainer.DefaultFont) is used,
116 or (when @link(TCastleContainer.DefaultFont) is also @nil) the global @link(UIFont). }
117 property CustomFont: TCastleAbstractFont
118 read FCustomFont write SetCustomFont;
119
120 { Use given font size when drawing. Leave at default zero to use
121 the default size of the current font. }
122 property FontSize: Single read FFontSize write SetFontSize {$ifdef FPC}default 0.0{$endif};
123
124 { Scale the font. This additionally scales the font defined
125 by all other properties of this class, thus you can use it to reliably
126 scale the font (regardless if you use @link(CustomFont) or not,
127 regardless if you set @link(FontSize) or not). }
128 property FontScale: Single read FFontScale write SetFontScale {$ifdef FPC}default 1.0{$endif};
129
130 { Outline size around the font.
131 Note that the current implementation is very simple, it will only
132 look sensible for small outline values (like 1 or 2).
133
134 See TCastleAbstractFont.Outline for more details.
135
136 Specifying a non-zero outline for a label overrides the default outline
137 settings for the current font. By default, fonts do not have an outline.
138
139 TODO: UI scaling (@link(TCastleContainer.UIScaling))
140 doesn't affect outline size now.
141
142 @seealso OutlineHighQuality }
143 property Outline: Cardinal read FOutline write SetOutline default 0;
144
145 { Optionally force better outline quality. Used only if Outline <> 0.
146 High quality outline looks better, but is about 2x more expensive to draw.
147 @seealso Outline }
148 property OutlineHighQuality: boolean
149 read FOutlineHighQuality write SetOutlineHighQuality default false;
150
151 {$define read_interface_class}
152 {$I auto_generated_persistent_vectors/tcastleuserinterfacefont_persistent_vectors.inc}
153 {$undef read_interface_class}
154 end;
155
156 TUIControlFont = TCastleUserInterfaceFont deprecated 'use TCastleUserInterfaceFont';
157
158 {$endif read_interface}
159
160 {$ifdef read_implementation}
161
162 type
163 TCastleLabelTooltip = class(TCastleLabel)
164 public
165 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
166 end;
167
168 procedure TCastleLabelTooltip.PreferredSize(
169 var PreferredWidth, PreferredHeight: Single);
170 begin
171 inherited;
172 end;
173
174 { TCastleUserInterfaceFont ---------------------------------------------------------- }
175
176 constructor TCastleUserInterfaceFont.Create(AOwner: TComponent);
177 begin
178 inherited;
179 FFontScale := 1.0;
180 FOutlineColor := Black;
181
182 {$define read_implementation_constructor}
183 {$I auto_generated_persistent_vectors/tcastleuserinterfacefont_persistent_vectors.inc}
184 {$undef read_implementation_constructor}
185 end;
186
187 destructor TCastleUserInterfaceFont.Destroy;
188 begin
189 CustomFont := nil; // make sure to free FCustomFont, if necessary
190 FreeAndNil(FCustomizedFont);
191
192 {$define read_implementation_destructor}
193 {$I auto_generated_persistent_vectors/tcastleuserinterfacefont_persistent_vectors.inc}
194 {$undef read_implementation_destructor}
195 inherited;
196 end;
197
198 function TCastleUserInterfaceFont.TooltipExists: boolean;
199 begin
200 Result := (TooltipControl <> nil) or (Tooltip <> '');
201 end;
202
203 procedure TCastleUserInterfaceFont.TooltipRender(const TooltipPosition: TVector2);
204 const
205 TooltipPadding = 5;
206 var
207 TooltipWidth, TooltipHeight: Single;
208 Pos: TVector2;
209 begin
210 // do not render tooltip here, if TooltipControl takes care of it
211 if TooltipControl <> nil then
212 Exit;
213 if Container = nil then
214 Exit; // we need Container to work
215
216 if TooltipLabelContainer = nil then
217 begin
218 TooltipLabelContainer := TCastleImageControl.Create(Self);
219 TooltipLabelContainer.Content.DrawableImage := Theme.ImagesPersistent[tiTooltip].DrawableImage;
220 TooltipLabelContainer.Content.OwnsDrawableImage := false;
221 TooltipLabelContainer.Content.ProtectedSides.Assign(Theme.ImagesPersistent[tiTooltip].ProtectedSides);
222 TooltipLabelContainer.AutoSizeToChildren := true;
223 TooltipLabelContainer.AutoSizeToChildrenPaddingRight := TooltipPadding;
224 TooltipLabelContainer.AutoSizeToChildrenPaddingTop := TooltipPadding;
225 TooltipLabelContainer.SetTransient; // in case this occurs at design-time
226 Container.Controls.InsertFront(TooltipLabelContainer);
227 end;
228
229 if TooltipLabel = nil then
230 begin
231 TooltipLabel := TCastleLabelTooltip.Create(Self);
232 TooltipLabel.Anchor(hpLeft, TooltipPadding);
233 TooltipLabel.Anchor(vpBottom, TooltipPadding);
234 TooltipLabelContainer.InsertFront(TooltipLabel);
235 end;
236
237 { assign TooltipLabel.Text first, to get TooltipLabelContainer.EffectiveXxx later }
238 TooltipLabel.Color := Theme.TooltipTextColor;
239 TooltipLabel.Text.Clear;
240 TooltipLabel.Text.Append(Tooltip);
241
242 TooltipWidth := TooltipLabelContainer.EffectiveWidth;
243 TooltipHeight := TooltipLabelContainer.EffectiveHeight;
244
245 // convert TooltipPosition from final coordinates to scaled coordinates
246 Pos := TooltipPosition / TooltipLabelContainer.UIScale;
247
248 { now try to fix X, Y to make tooltip fit inside a window }
249 {$warnings off} // using deprecated ContainerWidth/Height now
250 MinVar(Pos.X, ContainerWidth / TooltipLabelContainer.UIScale - TooltipWidth );
251 MinVar(Pos.Y, ContainerHeight / TooltipLabelContainer.UIScale - TooltipHeight);
252 {$warnings on}
253 MaxVar(Pos.X, 0);
254 MaxVar(Pos.Y, 0);
255
256 TooltipLabelContainer.Translation := Pos;
257 end;
258
259 procedure TCastleUserInterfaceFont.InternalSetTooltipExists(const Value: Boolean);
260 begin
261 inherited;
262 if TooltipLabelContainer <> nil then
263 TooltipLabelContainer.Exists := Value;
264 end;
265
266 function TCastleUserInterfaceFont.Font: TCastleAbstractFont;
267 begin
268 { We check everywhere FontLoaded, to not use some font if it is not loaded
269 (e.g. if freetype library not available, or font file failed to load),
270 as it would not be visible.
271 As a last fallback, we can always use UIFont. }
272 if (CustomFont <> nil) and
273 // TODO: hack for TCastleInspector, why needed?
274 (CustomFont.ClassType <> TCastleAbstractFont) and
275 (CustomFont.FontLoaded) then
276 Result := CustomFont
277 else
278 if (Container <> nil) and
279 (Container.DefaultFont <> nil) and
280 (Container.DefaultFont.FontLoaded) then
281 Result := Container.DefaultFont
282 else
283 { If the Container is not assigned yet, then assume that UI control
284 will be inserted to the GetMainContainer, and use GetMainContainer.DefaultFont
285 (which is typically adjusted using CastleSettings.xml).
286 This makes calling e.g. TCastleLabel.Font.Text before the TCastleLabel instance
287 is inserted into the Container sensible -- it will measure using the right font
288 (with right sizes, right characters).
289 Testcase: Escape warnings about invalid chars in Japanese version. }
290 if (Container = nil) and
291 (GetMainContainer <> nil) and
292 (GetMainContainer.DefaultFont <> nil) and
293 (GetMainContainer.DefaultFont.FontLoaded) then
294 Result := GetMainContainer.DefaultFont
295 else
296 Result := InternalGetUIFont;
297
298 { if needed, wrap Result in TCastleFontFamily instance }
299 if (FontSize <> 0) or
300 (FontScale <> 1) or
301 (UIScale <> 1) or
302 {$ifdef FPC}
303 {$warnings off} // using deprecated to keep it working
304 SmallFont or
305 {$warnings on}
306 {$endif}
307 (Outline <> 0) then
308 begin
309 if FCustomizedFont = nil then
310 FCustomizedFont := TCastleFontFamily.Create(nil);
311 if FontSize <> 0 then
312 FCustomizedFont.Size := FontSize
313 else
314 FCustomizedFont.Size := Result.EffectiveSize; // to have something to multiply in line below
315 FCustomizedFont.Size := FCustomizedFont.Size * UIScale * FontScale;
316 {$ifdef FPC}
317 {$warnings off} // using deprecated to keep it working
318 if SmallFont then
319 FCustomizedFont.Size := FCustomizedFont.Size * 0.5;
320 {$warnings on}
321 {$endif}
322 if Result is TCastleFontFamily then
323 begin
324 { copying Result to FCustomizedFont this way allows to render with HTML
325 when CustomFont and FontSize/FontScale are used together }
326 FCustomizedFont.Regular := TCastleFontFamily(Result).Regular;
327 FCustomizedFont.Bold := TCastleFontFamily(Result).Bold;
328 FCustomizedFont.Italic := TCastleFontFamily(Result).Italic;
329 FCustomizedFont.BoldItalic := TCastleFontFamily(Result).BoldItalic;
330 end else
331 begin
332 FCustomizedFont.Regular := Result;
333 FCustomizedFont.Bold := nil;
334 FCustomizedFont.Italic := nil;
335 FCustomizedFont.BoldItalic := nil;
336 end;
337 { When Outline = 0, set FCustomizedFont.CustomizeOutline = false,
338 this makes switching between Outline <> 0 to Outline = 0 make correct changes
339 -- outline should disappear. }
340 FCustomizedFont.CustomizeOutline := Outline <> 0;
341 if Outline <> 0 then
342 begin
343 FCustomizedFont.Outline := Outline;
344 FCustomizedFont.OutlineHighQuality := OutlineHighQuality;
345 FCustomizedFont.OutlineColor := OutlineColor;
346 end;
347 Result := FCustomizedFont;
348 end;
349 end;
350
351 procedure TCastleUserInterfaceFont.SetCustomFont(const Value: TCastleAbstractFont);
352 begin
353 if FCustomFont <> Value then
354 begin
355 if FCustomFont <> nil then
356 begin
357 FCustomFont.RemoveFreeNotification(Self);
358 FCustomFont.RemoveFontSizeChangeNotification({$ifdef FPC}@{$endif}CustomFontSizeChanged);
359 end;
360 {$ifdef FPC}
361 {$warnings off}
362 if OwnsCustomFont then
363 FreeAndNil(FCustomFont) else
364 FCustomFont := nil;
365 {$warnings on}
366 {$endif}
367 FCustomFont := Value;
368 if FCustomFont <> nil then
369 begin
370 FCustomFont.FreeNotification(Self);
371 { We need to be notified about font size changes,
372 in particular when new font file is loaded in TCastleFont.
373 Testcase: assing to TCastleLabel.CustomFont some TCastleFont,
374 then change the TCastleFont.URL (without changing anything
375 at TCastleLabel directly) -- the TCastleLabel size should change. }
376 FCustomFont.AddFontSizeChangeNotification({$ifdef FPC}@{$endif}CustomFontSizeChanged);
377 end;
378 { don't call virtual function, that may try to measure the text by accessing Font
379 (needlessly using UIFont, possibly even recreating UIFont if "finalization"
380 of this unit already run, possibly even accessing invalid freed
381 TextureFontData). }
382 if not (csDestroying in ComponentState) then
383 FontChanged;
384 end;
385 end;
386
387 procedure TCastleUserInterfaceFont.Notification(AComponent: TComponent; Operation: TOperation);
388 begin
389 inherited;
390
391 if (Operation = opRemove) and (AComponent = FCustomFont) then
392 begin
393 { set to nil by SetCustomFont to clean nicely }
394 { since it's already being freed, do not apply OwnsCustomFont effect
395 inside SetCustomFont. }
396 {$ifdef FPC}
397 {$warnings off}
398 OwnsCustomFont := false;
399 {$warnings on}
400 {$endif}
401 CustomFont := nil;
402 end;
403 end;
404
405 procedure TCastleUserInterfaceFont.SetFontSize(const Value: Single);
406 begin
407 if FFontSize <> Value then
408 begin
409 FFontSize := Value;
410 FontChanged;
411 end;
412 end;
413
414 procedure TCastleUserInterfaceFont.SetFontScale(const Value: Single);
415 begin
416 if FFontScale <> Value then
417 begin
418 FFontScale := Value;
419 FontChanged;
420 end;
421 end;
422
423 procedure TCastleUserInterfaceFont.SetSmallFont(const Value: boolean);
424 begin
425 if FSmallFont <> Value then
426 begin
427 FSmallFont := Value;
428 FontChanged;
429 end;
430 end;
431
432 procedure TCastleUserInterfaceFont.SetOutline(const Value: Cardinal);
433 begin
434 if FOutline <> Value then
435 begin
436 FOutline := Value;
437 FontChanged;
438 end;
439 end;
440
441 procedure TCastleUserInterfaceFont.SetOutlineColor(const Value: TCastleColor);
442 begin
443 if not TCastleColor.PerfectlyEquals(FOutlineColor, Value) then
444 begin
445 FOutlineColor := Value;
446 FontChanged;
447 end;
448 end;
449
450 procedure TCastleUserInterfaceFont.SetOutlineHighQuality(const Value: boolean);
451 begin
452 if FOutlineHighQuality <> Value then
453 begin
454 FOutlineHighQuality := Value;
455 FontChanged;
456 end;
457 end;
458
459 procedure TCastleUserInterfaceFont.FontChanged;
460 begin
461 { Changing font properties, like FontSize, may change the control size
462 (which is usually adjusted to font size,
463 e.g. TCastleLabel, TCastleButton with AutoSize). }
464 VisibleChange([chRectangle]);
465 end;
466
467 procedure TCastleUserInterfaceFont.UIScaleChanged;
468 begin
469 inherited;
470 FontChanged;
471 end;
472
473 procedure TCastleUserInterfaceFont.Render;
474 begin
475 inherited;
476 CheckFontChanged;
477 end;
478
479 procedure TCastleUserInterfaceFont.CheckFontChanged;
480 var
481 NewUIFont, NewDefaultFont: TCastleAbstractFont;
482 begin
483 { we do not use here UIFont but FUIFont, to not create font without need.
484 If the corresponding global font is nil now,
485 it can remain nil, no need to initialize it yet. }
486 NewUIFont := FUIFont;
487
488 if Container <> nil then
489 NewDefaultFont := Container.DefaultFont
490 else
491 NewDefaultFont := nil;
492
493 if (CustomFont = nil) and
494 ( (FLastSeenUIFont <> NewUIFont) or
495 (FLastSeenDefaultFont <> NewDefaultFont) ) then
496 FontChanged;
497
498 FLastSeenUIFont := NewUIFont;
499 FLastSeenDefaultFont := NewDefaultFont;
500 end;
501
502 procedure TCastleUserInterfaceFont.AssignFont(const Source: TCastleUserInterfaceFont);
503 begin
504 CustomFont := Source.CustomFont;
505 FontSize := Source.FontSize;
506 FontScale := Source.FontScale;
507 end;
508
509 function TCastleUserInterfaceFont.PropertySections(
510 const PropertyName: String): TPropertySections;
511 begin
512 if (PropertyName = 'FontSize') or
513 (PropertyName = 'CustomFont') then
514 Result := [psBasic]
515 else
516 Result := inherited PropertySections(PropertyName);
517 end;
518
519 procedure TCastleUserInterfaceFont.CustomFontSizeChanged(Sender: TObject);
520 begin
521 FontChanged;
522 end;
523
524 {$define read_implementation_methods}
525 {$I auto_generated_persistent_vectors/tcastleuserinterfacefont_persistent_vectors.inc}
526 {$undef read_implementation_methods}
527
528 {$endif read_implementation}