Menu

[r3136]: / branches / 3.x / Src / UFontHelper.pas  Maximize  Restore  History

Download this file

359 lines (328 with data), 12.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
{
* UFontHelper.pas
*
* Implements a static class used to assist when working with fonts.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UFontHelper.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UFontHelper;
interface
uses
// Delphi
Classes, Graphics,
// Project
UBaseObjects;
type
{
TFontHelper:
Static class used to assist when working with fonts.
}
TFontHelper = class(TNoConstructObject)
strict private
class function IsTrueTypeFont(const Font: TFont): Boolean;
{Checks if a font is a true type font.
@param Font [in] Font to be checked.
@return True if Font is true type, False if not.
}
class function FontExists(const FontName: string): Boolean;
{Checks if named font exists on sytem.
@param FontName [in] Name of required font.
@return True if font exists, False if not.
}
public
class procedure ListMonoSpaceFonts(const List: TStrings);
{Lists all mono-space fonts on system.
@param List [in] Receives list of fonts. Cleared before fonts added.
}
class procedure ListCommonFontSizes(const List: TStrings);
{Lists all commonly used font sizes.
@param List [in] Receives list of font sizes. Cleared before sizes
added.
}
class procedure SetDefaultFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be the default UI font for the underlying operating
system.
@param Font [in] Font to be set.
@param ForceTrueType [in] Whether content font must be true type.
}
class procedure SetDefaultBaseFont(const BaseFont: TFont;
const ForceTrueType: Boolean);
{Updates a font to use the face of the underlying operating system. Style,
colour etc are preserved and point size may be adjusted to retain relative
size to base font.
@param Font [in] Font to be updated.
@param ForceTrueType [in] Whether content font must be true type.
}
class procedure SetContentFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be the appropriate content font for the underlying
operating system.
@param Font [in] Font to be set.
@param ForceTrueType [in] Whether content font must be true type.
}
class procedure SetDefaultMonoFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be used as the default mono spaced font used by the
program.
@param Font [in] Font to be set.
@param ForceTrueType [in] Whether font must be true type.
}
class function CloneFontHandle(const Handle: THandle): THandle;
{Clones a font described by a font handle. Takes a copy of the specified
font.
@param Handle [in] Handle to font to be cloned.
@return Handle to cloned font. Caller is responsible for releasing the
handle.
}
public
const
DefaultMonoFontName = 'Courier New'; // Default mono font name
strict private
const
DefaultFontName = 'MS Sans Serif'; // Default font name
DefaultTTFontName = 'Arial'; // Default TT font name
DefaultFontSize = 8; // Default font size
DefaultContentFontName = DefaultFontName; // Default content font name
DefaultTTContentFontName // Default TT content font name
= DefaultTTFontName;
DefaultContentFontSize = DefaultFontSize; // Default content font size
VistaFontName = 'Segoe UI'; // Vista default font name
VistaFontSize = 9; // Vista default font size
VistaContentFontName = 'Calibri'; // Vista content font name
VistaContentFontSize = 10; // Vista content font size
XPFontName = 'Tahoma'; // XP default font name
XPFontSize = DefaultFontSize; // XP default font size
XPContentFontName = 'Verdana'; // XP content font name
XPContentFontSize // XP content font size
= DefaultContentFontSize;
DefaultTTMonoFontName // Default TT mono font
= DefaultMonoFontName;
DefaultMonoFontSize = 8; // Default mono font size
end;
implementation
uses
// Delphi
SysUtils, Windows, Forms,
// Project
UGraphicUtils, USystemInfo;
{ TFontHelper }
function MonoFontFamilyProc(PLF: PEnumLogFont; PNTM: PNewTextMetric;
FontType: Integer; List: TStrings): Integer; stdcall;
{EnumFontFamilies() callback function used to add mono-spaced fonts to a list.
@param PLF [in] Structure containing info about a logical font.
@param PNTM [in] Not used.
@param FontType [in] Not used.
@param List [in] List to which mono-spaced fonts are added.
}
begin
if (PLF.elfLogFont.lfPitchAndFamily and $F) = FIXED_PITCH then
List.Add(PLF.elfLogFont.lfFaceName);
Result := 1;
end;
class function TFontHelper.CloneFontHandle(const Handle: THandle): THandle;
{Clones a font described by a font handle. Takes a copy of the specified font.
@param Handle [in] Handle to font to be cloned.
@return Handle to cloned font. Caller is responsible for releasing the
handle.
}
var
LogFont: TLogFont; // logical font info for memo control's font
begin
Result := 0;
if GetObject(Handle, SizeOf(LogFont), @LogFont) > 0 then
Result := CreateFontIndirect(LogFont);
Assert(Result <> 0, ClassName + '.CloneFontHandle: Can''t clone handle');
end;
class function TFontHelper.FontExists(const FontName: string): Boolean;
{Checks if named font exists on sytem.
@param FontName [in] Name of required font.
@return True if font exists, False if not.
}
begin
Result := Screen.Fonts.IndexOf(FontName) >= 0;
end;
class function TFontHelper.IsTrueTypeFont(const Font: TFont): Boolean;
{Checks if a font is a true type font.
@param Font [in] Font to be checked.
@return True if Font is true type, False if not.
}
var
DC: HDC; // device context in which font is selected
TM: TTextMetric; // text metrics for font in DC
begin
DC := CreateDisplayDC;
try
SelectObject(DC, Font.Handle);
GetTextMetrics(DC, TM);
Result := (TM.tmPitchAndFamily and TMPF_TRUETYPE) = TMPF_TRUETYPE;
finally
DeleteDC(DC);
end;
end;
class procedure TFontHelper.ListCommonFontSizes(const List: TStrings);
{Lists all commonly used font sizes.
@param List [in] Receives list of font sizes. Cleared before sizes added.
}
var
FontSize: Integer; // loops thru all font sizes
begin
Assert(Assigned(List), ClassName + '.ListCommonFontSizes: List is nil');
List.Clear;
for FontSize := 7 to 32 do
List.Add(IntToStr(FontSize));
end;
class procedure TFontHelper.ListMonoSpaceFonts(const List: TStrings);
{Lists all mono-space fonts on system.
@param List [in] Receives list of fonts. Cleared before fonts added.
}
var
DC: HDC; // device context required for API call
begin
Assert(Assigned(List), ClassName + '.ListMonoSpaceFonts: List is nil');
List.Clear;
// Get a device context needed for API call
DC := GetDC(0);
try
// Enumerate all font families: handle each font in MonoFontFamilyProc()
EnumFontFamilies(DC, nil, @MonoFontFamilyProc, Integer(List));
finally
ReleaseDC(0, DC);
end;
end;
class procedure TFontHelper.SetContentFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be the appropriate content font for the underlying operating
system.
@param Font [in] Font to be set.
@param ForceTrueType [in] Whether content font must be true type.
}
begin
Assert(TOSInfo.IsWinNT, ClassName + '.SetContentFont: NT platform required');
// Set default content font, size and style
Font.Name := DefaultContentFontName;
Font.Size := DefaultContentFontSize;
Font.Style := [];
if TOSInfo.CheckReportedOS(TOSInfo.WinVista) then
begin
// We have Vista - use Calibri if installed
if FontExists(VistaContentFontName) then
begin
Font.Name := VistaContentFontName;
Font.Size := VistaContentFontSize;
end;
end
else if TOSInfo.CheckReportedOS(TOSInfo.Win2K) then
begin
// We have Win 2K or XP - use Verdana if installed
if FontExists(XPContentFontName) then
begin
Font.Name := XPContentFontName;
Font.Size := XPContentFontSize;
end;
end;
// Force font to true type if required and necessary
if ForceTrueType and not IsTrueTypeFont(Font) then
Font.Name := DefaultTTContentFontName;
end;
class procedure TFontHelper.SetDefaultBaseFont(const BaseFont: TFont;
const ForceTrueType: Boolean);
{Updates a font to use the face of the underlying operating system. Style,
colour etc are preserved and point size may be adjusted to retain relative
size to base font.
@param Font [in] Font to be updated.
@param ForceTrueType [in] Whether content font must be true type.
}
var
DefaultFont: TFont; // default font per OS
FontDelta: Integer; // amount to increment font size by
begin
// Create default font
DefaultFont := TFont.Create;
try
SetDefaultFont(DefaultFont, ForceTrueType);
// font delta is difference between normal default font size and that used
// on a specific OS (e.g. Vista uses Segoe UI 9 rather than MS Sans Serif 8)
FontDelta := DefaultFont.Size - DefaultFontSize;
// change base font name and size as required
BaseFont.Name := DefaultFont.Name;
BaseFont.Size := BaseFont.Size + FontDelta;
finally
FreeAndNil(DefaultFont);
end;
end;
class procedure TFontHelper.SetDefaultFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be the default UI font for the underlying operating system.
@param Font [in] Font to be set.
@param ForceTrueType [in] Whether content font must be true type.
}
begin
Assert(TOSInfo.IsWinNT, ClassName + '.SetDefaultFont: NT platform required');
// Set default font, size and style
Font.Name := DefaultFontName;
Font.Size := DefaultFontSize;
Font.Style := [];
if TOSInfo.CheckReportedOS(TOSInfo.WinVista) then
begin
// We have Vista - use Segoe UI if installed
if FontExists(VistaFontName) then
begin
Font.Name := VistaFontName;
Font.Size := VistaFontSize;
end;
end
else if TOSInfo.CheckReportedOS(TOSInfo.Win2K) then
begin
// We have Win 2K or XP - use Tahoma if installed
if FontExists(XPFontName) then
begin
Font.Name := XPFontName;
Font.Size := XPFontSize;
end;
end;
// Force font to true type if required and necessary
if ForceTrueType and not IsTrueTypeFont(Font) then
Font.Name := DefaultTTFontName;
end;
class procedure TFontHelper.SetDefaultMonoFont(const Font: TFont;
const ForceTrueType: Boolean);
{Sets a font to be used as the default mono spaced font used by the program.
@param Font [in] Font to be set to default mono font.
@param ForceTrueType [in] Whether font must be true type.
}
begin
if ForceTrueType then
Font.Name := DefaultTTMonoFontName
else
Font.Name := DefaultMonoFontName;
Font.Size := DefaultMonoFontSize;
Font.Style := [];
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.