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 licence
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"
42 #include "wx/encinfo.h"
44 #include "wx/tokenzr.h"
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
51 // wxNativeEncodingInfo
52 // ----------------------------------------------------------------------------
54 // convert to/from the string representation: format is
55 // encodingid;facename[;charset]
57 bool wxNativeEncodingInfo::FromString(
61 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
62 wxString sEncid
= vTokenizer
.GetNextToken();
65 if (!sEncid
.ToLong(&lEnc
))
67 encoding
= (wxFontEncoding
)lEnc
;
68 facename
= vTokenizer
.GetNextToken();
72 wxString sTmp
= vTokenizer
.GetNextToken();
80 if ( wxSscanf(sTmp
, _T("%u"), &charset
) != 1 )
82 // should be a number!
87 } // end of wxNativeEncodingInfo::FromString
89 wxString
wxNativeEncodingInfo::ToString() const
93 sStr
<< (long)encoding
<< _T(';') << facename
;
97 sStr
<< _T(';') << charset
;
100 } // end of wxNativeEncodingInfo::ToString
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 bool wxGetNativeFontEncoding(
107 wxFontEncoding vEncoding
108 , wxNativeEncodingInfo
* pInfo
111 wxCHECK_MSG(pInfo
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
112 if (vEncoding
== wxFONTENCODING_DEFAULT
)
114 vEncoding
= wxFont::GetDefaultEncoding();
118 case wxFONTENCODING_ISO8859_1
:
119 case wxFONTENCODING_ISO8859_15
:
120 case wxFONTENCODING_CP1250
:
121 pInfo
->charset
= 1250;
124 case wxFONTENCODING_ISO8859_2
:
125 case wxFONTENCODING_CP1252
:
126 pInfo
->charset
= 1252;
129 case wxFONTENCODING_ISO8859_4
:
130 case wxFONTENCODING_ISO8859_10
:
131 pInfo
->charset
= 921; // what is baltic?
134 case wxFONTENCODING_ISO8859_5
:
135 case wxFONTENCODING_CP1251
:
136 pInfo
->charset
= 1251;
139 case wxFONTENCODING_ISO8859_6
:
140 pInfo
->charset
= 864;
143 case wxFONTENCODING_ISO8859_7
:
144 pInfo
->charset
= 869;
147 case wxFONTENCODING_ISO8859_8
:
148 pInfo
->charset
= 862;
151 case wxFONTENCODING_ISO8859_9
:
152 pInfo
->charset
= 857;
155 case wxFONTENCODING_ISO8859_11
:
156 pInfo
->charset
= 874; // what is thai
159 case wxFONTENCODING_CP437
:
160 pInfo
->charset
= 437;
164 wxFAIL_MSG(wxT("unsupported encoding"));
167 case wxFONTENCODING_SYSTEM
:
168 pInfo
->charset
= 850;
172 } // end of wxGetNativeFontEncoding
174 wxFontEncoding
wxGetFontEncFromCharSet(
178 wxFontEncoding eFontEncoding
;
184 eFontEncoding
= wxFONTENCODING_CP1250
;
188 eFontEncoding
= wxFONTENCODING_CP1252
;
192 eFontEncoding
= wxFONTENCODING_ISO8859_4
;
196 eFontEncoding
= wxFONTENCODING_CP1251
;
200 eFontEncoding
= wxFONTENCODING_ISO8859_6
;
204 eFontEncoding
= wxFONTENCODING_ISO8859_7
;
208 eFontEncoding
= wxFONTENCODING_ISO8859_8
;
212 eFontEncoding
= wxFONTENCODING_ISO8859_9
;
216 eFontEncoding
= wxFONTENCODING_ISO8859_11
;
220 eFontEncoding
= wxFONTENCODING_CP437
;
223 return eFontEncoding
;
224 } // end of wxGetNativeFontEncoding
226 bool wxTestFontEncoding(
227 const wxNativeEncodingInfo
& rInfo
233 hPS
= ::WinGetPS(HWND_DESKTOP
);
235 memset(&vLogFont
, '\0', sizeof(FATTRS
)); // all default values
236 vLogFont
.usRecordLength
= sizeof(FATTRS
);
237 vLogFont
.usCodePage
= rInfo
.charset
;
238 vLogFont
.lMaxBaselineExt
= 0L; // Outline fonts should use 0
239 vLogFont
.lAveCharWidth
= 0L; // Outline fonts should use 0
240 vLogFont
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
241 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
243 strncpy(vLogFont
.szFacename
, rInfo
.facename
.c_str(), sizeof(vLogFont
.szFacename
));
245 if (!::GpiCreateLogFont( hPS
256 } // end of wxTestFontEncoding
258 // ----------------------------------------------------------------------------
259 // wxFont <-> LOGFONT conversion
260 // ----------------------------------------------------------------------------
262 void wxConvertVectorFontSize(
269 LONG lXFontResolution
;
270 LONG lYFontResolution
;
273 hPS
= WinGetScreenPS(HWND_DESKTOP
); // Screen presentation space
276 // Query device context for the screen and then query
277 // the resolution of the device for the device context.
280 hDC
= GpiQueryDevice(hPS
);
281 DevQueryCaps( hDC
, CAPS_HORIZONTAL_FONT_RES
, (LONG
)1, &lXFontResolution
);
282 DevQueryCaps( hDC
, CAPS_VERTICAL_FONT_RES
, (LONG
)1, &lYFontResolution
);
285 // Calculate the size of the character box, based on the
286 // point size selected and the resolution of the device.
287 // The size parameters are of type FIXED, NOT int.
288 // NOTE: 1 point == 1/72 of an inch.
291 vSizef
.cx
= (FIXED
)(((fxPointSize
) / 72 ) * lXFontResolution
);
292 vSizef
.cy
= (FIXED
)(((fxPointSize
) / 72 ) * lYFontResolution
);
294 pFattrs
->lMaxBaselineExt
= MAKELONG( HIUSHORT( vSizef
.cy
), 0 );
295 pFattrs
->lAveCharWidth
= MAKELONG( HIUSHORT( vSizef
.cx
), 0 );
298 } // end of wxConvertVectorPointSize
301 LOGFONT
* pFattrs
// OS2 GPI FATTRS
302 , PFACENAMEDESC pFaceName
306 , wxString
& sFaceName
310 LONG lNumFonts
= 0L; // For system font count
311 ERRORID vError
; // For logging API errors
313 bool bInternalPS
= FALSE
; // if we have to create one
314 PFONTMETRICS pFM
= NULL
;
317 // Initial house cleaning to free data buffers and ensure we have a
318 // functional PS to work with
322 *phPS
= ::WinGetPS(HWND_DESKTOP
);
327 // Determine the number of fonts.
329 if((lNumFonts
= ::GpiQueryFonts( *phPS
330 ,QF_PUBLIC
| QF_PRIVATE
333 ,(LONG
) sizeof(FONTMETRICS
)
340 vError
= ::WinGetLastError(wxGetInstance());
341 sError
= wxPMErrorToStr(vError
);
346 // Allocate space for the font metrics.
348 pFM
= new FONTMETRICS
[lNumFonts
+ 1];
351 // Retrieve the font metrics.
354 lTemp
= ::GpiQueryFonts( *phPS
358 ,(LONG
) sizeof(FONTMETRICS
)
366 // Initialize FATTR and FACENAMEDESC
368 pFattrs
->usRecordLength
= sizeof(FATTRS
);
369 pFattrs
->fsFontUse
= FATTR_FONTUSE_OUTLINE
; // only outline fonts allowed
371 pFattrs
->lMaxBaselineExt
= pFattrs
->lAveCharWidth
= 0;
372 pFattrs
->idRegistry
= 0;
375 pFaceName
->usSize
= sizeof(FACENAMEDESC
);
376 pFaceName
->usWeightClass
= FWEIGHT_DONT_CARE
;
377 pFaceName
->usWidthClass
= FWIDTH_DONT_CARE
;
378 pFaceName
->usReserved
= 0;
379 pFaceName
->flOptions
= 0;
382 // This does the actual selection of fonts
384 wxOS2SelectMatchingFontByName( pFattrs
391 // We should now have the correct FATTRS set with the selected
392 // font, so now we need to generate an ID
394 long lNumLids
= ::GpiQueryNumberSetIds(*phPS
);
403 memset(alIds
, 0, sizeof(long) * 255);
404 if(!::GpiQuerySetIds( *phPS
412 ::WinReleasePS(*phPS
);
417 for(unsigned long LCNum
= 0; LCNum
< lNumLids
; LCNum
++)
418 if(alIds
[LCNum
] == *pflId
)
420 if(*pflId
> 254) // wow, no id available!
423 ::WinReleasePS(*phPS
);
430 // Release and delete the current font
432 ::GpiSetCharSet(*phPS
, LCID_DEFAULT
);/* release the font before deleting */
433 ::GpiDeleteSetId(*phPS
, 1L); /* delete the logical font */
436 // Now build a facestring
440 strcpy(zFacename
, pFattrs
->szFacename
);
442 if(::GpiQueryFaceString( *phPS
449 vError
= ::WinGetLastError(vHabmain
);
451 sFaceName
= zFacename
;
452 *pbInternalPS
= bInternalPS
;
455 // That's it, we now have everything we need to actually create the font
457 } // end of wxFillLogFont
459 void wxOS2SelectMatchingFontByName(
461 , PFACENAMEDESC pFaceName
464 , const wxFont
* pFont
479 char zFontFaceName
[FACESIZE
];
481 USHORT usWeightClass
;
485 for(i
= 0;i
< 16; i
++)
486 anMinDiff
[i
] = nMinDiff0
;
488 switch (pFont
->GetFamily())
491 sFaceName
= wxT("Tms Rmn");
495 sFaceName
= wxT("WarpSans");
499 sFaceName
= wxT("Tms Rmn");
503 sFaceName
= wxT("Courier") ;
507 sFaceName
= wxT("System VIO") ;
511 sFaceName
= wxT("Helv") ;
516 sFaceName
= wxT("System VIO") ;
519 switch (pFont
->GetWeight())
522 wxFAIL_MSG(_T("unknown font weight"));
524 usWeightClass
= FWEIGHT_DONT_CARE
;
528 usWeightClass
= FWEIGHT_NORMAL
;
532 usWeightClass
= FWEIGHT_LIGHT
;
536 usWeightClass
= FWEIGHT_BOLD
;
539 case wxFONTWEIGHT_MAX
:
540 usWeightClass
= FWEIGHT_ULTRA_BOLD
;
543 pFaceName
->usWeightClass
= usWeightClass
;
545 switch (pFont
->GetStyle())
549 fsSelection
= FM_SEL_ITALIC
;
550 pFaceName
->flOptions
= FTYPE_ITALIC
;
554 wxFAIL_MSG(wxT("unknown font slant"));
562 wxStrncpy(zFontFaceName
, sFaceName
.c_str(), WXSIZEOF(zFontFaceName
));
563 nPointSize
= pFont
->GetPointSize();
566 // Matching logic to find the right FM struct
569 for(i
= 0, nIs
= 0; i
< nNumFonts
; i
++)
574 anDiff
[0] = wxGpiStrcmp(pFM
[i
].szFacename
, zFontFaceName
);
575 anDiff
[1] = abs(pFM
[i
].lEmHeight
- nPointSize
);
576 anDiff
[2] = abs(pFM
[i
].usWeightClass
- usWeightClass
);
577 anDiff
[3] = abs((pFM
[i
].fsSelection
& 0x2f) - fsSelection
);
580 nEmHeight
= (int)pFM
[i
].lEmHeight
;
581 nXHeight
=(int)pFM
[i
].lXHeight
;
582 if( (nIs
& 0x01) == 0)
586 anMinDiff
[1] = anDiff
[1];
587 anMinDiff
[2] = anDiff
[2];
588 anMinDiff
[3] = anDiff
[3];
590 else if(anDiff
[3] < anMinDiff
[3])
593 anMinDiff
[3] = anDiff
[3];
595 else if(anDiff
[2] < anMinDiff
[2])
598 anMinDiff
[2] = anDiff
[2];
600 else if(anDiff
[1] < anMinDiff
[1])
603 anMinDiff
[1] = anDiff
[1];
607 else if(anDiff
[0] < anMinDiff
[0])
611 anMinDiff
[3] = anDiff
[3];
612 anMinDiff
[2] = anDiff
[2];
613 anMinDiff
[1] = anDiff
[1];
614 anMinDiff
[0] = anDiff
[0];
616 else if(anDiff
[0] == anMinDiff
[0])
618 if(anDiff
[3] < anMinDiff
[3])
621 anMinDiff
[3] = anDiff
[3];
624 else if(anDiff
[2] < anMinDiff
[2])
627 anMinDiff
[2] = anDiff
[2];
630 else if(anDiff
[1] < anMinDiff
[1])
633 anMinDiff
[1] = anDiff
[1];
640 // Fill in the FATTRS with the best match from FONTMETRICS
642 pFattrs
->usRecordLength
= sizeof(FATTRS
); // Sets size of structure
643 pFattrs
->lMatch
= pFM
[nIndex
].lMatch
; // Force match
644 pFattrs
->idRegistry
= 0;
645 pFattrs
->usCodePage
= 0;
646 pFattrs
->fsFontUse
= 0;
648 pFattrs
->lMaxBaselineExt
= 0;
649 pFattrs
->lAveCharWidth
= 0;
650 wxStrcpy(pFattrs
->szFacename
, pFM
[nIndex
].szFacename
);
651 if (pFont
->GetWeight() == wxNORMAL
)
652 pFattrs
->fsSelection
= 0;
654 pFattrs
->fsSelection
= FATTR_SEL_BOLD
;
656 if (pFont
->GetStyle() == wxITALIC
|| pFont
->GetStyle() == wxSLANT
)
657 pFattrs
->fsSelection
|= FATTR_SEL_ITALIC
;
659 if (pFont
->GetUnderlined())
660 pFattrs
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
661 } // end of wxOS2SelectMatchingFontByName
663 wxFont
wxCreateFontFromLogFont(
664 const LOGFONT
* pLogFont
665 , const PFONTMETRICS pFM
666 , PFACENAMEDESC pFaceName
669 wxNativeFontInfo vInfo
;
671 vInfo
.fa
= *pLogFont
;
673 vInfo
.fn
= *pFaceName
;
674 return wxFont(vInfo
);
675 } // end of wxCreateFontFromLogFont
714 d1
= toupper(s0
[i
]) - toupper(s1
[i
]);