Debian Codegraph @

src/deprecated_units/castlewindowsfonts.pas

raw
1 {
2 Copyright 2002-2018 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 { Windows-specific font utilities. }
17
18 unit CastleWindowsFonts
19 deprecated 'use cross-platform fonts from CastleFonts instead of this Windows-specific (and unused) code';
20
21 {$I castleconf.inc}
22
23 interface
24
25 uses Windows, SysUtils, CastleUtils;
26
27 type
28 { Windows font charset values. For each value csXxx below,
29 WinAPI defines constant XXX_CHARSET.
30 Useful for enumerating available charsets, displaying charset name etc. }
31 TWinCharSet = (
32 wcsANSI, wcsDEFAULT, wcsSYMBOL, wcsSHIFTJIS,
33 wcsHANGEUL, wcsGB2312, wcsCHINESEBIG5, wcsOEM,
34 wcsHEBREW, wcsARABIC, wcsGREEK,
35 wcsTURKISH, wcsTHAI, wcsEASTEUROPE,
36 wcsRUSSIAN, wcsBALTIC);
37
38 { A wrapper for CreateFont WinAPI function.
39 Create an instance of this class, setup some attributes, and call GetHandle.
40 In the future this class may be extended to something less trivial.
41
42 For the meaning of properties see WinAPI documentation for CreateFont
43 function. }
44 TWindowsFont = class
45 private
46 FHeight: Integer;
47 FAngle: Integer;
48 FWeight: Integer;
49 FItalic: boolean;
50 FUnderline: boolean;
51 FStrikeOut: boolean;
52 FCharSet: TWinCharSet;
53 FOutputPrecision: DWord;
54 FClipPrecision: DWord;
55 FQuality: DWord;
56 FPitch: DWord;
57 FFamily: DWord;
58 FFaceName: string;
59 public
60 property Height: Integer read FHeight write FHeight;
61
62 { Value for both nEscapement and nOrientation parameters for
63 CreateFont. The only portable way is to set them both to the same
64 values. }
65 property Angle: Integer read FAngle write FAngle default 0;
66
67 property Weight: Integer read FWeight write FWeight default FW_REGULAR;
68 property Italic: boolean read FItalic write FItalic default false;
69 property Underline: boolean read FUnderline write FUnderline default false;
70 property StrikeOut: boolean read FStrikeOut write FStrikeOut default false;
71 property CharSet: TWinCharSet read FCharSet write FCharSet default wcsDEFAULT;
72
73 property OutputPrecision: DWord read FOutputPrecision write FOutputPrecision
74 default OUT_DEFAULT_PRECIS;
75
76 property ClipPrecision: DWord read FClipPrecision write FClipPrecision
77 default CLIP_DEFAULT_PRECIS;
78
79 property Quality: DWord read FQuality write FQuality default DEFAULT_QUALITY;
80
81 { Font pitch and family. They will be combined to create
82 fdwPitchAndFamily param, i.e. fdwPitchAndFamily := Pitch or Family.
83 Pitch is for XXX_PITCH consts, Family is for FF_XXX consts.
84 @groupBegin }
85 property Pitch: DWord read FPitch write FPitch default DEFAULT_PITCH;
86 property Family: DWord read FFamily write FFamily default FF_DONTCARE;
87 { @groupEnd }
88
89 { Font face name. Default is ''. }
90 property FaceName: string read FFaceName write FFaceName;
91
92 { Create a font with given properties. Calls WinAPI CreateFont.
93 Rememeber to free result somewhere by DeleteObject.
94
95 Remeber that you may not get the font you asked for.
96 Windows.CreateFont will try to return something as close as possible,
97 but if exact match will not be possible -- it can return something else.
98 E.g. specifying FaceName = 'some non-existing font name' will not
99 cause some error (like EOSError).
100 Instead it will result in default Windows font ("MS Sans Serif" usually)
101 being returned.
102
103 @raises(EOSError If font cannot be created
104 (when WinAPI CreateFont returned error)) }
105 function GetHandle: HFont;
106
107 { Constructor, takes initial Height value.
108 We require the height value to be passed to constructor,
109 simply because there's no "generally sensible" default value
110 for Height. }
111 constructor Create(AHeight: Integer);
112 end;
113
114 const
115 CharSetsNames: array [TWinCharSet] of string=(
116 'ANSI_CHARSET', 'DEFAULT_CHARSET', 'SYMBOL_CHARSET', 'SHIFTJIS_CHARSET',
117 'HANGEUL_CHARSET', 'GB2312_CHARSET', 'CHINESEBIG5_CHARSET', 'OEM_CHARSET',
118 'HEBREW_CHARSET', 'ARABIC_CHARSET', 'GREEK_CHARSET',
119 'TURKISH_CHARSET', 'THAI_CHARSET', 'EASTEUROPE_CHARSET',
120 'RUSSIAN_CHARSET', 'BALTIC_CHARSET');
121
122 { TODO:
123 Funcs below are a little old.
124 They probably could use some improvements. }
125
126 { Is given Windows font possibly true-type. }
127 function IsFontTrueType( Font: HFONT ): boolean;
128
129 type
130 TEnumFontCharsetsProc_ByObject = procedure( FontCharset: byte ) of object;
131 TEnumFontCharsetsProc = procedure( FontCharset: byte );
132
133 { Enumerate charsets handled by given font. Warning: enumerated values
134 may be repeated.
135 @groupBegin }
136 procedure EnumFontCharsetsObj(const FontName: string; EnumProc : TEnumFontCharsetsProc_ByObject);
137 procedure EnumFontCharsets(const FontName: string; EnumProc : TEnumFontCharsetsProc);
138 { @groupEnd }
139
140 function WinCharSetFromName(const Name: string): TWinCharSet;
141
142 implementation
143
144 uses CastleStringUtils;
145
146 const
147 CharSetsValues: array [TWinCharSet] of DWord = (
148 ANSI_CHARSET, DEFAULT_CHARSET, SYMBOL_CHARSET, SHIFTJIS_CHARSET,
149 HANGEUL_CHARSET, GB2312_CHARSET, CHINESEBIG5_CHARSET, OEM_CHARSET,
150 HEBREW_CHARSET, ARABIC_CHARSET, GREEK_CHARSET,
151 TURKISH_CHARSET, THAI_CHARSET, EASTEUROPE_CHARSET,
152 RUSSIAN_CHARSET, BALTIC_CHARSET);
153
154 { TWindowsFont ------------------------------------------------------------ }
155
156 constructor TWindowsFont.Create(AHeight: Integer);
157 begin
158 FHeight := AHeight;
159 FAngle := 0;
160 FWeight := FW_REGULAR;
161 FItalic := false;
162 FUnderline := false;
163 FStrikeOut := false;
164 FCharSet := wcsDEFAULT;
165 FOutputPrecision := OUT_DEFAULT_PRECIS;
166 FClipPrecision := CLIP_DEFAULT_PRECIS;
167 FQuality := DEFAULT_QUALITY;
168 FPitch := DEFAULT_PITCH;
169 FFamily := FF_DONTCARE;
170 FFaceName := '';
171 end;
172
173 function TWindowsFont.GetHandle: HFont;
174 const
175 BoolTo01: array[boolean]of Cardinal = (0, 1);
176 begin
177 Result := CreateFont(FHeight, 0, FAngle, FAngle,
178 FWeight, BoolTo01[FItalic], BoolTo01[FUnderline], BoolTo01[FStrikeOut],
179 CharSetsValues[FCharSet], FOutputPrecision, FClipPrecision, FQuality,
180 FPitch or FFamily, PCharOrNil(FaceName));
181 OSCheck( Result <> 0, 'CreateFont');
182 end;
183
184 { Windows font query ------------------------------------------------------- }
185
186 function EnumFontFamProc_IsTrueType(var EnumLogfont: TEnumLogFont;
187 var NewTextMetric: TNewTextMetric;
188 FontType: Int32;
189 FuncResultPtr: PtrInt): Int32; stdcall;
190 begin
191 { powinnismy sprawdzic czy znaleziony EnumLogFont.LogFont zgadza sie z szukanym
192 LogFontem. Skoro moze byc wiele fontow o tej samej nazwie ... wiemy ze do tej
193 procedury trafiaja tylko te ktorych nazwa sie zgadza. Ale co z reszta ?
194 Czysto teoretycznie np. wersja regular fontu moze byc realizowana bitmapowo,
195 a wersja Italic - jako TrueType. Nietesty - trudno rozstrzygnac czy znaleiony font
196 "pasuje" do naszego LogFontu - bo jesli np. wersja Italic zostala wygenerowana z
197 wersji regular to w naszym LogFoncie moze byc ustawione Italic a w EnumLogFoncie - nie,
198 ale to bedzie ten sam font ! W zasadzie powinnismy zapamietywac wszystkie
199 znalezione Logfonty a potem sprawdzac czy ten z nich ktory jest "najblizszy"
200 naszgeo szukanego jest czy nie jest true-type. Niestety, kompletny algorytm na
201 to czym jest "najblizszy" zna tylko Microsoft (zaimplementowali go chociazby w
202 CreateFont).
203
204 wiec co robimy ? Przeszukujemy wszystkie fonty o naszej nazwie. Jesli chociaz jeden
205 jest true type to uznajemy nasz font za true-type. }
206
207 if (FontType and TRUETYPE_FONTTYPE) <> 0 then
208 PBoolean(FuncResultPtr)^ := true;
209 Result := 1;
210 end;
211
212 function IsFontTrueType( Font: HFONT ): boolean;
213 { See EnumFontFamProc_IsTrueType implementation comments for more information. }
214 var
215 LogFont: TLogFont;
216 wynik: integer;
217 dc: HDC;
218 savedObj: HGDIOBJ;
219 begin
220 wynik := GetObject(Font, SizeOf(TLogFont), @LogFont);
221 if wynik = 0 then RaiseLastOSError else
222 if wynik <> SizeOf(TLogFont) then
223 raise Exception.Create('IsFontTrueType function : parameter is not a font !');
224 Result := false;
225 dc := GetDC(0);
226 SavedObj := SelectObject(dc, Font);
227 try
228 EnumFontFamilies(dc, @LogFont.lfFaceName, @EnumFontFamProc_IsTrueType, PtrUInt(@Result));
229 finally
230 ReleaseDC(0, dc);
231 SelectObject(dc, SavedObj);
232 end;
233 end;
234
235 { EnumFontCharsets ----------------------------------------------------------------------}
236
237 type
238 TEnumCharsetsInternalInfo_ByObject = record
239 UserEnumProc : TEnumFontCharsetsProc_ByObject;
240 end;
241 PEnumCharsetsInternalInfo_ByObject = ^TEnumCharsetsInternalInfo_ByObject;
242
243 function EnumFontFamExProc_ByObject(var LogFontData : TEnumLogFontEx;
244 var PhysFontData: TNewTextMetricEx;
245 FontType: Int32;
246 InternalInfo: PtrInt): Int32; stdcall;
247 begin
248 PEnumCharsetsInternalInfo_ByObject(InternalInfo)^.
249 UserEnumProc( PhysFontData.NtmENtm.tmCharset );
250 result := 1;
251 end;
252
253 procedure EnumFontCharsetsObj(const FontName: string; EnumProc : TEnumFontCharsetsProc_ByObject);
254 var
255 InternalInfo: TEnumCharsetsInternalInfo_ByObject;
256 DC: HDC;
257 LogFont: TLogFont;
258 begin
259 DC := GetDC(0); { device context desktopu }
260 try
261 FillChar(LogFont, SizeOf(LogFont), 0);
262 LogFont.lfCharSet := DEFAULT_CHARSET;
263 StrCopy(@LogFont.lfFaceName, PChar(FontName));
264 InternalInfo.UserEnumProc := EnumProc;
265 EnumFontFamiliesEx(Dc, {$ifdef FPC}@{$endif} LogFont,
266 {$ifdef FPC}@{$endif} EnumFontFamExProc_ByObject,
267 LPARAM(@InternalInfo), 0);
268 finally ReleaseDC(0, DC) end;
269 end;
270
271 type
272 TEnumCharsetsDisp = class
273 NonObjectEnumProc : TEnumFontCharsetsProc;
274 procedure ObjectEnumProc( FontCharset: byte );
275 end;
276 procedure TEnumCharsetsDisp.ObjectEnumProc(FontCharset: byte);
277 begin { ObjectEnumProc przekazuje po prostu swoj argument do NonObjectenumProc }
278 NonObjectEnumProc( FontCharset );
279 end;
280
281 procedure EnumFontCharsets(const FontName: string; EnumProc : TEnumFontCharsetsProc);
282 var
283 EnumObj: TEnumCharsetsDisp;
284 begin
285 EnumObj := TEnumCharsetsDisp.Create;
286 EnumObj.NonObjectEnumProc := EnumProc;
287 try
288 EnumFontCharsetsObj(FontName, @EnumObj.ObjectEnumProc );
289 finally EnumObj.Free end;
290 end;
291
292 function WinCharSetFromName(const Name: string): TWinCharSet;
293 begin
294 for Result := Low(Result) to High(Result) do
295 if Name = CharSetsNames[Result] then
296 Exit;
297 raise Exception.CreateFmt('Invalid charset name "%s"', [Name]);
298 end;
299
300 end.