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"
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 wxFontEncoding
wxGetFontEncFromCharSet(
177 wxFontEncoding eFontEncoding
;
183 eFontEncoding
= wxFONTENCODING_CP1250
;
187 eFontEncoding
= wxFONTENCODING_CP1252
;
191 eFontEncoding
= wxFONTENCODING_ISO8859_4
;
195 eFontEncoding
= wxFONTENCODING_CP1251
;
199 eFontEncoding
= wxFONTENCODING_ISO8859_6
;
203 eFontEncoding
= wxFONTENCODING_ISO8859_7
;
207 eFontEncoding
= wxFONTENCODING_ISO8859_8
;
211 eFontEncoding
= wxFONTENCODING_ISO8859_9
;
215 eFontEncoding
= wxFONTENCODING_ISO8859_11
;
219 eFontEncoding
= wxFONTENCODING_CP437
;
222 return eFontEncoding
;
223 } // end of wxGetNativeFontEncoding
225 bool wxTestFontEncoding(
226 const wxNativeEncodingInfo
& rInfo
232 hPS
= ::WinGetPS(HWND_DESKTOP
);
234 memset(&vLogFont
, '\0', sizeof(FATTRS
)); // all default values
235 vLogFont
.usRecordLength
= sizeof(FATTRS
);
236 vLogFont
.usCodePage
= rInfo
.charset
;
237 vLogFont
.lMaxBaselineExt
= 0L; // Outline fonts should use 0
238 vLogFont
.lAveCharWidth
= 0L; // Outline fonts should use 0
239 vLogFont
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
240 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
242 strncpy(vLogFont
.szFacename
, rInfo
.facename
.c_str(), sizeof(vLogFont
.szFacename
));
244 if (!::GpiCreateLogFont( hPS
255 } // end of wxTestFontEncoding
257 // ----------------------------------------------------------------------------
258 // wxFont <-> LOGFONT conversion
259 // ----------------------------------------------------------------------------
261 void wxConvertVectorFontSize(
268 LONG lXFontResolution
;
269 LONG lYFontResolution
;
272 hPS
= WinGetScreenPS(HWND_DESKTOP
); // Screen presentation space
275 // Query device context for the screen and then query
276 // the resolution of the device for the device context.
279 hDC
= GpiQueryDevice(hPS
);
280 DevQueryCaps( hDC
, CAPS_HORIZONTAL_FONT_RES
, (LONG
)1, &lXFontResolution
);
281 DevQueryCaps( hDC
, CAPS_VERTICAL_FONT_RES
, (LONG
)1, &lYFontResolution
);
284 // Calculate the size of the character box, based on the
285 // point size selected and the resolution of the device.
286 // The size parameters are of type FIXED, NOT int.
287 // NOTE: 1 point == 1/72 of an inch.
290 vSizef
.cx
= (FIXED
)(((fxPointSize
) / 72 ) * lXFontResolution
);
291 vSizef
.cy
= (FIXED
)(((fxPointSize
) / 72 ) * lYFontResolution
);
293 pFattrs
->lMaxBaselineExt
= MAKELONG( HIUSHORT( vSizef
.cy
), 0 );
294 pFattrs
->lAveCharWidth
= MAKELONG( HIUSHORT( vSizef
.cx
), 0 );
297 } // end of wxConvertVectorPointSize
300 LOGFONT
* pFattrs
// OS2 GPI FATTRS
301 , PFACENAMEDESC pFaceName
305 , wxString
& sFaceName
309 LONG lNumFonts
= 0L; // For system font count
310 ERRORID vError
; // For logging API errors
312 bool bInternalPS
= FALSE
; // if we have to create one
313 PFONTMETRICS pFM
= NULL
;
316 // Initial house cleaning to free data buffers and ensure we have a
317 // functional PS to work with
321 *phPS
= ::WinGetPS(HWND_DESKTOP
);
326 // Determine the number of fonts.
328 if((lNumFonts
= ::GpiQueryFonts( *phPS
329 ,QF_PUBLIC
| QF_PRIVATE
332 ,(LONG
) sizeof(FONTMETRICS
)
339 vError
= ::WinGetLastError(wxGetInstance());
340 sError
= wxPMErrorToStr(vError
);
345 // Allocate space for the font metrics.
347 pFM
= new FONTMETRICS
[lNumFonts
+ 1];
350 // Retrieve the font metrics.
353 lTemp
= ::GpiQueryFonts( *phPS
357 ,(LONG
) sizeof(FONTMETRICS
)
365 // Initialize FATTR and FACENAMEDESC
367 pFattrs
->usRecordLength
= sizeof(FATTRS
);
368 pFattrs
->fsFontUse
= FATTR_FONTUSE_OUTLINE
; // only outline fonts allowed
370 pFattrs
->lMaxBaselineExt
= pFattrs
->lAveCharWidth
= 0;
371 pFattrs
->idRegistry
= 0;
374 pFaceName
->usSize
= sizeof(FACENAMEDESC
);
375 pFaceName
->usWeightClass
= FWEIGHT_DONT_CARE
;
376 pFaceName
->usWidthClass
= FWIDTH_DONT_CARE
;
377 pFaceName
->usReserved
= 0;
378 pFaceName
->flOptions
= 0;
381 // This does the actual selection of fonts
383 wxOS2SelectMatchingFontByName( pFattrs
390 // We should now have the correct FATTRS set with the selected
391 // font, so now we need to generate an ID
393 long lNumLids
= ::GpiQueryNumberSetIds(*phPS
);
402 memset(alIds
, 0, sizeof(long) * 255);
403 if(!::GpiQuerySetIds( *phPS
411 ::WinReleasePS(*phPS
);
416 for(unsigned long LCNum
= 0; LCNum
< lNumLids
; LCNum
++)
417 if(alIds
[LCNum
] == *pflId
)
419 if(*pflId
> 254) // wow, no id available!
422 ::WinReleasePS(*phPS
);
429 // Release and delete the current font
431 ::GpiSetCharSet(*phPS
, LCID_DEFAULT
);/* release the font before deleting */
432 ::GpiDeleteSetId(*phPS
, 1L); /* delete the logical font */
435 // Now build a facestring
439 strcpy(zFacename
, pFattrs
->szFacename
);
441 if(::GpiQueryFaceString( *phPS
448 vError
= ::WinGetLastError(vHabmain
);
450 sFaceName
= zFacename
;
451 *pbInternalPS
= bInternalPS
;
454 // That's it, we now have everything we need to actually create the font
456 } // end of wxFillLogFont
458 void wxOS2SelectMatchingFontByName(
460 , PFACENAMEDESC pFaceName
463 , const wxFont
* pFont
478 char zFontFaceName
[FACESIZE
];
480 USHORT usWeightClass
;
484 for(i
= 0;i
< 16; i
++)
485 anMinDiff
[i
] = nMinDiff0
;
487 switch (pFont
->GetFamily())
490 sFaceName
= wxT("Tms Rmn");
494 sFaceName
= wxT("WarpSans");
498 sFaceName
= wxT("Tms Rmn");
502 sFaceName
= wxT("Courier") ;
506 sFaceName
= wxT("System VIO") ;
510 sFaceName
= wxT("Helv") ;
515 sFaceName
= wxT("System VIO") ;
518 switch (pFont
->GetWeight())
521 wxFAIL_MSG(_T("unknown font weight"));
523 usWeightClass
= FWEIGHT_DONT_CARE
;
527 usWeightClass
= FWEIGHT_NORMAL
;
531 usWeightClass
= FWEIGHT_LIGHT
;
535 usWeightClass
= FWEIGHT_BOLD
;
538 case wxFONTWEIGHT_MAX
:
539 usWeightClass
= FWEIGHT_ULTRA_BOLD
;
542 pFaceName
->usWeightClass
= usWeightClass
;
544 switch (pFont
->GetStyle())
548 fsSelection
= FM_SEL_ITALIC
;
549 pFaceName
->flOptions
= FTYPE_ITALIC
;
553 wxFAIL_MSG(wxT("unknown font slant"));
561 wxStrncpy(zFontFaceName
, sFaceName
.c_str(), WXSIZEOF(zFontFaceName
));
562 nPointSize
= pFont
->GetPointSize();
565 // Matching logic to find the right FM struct
568 for(i
= 0, nIs
= 0; i
< nNumFonts
; i
++)
573 anDiff
[0] = wxGpiStrcmp(pFM
[i
].szFacename
, zFontFaceName
);
574 anDiff
[1] = abs(pFM
[i
].lEmHeight
- nPointSize
);
575 anDiff
[2] = abs(pFM
[i
].usWeightClass
- usWeightClass
);
576 anDiff
[3] = abs((pFM
[i
].fsSelection
& 0x2f) - fsSelection
);
579 nEmHeight
= (int)pFM
[i
].lEmHeight
;
580 nXHeight
=(int)pFM
[i
].lXHeight
;
581 if( (nIs
& 0x01) == 0)
585 anMinDiff
[1] = anDiff
[1];
586 anMinDiff
[2] = anDiff
[2];
587 anMinDiff
[3] = anDiff
[3];
589 else if(anDiff
[3] < anMinDiff
[3])
592 anMinDiff
[3] = anDiff
[3];
594 else if(anDiff
[2] < anMinDiff
[2])
597 anMinDiff
[2] = anDiff
[2];
599 else if(anDiff
[1] < anMinDiff
[1])
602 anMinDiff
[1] = anDiff
[1];
606 else if(anDiff
[0] < anMinDiff
[0])
610 anMinDiff
[3] = anDiff
[3];
611 anMinDiff
[2] = anDiff
[2];
612 anMinDiff
[1] = anDiff
[1];
613 anMinDiff
[0] = anDiff
[0];
615 else if(anDiff
[0] == anMinDiff
[0])
617 if(anDiff
[3] < anMinDiff
[3])
620 anMinDiff
[3] = anDiff
[3];
623 else if(anDiff
[2] < anMinDiff
[2])
626 anMinDiff
[2] = anDiff
[2];
629 else if(anDiff
[1] < anMinDiff
[1])
632 anMinDiff
[1] = anDiff
[1];
639 // Fill in the FATTRS with the best match from FONTMETRICS
641 pFattrs
->usRecordLength
= sizeof(FATTRS
); // Sets size of structure
642 pFattrs
->lMatch
= pFM
[nIndex
].lMatch
; // Force match
643 pFattrs
->idRegistry
= 0;
644 pFattrs
->usCodePage
= 0;
645 pFattrs
->fsFontUse
= 0;
647 pFattrs
->lMaxBaselineExt
= 0;
648 pFattrs
->lAveCharWidth
= 0;
649 wxStrcpy(pFattrs
->szFacename
, pFM
[nIndex
].szFacename
);
650 if (pFont
->GetWeight() == wxNORMAL
)
651 pFattrs
->fsSelection
= 0;
653 pFattrs
->fsSelection
= FATTR_SEL_BOLD
;
655 if (pFont
->GetStyle() == wxITALIC
|| pFont
->GetStyle() == wxSLANT
)
656 pFattrs
->fsSelection
|= FATTR_SEL_ITALIC
;
658 if (pFont
->GetUnderlined())
659 pFattrs
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
660 } // end of wxOS2SelectMatchingFontByName
662 wxFont
wxCreateFontFromLogFont(
663 const LOGFONT
* pLogFont
664 , const PFONTMETRICS pFM
665 , PFACENAMEDESC pFaceName
668 wxNativeFontInfo vInfo
;
670 vInfo
.fa
= *pLogFont
;
672 vInfo
.fn
= *pFaceName
;
673 return wxFont(vInfo
);
674 } // end of wxCreateFontFromLogFont
713 d1
= toupper(s0
[i
]) - toupper(s1
[i
]);