1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
37 #include "wx/msw/private.h"
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
42 #include "wx/tokenzr.h"
44 // for MSVC5 and old w32api
45 #ifndef HANGUL_CHARSET
46 # define HANGUL_CHARSET 129
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxNativeEncodingInfo
55 // ----------------------------------------------------------------------------
57 // convert to/from the string representation: format is
58 // encodingid;facename[;charset]
60 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
62 wxStringTokenizer
tokenizer(s
, _T(";"));
64 wxString encid
= tokenizer
.GetNextToken();
66 if ( !encid
.ToLong(&enc
) )
68 encoding
= (wxFontEncoding
)enc
;
70 facename
= tokenizer
.GetNextToken();
72 wxString tmp
= tokenizer
.GetNextToken();
75 // default charset (don't use DEFAULT_CHARSET though because of subtle
76 // Windows 9x/NT differences in handling it)
77 charset
= ANSI_CHARSET
;
81 if ( wxSscanf(tmp
, _T("%u"), &charset
) != 1 )
83 // should be a number!
91 wxString
wxNativeEncodingInfo::ToString() const
95 s
<< (long)encoding
<< _T(';') << facename
;
96 if ( charset
!= ANSI_CHARSET
)
98 s
<< _T(';') << charset
;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
109 wxNativeEncodingInfo
*info
)
111 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
113 if ( encoding
== wxFONTENCODING_DEFAULT
)
115 encoding
= wxFont::GetDefaultEncoding();
120 // although this function is supposed to return an exact match, do do
121 // some mappings here for the most common case of "standard" encoding
122 case wxFONTENCODING_SYSTEM
:
123 case wxFONTENCODING_ISO8859_1
:
124 case wxFONTENCODING_ISO8859_15
:
125 case wxFONTENCODING_CP1252
:
126 info
->charset
= ANSI_CHARSET
;
129 #if !defined(__WIN16__) && !defined(__WXMICROWIN__)
131 // The following four fonts are multi-byte charsets
132 case wxFONTENCODING_CP932
:
133 info
->charset
= SHIFTJIS_CHARSET
;
136 case wxFONTENCODING_CP936
:
137 info
->charset
= GB2312_CHARSET
;
140 case wxFONTENCODING_CP949
:
141 info
->charset
= HANGUL_CHARSET
;
144 case wxFONTENCODING_CP950
:
145 info
->charset
= CHINESEBIG5_CHARSET
;
148 // The rest are single byte encodings
149 case wxFONTENCODING_CP1250
:
150 info
->charset
= EASTEUROPE_CHARSET
;
153 case wxFONTENCODING_CP1251
:
154 info
->charset
= RUSSIAN_CHARSET
;
157 case wxFONTENCODING_CP1253
:
158 info
->charset
= GREEK_CHARSET
;
161 case wxFONTENCODING_CP1254
:
162 info
->charset
= TURKISH_CHARSET
;
165 case wxFONTENCODING_CP1255
:
166 info
->charset
= HEBREW_CHARSET
;
169 case wxFONTENCODING_CP1256
:
170 info
->charset
= ARABIC_CHARSET
;
173 case wxFONTENCODING_CP1257
:
174 info
->charset
= BALTIC_CHARSET
;
177 case wxFONTENCODING_CP874
:
178 info
->charset
= THAI_CHARSET
;
184 case wxFONTENCODING_CP437
:
185 info
->charset
= OEM_CHARSET
;
189 // no way to translate this encoding into a Windows charset
193 info
->encoding
= encoding
;
198 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
200 // try to create such font
202 wxZeroMemory(lf
); // all default values
204 lf
.lfCharSet
= info
.charset
;
205 wxStrncpy(lf
.lfFaceName
, info
.facename
, sizeof(lf
.lfFaceName
));
207 HFONT hfont
= ::CreateFontIndirect(&lf
);
214 ::DeleteObject((HGDIOBJ
)hfont
);
219 // ----------------------------------------------------------------------------
220 // wxFontEncoding <-> CHARSET_XXX
221 // ----------------------------------------------------------------------------
223 wxFontEncoding
wxGetFontEncFromCharSet(int cs
)
225 wxFontEncoding fontEncoding
;
230 // JACS: Silently using ANSI_CHARSET
231 // apparently works for Chinese Windows. Assume it works
232 // for all/most other languages.
233 //wxFAIL_MSG(wxT("unsupported charset"));
237 fontEncoding
= wxFONTENCODING_CP1252
;
240 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
241 case EASTEUROPE_CHARSET
:
242 fontEncoding
= wxFONTENCODING_CP1250
;
246 fontEncoding
= wxFONTENCODING_CP1257
;
249 case RUSSIAN_CHARSET
:
250 fontEncoding
= wxFONTENCODING_CP1251
;
254 fontEncoding
= wxFONTENCODING_CP1256
;
258 fontEncoding
= wxFONTENCODING_CP1253
;
262 fontEncoding
= wxFONTENCODING_CP1255
;
265 case TURKISH_CHARSET
:
266 fontEncoding
= wxFONTENCODING_CP1254
;
270 fontEncoding
= wxFONTENCODING_CP437
;
273 case SHIFTJIS_CHARSET
:
274 fontEncoding
= wxFONTENCODING_CP932
;
278 fontEncoding
= wxFONTENCODING_CP936
;
282 fontEncoding
= wxFONTENCODING_CP949
;
285 case CHINESEBIG5_CHARSET
:
286 fontEncoding
= wxFONTENCODING_CP950
;
292 fontEncoding
= wxFONTENCODING_CP437
;
299 // ----------------------------------------------------------------------------
300 // wxFont <-> LOGFONT conversion
301 // ----------------------------------------------------------------------------
303 void wxFillLogFont(LOGFONT
*logFont
, const wxFont
*font
)
308 switch ( font
->GetFamily() )
311 ff_family
= FF_SCRIPT
;
312 ff_face
= _T("Script");
316 ff_family
= FF_DECORATIVE
;
320 ff_family
= FF_ROMAN
;
321 ff_face
= _T("Times New Roman");
326 ff_family
= FF_MODERN
;
327 ff_face
= _T("Courier New");
331 ff_family
= FF_SWISS
;
332 ff_face
= _T("Arial");
337 ff_family
= FF_SWISS
;
338 ff_face
= _T("MS Sans Serif");
342 switch ( font
->GetStyle() )
350 wxFAIL_MSG(wxT("unknown font slant"));
358 switch ( font
->GetWeight() )
361 wxFAIL_MSG(_T("unknown font weight"));
365 ff_weight
= FW_NORMAL
;
369 ff_weight
= FW_LIGHT
;
377 // VZ: I'm reverting this as we clearly must use the real screen
378 // resolution instead of hardcoded one or it surely will fail to work
381 // If there are any problems with this code, please let me know about
382 // it instead of reverting this change, thanks!
383 #if 1 // wxUSE_SCREEN_DPI
384 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
386 // New behaviour: apparently ppInch varies according to Large/Small Fonts
387 // setting in Windows. This messes up fonts. So, set ppInch to a constant
389 static const int ppInch
= 96;
392 int pointSize
= font
->GetPointSize();
393 #if wxFONT_SIZE_COMPATIBILITY
394 // Incorrect, but compatible with old wxWindows behaviour
395 int nHeight
= (pointSize
*ppInch
)/72;
397 // Correct for Windows compatibility
398 int nHeight
= -(int)((pointSize
*((double)ppInch
)/72.0) + 0.5);
401 wxString facename
= font
->GetFaceName();
406 //else: ff_face is a reasonable default facename for this font family
408 // deal with encoding now
409 wxNativeEncodingInfo info
;
410 wxFontEncoding encoding
= font
->GetEncoding();
411 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
414 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
415 #endif // wxUSE_FONTMAP
417 // unsupported encoding, replace with the default
418 info
.charset
= ANSI_CHARSET
;
422 if ( !info
.facename
.IsEmpty() )
424 // the facename determined by the encoding overrides everything else
425 ff_face
= info
.facename
;
428 // transfer all the data to LOGFONT
429 logFont
->lfHeight
= nHeight
;
430 logFont
->lfWidth
= 0;
431 logFont
->lfEscapement
= 0;
432 logFont
->lfOrientation
= 0;
433 logFont
->lfWeight
= ff_weight
;
434 logFont
->lfItalic
= ff_italic
;
435 logFont
->lfUnderline
= (BYTE
)font
->GetUnderlined();
436 logFont
->lfStrikeOut
= 0;
437 logFont
->lfCharSet
= info
.charset
;
438 logFont
->lfOutPrecision
= OUT_DEFAULT_PRECIS
;
439 logFont
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
440 logFont
->lfQuality
= PROOF_QUALITY
;
441 logFont
->lfPitchAndFamily
= DEFAULT_PITCH
| ff_family
;
442 wxStrncpy(logFont
->lfFaceName
, ff_face
, WXSIZEOF(logFont
->lfFaceName
));
445 wxFont
wxCreateFontFromLogFont(const LOGFONT
*logFont
)
447 wxNativeFontInfo info
;