Debian Codegraph @

tools/texture-font-to-pascal/castlefont2pascal.pas

raw
1 {
2 Copyright 2004-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Converting fonts (TTextureFontData) to Pascal source code. }
17 unit CastleFont2Pascal;
18
19 interface
20
21 uses CastleTextureFontData, Classes;
22
23 { @noAutoLinkHere }
24 procedure Font2Pascal(const Font: TTextureFontData;
25 const UnitName, PrecedingComment, FontFunctionName: string; Stream: TStream);
26 overload;
27
28 { @noAutoLinkHere }
29 procedure Font2Pascal(const Font: TTextureFontData;
30 const UnitName, PrecedingComment, FontFunctionName: string;
31 const OutUrl: String); overload;
32
33 implementation
34
35 uses SysUtils, CastleUtils, CastleStringUtils, CastleClassUtils, CastleDownload,
36 CastleUnicode;
37
38 procedure Font2Pascal(const Font: TTextureFontData;
39 const UnitName, PrecedingComment, FontFunctionName: string; Stream: TStream);
40 var
41 C: TUnicodeChar;
42 G: TTextureFontData.TGlyph;
43 ImageInterface, ImageImplementation, ImageInitialization, ImageFinalization: string;
44 LoadedGlyphs: TUnicodeCharList;
45 begin
46 WriteStr(Stream,
47 '{ -*- buffer-read-only: t -*- }' +NL+
48 NL+
49 '{ Unit automatically generated by ' + ApplicationName + ',' +NL+
50 ' to embed font data in Pascal source code.' +NL+
51 ' @exclude (Exclude this unit from PasDoc documentation.)' +NL+
52 NL+
53 PrecedingComment+
54 '}' +NL+
55 'unit ' + UnitName + ';' +NL+
56 NL+
57 'interface'+NL+
58 NL+
59 'uses CastleTextureFontData;' +NL+
60 NL+
61 'function ' + FontFunctionName + ': TTextureFontData;' +NL+
62 NL+
63 'implementation' +NL+
64 NL+
65 'uses SysUtils, CastleImages, CastleInternalDataCompression;' + NL+
66 NL
67 );
68
69 ImageInterface := '';
70 ImageImplementation := '';
71 ImageInitialization := '';
72 ImageFinalization := '';
73
74 Font.Image.SaveToPascalCode('FontImage',
75 ImageInterface, ImageImplementation, ImageInitialization, ImageFinalization);
76
77 WriteStr(Stream,
78 // ImageInterface + // no need for this (and it would require additional "forward")
79 '{ -----------------------------------------------------------------------' + NL +
80 ' Embedded font image (TCastleImage instance). }' + NL +
81 NL +
82 ImageImplementation +
83 NL +
84 '{ -----------------------------------------------------------------------' + NL +
85 ' Embedded font (TTextureFontData instance). }' + NL +
86 NL +
87 'function Create' + FontFunctionName + ': TTextureFontData;' +NL+
88 'var' +NL+
89 ' Glyphs: TTextureFontData.TGlyphDictionary;' +NL+
90 ' G: TTextureFontData.TGlyph;' +NL+
91 'begin' +NL+
92 ' FontImage.TreatAsAlpha := true;' +NL+
93 ' FontImage.Url := ''embedded-font:/' + UnitName + ''';' +NL+
94 NL+
95 ' Glyphs := TTextureFontData.TGlyphDictionary.Create;' +NL+
96 NL);
97
98 LoadedGlyphs := Font.LoadedGlyphs;
99 try
100 for C in LoadedGlyphs do
101 begin
102 G := Font.Glyph(C, false);
103 if G <> nil then
104 begin
105 WriteStr(Stream,
106 ' G := TTextureFontData.TGlyph.Create;' +NL+
107 ' G.X := ' + IntToStr(G.X) + ';' +NL+
108 ' G.Y := ' + IntToStr(G.Y) + ';' +NL+
109 ' G.AdvanceX := ' + IntToStr(G.AdvanceX) + ';' +NL+
110 ' G.AdvanceY := ' + IntToStr(G.AdvanceY) + ';' +NL+
111 ' G.Width := ' + IntToStr(G.Width) + ';' +NL+
112 ' G.Height := ' + IntToStr(G.Height) + ';' +NL+
113 ' G.ImageX := ' + IntToStr(G.ImageX) + ';' +NL+
114 ' G.ImageY := ' + IntToStr(G.ImageY) + ';' +NL+
115 ' Glyphs[' + IntToStr(Ord(C)) + '] := G;' +NL+
116 NL);
117 end;
118 end;
119 finally FreeAndNil(LoadedGlyphs) end;
120
121 WriteStr(Stream,
122 ' Result := TTextureFontData.CreateFromData(Glyphs, FontImage, ' +
123 IntToStr(Font.Size) + ', ' +
124 LowerCase(BoolToStr(Font.AntiAliased, true)) + ');' +NL+
125 'end;' +NL+
126 NL+
127 'var' +NL+
128 ' FFont: TTextureFontData;' +NL+
129 '' +NL+
130 'function ' + FontFunctionName + ': TTextureFontData;' +NL+
131 'begin' +NL+
132 ' // create FFont on-demand' +NL+
133 ' if FFont = nil then FFont := Create' + FontFunctionName + ';' +NL+
134 ' Result := FFont;' +NL+
135 'end;' +NL+
136 NL+
137 'initialization' +NL+
138 ImageInitialization +
139 'finalization' +NL+
140 ' FreeAndNil(FFont);' +NL+
141 { This frees the image, but we don't want it --
142 if the image is created, it is owned by TTextureFontData instance. }
143 // ImageFinalization +
144 'end.' + NL);
145 end;
146
147 procedure Font2Pascal(const Font: TTextureFontData;
148 const UnitName, PrecedingComment, FontFunctionName: string;
149 const OutUrl: String); overload;
150 var
151 Stream: TStream;
152 begin
153 Stream := UrlSaveStream(OutUrl);
154 try
155 Font2Pascal(Font, UnitName, PrecedingComment, FontFunctionName, Stream);
156 finally Stream.Free end;
157 end;
158
159 end.