]> git.saurik.com Git - wxWidgets.git/blame - src/msw/fontutil.cpp
1. wxMSW::wxNotebook::SetPageSize() and SetPadding() added
[wxWidgets.git] / src / msw / fontutil.cpp
CommitLineData
11c7d5b6
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/fontutil.cpp
3// Purpose: font-related helper functions for wxMSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 05.11.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "fontutil.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35#endif //WX_PRECOMP
36
37#include "wx/msw/private.h"
38
39#include "wx/fontutil.h"
40#include "wx/fontmap.h"
41
42#include "wx/tokenzr.h"
43
44// ============================================================================
45// implementation
46// ============================================================================
47
48// ----------------------------------------------------------------------------
49// wxNativeEncodingInfo
50// ----------------------------------------------------------------------------
51
52// convert to/from the string representation: format is
1e1d0be1 53// encodingid;facename[;charset]
11c7d5b6
VZ
54
55bool wxNativeEncodingInfo::FromString(const wxString& s)
56{
57 wxStringTokenizer tokenizer(s, _T(";"));
58
1e1d0be1
VS
59 wxString encid = tokenizer.GetNextToken();
60 long enc;
61 if ( !encid.ToLong(&enc) )
62 return FALSE;
63 encoding = (wxFontEncoding)enc;
64
11c7d5b6
VZ
65 facename = tokenizer.GetNextToken();
66 if ( !facename )
67 return FALSE;
68
69 wxString tmp = tokenizer.GetNextToken();
70 if ( !tmp )
71 {
72 // default charset (don't use DEFAULT_CHARSET though because of subtle
73 // Windows 9x/NT differences in handling it)
74 charset = ANSI_CHARSET;
75 }
76 else
77 {
78 if ( wxSscanf(tmp, _T("%u"), &charset) != 1 )
79 {
80 // should be a number!
81 return FALSE;
82 }
83 }
84
85 return TRUE;
86}
87
88wxString wxNativeEncodingInfo::ToString() const
89{
1e1d0be1
VS
90 wxString s;
91
92 s << (long)encoding << _T(';') << facename;
11c7d5b6
VZ
93 if ( charset != ANSI_CHARSET )
94 {
1e1d0be1 95 s << _T(';') << charset;
11c7d5b6
VZ
96 }
97
98 return s;
99}
100
101// ----------------------------------------------------------------------------
102// helper functions
103// ----------------------------------------------------------------------------
104
105bool wxGetNativeFontEncoding(wxFontEncoding encoding,
106 wxNativeEncodingInfo *info)
107{
108 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
109
110 if ( encoding == wxFONTENCODING_DEFAULT )
111 {
112 encoding = wxFont::GetDefaultEncoding();
113 }
114
115 switch ( encoding )
116 {
117 // although this function is supposed to return an exact match, do do
118 // some mappings here for the most common case of "standard" encoding
119 case wxFONTENCODING_SYSTEM:
120 case wxFONTENCODING_ISO8859_1:
121 case wxFONTENCODING_ISO8859_15:
122 case wxFONTENCODING_CP1252:
123 info->charset = ANSI_CHARSET;
124 break;
125
126#if !defined(__WIN16__)
127 case wxFONTENCODING_CP1250:
128 info->charset = EASTEUROPE_CHARSET;
129 break;
130
131 case wxFONTENCODING_CP1251:
132 info->charset = RUSSIAN_CHARSET;
133 break;
134
135 case wxFONTENCODING_CP1253:
136 info->charset = GREEK_CHARSET;
137 break;
138
139 case wxFONTENCODING_CP1254:
140 info->charset = TURKISH_CHARSET;
141 break;
142
143 case wxFONTENCODING_CP1255:
144 info->charset = HEBREW_CHARSET;
145 break;
146
147 case wxFONTENCODING_CP1256:
148 info->charset = ARABIC_CHARSET;
149 break;
150
151 case wxFONTENCODING_CP1257:
152 info->charset = BALTIC_CHARSET;
153 break;
f3314fbc
JS
154
155 case wxFONTENCODING_CP874:
156 info->charset = THAI_CHARSET;
157 break;
11c7d5b6
VZ
158#endif // !Win16
159
160 case wxFONTENCODING_CP437:
161 info->charset = OEM_CHARSET;
162 break;
163
164 default:
165 // no way to translate this encoding into a Windows charset
166 return FALSE;
167 }
168
6b0eebc5
VS
169 info->encoding = encoding;
170
11c7d5b6
VZ
171 return TRUE;
172}
173
174bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
175{
176 // try to create such font
177 LOGFONT lf;
178 wxZeroMemory(lf); // all default values
179
180 lf.lfCharSet = info.charset;
0e89bb17 181 wxStrncpy(lf.lfFaceName, info.facename, sizeof(lf.lfFaceName));
11c7d5b6
VZ
182
183 HFONT hfont = ::CreateFontIndirect(&lf);
184 if ( !hfont )
185 {
186 // no such font
187 return FALSE;
188 }
189
190 ::DeleteObject((HGDIOBJ)hfont);
191
192 return TRUE;
193}
194
195// ----------------------------------------------------------------------------
196// wxFont <-> LOGFONT conversion
197// ----------------------------------------------------------------------------
198
199void wxFillLogFont(LOGFONT *logFont, const wxFont *font)
200{
201 int ff_family;
202 wxString ff_face;
203
204 switch ( font->GetFamily() )
205 {
206 case wxSCRIPT:
207 ff_family = FF_SCRIPT;
208 ff_face = _T("Script");
209 break;
210
211 case wxDECORATIVE:
212 ff_family = FF_DECORATIVE;
213 break;
214
215 case wxROMAN:
216 ff_family = FF_ROMAN;
217 ff_face = _T("Times New Roman");
218 break;
219
220 case wxTELETYPE:
221 case wxMODERN:
222 ff_family = FF_MODERN;
223 ff_face = _T("Courier New");
224 break;
225
226 case wxSWISS:
227 ff_family = FF_SWISS;
228 ff_face = _T("Arial");
229 break;
230
231 case wxDEFAULT:
232 default:
233 ff_family = FF_SWISS;
234 ff_face = _T("MS Sans Serif");
235 }
236
237 BYTE ff_italic;
238 switch ( font->GetStyle() )
239 {
240 case wxITALIC:
241 case wxSLANT:
242 ff_italic = 1;
243 break;
244
245 default:
246 wxFAIL_MSG(wxT("unknown font slant"));
247 // fall through
248
249 case wxNORMAL:
250 ff_italic = 0;
251 }
252
253 int ff_weight;
254 switch ( font->GetWeight() )
255 {
256 default:
257 wxFAIL_MSG(_T("unknown font weight"));
258 // fall through
259
260 case wxNORMAL:
261 ff_weight = FW_NORMAL;
262 break;
263
264 case wxLIGHT:
265 ff_weight = FW_LIGHT;
266 break;
267
268 case wxBOLD:
269 ff_weight = FW_BOLD;
270 break;
271 }
272
273#if 0
274 HDC dc = ::GetDC(NULL);
275 int ppInch = ::GetDeviceCaps(dc, LOGPIXELSY);
276 ::ReleaseDC(NULL, dc);
277#else
278 // New behaviour: apparently ppInch varies according to Large/Small Fonts
279 // setting in Windows. This messes up fonts. So, set ppInch to a constant
280 // 96 dpi.
281 static const int ppInch = 96;
282#endif // 0/1
283
284#if wxFONT_SIZE_COMPATIBILITY
285 // Incorrect, but compatible with old wxWindows behaviour
286 int nHeight = (font->GetPointSize()*ppInch/72);
287#else
288 // Correct for Windows compatibility
bf027a6d
JS
289// int nHeight = - (font->GetPointSize()*ppInch/72);
290 int nHeight = - (int) ( (font->GetPointSize()*((double)ppInch)/72.0) + 0.5);
11c7d5b6
VZ
291#endif
292
293 wxString facename = font->GetFaceName();
294 if ( !!facename )
295 {
296 ff_face = facename;
297 }
298 //else: ff_face is a reasonable default facename for this font family
299
300 // deal with encoding now
301 wxNativeEncodingInfo info;
302 wxFontEncoding encoding = font->GetEncoding();
303 if ( !wxGetNativeFontEncoding(encoding, &info) )
304 {
305 if ( !wxTheFontMapper->GetAltForEncoding(encoding, &info) )
306 {
307 // unsupported encoding, replace with the default
308 info.charset = ANSI_CHARSET;
309 }
310 }
311
312 if ( !info.facename.IsEmpty() )
313 {
314 // the facename determined by the encoding overrides everything else
315 ff_face = info.facename;
316 }
317
318 // transfer all the data to LOGFONT
319 logFont->lfHeight = nHeight;
320 logFont->lfWidth = 0;
321 logFont->lfEscapement = 0;
322 logFont->lfOrientation = 0;
323 logFont->lfWeight = ff_weight;
324 logFont->lfItalic = ff_italic;
325 logFont->lfUnderline = (BYTE)font->GetUnderlined();
326 logFont->lfStrikeOut = 0;
327 logFont->lfCharSet = info.charset;
328 logFont->lfOutPrecision = OUT_DEFAULT_PRECIS;
329 logFont->lfClipPrecision = CLIP_DEFAULT_PRECIS;
330 logFont->lfQuality = PROOF_QUALITY;
331 logFont->lfPitchAndFamily = DEFAULT_PITCH | ff_family;
332 wxStrncpy(logFont->lfFaceName, ff_face, WXSIZEOF(logFont->lfFaceName));
333}
334
335wxFont wxCreateFontFromLogFont(const LOGFONT *logFont)
336{
337 // extract family from pitch-and-family
338 int lfFamily = logFont->lfPitchAndFamily;
339 if ( lfFamily & FIXED_PITCH )
340 lfFamily -= FIXED_PITCH;
341 if ( lfFamily & VARIABLE_PITCH )
342 lfFamily -= VARIABLE_PITCH;
343
344 int fontFamily;
345 switch ( lfFamily )
346 {
347 case FF_ROMAN:
348 fontFamily = wxROMAN;
349 break;
350
351 case FF_SWISS:
352 fontFamily = wxSWISS;
353 break;
354
355 case FF_SCRIPT:
356 fontFamily = wxSCRIPT;
357 break;
358
359 case FF_MODERN:
360 fontFamily = wxMODERN;
361 break;
362
363 case FF_DECORATIVE:
364 fontFamily = wxDECORATIVE;
365 break;
366
367 default:
368 fontFamily = wxSWISS;
369 }
370
371 // weight and style
372 int fontWeight = wxNORMAL;
373 switch ( logFont->lfWeight )
374 {
375 case FW_LIGHT:
376 fontWeight = wxLIGHT;
377 break;
378
379 default:
380 case FW_NORMAL:
381 fontWeight = wxNORMAL;
382 break;
383
384 case FW_BOLD:
385 fontWeight = wxBOLD;
386 break;
387 }
388
389 int fontStyle = logFont->lfItalic ? wxITALIC : wxNORMAL;
390
391 bool fontUnderline = logFont->lfUnderline != 0;
392
393 wxString fontFace = logFont->lfFaceName;
394
395 // font size
396 HDC dc = ::GetDC(NULL);
397
398 // remember that 1pt = 1/72inch
399 int height = abs(logFont->lfHeight);
bf027a6d 400 int fontPoints = (int) ((72.0*((double)height))/(double) GetDeviceCaps(dc, LOGPIXELSY) + 0.5);
11c7d5b6
VZ
401
402 ::ReleaseDC(NULL, dc);
403
404 wxFontEncoding fontEncoding;
405 switch ( logFont->lfCharSet )
406 {
407 default:
408 wxFAIL_MSG(wxT("unsupported charset"));
409 // fall through
410
411 case ANSI_CHARSET:
412 fontEncoding = wxFONTENCODING_CP1252;
413 break;
414
0e528b99 415#ifdef __WIN32__
11c7d5b6
VZ
416 case EASTEUROPE_CHARSET:
417 fontEncoding = wxFONTENCODING_CP1250;
418 break;
419
420 case BALTIC_CHARSET:
421 fontEncoding = wxFONTENCODING_CP1257;
422 break;
423
424 case RUSSIAN_CHARSET:
425 fontEncoding = wxFONTENCODING_CP1251;
426 break;
427
428 case ARABIC_CHARSET:
429 fontEncoding = wxFONTENCODING_CP1256;
430 break;
431
432 case GREEK_CHARSET:
433 fontEncoding = wxFONTENCODING_CP1253;
434 break;
435
436 case HEBREW_CHARSET:
437 fontEncoding = wxFONTENCODING_CP1255;
438 break;
439
440 case TURKISH_CHARSET:
441 fontEncoding = wxFONTENCODING_CP1254;
442 break;
f3314fbc
JS
443
444 case THAI_CHARSET:
445 fontEncoding = wxFONTENCODING_CP437;
446 break;
0e528b99 447#endif
11c7d5b6
VZ
448
449 case OEM_CHARSET:
450 fontEncoding = wxFONTENCODING_CP437;
451 break;
452 }
453
454 return wxFont(fontPoints, fontFamily, fontStyle,
455 fontWeight, fontUnderline, fontFace,
456 fontEncoding);
457}
458
459