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 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #define DEBUG_PRINTF(NAME) \
30 printf( #NAME " %i\n",raz); fflush(stdout); \
36 #include "wx/string.h"
41 #include "wx/os2/private.h"
43 #include "wx/fontutil.h"
44 #include "wx/fontmap.h"
45 #include "wx/encinfo.h"
47 #include "wx/tokenzr.h"
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxNativeEncodingInfo
55 // ----------------------------------------------------------------------------
57 // convert to/from the string representation: format is
58 // encodingid;facename[;charset]
60 bool wxNativeEncodingInfo::FromString( const wxString
& rsStr
)
62 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
63 wxString sEncid
= vTokenizer
.GetNextToken();
66 if (!sEncid
.ToLong(&lEnc
))
68 encoding
= (wxFontEncoding
)lEnc
;
69 facename
= vTokenizer
.GetNextToken();
73 wxString sTmp
= vTokenizer
.GetNextToken();
81 if ( wxSscanf(sTmp
, _T("%u"), &charset
) != 1 )
83 // should be a number!
88 } // end of wxNativeEncodingInfo::FromString
90 wxString
wxNativeEncodingInfo::ToString() const
94 sStr
<< (long)encoding
<< _T(';') << facename
;
98 sStr
<< _T(';') << charset
;
101 } // end of wxNativeEncodingInfo::ToString
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 bool wxGetNativeFontEncoding( wxFontEncoding vEncoding
,
108 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( const wxNativeEncodingInfo
& rInfo
)
230 hPS
= ::WinGetPS(HWND_DESKTOP
);
232 memset(&vLogFont
, '\0', sizeof(FATTRS
)); // all default values
233 vLogFont
.usRecordLength
= sizeof(FATTRS
);
234 vLogFont
.usCodePage
= (USHORT
)rInfo
.charset
;
235 vLogFont
.lMaxBaselineExt
= 0L; // Outline fonts should use 0
236 vLogFont
.lAveCharWidth
= 0L; // Outline fonts should use 0
237 vLogFont
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
238 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
240 wxStrncpy((wxChar
*)vLogFont
.szFacename
, rInfo
.facename
.c_str(), WXSIZEOF(vLogFont
.szFacename
));
242 if (!::GpiCreateLogFont( hPS
253 } // end of wxTestFontEncoding
255 // ----------------------------------------------------------------------------
256 // wxFont <-> LOGFONT conversion
257 // ----------------------------------------------------------------------------
259 void wxConvertVectorFontSize(
266 LONG lXFontResolution
;
267 LONG lYFontResolution
;
270 hPS
= WinGetScreenPS(HWND_DESKTOP
); // Screen presentation space
273 // Query device context for the screen and then query
274 // the resolution of the device for the device context.
277 hDC
= GpiQueryDevice(hPS
);
278 DevQueryCaps( hDC
, CAPS_HORIZONTAL_FONT_RES
, (LONG
)1, &lXFontResolution
);
279 DevQueryCaps( hDC
, CAPS_VERTICAL_FONT_RES
, (LONG
)1, &lYFontResolution
);
282 // Calculate the size of the character box, based on the
283 // point size selected and the resolution of the device.
284 // The size parameters are of type FIXED, NOT int.
285 // NOTE: 1 point == 1/72 of an inch.
288 vSizef
.cx
= (FIXED
)(((fxPointSize
) / 72 ) * lXFontResolution
);
289 vSizef
.cy
= (FIXED
)(((fxPointSize
) / 72 ) * lYFontResolution
);
291 pFattrs
->lMaxBaselineExt
= MAKELONG( HIUSHORT( vSizef
.cy
), 0 );
292 pFattrs
->lAveCharWidth
= MAKELONG( HIUSHORT( vSizef
.cx
), 0 );
295 } // end of wxConvertVectorPointSize
297 void wxFillLogFont( LOGFONT
* pFattrs
, // OS2 GPI FATTRS
298 PFACENAMEDESC pFaceName
,
305 LONG lNumFonts
= 0L; // For system font count
306 ERRORID vError
; // For logging API errors
308 bool bInternalPS
= false; // if we have to create one
309 PFONTMETRICS pFM
= NULL
;
312 // Initial house cleaning to free data buffers and ensure we have a
313 // functional PS to work with
317 *phPS
= ::WinGetPS(HWND_DESKTOP
);
322 // Determine the number of fonts.
324 if((lNumFonts
= ::GpiQueryFonts( *phPS
325 ,QF_PUBLIC
| QF_PRIVATE
328 ,(LONG
) sizeof(FONTMETRICS
)
335 vError
= ::WinGetLastError(wxGetInstance());
336 sError
= wxPMErrorToStr(vError
);
341 // Allocate space for the font metrics.
343 pFM
= new FONTMETRICS
[lNumFonts
+ 1];
346 // Retrieve the font metrics.
349 lTemp
= ::GpiQueryFonts( *phPS
353 ,(LONG
) sizeof(FONTMETRICS
)
361 // Initialize FATTR and FACENAMEDESC
363 pFattrs
->usRecordLength
= sizeof(FATTRS
);
364 pFattrs
->fsFontUse
= FATTR_FONTUSE_OUTLINE
; // only outline fonts allowed
366 pFattrs
->lMaxBaselineExt
= pFattrs
->lAveCharWidth
= 0;
367 pFattrs
->idRegistry
= 0;
370 pFaceName
->usSize
= sizeof(FACENAMEDESC
);
371 pFaceName
->usWeightClass
= FWEIGHT_DONT_CARE
;
372 pFaceName
->usWidthClass
= FWIDTH_DONT_CARE
;
373 pFaceName
->usReserved
= 0;
374 pFaceName
->flOptions
= 0;
377 // This does the actual selection of fonts
379 wxOS2SelectMatchingFontByName( pFattrs
386 // We should now have the correct FATTRS set with the selected
387 // font, so now we need to generate an ID
389 long lNumLids
= ::GpiQueryNumberSetIds(*phPS
);
397 memset(alIds
, 0, sizeof(long) * 255);
398 if(!::GpiQuerySetIds( *phPS
406 ::WinReleasePS(*phPS
);
411 for(unsigned long LCNum
= 0; LCNum
< (unsigned long)lNumLids
; LCNum
++)
412 if(alIds
[LCNum
] == *pflId
)
414 if(*pflId
> 254) // wow, no id available!
417 ::WinReleasePS(*phPS
);
424 // Release and delete the current font
426 ::GpiSetCharSet(*phPS
, LCID_DEFAULT
);/* release the font before deleting */
427 ::GpiDeleteSetId(*phPS
, 1L); /* delete the logical font */
430 // Now build a facestring
434 strcpy(zFacename
, pFattrs
->szFacename
);
436 if(::GpiQueryFaceString( *phPS
443 vError
= ::WinGetLastError(vHabmain
);
445 sFaceName
= (wxChar
*)zFacename
;
446 *pbInternalPS
= bInternalPS
;
449 // That's it, we now have everything we need to actually create the font
451 } // end of wxFillLogFont
453 void wxOS2SelectMatchingFontByName(
455 , PFACENAMEDESC pFaceName
458 , const wxFont
* pFont
468 wxChar zFontFaceName
[FACESIZE
];
470 USHORT usWeightClass
;
474 for(i
= 0;i
< 16; i
++)
475 anMinDiff
[i
] = nMinDiff0
;
477 switch (pFont
->GetFamily())
480 sFaceName
= wxT("Tms Rmn");
484 sFaceName
= wxT("WarpSans");
488 sFaceName
= wxT("Tms Rmn");
492 sFaceName
= wxT("Courier") ;
496 sFaceName
= wxT("System VIO") ;
500 sFaceName
= wxT("Helv") ;
505 sFaceName
= wxT("System VIO") ;
508 switch (pFont
->GetWeight())
511 wxFAIL_MSG(_T("unknown font weight"));
513 usWeightClass
= FWEIGHT_DONT_CARE
;
517 usWeightClass
= FWEIGHT_NORMAL
;
521 usWeightClass
= FWEIGHT_LIGHT
;
525 usWeightClass
= FWEIGHT_BOLD
;
528 case wxFONTWEIGHT_MAX
:
529 usWeightClass
= FWEIGHT_ULTRA_BOLD
;
532 pFaceName
->usWeightClass
= usWeightClass
;
534 switch (pFont
->GetStyle())
538 fsSelection
= FM_SEL_ITALIC
;
539 pFaceName
->flOptions
= FTYPE_ITALIC
;
543 wxFAIL_MSG(wxT("unknown font slant"));
551 wxStrncpy(zFontFaceName
, sFaceName
.c_str(), WXSIZEOF(zFontFaceName
));
552 nPointSize
= pFont
->GetPointSize();
555 // Matching logic to find the right FM struct
558 for(i
= 0, nIs
= 0; i
< nNumFonts
; i
++)
563 anDiff
[0] = wxGpiStrcmp((wxChar
*)pFM
[i
].szFacename
, zFontFaceName
);
564 anDiff
[1] = abs(pFM
[i
].lEmHeight
- nPointSize
);
565 anDiff
[2] = abs(pFM
[i
].usWeightClass
- usWeightClass
);
566 anDiff
[3] = abs((pFM
[i
].fsSelection
& 0x2f) - fsSelection
);
569 nEmHeight
= (int)pFM
[i
].lEmHeight
;
570 nXHeight
=(int)pFM
[i
].lXHeight
;
571 if( (nIs
& 0x01) == 0)
575 anMinDiff
[1] = anDiff
[1];
576 anMinDiff
[2] = anDiff
[2];
577 anMinDiff
[3] = anDiff
[3];
579 else if(anDiff
[3] < anMinDiff
[3])
582 anMinDiff
[3] = anDiff
[3];
584 else if(anDiff
[2] < anMinDiff
[2])
587 anMinDiff
[2] = anDiff
[2];
589 else if(anDiff
[1] < anMinDiff
[1])
592 anMinDiff
[1] = anDiff
[1];
596 else if(anDiff
[0] < anMinDiff
[0])
600 anMinDiff
[3] = anDiff
[3];
601 anMinDiff
[2] = anDiff
[2];
602 anMinDiff
[1] = anDiff
[1];
603 anMinDiff
[0] = anDiff
[0];
605 else if(anDiff
[0] == anMinDiff
[0])
607 if(anDiff
[3] < anMinDiff
[3])
610 anMinDiff
[3] = anDiff
[3];
613 else if(anDiff
[2] < anMinDiff
[2])
616 anMinDiff
[2] = anDiff
[2];
619 else if(anDiff
[1] < anMinDiff
[1])
622 anMinDiff
[1] = anDiff
[1];
629 // Fill in the FATTRS with the best match from FONTMETRICS
631 pFattrs
->usRecordLength
= sizeof(FATTRS
); // Sets size of structure
632 pFattrs
->lMatch
= pFM
[nIndex
].lMatch
; // Force match
633 pFattrs
->idRegistry
= 0;
634 pFattrs
->usCodePage
= 0;
635 pFattrs
->fsFontUse
= 0;
637 pFattrs
->lMaxBaselineExt
= 0;
638 pFattrs
->lAveCharWidth
= 0;
639 wxStrcpy((wxChar
*)pFattrs
->szFacename
, (wxChar
*)pFM
[nIndex
].szFacename
);
640 if (pFont
->GetWeight() == wxNORMAL
)
641 pFattrs
->fsSelection
= 0;
643 pFattrs
->fsSelection
= FATTR_SEL_BOLD
;
645 if (pFont
->GetStyle() == wxITALIC
|| pFont
->GetStyle() == wxSLANT
)
646 pFattrs
->fsSelection
|= FATTR_SEL_ITALIC
;
648 if (pFont
->GetUnderlined())
649 pFattrs
->fsSelection
|= FATTR_SEL_UNDERSCORE
;
650 } // end of wxOS2SelectMatchingFontByName
652 wxFont
wxCreateFontFromLogFont(
653 const LOGFONT
* pLogFont
654 , const PFONTMETRICS pFM
655 , PFACENAMEDESC pFaceName
658 wxNativeFontInfo vInfo
;
660 vInfo
.fa
= *pLogFont
;
662 vInfo
.fn
= *pFaceName
;
663 return wxFont(vInfo
);
664 } // end of wxCreateFontFromLogFont
703 d1
= wxToupper(s0
[i
]) - wxToupper(s1
[i
]);