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 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 // ----------------------------------------------------------------------------
262 LOGFONT
* pFattrs
// OS2 GPI FATTRS
263 , PFACENAMEDESC pFaceName
267 , wxString
& sFaceName
271 LONG lNumFonts
= 0L; // For system font count
272 ERRORID vError
; // For logging API errors
274 bool bInternalPS
= FALSE
; // if we have to create one
275 PFONTMETRICS pFM
= NULL
;
278 // Initial house cleaning to free data buffers and ensure we have a
279 // functional PS to work with
283 *phPS
= ::WinGetPS(HWND_DESKTOP
);
288 // Determine the number of fonts.
290 if((lNumFonts
= ::GpiQueryFonts( *phPS
291 ,QF_PUBLIC
| QF_PRIVATE
294 ,(LONG
) sizeof(FONTMETRICS
)
301 vError
= ::WinGetLastError(wxGetInstance());
302 sError
= wxPMErrorToStr(vError
);
307 // Allocate space for the font metrics.
309 pFM
= new FONTMETRICS
[lNumFonts
+ 1];
312 // Retrieve the font metrics.
315 lTemp
= ::GpiQueryFonts( *phPS
319 ,(LONG
) sizeof(FONTMETRICS
)
327 // Initialize FATTR and FACENAMEDESC
329 pFattrs
->usRecordLength
= sizeof(FATTRS
);
330 pFattrs
->fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
331 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
333 pFattrs
->lMaxBaselineExt
= pFattrs
->lAveCharWidth
= 0;
334 pFattrs
->idRegistry
= 0;
337 pFaceName
->usSize
= sizeof(FACENAMEDESC
);
338 pFaceName
->usWeightClass
= FWEIGHT_DONT_CARE
;
339 pFaceName
->usWidthClass
= FWIDTH_DONT_CARE
;
340 pFaceName
->usReserved
= 0;
341 pFaceName
->flOptions
= 0;
344 // This does the actual selection of fonts
346 wxOS2SelectMatchingFontByName( pFattrs
353 // We should now have the correct FATTRS set with the selected
354 // font, so now we need to generate an ID
356 long lNumLids
= ::GpiQueryNumberSetIds(*phPS
);
365 memset(alIds
, 0, sizeof(long) * 255);
366 if(!::GpiQuerySetIds( *phPS
374 ::WinReleasePS(*phPS
);
379 for(unsigned long LCNum
= 0; LCNum
< lNumLids
; LCNum
++)
380 if(alIds
[LCNum
] == *pflId
)
382 if(*pflId
> 254) // wow, no id available!
385 ::WinReleasePS(*phPS
);
392 // Release and delete the current font
394 ::GpiSetCharSet(*phPS
, LCID_DEFAULT
);/* release the font before deleting */
395 ::GpiDeleteSetId(*phPS
, 1L); /* delete the logical font */
398 // Now build a facestring
402 strcpy(zFacename
, pFattrs
->szFacename
);
404 if(::GpiQueryFaceString( *phPS
411 vError
= ::WinGetLastError(vHabmain
);
413 sFaceName
= zFacename
;
414 *pbInternalPS
= bInternalPS
;
417 // That's it, we now have everything we need to actually create the font
419 } // end of wxFillLogFont
421 void wxOS2SelectMatchingFontByName(
423 , PFACENAMEDESC pFaceName
426 , const wxFont
* pFont
441 char zFontFaceName
[FACESIZE
];
443 USHORT usWeightClass
;
447 for(i
= 0;i
< 16; i
++)
448 anMinDiff
[i
] = nMinDiff0
;
450 switch (pFont
->GetFamily())
453 sFaceName
= wxT("Script");
457 sFaceName
= wxT("WarpSans");
461 sFaceName
= wxT("Times New Roman");
465 sFaceName
= wxT("Courier New") ;
469 sFaceName
= wxT("Courier New") ;
473 sFaceName
= wxT("Helv") ;
478 sFaceName
= wxT("System VIO") ;
481 switch (pFont
->GetWeight())
484 wxFAIL_MSG(_T("unknown font weight"));
486 usWeightClass
= FWEIGHT_DONT_CARE
;
490 usWeightClass
= FWEIGHT_NORMAL
;
494 usWeightClass
= FWEIGHT_LIGHT
;
498 usWeightClass
= FWEIGHT_BOLD
;
501 case wxFONTWEIGHT_MAX
:
502 usWeightClass
= FWEIGHT_ULTRA_BOLD
;
505 pFaceName
->usWeightClass
= usWeightClass
;
507 switch (pFont
->GetStyle())
511 fsSelection
= FM_SEL_ITALIC
;
512 pFaceName
->flOptions
= FTYPE_ITALIC
;
516 wxFAIL_MSG(wxT("unknown font slant"));
524 wxStrncpy(zFontFaceName
, sFaceName
.c_str(), WXSIZEOF(zFontFaceName
));
525 nPointSize
= pFont
->GetPointSize();
528 // Matching logic to find the right FM struct
531 for(i
= 0, nIs
= 0; i
< nNumFonts
; i
++)
536 anDiff
[0] = wxGpiStrcmp(pFM
[i
].szFacename
, zFontFaceName
);
537 anDiff
[1] = abs(pFM
[i
].lEmHeight
- nPointSize
);
538 anDiff
[2] = abs(pFM
[i
].usWeightClass
- usWeightClass
);
539 anDiff
[3] = abs((pFM
[i
].fsSelection
& 0x2f) - fsSelection
);
542 nEmHeight
= (int)pFM
[i
].lEmHeight
;
543 nXHeight
=(int)pFM
[i
].lXHeight
;
544 if( (nIs
& 0x01) == 0)
548 anMinDiff
[1] = anDiff
[1];
549 anMinDiff
[2] = anDiff
[2];
550 anMinDiff
[3] = anDiff
[3];
552 else if(anDiff
[3] < anMinDiff
[3])
555 anMinDiff
[3] = anDiff
[3];
557 else if(anDiff
[2] < anMinDiff
[2])
560 anMinDiff
[2] = anDiff
[2];
562 else if(anDiff
[1] < anMinDiff
[1])
565 anMinDiff
[1] = anDiff
[1];
569 else if(anDiff
[0] < anMinDiff
[0])
573 anMinDiff
[3] = anDiff
[3];
574 anMinDiff
[2] = anDiff
[2];
575 anMinDiff
[1] = anDiff
[1];
576 anMinDiff
[0] = anDiff
[0];
578 else if(anDiff
[0] == anMinDiff
[0])
580 if(anDiff
[3] < anMinDiff
[3])
583 anMinDiff
[3] = anDiff
[3];
586 else if(anDiff
[2] < anMinDiff
[2])
589 anMinDiff
[2] = anDiff
[2];
592 else if(anDiff
[1] < anMinDiff
[1])
595 anMinDiff
[1] = anDiff
[1];
602 // Fill in the FATTRS with the best match from FONTMETRICS
604 pFattrs
->usRecordLength
= sizeof(FATTRS
); // Sets size of structure
605 pFattrs
->fsSelection
= pFM
[nIndex
].fsSelection
; // Uses default selection
606 pFattrs
->lMatch
= pFM
[nIndex
].lMatch
; // Force match
607 pFattrs
->idRegistry
= pFM
[nIndex
].idRegistry
; // Uses default registry
608 pFattrs
->usCodePage
= pFM
[nIndex
].usCodePage
; // Code-page
609 pFattrs
->fsType
= 0; // Uses default type
610 pFattrs
->lMaxBaselineExt
= 0;
611 pFattrs
->lAveCharWidth
= 0;
612 pFattrs
->fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
613 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
615 pFattrs
->lMaxBaselineExt
= pFM
[nIndex
].lMaxBaselineExt
;
616 pFattrs
->lAveCharWidth
= pFM
[nIndex
].lAveCharWidth
;
618 wxStrcpy(pFattrs
->szFacename
, pFM
[nIndex
].szFacename
);
620 strcpy(zFontFaceName
, pFM
[nIndex
].szFacename
);
621 strcpy(zFontFaceName
, pFattrs
->szFacename
);
623 if(usWeightClass
>= FWEIGHT_BOLD
)
624 pFattrs
->fsSelection
|= FATTR_SEL_BOLD
;
625 if(pFont
->GetUnderlined())
626 pFattrs
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
627 if(fsSelection
& FM_SEL_ITALIC
)
628 pFattrs
->fsSelection
|= FATTR_SEL_ITALIC
;
629 } // end of wxOS2SelectMatchingFontByName
631 wxFont
wxCreateFontFromLogFont(
632 const LOGFONT
* pLogFont
633 , const PFONTMETRICS pFM
634 , PFACENAMEDESC pFaceName
637 wxNativeFontInfo vInfo
;
639 vInfo
.fa
= *pLogFont
;
641 vInfo
.fn
= *pFaceName
;
642 return wxFont(vInfo
);
643 } // end of wxCreateFontFromLogFont
682 d1
= toupper(s0
[i
]) - toupper(s1
[i
]);