1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Modified by David Webster for OS/2
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11 #define DEBUG_PRINTF(NAME) { static int raz=0; \
12 printf( #NAME " %i\n",raz); fflush(stdout); \
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 #pragma implementation "fontutil.h"
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
33 #include "wx/string.h"
38 #include "wx/os2/private.h"
40 #include "wx/fontutil.h"
41 #include "wx/fontmap.h"
43 #include "wx/tokenzr.h"
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
50 // wxNativeEncodingInfo
51 // ----------------------------------------------------------------------------
53 // convert to/from the string representation: format is
54 // encodingid;facename[;charset]
56 bool wxNativeEncodingInfo::FromString(
60 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
61 wxString sEncid
= vTokenizer
.GetNextToken();
64 if (!sEncid
.ToLong(&lEnc
))
66 encoding
= (wxFontEncoding
)lEnc
;
67 facename
= vTokenizer
.GetNextToken();
71 wxString sTmp
= vTokenizer
.GetNextToken();
79 if ( wxSscanf(sTmp
, _T("%u"), &charset
) != 1 )
81 // should be a number!
86 } // end of wxNativeEncodingInfo::FromString
88 wxString
wxNativeEncodingInfo::ToString() const
92 sStr
<< (long)encoding
<< _T(';') << facename
;
96 sStr
<< _T(';') << charset
;
99 } // end of wxNativeEncodingInfo::ToString
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 bool wxGetNativeFontEncoding(
106 wxFontEncoding vEncoding
107 , wxNativeEncodingInfo
* pInfo
110 wxCHECK_MSG(pInfo
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
111 if (vEncoding
== wxFONTENCODING_DEFAULT
)
113 vEncoding
= wxFont::GetDefaultEncoding();
117 case wxFONTENCODING_ISO8859_1
:
118 case wxFONTENCODING_ISO8859_15
:
119 case wxFONTENCODING_CP1250
:
120 pInfo
->charset
= 1250;
123 case wxFONTENCODING_ISO8859_2
:
124 case wxFONTENCODING_CP1252
:
125 pInfo
->charset
= 1252;
128 case wxFONTENCODING_ISO8859_4
:
129 case wxFONTENCODING_ISO8859_10
:
130 pInfo
->charset
= 921; // what is baltic?
133 case wxFONTENCODING_ISO8859_5
:
134 case wxFONTENCODING_CP1251
:
135 pInfo
->charset
= 1251;
138 case wxFONTENCODING_ISO8859_6
:
139 pInfo
->charset
= 864;
142 case wxFONTENCODING_ISO8859_7
:
143 pInfo
->charset
= 869;
146 case wxFONTENCODING_ISO8859_8
:
147 pInfo
->charset
= 862;
150 case wxFONTENCODING_ISO8859_9
:
151 pInfo
->charset
= 857;
154 case wxFONTENCODING_ISO8859_11
:
155 pInfo
->charset
= 874; // what is thai
158 case wxFONTENCODING_CP437
:
159 pInfo
->charset
= 437;
163 wxFAIL_MSG(wxT("unsupported encoding"));
166 case wxFONTENCODING_SYSTEM
:
167 pInfo
->charset
= 850;
171 } // end of wxGetNativeFontEncoding
173 bool wxTestFontEncoding(
174 const wxNativeEncodingInfo
& rInfo
180 hPS
= ::WinGetPS(HWND_DESKTOP
);
182 memset(&vLogFont
, '\0', sizeof(FATTRS
)); // all default values
183 vLogFont
.usRecordLength
= sizeof(FATTRS
);
184 vLogFont
.usCodePage
= rInfo
.charset
;
185 vLogFont
.lMaxBaselineExt
= 0L; // Outline fonts should use 0
186 vLogFont
.lAveCharWidth
= 0L; // Outline fonts should use 0
187 vLogFont
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
188 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
190 strncpy(vLogFont
.szFacename
, rInfo
.facename
.c_str(), sizeof(vLogFont
.szFacename
));
192 if (!::GpiCreateLogFont( hPS
203 } // end of wxTestFontEncoding
205 // ----------------------------------------------------------------------------
206 // wxFont <-> LOGFONT conversion
207 // ----------------------------------------------------------------------------
210 LOGFONT
* pLogFont
// OS2 GPI FATTRS
211 , PFACENAMEDESC pFaceName
212 , const wxFont
* pFont
219 pLogFont
->fsSelection
= 0;
220 pLogFont
->fsSelection
= FATTR_SEL_OUTLINE
; // we will alway use only outlines
221 pFaceName
->usWeightClass
= 0;
222 pFaceName
->flOptions
= 0;
223 switch (pFont
->GetFamily())
226 sFace
= _T("Script");
231 sFace
= _T("Times New Roman");
236 sFace
= _T("Courier New");
240 sFace
= _T("WarpSans");
248 switch (pFont
->GetWeight())
251 wxFAIL_MSG(_T("unknown font weight"));
252 uWeight
= FWEIGHT_DONT_CARE
;
256 uWeight
= FWEIGHT_NORMAL
;
260 uWeight
= FWEIGHT_LIGHT
;
264 uWeight
= FWEIGHT_BOLD
;
265 pLogFont
->fsSelection
|= FATTR_SEL_BOLD
;
268 case wxFONTWEIGHT_MAX
:
269 uWeight
= FWEIGHT_ULTRA_BOLD
;
270 pLogFont
->fsSelection
|= FATTR_SEL_BOLD
;
273 pFaceName
->usWeightClass
|= uWeight
;
275 switch (pFont
->GetStyle())
279 nItalic
= FTYPE_ITALIC
;
280 pLogFont
->fsSelection
|= FATTR_SEL_ITALIC
;
284 wxFAIL_MSG(wxT("unknown font slant"));
291 pFaceName
->flOptions
|= nItalic
;
292 if(pFont
->GetUnderlined())
293 pLogFont
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
296 // In PM a font's height is expressed in points. A point equals
297 // approximately 1/72 of an inch. We'll assume for now that,
298 // like Windows, that fonts are 96 dpi.
300 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
301 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
302 LONG lStart
= CAPS_FAMILY
;
303 LONG lCount
= CAPS_VERTICAL_RESOLUTION
;
304 LONG alArray
[CAPS_VERTICAL_RESOLUTION
];
309 ::DevQueryCaps(hDC
, lStart
, lCount
, alArray
);
310 lRes
= alArray
[CAPS_VERTICAL_RESOLUTION
-1];
312 nPpInch
= (int)(lRes
/39.6); // lres is in pixels per meter
316 int nHeight
= (pFont
->GetPointSize() * nPpInch
/72);
317 wxString sFacename
= pFont
->GetFaceName();
323 //else: ff_face is a reasonable default facename for this font family
326 // Deal with encoding now
328 wxNativeEncodingInfo vInfo
;
329 wxFontEncoding vEncoding
= pFont
->GetEncoding();
331 if (!wxGetNativeFontEncoding( vEncoding
336 if (!wxTheFontMapper
->GetAltForEncoding(vEncoding
, &vInfo
))
337 #endif // wxUSE_FONTMAP
340 // Unsupported encoding, replace with the default
346 if (!vInfo
.facename
.IsEmpty() )
349 // The facename determined by the encoding overrides everything else
351 sFace
= vInfo
.facename
;
355 // Transfer all the data to LOGFONT
357 pLogFont
->usRecordLength
= sizeof(FATTRS
);
358 wxStrcpy(pLogFont
->szFacename
, sFace
.c_str());
359 pLogFont
->usCodePage
= vInfo
.charset
;
360 pLogFont
->fsFontUse
|= FATTR_FONTUSE_OUTLINE
|
361 FATTR_FONTUSE_TRANSFORMABLE
;
362 } // end of wxFillLogFont
364 wxFont
wxCreateFontFromLogFont(
365 const LOGFONT
* pLogFont
366 , const PFONTMETRICS pFM
367 , PFACENAMEDESC pFaceName
371 // Extract family from facename
375 if (strcmp(pLogFont
->szFacename
, "Times New Roman") == 0)
376 nFontFamily
= wxROMAN
;
377 else if (strcmp(pLogFont
->szFacename
, "WarpSans") == 0)
378 nFontFamily
= wxSWISS
;
379 else if (strcmp(pLogFont
->szFacename
, "Script") == 0)
380 nFontFamily
= wxSCRIPT
;
381 else if (strcmp(pLogFont
->szFacename
, "Courier New") == 0)
382 nFontFamily
= wxMODERN
;
384 nFontFamily
= wxSWISS
;
389 int nFontWeight
= wxNORMAL
;
391 switch (pFaceName
->usWeightClass
)
394 nFontWeight
= wxLIGHT
;
399 nFontWeight
= wxNORMAL
;
403 nFontWeight
= wxBOLD
;
409 if(pLogFont
->fsSelection
& FATTR_SEL_ITALIC
)
410 nFontStyle
= wxITALIC
;
412 nFontStyle
= wxNORMAL
;
414 bool bFontUnderline
= (pLogFont
->fsSelection
& FATTR_SEL_UNDERSCORE
);
415 wxString sFontFace
= pLogFont
->szFacename
;
416 int nFontPoints
= pFM
->lEmHeight
;
417 wxFontEncoding vFontEncoding
;
419 switch (pLogFont
->usCodePage
)
422 wxFAIL_MSG(wxT("unsupported charset"));
426 vFontEncoding
= wxFONTENCODING_CP1252
;
430 vFontEncoding
= wxFONTENCODING_CP1250
;
434 vFontEncoding
= wxFONTENCODING_CP1257
;
438 vFontEncoding
= wxFONTENCODING_CP1251
;
442 vFontEncoding
= wxFONTENCODING_CP1256
;
446 vFontEncoding
= wxFONTENCODING_CP1253
;
450 vFontEncoding
= wxFONTENCODING_CP1255
;
454 vFontEncoding
= wxFONTENCODING_CP1254
;
458 vFontEncoding
= wxFONTENCODING_CP437
;
462 vFontEncoding
= wxFONTENCODING_CP437
;
466 return wxFont( nFontPoints
474 } // end of wxCreateFontFromLogFont
513 d1
= toupper(s0
[i
]) - toupper(s1
[i
]);