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