Improved the handling of font encodings.
[wine] / graphics / x11drv / xfont.c
1 /*
2  * X11 physical font objects
3  *
4  * Copyright 1997 Alex Korobka
5  *
6  * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
7  *       have to be changed for that), dynamic font loading (FreeType).
8  */
9
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <pwd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include "ts_xlib.h"
20 #include <X11/Xatom.h>
21 #include <math.h>
22 #include <assert.h>
23 #include "heap.h"
24 #include "options.h"
25 #include "x11font.h"
26 #include "font.h"
27 #include "debug.h"
28
29 #define X_PFONT_MAGIC           (0xFADE0000)
30 #define X_FMC_MAGIC             (0x0000CAFE)
31
32 #define MAX_FONT_FAMILIES       128
33 #define MAX_LFD_LENGTH          256
34
35 #define REMOVE_SUBSETS          1
36 #define UNMARK_SUBSETS          0
37
38 #define DEF_SCALABLE_HEIGHT     24
39 #define DEF_SCALABLE_DP         240
40
41 #define FF_FAMILY       (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
42
43 typedef struct __fontAlias
44 {
45   LPSTR                 faTypeFace;
46   LPSTR                 faAlias;
47   struct __fontAlias*   next;
48 } fontAlias;
49
50 typedef struct
51 {
52   LPSTR                 fatResource;
53   LPSTR                 fatAlias;
54 } aliasTemplate;
55
56 /* Font alias table - these 2 aliases are always present */
57
58 static fontAlias __aliasTable[2] = { 
59                         { "Helvetica", "Helv", &__aliasTable[1] },
60                         { "Times", "Tms Rmn", NULL } 
61                         };
62
63 static fontAlias *aliasTable = __aliasTable;
64
65 /* Optional built-in aliases, they are installed only when X
66  * cannot supply us with original MS fonts */
67
68 static int           faTemplateNum = 4;
69 static aliasTemplate faTemplate[4] = {
70                           { "-adobe-helvetica-", "MS Sans Serif" },
71                           { "-bitstream-charter-", "MS Serif" },
72                           { "-adobe-times-", "Times New Roman" },
73                           { "-adobe-helvetica-", "Arial" }
74                           };
75
76 UINT16                  XTextCaps = TC_OP_CHARACTER | TC_OP_STROKE |
77 TC_CP_STROKE | TC_CR_ANY |
78                                     TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
79                                     TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE;
80
81                         /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
82
83 static const char*      INIWinePrefix = "/.wine";
84 static const char*      INIFontMetrics = "/.cachedmetrics";
85 static const char*      INIFontSection = "fonts";
86 static const char*      INISubSection = "Alias";
87 static const char*      INIDefault = "Default";
88 static const char*      INIDefaultFixed = "DefaultFixed";
89 static const char*      INIResolution = "Resolution";
90 static const char*      INIGlobalMetrics = "FontMetrics";
91
92 static const char*      LFDSeparator = "*-";
93 static const char*      MSEncoding = "microsoft-";
94 static const char*      iso8859Encoding = "iso8859-";
95 static const char*      iso646Encoding = "iso646.1991-";
96 static const char*      ansiEncoding = "ansi-";
97 static unsigned         DefResolution = 0;
98
99 static fontResource*    fontList = NULL;
100 static fontObject*      fontCache = NULL;               /* array */
101 static int              fontCacheSize = FONTCACHE;
102 static int              fontLF = -1, fontMRU = -1;      /* last free, most recently used */
103
104 #define __PFONT(pFont)     ( fontCache + ((UINT32)(pFont) & 0x0000FFFF) )
105 #define CHECK_PFONT(pFont) ( (((UINT32)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
106                              (((UINT32)(pFont) & 0x0000FFFF) < fontCacheSize) )
107
108 static INT32 XFONT_IsSubset(fontInfo*, fontInfo*);
109 static void  XFONT_CheckFIList(fontResource*, fontInfo*, int subset_action);
110 static void  XFONT_GrowFreeList(int start, int end);
111
112
113 static Atom RAW_ASCENT;
114 static Atom RAW_DESCENT;
115
116 /***********************************************************************
117  *           Helper macros from X distribution
118  */
119
120 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
121                              (((cs)->rbearing|(cs)->lbearing| \
122                                (cs)->ascent|(cs)->descent) == 0))
123
124 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
125 { \
126     cs = def; \
127     if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
128         if (fs->per_char == NULL) { \
129             cs = &fs->min_bounds; \
130         } else { \
131             cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
132             if (CI_NONEXISTCHAR(cs)) cs = def; \
133         } \
134     } \
135 }
136
137 #define CI_GET_DEFAULT_INFO(fs,cs) \
138   CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
139
140 /***********************************************************************
141  *           Checksums
142  */
143 static UINT16   __lfCheckSum( LPLOGFONT16 plf )
144 {
145     CHAR        font[LF_FACESIZE];
146     UINT16      checksum = 0;
147     UINT16      i;
148
149 #define ptr ((UINT16*)plf)
150    for( i = 0; i < 9; i++ ) checksum ^= *ptr++;
151 #undef ptr
152    i = 0;
153 #define ptr ((CHAR*)plf)
154    do { font[i++] = tolower(*ptr++); } while (( i < LF_FACESIZE) && (*ptr) && (*ptr!=' '));
155    for( ptr = font, i >>= 1; i > 0; i-- ) 
156 #undef ptr
157 #define ptr ((UINT16*)plf)
158         checksum ^= *ptr++;
159 #undef ptr
160    return checksum;
161 }
162
163 static UINT16   __genericCheckSum( const void *ptr, int size )
164 {
165    unsigned int checksum = 0;
166    const char *p = (const char *)ptr;
167    while (size-- > 0)
168      checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
169
170    return checksum & 0xffff;
171 }
172
173 /*************************************************************************
174  *           LFD parse/compose routines
175  */
176 static char* LFD_Advance(LPSTR lpFont, UINT16 uParmsNo)
177 {
178   int   j = 0;
179   char* lpch = lpFont;
180
181   for( ; j < uParmsNo && *lpch; lpch++ ) if( *lpch == LFDSeparator[1] ) j++;
182   return lpch;
183 }
184
185 static void LFD_GetWeight( fontInfo* fi, LPSTR lpStr, int j)
186 {
187   if( j == 1 && *lpStr == '0' )
188       fi->fi_flags |= FI_POLYWEIGHT;
189   else if( j == 4 )
190        {
191          if( !strncasecmp( "bold", lpStr, 4) )
192            fi->df.dfWeight = FW_BOLD; 
193          else if( !strncasecmp( "demi", lpStr, 4) )
194          {
195            fi->fi_flags |= FI_FW_DEMI;
196            fi->df.dfWeight = FW_DEMIBOLD;
197          }
198          else if( !strncasecmp( "book", lpStr, 4) )
199          {
200            fi->fi_flags |= FI_FW_BOOK;
201            fi->df.dfWeight = FW_REGULAR;
202          }
203        }
204   else if( j == 5 )
205        {
206          if( !strncasecmp( "light", lpStr, 5) )
207            fi->df.dfWeight = FW_LIGHT;
208          else if( !strncasecmp( "black", lpStr, 5) )
209            fi->df.dfWeight = FW_BLACK;
210        }
211   else if( j == 6 && !strncasecmp( "medium", lpStr, 6) )
212       fi->df.dfWeight = FW_REGULAR; 
213   else if( j == 8 && !strncasecmp( "demibold", lpStr, 8) )
214       fi->df.dfWeight = FW_DEMIBOLD; 
215   else
216       fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
217                                       * from the weight property */
218 }
219
220 static int LFD_GetSlant( fontInfo* fi, LPSTR lpStr, int l)
221 {
222     if( l == 1 )
223     {
224         switch( tolower( *lpStr ) )
225         {
226             case '0':  fi->fi_flags |= FI_POLYSLANT;    /* haven't seen this one yet */
227             default:
228             case 'r':  fi->df.dfItalic = 0;
229                        break;
230             case 'o':
231                        fi->fi_flags |= FI_OBLIQUE;
232             case 'i':  fi->df.dfItalic = 1;
233                        break;
234         }
235         return 0;
236     }
237     return 1;
238 }
239
240 /*************************************************************************
241  *           LFD_InitFontInfo
242  *
243  * Fill in some fields in the fontInfo struct.
244  */
245 static int LFD_InitFontInfo( fontInfo* fi, LPSTR lpstr )
246 {
247    LPSTR        lpch;
248    int          i, j, dec_style_check, scalability;
249    UINT16       tmp[3];
250
251    memset(fi, 0, sizeof(fontInfo) );
252
253 /* weight name - */
254    lpch = LFD_Advance( lpstr, 1);
255    if( !*lpch ) return FALSE;
256    j = lpch - lpstr - 1;
257    LFD_GetWeight( fi, lpstr, j );
258
259 /* slant - */
260    lpch = LFD_Advance( lpstr = lpch, 1);
261    if( !*lpch ) return FALSE;
262    j = lpch - lpstr - 1;
263    dec_style_check = LFD_GetSlant( fi, lpstr, j );
264
265 /* width name - */
266    lpch = LFD_Advance( lpstr = lpch, 1);
267    if( !*lpch ) return FALSE;
268    if( strncasecmp( "normal", lpstr, 6) )       /* XXX 'narrow', 'condensed', etc... */
269        dec_style_check = TRUE;
270    else
271        fi->fi_flags |= FI_NORMAL;
272
273 /* style - */
274    lpch = LFD_Advance( lpstr = lpch, 1);
275    if( !*lpch ) return FALSE;
276    j = lpch - lpstr - 1;
277    if( j > 3 )  /* find out is there "sans" or "script" */
278    {
279         j = 0;
280         *(lpch - 1) = '\0';
281
282         if( strstr(lpstr, "sans") ) 
283         { 
284             fi->df.dfPitchAndFamily |= FF_SWISS; 
285             j = 1; 
286         }
287         if( strstr(lpstr, "script") ) 
288         { 
289             fi->df.dfPitchAndFamily |= FF_SCRIPT; 
290             j = 1; 
291         }
292         if( !j && dec_style_check ) 
293             fi->df.dfPitchAndFamily |= FF_DECORATIVE;
294         *(lpch - 1) = LFDSeparator[1];
295    }
296
297 /* pixel height, decipoint height, and res_x */
298
299    for( i = scalability = 0; i < 3; i++ )
300    {
301      lpch = LFD_Advance( lpstr = lpch, 1);
302      if( !*lpch ) return FALSE;
303      if( lpch - lpstr == 1 || lpch - lpstr > 4 ) return FALSE; /* ridiculous */
304
305     *(lpch - 1) = '\0';
306      if( !(tmp[i] = atoi(lpstr)) ) scalability++;
307     *(lpch - 1) = LFDSeparator[1];
308    }
309    if( scalability == 3 )       /* Type1 */
310    {
311      fi->fi_flags |= FI_SCALABLE;
312      fi->lfd_height = DEF_SCALABLE_HEIGHT; fi->lfd_decipoints = DEF_SCALABLE_DP;
313      fi->lfd_resolution = DefResolution;
314    }
315    else if( scalability == 0 )  /* Bitmap */
316    {
317      fi->lfd_height = tmp[0]; fi->lfd_decipoints = tmp[1]; 
318      fi->lfd_resolution = tmp[2];
319    }
320    else return FALSE; /* #$%^!!! X11R6 mutant garbage */
321
322 /* res_y - skip, spacing - */
323    lpstr = LFD_Advance( lpch, 1);
324    switch( *lpstr )
325    {
326      case '\0': return FALSE;
327
328      case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
329                break;
330      case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
331                fi->fi_flags |= FI_FIXEDEX;
332                /* fall through */
333      case 'm': fi->fi_flags |= FI_FIXEDPITCH;
334                break;
335      default:
336                fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
337    }
338    lpstr = LFD_Advance(lpstr, 1);
339    if( !*lpstr ) return FALSE;
340
341 /* average width - */
342    lpch = LFD_Advance( lpstr, 1);
343    if( !*lpch ) return FALSE;
344    if( lpch - lpstr == 1 || lpch - lpstr > 4 ) return FALSE; /* ridiculous */
345   *(lpch - 1) = '\0'; 
346    if( !(fi->lfd_width = atoi(lpstr)) && !scalability ) return FALSE;
347   *(lpch - 1) = LFDSeparator[1];
348
349 /* charset registry, charset encoding - */
350    if( strstr(lpch, "jisx") || 
351        strstr(lpch, "ksc") || 
352        strstr(lpch, "gb2312") ||
353        strstr(lpch, "big5") ||
354        strstr(lpch, "unicode") ) return FALSE;  /* 2-byte stuff */
355
356    fi->df.dfCharSet = ANSI_CHARSET;
357    if( strstr(lpch, iso8859Encoding) ) {
358      fi->fi_flags |= FI_ENC_ISO8859;
359      if( strstr(lpch, "iso8859-15") ) fi->df.dfCharSet = ANSI_CHARSET;
360      else if( strstr(lpch, "iso8859-11") ) fi->df.dfCharSet = THAI_CHARSET;
361      else if( strstr(lpch, "iso8859-10") ) fi->df.dfCharSet = BALTIC_CHARSET;
362      else if( strstr(lpch, "iso8859-9") ) fi->df.dfCharSet = TURKISH_CHARSET;
363      else if( strstr(lpch, "iso8859-8") ) fi->df.dfCharSet = HEBREW_CHARSET;
364      else if( strstr(lpch, "iso8859-7") ) fi->df.dfCharSet = GREEK_CHARSET;
365      else if( strstr(lpch, "iso8859-6") ) fi->df.dfCharSet = ARABIC_CHARSET;
366      else if( strstr(lpch, "iso8859-5") ) fi->df.dfCharSet = RUSSIAN_CHARSET;
367      else if( strstr(lpch, "iso8859-4") ) fi->df.dfCharSet = ISO4_CHARSET;
368      else if( strstr(lpch, "iso8859-3") ) fi->df.dfCharSet = ISO3_CHARSET;
369      else if( strstr(lpch, "iso8859-2") ) fi->df.dfCharSet = EE_CHARSET;
370      else if( strstr(lpch, "iso8859-1") ) fi->df.dfCharSet = ANSI_CHARSET;
371      else fi->df.dfCharSet = SYMBOL_CHARSET;
372    } else if( strstr(lpch, iso646Encoding) ) {
373      fi->fi_flags |= FI_ENC_ISO646;
374    } else if( strstr(lpch, ansiEncoding) ) { /* fnt2bdf produces -ansi-0 LFD */
375      fi->fi_flags |= FI_ENC_ANSI;
376    } else {                                  /* ... and -microsoft-cp125x */
377    
378         fi->df.dfCharSet = OEM_CHARSET;
379         if( !strncasecmp(lpch, "microsoft-", 10) ) {
380             fi->fi_flags |= FI_ENC_MSCODEPAGE;
381             if( strstr(lpch, "-cp1250") ) fi->df.dfCharSet = EE_CHARSET;
382             else if( strstr(lpch, "-cp1251") ) fi->df.dfCharSet = RUSSIAN_CHARSET;
383             else if( strstr(lpch, "-cp1252") ) fi->df.dfCharSet = ANSI_CHARSET;
384             else if( strstr(lpch, "-cp1253") ) fi->df.dfCharSet = GREEK_CHARSET;
385             else if( strstr(lpch, "-cp1254") ) fi->df.dfCharSet = TURKISH_CHARSET;
386             else if( strstr(lpch, "-cp1255") ) fi->df.dfCharSet = HEBREW_CHARSET;
387             else if( strstr(lpch, "-cp1256") ) fi->df.dfCharSet = ARABIC_CHARSET;
388             else if( strstr(lpch, "-cp1257") ) fi->df.dfCharSet = BALTIC_CHARSET;
389             else if( strstr(lpch, "-fontspecific") ) fi->df.dfCharSet = ANSI_CHARSET;
390             else if( strstr(lpch, "-symbol") ) fi->df.dfCharSet = SYMBOL_CHARSET;
391             else fi->df.dfCharSet = SYMBOL_CHARSET;
392         } else if( !strncasecmp(lpch, "koi8-", 5) ) {
393             fi->df.dfCharSet = KOI8_CHARSET;
394         } else if( !strncasecmp(lpch, "viscii", 6) ) {
395             fi->fi_flags |= FI_ENC_ISO8859;
396             fi->df.dfCharSet = VISCII_CHARSET;
397         } else if( !strncasecmp(lpch, "tcvn-", 5) ) {
398             fi->df.dfCharSet = TCVN_CHARSET;
399         } else if( !strncasecmp(lpch, "tis620", 6) ) {
400             fi->fi_flags |= FI_ENC_ISO8859;
401             fi->df.dfCharSet = THAI_CHARSET;
402         } else if( !strncasecmp(lpch, "ascii", 5) ) {
403             fi->fi_flags |= FI_ENC_ISO646;
404             fi->df.dfCharSet = ANSI_CHARSET;
405         } else if( strstr(lpch, "fontspecific") ||
406                  strstr(lpch, "microsoft-symbol") ) {
407             fi->df.dfCharSet = SYMBOL_CHARSET;
408         }
409    }
410    return TRUE;                                 
411 }
412
413
414 /*************************************************************************
415  *           LFD_ComposeLFD
416  */
417 static BOOL32  LFD_ComposeLFD( fontObject* fo, 
418                                INT32 height, LPSTR lpLFD, UINT32 uRelax )
419 {
420    int          h, w, ch, point = 0;
421    char*        lpch; 
422    char         lpEncoding[32];
423    char         h_string[64], point_string[64];
424
425    *(lpLFD+MAX_LFD_LENGTH-1)=0;
426    lstrcpy32A( lpLFD, fo->fr->resource );
427
428 /* add weight */
429    switch( fo->fi->df.dfWeight )
430    {
431         case FW_BOLD:
432                 strcat( lpLFD, "bold" ); break;
433         case FW_REGULAR:
434                 if( fo->fi->fi_flags & FI_FW_BOOK )
435                     strcat( lpLFD, "book" );
436                 else
437                     strcat( lpLFD, "medium" );
438                 break;
439         case FW_DEMIBOLD:
440                 strcat( lpLFD, "demi" );
441                 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
442                      strcat ( lpLFD, "bold" );
443                 break;
444         case FW_BLACK:
445                 strcat( lpLFD, "black" ); break;
446         case FW_LIGHT:
447                 strcat( lpLFD, "light" ); break;
448         default:
449                 strcat( lpLFD, "*" );
450    }
451
452 /* add slant */
453    if( fo->fi->df.dfItalic )
454        if( fo->fi->fi_flags & FI_OBLIQUE )
455            strcat( lpLFD, "-o" );
456        else
457            strcat( lpLFD, "-i" );
458    else 
459        strcat( lpLFD, (uRelax < 4) ? "-r" : "-*" );
460
461 /* add width style and skip serifs */
462    if( fo->fi->fi_flags & FI_NORMAL )
463        strcat( lpLFD, "-normal-*-");
464    else
465        strcat( lpLFD, "-*-*-" );
466
467 /* add pixelheight, pointheight, and resolution 
468  *
469  * FIXME: fill in lpXForm and lpPixmap for rotated fonts
470  */
471    if( fo->fo_flags & FO_SYNTH_HEIGHT ) h = fo->fi->lfd_height;
472    else h = (fo->fi->lfd_height * height) / fo->fi->df.dfPixHeight;
473
474    if( XTextCaps & TC_SF_X_YINDEP ) 
475    {
476         if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH) )
477             point = (fo->fi->lfd_decipoints * fo->lf.lfWidth) / fo->fi->df.dfAvgWidth;
478         else 
479         if( fo->fi->fi_flags & FI_SCALABLE ) /* adjust h/w ratio */
480             point = h * 72 * 10 / fo->fi->lfd_resolution;
481    }
482
483    /* handle rotated fonts */
484    if (fo->lf.lfEscapement) {
485      /* escapement is in tenths of degrees, theta is in radians */
486      double theta = M_PI*fo->lf.lfEscapement/1800.;  
487      double h_matrix[4] = {h*cos(theta), h*sin(theta), -h*sin(theta), h*cos(theta)};
488      double point_matrix[4] = {point*cos(theta), point*sin(theta), -point*sin(theta), point*cos(theta)};
489      char *s;
490      sprintf(h_string, "[%+f%+f%+f%+f]", h_matrix[0], h_matrix[1], h_matrix[2], h_matrix[3]);
491      sprintf(point_string, "[%+f%+f%+f%+f]", point_matrix[0], point_matrix[1], point_matrix[2], point_matrix[3]);
492      while ((s = strchr(h_string, '-'))) *s='~';
493      while ((s = strchr(point_string, '-'))) *s='~';
494    } else {
495      sprintf(h_string, "%d", h);
496      sprintf(point_string, "%d", point);
497    }
498
499
500 /* spacing and width */
501
502    if( fo->fi->fi_flags & FI_FIXEDPITCH ) 
503        w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? 'c' : 'm';
504    else 
505        w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? 'p' : LFDSeparator[0]; 
506
507 /* encoding */
508
509 #define CHRS_CASE1(charset)     case 0: \
510                                 case 3: \
511                                 case 6: \
512                                 case 9: sprintf(lpEncoding, charset ); break; 
513 #define CHRS_CASE2(charset)     case 1: \
514                                 case 4: \
515                                 case 7: \
516                                 case 10: sprintf(lpEncoding, charset ); break;
517 #define CHRS_CASE3(charset)     case 2: \
518                                 case 5: \
519                                 case 8: \
520                                 case 11: sprintf(lpEncoding, charset ); break;
521 #define CHRS_DEF(charset)       default: sprintf(lpEncoding, charset ); break;
522
523    if( fo->fi->df.dfCharSet == ANSI_CHARSET )
524    {
525         if( fo->fi->fi_flags & FI_ENC_ISO8859 )
526         switch (uRelax) {
527             CHRS_CASE1( "iso8859-1" );
528             CHRS_CASE2( "iso8859-1" );
529             CHRS_CASE3( "iso8859-15" );
530             CHRS_DEF( "iso8859-*" );
531         }
532         else if( fo->fi->fi_flags & FI_ENC_ISO646 )
533         switch (uRelax) {
534             CHRS_CASE1( "ascii-0" );
535             CHRS_DEF( "iso8859-1" );
536         }
537         else if( fo->fi->fi_flags & FI_ENC_MSCODEPAGE )
538         switch (uRelax) {
539             CHRS_CASE1( "microsoft-cp1252" );
540             CHRS_CASE2( "microsoft-fontspecific" );
541             CHRS_CASE3( "microsoft-cp125*" );
542             CHRS_DEF( "microsoft-*" );
543         }
544         else
545         switch (uRelax) {
546             CHRS_CASE1( "ansi-0" );
547             CHRS_CASE2( "microsoft-125*" );
548             CHRS_CASE3( "microsoft-*");
549             CHRS_DEF( "iso8859-*" );
550         }
551    } 
552    else if( fo->fi->fi_flags & FI_ENC_MSCODEPAGE )
553    {
554         switch (fo->fi->df.dfCharSet) {
555         case EE_CHARSET: 
556                 switch (uRelax) {
557                 CHRS_CASE1( "microsoft-1250" );
558                 CHRS_CASE2( "iso8859-2" );
559                 CHRS_DEF( "iso8859-*" );
560                 } ; break;
561         case RUSSIAN_CHARSET:
562                 switch (uRelax) {
563                 CHRS_CASE1( "microsoft-1251" );
564                 CHRS_CASE2( "iso8859-5" );
565                 CHRS_CASE3( "koi8-*" );
566                 CHRS_DEF( "iso8859-*" );
567                 } ; break;
568         case ANSI_CHARSET:
569                 switch (uRelax) {
570                 CHRS_CASE1( "microsoft-1252" );
571                 CHRS_CASE2( "iso8859-1" );
572                 CHRS_CASE3( "iso8859-15" );
573                 CHRS_DEF( "iso8859-*" );
574                 } ; break;
575         case GREEK_CHARSET:
576                 switch (uRelax) {
577                 CHRS_CASE1( "microsoft-1253" );
578                 CHRS_CASE2( "iso8859-7" );
579                 CHRS_DEF( "iso8859-*" );
580                 } ; break;
581         case TURKISH_CHARSET:
582                 switch (uRelax) {
583                 CHRS_CASE1( "microsoft-1254" );
584                 CHRS_CASE2( "iso8859-9" );
585                 CHRS_DEF( "iso8859-*" );
586                 } ; break;
587         case HEBREW_CHARSET:
588                 switch (uRelax) {
589                 CHRS_CASE1( "microsoft-1255" );
590                 CHRS_CASE2( "iso8859-8" );
591                 CHRS_DEF( "iso8859-*" );
592                 } ; break;
593         case ARABIC_CHARSET:
594                 switch (uRelax) {
595                 CHRS_CASE1( "microsoft-1256" );
596                 CHRS_CASE2( "iso8859-6" );
597                 CHRS_DEF( "iso8859-*" );
598                 } ; break;
599         case BALTIC_CHARSET:
600                 switch (uRelax) {
601                 CHRS_CASE1( "microsoft-1257" );
602                 CHRS_CASE2( "iso8859-10" );
603                 CHRS_CASE3( "iso8859-15" );
604                 CHRS_DEF( "iso8859-*" );
605                 } ; break;
606         case THAI_CHARSET:
607                 switch (uRelax) {
608                 CHRS_CASE1( "iso8859-11" );
609                 CHRS_CASE2( "tis620*" );
610                 CHRS_DEF( "iso8859-*" );
611                 } ; break;
612         case VISCII_CHARSET:
613                 switch (uRelax) {
614                 CHRS_CASE1( "viscii1.1-1" );
615                 CHRS_CASE2( "viscii*" );
616                 CHRS_DEF( "iso8859-*" );
617                 } ; break;
618         case TCVN_CHARSET:
619                 switch (uRelax) {
620                 CHRS_CASE1( "tcvn-0" );
621                 CHRS_CASE2( "tcvn*" );
622                 CHRS_DEF( "iso8859-*" );
623                 } ; break;
624         case KOI8_CHARSET:
625                 switch (uRelax) {
626                 CHRS_CASE1( "koi8-ru" );
627                 CHRS_CASE2( "koi8-r" );
628                 CHRS_CASE3( "koi8-*" );
629                 CHRS_DEF( "iso8859-*" );
630                 } ; break;
631         case ISO3_CHARSET:
632                 switch (uRelax) {
633                 CHRS_CASE1( "iso8859-3" );
634                 CHRS_DEF( "iso8859-*" );
635                 } ; break;
636         case ISO4_CHARSET:
637                 switch (uRelax) {
638                 CHRS_CASE1( "iso8859-4" );
639                 CHRS_DEF( "iso8859-*" );
640                 } ; break;
641         default:
642                 switch (uRelax) {
643                 CHRS_CASE1( "microsoft-symbol" );
644                 CHRS_DEF( "microsoft-fontspecific" );
645                 } ; break;
646         }
647    }
648    else {
649         switch (uRelax) {
650         CHRS_CASE1( "*-fontspecific" );
651         CHRS_CASE2( "*-symbol" );
652         CHRS_DEF( "*" ); /* whatever */
653         }
654    }
655
656    lpch = lpLFD + lstrlen32A(lpLFD);
657    ch = (fo->fi->fi_flags & FI_SCALABLE) ? '0' : LFDSeparator[0];
658
659    switch( uRelax )
660    {
661        /* RealizeFont() will call us repeatedly with increasing uRelax 
662         * until XLoadFont() succeeds. */
663
664        case 0: 
665        case 1:
666        case 2:
667             if( point )
668             {
669                 sprintf( lpch, "%s-%s-%i-%c-%c-*-%s", h_string, 
670                          point_string, 
671                          fo->fi->lfd_resolution, ch, w, lpEncoding );
672                 break;
673             }
674             /* fall through */
675
676        case 3: 
677        case 4:
678        case 5:
679             sprintf( lpch, "%s-*-%i-%c-%c-*-%s", h_string, 
680                         fo->fi->lfd_resolution, ch, w, lpEncoding );
681             break;
682
683        case 6:
684        case 7:
685        case 8:
686             sprintf( lpch, "%s-*-%i-%c-*-*-%s",
687                         h_string, fo->fi->lfd_resolution, ch, lpEncoding );
688             break;
689
690        case 9:
691        case 10:
692        case 11:
693             sprintf( lpch, "%i-*-%i-%c-*-*-%s", fo->fi->lfd_height,
694                         fo->fi->lfd_resolution, ch, lpEncoding );
695             break;
696
697        case 12:
698             sprintf( lpch, "%i-*-*-*-*-*-%s", fo->fi->lfd_height, lpEncoding );
699             break;
700
701        default:
702             sprintf( lpch, "%i-*-*-*-*-*-*", fo->fi->lfd_height);
703             break;
704
705        /* to avoid an infinite loop; those will allways match */
706        case 200:
707             sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
708             break;
709        case 201:
710             sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*" );
711             break;
712    }
713
714    TRACE(font,"\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
715    assert(*(lpLFD+MAX_LFD_LENGTH-1)==0); /* check if overwrittem */
716    return TRUE;
717 }
718
719
720 /***********************************************************************
721  *              X Font Resources
722  *
723  * font info            - http://www.microsoft.com/kb/articles/q65/1/23.htm
724  * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
725  */
726 static BOOL32 XFONT_GetLeading( LPIFONTINFO16 pFI, XFontStruct* x_fs, INT32*
727        pIL, INT32* pEL, XFONTTRANS *XFT )
728 {
729     unsigned long height;
730     unsigned min = (unsigned char)pFI->dfFirstChar;
731     unsigned max = (unsigned char)pFI->dfLastChar;
732     BOOL32 bHaveCapHeight = (pFI->dfCharSet == ANSI_CHARSET && 'X' >= min && 'X' <= max );
733
734     if( pEL ) *pEL = 0;
735
736     if(XFT) {
737         Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
738         if(TSXGetFontProperty(x_fs, RAW_CAP_HEIGHT, &height))
739             *pIL = XFT->ascent - 
740                             (INT32)(XFT->pixelsize / 1000.0 * height);
741         else
742             *pIL = 0;
743         return bHaveCapHeight && x_fs->per_char;
744     }
745        
746     if( TSXGetFontProperty(x_fs, XA_CAP_HEIGHT, &height) == FALSE )
747     {
748         if( x_fs->per_char )
749             if( bHaveCapHeight )
750                     height = x_fs->per_char['X' - min].ascent;
751             else
752                 if (x_fs->ascent >= x_fs->max_bounds.ascent)
753                     height = x_fs->max_bounds.ascent;
754                 else
755                 {
756                     height = x_fs->ascent;
757                     if( pEL )
758                         *pEL = x_fs->max_bounds.ascent - height;
759                 }
760         else 
761             height = x_fs->min_bounds.ascent;
762     }
763
764    *pIL = x_fs->ascent - height;
765     return (bHaveCapHeight && x_fs->per_char);
766 }
767
768 static INT32 XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, XFontStruct* x_fs,
769                                     XFONTTRANS *XFT)
770 {
771     unsigned min = (unsigned char)pFI->dfFirstChar;
772     unsigned max = (unsigned char)pFI->dfLastChar;
773
774     if( x_fs->per_char )
775     {
776         int  width, chars, j;
777         for( j = 0, width = 0, chars = 0, max -= min; j <= max; j++ )
778             if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
779             {
780                 if(!XFT)
781                     width += x_fs->per_char[j].width;
782                 else
783                     width += x_fs->per_char[j].attributes * 
784                       XFT->pixelsize / 1000.0;
785                 chars++;
786             }
787         return (width / chars);
788     }
789     /* uniform width */
790     return x_fs->min_bounds.width;
791 }
792
793 static INT32 XFONT_GetMaxCharWidth(fontObject *pfo)
794 {
795     unsigned min = (unsigned char)pfo->fs->min_char_or_byte2;
796     unsigned max = (unsigned char)pfo->fs->max_char_or_byte2;
797
798     if(!pfo->lpX11Trans)
799         return abs(pfo->fs->max_bounds.width);
800
801     if( pfo->fs->per_char )
802     {
803         int  maxwidth, j;
804         for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
805             if( !CI_NONEXISTCHAR(pfo->fs->per_char + j) )
806                 if(maxwidth < pfo->fs->per_char[j].attributes)
807                     maxwidth = pfo->fs->per_char[j].attributes;
808
809         maxwidth *= pfo->lpX11Trans->pixelsize / 1000.0;
810         return maxwidth;
811     }
812     return pfo->foAvgCharWidth;
813 }
814
815 /***********************************************************************
816  *              XFONT_SetFontMetric
817  *
818  * Initializes IFONTINFO16. dfHorizRes and dfVertRes must be already set.
819  */
820 static void XFONT_SetFontMetric(fontInfo* fi, fontResource* fr, XFontStruct* xfs)
821 {
822     unsigned     min, max;
823     INT32        el, il;
824
825     fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
826     fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
827
828     fi->df.dfDefaultChar = (BYTE)xfs->default_char;
829     fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
830
831     fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
832     fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
833     fi->df.dfMaxWidth = (INT16)abs(xfs->max_bounds.width);
834
835     if( XFONT_GetLeading( &fi->df, xfs, &il, &el, NULL ) )
836         fi->df.dfAvgWidth = (INT16)xfs->per_char['X' - min].width;
837     else
838         fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth( &fi->df, xfs, NULL);
839
840     fi->df.dfInternalLeading = (INT16)il;
841     fi->df.dfExternalLeading = (INT16)el;
842
843     fi->df.dfPoints = (INT16)(((INT32)(fi->df.dfPixHeight - 
844                fi->df.dfInternalLeading) * 72 + (fi->df.dfVertRes >> 1)) / fi->df.dfVertRes);
845
846     if( xfs->min_bounds.width != xfs->max_bounds.width )
847         fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
848     if( fi->fi_flags & FI_SCALABLE ) 
849     {
850         fi->df.dfType = DEVICE_FONTTYPE;
851         fi->df.dfPitchAndFamily |= TMPF_DEVICE;
852     } 
853     else if( fi->fi_flags & FI_TRUETYPE )
854         fi->df.dfType = TRUETYPE_FONTTYPE;
855     else
856         fi->df.dfType = RASTER_FONTTYPE;
857
858     fi->df.dfFace = fr->lfFaceName;
859 }
860
861 /***********************************************************************
862  *              XFONT_GetTextMetric
863  */
864 static void XFONT_GetTextMetric( fontObject* pfo, LPTEXTMETRIC32A pTM )
865 {
866     LPIFONTINFO16 pdf = &pfo->fi->df;
867
868     if( ! pfo->lpX11Trans ) {
869       pTM->tmAscent = pfo->fs->ascent;
870       pTM->tmDescent = pfo->fs->descent;
871     } else {
872       pTM->tmAscent = pfo->lpX11Trans->ascent;
873       pTM->tmDescent = pfo->lpX11Trans->descent;
874     }
875     pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
876
877     pTM->tmAveCharWidth = pfo->foAvgCharWidth;
878     pTM->tmMaxCharWidth = pfo->foMaxCharWidth;
879
880     pTM->tmInternalLeading = pfo->foInternalLeading;
881     pTM->tmExternalLeading = pdf->dfExternalLeading;
882
883     pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
884                         ? 1 : pdf->dfStrikeOut;
885     pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
886                         ? 1 : pdf->dfUnderline;
887
888     pTM->tmOverhang = 0;
889     if( pfo->fo_flags & FO_SYNTH_ITALIC ) 
890     {
891         pTM->tmOverhang += pTM->tmHeight/3;
892         pTM->tmItalic = 1;
893     } else 
894         pTM->tmItalic = pdf->dfItalic;
895
896     pTM->tmWeight = pdf->dfWeight;
897     if( pfo->fo_flags & FO_SYNTH_BOLD ) 
898     {
899         pTM->tmOverhang++; 
900         pTM->tmWeight += 100;
901     } 
902
903     *(INT32*)&pTM->tmFirstChar = *(INT32*)&pdf->dfFirstChar;
904
905     pTM->tmCharSet = pdf->dfCharSet;
906     pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
907
908     pTM->tmDigitizedAspectX = pdf->dfHorizRes;
909     pTM->tmDigitizedAspectY = pdf->dfVertRes;
910 }
911
912 /***********************************************************************
913  *              XFONT_GetFontMetric
914  *
915  * Retrieve font metric info (enumeration).
916  */
917 static UINT32 XFONT_GetFontMetric( fontInfo* pfi, LPENUMLOGFONTEX16 pLF,
918                                                   LPNEWTEXTMETRIC16 pTM )
919 {
920     memset( pLF, 0, sizeof(*pLF) );
921     memset( pTM, 0, sizeof(*pTM) );
922
923 #define plf ((LPLOGFONT16)pLF)
924     plf->lfHeight    = pTM->tmHeight       = pfi->df.dfPixHeight;
925     plf->lfWidth     = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
926     plf->lfWeight    = pTM->tmWeight       = pfi->df.dfWeight;
927     plf->lfItalic    = pTM->tmItalic       = pfi->df.dfItalic;
928     plf->lfUnderline = pTM->tmUnderlined   = pfi->df.dfUnderline;
929     plf->lfStrikeOut = pTM->tmStruckOut    = pfi->df.dfStrikeOut;
930     plf->lfCharSet   = pTM->tmCharSet      = pfi->df.dfCharSet;
931
932     /* convert pitch values */
933
934     pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
935     plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
936
937     lstrcpyn32A( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
938 #undef plf
939
940     pTM->tmAscent = pfi->df.dfAscent;
941     pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
942     pTM->tmInternalLeading = pfi->df.dfInternalLeading;
943     pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
944     pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
945     pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
946
947     *(INT32*)&pTM->tmFirstChar = *(INT32*)&pfi->df.dfFirstChar;
948
949     /* return font type */
950
951     return pfi->df.dfType;
952 }
953
954
955 /***********************************************************************
956  *           XFONT_FixupFlags
957  * 
958  * dfPitchAndFamily flags for some common typefaces.
959  */
960 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
961 {
962    switch( lfFaceName[0] )
963    {
964         case 'h':
965         case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
966                     return FF_SWISS;
967                   break;
968         case 'c':
969         case 'C': if(!strcasecmp(lfFaceName, "Courier") ||
970                      !strcasecmp(lfFaceName, "Charter") )
971                     return FF_ROMAN;
972                   break;
973         case 'p':
974         case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
975                     return FF_ROMAN;
976                   break;
977         case 't':
978         case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
979                     return FF_ROMAN;
980                   break;
981         case 'u':
982         case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
983                     return FF_ROMAN;
984                   break;
985         case 'z':
986         case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
987                     return FF_DECORATIVE;
988    }
989    return 0;
990 }
991
992
993 /***********************************************************************
994  *           XFONT_CheckResourceName
995  */
996 static BOOL32 XFONT_CheckResourceName( LPSTR resource, LPCSTR name, INT32 n )
997 {
998     resource = LFD_Advance( resource, 2 );
999     if( resource )
1000         return (!strncasecmp( resource, name, n ));
1001     return FALSE;
1002 }
1003
1004
1005 /***********************************************************************
1006  *           XFONT_WindowsNames
1007  *
1008  * Build generic Windows aliases for X font names.
1009  *
1010  * -misc-fixed- -> "Fixed"
1011  * -sony-fixed- -> "Sony Fixed", etc...
1012  */
1013 static void XFONT_WindowsNames( char* buffer )
1014 {
1015     fontResource* fr, *pfr;
1016     char*       lpstr, *lpch;
1017     int         i, up;
1018     BYTE        bFamilyStyle;
1019     const char* relocTable[] = { INIDefaultFixed, INIDefault, NULL };
1020
1021     for( fr = fontList; fr ; fr = fr->next )
1022     {
1023         if( fr->fr_flags & FR_NAMESET ) continue;     /* skip already assigned */
1024
1025         lpstr = LFD_Advance(fr->resource, 2);
1026         i = LFD_Advance( lpstr, 1 ) - lpstr;
1027
1028         for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1029             if( pfr->fr_flags & FR_NAMESET )
1030                 if( XFONT_CheckResourceName( pfr->resource, lpstr, i ) )
1031                     break;
1032
1033         if( pfr != fr )  /* prepend vendor name */
1034             lpstr = fr->resource + 1;
1035
1036         for( i = 0, up = 1, lpch = fr->lfFaceName; *lpstr && i < 32;
1037                                                 lpch++, lpstr++, i++ )
1038         {
1039             if( *lpstr == LFDSeparator[1] || *lpstr == ' ' ) 
1040             { 
1041                 *lpch = ' '; 
1042                 up = 1; continue; 
1043             }
1044             else if( isalpha(*lpstr) && up )
1045             { 
1046                 *lpch = toupper(*lpstr); 
1047                 up = 0; continue; 
1048             }
1049             *lpch = *lpstr;
1050         }
1051         while (*(lpch - 1) == ' ') *(--lpch) = '\0';
1052
1053         if( (bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName )) )
1054         {
1055             fontInfo* fi;
1056             for( fi = fr->fi ; fi ; fi = fi->next )
1057                 fi->df.dfPitchAndFamily |= bFamilyStyle;
1058         }
1059
1060         TRACE(font,"typeface \'%s\'\n", fr->lfFaceName);
1061
1062         fr->fr_flags |= FR_NAMESET;
1063     }
1064
1065     for( up = 0; relocTable[up]; up++ )
1066         if( PROFILE_GetWineIniString( INIFontSection, relocTable[up], "", buffer, 128 ) )
1067         {
1068             while( *buffer && isspace(*buffer) ) buffer++;
1069             for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1070             {
1071                 i = strlen( pfr->resource );
1072                 if( !strncasecmp( pfr->resource, buffer, i) )
1073                 {
1074                     if( fr )
1075                     {
1076                         fr->next = pfr->next;
1077                         pfr->next = fontList;
1078                         fontList = pfr;
1079                     }
1080                     break;
1081                 }
1082                 fr = pfr;
1083             }
1084         }
1085 }
1086
1087 /***********************************************************************
1088  *           XFONT_CreateAlias
1089  */
1090 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1091 {
1092     int j;
1093     fontAlias* pfa = aliasTable;
1094
1095     while( 1 ) 
1096     {
1097         /* check if we already got one */
1098         if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1099         {
1100             TRACE(font,"\tredundant alias '%s' -> '%s'\n", 
1101                   lpAlias, lpTypeFace );
1102             return NULL;
1103         } 
1104         if( pfa->next ) pfa = pfa->next;
1105         else break;
1106     }
1107
1108     j = lstrlen32A(lpTypeFace) + 1;
1109     pfa->next = HeapAlloc( SystemHeap, 0, sizeof(fontAlias) +
1110                                           j + lstrlen32A(lpAlias) + 1 );
1111     if((pfa = pfa->next))
1112     {
1113         pfa->next = NULL;
1114         pfa->faTypeFace = (LPSTR)(pfa + 1);
1115         lstrcpy32A( pfa->faTypeFace, lpTypeFace );
1116         pfa->faAlias = pfa->faTypeFace + j;
1117         lstrcpy32A( pfa->faAlias, lpAlias );
1118
1119         TRACE(font, "\tadded alias '%s' for %s\n", lpAlias, lpTypeFace );
1120
1121         return pfa;
1122     }
1123     return NULL;
1124 }
1125
1126 /***********************************************************************
1127  *           XFONT_LoadAliases
1128  *
1129  * Read user-defined aliases from wine.conf. Format is as follows
1130  *
1131  * Alias# = [Windows font name],[LFD font name], <substitute original name>
1132  *
1133  * Example:
1134  *   Alias0 = Arial, -adobe-helvetica- 
1135  *   Alias1 = Times New Roman, -bitstream-courier-, 1
1136  *   ...
1137  *
1138  * Note that from 081797 and on we have built-in alias templates that take
1139  * care of the necessary Windows typefaces.
1140  */
1141 static void XFONT_LoadAliases( char** buffer, int buf_size )
1142 {
1143     char* lpResource, *lpAlias;
1144     char  subsection[32];
1145     int   i = 0, j = 0;
1146     BOOL32 bHaveAlias = TRUE, bSubst = FALSE;
1147
1148     if( buf_size < 128 )
1149         *buffer = HeapReAlloc(SystemHeap, 0, *buffer, 256 );
1150     do
1151     {
1152         if( j < faTemplateNum )
1153         {
1154             /* built-in templates first */
1155
1156             lpResource = faTemplate[j].fatResource;
1157             lpAlias = faTemplate[j].fatAlias;
1158             j++;
1159         }
1160         else
1161         {
1162             /* then WINE.CONF */
1163
1164             wsprintf32A( subsection, "%s%i", INISubSection, i++ );
1165
1166             if( (bHaveAlias = PROFILE_GetWineIniString( INIFontSection, 
1167                                         subsection, "", *buffer, 128 )) )
1168             {
1169                 lpAlias = *buffer;
1170                 while( isspace(*lpAlias) ) lpAlias++;
1171                 lpResource = PROFILE_GetStringItem( lpAlias );
1172                 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1173             }
1174         }
1175
1176         if( bHaveAlias )
1177         {
1178             int length;
1179
1180             length = strlen( lpAlias );
1181             if( lpResource && length )
1182             {
1183                 fontResource* fr, *frMatch = NULL;
1184
1185                 for (fr = fontList; fr ; fr = fr->next)
1186                 {
1187                     if( !strcasecmp( fr->resource, lpResource ) ) frMatch = fr;
1188                     if( XFONT_CheckResourceName( fr->resource, lpAlias, length ) )
1189                     {
1190                         /* alias is not needed since the real font is present */
1191                         frMatch = NULL; break;
1192                     }
1193                 }
1194
1195                 if( frMatch )
1196                 {
1197                     if( bSubst )
1198                     {
1199                         fontAlias *pfa, *prev = NULL;
1200
1201                         for(pfa = aliasTable; pfa; pfa = pfa->next)
1202                         {
1203         /* Remove lpAlias from aliasTable - we should free the old entry */
1204                             if(!strcmp(lpAlias, pfa->faAlias))
1205                             {
1206                                 if(prev)
1207                                     prev->next = pfa->next;
1208                                 else
1209                                     aliasTable = pfa->next;
1210                              }
1211
1212         /* Update any references to the substituted font in aliasTable */
1213                             if(!strcmp(frMatch->lfFaceName,
1214                                                         pfa->faTypeFace))
1215                                 pfa->faTypeFace = HEAP_strdupA( SystemHeap, 0,
1216                                                          lpAlias );
1217                             prev = pfa;
1218                         }
1219                                                 
1220                         TRACE(font, "\tsubstituted '%s' with %s\n",
1221                                                 frMatch->lfFaceName, lpAlias );
1222
1223                         lstrcpyn32A( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1224                         frMatch->fr_flags |= FR_NAMESET;
1225                     }
1226                     else
1227                     {
1228                         /* create new entry in the alias table */
1229                         XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1230                     }
1231                 }
1232             }
1233             else ERR(font, " malformed font alias '%s'\n", *buffer );
1234         }
1235         else break;
1236     } while(TRUE);
1237 }
1238
1239 /***********************************************************************
1240  *           XFONT_UserMetricsCache
1241  * 
1242  * Returns expanded name for the ~/.wine/.cachedmetrics file.
1243  */
1244 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1245 {
1246     struct passwd* pwd;
1247
1248     pwd = getpwuid(getuid());
1249     if( pwd && pwd->pw_dir )
1250     {
1251         int i = strlen( pwd->pw_dir ) + strlen( INIWinePrefix ) + 
1252                                             strlen( INIFontMetrics ) + 2;
1253         if( i > *buf_size ) 
1254             buffer = (char*) HeapReAlloc( SystemHeap, 0, buffer, *buf_size = i );
1255         strcpy( buffer, pwd->pw_dir );
1256         strcat( buffer, INIWinePrefix );
1257         strcat( buffer, INIFontMetrics );
1258     } else buffer[0] = '\0';
1259     return buffer;
1260 }
1261
1262
1263 /***********************************************************************
1264  *           XFONT_ReadCachedMetrics
1265  */
1266 static BOOL32 XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
1267 {
1268     if( fd >= 0 )
1269     {
1270         unsigned u;
1271         int i, j;
1272
1273         /* read checksums */
1274         read( fd, &u, sizeof(unsigned) );
1275         read( fd, &i, sizeof(int) );
1276
1277         if( u == x_checksum && i == x_count )
1278         {
1279             off_t length, offset = 3 * sizeof(int);
1280
1281             /* read total size */
1282             read( fd, &i, sizeof(int) );
1283             length = lseek( fd, 0, SEEK_END );
1284
1285             if( length == (i + offset) )
1286             {
1287                 lseek( fd, offset, SEEK_SET );
1288                 fontList = (fontResource*)HeapAlloc( SystemHeap, 0, i);
1289                 if( fontList )
1290                 {
1291                     fontResource*       pfr = fontList;
1292                     fontInfo*           pfi = NULL;
1293
1294                     TRACE(font,"Reading cached font metrics:\n");
1295
1296                     read( fd, fontList, i); /* read all metrics at once */
1297                     while( offset < length )
1298                     {
1299                         offset += sizeof(fontResource) + sizeof(fontInfo);
1300                         pfr->fi = pfi = (fontInfo*)(pfr + 1);
1301                         j = 1;
1302                         while( TRUE )
1303                         {
1304                            if( offset > length ||
1305                               (int)(pfi->next) != j++ ) goto fail;
1306
1307                            pfi->df.dfFace = pfr->lfFaceName;
1308                            pfi->df.dfHorizRes = pfi->df.dfVertRes = res;
1309                            pfi->df.dfPoints = (INT16)(((INT32)(pfi->df.dfPixHeight -
1310                                 pfi->df.dfInternalLeading) * 72 + (res >> 1)) / res );
1311                            pfi->next = pfi + 1;
1312
1313                            if( j > pfr->count ) break;
1314
1315                            pfi = pfi->next;
1316                            offset += sizeof(fontInfo);
1317                         }
1318                         pfi->next = NULL;
1319                         if( pfr->next )
1320                         {
1321                             pfr->next = (fontResource*)(pfi + 1);
1322                             pfr = pfr->next;
1323                         } 
1324                         else break;
1325                     }
1326                     if( pfr->next == NULL &&
1327                         *(int*)(pfi + 1) == X_FMC_MAGIC )
1328                     {
1329                         /* read LFD stubs */
1330                         char* lpch = (char*)((int*)(pfi + 1) + 1);
1331                         offset += sizeof(int);
1332                         for( pfr = fontList; pfr; pfr = pfr->next )
1333                         {
1334                             TRACE(font,"\t%s, %i instances\n", lpch, pfr->count );
1335                             pfr->resource = lpch;
1336                             while( TRUE )
1337                             { 
1338                                 if( ++offset > length ) goto fail;
1339                                 if( !*lpch++ ) break;
1340                             }
1341                         }
1342                         close( fd );
1343                         return TRUE;
1344                     }
1345                 }
1346             }
1347         }
1348 fail:
1349         if( fontList ) HeapFree( SystemHeap, 0, fontList );
1350         fontList = NULL;
1351         close( fd );
1352     }
1353     return FALSE;
1354 }
1355
1356 /***********************************************************************
1357  *           XFONT_WriteCachedMetrics
1358  */
1359 static BOOL32 XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
1360 {
1361     fontResource* pfr;
1362     fontInfo* pfi;
1363
1364     if( fd >= 0 )
1365     {
1366         int  i, j, k;
1367
1368         /* font metrics file:
1369          *
1370          * +0000 x_checksum
1371          * +0004 x_count
1372          * +0008 total size to load
1373          * +000C prepackaged font metrics
1374          * ...
1375          * +...x        X_FMC_MAGIC
1376          * +...x + 4    LFD stubs
1377          */
1378
1379         write( fd, &x_checksum, sizeof(unsigned) );
1380         write( fd, &x_count, sizeof(int) );
1381
1382         for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next ) 
1383         {
1384             i += strlen( pfr->resource ) + 1;
1385             j += pfr->count;
1386         }
1387         i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
1388         write( fd, &i, sizeof(int) );
1389
1390         TRACE(font,"Writing font cache:\n");
1391
1392         for( pfr = fontList; pfr; pfr = pfr->next )
1393         {
1394             fontInfo fi;
1395
1396             TRACE(font,"\t%s, %i instances\n", pfr->resource, pfr->count );
1397
1398             i = write( fd, pfr, sizeof(fontResource) );
1399             if( i == sizeof(fontResource) ) 
1400             {
1401                 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
1402                 {
1403                     memcpy( &fi, pfi, sizeof(fi) );
1404
1405                     fi.df.dfFace = NULL;
1406                     fi.next = (fontInfo*)k;     /* loader checks this */
1407
1408                     j = write( fd, &fi, sizeof(fi) );
1409                     k++;
1410                 }
1411                 if( j == sizeof(fontInfo) ) continue;
1412             }
1413             break;
1414         }
1415         if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
1416         {
1417             i = j = X_FMC_MAGIC;
1418             write( fd, &i, sizeof(int) );
1419             for( pfr = fontList; pfr && i == j; pfr = pfr->next )
1420             {
1421                 i = strlen( pfr->resource ) + 1;
1422                 j = write( fd, pfr->resource, i );
1423             }
1424         }
1425         close( fd );
1426         return ( i == j );
1427     }
1428     return TRUE;
1429 }
1430
1431 /***********************************************************************
1432  *           XFONT_CheckIniSection
1433  *
1434  *   Examines wine.conf for old/invalid font entries and recommend changes to
1435  *   the user.
1436  *
1437  *   Revision history
1438  *        05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
1439  *             Original implementation.
1440  */
1441 static void XFONT_CheckIniCallback(char const *, char const *, void *);
1442
1443 static char const  *fontmsgprologue =
1444 "Wine warning:\n"
1445 "   The following entries in the [fonts] section of the wine.conf file are\n"
1446 "   obsolete or invalid:\n";
1447
1448 static char const  *fontmsgepilogue =
1449 "   These entries should be eliminated or updated.\n"
1450 "   See the documentation/fonts file for more information.\n";
1451
1452 static int XFONT_CheckIniSection()
1453 {
1454     int  found = 0;
1455
1456     PROFILE_EnumerateWineIniSection("Fonts", &XFONT_CheckIniCallback,
1457                     (void *)&found);
1458     if(found)
1459     MSG(fontmsgepilogue);
1460
1461     return 1;
1462 }
1463
1464 static void  XFONT_CheckIniCallback(
1465     char const  *key,
1466     char const  *value,
1467     void  *found)
1468 {
1469     /* Ignore any keys that start with potential comment characters "'", '#',
1470        or ';'. */
1471     if(key[0] == '\'' || key[0] == '#' || key[0] == ';' || key[0] == '\0')
1472     return;
1473
1474     /* Make sure this is a valid key */
1475     if((strncasecmp(key, INISubSection, 5) == 0) ||
1476        (strcasecmp( key, INIDefault) == 0) ||
1477        (strcasecmp( key, INIDefaultFixed) == 0) ||
1478        (strcasecmp( key, INIGlobalMetrics) == 0) ||
1479        (strcasecmp( key, INIResolution) == 0) ) 
1480     {
1481         /* Valid key; make sure the value doesn't contain a wildcard */
1482         if(strchr(value, '*')) {
1483             if(*(int *)found == 0) {
1484                 MSG(fontmsgprologue);
1485                 ++*(int *)found;
1486             }
1487
1488             MSG("     %s=%s [no wildcards allowed]\n", key, value);
1489         }
1490     }
1491     else {
1492         /* Not a valid key */
1493         if(*(int *)found == 0) {
1494             MSG(fontmsgprologue);
1495             ++*(int *)found;
1496     }
1497
1498         MSG("     %s=%s [obsolete]\n", key, value);
1499     }
1500
1501     return;
1502 }
1503
1504 /***********************************************************************
1505  *           XFONT_GetPointResolution()
1506  *
1507  * Here we initialize DefResolution which is used in the
1508  * XFONT_Match() penalty function. We also load the point
1509  * resolution value (higher values result in larger fonts).
1510  */
1511 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
1512 {
1513     int i, j, point_resolution, num = 3; 
1514     int allowed_xfont_resolutions[3] = { 72, 75, 100 };
1515     int best = 0, best_diff = 65536;
1516
1517     DefResolution = point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
1518     if( !DefResolution ) DefResolution = point_resolution = pDevCaps->logPixelsY;
1519     else pDevCaps->logPixelsX = pDevCaps->logPixelsY = DefResolution;
1520
1521     for( i = best = 0; i < num; i++ )
1522     {
1523         j = abs( DefResolution - allowed_xfont_resolutions[i] );
1524         if( j < best_diff )
1525         {
1526             best = i;
1527                 best_diff = j;
1528         }
1529     }
1530     DefResolution = allowed_xfont_resolutions[best];
1531     return point_resolution;
1532 }
1533
1534 /***********************************************************************
1535  *           X11DRV_FONT_Init
1536  *
1537  * Initialize font resource list and allocate font cache.
1538  */
1539 BOOL32 X11DRV_FONT_Init( DeviceCaps* pDevCaps )
1540 {
1541   XFontStruct*  x_fs;
1542   fontResource* fr, *pfr;
1543   fontInfo*     fi, *pfi;
1544   unsigned      x_checksum;
1545   int           i, j, res, x_count, fd = -1, buf_size = 0;
1546   char*         lpstr, *lpch, *lpmetrics, *buffer;
1547   char**        x_pattern;
1548
1549   XFONT_CheckIniSection();
1550
1551   res = XFONT_GetPointResolution( pDevCaps );
1552       
1553   x_pattern = TSXListFonts(display, "*", MAX_FONT_FAMILIES * 16, &x_count );
1554
1555   TRACE(font,"Font Mapper: initializing %i fonts [LPY=%i, XDR=%i, DR=%i]\n", 
1556                                     x_count, pDevCaps->logPixelsY, DefResolution, res);
1557   for( i = x_checksum = 0; i < x_count; i++ )
1558   {
1559 #if 0
1560      printf("%i\t: %s\n", i, x_pattern[i] );
1561 #endif
1562
1563      j = strlen( x_pattern[i] );
1564      if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
1565   }
1566   x_checksum |= X_PFONT_MAGIC;
1567
1568   buf_size = 128;
1569   buffer = HeapAlloc( SystemHeap, 0, buf_size );
1570   lpmetrics = NULL;
1571
1572   /* deal with systemwide font metrics cache */
1573
1574   if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, 128 ) )
1575       fd = open( buffer, O_RDONLY );
1576
1577   if( XFONT_ReadCachedMetrics(fd, res, x_checksum, x_count) == FALSE )
1578   {
1579       /* try per-user */
1580       buffer = XFONT_UserMetricsCache( buffer, &buf_size );
1581       if( buffer[0] )
1582       {
1583           fd = open( buffer, O_RDONLY );
1584           if( XFONT_ReadCachedMetrics(fd, res, x_checksum, x_count) == FALSE )
1585               lpmetrics = HEAP_strdupA( SystemHeap, 0, buffer ); /* update later on */
1586       }
1587   }
1588
1589   fi = NULL;
1590   if( fontList == NULL )        /* build metrics from scratch */
1591   {
1592      int n_ff;
1593      char* typeface;
1594
1595      for( i = n_ff = 0; i < x_count; i++ )
1596      {
1597         typeface = lpch = x_pattern[i];
1598
1599         lpch = LFD_Advance(typeface, 3); /* extra '-' in the beginning */
1600         if( !*lpch ) continue;
1601
1602         lpstr = lpch; 
1603         j = lpch - typeface;    /* resource name length */
1604
1605         /* find a family to insert into */
1606
1607         for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1608         {
1609            if( !strncasecmp(fr->resource, typeface, j) && 
1610                strlen(fr->resource) == j ) break;
1611            pfr = fr;
1612         }  
1613
1614         if( !fi ) fi = (fontInfo*) HeapAlloc(SystemHeap, 0, sizeof(fontInfo));
1615
1616         if( !fr ) /* add new family */
1617         {
1618            if( n_ff >= MAX_FONT_FAMILIES ) break;
1619            if( !LFD_InitFontInfo( fi, lpstr) ) continue;
1620
1621            n_ff++;
1622            fr = (fontResource*) HeapAlloc(SystemHeap, 0, sizeof(fontResource)); 
1623            memset(fr, 0, sizeof(fontResource));
1624            fr->resource = (char*) HeapAlloc(SystemHeap, 0, j + 1 );
1625            lstrcpyn32A( fr->resource, typeface, j + 1 );
1626
1627            TRACE(font,"    family: %s\n", fr->resource );
1628
1629            if( pfr ) pfr->next = fr;
1630            else fontList = fr;
1631         }
1632         else if( !LFD_InitFontInfo( fi, lpstr) ) continue;
1633
1634         /* check if we already have something better than "fi" */
1635
1636         for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next ) 
1637            if( (j = XFONT_IsSubset( pfi, fi )) < 0 ) 
1638                pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1639         if( j > 0 ) continue;
1640
1641         /* add new font instance "fi" to the "fr" font resource */
1642
1643         if( fi->fi_flags & FI_SCALABLE )
1644         {
1645             /* set scalable font height to 24 to get an origin for extrapolation */
1646
1647            j = strlen(typeface);  j += 0x10;
1648            if( j > buf_size ) 
1649                buffer = (char*)HeapReAlloc( SystemHeap, 0, buffer, buf_size = j );
1650
1651            lpch = LFD_Advance(typeface, 7);
1652            memcpy( buffer, typeface, (j = lpch - typeface) );
1653            lpch = LFD_Advance(lpch, 4);
1654            sprintf( buffer + j, "%d-%d-%d-*-%c-*-", fi->lfd_height,
1655                             fi->lfd_decipoints, fi->lfd_resolution,
1656                                         (*lpch == '-')?'*':*lpch );
1657            lpch = LFD_Advance(lpch, 2);
1658            strcat( lpstr = buffer, lpch);
1659         }
1660         else lpstr = typeface;
1661
1662         if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1663         {
1664            fi->df.dfHorizRes = fi->df.dfVertRes = res;
1665
1666            XFONT_SetFontMetric( fi, fr, x_fs );
1667            TSXFreeFont( display, x_fs );
1668
1669            TRACE(font,"\t[% 2ipt] '%s'\n", fi->df.dfPoints, typeface );
1670
1671            XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1672            fi = NULL;   /* preventing reuse */
1673         }
1674         else
1675         {
1676            ERR(font, "failed to load %s\n", lpstr );
1677
1678            XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1679         }
1680      }
1681
1682      if( lpmetrics )     /* update cached metrics */
1683      {
1684          fd = open( lpmetrics, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
1685          if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
1686              if( fd ) remove( lpmetrics );      /* couldn't write entire file */
1687          HeapFree( SystemHeap, 0, lpmetrics );
1688      }
1689   }
1690
1691   if( fi ) HeapFree(SystemHeap, 0, fi);
1692   TSXFreeFontNames(x_pattern);
1693
1694   /* check if we're dealing with X11 R6 server */
1695
1696   strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
1697   if( (x_fs = TSXLoadQueryFont(display, buffer)) )
1698   {
1699        XTextCaps |= TC_SF_X_YINDEP;
1700        TSXFreeFont(display, x_fs);
1701   }
1702
1703   XFONT_WindowsNames( buffer );
1704   XFONT_LoadAliases( &buffer, buf_size );
1705   HeapFree(SystemHeap, 0, buffer);
1706
1707
1708   /* fontList initialization is over, allocate X font cache */
1709
1710   fontCache = (fontObject*) HeapAlloc(SystemHeap, 0, fontCacheSize * sizeof(fontObject));
1711   XFONT_GrowFreeList(0, fontCacheSize - 1);
1712
1713   TRACE(font,"done!\n");
1714
1715   /* update text caps parameter */
1716
1717   pDevCaps->textCaps = XTextCaps;
1718
1719   RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
1720   RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
1721   
1722   return TRUE;
1723 }
1724
1725
1726 /***********************************************************************
1727  *           X Font Matching
1728  *
1729  * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1730  */
1731 static INT32 XFONT_IsSubset(fontInfo* match, fontInfo* fi)
1732 {
1733   INT32           m;
1734
1735   /* 0 - keep both, 1 - keep match, -1 - keep fi */
1736
1737   m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1738   if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1739
1740   if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1741        && fi->lfd_height != match->lfd_height) ||
1742       (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1743        && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1744
1745   m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1746       (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1747
1748   if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1749       m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0;    /* keep both */
1750   else if( m >= 0 ) return 1;   /* 'match' is better */
1751
1752   return -1;                    /* 'fi' is better */
1753 }
1754
1755 /***********************************************************************
1756  *            XFONT_Match
1757  *
1758  * Compute the matching score between the logical font and the device font.
1759  *
1760  * contributions from highest to lowest:
1761  *      charset
1762  *      fixed pitch
1763  *      height
1764  *      family flags (only when the facename is not present)
1765  *      width
1766  *      weight, italics, underlines, strikeouts
1767  *
1768  * NOTE: you can experiment with different penalty weights to see what happens.
1769  * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
1770  */
1771 static UINT32 XFONT_Match( fontMatch* pfm )
1772 {
1773    fontInfo*    pfi = pfm->pfi;         /* device font to match */
1774    LPLOGFONT16  plf = pfm->plf;         /* wanted logical font */
1775    UINT32       penalty = 0;
1776    BOOL32       bR6 = pfm->flags & FO_MATCH_XYINDEP;    /* from TextCaps */
1777    BOOL32       bScale = pfi->fi_flags & FI_SCALABLE;
1778    INT32        d, h;
1779
1780    TRACE(font,"\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
1781                  pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
1782                 (pfi->df.dfWeight > 400) ? "Bold " : "Normal ",
1783                 (pfi->df.dfItalic) ? "Italic" : "" );
1784
1785    pfm->flags = 0;
1786
1787    if( plf->lfCharSet == DEFAULT_CHARSET )
1788    {
1789        if( (pfi->df.dfCharSet!= ANSI_CHARSET) && (pfi->df.dfCharSet!=DEFAULT_CHARSET) ) 
1790             penalty += 0x200;
1791    }
1792    else if (plf->lfCharSet != pfi->df.dfCharSet) penalty += 0x200;
1793
1794    /* FIXME: Hack to demote symbols and nil fonts.  Should take into
1795              account if a program ever actually asked for this type of
1796              font */
1797    if ( (strcmp(pfm->pfr->lfFaceName,"Symbol")==0) || (strcmp(pfm->pfr->lfFaceName,"Nil")==0) ) 
1798      penalty += 0x200; /* very stiff penality */
1799
1800    /* TMPF_FIXED_PITCH means exactly the opposite */
1801
1802    if( plf->lfPitchAndFamily & FIXED_PITCH ) 
1803    {
1804       if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
1805    }
1806    else if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
1807
1808    if( plf->lfHeight > 0 )
1809        d = (h = pfi->df.dfPixHeight) - plf->lfHeight;
1810    else if( plf->lfHeight < -1 )
1811        d = (h = pfi->df.dfPoints) + plf->lfHeight;
1812    else d = h = 0;      
1813
1814    if( d && plf->lfHeight )
1815    {
1816        UINT16   height = ( plf->lfHeight > 0 ) ? plf->lfHeight
1817                          : ((-plf->lfHeight * pfi->df.dfPixHeight) / h);
1818        if( bScale ) pfm->height = height;
1819        else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
1820        {
1821            if( d > 0 )  /* do not shrink raster fonts */
1822            {
1823                pfm->height = pfi->df.dfPixHeight;
1824                penalty += (pfi->df.dfPixHeight - height) * 0x4;
1825            }
1826            else         /* expand only in integer multiples */
1827            {
1828                pfm->height = height - height%pfi->df.dfPixHeight;
1829                penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
1830            }
1831        }
1832        else /* can't be scaled at all */
1833        {
1834            if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
1835            pfm->height = pfi->df.dfPixHeight;
1836            penalty += (d > 0)? d * 0x8 : -d * 0x10;
1837        }
1838    } else pfm->height = pfi->df.dfPixHeight;
1839
1840    if((pfm->flags & FO_MATCH_PAF) &&
1841       (plf->lfPitchAndFamily & FF_FAMILY) != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
1842        penalty += 0x10;
1843
1844    if( plf->lfWidth )
1845    {
1846        if( bR6 && bScale ) h = 0; 
1847        else
1848        {
1849            /* FIXME: not complete */
1850
1851            pfm->flags |= FO_SYNTH_WIDTH;
1852            h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
1853        }
1854        penalty += h * ( d ) ? 0x2 : 0x1 ;
1855    }
1856    else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
1857
1858    if( plf->lfWeight != FW_DONTCARE )
1859    {
1860        penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
1861        if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
1862    } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++;  /* choose normal by default */
1863
1864    if( plf->lfItalic != pfi->df.dfItalic )
1865    {
1866        penalty += 0x4;
1867        pfm->flags |= FO_SYNTH_ITALIC;
1868    }
1869
1870    if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
1871    if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
1872
1873    if( penalty && pfi->lfd_resolution != DefResolution ) 
1874        penalty++;
1875
1876    TRACE(font,"  returning %i\n", penalty );
1877
1878    return penalty;
1879 }
1880
1881 /***********************************************************************
1882  *            XFONT_MatchFIList
1883  *
1884  * Scan a particular font resource for the best match.
1885  */
1886 static UINT32 XFONT_MatchFIList( fontMatch* pfm )
1887 {
1888   BOOL32        skipRaster = (pfm->flags & FO_MATCH_NORASTER);
1889   UINT32        current_score, score = (UINT32)(-1);
1890   UINT16        origflags = pfm->flags; /* Preserve FO_MATCH_XYINDEP */
1891   fontMatch     fm = *pfm;
1892
1893   for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next,
1894       fm.flags = origflags )
1895   {
1896      if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
1897          continue;
1898
1899      current_score = XFONT_Match( &fm );
1900      if( score > current_score )
1901      {
1902         memcpy( pfm, &fm, sizeof(fontMatch) );
1903         score = current_score;
1904      }
1905   }
1906   return score;
1907 }
1908
1909 /***********************************************************************
1910  *            XFONT_CheckFIList
1911  *
1912  * REMOVE_SUBSETS - attach new fi and purge subsets
1913  * UNMARK_SUBSETS - remove subset flags from all fi entries
1914  */
1915 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1916 {
1917   int           i = 0;
1918   fontInfo*     pfi, *prev;
1919
1920   for( prev = NULL, pfi = fr->fi; pfi; )
1921   {
1922     if( action == REMOVE_SUBSETS )
1923     {
1924         if( pfi->fi_flags & FI_SUBSET )
1925         {
1926             fontInfo* subset = pfi;
1927
1928             i++;
1929             fr->count--;
1930             if( prev ) prev->next = pfi = pfi->next;
1931             else fr->fi = pfi = pfi->next;
1932             HeapFree( SystemHeap, 0, subset );
1933             continue;
1934         }
1935     }
1936     else pfi->fi_flags &= ~FI_SUBSET;
1937
1938     prev = pfi; 
1939     pfi = pfi->next;
1940   }
1941
1942   if( action == REMOVE_SUBSETS )        /* also add the superset */
1943   {
1944     if( fi->fi_flags & FI_SCALABLE )
1945     {
1946         fi->next = fr->fi;
1947         fr->fi = fi;
1948     }
1949     else if( prev ) prev->next = fi; else fr->fi = fi;
1950     fr->count++;
1951   }
1952
1953   if( i ) TRACE(font,"\t    purged %i subsets [%i]\n", i , fr->count);
1954 }
1955
1956 /***********************************************************************
1957  *            XFONT_FindFIList
1958  */
1959 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1960 {
1961   while( pfr )
1962   {
1963     if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1964     pfr = pfr->next;
1965   }
1966   return pfr;
1967 }
1968
1969 /***********************************************************************
1970  *          XFONT_MatchDeviceFont
1971  *
1972  * Scan font resource tree.
1973  */
1974 static BOOL32 XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm )
1975 {
1976     fontMatch           fm = *pfm;
1977
1978     pfm->pfi = NULL;
1979     if( fm.plf->lfFaceName[0] ) 
1980     {
1981         fontAlias* fa;
1982         LPSTR str = NULL;
1983
1984         for( fa = aliasTable; fa; fa = fa->next )
1985              if( !strcmp( fa->faAlias, fm.plf->lfFaceName ) ) 
1986              {
1987                 str = fa->faTypeFace;
1988                 break;
1989              }
1990         fm.pfr = XFONT_FindFIList( start, str ? str : fm.plf->lfFaceName );
1991     }
1992
1993     if( fm.pfr )        /* match family */
1994     {
1995         TRACE(font, "%s\n", fm.pfr->lfFaceName );
1996
1997         XFONT_MatchFIList( &fm );
1998         *pfm = fm;
1999     }
2000
2001     if( !pfm->pfi )      /* match all available fonts */
2002     {
2003         UINT32          current_score, score = (UINT32)(-1);
2004
2005         fm.flags |= FO_MATCH_PAF;
2006         for( start = fontList; start && score; start = start->next )
2007         {
2008             fm.pfr = start;
2009
2010             TRACE(font, "%s\n", fm.pfr->lfFaceName );
2011
2012             current_score = XFONT_MatchFIList( &fm );
2013             if( current_score < score )
2014             {
2015                 score = current_score;
2016                 *pfm = fm;
2017             }
2018         }
2019     }
2020     return TRUE;
2021 }
2022
2023
2024 /***********************************************************************
2025  *           X Font Cache
2026  */
2027 static void XFONT_GrowFreeList(int start, int end)
2028 {
2029    /* add all entries from 'start' up to and including 'end' */
2030
2031    memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2032
2033    fontCache[end].lru = fontLF;
2034    fontCache[end].count = -1;
2035    fontLF = start;
2036    while( start < end )
2037    {
2038         fontCache[start].count = -1;
2039         fontCache[start].lru = start + 1;
2040         start++;
2041    } 
2042 }
2043
2044 static fontObject* XFONT_LookupCachedFont( LPLOGFONT16 plf, UINT16* checksum )
2045 {
2046     UINT16      cs = __lfCheckSum( plf );
2047     int         i = fontMRU, prev = -1;
2048    
2049    *checksum = cs;
2050     while( i >= 0 )
2051     {
2052         if( fontCache[i].lfchecksum == cs &&
2053           !(fontCache[i].fo_flags & FO_REMOVED) )
2054         {
2055             /* FIXME: something more intelligent here */
2056
2057             if( !memcmp( plf, &fontCache[i].lf,
2058                          sizeof(LOGFONT16) - LF_FACESIZE ) &&
2059                 !strncasecmp( plf->lfFaceName, fontCache[i].lf.lfFaceName, 
2060                                                             LF_FACESIZE ) )
2061             {
2062                 /* remove temporarily from the lru list */
2063
2064                 if( prev >= 0 )
2065                     fontCache[prev].lru = fontCache[i].lru;
2066                 else 
2067                     fontMRU = (INT16)fontCache[i].lru;
2068                 return (fontCache + i);
2069             }
2070         }
2071         prev = i;
2072         i = (INT16)fontCache[i].lru;
2073     }
2074     return NULL;
2075 }
2076
2077 static fontObject* XFONT_GetCacheEntry()
2078 {
2079     int         i;
2080
2081     if( fontLF == -1 )
2082     {
2083         int     prev_i, prev_j, j;
2084
2085         TRACE(font,"font cache is full\n");
2086
2087         /* lookup the least recently used font */
2088
2089         for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2090         {
2091             if( fontCache[i].count <= 0 &&
2092               !(fontCache[i].fo_flags & FO_SYSTEM) )
2093             {
2094                 prev_j = prev_i;
2095                 j = i;
2096             }
2097             prev_i = i;
2098         }
2099
2100         if( j >= 0 )    /* unload font */
2101         {
2102             /* detach from the lru list */
2103
2104             TRACE(font,"\tfreeing entry %i\n", j );
2105
2106             if( prev_j >= 0 )
2107                 fontCache[prev_j].lru = fontCache[j].lru;
2108             else fontMRU = (INT16)fontCache[j].lru;
2109
2110             /* FIXME: lpXForm, lpPixmap */
2111             if(fontCache[j].lpX11Trans)
2112                 HeapFree( SystemHeap, 0, fontCache[j].lpX11Trans );
2113
2114             TSXFreeFont( display, fontCache[j].fs );
2115
2116             memset( fontCache + j, 0, sizeof(fontObject) );
2117             return (fontCache + j);
2118         }
2119         else            /* expand cache */
2120         {
2121             fontObject* newCache;
2122
2123             prev_i = fontCacheSize + FONTCACHE;
2124
2125             TRACE(font,"\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2126
2127             if( (newCache = (fontObject*)HeapReAlloc(SystemHeap, 0,  
2128                                                      fontCache, prev_i)) )
2129             {
2130                 i = fontCacheSize;
2131                 fontCacheSize  = prev_i;
2132                 fontCache = newCache;
2133                 XFONT_GrowFreeList( i, fontCacheSize - 1);
2134             } 
2135             else return NULL;
2136         }
2137     }
2138
2139     /* detach from the free list */
2140
2141     i = fontLF;
2142     fontLF = (INT16)fontCache[i].lru;
2143     fontCache[i].count = 0;
2144     return (fontCache + i);
2145 }
2146
2147 static int XFONT_ReleaseCacheEntry(fontObject* pfo)
2148 {
2149     UINT32      u = (UINT32)(pfo - fontCache);
2150
2151     if( u < fontCacheSize ) return (--fontCache[u].count);
2152     return -1;
2153 }
2154
2155 /**********************************************************************
2156  *      XFONT_SetX11Trans
2157  */
2158 static BOOL32 XFONT_SetX11Trans( fontObject *pfo )
2159 {
2160   char *fontName;
2161   Atom nameAtom;
2162   int i;
2163   char *cp, *start;
2164
2165   TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2166   fontName = TSXGetAtomName( display, nameAtom );
2167   for(i = 0, cp = fontName; i < 7; i++) {
2168     cp = strchr(cp, '-');
2169     cp++;
2170   }
2171   if(*cp != '[') {
2172     TSXFree(fontName);
2173     return FALSE;
2174   }
2175   start = cp;
2176   while((cp = strchr(cp, '~')))
2177     *cp = '-';
2178
2179 #define PX pfo->lpX11Trans
2180
2181   sscanf(start, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2182   TSXFree(fontName);
2183
2184   TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2185   TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2186
2187   PX->pixelsize = hypot(PX->a, PX->b);
2188   PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2189   PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2190
2191   TRACE(font, "[%f %f %f %f] RA = %ld RD = %ld\n", pfo->lpX11Trans->a, 
2192         pfo->lpX11Trans->b, pfo->lpX11Trans->c, pfo->lpX11Trans->d,
2193         pfo->lpX11Trans->RAW_ASCENT, pfo->lpX11Trans->RAW_DESCENT);
2194
2195 #undef PX
2196   return TRUE;
2197 }
2198
2199 /***********************************************************************
2200  *           X Device Font Objects
2201  */
2202 static X_PHYSFONT XFONT_RealizeFont( LPLOGFONT16 plf )
2203 {
2204     UINT16      checksum;
2205     fontObject* pfo = XFONT_LookupCachedFont( plf, &checksum );
2206
2207     if( !pfo )
2208     {
2209         fontMatch       fm = { NULL, NULL, 0, 0, plf};
2210         INT32           i, index;
2211
2212         if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2213
2214         /* allocate new font cache entry */
2215
2216         if( (pfo = XFONT_GetCacheEntry()) )
2217         {
2218             LPSTR       lpLFD = HeapAlloc( GetProcessHeap(), 0, MAX_LFD_LENGTH );
2219             
2220             if( lpLFD ) /* initialize entry and load font */
2221             {
2222                 UINT32  uRelaxLevel = 0;
2223
2224                 TRACE(font,"(%u) '%s' h=%i weight=%i %s\n",
2225                              plf->lfCharSet, plf->lfFaceName, plf->lfHeight, 
2226                              plf->lfWeight, (plf->lfItalic) ? "Italic" : "" );
2227
2228                 XFONT_MatchDeviceFont( fontList, &fm );
2229
2230                 pfo->fr = fm.pfr;
2231                 pfo->fi = fm.pfi;
2232                 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2233
2234                 memcpy( &pfo->lf, plf, sizeof(LOGFONT16) );
2235                 pfo->lfchecksum = checksum;
2236
2237                 do
2238                 {
2239                     LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2240                     if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2241                 } while( uRelaxLevel );
2242
2243                 
2244                 if(pfo->lf.lfEscapement != 0) {
2245                     pfo->lpX11Trans = HeapAlloc(SystemHeap, 0,
2246                                                 sizeof(XFONTTRANS));
2247                     if(!XFONT_SetX11Trans( pfo )) {
2248                         HeapFree(SystemHeap, 0, pfo->lpX11Trans);
2249                         pfo->lpX11Trans = NULL;
2250                     }
2251                 }
2252
2253                 if( XFONT_GetLeading( &pfo->fi->df, pfo->fs, &i, NULL, 
2254                                       pfo->lpX11Trans ) )
2255
2256                     if(!pfo->lpX11Trans)
2257                         pfo->foAvgCharWidth =
2258               (INT16)pfo->fs->per_char['X' - pfo->fs->min_char_or_byte2].width;
2259                     else
2260                         pfo->foAvgCharWidth = 
2261          (INT16)pfo->fs->per_char['X' - pfo->fs->min_char_or_byte2].attributes
2262                           * pfo->lpX11Trans->pixelsize / 1000.0;
2263                 else
2264                     pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(
2265                                      &pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2266                 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo);
2267                 pfo->foInternalLeading = (INT16)i;
2268
2269                 /* FIXME: If we've got a soft font or
2270                  * there are FO_SYNTH_... flags for the
2271                  * non PROOF_QUALITY request, the engine
2272                  * should rasterize characters into mono
2273                  * pixmaps and store them in the pfo->lpPixmap
2274                  * array (pfo->fs should be updated as well).
2275                  * X11DRV_ExtTextOut() must be heavily modified 
2276                  * to support pixmap blitting and FO_SYNTH_...
2277                  * styles.
2278                  */
2279
2280                 pfo->lpXForm = NULL;
2281                 pfo->lpPixmap = NULL;
2282
2283                 HeapFree( GetProcessHeap(), 0, lpLFD );
2284             } 
2285             else /* attach back to the free list */
2286             {
2287                 pfo->count = -1;
2288                 pfo->lru = fontLF;
2289                 fontLF = (pfo - fontCache);
2290                 pfo = NULL;
2291             }
2292         }
2293
2294         if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2295         {
2296             UINT32              current_score, score = (UINT32)(-1);
2297
2298             i = index = fontMRU; 
2299             fm.flags |= FO_MATCH_PAF;
2300             do
2301             {
2302                 pfo = fontCache + i;
2303                 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2304
2305                 current_score = XFONT_Match( &fm );
2306                 if( current_score < score ) index = i;
2307
2308                 i =  pfo->lru;
2309             } while( i >= 0 );
2310             pfo = fontCache + index;
2311             pfo->count++;
2312             return (X_PHYSFONT)(X_PFONT_MAGIC | index);
2313         }
2314     }
2315  
2316     /* attach at the head of the lru list */
2317
2318     pfo->count++;
2319     pfo->lru = fontMRU;
2320     fontMRU = (pfo - fontCache);
2321
2322     TRACE(font,"physfont %i\n", fontMRU);
2323
2324     return (X_PHYSFONT)(X_PFONT_MAGIC | fontMRU);
2325 }
2326
2327 /***********************************************************************
2328  *           XFONT_GetFontObject
2329  */
2330 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
2331 {
2332     if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
2333     return NULL;
2334 }
2335
2336 /***********************************************************************
2337  *           XFONT_GetFontStruct
2338  */
2339 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
2340 {
2341     if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
2342     return NULL;
2343 }
2344
2345 /***********************************************************************
2346  *           XFONT_GetFontInfo
2347  */
2348 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
2349 {
2350     if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
2351     return NULL;
2352 }
2353
2354
2355
2356 /* X11DRV Interface ****************************************************
2357  *                                                                     *
2358  * Exposed via the dc->funcs dispatch table.                           *
2359  *                                                                     *
2360  ***********************************************************************/
2361 /***********************************************************************
2362  *           X11DRV_FONT_SelectObject
2363  */
2364 HFONT32 X11DRV_FONT_SelectObject( DC* dc, HFONT32 hfont, FONTOBJ* font )
2365 {
2366     HFONT32 hPrevFont = 0;
2367     LOGFONT16 lf;
2368     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2369
2370     if( CHECK_PFONT(physDev->font) ) 
2371         XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
2372
2373     /* FIXME: do we need to pass anything back from here? */
2374     memcpy(&lf,&font->logfont,sizeof(lf));
2375     lf.lfWidth  = font->logfont.lfWidth * dc->vportExtX/dc->wndExtX;
2376     lf.lfHeight = font->logfont.lfHeight* dc->vportExtY/dc->wndExtY;
2377
2378     physDev->font = XFONT_RealizeFont( &lf );
2379     hPrevFont = dc->w.hFont;
2380     dc->w.hFont = hfont;
2381
2382     return hPrevFont;
2383 }
2384
2385
2386 /***********************************************************************
2387  *
2388  *           X11DRV_EnumDeviceFonts
2389  */
2390 BOOL32  X11DRV_EnumDeviceFonts( DC* dc, LPLOGFONT16 plf, 
2391                                         DEVICEFONTENUMPROC proc, LPARAM lp )
2392 {
2393     ENUMLOGFONTEX16     lf;
2394     NEWTEXTMETRIC16     tm;
2395     fontResource*       pfr = fontList;
2396     BOOL32              b, bRet = 0;
2397
2398     if( plf->lfFaceName[0] )
2399     {
2400         pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
2401         if( pfr )
2402         {
2403             fontInfo*   pfi;
2404             for( pfi = pfr->fi; pfi; pfi = pfi->next )
2405                 if( (b = (*proc)( (LPENUMLOGFONT16)&lf, &tm, 
2406                         XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
2407                      bRet = b;
2408                 else break;
2409         }
2410     }
2411     else
2412         for( ; pfr ; pfr = pfr->next )
2413           if(pfr->fi)
2414           {
2415             if( (b = (*proc)( (LPENUMLOGFONT16)&lf, &tm, 
2416                        XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
2417                  bRet = b;
2418             else break;
2419           }
2420
2421     return bRet;
2422 }
2423
2424
2425 /***********************************************************************
2426  *           X11DRV_GetTextExtentPoint
2427  */
2428 BOOL32 X11DRV_GetTextExtentPoint( DC *dc, LPCSTR str, INT32 count,
2429                                   LPSIZE32 size )
2430 {
2431     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2432     fontObject* pfo = XFONT_GetFontObject( physDev->font );
2433     if( pfo ) {
2434         if( !pfo->lpX11Trans ) {
2435             int dir, ascent, descent;
2436             XCharStruct info;
2437
2438             TSXTextExtents( pfo->fs, str, count, &dir, &ascent, &descent, &info );
2439             size->cx = abs((info.width + dc->w.breakRem + count * 
2440                             dc->w.charExtra) * dc->wndExtX / dc->vportExtX);
2441             size->cy = abs((pfo->fs->ascent + pfo->fs->descent) * 
2442                            dc->wndExtY / dc->vportExtY);
2443         } else {
2444
2445             INT32 i;
2446             float x = 0.0, y = 0.0;
2447             for(i = 0; i < count; i++) {
2448                 x += pfo->fs->per_char ? 
2449            pfo->fs->per_char[str[i] - pfo->fs->min_char_or_byte2].attributes : 
2450            pfo->fs->min_bounds.attributes;
2451             }
2452             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
2453             TRACE(font, "x = %f y = %f\n", x, y);
2454             x *= pfo->lpX11Trans->pixelsize / 1000.0;
2455             y *= pfo->lpX11Trans->pixelsize / 1000.0; 
2456             size->cx = fabsf((x + dc->w.breakRem + count * dc->w.charExtra) *
2457                              dc->wndExtX / dc->vportExtX);
2458             size->cy = fabsf(y * dc->wndExtY / dc->vportExtY);
2459         }
2460         return TRUE;
2461     }
2462     return FALSE;
2463 }
2464
2465
2466 /***********************************************************************
2467  *           X11DRV_GetTextMetrics
2468  */
2469 BOOL32 X11DRV_GetTextMetrics(DC *dc, TEXTMETRIC32A *metrics)
2470 {
2471     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2472
2473     if( CHECK_PFONT(physDev->font) )
2474     {
2475         fontObject* pfo = __PFONT(physDev->font);
2476         XFONT_GetTextMetric( pfo, metrics );
2477
2478         return TRUE;
2479     }
2480     return FALSE;
2481 }
2482
2483
2484 /***********************************************************************
2485  *           X11DRV_GetCharWidth
2486  */
2487 BOOL32 X11DRV_GetCharWidth( DC *dc, UINT32 firstChar, UINT32 lastChar,
2488                             LPINT32 buffer )
2489 {
2490     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2491     fontObject* pfo = XFONT_GetFontObject( physDev->font );
2492
2493     if( pfo )
2494     {
2495         int i;
2496
2497         if (pfo->fs->per_char == NULL)
2498             for (i = firstChar; i <= lastChar; i++)
2499                 if(pfo->lpX11Trans)
2500                     *buffer++ = pfo->fs->min_bounds.attributes *
2501                       pfo->lpX11Trans->pixelsize / 1000.0;
2502                 else
2503                     *buffer++ = pfo->fs->min_bounds.width;
2504         else
2505         {
2506             XCharStruct *cs, *def;
2507             static XCharStruct  __null_char = { 0, 0, 0, 0, 0, 0 };
2508
2509             CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
2510                              def);
2511
2512             for (i = firstChar; i <= lastChar; i++)
2513             {
2514                 if (i >= pfo->fs->min_char_or_byte2 && 
2515                     i <= pfo->fs->max_char_or_byte2)
2516                 {
2517                     cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)]; 
2518                     if (CI_NONEXISTCHAR(cs)) cs = def; 
2519                 } else cs = def;
2520                 if(pfo->lpX11Trans)
2521                     *buffer++ = MAX(cs->attributes, 0) *
2522                       pfo->lpX11Trans->pixelsize / 1000.0;
2523                 else
2524                     *buffer++ = MAX(cs->width, 0 );
2525             }
2526         }
2527
2528         return TRUE;
2529     }
2530     return FALSE;
2531 }
2532
2533 /***********************************************************************
2534  *                                                                     *
2535  *           Font Resource API                                         *
2536  *                                                                     *
2537  ***********************************************************************/
2538 /***********************************************************************
2539  *           AddFontResource16    (GDI.119)
2540  *
2541  *  Can be either .FON, or .FNT, or .TTF, or .FOT font file.
2542  *
2543  *  FIXME: Load header and find the best-matching font in the fontList;
2544  *         fixup dfPoints if all metrics are identical, otherwise create
2545  *         new fontAlias. When soft font support is ready this will
2546  *         simply create a new fontResource ('filename' will go into
2547  *         the pfr->resource field) with FR_SOFTFONT/FR_SOFTRESOURCE 
2548  *         flag set. 
2549  */
2550 INT16 WINAPI AddFontResource16( LPCSTR filename )
2551 {
2552     return AddFontResource32A( filename );
2553 }
2554
2555
2556 /***********************************************************************
2557  *           AddFontResource32A    (GDI32.2)
2558  */
2559 INT32 WINAPI AddFontResource32A( LPCSTR str )
2560 {
2561     FIXME(font, "(%s): stub\n", debugres_a(str));
2562     return 1;
2563 }
2564
2565
2566 /***********************************************************************
2567  *           AddFontResource32W    (GDI32.4)
2568  */
2569 INT32 WINAPI AddFontResource32W( LPCWSTR str )
2570 {
2571     FIXME(font, "(%s): stub\n", debugres_w(str) );
2572     return 1;
2573 }
2574
2575 /***********************************************************************
2576  *           RemoveFontResource16    (GDI.136)
2577  */
2578 BOOL16 WINAPI RemoveFontResource16( SEGPTR str )
2579 {
2580     FIXME(font, "(%s): stub\n", debugres_a(PTR_SEG_TO_LIN(str)));
2581     return TRUE;
2582 }
2583
2584
2585 /***********************************************************************
2586  *           RemoveFontResource32A    (GDI32.284)
2587  */
2588 BOOL32 WINAPI RemoveFontResource32A( LPCSTR str )
2589 {
2590     FIXME(font, "(%s): stub\n", debugres_a(str));
2591     return TRUE;
2592 }
2593
2594
2595 /***********************************************************************
2596  *           RemoveFontResource32W    (GDI32.286)
2597  */
2598 BOOL32 WINAPI RemoveFontResource32W( LPCWSTR str )
2599 {
2600     FIXME(font, "(%s): stub\n", debugres_w(str) );
2601     return TRUE;
2602 }
2603
2604