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"
32 #include "wx/string.h"
37 #include "wx/os2/private.h"
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
42 #include "wx/tokenzr.h"
44 // ============================================================================
46 // ============================================================================
48 // ----------------------------------------------------------------------------
49 // wxNativeEncodingInfo
50 // ----------------------------------------------------------------------------
52 // convert to/from the string representation: format is
53 // encodingid;facename[;charset]
55 bool wxNativeEncodingInfo::FromString(
59 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
60 wxString sEncid
= vTokenizer
.GetNextToken();
63 if (!sEncid
.ToLong(&lEnc
))
65 encoding
= (wxFontEncoding
)lEnc
;
66 facename
= vTokenizer
.GetNextToken();
70 wxString sTmp
= vTokenizer
.GetNextToken();
78 if ( wxSscanf(sTmp
, _T("%u"), &charset
) != 1 )
80 // should be a number!
85 } // end of wxNativeEncodingInfo::FromString
87 wxString
wxNativeEncodingInfo::ToString() const
91 sStr
<< (long)encoding
<< _T(';') << facename
;
95 sStr
<< _T(';') << charset
;
98 } // end of wxNativeEncodingInfo::ToString
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 bool wxGetNativeFontEncoding(
105 wxFontEncoding vEncoding
106 , wxNativeEncodingInfo
* pInfo
109 wxCHECK_MSG(pInfo
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
110 if (vEncoding
== wxFONTENCODING_DEFAULT
)
112 vEncoding
= wxFont::GetDefaultEncoding();
116 case wxFONTENCODING_ISO8859_1
:
117 case wxFONTENCODING_ISO8859_15
:
118 case wxFONTENCODING_CP1250
:
119 pInfo
->charset
= 1250;
122 case wxFONTENCODING_ISO8859_2
:
123 case wxFONTENCODING_CP1252
:
124 pInfo
->charset
= 1252;
127 case wxFONTENCODING_ISO8859_4
:
128 case wxFONTENCODING_ISO8859_10
:
129 pInfo
->charset
= 921; // what is baltic?
132 case wxFONTENCODING_ISO8859_5
:
133 case wxFONTENCODING_CP1251
:
134 pInfo
->charset
= 1251;
137 case wxFONTENCODING_ISO8859_6
:
138 pInfo
->charset
= 864;
141 case wxFONTENCODING_ISO8859_7
:
142 pInfo
->charset
= 869;
145 case wxFONTENCODING_ISO8859_8
:
146 pInfo
->charset
= 862;
149 case wxFONTENCODING_ISO8859_9
:
150 pInfo
->charset
= 857;
153 case wxFONTENCODING_ISO8859_11
:
154 pInfo
->charset
= 874; // what is thai
157 case wxFONTENCODING_CP437
:
158 pInfo
->charset
= 437;
162 wxFAIL_MSG(wxT("unsupported encoding"));
165 case wxFONTENCODING_SYSTEM
:
166 pInfo
->charset
= 850;
170 } // end of wxGetNativeFontEncoding
172 bool wxTestFontEncoding(
173 const wxNativeEncodingInfo
& rInfo
179 hPS
= ::WinGetPS(HWND_DESKTOP
);
181 memset(&vLogFont
, '\0', sizeof(FATTRS
)); // all default values
182 vLogFont
.usRecordLength
= sizeof(FATTRS
);
183 vLogFont
.usCodePage
= rInfo
.charset
;
184 vLogFont
.lMaxBaselineExt
= 0L; // Outline fonts should use 0
185 vLogFont
.lAveCharWidth
= 0L; // Outline fonts should use 0
186 vLogFont
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
187 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
189 strncpy(vLogFont
.szFacename
, rInfo
.facename
.c_str(), sizeof(vLogFont
.szFacename
));
191 if (!::GpiCreateLogFont( hPS
202 } // end of wxTestFontEncoding
204 // ----------------------------------------------------------------------------
205 // wxFont <-> LOGFONT conversion
206 // ----------------------------------------------------------------------------
209 LOGFONT
* pLogFont
// OS2 GPI FATTRS
210 , PFACENAMEDESC pFaceName
211 , const wxFont
* pFont
218 pLogFont
->fsSelection
= 0;
219 pLogFont
->fsSelection
= FATTR_SEL_OUTLINE
; // we will alway use only outlines
220 pFaceName
->usWeightClass
= 0;
221 pFaceName
->flOptions
= 0;
222 switch (pFont
->GetFamily())
225 sFace
= _T("Script");
230 sFace
= _T("Times New Roman");
235 sFace
= _T("Courier New");
239 sFace
= _T("WarpSans");
247 switch (pFont
->GetWeight())
250 wxFAIL_MSG(_T("unknown font weight"));
251 uWeight
= FWEIGHT_DONT_CARE
;
255 uWeight
= FWEIGHT_NORMAL
;
259 uWeight
= FWEIGHT_LIGHT
;
263 uWeight
= FWEIGHT_BOLD
;
264 pLogFont
->fsSelection
|= FATTR_SEL_BOLD
;
267 case wxFONTWEIGHT_MAX
:
268 uWeight
= FWEIGHT_ULTRA_BOLD
;
269 pLogFont
->fsSelection
|= FATTR_SEL_BOLD
;
272 pFaceName
->usWeightClass
|= uWeight
;
274 switch (pFont
->GetStyle())
278 nItalic
= FTYPE_ITALIC
;
279 pLogFont
->fsSelection
|= FATTR_SEL_ITALIC
;
283 wxFAIL_MSG(wxT("unknown font slant"));
290 pFaceName
->flOptions
|= nItalic
;
291 if(pFont
->GetUnderlined())
292 pLogFont
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
295 // In PM a font's height is expressed in points. A point equals
296 // approximately 1/72 of an inch. We'll assume for now that,
297 // like Windows, that fonts are 96 dpi.
299 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
300 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
301 LONG lStart
= CAPS_FAMILY
;
302 LONG lCount
= CAPS_VERTICAL_RESOLUTION
;
303 LONG alArray
[CAPS_VERTICAL_RESOLUTION
];
308 ::DevQueryCaps(hDC
, lStart
, lCount
, alArray
);
309 lRes
= alArray
[CAPS_VERTICAL_RESOLUTION
-1];
311 nPpInch
= (int)(lRes
/39.6); // lres is in pixels per meter
315 int nHeight
= (pFont
->GetPointSize() * nPpInch
/72);
316 wxString sFacename
= pFont
->GetFaceName();
322 //else: ff_face is a reasonable default facename for this font family
325 // Deal with encoding now
327 wxNativeEncodingInfo vInfo
;
328 wxFontEncoding vEncoding
= pFont
->GetEncoding();
330 if (!wxGetNativeFontEncoding( vEncoding
334 if ( !wxTheFontMapper
->GetAltForEncoding( vEncoding
339 // Unsupported encoding, replace with the default
345 if (!vInfo
.facename
.IsEmpty() )
348 // The facename determined by the encoding overrides everything else
350 sFace
= vInfo
.facename
;
354 // Transfer all the data to LOGFONT
356 pLogFont
->usRecordLength
= sizeof(FATTRS
);
357 wxStrcpy(pLogFont
->szFacename
, sFace
.c_str());
358 pLogFont
->usCodePage
= vInfo
.charset
;
359 pLogFont
->fsFontUse
|= FATTR_FONTUSE_OUTLINE
|
360 FATTR_FONTUSE_TRANSFORMABLE
;
361 } // end of wxFillLogFont
363 wxFont
wxCreateFontFromLogFont(
364 const LOGFONT
* pLogFont
365 , const PFONTMETRICS pFM
366 , PFACENAMEDESC pFaceName
370 // Extract family from facename
374 if (strcmp(pLogFont
->szFacename
, "Times New Roman") == 0)
375 nFontFamily
= wxROMAN
;
376 else if (strcmp(pLogFont
->szFacename
, "WarpSans") == 0)
377 nFontFamily
= wxSWISS
;
378 else if (strcmp(pLogFont
->szFacename
, "Script") == 0)
379 nFontFamily
= wxSCRIPT
;
380 else if (strcmp(pLogFont
->szFacename
, "Courier New") == 0)
381 nFontFamily
= wxMODERN
;
383 nFontFamily
= wxSWISS
;
388 int nFontWeight
= wxNORMAL
;
390 switch (pFaceName
->usWeightClass
)
393 nFontWeight
= wxLIGHT
;
398 nFontWeight
= wxNORMAL
;
402 nFontWeight
= wxBOLD
;
408 if(pLogFont
->fsSelection
& FATTR_SEL_ITALIC
)
409 nFontStyle
= wxITALIC
;
411 nFontStyle
= wxNORMAL
;
413 bool bFontUnderline
= (pLogFont
->fsSelection
& FATTR_SEL_UNDERSCORE
);
414 wxString sFontFace
= pLogFont
->szFacename
;
415 int nFontPoints
= pFM
->lEmHeight
;
416 wxFontEncoding vFontEncoding
;
418 switch (pLogFont
->usCodePage
)
421 wxFAIL_MSG(wxT("unsupported charset"));
425 vFontEncoding
= wxFONTENCODING_CP1252
;
429 vFontEncoding
= wxFONTENCODING_CP1250
;
433 vFontEncoding
= wxFONTENCODING_CP1257
;
437 vFontEncoding
= wxFONTENCODING_CP1251
;
441 vFontEncoding
= wxFONTENCODING_CP1256
;
445 vFontEncoding
= wxFONTENCODING_CP1253
;
449 vFontEncoding
= wxFONTENCODING_CP1255
;
453 vFontEncoding
= wxFONTENCODING_CP1254
;
457 vFontEncoding
= wxFONTENCODING_CP437
;
461 vFontEncoding
= wxFONTENCODING_CP437
;
465 return wxFont( nFontPoints
473 } // end of wxCreateFontFromLogFont
512 d1
= toupper(s0
[i
]) - toupper(s1
[i
]);