Font fixes for the font dialog
[wxWidgets.git] / src / os2 / fontutil.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontutil.cpp
3 // Purpose: font-related helper functions for wxMSW
4 // Author: Modified by David Webster for OS/2
5 // Modified by:
6 // Created: 01.03.00
7 // RCS-ID: $Id$
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); \
13 raz++; \
14 }
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #ifdef __GNUG__
25 #pragma implementation "fontutil.h"
26 #endif
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/string.h"
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #endif //WX_PRECOMP
37
38 #include "wx/os2/private.h"
39
40 #include "wx/fontutil.h"
41 #include "wx/fontmap.h"
42
43 #include "wx/tokenzr.h"
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // wxNativeEncodingInfo
51 // ----------------------------------------------------------------------------
52
53 // convert to/from the string representation: format is
54 // encodingid;facename[;charset]
55
56 bool wxNativeEncodingInfo::FromString(
57 const wxString& rsStr
58 )
59 {
60 wxStringTokenizer vTokenizer(rsStr, _T(";"));
61 wxString sEncid = vTokenizer.GetNextToken();
62 long lEnc;
63
64 if (!sEncid.ToLong(&lEnc))
65 return FALSE;
66 encoding = (wxFontEncoding)lEnc;
67 facename = vTokenizer.GetNextToken();
68 if (!facename)
69 return FALSE;
70
71 wxString sTmp = vTokenizer.GetNextToken();
72
73 if (!sTmp)
74 {
75 charset = 850;
76 }
77 else
78 {
79 if ( wxSscanf(sTmp, _T("%u"), &charset) != 1 )
80 {
81 // should be a number!
82 return FALSE;
83 }
84 }
85 return TRUE;
86 } // end of wxNativeEncodingInfo::FromString
87
88 wxString wxNativeEncodingInfo::ToString() const
89 {
90 wxString sStr;
91
92 sStr << (long)encoding << _T(';') << facename;
93
94 if (charset != 850)
95 {
96 sStr << _T(';') << charset;
97 }
98 return sStr;
99 } // end of wxNativeEncodingInfo::ToString
100
101 // ----------------------------------------------------------------------------
102 // helper functions
103 // ----------------------------------------------------------------------------
104
105 bool wxGetNativeFontEncoding(
106 wxFontEncoding vEncoding
107 , wxNativeEncodingInfo* pInfo
108 )
109 {
110 wxCHECK_MSG(pInfo, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
111 if (vEncoding == wxFONTENCODING_DEFAULT)
112 {
113 vEncoding = wxFont::GetDefaultEncoding();
114 }
115 switch (vEncoding)
116 {
117 case wxFONTENCODING_ISO8859_1:
118 case wxFONTENCODING_ISO8859_15:
119 case wxFONTENCODING_CP1250:
120 pInfo->charset = 1250;
121 break;
122
123 case wxFONTENCODING_ISO8859_2:
124 case wxFONTENCODING_CP1252:
125 pInfo->charset = 1252;
126 break;
127
128 case wxFONTENCODING_ISO8859_4:
129 case wxFONTENCODING_ISO8859_10:
130 pInfo->charset = 921; // what is baltic?
131 break;
132
133 case wxFONTENCODING_ISO8859_5:
134 case wxFONTENCODING_CP1251:
135 pInfo->charset = 1251;
136 break;
137
138 case wxFONTENCODING_ISO8859_6:
139 pInfo->charset = 864;
140 break;
141
142 case wxFONTENCODING_ISO8859_7:
143 pInfo->charset = 869;
144 break;
145
146 case wxFONTENCODING_ISO8859_8:
147 pInfo->charset = 862;
148 break;
149
150 case wxFONTENCODING_ISO8859_9:
151 pInfo->charset = 857;
152 break;
153
154 case wxFONTENCODING_ISO8859_11:
155 pInfo->charset = 874; // what is thai
156 break;
157
158 case wxFONTENCODING_CP437:
159 pInfo->charset = 437;
160 break;
161
162 default:
163 wxFAIL_MSG(wxT("unsupported encoding"));
164 // fall through
165
166 case wxFONTENCODING_SYSTEM:
167 pInfo->charset = 850;
168 break;
169 }
170 return TRUE;
171 } // end of wxGetNativeFontEncoding
172
173 wxFontEncoding wxGetFontEncFromCharSet(
174 int nCharSet
175 )
176 {
177 wxFontEncoding eFontEncoding;
178
179 switch (nCharSet)
180 {
181 default:
182 case 1250:
183 eFontEncoding = wxFONTENCODING_CP1250;
184 break;
185
186 case 1252:
187 eFontEncoding = wxFONTENCODING_CP1252;
188 break;
189
190 case 921:
191 eFontEncoding = wxFONTENCODING_ISO8859_4;
192 break;
193
194 case 1251:
195 eFontEncoding = wxFONTENCODING_CP1251;
196 break;
197
198 case 864:
199 eFontEncoding = wxFONTENCODING_ISO8859_6;
200 break;
201
202 case 869:
203 eFontEncoding = wxFONTENCODING_ISO8859_7;
204 break;
205
206 case 862:
207 eFontEncoding = wxFONTENCODING_ISO8859_8;
208 break;
209
210 case 857:
211 eFontEncoding = wxFONTENCODING_ISO8859_9;
212 break;
213
214 case 874:
215 eFontEncoding = wxFONTENCODING_ISO8859_11;
216 break;
217
218 case 437:
219 eFontEncoding = wxFONTENCODING_CP437;
220 break;
221 }
222 return eFontEncoding;
223 } // end of wxGetNativeFontEncoding
224
225 bool wxTestFontEncoding(
226 const wxNativeEncodingInfo& rInfo
227 )
228 {
229 FATTRS vLogFont;
230 HPS hPS;
231
232 hPS = ::WinGetPS(HWND_DESKTOP);
233
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
241
242 strncpy(vLogFont.szFacename, rInfo.facename.c_str(), sizeof(vLogFont.szFacename));
243
244 if (!::GpiCreateLogFont( hPS
245 ,NULL
246 ,1L
247 ,&vLogFont
248 ))
249 {
250 ::WinReleasePS(hPS);
251 return FALSE;
252 }
253 ::WinReleasePS(hPS);
254 return TRUE;
255 } // end of wxTestFontEncoding
256
257 // ----------------------------------------------------------------------------
258 // wxFont <-> LOGFONT conversion
259 // ----------------------------------------------------------------------------
260
261 void wxFillLogFont(
262 LOGFONT* pFattrs // OS2 GPI FATTRS
263 , PFACENAMEDESC pFaceName
264 , HPS* phPS
265 , bool* pbInternalPS
266 , long* pflId
267 , wxString& sFaceName
268 , wxFont* pFont
269 )
270 {
271 LONG lNumFonts = 0L; // For system font count
272 ERRORID vError; // For logging API errors
273 LONG lTemp = 0L;
274 bool bInternalPS = FALSE; // if we have to create one
275 PFONTMETRICS pFM = NULL;
276
277 //
278 // Initial house cleaning to free data buffers and ensure we have a
279 // functional PS to work with
280 //
281 if (!*phPS)
282 {
283 *phPS = ::WinGetPS(HWND_DESKTOP);
284 bInternalPS = TRUE;
285 }
286
287 //
288 // Determine the number of fonts.
289 //
290 if((lNumFonts = ::GpiQueryFonts( *phPS
291 ,QF_PUBLIC
292 ,NULL
293 ,&lTemp
294 ,(LONG) sizeof(FONTMETRICS)
295 ,NULL
296 )) < 0L)
297 {
298 ERRORID vError;
299 wxString sError;
300
301 vError = ::WinGetLastError(wxGetInstance());
302 sError = wxPMErrorToStr(vError);
303 return;
304 }
305
306 //
307 // Allocate space for the font metrics.
308 //
309 pFM = new FONTMETRICS[lNumFonts + 1];
310
311 //
312 // Retrieve the font metrics.
313 //
314 lTemp = lNumFonts;
315 lTemp = ::GpiQueryFonts( *phPS
316 ,QF_PUBLIC
317 ,NULL
318 ,&lTemp
319 ,(LONG) sizeof(FONTMETRICS)
320 ,pFM
321 );
322 pFont->SetFM( pFM
323 ,(int)lNumFonts
324 );
325
326 //
327 // Initialize FATTR and FACENAMEDESC
328 //
329 pFattrs->usRecordLength = sizeof(FATTRS);
330 pFattrs->fsFontUse = FATTR_FONTUSE_OUTLINE | // only outline fonts allowed
331 FATTR_FONTUSE_TRANSFORMABLE; // may be transformed
332 pFattrs->fsType = 0;
333 pFattrs->lMaxBaselineExt = pFattrs->lAveCharWidth = 0;
334 pFattrs->idRegistry = 0;
335 pFattrs->lMatch = 0;
336
337 pFaceName->usSize = sizeof(FACENAMEDESC);
338 pFaceName->usWidthClass = FWIDTH_NORMAL;
339 pFaceName->usReserved = 0;
340 pFaceName->flOptions = 0;
341
342 //
343 // This does the actual selection of fonts
344 //
345 wxOS2SelectMatchingFontByName( pFattrs
346 ,pFaceName
347 ,pFM
348 ,(int)lNumFonts
349 ,pFont
350 );
351 //
352 // We should now have the correct FATTRS set with the selected
353 // font, so now we need to generate an ID
354 //
355 long lNumLids = ::GpiQueryNumberSetIds(*phPS);
356 long lGpiError;
357
358 if(lNumLids )
359 {
360 long alTypes[255];
361 STR8 azNames[255];
362 long alIds[255];
363
364 memset(alIds, 0, sizeof(long) * 255);
365 if(!::GpiQuerySetIds( *phPS
366 ,lNumLids
367 ,alTypes
368 ,azNames
369 ,alIds
370 ))
371 {
372 if (bInternalPS)
373 ::WinReleasePS(*phPS);
374 return;
375 }
376 if (*pflId == 0L)
377 *pflId = 1L;
378 for(unsigned long LCNum = 0; LCNum < lNumLids; LCNum++)
379 if(alIds[LCNum] == *pflId)
380 ++*pflId;
381 if(*pflId > 254) // wow, no id available!
382 {
383 if (bInternalPS)
384 ::WinReleasePS(*phPS);
385 return;
386 }
387 }
388 else
389 *pflId = 1L;
390 //
391 // Release and delete the current font
392 //
393 ::GpiSetCharSet(*phPS, LCID_DEFAULT);/* release the font before deleting */
394 ::GpiDeleteSetId(*phPS, 1L); /* delete the logical font */
395
396 //
397 // Now build a facestring
398 //
399 char zFacename[128];
400
401 strcpy(zFacename, pFattrs->szFacename);
402
403 if(::GpiQueryFaceString( *phPS
404 ,zFacename
405 ,pFaceName
406 ,FACESIZE
407 ,pFattrs->szFacename
408 ) == GPI_ERROR)
409 {
410 vError = ::WinGetLastError(vHabmain);
411 }
412 sFaceName = zFacename;
413 *pbInternalPS = bInternalPS;
414
415 //
416 // That's it, we now have everything we need to actually create the font
417 //
418 } // end of wxFillLogFont
419
420 void wxOS2SelectMatchingFontByName(
421 PFATTRS pFattrs
422 , PFACENAMEDESC pFaceName
423 , PFONTMETRICS pFM
424 , int nNumFonts
425 , const wxFont* pFont
426 )
427 {
428 int i;
429 int nDiff0;
430 int nPointSize;
431 int nDiff;
432 int nIs;
433 int nMinDiff;
434 int nMinDiff0;
435 int nApirc;
436 int anDiff[16];
437 int anMinDiff[16];
438 int nIndex = 0;
439 STR8 zFn;
440 char zFontFaceName[FACESIZE];
441 wxString sFaceName;
442 USHORT usWeightClass;
443 int fsSelection = 0;
444
445 nMinDiff0 = 0xf000;
446 for(i = 0;i < 16; i++)
447 anMinDiff[i] = nMinDiff0;
448
449 switch (pFont->GetFamily())
450 {
451 case wxSCRIPT:
452 sFaceName = wxT("Script");
453 break;
454
455 case wxDECORATIVE:
456 sFaceName = wxT("WarpSans");
457 break;
458
459 case wxROMAN:
460 sFaceName = wxT("Times New Roman");
461 break;
462
463 case wxTELETYPE:
464 sFaceName = wxT("Courier New") ;
465 break;
466
467 case wxMODERN:
468 sFaceName = wxT("Arial") ;
469 break;
470
471 case wxSWISS:
472 sFaceName = wxT("Helv") ;
473 break;
474
475 case wxDEFAULT:
476 default:
477 sFaceName = wxT("System Proportional") ;
478 }
479
480 switch (pFont->GetWeight())
481 {
482 default:
483 wxFAIL_MSG(_T("unknown font weight"));
484 // fall through
485 usWeightClass = FWEIGHT_DONT_CARE;
486 break;
487
488 case wxNORMAL:
489 usWeightClass = FWEIGHT_NORMAL;
490 break;
491
492 case wxLIGHT:
493 usWeightClass = FWEIGHT_LIGHT;
494 break;
495
496 case wxBOLD:
497 usWeightClass = FWEIGHT_BOLD;
498 break;
499
500 case wxFONTWEIGHT_MAX:
501 usWeightClass = FWEIGHT_ULTRA_BOLD;
502 break;
503 }
504 pFaceName->usWeightClass = usWeightClass;
505
506 switch (pFont->GetStyle())
507 {
508 case wxITALIC:
509 case wxSLANT:
510 fsSelection = FM_SEL_ITALIC;
511 pFaceName->flOptions = FTYPE_ITALIC;
512 break;
513
514 default:
515 wxFAIL_MSG(wxT("unknown font slant"));
516 // fall through
517
518 case wxNORMAL:
519 fsSelection = 0;
520 break;
521 }
522
523 wxStrncpy(zFontFaceName, sFaceName.c_str(), WXSIZEOF(zFontFaceName));
524 nPointSize = pFont->GetPointSize();
525
526 //
527 // Matching logic to find the right FM struct
528 //
529 nIndex = 0;
530 for(i = 0, nIs = 0; i < nNumFonts; i++)
531 {
532 int nEmHeight = 0;
533 int nXHeight = 0;
534
535 anDiff[0] = wxGpiStrcmp(pFM[i].szFamilyname, zFontFaceName);
536 anDiff[1] = abs(pFM[i].lEmHeight - nPointSize);
537 anDiff[2] = abs(pFM[i].usWeightClass - usWeightClass);
538 anDiff[3] = abs((pFM[i].fsSelection & 0x2f) - fsSelection);
539 if(anDiff[0] == 0)
540 {
541 nEmHeight = (int)pFM[i].lEmHeight;
542 nXHeight =(int)pFM[i].lXHeight;
543 if( (nIs & 0x01) == 0)
544 {
545 nIs = 1;
546 nIndex = i;
547 anMinDiff[1] = anDiff[1];
548 anMinDiff[2] = anDiff[2];
549 anMinDiff[3] = anDiff[3];
550 }
551 else if(anDiff[3] < anMinDiff[3])
552 {
553 nIndex = i;
554 anMinDiff[3] = anDiff[3];
555 }
556 else if(anDiff[2] < anMinDiff[2])
557 {
558 nIndex = i;
559 anMinDiff[2] = anDiff[2];
560 }
561 else if(anDiff[1] < anMinDiff[1])
562 {
563 nIndex = i;
564 anMinDiff[1] = anDiff[1];
565 }
566 anMinDiff[0] = 0;
567 }
568 else if(anDiff[0] < anMinDiff[0])
569 {
570 nIs = 2;
571 nIndex = i;
572 anMinDiff[3] = anDiff[3];
573 anMinDiff[2] = anDiff[2];
574 anMinDiff[1] = anDiff[1];
575 anMinDiff[0] = anDiff[0];
576 }
577 else if(anDiff[0] == anMinDiff[0])
578 {
579 if(anDiff[3] < anMinDiff[3])
580 {
581 nIndex = i;
582 anMinDiff[3] = anDiff[3];
583 nIs = 2;
584 }
585 else if(anDiff[2] < anMinDiff[2])
586 {
587 nIndex = i;
588 anMinDiff[2] = anDiff[2];
589 nIs = 2;
590 }
591 else if(anDiff[1] < anMinDiff[1])
592 {
593 nIndex = i;
594 anMinDiff[1] = anDiff[1];
595 nIs = 2;
596 }
597 }
598 }
599
600 //
601 // Fill in the FATTRS with the best match from FONTMETRICS
602 //
603 pFattrs->usRecordLength = sizeof(FATTRS); // sets size of structure
604 pFattrs->fsSelection = pFM[nIndex].fsSelection; // uses default selection
605 pFattrs->lMatch = pFM[nIndex].lMatch; // force match
606 pFattrs->idRegistry = pFM[nIndex].idRegistry; // uses default registry
607 pFattrs->usCodePage = pFM[nIndex].usCodePage; // code-page
608 pFattrs->lMaxBaselineExt = 0; // OUTLINE fonts need this set to 0 as they use other attributes to match
609 pFattrs->lAveCharWidth = 0; // OUTLINE fonts need this set to 0 as they use other attributes to match
610 pFattrs->fsType = 0;// pfm->fsType; /* uses default type */
611 pFattrs->fsFontUse = 0;
612
613 wxStrcpy(pFattrs->szFacename, pFM[nIndex].szFacename);
614 // Debug
615 strcpy(zFontFaceName, pFM[nIndex].szFacename);
616 strcpy(zFontFaceName, pFattrs->szFacename);
617
618 if(usWeightClass >= FWEIGHT_BOLD)
619 pFattrs->fsSelection |= FATTR_SEL_BOLD;
620 if(pFont->GetUnderlined())
621 pFattrs->fsSelection |= FATTR_SEL_UNDERSCORE;
622 if(fsSelection & FM_SEL_ITALIC)
623 pFattrs->fsSelection |= FATTR_SEL_ITALIC;
624 } // end of wxOS2SelectMatchingFontByName
625
626 wxFont wxCreateFontFromLogFont(
627 const LOGFONT* pLogFont
628 , const PFONTMETRICS pFM
629 , PFACENAMEDESC pFaceName
630 )
631 {
632 wxNativeFontInfo vInfo;
633
634 vInfo.fa = *pLogFont;
635 vInfo.fm = *pFM;
636 vInfo.fn = *pFaceName;
637 return wxFont(vInfo);
638 } // end of wxCreateFontFromLogFont
639
640 int wxGpiStrcmp(
641 char* s0
642 , char* s1
643 )
644 { int l0;
645 int l1;
646 int l;
647 int d;
648 int d1;
649 int i;
650 int rc;
651
652 rc = 0;
653 if(s0 == NULL)
654 {
655 if(s1 == NULL)
656 return 0;
657 else
658 return 32;
659 }
660 else if(s1 == NULL)
661 return 32;
662
663 l0 = strlen(s0);
664 l1 = strlen(s1);
665 l = l0;
666 if(l0 != l1)
667 {
668 rc++;
669 if(l1 < l0)
670 l = l1;
671 }
672 for(i=0;i<l;i++)
673 {
674 d = s0[i]-s1[i];
675 if(!d)
676 continue;
677 d1 = toupper(s0[i]) - toupper(s1[i]);
678 if(!d1)
679 continue;
680 rc += abs(d);
681 }
682 return rc;
683 }
684