Debian Codegraph @

src/fonts/castlefonts_bitmapfont.inc

raw
1 {%MainUnit castlefonts.pas}
2 {
3 Copyright 2001-2024 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 { Bitmap font, where each character is just drawn (and may be multi-color)
21 on a raster image. See https://en.wikipedia.org/wiki/Computer_font about
22 "bitmap font" or (less common name, but more valid) "raster font".
23
24 By default this class makes some assumptions about how the font image looks like:
25 the characters are placed on the image in their Unicode order,
26 starting from space in the upper-left corner.
27 TODO: Use OnGlyph event to customize it.
28
29 Examples of such fonts:
30
31 @unorderedList(
32 @item(See examples/fonts/data/sonic_asalga_0.png in CGE examples.)
33 @item(From OpenGameArt: http://opengameart.org/content/sonic-font,
34 http://opengameart.org/content/null-terminator .)
35 @item(From itch.io: https://itch.io/game-assets/tag-bitmap-font .)
36 ) }
37 TCastleBitmapFont = class(TCastleAbstractFont)
38 strict private
39 FDrawableImage: TDrawableImage;
40 FImageUrl: String;
41 FImage: TCastleImage;
42 FImageColumns, FImageRows,FImageMargin: Cardinal;
43 FDisplayMargin: Single;
44 FGlyphsScreenRects, FGlyphsImageRects: TFloatRectangleList;
45 function ScaledCharWidth: Single;
46 function ScaledCharHeight: Single;
47 function ScaledDisplayMargin: Single;
48 function GetSmoothScaling: boolean;
49 { Scale applied to the rendered DrawableImage to honor changing the Size property. }
50 function Scale: Single;
51 { Free stuff loaded by @link(Load). }
52 procedure Unload;
53 procedure SetImageUrl(const Value: String);
54 procedure SetImageColumns(const Value: Cardinal);
55 procedure SetImageRows(const Value: Cardinal);
56 procedure SetImageMargin(const Value: Cardinal);
57 procedure SetDisplayMargin(const Value: Single);
58 function CharWidth: Cardinal;
59 function CharHeight: Cardinal;
60 strict protected
61 procedure SetSize(const Value: Single); override;
62 procedure GLContextClose; override;
63 public
64 constructor Create(AOwner: TComponent); override;
65 destructor Destroy; override;
66 function PropertySections(const PropertyName: String): TPropertySections; override;
67 { Load font from given image.
68 @param AImage Image data, becomes owned by this class.
69 @param AImageMargin There is a margin in the image between rows and cols.
70 @param(ADisplayMargin We can display some spacing between characters.
71 This is independent from ImageMargin and image contents.) }
72 procedure Load(const AImage: TCastleImage;
73 const AImageColumns, AImageRows, AImageMargin: Cardinal; const ADisplayMargin: Single);
74 deprecated 'assign properties to load font: ImageUrl, ImageColumns, ImageRows, ImageMargin and (only for display) DisplayMargin';
75 procedure PrepareResources; override;
76 procedure Print(const X, Y: Single; const Color: TCastleColor;
77 const S: string); override;
78 function TextWidth(const S: string): Single; override;
79 function TextHeight(const S: string): Single; override;
80 function TextHeightBase(const S: string): Single; override;
81 function TextMove(const S: string): TVector2; override;
82 function FontLoaded: Boolean; override;
83 published
84 { Image that contains the characters.
85 The characters are placed on the image in their Unicode order,
86 starting from space in the upper-left corner.
87
88 The characters are organized in ImageColumns columns and ImageRows rows,
89 and have ImageMargin space between them. }
90 property ImageUrl: String read FImageUrl write SetImageUrl;
91 { Columns of the characters in the image @link(ImageUrl). }
92 property ImageColumns: Cardinal read FImageColumns write SetImageColumns default 8;
93 { Rows of the characters in the image @link(ImageUrl). }
94 property ImageRows: Cardinal read FImageRows write SetImageRows default 8;
95 { Margin between characters in the image @link(ImageUrl). }
96 property ImageMargin: Cardinal read FImageMargin write SetImageMargin default 0;
97 { Spacing between characters when rendering the font.
98 This is independent from @link(ImageMargin). }
99 property DisplayMargin: Single read FDisplayMargin write SetDisplayMargin {$ifdef FPC}default 2{$endif};
100 end;
101
102 TSimpleTextureFont = TCastleBitmapFont deprecated 'use TCastleBitmapFont';
103
104 {$endif read_interface}
105
106 {$ifdef read_implementation}
107
108 { TCastleBitmapFont --------------------------------------------------------- }
109
110 constructor TCastleBitmapFont.Create(AOwner: TComponent);
111 begin
112 inherited;
113 FGlyphsScreenRects := TFloatRectangleList.Create;
114 FGlyphsImageRects := TFloatRectangleList.Create;
115 FImageColumns := 8;
116 FImageRows := 8;
117 FImageMargin := 0;
118 FDisplayMargin := 2;
119 end;
120
121 destructor TCastleBitmapFont.Destroy;
122 begin
123 FreeAndNil(FGlyphsScreenRects);
124 FreeAndNil(FGlyphsImageRects);
125 Unload;
126 inherited;
127 end;
128
129 procedure TCastleBitmapFont.Unload;
130 begin
131 GLContextClose;
132 FreeAndNil(FImage);
133 end;
134
135 procedure TCastleBitmapFont.SetImageUrl(const Value: String);
136 begin
137 if FImageUrl <> Value then
138 begin
139 FImageUrl := Value;
140 Unload;
141 if Value <> '' then
142 try
143 FImage := LoadImage(Value);
144 { CharHeight changed, reset Size -- this makes it natural to work,
145 as in usual case, when working with TCastleLabel, you don't care about
146 TCastleBitmapFont.Size, you only set final size on TCastleLabel. }
147 Size := CharHeight;
148 except
149 on E: Exception do
150 begin
151 if CastleDesignMode then
152 begin
153 WritelnWarning('TCastleBitmapFont', 'Failed to load image "%s": %s',
154 [UriDisplay(FImageUrl), ExceptMessage(E)]);
155 Exit;
156 end else
157 raise;
158 end;
159 end;
160 end;
161 end;
162
163 procedure TCastleBitmapFont.SetImageColumns(const Value: Cardinal);
164 begin
165 if FImageColumns <> Value then
166 begin
167 FImageColumns := Value;
168 FontSizesChanged; // cause repaint, glyphs changed
169 end;
170 end;
171
172 procedure TCastleBitmapFont.SetImageRows(const Value: Cardinal);
173 begin
174 if FImageRows <> Value then
175 begin
176 FImageRows := Value;
177 { CharHeight changed, reset Size -- this makes it natural to work,
178 as in usual case, when working with TCastleLabel, you don't care about
179 TCastleBitmapFont.Size, you only set final size on TCastleLabel. }
180 Size := CharHeight;
181 FontSizesChanged; // cause repaint, glyphs changed
182 end;
183 end;
184
185 procedure TCastleBitmapFont.SetImageMargin(const Value: Cardinal);
186 begin
187 if FImageMargin <> Value then
188 begin
189 FImageMargin := Value;
190 FontSizesChanged; // cause repaint, glyphs changed
191 end;
192 end;
193
194 procedure TCastleBitmapFont.SetDisplayMargin(const Value: Single);
195 begin
196 if FDisplayMargin <> Value then
197 begin
198 FDisplayMargin := Value;
199 FontSizesChanged; // cause repaint
200 end;
201 end;
202
203 function TCastleBitmapFont.CharWidth: Cardinal;
204 begin
205 if FImage <> nil then
206 Result := Max(0, Integer(FImage.Width div ImageColumns) - ImageMargin)
207 else
208 Result := 0;
209 end;
210
211 function TCastleBitmapFont.CharHeight: Cardinal;
212 begin
213 if FImage <> nil then
214 Result := Max(0, Integer(FImage.Height div ImageRows) - ImageMargin)
215 else
216 Result := 0;
217 end;
218
219 procedure TCastleBitmapFont.Load(const AImage: TCastleImage;
220 const AImageColumns, AImageRows, AImageMargin: Cardinal; const ADisplayMargin: Single);
221 begin
222 Unload;
223
224 FImage := AImage;
225
226 FImageColumns := AImageColumns;
227 FImageRows := AImageRows;
228 FImageMargin := AImageMargin;
229 FDisplayMargin := ADisplayMargin;
230 Size := CharHeight;
231
232 FontSizesChanged;
233 end;
234
235 function TCastleBitmapFont.ScaledCharWidth: Single;
236 begin
237 Result := CharWidth * Scale + Outline * 2;
238 end;
239
240 function TCastleBitmapFont.ScaledCharHeight: Single;
241 begin
242 Result := CharHeight * Scale + Outline * 2;
243 end;
244
245 function TCastleBitmapFont.ScaledDisplayMargin: Single;
246 begin
247 Result := FDisplayMargin * Scale;
248 end;
249
250 function TCastleBitmapFont.Scale: Single;
251 begin
252 Result := Size / CharHeight;
253 end;
254
255 procedure TCastleBitmapFont.SetSize(const Value: Single);
256 begin
257 inherited SetSize(Value);
258 if FDrawableImage <> nil then
259 FDrawableImage.SmoothScaling := GetSmoothScaling;
260 end;
261
262 function TCastleBitmapFont.GetSmoothScaling: boolean;
263 begin
264 Result := Size <> CharHeight;
265 end;
266
267 procedure TCastleBitmapFont.PrepareResources;
268 begin
269 inherited;
270 if FDrawableImage = nil then
271 FDrawableImage := TDrawableImage.Create(FImage, GetSmoothScaling, false);
272 end;
273
274 procedure TCastleBitmapFont.GLContextClose;
275 begin
276 FreeAndNil(FDrawableImage);
277 inherited;
278 end;
279
280 procedure TCastleBitmapFont.Print(const X, Y: Single; const Color: TCastleColor;
281 const S: string);
282 var
283 GlyphsToRender: Integer;
284
285 procedure GlyphDraw(const ScreenRect, ImageRect: TFloatRectangle);
286 begin
287 if TargetImage = nil then
288 begin
289 Assert(GlyphsToRender < FGlyphsScreenRects.Count);
290 FGlyphsScreenRects.L[GlyphsToRender] := ScreenRect;
291 FGlyphsImageRects .L[GlyphsToRender] := ImageRect;
292 Inc(GlyphsToRender);
293 end else
294 begin
295 TargetImage.DrawFrom(FImage,
296 Round(ScreenRect.Left),
297 Round(ScreenRect.Bottom),
298 Round(ImageRect.Left),
299 Round(ImageRect.Bottom),
300 Round(ImageRect.Width),
301 Round(ImageRect.Height));
302 end;
303 end;
304
305 var
306 ImageX, ImageY, ScreenX, ScreenY, InitialMargin: Single;
307 CharIndex: Integer;
308 Iter: TCastleStringIterator;
309 I: Integer;
310 begin
311 if TargetImage = nil then
312 begin
313 PrepareResources;
314 FGlyphsScreenRects.Count := Max(MinimumGlyphsAllocated, Length(S));
315 FGlyphsImageRects .Count := Max(MinimumGlyphsAllocated, Length(S));
316 FDrawableImage.Color := Color;
317 end;
318
319 GlyphsToRender := 0;
320
321 { Using Floor(ScaledDisplayMargin / 2)
322 instead of just "ScaledDisplayMargin / 2",
323 because it looks better in case of rendering the unscaled font
324 (Scale = 1), at integer coordinates (e.g. X=10.0, Y=10.0),
325 and ScaledDisplayMargin is odd ineger (e.g. 1.0).
326
327 You expect then the font to hit pixels exactly,
328 while "ScaledDisplayMargin / 2" would mean that everything is shifted
329 by 0.5 pixel.
330
331 Testcase: font_from_texture.lpr using null_terminator_0.png font. }
332 InitialMargin := Floor(ScaledDisplayMargin / 2);
333
334 I := 0;
335 Iter.Start(S);
336 while Iter.GetNext do
337 begin
338 CharIndex := Iter.Current - Ord(' ');
339 ImageX := CharIndex mod FImageColumns;
340 ImageY := CharIndex div FImageColumns;
341 if ImageY < ImageRows then
342 begin
343 ImageX := ImageX * (CharWidth + FImageMargin);
344 ImageY := FImage.Height - (ImageY + 1) * (CharHeight + FImageMargin);
345 ScreenX := InitialMargin + X + I * (ScaledCharWidth + ScaledDisplayMargin);
346 ScreenY := InitialMargin + Y;
347 Inc(I);
348
349 { TODO: this ignores Outline and related properties now, always renders like Outline = 0. }
350
351 GlyphDraw(
352 FloatRectangle(ScreenX, ScreenY, ScaledCharWidth, ScaledCharHeight),
353 FloatRectangle(ImageX, ImageY, CharWidth, CharHeight));
354 end;
355 end;
356
357 if TargetImage = nil then
358 begin
359 FDrawableImage.Color := Color;
360 FDrawableImage.Draw(
361 PFloatRectangleArray(FGlyphsScreenRects.L),
362 PFloatRectangleArray(FGlyphsImageRects.L), GlyphsToRender);
363 end;
364 end;
365
366 function TCastleBitmapFont.TextWidth(const S: string): Single;
367 begin
368 Result := Length(S) * (ScaledCharWidth + ScaledDisplayMargin);
369 end;
370
371 function TCastleBitmapFont.TextHeight(const S: string): Single;
372 begin
373 Result := ScaledCharHeight + ScaledDisplayMargin;
374 end;
375
376 function TCastleBitmapFont.TextHeightBase(const S: string): Single;
377 begin
378 Result := ScaledCharHeight + ScaledDisplayMargin;
379 end;
380
381 function TCastleBitmapFont.TextMove(const S: string): TVector2;
382 begin
383 Result := Vector2(TextWidth(S), TextHeight(S));
384 end;
385
386 function TCastleBitmapFont.FontLoaded: Boolean;
387 begin
388 Result := FImage <> nil;
389 end;
390
391 function TCastleBitmapFont.PropertySections(const PropertyName: String): TPropertySections;
392 begin
393 if (PropertyName = 'ImageUrl') or
394 (PropertyName = 'ImageColumns') or
395 (PropertyName = 'ImageRows') or
396 (PropertyName = 'ImageMargin') or
397 (PropertyName = 'DisplayMargin') then
398 Result := [psBasic]
399 else
400 Result := inherited PropertySections(PropertyName);
401 end;
402
403 {$endif read_implementation}