2 * X11 physical font objects
4 * Copyright 1997 Alex Korobka
6 * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
7 * have to be changed for that), dynamic font loading (FreeType).
12 #ifndef X_DISPLAY_MISSING
13 #include <X11/Xatom.h>
16 #endif /* !defined(X_DISPLAY_MISSING) */
23 #include <sys/types.h>
32 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(font)
38 #ifndef X_DISPLAY_MISSING
40 #define X_PFONT_MAGIC (0xFADE0000)
41 #define X_FMC_MAGIC (0x0000CAFE)
43 #define MAX_FONTS 1024*16
44 #define MAX_LFD_LENGTH 256
48 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
49 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
50 #define MIN_FONT_SIZE 2 /* Min size in pixels */
51 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
53 #define REMOVE_SUBSETS 1
54 #define UNMARK_SUBSETS 0
57 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
59 typedef struct __fontAlias
63 struct __fontAlias* next;
66 static fontAlias *aliasTable = NULL;
68 UINT16 XTextCaps = TC_OP_CHARACTER | TC_OP_STROKE |
69 TC_CP_STROKE | TC_CR_ANY |
70 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
71 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE;
73 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
75 static const char* INIWinePrefix = "/.wine";
76 static const char* INIFontMetrics = "/cachedmetrics.";
77 static const char* INIFontSection = "fonts";
78 static const char* INIAliasSection = "Alias";
79 static const char* INIIgnoreSection = "Ignore";
80 static const char* INIDefault = "Default";
81 static const char* INIDefaultFixed = "DefaultFixed";
82 static const char* INIResolution = "Resolution";
83 static const char* INIGlobalMetrics = "FontMetrics";
84 static const char* INIDefaultSerif = "DefaultSerif";
85 static const char* INIDefaultSansSerif = "DefaultSansSerif";
88 /* FIXME - are there any more Latin charsets ? */
89 #define IS_LATIN_CHARSET(ch) \
90 ((ch)==ANSI_CHARSET ||\
92 (ch)==ISO3_CHARSET ||\
93 (ch)==ISO4_CHARSET ||\
94 (ch)==RUSSIAN_CHARSET ||\
95 (ch)==ARABIC_CHARSET ||\
96 (ch)==GREEK_CHARSET ||\
97 (ch)==HEBREW_CHARSET ||\
98 (ch)==TURKISH_CHARSET ||\
101 /* suffix-charset mapping tables - must be less than 254 entries long */
103 typedef struct __sufch
109 static SuffixCharset sufch_ansi[] = {
110 { "0", ANSI_CHARSET },
111 { NULL, ANSI_CHARSET }};
113 static SuffixCharset sufch_iso646[] = {
114 { "irv", ANSI_CHARSET },
115 { NULL, SYMBOL_CHARSET }};
117 static SuffixCharset sufch_iso8859[] = {
118 { "1", ANSI_CHARSET },
120 { "3", ISO3_CHARSET },
121 { "4", ISO4_CHARSET },
122 { "5", RUSSIAN_CHARSET },
123 { "6", ARABIC_CHARSET },
124 { "7", GREEK_CHARSET },
125 { "8", HEBREW_CHARSET },
126 { "9", TURKISH_CHARSET },
127 { "10", BALTIC_CHARSET },
128 { "11", THAI_CHARSET },
129 { "12", SYMBOL_CHARSET },
130 { "13", SYMBOL_CHARSET },
131 { "14", SYMBOL_CHARSET },
132 { "15", ANSI_CHARSET },
133 { NULL, SYMBOL_CHARSET }};
135 static SuffixCharset sufch_microsoft[] = {
136 { "cp1250", EE_CHARSET },
137 { "cp1251", RUSSIAN_CHARSET },
138 { "cp1252", ANSI_CHARSET },
139 { "cp1253", GREEK_CHARSET },
140 { "cp1254", TURKISH_CHARSET },
141 { "cp1255", HEBREW_CHARSET },
142 { "cp1256", ARABIC_CHARSET },
143 { "cp1257", BALTIC_CHARSET },
144 { "fontspecific", SYMBOL_CHARSET },
145 { "symbol", SYMBOL_CHARSET },
146 { NULL, SYMBOL_CHARSET }};
148 static SuffixCharset sufch_tcvn[] = {
149 { "0", TCVN_CHARSET },
150 { NULL, TCVN_CHARSET }};
152 static SuffixCharset sufch_tis620[] = {
153 { "0", THAI_CHARSET },
154 { NULL, THAI_CHARSET }};
156 static SuffixCharset sufch_viscii[] = {
157 { "1", VISCII_CHARSET },
158 { NULL, VISCII_CHARSET }};
160 static SuffixCharset sufch_windows[] = {
161 { "1250", EE_CHARSET },
162 { "1251", RUSSIAN_CHARSET },
163 { "1252", ANSI_CHARSET },
164 { "1253", GREEK_CHARSET },
165 { "1254", TURKISH_CHARSET },
166 { "1255", HEBREW_CHARSET },
167 { "1256", ARABIC_CHARSET },
168 { "1257", BALTIC_CHARSET },
169 { NULL, BALTIC_CHARSET }}; /* CHECK/FIXME is BALTIC really the default ? */
171 /* Each of these must be matched explicitly */
172 static SuffixCharset sufch_any[] = {
173 { "fontspecific", SYMBOL_CHARSET },
180 SuffixCharset* sufch;
182 } fontEncodingTemplate;
184 /* Note: we can attach additional encoding mappings to the end
185 * of this table at runtime.
187 static fontEncodingTemplate __fETTable[] = {
188 { "ansi", sufch_ansi, &__fETTable[1] },
189 { "ascii", sufch_ansi, &__fETTable[2] },
190 { "iso646.1991", sufch_iso646, &__fETTable[3] },
191 { "iso8859", sufch_iso8859, &__fETTable[4] },
192 { "microsoft", sufch_microsoft, &__fETTable[5] },
193 { "tcvn", sufch_tcvn, &__fETTable[6] },
194 { "tis620.2533", sufch_tis620, &__fETTable[7] },
195 { "viscii1.1", sufch_viscii, &__fETTable[8] },
196 { "windows", sufch_windows, &__fETTable[9] },
197 /* NULL prefix matches anything so put it last */
198 { NULL, sufch_any, NULL },
200 static fontEncodingTemplate* fETTable = __fETTable;
202 static int DefResolution = 0;
204 static CRITICAL_SECTION crtsc_fonts_X11;
206 static fontResource* fontList = NULL;
207 static fontObject* fontCache = NULL; /* array */
208 static int fontCacheSize = FONTCACHE;
209 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
211 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
212 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
213 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
215 static Atom RAW_ASCENT;
216 static Atom RAW_DESCENT;
218 /***********************************************************************
219 * Helper macros from X distribution
222 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
223 (((cs)->rbearing|(cs)->lbearing| \
224 (cs)->ascent|(cs)->descent) == 0))
226 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
229 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
230 if (fs->per_char == NULL) { \
231 cs = &fs->min_bounds; \
233 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
234 if (CI_NONEXISTCHAR(cs)) cs = def; \
239 #define CI_GET_DEFAULT_INFO(fs,cs) \
240 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
242 /***********************************************************************
245 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
247 CHAR font[LF_FACESIZE];
251 #define ptr ((UINT16*)plf)
252 for( i = 0; i < 9; i++ ) checksum ^= *ptr++;
255 #define ptr ((CHAR*)plf)
256 do { font[i++] = tolower(*ptr++); } while (( i < LF_FACESIZE) && (*ptr) && (*ptr!=' '));
257 for( ptr = font, i >>= 1; i > 0; i-- )
259 #define ptr ((UINT16*)plf)
265 static UINT16 __genericCheckSum( const void *ptr, int size )
267 unsigned int checksum = 0;
268 const char *p = (const char *)ptr;
270 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
272 return checksum & 0xffff;
275 /*************************************************************************
276 * LFD parse/compose routines
278 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
281 * These functions also do TILDE to HYPHEN conversion
283 static LFD* LFD_Parse(LPSTR lpFont)
286 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
290 /* WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN); */
294 field_start = ++lpch;
295 for( i = 0; i < LFD_FIELDS; )
300 lfd_fld[i] = field_start;
302 field_start = ++lpch;
306 lfd_fld[i] = field_start;
310 else if (*lpch == TILDE)
318 /* Fill in the empty fields with NULLS */
319 for (; i< LFD_FIELDS; i++)
322 WARN("Extra ignored in font '%s'\n", lpFont);
324 lfd = HeapAlloc( SystemHeap, 0, sizeof(LFD) );
327 lfd->foundry = lfd_fld[0];
328 lfd->family = lfd_fld[1];
329 lfd->weight = lfd_fld[2];
330 lfd->slant = lfd_fld[3];
331 lfd->set_width = lfd_fld[4];
332 lfd->add_style = lfd_fld[5];
333 lfd->pixel_size = lfd_fld[6];
334 lfd->point_size = lfd_fld[7];
335 lfd->resolution_x = lfd_fld[8];
336 lfd->resolution_y = lfd_fld[9];
337 lfd->spacing = lfd_fld[10];
338 lfd->average_width = lfd_fld[11];
339 lfd->charset_registry = lfd_fld[12];
340 lfd->charset_encoding = lfd_fld[13];
346 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
348 char* lfd_fld[LFD_FIELDS];
352 return; /* Dont be silly */
354 lfd_fld[0] = lfd->foundry;
355 lfd_fld[1] = lfd->family;
356 lfd_fld[2] = lfd->weight ;
357 lfd_fld[3] = lfd->slant ;
358 lfd_fld[4] = lfd->set_width ;
359 lfd_fld[5] = lfd->add_style;
360 lfd_fld[6] = lfd->pixel_size;
361 lfd_fld[7] = lfd->point_size;
362 lfd_fld[8] = lfd->resolution_x;
363 lfd_fld[9] = lfd->resolution_y ;
364 lfd_fld[10] = lfd->spacing ;
365 lfd_fld[11] = lfd->average_width ;
366 lfd_fld[12] = lfd->charset_registry ;
367 lfd_fld[13] = lfd->charset_encoding ;
369 buf_size--; /* Room for the terminator */
371 for (i = 0; i < LFD_FIELDS; i++)
373 char* sp = lfd_fld[i];
374 if (!sp || !buf_size)
379 while (buf_size > 0 && *sp)
381 *dp = (*sp == HYPHEN) ? TILDE : *sp;
390 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
392 int j = lstrlenA(lpStr);
393 if( j == 1 && *lpStr == '0')
394 fi->fi_flags |= FI_POLYWEIGHT;
397 if( !strcasecmp( "bold", lpStr) )
398 fi->df.dfWeight = FW_BOLD;
399 else if( !strcasecmp( "demi", lpStr) )
401 fi->fi_flags |= FI_FW_DEMI;
402 fi->df.dfWeight = FW_DEMIBOLD;
404 else if( !strcasecmp( "book", lpStr) )
406 fi->fi_flags |= FI_FW_BOOK;
407 fi->df.dfWeight = FW_REGULAR;
412 if( !strcasecmp( "light", lpStr) )
413 fi->df.dfWeight = FW_LIGHT;
414 else if( !strcasecmp( "black", lpStr) )
415 fi->df.dfWeight = FW_BLACK;
417 else if( j == 6 && !strcasecmp( "medium", lpStr) )
418 fi->df.dfWeight = FW_REGULAR;
419 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
420 fi->df.dfWeight = FW_DEMIBOLD;
422 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
423 * from the weight property */
426 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
428 int l = lstrlenA(lpStr);
431 switch( tolower( *lpStr ) )
433 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
435 case 'r': fi->df.dfItalic = 0;
438 fi->fi_flags |= FI_OBLIQUE;
439 case 'i': fi->df.dfItalic = 1;
447 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
449 int j = lstrlenA(lpstr);
450 if( j > 3 ) /* find out is there "sans" or "script" */
454 if( strstr(lpstr, "sans") )
456 fi->df.dfPitchAndFamily |= FF_SWISS;
459 if( strstr(lpstr, "script") )
461 fi->df.dfPitchAndFamily |= FF_SCRIPT;
464 if( !j && dec_style_check )
465 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
469 /*************************************************************************
474 * Fill in some fields in the fontInfo struct.
476 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
478 int i, j, dec_style_check, scalability;
479 fontEncodingTemplate* boba;
480 const char* ridiculous = "font '%s' has ridiculous %s\n";
483 if (!lfd->charset_registry)
485 WARN("font '%s' does not have enough fields\n", fullname);
489 memset(fi, 0, sizeof(fontInfo) );
492 LFD_GetWeight( fi, lfd->weight);
495 dec_style_check = LFD_GetSlant( fi, lfd->slant);
498 lpstr = lfd->set_width;
499 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
500 dec_style_check = TRUE;
502 fi->fi_flags |= FI_NORMAL;
505 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
507 /* pixel & decipoint height, and res_x & y */
511 j = strlen(lfd->pixel_size);
512 if( j == 0 || j > 3 )
514 WARN(ridiculous, fullname, "pixel_size");
517 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
520 j = strlen(lfd->point_size);
521 if( j == 0 || j > 3 )
523 WARN(ridiculous, fullname, "point_size");
526 if( !(atoi(lfd->point_size)) )
529 j = strlen(lfd->resolution_x);
530 if( j == 0 || j > 3 )
532 WARN(ridiculous, fullname, "resolution_x");
535 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
538 j = strlen(lfd->resolution_y);
539 if( j == 0 || j > 3 )
541 WARN(ridiculous, fullname, "resolution_y");
544 if( !(atoi(lfd->resolution_y)) )
547 /* Check scalability */
552 case 4: /* Scalable */
553 fi->fi_flags |= FI_SCALABLE;
556 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
557 TRACE("Skipping scalable bitmap '%s'\n", fullname);
560 WARN("Font '%s' has weird scalability\n", fullname);
565 lpstr = lfd->spacing;
569 WARN("font '%s' has no spacing\n", fullname);
572 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
574 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
575 fi->fi_flags |= FI_FIXEDEX;
577 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
580 /* Of course this line does nothing */
581 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
584 /* average width - */
585 lpstr = lfd->average_width;
587 if( j == 0 || j > 3 )
589 WARN(ridiculous, fullname, "average_width");
593 if( !(atoi(lpstr)) && !scalability )
595 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
599 /* charset registry, charset encoding - */
600 lpstr = lfd->charset_registry;
601 if( strstr(lpstr, "jisx") ||
602 strstr(lpstr, "ksc") ||
603 strstr(lpstr, "gb2312") ||
604 strstr(lpstr, "big5") ||
605 strstr(lpstr, "unicode") )
607 TRACE(" 2-byte fonts like '%s' are not supported\n", fullname);
611 fi->df.dfCharSet = ANSI_CHARSET;
613 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
615 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
617 if (lfd->charset_encoding)
619 for( j = 0; boba->sufch[j].psuffix; j++ )
621 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
623 fi->df.dfCharSet = boba->sufch[j].charset;
629 WARN("font '%s' has unknown character encoding '%s'\n",
630 fullname, lfd->charset_encoding);
631 fi->df.dfCharSet = boba->sufch[j].charset;
636 else if (boba->prefix)
638 for( j = 0; boba->sufch[j].psuffix; j++ )
640 fi->df.dfCharSet = boba->sufch[j].charset;
646 /* WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr); */
650 /* i - index into fETTable
651 * j - index into suffix array for fETTable[i]
653 * 254 - unknown suffix
654 * 255 - no suffix at all.
656 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
662 /*************************************************************************
665 * make a matrix suitable for LFD based on theta radians
667 static void LFD_AngleMatrix( char* buffer, int h, double theta)
670 matrix[0] = h*cos(theta);
671 matrix[1] = h*sin(theta);
672 matrix[2] = -h*sin(theta);
673 matrix[3] = h*cos(theta);
674 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
677 /*************************************************************************
680 * Note: uRelax is a treatment not a cure. Font mapping algorithm
681 * should be bulletproof enough to allow us to avoid hacks like
682 * this despite LFD being so braindead.
684 static BOOL LFD_ComposeLFD( const fontObject* fo,
685 INT height, LPSTR lpLFD, UINT uRelax )
689 char h_string[64], resx_string[64], resy_string[64];
692 /* Get the worst case over with first */
694 /* RealizeFont() will call us repeatedly with increasing uRelax
695 * until XLoadFont() succeeds.
696 * to avoid an infinite loop; these will always match
701 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
703 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
707 /* read foundry + family from fo */
708 aLFD.foundry = fo->fr->resource->foundry;
709 aLFD.family = fo->fr->resource->family;
712 switch( fo->fi->df.dfWeight )
715 aLFD.weight = "bold"; break;
717 if( fo->fi->fi_flags & FI_FW_BOOK )
718 aLFD.weight = "book";
720 aLFD.weight = "medium";
723 aLFD.weight = "demi";
724 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
725 aLFD.weight = "bold";
728 aLFD.weight = "black"; break;
730 aLFD.weight = "light"; break;
736 if( fo->fi->df.dfItalic )
737 if( fo->fi->fi_flags & FI_OBLIQUE )
742 aLFD.slant = (uRelax < 1) ? "r" : any;
745 if( fo->fi->fi_flags & FI_NORMAL )
746 aLFD.set_width = "normal";
748 aLFD.set_width = any;
751 aLFD.add_style = any;
755 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
757 if( fo->fo_flags & FO_SYNTH_HEIGHT )
758 h = fo->fi->lfd_height;
761 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
762 h /= fo->fi->df.dfPixHeight;
764 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
766 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
768 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
773 /* handle rotated fonts */
774 if (fo->lf.lfEscapement) {
775 /* escapement is in tenths of degrees, theta is in radians */
776 double theta = M_PI*fo->lf.lfEscapement/1800.;
777 LFD_AngleMatrix(h_string, h, theta);
781 sprintf(h_string, "%d", h);
784 sprintf(h_string, "%d", fo->fi->lfd_height);
786 aLFD.pixel_size = h_string;
787 aLFD.point_size = any;
789 /* resolution_x,y, average width */
790 /* FOX ME - Why do some font servers ignore average width ?
791 * so that you have to mess around with res_y
793 aLFD.average_width = any;
796 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
797 aLFD.resolution_x = resx_string;
799 strcpy(resy_string, resx_string);
800 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
802 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
804 int resy = 0.5 + fo->fi->lfd_resolution *
805 (fo->fi->df.dfAvgWidth * height) /
806 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
807 sprintf(resy_string, "%d", resy);
810 ; /* FIXME - synth width */
812 aLFD.resolution_y = resy_string;
816 aLFD.resolution_x = aLFD.resolution_y = any;
823 if( fo->fi->fi_flags & FI_FIXEDPITCH )
824 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
826 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
828 aLFD.spacing = (uRelax <= 1) ? w : any;
835 fontEncodingTemplate* boba;
837 i = fo->fi->fi_encoding >> 8;
838 for( boba = fETTable; i; i--, boba = boba->next );
840 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
842 i = fo->fi->fi_encoding & 255;
846 aLFD.charset_encoding = boba->sufch[i].psuffix;
850 aLFD.charset_encoding = any;
853 case 255: /* no suffix - it ends eg "-ascii" */
854 aLFD.charset_encoding = NULL;
860 aLFD.charset_registry = any;
861 aLFD.charset_encoding = any;
864 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
866 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
871 /***********************************************************************
874 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
875 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
877 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
878 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
880 unsigned long height;
881 unsigned min = (unsigned char)pFI->dfFirstChar;
882 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
887 Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
888 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
890 (INT)(XFT->pixelsize / 1000.0 * height);
896 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
900 height = x_fs->per_char['X' - min].ascent;
902 if (x_fs->ascent >= x_fs->max_bounds.ascent)
903 height = x_fs->max_bounds.ascent;
906 height = x_fs->ascent;
908 *pEL = x_fs->max_bounds.ascent - height;
911 height = x_fs->min_bounds.ascent;
914 *pIL = x_fs->ascent - height;
917 /***********************************************************************
920 static int XFONT_CharWidth(const XFontStruct* x_fs,
921 const XFONTTRANS *XFT, int ch)
924 return x_fs->per_char[ch].width;
926 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
929 /***********************************************************************
930 * XFONT_GetAvgCharWidth
932 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
933 const XFONTTRANS *XFT)
935 unsigned min = (unsigned char)pFI->dfFirstChar;
936 unsigned max = (unsigned char)pFI->dfLastChar;
942 int width = 0, chars = 0, j;
943 if( IS_LATIN_CHARSET(pFI->dfCharSet))
945 /* FIXME - should use a weighted average */
946 for( j = 0; j < 26; j++ )
947 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
948 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
951 else /* unweighted average of everything */
953 for( j = 0, max -= min; j <= max; j++ )
954 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
956 width += XFONT_CharWidth(x_fs, XFT, j);
960 avg = (width + (chars>>1))/ chars;
962 else /* uniform width */
963 avg = x_fs->min_bounds.width;
968 /***********************************************************************
969 * XFONT_GetMaxCharWidth
971 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
973 unsigned min = (unsigned char)xfs->min_char_or_byte2;
974 unsigned max = (unsigned char)xfs->max_char_or_byte2;
977 if(!XFT || !xfs->per_char)
978 return abs(xfs->max_bounds.width);
980 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
981 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
982 if(maxwidth < xfs->per_char[j].attributes)
983 maxwidth = xfs->per_char[j].attributes;
985 maxwidth *= XFT->pixelsize / 1000.0;
989 /***********************************************************************
990 * XFONT_SetFontMetric
994 * Initializes IFONTINFO16.
996 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
999 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1000 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1002 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1003 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1005 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1006 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1008 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1009 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1010 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1012 if( xfs->min_bounds.width != xfs->max_bounds.width )
1013 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1014 if( fi->fi_flags & FI_SCALABLE )
1016 fi->df.dfType = DEVICE_FONTTYPE;
1017 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1019 else if( fi->fi_flags & FI_TRUETYPE )
1020 fi->df.dfType = TRUETYPE_FONTTYPE;
1022 fi->df.dfType = RASTER_FONTTYPE;
1024 fi->df.dfFace = fr->lfFaceName;
1027 /***********************************************************************
1028 * XFONT_GetTextMetrics
1030 * GetTextMetrics() back end.
1032 static void XFONT_GetTextMetrics( const fontObject* pfo, const LPTEXTMETRICA pTM )
1034 LPIFONTINFO16 pdf = &pfo->fi->df;
1036 if( ! pfo->lpX11Trans ) {
1037 pTM->tmAscent = pfo->fs->ascent;
1038 pTM->tmDescent = pfo->fs->descent;
1040 pTM->tmAscent = pfo->lpX11Trans->ascent;
1041 pTM->tmDescent = pfo->lpX11Trans->descent;
1044 pTM->tmAscent *= pfo->rescale;
1045 pTM->tmDescent *= pfo->rescale;
1047 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
1049 pTM->tmAveCharWidth = pfo->foAvgCharWidth * pfo->rescale;
1050 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
1052 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
1053 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
1055 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
1056 ? 1 : pdf->dfStrikeOut;
1057 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
1058 ? 1 : pdf->dfUnderline;
1060 pTM->tmOverhang = 0;
1061 if( pfo->fo_flags & FO_SYNTH_ITALIC )
1063 pTM->tmOverhang += pTM->tmHeight/3;
1066 pTM->tmItalic = pdf->dfItalic;
1068 pTM->tmWeight = pdf->dfWeight;
1069 if( pfo->fo_flags & FO_SYNTH_BOLD )
1072 pTM->tmWeight += 100;
1075 pTM->tmFirstChar = pdf->dfFirstChar;
1076 pTM->tmLastChar = pdf->dfLastChar;
1077 pTM->tmDefaultChar = pdf->dfDefaultChar;
1078 pTM->tmBreakChar = pdf->dfBreakChar;
1080 pTM->tmCharSet = pdf->dfCharSet;
1081 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
1083 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
1084 pTM->tmDigitizedAspectY = pdf->dfVertRes;
1087 /***********************************************************************
1088 * XFONT_GetFontMetric
1090 * Retrieve font metric info (enumeration).
1092 static UINT XFONT_GetFontMetric( const fontInfo* pfi, const LPENUMLOGFONTEX16 pLF,
1093 const LPNEWTEXTMETRIC16 pTM )
1095 memset( pLF, 0, sizeof(*pLF) );
1096 memset( pTM, 0, sizeof(*pTM) );
1098 #define plf ((LPLOGFONT16)pLF)
1099 plf->lfHeight = pTM->tmHeight = pfi->df.dfPixHeight;
1100 plf->lfWidth = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
1101 plf->lfWeight = pTM->tmWeight = pfi->df.dfWeight;
1102 plf->lfItalic = pTM->tmItalic = pfi->df.dfItalic;
1103 plf->lfUnderline = pTM->tmUnderlined = pfi->df.dfUnderline;
1104 plf->lfStrikeOut = pTM->tmStruckOut = pfi->df.dfStrikeOut;
1105 plf->lfCharSet = pTM->tmCharSet = pfi->df.dfCharSet;
1107 /* convert pitch values */
1109 pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1110 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1112 lstrcpynA( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
1115 /* FIXME: fill in rest of plF values
1116 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1117 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1118 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1121 pTM->tmAscent = pfi->df.dfAscent;
1122 pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
1123 pTM->tmInternalLeading = pfi->df.dfInternalLeading;
1124 pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
1125 pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
1126 pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
1128 pTM->tmFirstChar = pfi->df.dfFirstChar;
1129 pTM->tmLastChar = pfi->df.dfLastChar;
1130 pTM->tmDefaultChar = pfi->df.dfDefaultChar;
1131 pTM->tmBreakChar = pfi->df.dfBreakChar;
1133 /* return font type */
1135 return pfi->df.dfType;
1139 /***********************************************************************
1144 * dfPitchAndFamily flags for some common typefaces.
1146 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1148 switch( lfFaceName[0] )
1151 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1155 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1159 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1162 if (!strcasecmp(lfFaceName, "Charter") )
1166 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1170 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1174 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1178 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1179 return FF_DECORATIVE;
1184 /***********************************************************************
1185 * XFONT_SameFoundryAndFamily
1189 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1191 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1192 !strcasecmp( lfd1->family, lfd2->family ) );
1195 /***********************************************************************
1196 * XFONT_InitialCapitals
1200 * Upper case first letters of words & remove multiple spaces
1202 static void XFONT_InitialCapitals(LPSTR lpch)
1208 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1210 if( isspace(*lpch) )
1212 if (!up) /* Not already got one */
1218 else if( isalpha(*lpch) && up )
1220 *lpstr++ = toupper(*lpch);
1230 /* Remove possible trailing space */
1237 /***********************************************************************
1238 * XFONT_WindowsNames
1242 * Build generic Windows aliases for X font names.
1244 * -misc-fixed- -> "Fixed"
1245 * -sony-fixed- -> "Sony Fixed", etc...
1247 static void XFONT_WindowsNames(void)
1251 for( fr = fontList; fr ; fr = fr->next )
1256 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1258 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1259 if( pfr->fr_flags & FR_NAMESET )
1261 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1265 lpch = fr->lfFaceName;
1266 wsnprintfA( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1267 /* prepend vendor name */
1268 (pfr==fr) ? "" : fr->resource->foundry,
1269 fr->resource->family);
1270 XFONT_InitialCapitals(fr->lfFaceName);
1272 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1276 for( fi = fr->fi ; fi ; fi = fi->next )
1277 fi->df.dfPitchAndFamily |= bFamilyStyle;
1281 TRACE("typeface '%s'\n", fr->lfFaceName);
1283 fr->fr_flags |= FR_NAMESET;
1287 /***********************************************************************
1288 * XFONT_LoadDefaultLFD
1290 * Move lfd to the head of fontList to make it more likely to be matched
1292 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1295 fontResource *fr, *pfr;
1296 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1298 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1302 fr->next = pfr->next;
1303 pfr->next = fontList;
1311 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1312 lfd->foundry, lfd->family);
1316 /***********************************************************************
1319 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1321 char buffer[MAX_LFD_LENGTH];
1323 if( PROFILE_GetWineIniString( INIFontSection, ini, "", buffer, sizeof buffer ) )
1329 while( *pch && isspace(*pch) ) pch++;
1331 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1332 lfd = LFD_Parse(pch);
1333 if (lfd && lfd->foundry && lfd->family)
1334 XFONT_LoadDefaultLFD(lfd, fonttype);
1336 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1337 HeapFree(SystemHeap, 0, lfd);
1342 /***********************************************************************
1343 * XFONT_LoadDefaults
1345 static void XFONT_LoadDefaults(void)
1347 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1348 XFONT_LoadDefault(INIDefault, "");
1351 /***********************************************************************
1354 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1357 fontAlias *pfa, *prev = NULL;
1359 for(pfa = aliasTable; pfa; pfa = pfa->next)
1361 /* check if we already got one */
1362 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1364 TRACE("redundant alias '%s' -> '%s'\n",
1365 lpAlias, lpTypeFace );
1371 j = lstrlenA(lpTypeFace) + 1;
1372 pfa = HeapAlloc( SystemHeap, 0, sizeof(fontAlias) +
1373 j + lstrlenA(lpAlias) + 1 );
1382 pfa->faTypeFace = (LPSTR)(pfa + 1);
1383 lstrcpyA( pfa->faTypeFace, lpTypeFace );
1384 pfa->faAlias = pfa->faTypeFace + j;
1385 lstrcpyA( pfa->faAlias, lpAlias );
1387 TRACE("added alias '%s' for %s\n", lpAlias, lpTypeFace );
1395 /***********************************************************************
1398 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1400 fontResource *fr, *frMatch = NULL;
1401 if (!lfd->foundry || ! lfd->family)
1403 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1406 for (fr = fontList; fr ; fr = fr->next)
1408 if(!strcasecmp(fr->resource->family, lpAlias))
1410 /* alias is not needed since the real font is present */
1411 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1414 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1425 fontAlias *pfa, *prev = NULL;
1427 for(pfa = aliasTable; pfa; pfa = pfa->next)
1429 /* Remove lpAlias from aliasTable - we should free the old entry */
1430 if(!strcmp(lpAlias, pfa->faAlias))
1433 prev->next = pfa->next;
1435 aliasTable = pfa->next;
1438 /* Update any references to the substituted font in aliasTable */
1439 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1440 pfa->faTypeFace = HEAP_strdupA( SystemHeap, 0, lpAlias );
1444 TRACE("\tsubstituted '%s' with %s\n", frMatch->lfFaceName, lpAlias );
1446 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1447 frMatch->fr_flags |= FR_NAMESET;
1451 /* create new entry in the alias table */
1452 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1457 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1461 /***********************************************************************
1466 * Create font aliases for some standard windows fonts using users
1467 * default choice of (sans-)serif fonts
1469 * Read user-defined aliases from wine.conf. Format is as follows
1471 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1474 * Alias0 = Arial, -adobe-helvetica-
1475 * Alias1 = Times New Roman, -bitstream-courier-, 1
1478 * Note that from 081797 and on we have built-in alias templates that take
1479 * care of the necessary Windows typefaces.
1487 static void XFONT_LoadAliases(void)
1490 char buffer[MAX_LFD_LENGTH];
1494 /* built-ins first */
1495 PROFILE_GetWineIniString( INIFontSection, INIDefaultSerif,
1496 "-bitstream-charter-", buffer, sizeof buffer );
1497 TRACE("Using '%s' as default serif font\n", buffer);
1498 lfd = LFD_Parse(buffer);
1499 /* NB XFONT_InitialCapitals should not change these standard aliases */
1502 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1503 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1504 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1506 XFONT_LoadDefaultLFD( lfd, "serif ");
1507 HeapFree(SystemHeap, 0, lfd);
1510 PROFILE_GetWineIniString( INIFontSection, INIDefaultSansSerif,
1511 "-adobe-helvetica-", buffer, sizeof buffer);
1512 TRACE("Using '%s' as default sans serif font\n", buffer);
1513 lfd = LFD_Parse(buffer);
1516 XFONT_LoadAlias( lfd, "Helv", FALSE);
1517 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1518 XFONT_LoadAlias( lfd, "Arial", FALSE);
1520 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1521 HeapFree(SystemHeap, 0, lfd);
1524 /* then user specified aliases */
1527 BOOL bHaveAlias, bSubst;
1528 char subsection[32];
1529 wsnprintfA( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1531 bHaveAlias = PROFILE_GetWineIniString( INIFontSection,
1532 subsection, "", buffer, sizeof buffer);
1536 XFONT_InitialCapitals(buffer);
1537 lpResource = PROFILE_GetStringItem( buffer );
1538 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1539 if( lpResource && *lpResource )
1541 lfd = LFD_Parse(lpResource);
1544 XFONT_LoadAlias(lfd, buffer, bSubst);
1545 HeapFree(SystemHeap, 0, lfd);
1549 WARN("malformed font alias '%s'\n", buffer );
1554 /***********************************************************************
1557 * Convert an (potential) alias into a real windows name
1560 static LPCSTR XFONT_UnAlias(char* font)
1565 XFONT_InitialCapitals(font); /* to remove extra white space */
1567 for( fa = aliasTable; fa; fa = fa->next )
1568 /* use case insensitive matching to handle eg "MS Sans Serif" */
1569 if( !strcasecmp( fa->faAlias, font ) )
1571 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1572 strcpy(font, fa->faTypeFace);
1580 /***********************************************************************
1581 * XFONT_RemoveFontResource
1583 * Caller should check if the font resource is in use. If it is it should
1584 * set FR_REMOVED flag to delay removal until the resource is not in use
1587 void XFONT_RemoveFontResource( fontResource** ppfr )
1589 fontResource* pfr = *ppfr;
1593 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1596 pfi = pfr->fi->next;
1597 HeapFree( SystemHeap, 0, pfr->fi );
1600 HeapFree( SystemHeap, 0, pfr );
1605 /***********************************************************************
1610 * Removes specified fonts from the font table to prevent Wine from
1613 * Ignore# = [LFD font name]
1616 * Ignore0 = -misc-nil-
1617 * Ignore1 = -sun-open look glyph-
1621 static void XFONT_LoadIgnore(char* lfdname)
1623 fontResource** ppfr;
1625 LFD* lfd = LFD_Parse(lfdname);
1626 if (lfd && lfd->foundry && lfd->family)
1628 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1630 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1632 TRACE("Ignoring '-%s-%s-'\n",
1633 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1635 XFONT_RemoveFontResource( ppfr );
1641 WARN("Malformed font resource\n");
1643 HeapFree(SystemHeap, 0, lfd);
1646 static void XFONT_LoadIgnores(void)
1649 char subsection[32];
1650 char buffer[MAX_LFD_LENGTH];
1652 /* Standard one that noone wants */
1653 strcpy(buffer, "-misc-nil-");
1654 XFONT_LoadIgnore(buffer);
1656 /* Others from INI file */
1659 wsprintfA( subsection, "%s%i", INIIgnoreSection, i++ );
1661 if( PROFILE_GetWineIniString( INIFontSection,
1662 subsection, "", buffer, sizeof buffer) )
1665 while( *pch && isspace(*pch) ) pch++;
1666 XFONT_LoadIgnore(pch);
1674 /***********************************************************************
1675 * XFONT_UserMetricsCache
1677 * Returns expanded name for the cachedmetrics file.
1678 * Now it also appends the current value of the $DISPLAY varaible.
1680 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1682 char* pchDisplay, *home;
1684 pchDisplay = getenv( "DISPLAY" );
1685 if( !pchDisplay ) pchDisplay = "0";
1687 if ((home = getenv( "HOME" )) != NULL)
1689 int i = strlen( home ) + strlen( INIWinePrefix ) +
1690 strlen( INIFontMetrics ) + strlen( pchDisplay ) + 2;
1692 buffer = (char*) HeapReAlloc( SystemHeap, 0, buffer, *buf_size = i );
1693 strcpy( buffer, home );
1694 strcat( buffer, INIWinePrefix );
1695 strcat( buffer, INIFontMetrics );
1696 strcat( buffer, pchDisplay );
1697 } else buffer[0] = '\0';
1702 /***********************************************************************
1705 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1707 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1711 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1713 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1714 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1715 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1717 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1718 && fi->lfd_height != match->lfd_height) ||
1719 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1720 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1722 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1723 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1725 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1726 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1727 else if( m >= 0 ) return 1; /* 'match' is better */
1729 return -1; /* 'fi' is better */
1732 /***********************************************************************
1735 * REMOVE_SUBSETS - attach new fi and purge subsets
1736 * UNMARK_SUBSETS - remove subset flags from all fi entries
1738 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1741 fontInfo* pfi, *prev;
1743 for( prev = NULL, pfi = fr->fi; pfi; )
1745 if( action == REMOVE_SUBSETS )
1747 if( pfi->fi_flags & FI_SUBSET )
1749 fontInfo* subset = pfi;
1753 if( prev ) prev->next = pfi = pfi->next;
1754 else fr->fi = pfi = pfi->next;
1755 HeapFree( SystemHeap, 0, subset );
1759 else pfi->fi_flags &= ~FI_SUBSET;
1765 if( action == REMOVE_SUBSETS ) /* also add the superset */
1767 if( fi->fi_flags & FI_SCALABLE )
1772 else if( prev ) prev->next = fi; else fr->fi = fi;
1776 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1779 /***********************************************************************
1782 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1786 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1792 /***********************************************************************
1793 * XFONT_FixupPointSize
1795 static void XFONT_FixupPointSize(fontInfo* fi)
1798 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1799 df.dfPoints = (INT16)
1800 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1805 /***********************************************************************
1806 * XFONT_BuildMetrics
1808 * Build font metrics from X font
1810 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1813 fontInfo* fi = NULL;
1816 MESSAGE("Building font metrics. This may take some time...\n");
1817 for( i = 0; i < x_count; i++ )
1821 fontResource* fr, *pfr;
1823 char buffer[MAX_LFD_LENGTH];
1828 typeface = HEAP_strdupA(SystemHeap, 0, x_pattern[i]);
1832 lfd = LFD_Parse(typeface);
1835 HeapFree(SystemHeap, 0, typeface);
1839 /* find a family to insert into */
1841 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1843 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
1848 if( !fi ) fi = (fontInfo*) HeapAlloc(SystemHeap, 0, sizeof(fontInfo));
1850 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
1853 if( !fr ) /* add new family */
1856 fr = (fontResource*) HeapAlloc(SystemHeap, 0, sizeof(fontResource));
1859 memset(fr, 0, sizeof(fontResource));
1861 fr->resource = (LFD*) HeapAlloc(SystemHeap, 0, sizeof(LFD));
1862 memset(fr->resource, 0, sizeof(LFD));
1864 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
1865 fr->resource->foundry = HEAP_strdupA(SystemHeap, 0, lfd->foundry);
1866 fr->resource->family = HEAP_strdupA(SystemHeap, 0, lfd->family);
1867 fr->resource->weight = "";
1869 if( pfr ) pfr->next = fr;
1873 WARN("Not enough memory for a new font family\n");
1876 /* check if we already have something better than "fi" */
1878 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
1879 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
1880 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1881 if( j > 0 ) goto nextfont;
1883 /* add new font instance "fi" to the "fr" font resource */
1885 if( fi->fi_flags & FI_SCALABLE )
1888 char pxl_string[4], res_string[4];
1889 /* set scalable font height to get an basis for extrapolation */
1891 fi->lfd_height = DEF_SCALABLE_HEIGHT;
1892 fi->lfd_resolution = res;
1894 sprintf(pxl_string, "%d", fi->lfd_height);
1895 sprintf(res_string, "%d", fi->lfd_resolution);
1898 lfd1.pixel_size = pxl_string;
1899 lfd1.point_size = "*";
1900 lfd1.resolution_x = res_string;
1901 lfd1.resolution_y = res_string;
1903 LFD_UnParse(buffer, sizeof buffer, &lfd1);
1907 else lpstr = x_pattern[i];
1909 if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1911 XFONT_SetFontMetric( fi, fr, x_fs );
1912 TSXFreeFont( display, x_fs );
1914 XFONT_FixupPointSize(fi);
1916 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
1918 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1919 fi = NULL; /* preventing reuse */
1923 ERR("failed to load %s\n", lpstr );
1925 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1928 HeapFree(SystemHeap, 0, lfd);
1929 HeapFree(SystemHeap, 0, typeface);
1931 if( fi ) HeapFree(SystemHeap, 0, fi);
1936 /***********************************************************************
1937 * XFONT_ReadCachedMetrics
1941 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
1948 /* read checksums */
1949 read( fd, &u, sizeof(unsigned) );
1950 read( fd, &i, sizeof(int) );
1952 if( u == x_checksum && i == x_count )
1954 off_t length, offset = 3 * sizeof(int);
1956 /* read total size */
1957 read( fd, &i, sizeof(int) );
1958 length = lseek( fd, 0, SEEK_END );
1960 if( length == (i + offset) )
1962 lseek( fd, offset, SEEK_SET );
1963 fontList = (fontResource*)HeapAlloc( SystemHeap, 0, i);
1966 fontResource* pfr = fontList;
1967 fontInfo* pfi = NULL;
1969 TRACE("Reading cached font metrics:\n");
1971 read( fd, fontList, i); /* read all metrics at once */
1972 while( offset < length )
1974 offset += sizeof(fontResource) + sizeof(fontInfo);
1975 pfr->fi = pfi = (fontInfo*)(pfr + 1);
1979 if( offset > length ||
1980 (int)(pfi->next) != j++ ) goto fail;
1982 pfi->df.dfFace = pfr->lfFaceName;
1983 if( pfi->fi_flags & FI_SCALABLE )
1985 /* we can pretend we got this font for any resolution */
1986 pfi->lfd_resolution = res;
1987 XFONT_FixupPointSize(pfi);
1989 pfi->next = pfi + 1;
1991 if( j > pfr->fi_count ) break;
1994 offset += sizeof(fontInfo);
1999 pfr->next = (fontResource*)(pfi + 1);
2004 if( pfr->next == NULL &&
2005 *(int*)(pfi + 1) == X_FMC_MAGIC )
2007 /* read LFD stubs */
2008 char* lpch = (char*)((int*)(pfi + 1) + 1);
2009 offset += sizeof(int);
2010 for( pfr = fontList; pfr; pfr = pfr->next )
2012 size_t len = strlen(lpch) + 1;
2013 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2014 pfr->resource = LFD_Parse(lpch);
2017 if (offset > length)
2027 if( fontList ) HeapFree( SystemHeap, 0, fontList );
2034 /***********************************************************************
2035 * XFONT_WriteCachedMetrics
2039 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2047 char buffer[MAX_LFD_LENGTH];
2049 /* font metrics file:
2053 * +0008 total size to load
2054 * +000C prepackaged font metrics
2057 * +...x + 4 LFD stubs
2060 write( fd, &x_checksum, sizeof(unsigned) );
2061 write( fd, &x_count, sizeof(int) );
2063 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2065 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2066 i += strlen( buffer) + 1;
2069 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2070 write( fd, &i, sizeof(int) );
2072 TRACE("Writing font cache:\n");
2074 for( pfr = fontList; pfr; pfr = pfr->next )
2078 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2080 i = write( fd, pfr, sizeof(fontResource) );
2081 if( i == sizeof(fontResource) )
2083 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2087 fi.df.dfFace = NULL;
2088 fi.next = (fontInfo*)k; /* loader checks this */
2090 j = write( fd, &fi, sizeof(fi) );
2093 if( j == sizeof(fontInfo) ) continue;
2097 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2099 i = j = X_FMC_MAGIC;
2100 write( fd, &i, sizeof(int) );
2101 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2103 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2104 i = strlen( buffer ) + 1;
2105 j = write( fd, buffer, i );
2114 /***********************************************************************
2115 * XFONT_CheckIniSection
2119 * Examines wine.conf for old/invalid font entries and recommend changes to
2123 * 05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
2124 * Original implementation.
2126 static void XFONT_CheckIniCallback(char const *, char const *, void *);
2128 static char const *fontmsgprologue =
2130 " The following entries in the [fonts] section of the wine.conf file are\n"
2131 " obsolete or invalid:\n";
2133 static char const *fontmsgepilogue =
2134 " These entries should be eliminated or updated.\n"
2135 " See the documentation/fonts file for more information.\n";
2137 static int XFONT_CheckIniSection(void)
2141 PROFILE_EnumerateWineIniSection("Fonts", &XFONT_CheckIniCallback,
2144 MESSAGE(fontmsgepilogue);
2149 static void XFONT_CheckIniCallback(
2154 /* Ignore any keys that start with potential comment characters "'", '#',
2156 if(key[0] == '\'' || key[0] == '#' || key[0] == ';' || key[0] == '\0')
2159 /* Make sure this is a valid key */
2160 if((strncasecmp(key, INIAliasSection, 5) == 0) ||
2161 (strncasecmp(key, INIIgnoreSection, 6) == 0) ||
2162 (strcasecmp( key, INIDefault) == 0) ||
2163 (strcasecmp( key, INIDefaultFixed) == 0) ||
2164 (strcasecmp( key, INIGlobalMetrics) == 0) ||
2165 (strcasecmp( key, INIResolution) == 0) ||
2166 (strcasecmp( key, INIDefaultSerif) == 0) ||
2167 (strcasecmp( key, INIDefaultSansSerif) ==0) )
2169 /* Valid key; make sure the value doesn't contain a wildcard */
2170 if(strchr(value, '*')) {
2171 if(*(int *)found == 0) {
2172 MESSAGE(fontmsgprologue);
2175 MESSAGE(" %s=%s [no wildcards allowed]\n", key, value);
2179 /* Not a valid key */
2180 if(*(int *)found == 0) {
2181 MESSAGE(fontmsgprologue);
2185 MESSAGE(" %s=%s [obsolete]\n", key, value);
2191 /***********************************************************************
2192 * XFONT_GetPointResolution()
2196 * Here we initialize DefResolution which is used in the
2197 * XFONT_Match() penalty function. We also load the point
2198 * resolution value (higher values result in larger fonts).
2200 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
2202 int i, j, point_resolution, num = 3;
2203 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2204 int best = 0, best_diff = 65536;
2206 point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
2207 if( !point_resolution )
2208 point_resolution = pDevCaps->logPixelsY;
2210 pDevCaps->logPixelsX = pDevCaps->logPixelsY = point_resolution;
2213 /* FIXME We can only really guess at a best DefResolution
2214 * - this should be configurable
2216 for( i = best = 0; i < num; i++ )
2218 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2225 DefResolution = allowed_xfont_resolutions[best];
2227 /* FIXME - do win95,nt40,... do this as well ? */
2228 if (TWEAK_WineLook == WIN98_LOOK)
2230 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2232 denom = pDevCaps->logPixelsX * 10;
2233 pDevCaps->horzSize = (pDevCaps->horzRes * 254 + (denom>>1)) / denom;
2234 denom = pDevCaps->logPixelsY * 10;
2235 pDevCaps->vertSize = (pDevCaps->vertRes * 254 + (denom>>1)) / denom;
2238 return point_resolution;
2242 /***********************************************************************
2245 * Compute the matching score between the logical font and the device font.
2247 * contributions from highest to lowest:
2251 * family flags (only when the facename is not present)
2253 * weight, italics, underlines, strikeouts
2255 * NOTE: you can experiment with different penalty weights to see what happens.
2256 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2258 static UINT XFONT_Match( fontMatch* pfm )
2260 fontInfo* pfi = pfm->pfi; /* device font to match */
2261 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2263 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2264 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2267 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2268 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2269 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2270 (pfi->df.dfItalic) ? "Italic" : "" );
2272 pfm->flags &= FO_MATCH_MASK;
2275 if( plf->lfCharSet == DEFAULT_CHARSET )
2277 if( (pfi->df.dfCharSet != ANSI_CHARSET) && (pfi->df.dfCharSet != DEFAULT_CHARSET) )
2280 else if (plf->lfCharSet != pfi->df.dfCharSet) penalty += 0x200;
2285 if( plf->lfHeight > 0 )
2287 int h = pfi->df.dfPixHeight;
2288 d = h - plf->lfHeight;
2289 height = plf->lfHeight;
2293 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2296 d = h + plf->lfHeight;
2297 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2301 ERR("PixHeight == InternalLeading\n");
2302 penalty += 0x1000; /* dont want this */
2308 pfm->height = 1; /* Very small */
2312 pfm->height = height;
2313 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2315 if( d > 0 ) /* do not shrink raster fonts */
2317 pfm->height = pfi->df.dfPixHeight;
2318 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2320 else /* expand only in integer multiples */
2322 pfm->height = height - height%pfi->df.dfPixHeight;
2323 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2326 else /* can't be scaled at all */
2328 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2329 pfm->height = pfi->df.dfPixHeight;
2330 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2334 pfm->height = pfi->df.dfPixHeight;
2336 /* Pitch and Family */
2337 if( pfm->flags & FO_MATCH_PAF ) {
2338 int family = plf->lfPitchAndFamily & FF_FAMILY;
2340 /* TMPF_FIXED_PITCH means exactly the opposite */
2341 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2342 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2343 } else /* Variable is the default */
2344 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2346 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2354 if( bR6 || bScale ) h = 0;
2357 /* FIXME: not complete */
2359 pfm->flags |= FO_SYNTH_WIDTH;
2360 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2362 penalty += h * ( d ) ? 0x2 : 0x1 ;
2364 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2367 if( plf->lfWeight != FW_DONTCARE )
2369 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2370 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2371 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2374 if( plf->lfItalic != pfi->df.dfItalic )
2377 pfm->flags |= FO_SYNTH_ITALIC;
2380 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2383 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2386 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2389 TRACE(" returning %i\n", penalty );
2394 /***********************************************************************
2397 * Scan a particular font resource for the best match.
2399 static UINT XFONT_MatchFIList( fontMatch* pfm )
2401 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2402 UINT current_score, score = (UINT)(-1);
2403 fontMatch fm = *pfm;
2405 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2407 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2410 current_score = XFONT_Match( &fm );
2411 if( score > current_score )
2414 score = current_score;
2420 /***********************************************************************
2421 * XFONT_MatchDeviceFont
2423 * Scan font resource tree.
2426 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2428 fontMatch fm = *pfm;
2429 UINT current_score, score = (UINT)(-1);
2430 fontResource** ppfr;
2432 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2433 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2434 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2437 if( fm.plf->lfFaceName[0] )
2439 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2440 if( fm.pfr ) /* match family */
2442 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2444 if( fm.pfr->fr_flags & FR_REMOVED )
2448 XFONT_MatchFIList( &fm );
2456 /* match all available fonts */
2458 fm.flags |= FO_MATCH_PAF;
2459 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2461 if( (*ppfr)->fr_flags & FR_REMOVED )
2463 if( (*ppfr)->fo_count == 0 )
2464 XFONT_RemoveFontResource( ppfr );
2470 TRACE("%s\n", fm.pfr->lfFaceName );
2472 current_score = XFONT_MatchFIList( &fm );
2473 if( current_score < score )
2475 score = current_score;
2482 /***********************************************************************
2485 static void XFONT_GrowFreeList(int start, int end)
2487 /* add all entries from 'start' up to and including 'end' */
2489 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2491 fontCache[end].lru = fontLF;
2492 fontCache[end].count = -1;
2494 while( start < end )
2496 fontCache[start].count = -1;
2497 fontCache[start].lru = start + 1;
2502 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2504 UINT16 cs = __lfCheckSum( plf );
2505 int i = fontMRU, prev = -1;
2510 if( fontCache[i].lfchecksum == cs &&
2511 !(fontCache[i].fo_flags & FO_REMOVED) )
2513 /* FIXME: something more intelligent here ? */
2515 if( !memcmp( plf, &fontCache[i].lf,
2516 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2517 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2519 /* remove temporarily from the lru list */
2521 fontCache[prev].lru = fontCache[i].lru;
2523 fontMRU = (INT16)fontCache[i].lru;
2524 return (fontCache + i);
2528 i = (INT16)fontCache[i].lru;
2533 static fontObject* XFONT_GetCacheEntry(void)
2539 int prev_i, prev_j, j;
2541 TRACE("font cache is full\n");
2543 /* lookup the least recently used font */
2545 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2547 if( fontCache[i].count <= 0 &&
2548 !(fontCache[i].fo_flags & FO_SYSTEM) )
2556 if( j >= 0 ) /* unload font */
2558 /* detach from the lru list */
2560 TRACE("\tfreeing entry %i\n", j );
2562 fontCache[j].fr->fo_count--;
2565 fontCache[prev_j].lru = fontCache[j].lru;
2566 else fontMRU = (INT16)fontCache[j].lru;
2568 /* FIXME: lpXForm, lpPixmap */
2569 if(fontCache[j].lpX11Trans)
2570 HeapFree( SystemHeap, 0, fontCache[j].lpX11Trans );
2572 TSXFreeFont( display, fontCache[j].fs );
2574 memset( fontCache + j, 0, sizeof(fontObject) );
2575 return (fontCache + j);
2577 else /* expand cache */
2579 fontObject* newCache;
2581 prev_i = fontCacheSize + FONTCACHE;
2583 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2585 if( (newCache = (fontObject*)HeapReAlloc(SystemHeap, 0,
2586 fontCache, prev_i)) )
2589 fontCacheSize = prev_i;
2590 fontCache = newCache;
2591 XFONT_GrowFreeList( i, fontCacheSize - 1);
2597 /* detach from the free list */
2600 fontLF = (INT16)fontCache[i].lru;
2601 fontCache[i].count = 0;
2602 return (fontCache + i);
2605 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2607 UINT u = (UINT)(pfo - fontCache);
2609 if( u < fontCacheSize ) return (--fontCache[u].count);
2613 /***********************************************************************
2616 * Initialize font resource list and allocate font cache.
2618 BOOL X11DRV_FONT_Init( DeviceCaps* pDevCaps )
2621 unsigned x_checksum;
2622 int i,res, x_count, fd, buf_size;
2625 XFONT_CheckIniSection();
2627 res = XFONT_GetPointResolution( pDevCaps );
2629 x_pattern = TSXListFonts(display, "*", MAX_FONTS, &x_count );
2631 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2632 x_count, res, DefResolution);
2633 if (x_count == MAX_FONTS)
2634 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2636 for( i = x_checksum = 0; i < x_count; i++ )
2640 printf("%i\t: %s\n", i, x_pattern[i] );
2643 j = strlen( x_pattern[i] );
2644 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2646 x_checksum |= X_PFONT_MAGIC;
2648 buffer = HeapAlloc( SystemHeap, 0, buf_size );
2650 /* deal with systemwide font metrics cache */
2652 if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, buf_size ) )
2654 fd = open( buffer, O_RDONLY );
2655 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2657 if (fontList == NULL)
2660 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2663 fd = open( buffer, O_RDONLY );
2664 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2668 if( fontList == NULL ) /* build metrics from scratch */
2670 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2671 if( buffer[0] ) /* update cached metrics */
2673 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2674 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2676 WARN("Unable to write to fontcache '%s'\n", buffer);
2677 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2682 TSXFreeFontNames(x_pattern);
2684 /* check if we're dealing with X11 R6 server */
2687 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2688 if( (x_fs = TSXLoadQueryFont(display, buffer)) )
2690 XTextCaps |= TC_SF_X_YINDEP;
2691 TSXFreeFont(display, x_fs);
2694 HeapFree(SystemHeap, 0, buffer);
2696 XFONT_WindowsNames();
2697 XFONT_LoadAliases();
2698 XFONT_LoadDefaults();
2699 XFONT_LoadIgnores();
2701 InitializeCriticalSection( &crtsc_fonts_X11 );
2702 MakeCriticalSectionGlobal( &crtsc_fonts_X11 );
2704 /* fontList initialization is over, allocate X font cache */
2706 fontCache = (fontObject*) HeapAlloc(SystemHeap, 0, fontCacheSize * sizeof(fontObject));
2707 XFONT_GrowFreeList(0, fontCacheSize - 1);
2711 /* update text caps parameter */
2713 pDevCaps->textCaps = XTextCaps;
2715 RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
2716 RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
2721 /**********************************************************************
2724 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2730 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2731 fontName = TSXGetAtomName( display, nameAtom );
2732 lfd = LFD_Parse(fontName);
2739 if (lfd->pixel_size[0] != '[') {
2740 HeapFree(SystemHeap, 0, lfd);
2745 #define PX pfo->lpX11Trans
2747 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2749 HeapFree(SystemHeap, 0, lfd);
2751 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2752 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2754 PX->pixelsize = hypot(PX->a, PX->b);
2755 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2756 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2758 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2759 PX->a, PX->b, PX->c, PX->d,
2760 PX->RAW_ASCENT, PX->RAW_DESCENT);
2766 /***********************************************************************
2767 * X Device Font Objects
2769 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf, LPCSTR* faceMatched)
2775 pfo = XFONT_LookupCachedFont( plf, &checksum );
2787 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2789 /* allocate new font cache entry */
2791 if( (pfo = XFONT_GetCacheEntry()) )
2793 /* initialize entry and load font */
2794 char lpLFD[MAX_LFD_LENGTH];
2795 UINT uRelaxLevel = 0;
2797 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2799 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2801 pfo->rescale = fabs(plf->lfHeight / 100.0);
2802 if(plf->lfHeight > 0) plf->lfHeight = 100;
2803 else plf->lfHeight = -100;
2807 XFONT_MatchDeviceFont( fontList, &fm );
2810 pfo->fr->fo_count++;
2811 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2814 pfo->lfchecksum = checksum;
2818 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2819 if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2820 } while( uRelaxLevel );
2823 if(pfo->lf.lfEscapement != 0) {
2824 pfo->lpX11Trans = HeapAlloc(SystemHeap, 0, sizeof(XFONTTRANS));
2825 if(!XFONT_SetX11Trans( pfo )) {
2826 HeapFree(SystemHeap, 0, pfo->lpX11Trans);
2827 pfo->lpX11Trans = NULL;
2830 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
2831 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
2832 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2833 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
2835 /* FIXME: If we've got a soft font or
2836 * there are FO_SYNTH_... flags for the
2837 * non PROOF_QUALITY request, the engine
2838 * should rasterize characters into mono
2839 * pixmaps and store them in the pfo->lpPixmap
2840 * array (pfo->fs should be updated as well).
2841 * array (pfo->fs should be updated as well).
2842 * X11DRV_ExtTextOut() must be heavily modified
2843 * to support pixmap blitting and FO_SYNTH_...
2847 pfo->lpPixmap = NULL;
2850 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2852 UINT current_score, score = (UINT)(-1);
2854 i = index = fontMRU;
2855 fm.flags |= FO_MATCH_PAF;
2858 pfo = fontCache + i;
2859 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2861 current_score = XFONT_Match( &fm );
2862 if( current_score < score ) index = i;
2866 pfo = fontCache + index;
2871 /* attach at the head of the lru list */
2873 index = fontMRU = (pfo - fontCache);
2878 TRACE("physfont %i\n", index);
2879 *faceMatched = pfo->fi->df.dfFace;
2881 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
2884 /***********************************************************************
2885 * XFONT_GetFontObject
2887 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
2889 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
2893 /***********************************************************************
2894 * XFONT_GetFontStruct
2896 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
2898 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
2902 /***********************************************************************
2905 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
2907 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
2913 /* X11DRV Interface ****************************************************
2915 * Exposed via the dc->funcs dispatch table. *
2917 ***********************************************************************/
2918 /***********************************************************************
2919 * X11DRV_FONT_SelectObject
2921 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont, FONTOBJ* font )
2923 HFONT hPrevFont = 0;
2925 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2927 EnterCriticalSection( &crtsc_fonts_X11 );
2929 if( CHECK_PFONT(physDev->font) )
2930 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
2934 /* Make sure we don't change the sign when converting to device coords */
2935 /* FIXME - check that the other drivers do this correctly */
2938 int vpt = abs(dc->vportExtX);
2939 int wnd = abs(dc->wndExtX);
2940 lf.lfWidth = (abs(lf.lfWidth) * vpt + (wnd>>1))/wnd;
2941 if (lf.lfWidth == 0)
2942 lf.lfWidth = 1; /* Minimum width */
2946 int vpt = abs(dc->vportExtY);
2947 int wnd = abs(dc->wndExtY);
2948 if (lf.lfHeight > 0)
2949 lf.lfHeight = (lf.lfHeight * vpt + (wnd>>1))/wnd;
2951 lf.lfHeight = (lf.lfHeight * vpt - (wnd>>1))/wnd;
2953 if (lf.lfHeight == 0)
2954 lf.lfHeight = MIN_FONT_SIZE;
2957 lf.lfHeight = -(DEF_POINT_SIZE * dc->w.devCaps->logPixelsY + (72>>1)) / 72;
2960 /* Fixup aliases before passing to RealizeFont */
2961 /* alias = Windows name in the alias table */
2962 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
2965 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
2966 physDev->font = XFONT_RealizeFont( &lf, &faceMatched );
2968 /* set face to the requested facename if it matched
2969 * so that GetTextFace can get the correct face name
2971 if (alias && !strcmp(faceMatched, lf.lfFaceName))
2972 strcpy( font->logfont.lfFaceName, alias );
2974 strcpy( font->logfont.lfFaceName, faceMatched );
2977 hPrevFont = dc->w.hFont;
2978 dc->w.hFont = hfont;
2980 LeaveCriticalSection( &crtsc_fonts_X11 );
2986 /***********************************************************************
2988 * X11DRV_EnumDeviceFonts
2990 BOOL X11DRV_EnumDeviceFonts( DC* dc, LPLOGFONT16 plf,
2991 DEVICEFONTENUMPROC proc, LPARAM lp )
2995 fontResource* pfr = fontList;
2998 if( plf->lfFaceName[0] )
3000 /* enum all entries in this resource */
3001 pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
3005 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3007 /* Note: XFONT_GetFontMetric() will have to
3008 release the crit section, font list will
3009 have to be retraversed on return */
3011 if( (b = (*proc)( &lf, &tm,
3012 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3018 else /* enum first entry in each resource */
3019 for( ; pfr ; pfr = pfr->next )
3023 if( (b = (*proc)( &lf, &tm,
3024 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3033 /***********************************************************************
3034 * X11DRV_GetTextExtentPoint
3036 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCSTR str, INT count,
3039 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3040 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3042 if( !pfo->lpX11Trans ) {
3043 int dir, ascent, descent;
3046 TSXTextExtents( pfo->fs, str, count, &dir, &ascent, &descent, &info );
3047 size->cx = abs((info.width + dc->w.breakRem + count *
3048 dc->w.charExtra) * dc->wndExtX / dc->vportExtX);
3049 size->cy = abs((pfo->fs->ascent + pfo->fs->descent) *
3050 dc->wndExtY / dc->vportExtY);
3054 float x = 0.0, y = 0.0;
3055 for(i = 0; i < count; i++) {
3056 x += pfo->fs->per_char ?
3057 pfo->fs->per_char[str[i] - pfo->fs->min_char_or_byte2].attributes :
3058 pfo->fs->min_bounds.attributes;
3060 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
3061 TRACE("x = %f y = %f\n", x, y);
3062 x *= pfo->lpX11Trans->pixelsize / 1000.0;
3063 y *= pfo->lpX11Trans->pixelsize / 1000.0;
3064 size->cx = fabs((x + dc->w.breakRem + count * dc->w.charExtra) *
3065 dc->wndExtX / dc->vportExtX);
3066 size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
3068 size->cx *= pfo->rescale;
3069 size->cy *= pfo->rescale;
3076 /***********************************************************************
3077 * X11DRV_GetTextMetrics
3079 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
3081 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3083 if( CHECK_PFONT(physDev->font) )
3085 fontObject* pfo = __PFONT(physDev->font);
3086 XFONT_GetTextMetrics( pfo, metrics );
3094 /***********************************************************************
3095 * X11DRV_GetCharWidth
3097 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3100 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3101 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3107 if (pfo->fs->per_char == NULL)
3108 for (i = firstChar; i <= lastChar; i++)
3110 *buffer++ = pfo->fs->min_bounds.attributes *
3111 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3113 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3116 XCharStruct *cs, *def;
3117 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3119 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3122 for (i = firstChar; i <= lastChar; i++)
3124 if (i >= pfo->fs->min_char_or_byte2 &&
3125 i <= pfo->fs->max_char_or_byte2)
3127 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3128 if (CI_NONEXISTCHAR(cs)) cs = def;
3131 *buffer++ = MAX(cs->attributes, 0) *
3132 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3134 *buffer++ = MAX(cs->width, 0 ) * pfo->rescale;
3143 #endif /* !defined(X_DISPLAY_MISSING) */
3145 /***********************************************************************
3147 * Font Resource API *
3149 ***********************************************************************/
3150 /***********************************************************************
3151 * AddFontResource16 (GDI.119)
3153 * Can be either .FON, or .FNT, or .TTF, or .FOT font file.
3155 * FIXME: Load header and find the best-matching font in the fontList;
3156 * fixup dfPoints if all metrics are identical, otherwise create
3157 * new fontAlias. When soft font support is ready this will
3158 * simply create a new fontResource ('filename' will go into
3159 * the pfr->resource field) with FR_SOFTFONT/FR_SOFTRESOURCE
3162 INT16 WINAPI AddFontResource16( LPCSTR filename )
3164 return AddFontResourceA( filename );
3168 /***********************************************************************
3169 * AddFontResource32A (GDI32.2)
3171 INT WINAPI AddFontResourceA( LPCSTR str )
3173 FIXME("(%s): stub\n", debugres_a(str));
3178 /***********************************************************************
3179 * AddFontResource32W (GDI32.4)
3181 INT WINAPI AddFontResourceW( LPCWSTR str )
3183 FIXME("(%s): stub\n", debugres_w(str) );
3187 /***********************************************************************
3188 * RemoveFontResource16 (GDI.136)
3190 BOOL16 WINAPI RemoveFontResource16( SEGPTR str )
3192 FIXME("(%s): stub\n", debugres_a(PTR_SEG_TO_LIN(str)));
3197 /***********************************************************************
3198 * RemoveFontResource32A (GDI32.284)
3200 BOOL WINAPI RemoveFontResourceA( LPCSTR str )
3202 /* This is how it should look like */
3204 fontResource** ppfr;
3205 BOOL32 retVal = FALSE;
3207 EnterCriticalSection( &crtsc_fonts_X11 );
3208 for( ppfr = &fontList; *ppfr; ppfr = &(*ppfr)->next )
3209 if( !strcasecmp( (*ppfr)->lfFaceName, str ) )
3211 if(((*ppfr)->fr_flags & (FR_SOFTFONT | FR_SOFTRESOURCE)) &&
3212 (*ppfr)->hOwnerProcess == GetCurrentProcess() )
3214 if( (*ppfr)->fo_count )
3215 (*ppfr)->fr_flags |= FR_REMOVED;
3217 XFONT_RemoveFontResource( ppfr );
3221 LeaveCriticalSection( &crtsc_fonts_X11 );
3224 FIXME("(%s): stub\n", debugres_a(str));
3229 /***********************************************************************
3230 * RemoveFontResource32W (GDI32.286)
3232 BOOL WINAPI RemoveFontResourceW( LPCWSTR str )
3234 FIXME("(%s): stub\n", debugres_w(str) );