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