Change up font selection process
[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 wxString sVals;
327
328 for (int i = 0; i < lNumFonts; i++)
329 {
330 sVals << "Face: " << pFM[i].szFacename
331 << "Family: " << pFM[i].szFamilyname
332 << " PointSize: " << pFM[i].lEmHeight
333 << " Height: " << pFM[i].lXHeight
334 ;
335 sVals = "";
336 }
337
338 //
339 // Initialize FATTR and FACENAMEDESC
340 //
341 pFattrs->usRecordLength = sizeof(FATTRS);
342 pFattrs->fsFontUse = FATTR_FONTUSE_OUTLINE | // only outline fonts allowed
343 FATTR_FONTUSE_TRANSFORMABLE; // may be transformed
344 pFattrs->fsType = 0;
345 pFattrs->lMaxBaselineExt = pFattrs->lAveCharWidth = 0;
346 pFattrs->idRegistry = 0;
347 pFattrs->lMatch = 0;
348
349 pFaceName->usSize = sizeof(FACENAMEDESC);
350 pFaceName->usWidthClass = FWIDTH_NORMAL;
351 pFaceName->usReserved = 0;
352 pFaceName->flOptions = 0;
353
354 //
355 // This does the actual selection of fonts
356 //
357 wxOS2SelectMatchingFontByName( pFattrs
358 ,pFaceName
359 ,pFM
360 ,(int)lNumFonts
361 ,pFont
362 );
363 //
364 // We should now have the correct FATTRS set with the selected
365 // font, so now we need to generate an ID
366 //
367 long lNumLids = ::GpiQueryNumberSetIds(*phPS);
368 long lGpiError;
369
370 if(lNumLids )
371 {
372 long alTypes[255];
373 STR8 azNames[255];
374 long alIds[255];
375
376 if(!::GpiQuerySetIds( *phPS
377 ,lNumLids
378 ,alTypes
379 ,azNames
380 ,alIds
381 ))
382 {
383 if (bInternalPS)
384 ::WinReleasePS(*phPS);
385 return;
386 }
387
388 for(unsigned long LCNum = 0; LCNum < lNumLids; LCNum++)
389 if(alIds[LCNum] == *pflId)
390 ++*pflId;
391 if(*pflId > 254) // wow, no id available!
392 {
393 if (bInternalPS)
394 ::WinReleasePS(*phPS);
395 return;
396 }
397 }
398 else
399 *pflId = 1L;
400 //
401 // Release and delete the current font
402 //
403 ::GpiSetCharSet(*phPS, LCID_DEFAULT);/* release the font before deleting */
404 ::GpiDeleteSetId(*phPS, 1L); /* delete the logical font */
405
406 //
407 // Now build a facestring
408 //
409 char zFacename[128];
410
411 strcpy(zFacename, pFattrs->szFacename);
412
413 if(::GpiQueryFaceString( *phPS
414 ,zFacename
415 ,pFaceName
416 ,FACESIZE
417 ,pFattrs->szFacename
418 ) == GPI_ERROR)
419 {
420 vError = ::WinGetLastError(vHabmain);
421 }
422 sFaceName = zFacename;
423 *pbInternalPS = bInternalPS;
424
425 //
426 // That's it, we now have everything we need to actually create the font
427 //
428 } // end of wxFillLogFont
429
430 void wxOS2SelectMatchingFontByName(
431 PFATTRS pFattrs
432 , PFACENAMEDESC pFaceName
433 , PFONTMETRICS pFM
434 , int nNumFonts
435 , const wxFont* pFont
436 )
437 {
438 int i;
439 int nDiff0;
440 int nPointSize;
441 int nDiff;
442 int nIs;
443 int nIndex;
444 int nMinDiff;
445 int nMinDiff0;
446 int nApirc;
447 int anDiff[16];
448 int anMinDiff[16];
449 STR8 zFn;
450 char zFontFaceName[FACESIZE];
451 wxString sFaceName;
452 USHORT usWeightClass;
453 int fsSelection = 0;
454
455 nMinDiff0 = 0xf000;
456 for(i = 0;i < 16; i++)
457 anMinDiff[i] = nMinDiff0;
458
459 switch (pFont->GetFamily())
460 {
461 case wxSCRIPT:
462 sFaceName = wxT("Script");
463 break;
464
465 case wxDECORATIVE:
466 case wxROMAN:
467 sFaceName = wxT("Tms Rmn");
468 break;
469
470 case wxTELETYPE:
471 sFaceName = wxT("Courier") ;
472 break;
473
474 case wxMODERN:
475 sFaceName = wxT("System VIO") ;
476 break;
477
478 case wxSWISS:
479 sFaceName = wxT("Helv") ;
480 break;
481
482 case wxDEFAULT:
483 default:
484 sFaceName = wxT("System VIO") ;
485 }
486
487 switch (pFont->GetWeight())
488 {
489 default:
490 wxFAIL_MSG(_T("unknown font weight"));
491 // fall through
492 usWeightClass = FWEIGHT_DONT_CARE;
493 break;
494
495 case wxNORMAL:
496 usWeightClass = FWEIGHT_NORMAL;
497 break;
498
499 case wxLIGHT:
500 usWeightClass = FWEIGHT_LIGHT;
501 break;
502
503 case wxBOLD:
504 usWeightClass = FWEIGHT_BOLD;
505 break;
506
507 case wxFONTWEIGHT_MAX:
508 usWeightClass = FWEIGHT_ULTRA_BOLD;
509 break;
510 }
511 pFaceName->usWeightClass = usWeightClass;
512
513 switch (pFont->GetStyle())
514 {
515 case wxITALIC:
516 case wxSLANT:
517 fsSelection = FM_SEL_ITALIC;
518 pFaceName->flOptions = FTYPE_ITALIC;
519 break;
520
521 default:
522 wxFAIL_MSG(wxT("unknown font slant"));
523 // fall through
524
525 case wxNORMAL:
526 fsSelection = 0;
527 break;
528 }
529
530 wxStrncpy(zFontFaceName, sFaceName.c_str(), WXSIZEOF(zFontFaceName));
531 nPointSize = pFont->GetPointSize();
532
533 //
534 // Matching logic to find the right FM struct
535 //
536 nIndex = 0;
537 for(i = 0, nIs = 0; i < nNumFonts; i++)
538 {
539 int nEmHeight = 0;
540 int nXHeight = 0;
541
542 anDiff[0] = wxGpiStrcmp(pFM[i].szFamilyname, zFontFaceName);
543 anDiff[1] = abs(pFM[i].lEmHeight - nPointSize);
544 anDiff[2] = abs(pFM[i].usWeightClass - usWeightClass);
545 anDiff[3] = abs((pFM[i].fsSelection & 0x2f) - fsSelection);
546 if(anDiff[0] == 0)
547 {
548 nEmHeight = (int)pFM[i].lEmHeight;
549 nXHeight =(int)pFM[i].lXHeight;
550 if( (nIs & 0x01) == 0)
551 {
552 nIs = 1;
553 nIndex = i;
554 anMinDiff[1] = anDiff[1];
555 anMinDiff[2] = anDiff[2];
556 anMinDiff[3] = anDiff[3];
557 }
558 else if(anDiff[3] < anMinDiff[3])
559 {
560 nIndex = i;
561 anMinDiff[3] = anDiff[3];
562 }
563 else if(anDiff[2] < anMinDiff[2])
564 {
565 nIndex = i;
566 anMinDiff[2] = anDiff[2];
567 }
568 else if(anDiff[1] < anMinDiff[1])
569 {
570 nIndex = i;
571 anMinDiff[1] = anDiff[1];
572 }
573 anMinDiff[0] = 0;
574 }
575 else if(anDiff[0] < anMinDiff[0])
576 {
577 nIs = 2;
578 nIndex = i;
579 anMinDiff[3] = anDiff[3];
580 anMinDiff[2] = anDiff[2];
581 anMinDiff[1] = anDiff[1];
582 anMinDiff[0] = anDiff[0];
583 }
584 else if(anDiff[0] == anMinDiff[0])
585 {
586 if(anDiff[3] < anMinDiff[3])
587 {
588 nIndex = i;
589 anMinDiff[3] = anDiff[3];
590 nIs = 2;
591 }
592 else if(anDiff[2] < anMinDiff[2])
593 {
594 nIndex = i;
595 anMinDiff[2] = anDiff[2];
596 nIs = 2;
597 }
598 else if(anDiff[1] < anMinDiff[1])
599 {
600 nIndex = i;
601 anMinDiff[1] = anDiff[1];
602 nIs = 2;
603 }
604 }
605 }
606
607 //
608 // Fill in the FATTRS with the best match from FONTMETRICS
609 //
610 pFattrs->usRecordLength = sizeof(FATTRS); // sets size of structure
611 pFattrs->fsSelection = pFM[nIndex].fsSelection; // uses default selection
612 pFattrs->lMatch = pFM[nIndex].lMatch; // force match
613 pFattrs->idRegistry = pFM[nIndex].idRegistry; // uses default registry
614 pFattrs->usCodePage = pFM[nIndex].usCodePage; // code-page
615 if(pFM[nIndex].lMatch)
616 {
617 pFattrs->lMaxBaselineExt = pFM[nIndex].lMaxBaselineExt; // requested font height
618 pFattrs->lAveCharWidth = pFM[nIndex].lAveCharWidth ; // requested font width
619 }
620 else
621 {
622 pFattrs->lMaxBaselineExt = 0;
623 pFattrs->lAveCharWidth = 0;
624 }
625 pFattrs->fsType = 0;// pfm->fsType; /* uses default type */
626 pFattrs->fsFontUse = 0;
627
628 wxStrcpy(pFattrs->szFacename, pFM[nIndex].szFacename);
629 // Debug
630 strcpy(zFontFaceName, pFM[nIndex].szFacename);
631 strcpy(zFontFaceName, pFattrs->szFacename);
632
633 if(usWeightClass >= FWEIGHT_BOLD)
634 pFattrs->fsSelection |= FATTR_SEL_BOLD;
635 if(pFont->GetUnderlined())
636 pFattrs->fsSelection |= FATTR_SEL_UNDERSCORE;
637 if(fsSelection & FM_SEL_ITALIC)
638 pFattrs->fsSelection |= FATTR_SEL_ITALIC;
639 } // end of wxOS2SelectMatchingFontByName
640
641 wxFont wxCreateFontFromLogFont(
642 const LOGFONT* pLogFont
643 , const PFONTMETRICS pFM
644 , PFACENAMEDESC pFaceName
645 )
646 {
647 //
648 // Extract family from facename
649 //
650 int nFontFamily;
651
652 if (strcmp(pLogFont->szFacename, "Times New Roman") == 0)
653 nFontFamily = wxROMAN;
654 else if (strcmp(pLogFont->szFacename, "WarpSans") == 0)
655 nFontFamily = wxSWISS;
656 else if (strcmp(pLogFont->szFacename, "Script") == 0)
657 nFontFamily = wxSCRIPT;
658 else if (strcmp(pLogFont->szFacename, "Courier New") == 0)
659 nFontFamily = wxMODERN;
660 else
661 nFontFamily = wxSWISS;
662
663 //
664 // Weight and Style
665 //
666 int nFontWeight = wxNORMAL;
667
668 switch (pFaceName->usWeightClass)
669 {
670 case FWEIGHT_LIGHT:
671 nFontWeight = wxLIGHT;
672 break;
673
674 default:
675 case FWEIGHT_NORMAL:
676 nFontWeight = wxNORMAL;
677 break;
678
679 case FWEIGHT_BOLD:
680 nFontWeight = wxBOLD;
681 break;
682 }
683
684 int nFontStyle;
685
686 if(pLogFont->fsSelection & FATTR_SEL_ITALIC)
687 nFontStyle = wxITALIC;
688 else
689 nFontStyle = wxNORMAL;
690
691 bool bFontUnderline = (pLogFont->fsSelection & FATTR_SEL_UNDERSCORE);
692 wxString sFontFace = pLogFont->szFacename;
693 int nFontPoints = pFM->lEmHeight;
694 wxFontEncoding vFontEncoding;
695
696 switch (pLogFont->usCodePage)
697 {
698 default:
699 wxFAIL_MSG(wxT("unsupported charset"));
700 // fall through
701
702 case 850:
703 vFontEncoding = wxFONTENCODING_CP1252;
704 break;
705
706 case 1250:
707 vFontEncoding = wxFONTENCODING_CP1250;
708 break;
709
710 case 921:
711 vFontEncoding = wxFONTENCODING_CP1257;
712 break;
713
714 case 866:
715 vFontEncoding = wxFONTENCODING_CP1251;
716 break;
717
718 case 864:
719 vFontEncoding = wxFONTENCODING_CP1256;
720 break;
721
722 case 869:
723 vFontEncoding = wxFONTENCODING_CP1253;
724 break;
725
726 case 862:
727 vFontEncoding = wxFONTENCODING_CP1255;
728 break;
729
730 case 857:
731 vFontEncoding = wxFONTENCODING_CP1254;
732 break;
733
734 case 874:
735 vFontEncoding = wxFONTENCODING_CP437;
736 break;
737
738 case 437:
739 vFontEncoding = wxFONTENCODING_CP437;
740 break;
741 }
742
743 return wxFont( nFontPoints
744 ,nFontFamily
745 ,nFontStyle
746 ,nFontWeight
747 ,bFontUnderline
748 ,sFontFace
749 ,vFontEncoding
750 );
751 } // end of wxCreateFontFromLogFont
752
753 int wxGpiStrcmp(
754 char* s0
755 , char* s1
756 )
757 { int l0;
758 int l1;
759 int l;
760 int d;
761 int d1;
762 int i;
763 int rc;
764
765 rc = 0;
766 if(s0 == NULL)
767 {
768 if(s1 == NULL)
769 return 0;
770 else
771 return 32;
772 }
773 else if(s1 == NULL)
774 return 32;
775
776 l0 = strlen(s0);
777 l1 = strlen(s1);
778 l = l0;
779 if(l0 != l1)
780 {
781 rc++;
782 if(l1 < l0)
783 l = l1;
784 }
785 for(i=0;i<l;i++)
786 {
787 d = s0[i]-s1[i];
788 if(!d)
789 continue;
790 d1 = toupper(s0[i]) - toupper(s1[i]);
791 if(!d1)
792 continue;
793 rc += abs(d);
794 }
795 return rc;
796 }
797