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>
34 #include "debugtools.h"
38 DEFAULT_DEBUG_CHANNEL(font
)
40 #ifndef X_DISPLAY_MISSING
42 #define X_PFONT_MAGIC (0xFADE0000)
43 #define X_FMC_MAGIC (0x0000CAFE)
45 #define MAX_FONTS 1024*16
46 #define MAX_LFD_LENGTH 256
50 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
51 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
52 #define MIN_FONT_SIZE 2 /* Min size in pixels */
53 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
55 #define REMOVE_SUBSETS 1
56 #define UNMARK_SUBSETS 0
59 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
61 typedef struct __fontAlias
65 struct __fontAlias
* next
;
68 static fontAlias
*aliasTable
= NULL
;
70 UINT16 XTextCaps
= TC_OP_CHARACTER
| TC_OP_STROKE
|
71 TC_CP_STROKE
| TC_CR_ANY
|
72 TC_SA_DOUBLE
| TC_SA_INTEGER
| TC_SA_CONTIN
|
73 TC_UA_ABLE
| TC_SO_ABLE
| TC_RA_ABLE
;
75 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
77 static const char* INIWinePrefix
= "/.wine";
78 static const char* INIFontMetrics
= "/cachedmetrics.";
79 static const char* INIFontSection
= "fonts";
80 static const char* INIAliasSection
= "Alias";
81 static const char* INIIgnoreSection
= "Ignore";
82 static const char* INIDefault
= "Default";
83 static const char* INIDefaultFixed
= "DefaultFixed";
84 static const char* INIResolution
= "Resolution";
85 static const char* INIGlobalMetrics
= "FontMetrics";
86 static const char* INIDefaultSerif
= "DefaultSerif";
87 static const char* INIDefaultSansSerif
= "DefaultSansSerif";
90 /* FIXME - are there any more Latin charsets ? */
91 #define IS_LATIN_CHARSET(ch) \
92 ((ch)==ANSI_CHARSET ||\
94 (ch)==ISO3_CHARSET ||\
95 (ch)==ISO4_CHARSET ||\
96 (ch)==RUSSIAN_CHARSET ||\
97 (ch)==ARABIC_CHARSET ||\
98 (ch)==GREEK_CHARSET ||\
99 (ch)==HEBREW_CHARSET ||\
100 (ch)==TURKISH_CHARSET ||\
101 (ch)==BALTIC_CHARSET)
103 /* suffix-charset mapping tables - must be less than 254 entries long */
105 typedef struct __sufch
111 static SuffixCharset sufch_ansi
[] = {
112 { "0", ANSI_CHARSET
},
113 { NULL
, ANSI_CHARSET
}};
115 static SuffixCharset sufch_iso646
[] = {
116 { "irv", ANSI_CHARSET
},
117 { NULL
, SYMBOL_CHARSET
}};
119 static SuffixCharset sufch_iso8859
[] = {
120 { "1", ANSI_CHARSET
},
122 { "3", ISO3_CHARSET
},
123 { "4", ISO4_CHARSET
},
124 { "5", RUSSIAN_CHARSET
},
125 { "6", ARABIC_CHARSET
},
126 { "7", GREEK_CHARSET
},
127 { "8", HEBREW_CHARSET
},
128 { "9", TURKISH_CHARSET
},
129 { "10", BALTIC_CHARSET
},
130 { "11", THAI_CHARSET
},
131 { "12", SYMBOL_CHARSET
},
132 { "13", SYMBOL_CHARSET
},
133 { "14", SYMBOL_CHARSET
},
134 { "15", ANSI_CHARSET
},
135 { NULL
, SYMBOL_CHARSET
}};
137 static SuffixCharset sufch_microsoft
[] = {
138 { "cp1250", EE_CHARSET
},
139 { "cp1251", RUSSIAN_CHARSET
},
140 { "cp1252", ANSI_CHARSET
},
141 { "cp1253", GREEK_CHARSET
},
142 { "cp1254", TURKISH_CHARSET
},
143 { "cp1255", HEBREW_CHARSET
},
144 { "cp1256", ARABIC_CHARSET
},
145 { "cp1257", BALTIC_CHARSET
},
146 { "fontspecific", SYMBOL_CHARSET
},
147 { "symbol", SYMBOL_CHARSET
},
148 { NULL
, SYMBOL_CHARSET
}};
150 static SuffixCharset sufch_tcvn
[] = {
151 { "0", TCVN_CHARSET
},
152 { NULL
, TCVN_CHARSET
}};
154 static SuffixCharset sufch_tis620
[] = {
155 { "0", THAI_CHARSET
},
156 { NULL
, THAI_CHARSET
}};
158 static SuffixCharset sufch_viscii
[] = {
159 { "1", VISCII_CHARSET
},
160 { NULL
, VISCII_CHARSET
}};
162 static SuffixCharset sufch_windows
[] = {
163 { "1250", EE_CHARSET
},
164 { "1251", RUSSIAN_CHARSET
},
165 { "1252", ANSI_CHARSET
},
166 { "1253", GREEK_CHARSET
},
167 { "1254", TURKISH_CHARSET
},
168 { "1255", HEBREW_CHARSET
},
169 { "1256", ARABIC_CHARSET
},
170 { "1257", BALTIC_CHARSET
},
171 { NULL
, BALTIC_CHARSET
}}; /* CHECK/FIXME is BALTIC really the default ? */
173 static SuffixCharset sufch_koi8
[] = {
174 { "r", RUSSIAN_CHARSET
},
175 { NULL
, RUSSIAN_CHARSET
}};
177 /* Each of these must be matched explicitly */
178 static SuffixCharset sufch_any
[] = {
179 { "fontspecific", SYMBOL_CHARSET
},
186 SuffixCharset
* sufch
;
188 } fontEncodingTemplate
;
190 /* Note: we can attach additional encoding mappings to the end
191 * of this table at runtime.
193 static fontEncodingTemplate __fETTable
[] = {
194 { "ansi", sufch_ansi
, &__fETTable
[1] },
195 { "ascii", sufch_ansi
, &__fETTable
[2] },
196 { "iso646.1991", sufch_iso646
, &__fETTable
[3] },
197 { "iso8859", sufch_iso8859
, &__fETTable
[4] },
198 { "microsoft", sufch_microsoft
, &__fETTable
[5] },
199 { "tcvn", sufch_tcvn
, &__fETTable
[6] },
200 { "tis620.2533", sufch_tis620
, &__fETTable
[7] },
201 { "viscii1.1", sufch_viscii
, &__fETTable
[8] },
202 { "windows", sufch_windows
, &__fETTable
[9] },
203 { "koi8", sufch_koi8
, &__fETTable
[10] },
204 /* NULL prefix matches anything so put it last */
205 { NULL
, sufch_any
, NULL
},
207 static fontEncodingTemplate
* fETTable
= __fETTable
;
209 static int DefResolution
= 0;
211 static CRITICAL_SECTION crtsc_fonts_X11
;
213 static fontResource
* fontList
= NULL
;
214 static fontObject
* fontCache
= NULL
; /* array */
215 static int fontCacheSize
= FONTCACHE
;
216 static int fontLF
= -1, fontMRU
= -1; /* last free, most recently used */
218 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
219 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
220 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
222 static Atom RAW_ASCENT
;
223 static Atom RAW_DESCENT
;
225 /***********************************************************************
226 * Helper macros from X distribution
229 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
230 (((cs)->rbearing|(cs)->lbearing| \
231 (cs)->ascent|(cs)->descent) == 0))
233 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
236 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
237 if (fs->per_char == NULL) { \
238 cs = &fs->min_bounds; \
240 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
241 if (CI_NONEXISTCHAR(cs)) cs = def; \
246 #define CI_GET_DEFAULT_INFO(fs,cs) \
247 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
249 /***********************************************************************
252 static UINT16
__lfCheckSum( LPLOGFONT16 plf
)
254 CHAR font
[LF_FACESIZE
];
258 #define ptr ((UINT16*)plf)
259 for( i
= 0; i
< 9; i
++ ) checksum
^= *ptr
++;
262 #define ptr ((CHAR*)plf)
263 do { font
[i
++] = tolower(*ptr
++); } while (( i
< LF_FACESIZE
) && (*ptr
) && (*ptr
!=' '));
264 for( ptr
= font
, i
>>= 1; i
> 0; i
-- )
266 #define ptr ((UINT16*)plf)
272 static UINT16
__genericCheckSum( const void *ptr
, int size
)
274 unsigned int checksum
= 0;
275 const char *p
= (const char *)ptr
;
277 checksum
^= (checksum
<< 3) + (checksum
>> 29) + *p
++;
279 return checksum
& 0xffff;
282 /*************************************************************************
283 * LFD parse/compose routines
285 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
288 * These functions also do TILDE to HYPHEN conversion
290 static LFD
* LFD_Parse(LPSTR lpFont
)
293 char *lpch
= lpFont
, *lfd_fld
[LFD_FIELDS
], *field_start
;
297 WARN("font '%s' doesn't begin with '%c'\n", lpFont
, HYPHEN
);
301 field_start
= ++lpch
;
302 for( i
= 0; i
< LFD_FIELDS
; )
307 lfd_fld
[i
] = field_start
;
309 field_start
= ++lpch
;
313 lfd_fld
[i
] = field_start
;
317 else if (*lpch
== TILDE
)
325 /* Fill in the empty fields with NULLS */
326 for (; i
< LFD_FIELDS
; i
++)
329 WARN("Extra ignored in font '%s'\n", lpFont
);
331 lfd
= HeapAlloc( GetProcessHeap(), 0, sizeof(LFD
) );
334 lfd
->foundry
= lfd_fld
[0];
335 lfd
->family
= lfd_fld
[1];
336 lfd
->weight
= lfd_fld
[2];
337 lfd
->slant
= lfd_fld
[3];
338 lfd
->set_width
= lfd_fld
[4];
339 lfd
->add_style
= lfd_fld
[5];
340 lfd
->pixel_size
= lfd_fld
[6];
341 lfd
->point_size
= lfd_fld
[7];
342 lfd
->resolution_x
= lfd_fld
[8];
343 lfd
->resolution_y
= lfd_fld
[9];
344 lfd
->spacing
= lfd_fld
[10];
345 lfd
->average_width
= lfd_fld
[11];
346 lfd
->charset_registry
= lfd_fld
[12];
347 lfd
->charset_encoding
= lfd_fld
[13];
353 static void LFD_UnParse(LPSTR dp
, UINT buf_size
, LFD
* lfd
)
355 char* lfd_fld
[LFD_FIELDS
];
359 return; /* Dont be silly */
361 lfd_fld
[0] = lfd
->foundry
;
362 lfd_fld
[1] = lfd
->family
;
363 lfd_fld
[2] = lfd
->weight
;
364 lfd_fld
[3] = lfd
->slant
;
365 lfd_fld
[4] = lfd
->set_width
;
366 lfd_fld
[5] = lfd
->add_style
;
367 lfd_fld
[6] = lfd
->pixel_size
;
368 lfd_fld
[7] = lfd
->point_size
;
369 lfd_fld
[8] = lfd
->resolution_x
;
370 lfd_fld
[9] = lfd
->resolution_y
;
371 lfd_fld
[10] = lfd
->spacing
;
372 lfd_fld
[11] = lfd
->average_width
;
373 lfd_fld
[12] = lfd
->charset_registry
;
374 lfd_fld
[13] = lfd
->charset_encoding
;
376 buf_size
--; /* Room for the terminator */
378 for (i
= 0; i
< LFD_FIELDS
; i
++)
380 char* sp
= lfd_fld
[i
];
381 if (!sp
|| !buf_size
)
386 while (buf_size
> 0 && *sp
)
388 *dp
= (*sp
== HYPHEN
) ? TILDE
: *sp
;
397 static void LFD_GetWeight( fontInfo
* fi
, LPCSTR lpStr
)
399 int j
= lstrlenA(lpStr
);
400 if( j
== 1 && *lpStr
== '0')
401 fi
->fi_flags
|= FI_POLYWEIGHT
;
404 if( !strcasecmp( "bold", lpStr
) )
405 fi
->df
.dfWeight
= FW_BOLD
;
406 else if( !strcasecmp( "demi", lpStr
) )
408 fi
->fi_flags
|= FI_FW_DEMI
;
409 fi
->df
.dfWeight
= FW_DEMIBOLD
;
411 else if( !strcasecmp( "book", lpStr
) )
413 fi
->fi_flags
|= FI_FW_BOOK
;
414 fi
->df
.dfWeight
= FW_REGULAR
;
419 if( !strcasecmp( "light", lpStr
) )
420 fi
->df
.dfWeight
= FW_LIGHT
;
421 else if( !strcasecmp( "black", lpStr
) )
422 fi
->df
.dfWeight
= FW_BLACK
;
424 else if( j
== 6 && !strcasecmp( "medium", lpStr
) )
425 fi
->df
.dfWeight
= FW_REGULAR
;
426 else if( j
== 8 && !strcasecmp( "demibold", lpStr
) )
427 fi
->df
.dfWeight
= FW_DEMIBOLD
;
429 fi
->df
.dfWeight
= FW_DONTCARE
; /* FIXME: try to get something
430 * from the weight property */
433 static BOOL
LFD_GetSlant( fontInfo
* fi
, LPCSTR lpStr
)
435 int l
= lstrlenA(lpStr
);
438 switch( tolower( *lpStr
) )
440 case '0': fi
->fi_flags
|= FI_POLYSLANT
; /* haven't seen this one yet */
442 case 'r': fi
->df
.dfItalic
= 0;
445 fi
->fi_flags
|= FI_OBLIQUE
;
446 case 'i': fi
->df
.dfItalic
= 1;
454 static void LFD_GetStyle( fontInfo
* fi
, LPCSTR lpstr
, int dec_style_check
)
456 int j
= lstrlenA(lpstr
);
457 if( j
> 3 ) /* find out is there "sans" or "script" */
461 if( strstr(lpstr
, "sans") )
463 fi
->df
.dfPitchAndFamily
|= FF_SWISS
;
466 if( strstr(lpstr
, "script") )
468 fi
->df
.dfPitchAndFamily
|= FF_SCRIPT
;
471 if( !j
&& dec_style_check
)
472 fi
->df
.dfPitchAndFamily
|= FF_DECORATIVE
;
476 /*************************************************************************
481 * Fill in some fields in the fontInfo struct.
483 static int LFD_InitFontInfo( fontInfo
* fi
, const LFD
* lfd
, LPCSTR fullname
)
485 int i
, j
, dec_style_check
, scalability
;
486 fontEncodingTemplate
* boba
;
487 const char* ridiculous
= "font '%s' has ridiculous %s\n";
490 if (!lfd
->charset_registry
)
492 WARN("font '%s' does not have enough fields\n", fullname
);
496 memset(fi
, 0, sizeof(fontInfo
) );
499 LFD_GetWeight( fi
, lfd
->weight
);
502 dec_style_check
= LFD_GetSlant( fi
, lfd
->slant
);
505 lpstr
= lfd
->set_width
;
506 if( strcasecmp( "normal", lpstr
) ) /* XXX 'narrow', 'condensed', etc... */
507 dec_style_check
= TRUE
;
509 fi
->fi_flags
|= FI_NORMAL
;
512 LFD_GetStyle(fi
, lfd
->add_style
, dec_style_check
);
514 /* pixel & decipoint height, and res_x & y */
518 j
= strlen(lfd
->pixel_size
);
519 if( j
== 0 || j
> 3 )
521 WARN(ridiculous
, fullname
, "pixel_size");
524 if( !(fi
->lfd_height
= atoi(lfd
->pixel_size
)) )
527 j
= strlen(lfd
->point_size
);
528 if( j
== 0 || j
> 3 )
530 WARN(ridiculous
, fullname
, "point_size");
533 if( !(atoi(lfd
->point_size
)) )
536 j
= strlen(lfd
->resolution_x
);
537 if( j
== 0 || j
> 3 )
539 WARN(ridiculous
, fullname
, "resolution_x");
542 if( !(fi
->lfd_resolution
= atoi(lfd
->resolution_x
)) )
545 j
= strlen(lfd
->resolution_y
);
546 if( j
== 0 || j
> 3 )
548 WARN(ridiculous
, fullname
, "resolution_y");
551 if( !(atoi(lfd
->resolution_y
)) )
554 /* Check scalability */
559 case 4: /* Scalable */
560 fi
->fi_flags
|= FI_SCALABLE
;
563 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
564 TRACE("Skipping scalable bitmap '%s'\n", fullname
);
567 WARN("Font '%s' has weird scalability\n", fullname
);
572 lpstr
= lfd
->spacing
;
576 WARN("font '%s' has no spacing\n", fullname
);
579 case 'p': fi
->fi_flags
|= FI_VARIABLEPITCH
;
581 case 'c': fi
->df
.dfPitchAndFamily
|= FF_MODERN
;
582 fi
->fi_flags
|= FI_FIXEDEX
;
584 case 'm': fi
->fi_flags
|= FI_FIXEDPITCH
;
587 /* Of course this line does nothing */
588 fi
->df
.dfPitchAndFamily
|= DEFAULT_PITCH
| FF_DONTCARE
;
591 /* average width - */
592 lpstr
= lfd
->average_width
;
594 if( j
== 0 || j
> 3 )
596 WARN(ridiculous
, fullname
, "average_width");
600 if( !(atoi(lpstr
)) && !scalability
)
602 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname
);
606 /* charset registry, charset encoding - */
607 lpstr
= lfd
->charset_registry
;
608 if( strstr(lpstr
, "jisx") ||
609 strstr(lpstr
, "ksc") ||
610 strstr(lpstr
, "gb2312") ||
611 strstr(lpstr
, "big5") ||
612 strstr(lpstr
, "unicode") )
614 TRACE(" 2-byte fonts like '%s' are not supported\n", fullname
);
618 fi
->df
.dfCharSet
= ANSI_CHARSET
;
620 for( i
= 0, boba
= fETTable
; boba
; boba
= boba
->next
, i
++ )
622 if (!boba
->prefix
|| !strcasecmp(lpstr
, boba
->prefix
))
624 if (lfd
->charset_encoding
)
626 for( j
= 0; boba
->sufch
[j
].psuffix
; j
++ )
628 if( !strcasecmp(lfd
->charset_encoding
, boba
->sufch
[j
].psuffix
))
630 fi
->df
.dfCharSet
= boba
->sufch
[j
].charset
;
636 WARN("font '%s' has unknown character encoding '%s'\n",
637 fullname
, lfd
->charset_encoding
);
638 fi
->df
.dfCharSet
= boba
->sufch
[j
].charset
;
643 else if (boba
->prefix
)
645 for( j
= 0; boba
->sufch
[j
].psuffix
; j
++ )
647 fi
->df
.dfCharSet
= boba
->sufch
[j
].charset
;
653 WARN("font '%s' has unknown character set '%s'\n", fullname
, lpstr
);
657 /* i - index into fETTable
658 * j - index into suffix array for fETTable[i]
660 * 254 - unknown suffix
661 * 255 - no suffix at all.
663 fi
->fi_encoding
= 256 * (UINT16
)i
+ (UINT16
)j
;
669 /*************************************************************************
672 * make a matrix suitable for LFD based on theta radians
674 static void LFD_AngleMatrix( char* buffer
, int h
, double theta
)
677 matrix
[0] = h
*cos(theta
);
678 matrix
[1] = h
*sin(theta
);
679 matrix
[2] = -h
*sin(theta
);
680 matrix
[3] = h
*cos(theta
);
681 sprintf(buffer
, "[%+f%+f%+f%+f]", matrix
[0], matrix
[1], matrix
[2], matrix
[3]);
684 /*************************************************************************
687 * Note: uRelax is a treatment not a cure. Font mapping algorithm
688 * should be bulletproof enough to allow us to avoid hacks like
689 * this despite LFD being so braindead.
691 static BOOL
LFD_ComposeLFD( const fontObject
* fo
,
692 INT height
, LPSTR lpLFD
, UINT uRelax
)
696 char h_string
[64], resx_string
[64], resy_string
[64];
699 /* Get the worst case over with first */
701 /* RealizeFont() will call us repeatedly with increasing uRelax
702 * until XLoadFont() succeeds.
703 * to avoid an infinite loop; these will always match
708 sprintf( lpLFD
, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
710 sprintf( lpLFD
, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
714 /* read foundry + family from fo */
715 aLFD
.foundry
= fo
->fr
->resource
->foundry
;
716 aLFD
.family
= fo
->fr
->resource
->family
;
719 switch( fo
->fi
->df
.dfWeight
)
722 aLFD
.weight
= "bold"; break;
724 if( fo
->fi
->fi_flags
& FI_FW_BOOK
)
725 aLFD
.weight
= "book";
727 aLFD
.weight
= "medium";
730 aLFD
.weight
= "demi";
731 if( !( fo
->fi
->fi_flags
& FI_FW_DEMI
) )
732 aLFD
.weight
= "bold";
735 aLFD
.weight
= "black"; break;
737 aLFD
.weight
= "light"; break;
743 if( fo
->fi
->df
.dfItalic
)
744 if( fo
->fi
->fi_flags
& FI_OBLIQUE
)
749 aLFD
.slant
= (uRelax
< 1) ? "r" : any
;
752 if( fo
->fi
->fi_flags
& FI_NORMAL
)
753 aLFD
.set_width
= "normal";
755 aLFD
.set_width
= any
;
758 aLFD
.add_style
= any
;
762 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
764 if( fo
->fo_flags
& FO_SYNTH_HEIGHT
)
765 h
= fo
->fi
->lfd_height
;
768 h
= (fo
->fi
->lfd_height
* height
+ (fo
->fi
->df
.dfPixHeight
>>1));
769 h
/= fo
->fi
->df
.dfPixHeight
;
771 if (h
< MIN_FONT_SIZE
) /* Resist rounding down to 0 */
773 else if (h
> MAX_FONT_SIZE
) /* Sanity check as huge fonts can lock up the font server */
775 WARN("Huge font size %d pixels has been reduced to %d\n", h
, MAX_FONT_SIZE
);
780 /* handle rotated fonts */
781 if (fo
->lf
.lfEscapement
) {
782 /* escapement is in tenths of degrees, theta is in radians */
783 double theta
= M_PI
*fo
->lf
.lfEscapement
/1800.;
784 LFD_AngleMatrix(h_string
, h
, theta
);
788 sprintf(h_string
, "%d", h
);
791 sprintf(h_string
, "%d", fo
->fi
->lfd_height
);
793 aLFD
.pixel_size
= h_string
;
794 aLFD
.point_size
= any
;
796 /* resolution_x,y, average width */
797 /* FOX ME - Why do some font servers ignore average width ?
798 * so that you have to mess around with res_y
800 aLFD
.average_width
= any
;
803 sprintf(resx_string
, "%d", fo
->fi
->lfd_resolution
);
804 aLFD
.resolution_x
= resx_string
;
806 strcpy(resy_string
, resx_string
);
807 if( uRelax
== 0 && XTextCaps
& TC_SF_X_YINDEP
)
809 if( fo
->lf
.lfWidth
&& !(fo
->fo_flags
& FO_SYNTH_WIDTH
))
811 int resy
= 0.5 + fo
->fi
->lfd_resolution
*
812 (fo
->fi
->df
.dfAvgWidth
* height
) /
813 (fo
->lf
.lfWidth
* fo
->fi
->df
.dfPixHeight
) ;
814 sprintf(resy_string
, "%d", resy
);
817 ; /* FIXME - synth width */
819 aLFD
.resolution_y
= resy_string
;
823 aLFD
.resolution_x
= aLFD
.resolution_y
= any
;
830 if( fo
->fi
->fi_flags
& FI_FIXEDPITCH
)
831 w
= ( fo
->fi
->fi_flags
& FI_FIXEDEX
) ? "c" : "m";
833 w
= ( fo
->fi
->fi_flags
& FI_VARIABLEPITCH
) ? "p" : any
;
835 aLFD
.spacing
= (uRelax
<= 1) ? w
: any
;
842 fontEncodingTemplate
* boba
;
844 i
= fo
->fi
->fi_encoding
>> 8;
845 for( boba
= fETTable
; i
; i
--, boba
= boba
->next
);
847 aLFD
.charset_registry
= boba
->prefix
? boba
->prefix
: any
;
849 i
= fo
->fi
->fi_encoding
& 255;
853 aLFD
.charset_encoding
= boba
->sufch
[i
].psuffix
;
857 aLFD
.charset_encoding
= any
;
860 case 255: /* no suffix - it ends eg "-ascii" */
861 aLFD
.charset_encoding
= NULL
;
867 aLFD
.charset_registry
= any
;
868 aLFD
.charset_encoding
= any
;
871 LFD_UnParse(lpLFD
, MAX_LFD_LENGTH
, &aLFD
);
873 TRACE("\tLFD(uRelax=%d): %s\n", uRelax
, lpLFD
);
878 /***********************************************************************
881 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
882 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
884 static void XFONT_GetLeading( const LPIFONTINFO16 pFI
, const XFontStruct
* x_fs
,
885 INT16
* pIL
, INT16
* pEL
, const XFONTTRANS
*XFT
)
887 unsigned long height
;
888 unsigned min
= (unsigned char)pFI
->dfFirstChar
;
889 BOOL bIsLatin
= IS_LATIN_CHARSET(pFI
->dfCharSet
);
894 Atom RAW_CAP_HEIGHT
= TSXInternAtom(display
, "RAW_CAP_HEIGHT", TRUE
);
895 if(TSXGetFontProperty((XFontStruct
*)x_fs
, RAW_CAP_HEIGHT
, &height
))
897 (INT
)(XFT
->pixelsize
/ 1000.0 * height
);
903 if( TSXGetFontProperty((XFontStruct
*)x_fs
, XA_CAP_HEIGHT
, &height
) == FALSE
)
907 height
= x_fs
->per_char
['X' - min
].ascent
;
909 if (x_fs
->ascent
>= x_fs
->max_bounds
.ascent
)
910 height
= x_fs
->max_bounds
.ascent
;
913 height
= x_fs
->ascent
;
915 *pEL
= x_fs
->max_bounds
.ascent
- height
;
918 height
= x_fs
->min_bounds
.ascent
;
921 *pIL
= x_fs
->ascent
- height
;
924 /***********************************************************************
927 static int XFONT_CharWidth(const XFontStruct
* x_fs
,
928 const XFONTTRANS
*XFT
, int ch
)
931 return x_fs
->per_char
[ch
].width
;
933 return x_fs
->per_char
[ch
].attributes
* XFT
->pixelsize
/ 1000.0;
936 /***********************************************************************
937 * XFONT_GetAvgCharWidth
939 static INT
XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI
, const XFontStruct
* x_fs
,
940 const XFONTTRANS
*XFT
)
942 unsigned min
= (unsigned char)pFI
->dfFirstChar
;
943 unsigned max
= (unsigned char)pFI
->dfLastChar
;
949 int width
= 0, chars
= 0, j
;
950 if( IS_LATIN_CHARSET(pFI
->dfCharSet
))
952 /* FIXME - should use a weighted average */
953 for( j
= 0; j
< 26; j
++ )
954 width
+= XFONT_CharWidth(x_fs
, XFT
, 'a' + j
- min
) +
955 XFONT_CharWidth(x_fs
, XFT
, 'A' + j
- min
);
958 else /* unweighted average of everything */
960 for( j
= 0, max
-= min
; j
<= max
; j
++ )
961 if( !CI_NONEXISTCHAR(x_fs
->per_char
+ j
) )
963 width
+= XFONT_CharWidth(x_fs
, XFT
, j
);
967 avg
= (width
+ (chars
>>1))/ chars
;
969 else /* uniform width */
970 avg
= x_fs
->min_bounds
.width
;
975 /***********************************************************************
976 * XFONT_GetMaxCharWidth
978 static INT
XFONT_GetMaxCharWidth(const XFontStruct
* xfs
, const XFONTTRANS
*XFT
)
980 unsigned min
= (unsigned char)xfs
->min_char_or_byte2
;
981 unsigned max
= (unsigned char)xfs
->max_char_or_byte2
;
984 if(!XFT
|| !xfs
->per_char
)
985 return abs(xfs
->max_bounds
.width
);
987 for( j
= 0, maxwidth
= 0, max
-= min
; j
<= max
; j
++ )
988 if( !CI_NONEXISTCHAR(xfs
->per_char
+ j
) )
989 if(maxwidth
< xfs
->per_char
[j
].attributes
)
990 maxwidth
= xfs
->per_char
[j
].attributes
;
992 maxwidth
*= XFT
->pixelsize
/ 1000.0;
996 /***********************************************************************
997 * XFONT_SetFontMetric
1001 * Initializes IFONTINFO16.
1003 static void XFONT_SetFontMetric(fontInfo
* fi
, const fontResource
* fr
, XFontStruct
* xfs
)
1006 fi
->df
.dfFirstChar
= (BYTE
)(min
= xfs
->min_char_or_byte2
);
1007 fi
->df
.dfLastChar
= (BYTE
)(max
= xfs
->max_char_or_byte2
);
1009 fi
->df
.dfDefaultChar
= (BYTE
)xfs
->default_char
;
1010 fi
->df
.dfBreakChar
= (BYTE
)(( ' ' < min
|| ' ' > max
) ? xfs
->default_char
: ' ');
1012 fi
->df
.dfPixHeight
= (INT16
)((fi
->df
.dfAscent
= (INT16
)xfs
->ascent
) + xfs
->descent
);
1013 fi
->df
.dfPixWidth
= (xfs
->per_char
) ? 0 : xfs
->min_bounds
.width
;
1015 XFONT_GetLeading( &fi
->df
, xfs
, &fi
->df
.dfInternalLeading
, &fi
->df
.dfExternalLeading
, NULL
);
1016 fi
->df
.dfAvgWidth
= (INT16
)XFONT_GetAvgCharWidth(&fi
->df
, xfs
, NULL
);
1017 fi
->df
.dfMaxWidth
= (INT16
)XFONT_GetMaxCharWidth(xfs
, NULL
);
1019 if( xfs
->min_bounds
.width
!= xfs
->max_bounds
.width
)
1020 fi
->df
.dfPitchAndFamily
|= TMPF_FIXED_PITCH
; /* au contraire! */
1021 if( fi
->fi_flags
& FI_SCALABLE
)
1023 fi
->df
.dfType
= DEVICE_FONTTYPE
;
1024 fi
->df
.dfPitchAndFamily
|= TMPF_DEVICE
;
1026 else if( fi
->fi_flags
& FI_TRUETYPE
)
1027 fi
->df
.dfType
= TRUETYPE_FONTTYPE
;
1029 fi
->df
.dfType
= RASTER_FONTTYPE
;
1031 fi
->df
.dfFace
= fr
->lfFaceName
;
1034 /***********************************************************************
1035 * XFONT_GetTextMetrics
1037 * GetTextMetrics() back end.
1039 static void XFONT_GetTextMetrics( const fontObject
* pfo
, const LPTEXTMETRICA pTM
)
1041 LPIFONTINFO16 pdf
= &pfo
->fi
->df
;
1043 if( ! pfo
->lpX11Trans
) {
1044 pTM
->tmAscent
= pfo
->fs
->ascent
;
1045 pTM
->tmDescent
= pfo
->fs
->descent
;
1047 pTM
->tmAscent
= pfo
->lpX11Trans
->ascent
;
1048 pTM
->tmDescent
= pfo
->lpX11Trans
->descent
;
1051 pTM
->tmAscent
*= pfo
->rescale
;
1052 pTM
->tmDescent
*= pfo
->rescale
;
1054 pTM
->tmHeight
= pTM
->tmAscent
+ pTM
->tmDescent
;
1056 pTM
->tmAveCharWidth
= pfo
->foAvgCharWidth
* pfo
->rescale
;
1057 pTM
->tmMaxCharWidth
= pfo
->foMaxCharWidth
* pfo
->rescale
;
1059 pTM
->tmInternalLeading
= pfo
->foInternalLeading
* pfo
->rescale
;
1060 pTM
->tmExternalLeading
= pdf
->dfExternalLeading
* pfo
->rescale
;
1062 pTM
->tmStruckOut
= (pfo
->fo_flags
& FO_SYNTH_STRIKEOUT
)
1063 ? 1 : pdf
->dfStrikeOut
;
1064 pTM
->tmUnderlined
= (pfo
->fo_flags
& FO_SYNTH_UNDERLINE
)
1065 ? 1 : pdf
->dfUnderline
;
1067 pTM
->tmOverhang
= 0;
1068 if( pfo
->fo_flags
& FO_SYNTH_ITALIC
)
1070 pTM
->tmOverhang
+= pTM
->tmHeight
/3;
1073 pTM
->tmItalic
= pdf
->dfItalic
;
1075 pTM
->tmWeight
= pdf
->dfWeight
;
1076 if( pfo
->fo_flags
& FO_SYNTH_BOLD
)
1079 pTM
->tmWeight
+= 100;
1082 pTM
->tmFirstChar
= pdf
->dfFirstChar
;
1083 pTM
->tmLastChar
= pdf
->dfLastChar
;
1084 pTM
->tmDefaultChar
= pdf
->dfDefaultChar
;
1085 pTM
->tmBreakChar
= pdf
->dfBreakChar
;
1087 pTM
->tmCharSet
= pdf
->dfCharSet
;
1088 pTM
->tmPitchAndFamily
= pdf
->dfPitchAndFamily
;
1090 pTM
->tmDigitizedAspectX
= pdf
->dfHorizRes
;
1091 pTM
->tmDigitizedAspectY
= pdf
->dfVertRes
;
1094 /***********************************************************************
1095 * XFONT_GetFontMetric
1097 * Retrieve font metric info (enumeration).
1099 static UINT
XFONT_GetFontMetric( const fontInfo
* pfi
, const LPENUMLOGFONTEX16 pLF
,
1100 const LPNEWTEXTMETRIC16 pTM
)
1102 memset( pLF
, 0, sizeof(*pLF
) );
1103 memset( pTM
, 0, sizeof(*pTM
) );
1105 #define plf ((LPLOGFONT16)pLF)
1106 plf
->lfHeight
= pTM
->tmHeight
= pfi
->df
.dfPixHeight
;
1107 plf
->lfWidth
= pTM
->tmAveCharWidth
= pfi
->df
.dfAvgWidth
;
1108 plf
->lfWeight
= pTM
->tmWeight
= pfi
->df
.dfWeight
;
1109 plf
->lfItalic
= pTM
->tmItalic
= pfi
->df
.dfItalic
;
1110 plf
->lfUnderline
= pTM
->tmUnderlined
= pfi
->df
.dfUnderline
;
1111 plf
->lfStrikeOut
= pTM
->tmStruckOut
= pfi
->df
.dfStrikeOut
;
1112 plf
->lfCharSet
= pTM
->tmCharSet
= pfi
->df
.dfCharSet
;
1114 /* convert pitch values */
1116 pTM
->tmPitchAndFamily
= pfi
->df
.dfPitchAndFamily
;
1117 plf
->lfPitchAndFamily
= (pfi
->df
.dfPitchAndFamily
& 0xF1) + 1;
1119 lstrcpynA( plf
->lfFaceName
, pfi
->df
.dfFace
, LF_FACESIZE
);
1122 /* FIXME: fill in rest of plF values
1123 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1124 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1125 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1128 pTM
->tmAscent
= pfi
->df
.dfAscent
;
1129 pTM
->tmDescent
= pTM
->tmHeight
- pTM
->tmAscent
;
1130 pTM
->tmInternalLeading
= pfi
->df
.dfInternalLeading
;
1131 pTM
->tmMaxCharWidth
= pfi
->df
.dfMaxWidth
;
1132 pTM
->tmDigitizedAspectX
= pfi
->df
.dfHorizRes
;
1133 pTM
->tmDigitizedAspectY
= pfi
->df
.dfVertRes
;
1135 pTM
->tmFirstChar
= pfi
->df
.dfFirstChar
;
1136 pTM
->tmLastChar
= pfi
->df
.dfLastChar
;
1137 pTM
->tmDefaultChar
= pfi
->df
.dfDefaultChar
;
1138 pTM
->tmBreakChar
= pfi
->df
.dfBreakChar
;
1140 /* return font type */
1142 return pfi
->df
.dfType
;
1146 /***********************************************************************
1151 * dfPitchAndFamily flags for some common typefaces.
1153 static BYTE
XFONT_FixupFlags( LPCSTR lfFaceName
)
1155 switch( lfFaceName
[0] )
1158 case 'A': if(!strncasecmp(lfFaceName
, "Arial", 5) )
1162 case 'H': if(!strcasecmp(lfFaceName
, "Helvetica") )
1166 case 'C': if(!strncasecmp(lfFaceName
, "Courier", 7))
1169 if (!strcasecmp(lfFaceName
, "Charter") )
1173 case 'P': if( !strcasecmp(lfFaceName
,"Palatino") )
1177 case 'T': if(!strncasecmp(lfFaceName
, "Times", 5) )
1181 case 'U': if(!strcasecmp(lfFaceName
, "Utopia") )
1185 case 'Z': if(!strcasecmp(lfFaceName
, "Zapf Dingbats") )
1186 return FF_DECORATIVE
;
1191 /***********************************************************************
1192 * XFONT_SameFoundryAndFamily
1196 static BOOL
XFONT_SameFoundryAndFamily( const LFD
* lfd1
, const LFD
* lfd2
)
1198 return ( !strcasecmp( lfd1
->foundry
, lfd2
->foundry
) &&
1199 !strcasecmp( lfd1
->family
, lfd2
->family
) );
1202 /***********************************************************************
1203 * XFONT_InitialCapitals
1207 * Upper case first letters of words & remove multiple spaces
1209 static void XFONT_InitialCapitals(LPSTR lpch
)
1215 for( i
= 0, up
= TRUE
; *lpch
; lpch
++, i
++ )
1217 if( isspace(*lpch
) )
1219 if (!up
) /* Not already got one */
1225 else if( isalpha(*lpch
) && up
)
1227 *lpstr
++ = toupper(*lpch
);
1237 /* Remove possible trailing space */
1244 /***********************************************************************
1245 * XFONT_WindowsNames
1249 * Build generic Windows aliases for X font names.
1251 * -misc-fixed- -> "Fixed"
1252 * -sony-fixed- -> "Sony Fixed", etc...
1254 static void XFONT_WindowsNames(void)
1258 for( fr
= fontList
; fr
; fr
= fr
->next
)
1263 if( fr
->fr_flags
& FR_NAMESET
) continue; /* skip already assigned */
1265 for( pfr
= fontList
; pfr
!= fr
; pfr
= pfr
->next
)
1266 if( pfr
->fr_flags
& FR_NAMESET
)
1268 if (!strcasecmp( pfr
->resource
->family
, fr
->resource
->family
))
1272 lpch
= fr
->lfFaceName
;
1273 wsnprintfA( fr
->lfFaceName
, sizeof(fr
->lfFaceName
), "%s %s",
1274 /* prepend vendor name */
1275 (pfr
==fr
) ? "" : fr
->resource
->foundry
,
1276 fr
->resource
->family
);
1277 XFONT_InitialCapitals(fr
->lfFaceName
);
1279 BYTE bFamilyStyle
= XFONT_FixupFlags( fr
->lfFaceName
);
1283 for( fi
= fr
->fi
; fi
; fi
= fi
->next
)
1284 fi
->df
.dfPitchAndFamily
|= bFamilyStyle
;
1288 TRACE("typeface '%s'\n", fr
->lfFaceName
);
1290 fr
->fr_flags
|= FR_NAMESET
;
1294 /***********************************************************************
1295 * XFONT_LoadDefaultLFD
1297 * Move lfd to the head of fontList to make it more likely to be matched
1299 static void XFONT_LoadDefaultLFD(LFD
* lfd
, LPCSTR fonttype
)
1302 fontResource
*fr
, *pfr
;
1303 for( fr
= NULL
, pfr
= fontList
; pfr
; pfr
= pfr
->next
)
1305 if( XFONT_SameFoundryAndFamily(pfr
->resource
, lfd
) )
1309 fr
->next
= pfr
->next
;
1310 pfr
->next
= fontList
;
1318 WARN("Default %sfont '-%s-%s-' not available\n", fonttype
,
1319 lfd
->foundry
, lfd
->family
);
1323 /***********************************************************************
1326 static void XFONT_LoadDefault(LPCSTR ini
, LPCSTR fonttype
)
1328 char buffer
[MAX_LFD_LENGTH
];
1330 if( PROFILE_GetWineIniString( INIFontSection
, ini
, "", buffer
, sizeof buffer
) )
1336 while( *pch
&& isspace(*pch
) ) pch
++;
1338 TRACE("Using '%s' as default %sfont\n", pch
, fonttype
);
1339 lfd
= LFD_Parse(pch
);
1340 if (lfd
&& lfd
->foundry
&& lfd
->family
)
1341 XFONT_LoadDefaultLFD(lfd
, fonttype
);
1343 WARN("Ini section [%s]%s is malformed\n", INIFontSection
, ini
);
1344 HeapFree(GetProcessHeap(), 0, lfd
);
1349 /***********************************************************************
1350 * XFONT_LoadDefaults
1352 static void XFONT_LoadDefaults(void)
1354 XFONT_LoadDefault(INIDefaultFixed
, "fixed ");
1355 XFONT_LoadDefault(INIDefault
, "");
1358 /***********************************************************************
1361 static fontAlias
* XFONT_CreateAlias( LPCSTR lpTypeFace
, LPCSTR lpAlias
)
1364 fontAlias
*pfa
, *prev
= NULL
;
1366 for(pfa
= aliasTable
; pfa
; pfa
= pfa
->next
)
1368 /* check if we already got one */
1369 if( !strcasecmp( pfa
->faTypeFace
, lpAlias
) )
1371 TRACE("redundant alias '%s' -> '%s'\n",
1372 lpAlias
, lpTypeFace
);
1378 j
= lstrlenA(lpTypeFace
) + 1;
1379 pfa
= HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias
) +
1380 j
+ lstrlenA(lpAlias
) + 1 );
1389 pfa
->faTypeFace
= (LPSTR
)(pfa
+ 1);
1390 lstrcpyA( pfa
->faTypeFace
, lpTypeFace
);
1391 pfa
->faAlias
= pfa
->faTypeFace
+ j
;
1392 lstrcpyA( pfa
->faAlias
, lpAlias
);
1394 TRACE("added alias '%s' for %s\n", lpAlias
, lpTypeFace
);
1402 /***********************************************************************
1405 static void XFONT_LoadAlias(const LFD
* lfd
, LPCSTR lpAlias
, BOOL bSubst
)
1407 fontResource
*fr
, *frMatch
= NULL
;
1408 if (!lfd
->foundry
|| ! lfd
->family
)
1410 WARN("Malformed font resource for alias '%s'\n", lpAlias
);
1413 for (fr
= fontList
; fr
; fr
= fr
->next
)
1415 if(!strcasecmp(fr
->resource
->family
, lpAlias
))
1417 /* alias is not needed since the real font is present */
1418 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias
);
1421 if( XFONT_SameFoundryAndFamily( fr
->resource
, lfd
) )
1432 fontAlias
*pfa
, *prev
= NULL
;
1434 for(pfa
= aliasTable
; pfa
; pfa
= pfa
->next
)
1436 /* Remove lpAlias from aliasTable - we should free the old entry */
1437 if(!strcmp(lpAlias
, pfa
->faAlias
))
1440 prev
->next
= pfa
->next
;
1442 aliasTable
= pfa
->next
;
1445 /* Update any references to the substituted font in aliasTable */
1446 if(!strcmp(frMatch
->lfFaceName
, pfa
->faTypeFace
))
1447 pfa
->faTypeFace
= HEAP_strdupA( GetProcessHeap(), 0, lpAlias
);
1451 TRACE("\tsubstituted '%s' with %s\n", frMatch
->lfFaceName
, lpAlias
);
1453 lstrcpynA( frMatch
->lfFaceName
, lpAlias
, LF_FACESIZE
);
1454 frMatch
->fr_flags
|= FR_NAMESET
;
1458 /* create new entry in the alias table */
1459 XFONT_CreateAlias( frMatch
->lfFaceName
, lpAlias
);
1464 WARN("Font alias '-%s-%s-' is not available\n", lfd
->foundry
, lfd
->family
);
1468 /***********************************************************************
1473 * Create font aliases for some standard windows fonts using users
1474 * default choice of (sans-)serif fonts
1476 * Read user-defined aliases from wine.conf. Format is as follows
1478 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1481 * Alias0 = Arial, -adobe-helvetica-
1482 * Alias1 = Times New Roman, -bitstream-courier-, 1
1485 * Note that from 081797 and on we have built-in alias templates that take
1486 * care of the necessary Windows typefaces.
1494 static void XFONT_LoadAliases(void)
1497 char buffer
[MAX_LFD_LENGTH
];
1501 /* built-ins first */
1502 PROFILE_GetWineIniString( INIFontSection
, INIDefaultSerif
,
1503 "-bitstream-charter-", buffer
, sizeof buffer
);
1504 TRACE("Using '%s' as default serif font\n", buffer
);
1505 lfd
= LFD_Parse(buffer
);
1506 /* NB XFONT_InitialCapitals should not change these standard aliases */
1509 XFONT_LoadAlias( lfd
, "Tms Roman", FALSE
);
1510 XFONT_LoadAlias( lfd
, "MS Serif", FALSE
);
1511 XFONT_LoadAlias( lfd
, "Times New Roman", FALSE
);
1513 XFONT_LoadDefaultLFD( lfd
, "serif ");
1514 HeapFree(GetProcessHeap(), 0, lfd
);
1517 PROFILE_GetWineIniString( INIFontSection
, INIDefaultSansSerif
,
1518 "-adobe-helvetica-", buffer
, sizeof buffer
);
1519 TRACE("Using '%s' as default sans serif font\n", buffer
);
1520 lfd
= LFD_Parse(buffer
);
1523 XFONT_LoadAlias( lfd
, "Helv", FALSE
);
1524 XFONT_LoadAlias( lfd
, "MS Sans Serif", FALSE
);
1525 XFONT_LoadAlias( lfd
, "Arial", FALSE
);
1527 XFONT_LoadDefaultLFD( lfd
, "sans serif ");
1528 HeapFree(GetProcessHeap(), 0, lfd
);
1531 /* then user specified aliases */
1534 BOOL bHaveAlias
, bSubst
;
1535 char subsection
[32];
1536 wsnprintfA( subsection
, sizeof subsection
, "%s%i", INIAliasSection
, i
++ );
1538 bHaveAlias
= PROFILE_GetWineIniString( INIFontSection
,
1539 subsection
, "", buffer
, sizeof buffer
);
1543 XFONT_InitialCapitals(buffer
);
1544 lpResource
= PROFILE_GetStringItem( buffer
);
1545 bSubst
= (PROFILE_GetStringItem( lpResource
)) ? TRUE
: FALSE
;
1546 if( lpResource
&& *lpResource
)
1548 lfd
= LFD_Parse(lpResource
);
1551 XFONT_LoadAlias(lfd
, buffer
, bSubst
);
1552 HeapFree(GetProcessHeap(), 0, lfd
);
1556 WARN("malformed font alias '%s'\n", buffer
);
1561 /***********************************************************************
1564 * Convert an (potential) alias into a real windows name
1567 static LPCSTR
XFONT_UnAlias(char* font
)
1572 XFONT_InitialCapitals(font
); /* to remove extra white space */
1574 for( fa
= aliasTable
; fa
; fa
= fa
->next
)
1575 /* use case insensitive matching to handle eg "MS Sans Serif" */
1576 if( !strcasecmp( fa
->faAlias
, font
) )
1578 TRACE("found alias '%s'->%s'\n", font
, fa
->faTypeFace
);
1579 strcpy(font
, fa
->faTypeFace
);
1587 /***********************************************************************
1588 * XFONT_RemoveFontResource
1590 * Caller should check if the font resource is in use. If it is it should
1591 * set FR_REMOVED flag to delay removal until the resource is not in use
1594 void XFONT_RemoveFontResource( fontResource
** ppfr
)
1596 fontResource
* pfr
= *ppfr
;
1600 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1603 pfi
= pfr
->fi
->next
;
1604 HeapFree( GetProcessHeap(), 0, pfr
->fi
);
1607 HeapFree( GetProcessHeap(), 0, pfr
);
1612 /***********************************************************************
1617 * Removes specified fonts from the font table to prevent Wine from
1620 * Ignore# = [LFD font name]
1623 * Ignore0 = -misc-nil-
1624 * Ignore1 = -sun-open look glyph-
1628 static void XFONT_LoadIgnore(char* lfdname
)
1630 fontResource
** ppfr
;
1632 LFD
* lfd
= LFD_Parse(lfdname
);
1633 if (lfd
&& lfd
->foundry
&& lfd
->family
)
1635 for( ppfr
= &fontList
; *ppfr
; ppfr
= &((*ppfr
)->next
))
1637 if( XFONT_SameFoundryAndFamily( (*ppfr
)->resource
, lfd
) )
1639 TRACE("Ignoring '-%s-%s-'\n",
1640 (*ppfr
)->resource
->foundry
, (*ppfr
)->resource
->family
);
1642 XFONT_RemoveFontResource( ppfr
);
1648 WARN("Malformed font resource\n");
1650 HeapFree(GetProcessHeap(), 0, lfd
);
1653 static void XFONT_LoadIgnores(void)
1656 char subsection
[32];
1657 char buffer
[MAX_LFD_LENGTH
];
1659 /* Standard one that noone wants */
1660 strcpy(buffer
, "-misc-nil-");
1661 XFONT_LoadIgnore(buffer
);
1663 /* Others from INI file */
1666 wsprintfA( subsection
, "%s%i", INIIgnoreSection
, i
++ );
1668 if( PROFILE_GetWineIniString( INIFontSection
,
1669 subsection
, "", buffer
, sizeof buffer
) )
1672 while( *pch
&& isspace(*pch
) ) pch
++;
1673 XFONT_LoadIgnore(pch
);
1681 /***********************************************************************
1682 * XFONT_UserMetricsCache
1684 * Returns expanded name for the cachedmetrics file.
1685 * Now it also appends the current value of the $DISPLAY varaible.
1687 static char* XFONT_UserMetricsCache( char* buffer
, int* buf_size
)
1689 char* pchDisplay
, *home
;
1691 pchDisplay
= getenv( "DISPLAY" );
1692 if( !pchDisplay
) pchDisplay
= "0";
1694 if ((home
= getenv( "HOME" )) != NULL
)
1696 int i
= strlen( home
) + strlen( INIWinePrefix
) +
1697 strlen( INIFontMetrics
) + strlen( pchDisplay
) + 2;
1699 buffer
= (char*) HeapReAlloc( GetProcessHeap(), 0, buffer
, *buf_size
= i
);
1700 strcpy( buffer
, home
);
1701 strcat( buffer
, INIWinePrefix
);
1702 strcat( buffer
, INIFontMetrics
);
1703 strcat( buffer
, pchDisplay
);
1704 } else buffer
[0] = '\0';
1709 /***********************************************************************
1712 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1714 static INT
XFONT_IsSubset(const fontInfo
* match
, const fontInfo
* fi
)
1718 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1720 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1721 m
= (BYTE
*)&fi
->df
.dfPixWidth
- (BYTE
*)&fi
->df
.dfItalic
;
1722 if( memcmp(&match
->df
.dfItalic
, &fi
->df
.dfItalic
, m
)) return 0;
1724 if( (!((fi
->fi_flags
& FI_SCALABLE
) + (match
->fi_flags
& FI_SCALABLE
))
1725 && fi
->lfd_height
!= match
->lfd_height
) ||
1726 (!((fi
->fi_flags
& FI_POLYWEIGHT
) + (match
->fi_flags
& FI_POLYWEIGHT
))
1727 && fi
->df
.dfWeight
!= match
->df
.dfWeight
) ) return 0;
1729 m
= (int)(match
->fi_flags
& (FI_POLYWEIGHT
| FI_SCALABLE
)) -
1730 (int)(fi
->fi_flags
& (FI_SCALABLE
| FI_POLYWEIGHT
));
1732 if( m
== (FI_POLYWEIGHT
- FI_SCALABLE
) ||
1733 m
== (FI_SCALABLE
- FI_POLYWEIGHT
) ) return 0; /* keep both */
1734 else if( m
>= 0 ) return 1; /* 'match' is better */
1736 return -1; /* 'fi' is better */
1739 /***********************************************************************
1742 * REMOVE_SUBSETS - attach new fi and purge subsets
1743 * UNMARK_SUBSETS - remove subset flags from all fi entries
1745 static void XFONT_CheckFIList( fontResource
* fr
, fontInfo
* fi
, int action
)
1748 fontInfo
* pfi
, *prev
;
1750 for( prev
= NULL
, pfi
= fr
->fi
; pfi
; )
1752 if( action
== REMOVE_SUBSETS
)
1754 if( pfi
->fi_flags
& FI_SUBSET
)
1756 fontInfo
* subset
= pfi
;
1760 if( prev
) prev
->next
= pfi
= pfi
->next
;
1761 else fr
->fi
= pfi
= pfi
->next
;
1762 HeapFree( GetProcessHeap(), 0, subset
);
1766 else pfi
->fi_flags
&= ~FI_SUBSET
;
1772 if( action
== REMOVE_SUBSETS
) /* also add the superset */
1774 if( fi
->fi_flags
& FI_SCALABLE
)
1779 else if( prev
) prev
->next
= fi
; else fr
->fi
= fi
;
1783 if( i
) TRACE("\t purged %i subsets [%i]\n", i
, fr
->fi_count
);
1786 /***********************************************************************
1789 static fontResource
* XFONT_FindFIList( fontResource
* pfr
, const char* pTypeFace
)
1793 if( !strcasecmp( pfr
->lfFaceName
, pTypeFace
) ) break;
1796 /* Give the app back the font name it asked for. Encarta checks this. */
1797 if (pfr
) strcpy(pfr
->lfFaceName
,pTypeFace
);
1801 /***********************************************************************
1802 * XFONT_FixupPointSize
1804 static void XFONT_FixupPointSize(fontInfo
* fi
)
1807 df
.dfHorizRes
= df
.dfVertRes
= fi
->lfd_resolution
;
1808 df
.dfPoints
= (INT16
)
1809 (((INT
)(df
.dfPixHeight
- df
.dfInternalLeading
) * 72 + (df
.dfVertRes
>> 1)) /
1814 /***********************************************************************
1815 * XFONT_BuildMetrics
1817 * Build font metrics from X font
1819 static int XFONT_BuildMetrics(char** x_pattern
, int res
, unsigned x_checksum
, int x_count
)
1822 fontInfo
* fi
= NULL
;
1825 MESSAGE("Building font metrics. This may take some time...\n");
1826 for( i
= 0; i
< x_count
; i
++ )
1830 fontResource
* fr
, *pfr
;
1832 char buffer
[MAX_LFD_LENGTH
];
1837 typeface
= HEAP_strdupA(GetProcessHeap(), 0, x_pattern
[i
]);
1841 lfd
= LFD_Parse(typeface
);
1844 HeapFree(GetProcessHeap(), 0, typeface
);
1848 /* find a family to insert into */
1850 for( pfr
= NULL
, fr
= fontList
; fr
; fr
= fr
->next
)
1852 if( XFONT_SameFoundryAndFamily(fr
->resource
, lfd
))
1857 if( !fi
) fi
= (fontInfo
*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo
));
1859 if( !LFD_InitFontInfo( fi
, lfd
, x_pattern
[i
]) )
1862 if( !fr
) /* add new family */
1865 fr
= (fontResource
*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource
));
1868 memset(fr
, 0, sizeof(fontResource
));
1870 fr
->resource
= (LFD
*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD
));
1871 memset(fr
->resource
, 0, sizeof(LFD
));
1873 TRACE("family: -%s-%s-\n", lfd
->foundry
, lfd
->family
);
1874 fr
->resource
->foundry
= HEAP_strdupA(GetProcessHeap(), 0, lfd
->foundry
);
1875 fr
->resource
->family
= HEAP_strdupA(GetProcessHeap(), 0, lfd
->family
);
1876 fr
->resource
->weight
= "";
1878 if( pfr
) pfr
->next
= fr
;
1882 WARN("Not enough memory for a new font family\n");
1885 /* check if we already have something better than "fi" */
1887 for( pfi
= fr
->fi
, j
= 0; pfi
&& j
<= 0; pfi
= pfi
->next
)
1888 if( (j
= XFONT_IsSubset( pfi
, fi
)) < 0 )
1889 pfi
->fi_flags
|= FI_SUBSET
; /* superseded by "fi" */
1890 if( j
> 0 ) goto nextfont
;
1892 /* add new font instance "fi" to the "fr" font resource */
1894 if( fi
->fi_flags
& FI_SCALABLE
)
1897 char pxl_string
[4], res_string
[4];
1898 /* set scalable font height to get an basis for extrapolation */
1900 fi
->lfd_height
= DEF_SCALABLE_HEIGHT
;
1901 fi
->lfd_resolution
= res
;
1903 sprintf(pxl_string
, "%d", fi
->lfd_height
);
1904 sprintf(res_string
, "%d", fi
->lfd_resolution
);
1907 lfd1
.pixel_size
= pxl_string
;
1908 lfd1
.point_size
= "*";
1909 lfd1
.resolution_x
= res_string
;
1910 lfd1
.resolution_y
= res_string
;
1912 LFD_UnParse(buffer
, sizeof buffer
, &lfd1
);
1916 else lpstr
= x_pattern
[i
];
1918 if( (x_fs
= TSXLoadQueryFont(display
, lpstr
)) )
1920 XFONT_SetFontMetric( fi
, fr
, x_fs
);
1921 TSXFreeFont( display
, x_fs
);
1923 XFONT_FixupPointSize(fi
);
1925 TRACE("\t[% 2ipt] '%s'\n", fi
->df
.dfPoints
, x_pattern
[i
] );
1927 XFONT_CheckFIList( fr
, fi
, REMOVE_SUBSETS
);
1928 fi
= NULL
; /* preventing reuse */
1932 ERR("failed to load %s\n", lpstr
);
1934 XFONT_CheckFIList( fr
, fi
, UNMARK_SUBSETS
);
1937 HeapFree(GetProcessHeap(), 0, lfd
);
1938 HeapFree(GetProcessHeap(), 0, typeface
);
1940 if( fi
) HeapFree(GetProcessHeap(), 0, fi
);
1945 /***********************************************************************
1946 * XFONT_ReadCachedMetrics
1950 static BOOL
XFONT_ReadCachedMetrics( int fd
, int res
, unsigned x_checksum
, int x_count
)
1957 /* read checksums */
1958 read( fd
, &u
, sizeof(unsigned) );
1959 read( fd
, &i
, sizeof(int) );
1961 if( u
== x_checksum
&& i
== x_count
)
1963 off_t length
, offset
= 3 * sizeof(int);
1965 /* read total size */
1966 read( fd
, &i
, sizeof(int) );
1967 length
= lseek( fd
, 0, SEEK_END
);
1969 if( length
== (i
+ offset
) )
1971 lseek( fd
, offset
, SEEK_SET
);
1972 fontList
= (fontResource
*)HeapAlloc( GetProcessHeap(), 0, i
);
1975 fontResource
* pfr
= fontList
;
1976 fontInfo
* pfi
= NULL
;
1978 TRACE("Reading cached font metrics:\n");
1980 read( fd
, fontList
, i
); /* read all metrics at once */
1981 while( offset
< length
)
1983 offset
+= sizeof(fontResource
) + sizeof(fontInfo
);
1984 pfr
->fi
= pfi
= (fontInfo
*)(pfr
+ 1);
1988 if( offset
> length
||
1989 (int)(pfi
->next
) != j
++ ) goto fail
;
1991 pfi
->df
.dfFace
= pfr
->lfFaceName
;
1992 if( pfi
->fi_flags
& FI_SCALABLE
)
1994 /* we can pretend we got this font for any resolution */
1995 pfi
->lfd_resolution
= res
;
1996 XFONT_FixupPointSize(pfi
);
1998 pfi
->next
= pfi
+ 1;
2000 if( j
> pfr
->fi_count
) break;
2003 offset
+= sizeof(fontInfo
);
2008 pfr
->next
= (fontResource
*)(pfi
+ 1);
2013 if( pfr
->next
== NULL
&&
2014 *(int*)(pfi
+ 1) == X_FMC_MAGIC
)
2016 /* read LFD stubs */
2017 char* lpch
= (char*)((int*)(pfi
+ 1) + 1);
2018 offset
+= sizeof(int);
2019 for( pfr
= fontList
; pfr
; pfr
= pfr
->next
)
2021 size_t len
= strlen(lpch
) + 1;
2022 TRACE("\t%s, %i instances\n", lpch
, pfr
->fi_count
);
2023 pfr
->resource
= LFD_Parse(lpch
);
2026 if (offset
> length
)
2036 if( fontList
) HeapFree( GetProcessHeap(), 0, fontList
);
2043 /***********************************************************************
2044 * XFONT_WriteCachedMetrics
2048 static BOOL
XFONT_WriteCachedMetrics( int fd
, unsigned x_checksum
, int x_count
, int n_ff
)
2056 char buffer
[MAX_LFD_LENGTH
];
2058 /* font metrics file:
2062 * +0008 total size to load
2063 * +000C prepackaged font metrics
2066 * +...x + 4 LFD stubs
2069 write( fd
, &x_checksum
, sizeof(unsigned) );
2070 write( fd
, &x_count
, sizeof(int) );
2072 for( j
= i
= 0, pfr
= fontList
; pfr
; pfr
= pfr
->next
)
2074 LFD_UnParse(buffer
, sizeof buffer
, pfr
->resource
);
2075 i
+= strlen( buffer
) + 1;
2078 i
+= n_ff
* sizeof(fontResource
) + j
* sizeof(fontInfo
) + sizeof(int);
2079 write( fd
, &i
, sizeof(int) );
2081 TRACE("Writing font cache:\n");
2083 for( pfr
= fontList
; pfr
; pfr
= pfr
->next
)
2087 TRACE("\t-%s-%s-, %i instances\n", pfr
->resource
->foundry
, pfr
->resource
->family
, pfr
->fi_count
);
2089 i
= write( fd
, pfr
, sizeof(fontResource
) );
2090 if( i
== sizeof(fontResource
) )
2092 for( k
= 1, pfi
= pfr
->fi
; pfi
; pfi
= pfi
->next
)
2096 fi
.df
.dfFace
= NULL
;
2097 fi
.next
= (fontInfo
*)k
; /* loader checks this */
2099 j
= write( fd
, &fi
, sizeof(fi
) );
2102 if( j
== sizeof(fontInfo
) ) continue;
2106 if( i
== sizeof(fontResource
) && j
== sizeof(fontInfo
) )
2108 i
= j
= X_FMC_MAGIC
;
2109 write( fd
, &i
, sizeof(int) );
2110 for( pfr
= fontList
; pfr
&& i
== j
; pfr
= pfr
->next
)
2112 LFD_UnParse(buffer
, sizeof buffer
, pfr
->resource
);
2113 i
= strlen( buffer
) + 1;
2114 j
= write( fd
, buffer
, i
);
2123 /***********************************************************************
2124 * XFONT_GetPointResolution()
2128 * Here we initialize DefResolution which is used in the
2129 * XFONT_Match() penalty function. We also load the point
2130 * resolution value (higher values result in larger fonts).
2132 static int XFONT_GetPointResolution( DeviceCaps
* pDevCaps
)
2134 int i
, j
, point_resolution
, num
= 3;
2135 int allowed_xfont_resolutions
[3] = { 72, 75, 100 };
2136 int best
= 0, best_diff
= 65536;
2138 point_resolution
= PROFILE_GetWineIniInt( INIFontSection
, INIResolution
, 0 );
2139 if( !point_resolution
)
2140 point_resolution
= pDevCaps
->logPixelsY
;
2142 pDevCaps
->logPixelsX
= pDevCaps
->logPixelsY
= point_resolution
;
2145 /* FIXME We can only really guess at a best DefResolution
2146 * - this should be configurable
2148 for( i
= best
= 0; i
< num
; i
++ )
2150 j
= abs( point_resolution
- allowed_xfont_resolutions
[i
] );
2157 DefResolution
= allowed_xfont_resolutions
[best
];
2159 /* FIXME - do win95,nt40,... do this as well ? */
2160 if (TWEAK_WineLook
== WIN98_LOOK
)
2162 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2164 denom
= pDevCaps
->logPixelsX
* 10;
2165 pDevCaps
->horzSize
= (pDevCaps
->horzRes
* 254 + (denom
>>1)) / denom
;
2166 denom
= pDevCaps
->logPixelsY
* 10;
2167 pDevCaps
->vertSize
= (pDevCaps
->vertRes
* 254 + (denom
>>1)) / denom
;
2170 return point_resolution
;
2174 /***********************************************************************
2177 * Compute the matching score between the logical font and the device font.
2179 * contributions from highest to lowest:
2183 * family flags (only when the facename is not present)
2185 * weight, italics, underlines, strikeouts
2187 * NOTE: you can experiment with different penalty weights to see what happens.
2188 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2190 static UINT
XFONT_Match( fontMatch
* pfm
)
2192 fontInfo
* pfi
= pfm
->pfi
; /* device font to match */
2193 LPLOGFONT16 plf
= pfm
->plf
; /* wanted logical font */
2195 BOOL bR6
= pfm
->flags
& FO_MATCH_XYINDEP
; /* from TextCaps */
2196 BOOL bScale
= pfi
->fi_flags
& FI_SCALABLE
;
2199 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi
->df
.dfPoints
,
2200 pfi
->df
.dfPixHeight
, pfi
->df
.dfAvgWidth
,
2201 (pfi
->df
.dfWeight
> FW_NORMAL
) ? "Bold " : "Normal ",
2202 (pfi
->df
.dfItalic
) ? "Italic" : "" );
2204 pfm
->flags
&= FO_MATCH_MASK
;
2207 if( plf
->lfCharSet
== DEFAULT_CHARSET
)
2209 if( (pfi
->df
.dfCharSet
!= ANSI_CHARSET
) && (pfi
->df
.dfCharSet
!= DEFAULT_CHARSET
) )
2212 else if (plf
->lfCharSet
!= pfi
->df
.dfCharSet
) penalty
+= 0x200;
2217 if( plf
->lfHeight
> 0 )
2219 int h
= pfi
->df
.dfPixHeight
;
2220 d
= h
- plf
->lfHeight
;
2221 height
= plf
->lfHeight
;
2225 int h
= pfi
->df
.dfPixHeight
- pfi
->df
.dfInternalLeading
;
2228 d
= h
+ plf
->lfHeight
;
2229 height
= (-plf
->lfHeight
* pfi
->df
.dfPixHeight
) / h
;
2233 ERR("PixHeight == InternalLeading\n");
2234 penalty
+= 0x1000; /* dont want this */
2240 pfm
->height
= 1; /* Very small */
2244 pfm
->height
= height
;
2245 else if( (plf
->lfQuality
!= PROOF_QUALITY
) && bR6
)
2247 if( d
> 0 ) /* do not shrink raster fonts */
2249 pfm
->height
= pfi
->df
.dfPixHeight
;
2250 penalty
+= (pfi
->df
.dfPixHeight
- height
) * 0x4;
2252 else /* expand only in integer multiples */
2254 pfm
->height
= height
- height
%pfi
->df
.dfPixHeight
;
2255 penalty
+= (height
- pfm
->height
+ 1) * height
/ pfi
->df
.dfPixHeight
;
2258 else /* can't be scaled at all */
2260 if( plf
->lfQuality
!= PROOF_QUALITY
) pfm
->flags
|= FO_SYNTH_HEIGHT
;
2261 pfm
->height
= pfi
->df
.dfPixHeight
;
2262 penalty
+= (d
> 0)? d
* 0x8 : -d
* 0x10;
2266 pfm
->height
= pfi
->df
.dfPixHeight
;
2268 /* Pitch and Family */
2269 if( pfm
->flags
& FO_MATCH_PAF
) {
2270 int family
= plf
->lfPitchAndFamily
& FF_FAMILY
;
2272 /* TMPF_FIXED_PITCH means exactly the opposite */
2273 if( plf
->lfPitchAndFamily
& FIXED_PITCH
) {
2274 if( pfi
->df
.dfPitchAndFamily
& TMPF_FIXED_PITCH
) penalty
+= 0x100;
2275 } else /* Variable is the default */
2276 if( !(pfi
->df
.dfPitchAndFamily
& TMPF_FIXED_PITCH
) ) penalty
+= 0x2;
2278 if (family
!= FF_DONTCARE
&& family
!= (pfi
->df
.dfPitchAndFamily
& FF_FAMILY
) )
2286 if( bR6
|| bScale
) h
= 0;
2289 /* FIXME: not complete */
2291 pfm
->flags
|= FO_SYNTH_WIDTH
;
2292 h
= abs(plf
->lfWidth
- (pfm
->height
* pfi
->df
.dfAvgWidth
)/pfi
->df
.dfPixHeight
);
2294 penalty
+= h
* ( d
) ? 0x2 : 0x1 ;
2296 else if( !(pfi
->fi_flags
& FI_NORMAL
) ) penalty
++;
2299 if( plf
->lfWeight
!= FW_DONTCARE
)
2301 penalty
+= abs(plf
->lfWeight
- pfi
->df
.dfWeight
) / 40;
2302 if( plf
->lfWeight
> pfi
->df
.dfWeight
) pfm
->flags
|= FO_SYNTH_BOLD
;
2303 } else if( pfi
->df
.dfWeight
>= FW_BOLD
) penalty
++; /* choose normal by default */
2306 if( plf
->lfItalic
!= pfi
->df
.dfItalic
)
2309 pfm
->flags
|= FO_SYNTH_ITALIC
;
2312 if( plf
->lfUnderline
) pfm
->flags
|= FO_SYNTH_UNDERLINE
;
2315 if( plf
->lfStrikeOut
) pfm
->flags
|= FO_SYNTH_STRIKEOUT
;
2318 if( penalty
&& !bScale
&& pfi
->lfd_resolution
!= DefResolution
)
2321 TRACE(" returning %i\n", penalty
);
2326 /***********************************************************************
2329 * Scan a particular font resource for the best match.
2331 static UINT
XFONT_MatchFIList( fontMatch
* pfm
)
2333 BOOL skipRaster
= (pfm
->flags
& FO_MATCH_NORASTER
);
2334 UINT current_score
, score
= (UINT
)(-1);
2335 fontMatch fm
= *pfm
;
2337 for( fm
.pfi
= pfm
->pfr
->fi
; fm
.pfi
&& score
; fm
.pfi
= fm
.pfi
->next
)
2339 if( skipRaster
&& !(fm
.pfi
->fi_flags
& FI_SCALABLE
) )
2342 current_score
= XFONT_Match( &fm
);
2343 if( score
> current_score
)
2346 score
= current_score
;
2352 /***********************************************************************
2353 * XFONT_MatchDeviceFont
2355 * Scan font resource tree.
2358 static void XFONT_MatchDeviceFont( fontResource
* start
, fontMatch
* pfm
)
2360 fontMatch fm
= *pfm
;
2361 UINT current_score
, score
= (UINT
)(-1);
2362 fontResource
** ppfr
;
2364 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2365 pfm
->plf
->lfCharSet
, pfm
->plf
->lfFaceName
, pfm
->plf
->lfHeight
,
2366 pfm
->plf
->lfWeight
, (pfm
->plf
->lfItalic
) ? "Italic" : "" );
2369 if( fm
.plf
->lfFaceName
[0] )
2371 fm
.pfr
= XFONT_FindFIList( start
, fm
.plf
->lfFaceName
);
2372 if( fm
.pfr
) /* match family */
2374 TRACE("found facename '%s'\n", fm
.pfr
->lfFaceName
);
2376 if( fm
.pfr
->fr_flags
& FR_REMOVED
)
2380 XFONT_MatchFIList( &fm
);
2388 /* match all available fonts */
2390 fm
.flags
|= FO_MATCH_PAF
;
2391 for( ppfr
= &fontList
; *ppfr
&& score
; ppfr
= &(*ppfr
)->next
)
2393 if( (*ppfr
)->fr_flags
& FR_REMOVED
)
2395 if( (*ppfr
)->fo_count
== 0 )
2396 XFONT_RemoveFontResource( ppfr
);
2402 TRACE("%s\n", fm
.pfr
->lfFaceName
);
2404 current_score
= XFONT_MatchFIList( &fm
);
2405 if( current_score
< score
)
2407 score
= current_score
;
2414 /***********************************************************************
2417 static void XFONT_GrowFreeList(int start
, int end
)
2419 /* add all entries from 'start' up to and including 'end' */
2421 memset( fontCache
+ start
, 0, (end
- start
+ 1) * sizeof(fontObject
) );
2423 fontCache
[end
].lru
= fontLF
;
2424 fontCache
[end
].count
= -1;
2426 while( start
< end
)
2428 fontCache
[start
].count
= -1;
2429 fontCache
[start
].lru
= start
+ 1;
2434 static fontObject
* XFONT_LookupCachedFont( const LPLOGFONT16 plf
, UINT16
* checksum
)
2436 UINT16 cs
= __lfCheckSum( plf
);
2437 int i
= fontMRU
, prev
= -1;
2442 if( fontCache
[i
].lfchecksum
== cs
&&
2443 !(fontCache
[i
].fo_flags
& FO_REMOVED
) )
2445 /* FIXME: something more intelligent here ? */
2447 if( !memcmp( plf
, &fontCache
[i
].lf
,
2448 sizeof(LOGFONT16
) - LF_FACESIZE
) &&
2449 !strcmp( plf
->lfFaceName
, fontCache
[i
].lf
.lfFaceName
) )
2451 /* remove temporarily from the lru list */
2453 fontCache
[prev
].lru
= fontCache
[i
].lru
;
2455 fontMRU
= (INT16
)fontCache
[i
].lru
;
2456 return (fontCache
+ i
);
2460 i
= (INT16
)fontCache
[i
].lru
;
2465 static fontObject
* XFONT_GetCacheEntry(void)
2471 int prev_i
, prev_j
, j
;
2473 TRACE("font cache is full\n");
2475 /* lookup the least recently used font */
2477 for( prev_i
= prev_j
= j
= -1, i
= fontMRU
; i
>= 0; i
= (INT16
)fontCache
[i
].lru
)
2479 if( fontCache
[i
].count
<= 0 &&
2480 !(fontCache
[i
].fo_flags
& FO_SYSTEM
) )
2488 if( j
>= 0 ) /* unload font */
2490 /* detach from the lru list */
2492 TRACE("\tfreeing entry %i\n", j
);
2494 fontCache
[j
].fr
->fo_count
--;
2497 fontCache
[prev_j
].lru
= fontCache
[j
].lru
;
2498 else fontMRU
= (INT16
)fontCache
[j
].lru
;
2500 /* FIXME: lpXForm, lpPixmap */
2501 if(fontCache
[j
].lpX11Trans
)
2502 HeapFree( GetProcessHeap(), 0, fontCache
[j
].lpX11Trans
);
2504 TSXFreeFont( display
, fontCache
[j
].fs
);
2506 memset( fontCache
+ j
, 0, sizeof(fontObject
) );
2507 return (fontCache
+ j
);
2509 else /* expand cache */
2511 fontObject
* newCache
;
2513 prev_i
= fontCacheSize
+ FONTCACHE
;
2515 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize
, prev_i
);
2517 if( (newCache
= (fontObject
*)HeapReAlloc(GetProcessHeap(), 0,
2518 fontCache
, prev_i
)) )
2521 fontCacheSize
= prev_i
;
2522 fontCache
= newCache
;
2523 XFONT_GrowFreeList( i
, fontCacheSize
- 1);
2529 /* detach from the free list */
2532 fontLF
= (INT16
)fontCache
[i
].lru
;
2533 fontCache
[i
].count
= 0;
2534 return (fontCache
+ i
);
2537 static int XFONT_ReleaseCacheEntry(const fontObject
* pfo
)
2539 UINT u
= (UINT
)(pfo
- fontCache
);
2541 if( u
< fontCacheSize
) return (--fontCache
[u
].count
);
2545 /***********************************************************************
2548 * Initialize font resource list and allocate font cache.
2550 BOOL
X11DRV_FONT_Init( DeviceCaps
* pDevCaps
)
2553 unsigned x_checksum
;
2554 int i
,res
, x_count
, fd
, buf_size
;
2557 res
= XFONT_GetPointResolution( pDevCaps
);
2559 x_pattern
= TSXListFonts(display
, "*", MAX_FONTS
, &x_count
);
2561 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2562 x_count
, res
, DefResolution
);
2563 if (x_count
== MAX_FONTS
)
2564 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2566 for( i
= x_checksum
= 0; i
< x_count
; i
++ )
2570 printf("%i\t: %s\n", i
, x_pattern
[i
] );
2573 j
= strlen( x_pattern
[i
] );
2574 if( j
) x_checksum
^= __genericCheckSum( x_pattern
[i
], j
);
2576 x_checksum
|= X_PFONT_MAGIC
;
2578 buffer
= HeapAlloc( GetProcessHeap(), 0, buf_size
);
2580 /* deal with systemwide font metrics cache */
2582 if( PROFILE_GetWineIniString( INIFontSection
, INIGlobalMetrics
, "", buffer
, buf_size
) )
2584 fd
= open( buffer
, O_RDONLY
);
2585 XFONT_ReadCachedMetrics(fd
, DefResolution
, x_checksum
, x_count
);
2587 if (fontList
== NULL
)
2590 buffer
= XFONT_UserMetricsCache( buffer
, &buf_size
);
2593 fd
= open( buffer
, O_RDONLY
);
2594 XFONT_ReadCachedMetrics(fd
, DefResolution
, x_checksum
, x_count
);
2598 if( fontList
== NULL
) /* build metrics from scratch */
2600 int n_ff
= XFONT_BuildMetrics(x_pattern
, DefResolution
, x_checksum
, x_count
);
2601 if( buffer
[0] ) /* update cached metrics */
2603 fd
= open( buffer
, O_CREAT
| O_TRUNC
| O_RDWR
, 0644 ); /* -rw-r--r-- */
2604 if( XFONT_WriteCachedMetrics( fd
, x_checksum
, x_count
, n_ff
) == FALSE
)
2606 WARN("Unable to write to fontcache '%s'\n", buffer
);
2607 if( fd
>= 0) remove( buffer
); /* couldn't write entire file */
2612 TSXFreeFontNames(x_pattern
);
2614 /* check if we're dealing with X11 R6 server */
2617 strcpy(buffer
, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2618 if( (x_fs
= TSXLoadQueryFont(display
, buffer
)) )
2620 XTextCaps
|= TC_SF_X_YINDEP
;
2621 TSXFreeFont(display
, x_fs
);
2624 HeapFree(GetProcessHeap(), 0, buffer
);
2626 XFONT_WindowsNames();
2627 XFONT_LoadAliases();
2628 XFONT_LoadDefaults();
2629 XFONT_LoadIgnores();
2631 InitializeCriticalSection( &crtsc_fonts_X11
);
2632 MakeCriticalSectionGlobal( &crtsc_fonts_X11
);
2634 /* fontList initialization is over, allocate X font cache */
2636 fontCache
= (fontObject
*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize
* sizeof(fontObject
));
2637 XFONT_GrowFreeList(0, fontCacheSize
- 1);
2641 /* update text caps parameter */
2643 pDevCaps
->textCaps
= XTextCaps
;
2645 RAW_ASCENT
= TSXInternAtom(display
, "RAW_ASCENT", TRUE
);
2646 RAW_DESCENT
= TSXInternAtom(display
, "RAW_DESCENT", TRUE
);
2651 /**********************************************************************
2654 static BOOL
XFONT_SetX11Trans( fontObject
*pfo
)
2660 TSXGetFontProperty( pfo
->fs
, XA_FONT
, &nameAtom
);
2661 fontName
= TSXGetAtomName( display
, nameAtom
);
2662 lfd
= LFD_Parse(fontName
);
2669 if (lfd
->pixel_size
[0] != '[') {
2670 HeapFree(GetProcessHeap(), 0, lfd
);
2675 #define PX pfo->lpX11Trans
2677 sscanf(lfd
->pixel_size
, "[%f%f%f%f]", &PX
->a
, &PX
->b
, &PX
->c
, &PX
->d
);
2679 HeapFree(GetProcessHeap(), 0, lfd
);
2681 TSXGetFontProperty( pfo
->fs
, RAW_ASCENT
, &PX
->RAW_ASCENT
);
2682 TSXGetFontProperty( pfo
->fs
, RAW_DESCENT
, &PX
->RAW_DESCENT
);
2684 PX
->pixelsize
= hypot(PX
->a
, PX
->b
);
2685 PX
->ascent
= PX
->pixelsize
/ 1000.0 * PX
->RAW_ASCENT
;
2686 PX
->descent
= PX
->pixelsize
/ 1000.0 * PX
->RAW_DESCENT
;
2688 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2689 PX
->a
, PX
->b
, PX
->c
, PX
->d
,
2690 PX
->RAW_ASCENT
, PX
->RAW_DESCENT
);
2696 /***********************************************************************
2697 * X Device Font Objects
2699 static X_PHYSFONT
XFONT_RealizeFont( const LPLOGFONT16 plf
, LPCSTR
* faceMatched
)
2705 pfo
= XFONT_LookupCachedFont( plf
, &checksum
);
2717 if( XTextCaps
& TC_SF_X_YINDEP
) fm
.flags
= FO_MATCH_XYINDEP
;
2719 /* allocate new font cache entry */
2721 if( (pfo
= XFONT_GetCacheEntry()) )
2723 /* initialize entry and load font */
2724 char lpLFD
[MAX_LFD_LENGTH
];
2725 UINT uRelaxLevel
= 0;
2727 if(abs(plf
->lfHeight
) > MAX_FONT_SIZE
) {
2729 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2731 pfo
->rescale
= fabs(plf
->lfHeight
/ 100.0);
2732 if(plf
->lfHeight
> 0) plf
->lfHeight
= 100;
2733 else plf
->lfHeight
= -100;
2737 XFONT_MatchDeviceFont( fontList
, &fm
);
2740 pfo
->fr
->fo_count
++;
2741 pfo
->fo_flags
= fm
.flags
& ~FO_MATCH_MASK
;
2744 pfo
->lfchecksum
= checksum
;
2748 LFD_ComposeLFD( pfo
, fm
.height
, lpLFD
, uRelaxLevel
++ );
2749 if( (pfo
->fs
= TSXLoadQueryFont( display
, lpLFD
)) ) break;
2750 } while( uRelaxLevel
);
2753 if(pfo
->lf
.lfEscapement
!= 0) {
2754 pfo
->lpX11Trans
= HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS
));
2755 if(!XFONT_SetX11Trans( pfo
)) {
2756 HeapFree(GetProcessHeap(), 0, pfo
->lpX11Trans
);
2757 pfo
->lpX11Trans
= NULL
;
2760 XFONT_GetLeading( &pfo
->fi
->df
, pfo
->fs
,
2761 &pfo
->foInternalLeading
, NULL
, pfo
->lpX11Trans
);
2762 pfo
->foAvgCharWidth
= (INT16
)XFONT_GetAvgCharWidth(&pfo
->fi
->df
, pfo
->fs
, pfo
->lpX11Trans
);
2763 pfo
->foMaxCharWidth
= (INT16
)XFONT_GetMaxCharWidth(pfo
->fs
, pfo
->lpX11Trans
);
2765 /* FIXME: If we've got a soft font or
2766 * there are FO_SYNTH_... flags for the
2767 * non PROOF_QUALITY request, the engine
2768 * should rasterize characters into mono
2769 * pixmaps and store them in the pfo->lpPixmap
2770 * array (pfo->fs should be updated as well).
2771 * array (pfo->fs should be updated as well).
2772 * X11DRV_ExtTextOut() must be heavily modified
2773 * to support pixmap blitting and FO_SYNTH_...
2777 pfo
->lpPixmap
= NULL
;
2780 if( !pfo
) /* couldn't get a new entry, get one of the cached fonts */
2782 UINT current_score
, score
= (UINT
)(-1);
2784 i
= index
= fontMRU
;
2785 fm
.flags
|= FO_MATCH_PAF
;
2788 pfo
= fontCache
+ i
;
2789 fm
.pfr
= pfo
->fr
; fm
.pfi
= pfo
->fi
;
2791 current_score
= XFONT_Match( &fm
);
2792 if( current_score
< score
) index
= i
;
2796 pfo
= fontCache
+ index
;
2801 /* attach at the head of the lru list */
2803 index
= fontMRU
= (pfo
- fontCache
);
2808 TRACE("physfont %i\n", index
);
2809 *faceMatched
= pfo
->fi
->df
.dfFace
;
2811 return (X_PHYSFONT
)(X_PFONT_MAGIC
| index
);
2814 /***********************************************************************
2815 * XFONT_GetFontObject
2817 fontObject
* XFONT_GetFontObject( X_PHYSFONT pFont
)
2819 if( CHECK_PFONT(pFont
) ) return __PFONT(pFont
);
2823 /***********************************************************************
2824 * XFONT_GetFontStruct
2826 XFontStruct
* XFONT_GetFontStruct( X_PHYSFONT pFont
)
2828 if( CHECK_PFONT(pFont
) ) return __PFONT(pFont
)->fs
;
2832 /***********************************************************************
2835 LPIFONTINFO16
XFONT_GetFontInfo( X_PHYSFONT pFont
)
2837 if( CHECK_PFONT(pFont
) ) return &(__PFONT(pFont
)->fi
->df
);
2843 /* X11DRV Interface ****************************************************
2845 * Exposed via the dc->funcs dispatch table. *
2847 ***********************************************************************/
2848 /***********************************************************************
2849 * X11DRV_FONT_SelectObject
2851 HFONT
X11DRV_FONT_SelectObject( DC
* dc
, HFONT hfont
, FONTOBJ
* font
)
2853 HFONT hPrevFont
= 0;
2855 X11DRV_PDEVICE
*physDev
= (X11DRV_PDEVICE
*)dc
->physDev
;
2857 EnterCriticalSection( &crtsc_fonts_X11
);
2859 if( CHECK_PFONT(physDev
->font
) )
2860 XFONT_ReleaseCacheEntry( __PFONT(physDev
->font
) );
2864 /* Make sure we don't change the sign when converting to device coords */
2865 /* FIXME - check that the other drivers do this correctly */
2868 int vpt
= abs(dc
->vportExtX
);
2869 int wnd
= abs(dc
->wndExtX
);
2870 lf
.lfWidth
= (abs(lf
.lfWidth
) * vpt
+ (wnd
>>1))/wnd
;
2871 if (lf
.lfWidth
== 0)
2872 lf
.lfWidth
= 1; /* Minimum width */
2876 int vpt
= abs(dc
->vportExtY
);
2877 int wnd
= abs(dc
->wndExtY
);
2878 if (lf
.lfHeight
> 0)
2879 lf
.lfHeight
= (lf
.lfHeight
* vpt
+ (wnd
>>1))/wnd
;
2881 lf
.lfHeight
= (lf
.lfHeight
* vpt
- (wnd
>>1))/wnd
;
2883 if (lf
.lfHeight
== 0)
2884 lf
.lfHeight
= MIN_FONT_SIZE
;
2887 lf
.lfHeight
= -(DEF_POINT_SIZE
* dc
->w
.devCaps
->logPixelsY
+ (72>>1)) / 72;
2890 /* Fixup aliases before passing to RealizeFont */
2891 /* alias = Windows name in the alias table */
2892 LPCSTR alias
= XFONT_UnAlias( lf
.lfFaceName
);
2895 TRACE("hfont=%04x\n", hfont
); /* to connect with the trace from RealizeFont */
2896 physDev
->font
= XFONT_RealizeFont( &lf
, &faceMatched
);
2898 /* set face to the requested facename if it matched
2899 * so that GetTextFace can get the correct face name
2901 if (alias
&& !strcmp(faceMatched
, lf
.lfFaceName
))
2902 strcpy( font
->logfont
.lfFaceName
, alias
);
2904 strcpy( font
->logfont
.lfFaceName
, faceMatched
);
2907 hPrevFont
= dc
->w
.hFont
;
2908 dc
->w
.hFont
= hfont
;
2910 LeaveCriticalSection( &crtsc_fonts_X11
);
2916 /***********************************************************************
2918 * X11DRV_EnumDeviceFonts
2920 BOOL
X11DRV_EnumDeviceFonts( DC
* dc
, LPLOGFONT16 plf
,
2921 DEVICEFONTENUMPROC proc
, LPARAM lp
)
2925 fontResource
* pfr
= fontList
;
2928 if( plf
->lfFaceName
[0] )
2930 /* enum all entries in this resource */
2931 pfr
= XFONT_FindFIList( pfr
, plf
->lfFaceName
);
2935 for( pfi
= pfr
->fi
; pfi
; pfi
= pfi
->next
)
2937 /* Note: XFONT_GetFontMetric() will have to
2938 release the crit section, font list will
2939 have to be retraversed on return */
2941 if( (b
= (*proc
)( &lf
, &tm
,
2942 XFONT_GetFontMetric( pfi
, &lf
, &tm
), lp
)) )
2948 else /* enum first entry in each resource */
2949 for( ; pfr
; pfr
= pfr
->next
)
2953 if( (b
= (*proc
)( &lf
, &tm
,
2954 XFONT_GetFontMetric( pfr
->fi
, &lf
, &tm
), lp
)) )
2963 /***********************************************************************
2964 * X11DRV_GetTextExtentPoint
2966 BOOL
X11DRV_GetTextExtentPoint( DC
*dc
, LPCWSTR str
, INT count
,
2969 X11DRV_PDEVICE
*physDev
= (X11DRV_PDEVICE
*)dc
->physDev
;
2970 fontObject
* pfo
= XFONT_GetFontObject( physDev
->font
);
2972 TRACE("%s %d\n", debugstr_wn(str
,count
), count
);
2974 if( !pfo
->lpX11Trans
) {
2975 int dir
, ascent
, descent
, i
;
2977 XChar2b
*p
= HeapAlloc( GetProcessHeap(), 0,
2978 count
* sizeof(XChar2b
) );
2979 for(i
= 0; i
< count
; i
++) {
2980 p
[i
].byte1
= str
[i
] >> 8;
2981 p
[i
].byte2
= str
[i
] & 0xff;
2983 TSXTextExtents16( pfo
->fs
, p
, count
, &dir
, &ascent
, &descent
, &info
);
2984 size
->cx
= abs((info
.width
+ dc
->w
.breakRem
+ count
*
2985 dc
->w
.charExtra
) * dc
->wndExtX
/ dc
->vportExtX
);
2986 size
->cy
= abs((pfo
->fs
->ascent
+ pfo
->fs
->descent
) *
2987 dc
->wndExtY
/ dc
->vportExtY
);
2988 HeapFree( GetProcessHeap(), 0, p
);
2991 float x
= 0.0, y
= 0.0;
2992 for(i
= 0; i
< count
; i
++) {
2993 x
+= pfo
->fs
->per_char
?
2994 pfo
->fs
->per_char
[str
[i
] - pfo
->fs
->min_char_or_byte2
].attributes
:
2995 pfo
->fs
->min_bounds
.attributes
;
2997 y
= pfo
->lpX11Trans
->RAW_ASCENT
+ pfo
->lpX11Trans
->RAW_DESCENT
;
2998 TRACE("x = %f y = %f\n", x
, y
);
2999 x
*= pfo
->lpX11Trans
->pixelsize
/ 1000.0;
3000 y
*= pfo
->lpX11Trans
->pixelsize
/ 1000.0;
3001 size
->cx
= fabs((x
+ dc
->w
.breakRem
+ count
* dc
->w
.charExtra
) *
3002 dc
->wndExtX
/ dc
->vportExtX
);
3003 size
->cy
= fabs(y
* dc
->wndExtY
/ dc
->vportExtY
);
3005 size
->cx
*= pfo
->rescale
;
3006 size
->cy
*= pfo
->rescale
;
3013 /***********************************************************************
3014 * X11DRV_GetTextMetrics
3016 BOOL
X11DRV_GetTextMetrics(DC
*dc
, TEXTMETRICA
*metrics
)
3018 X11DRV_PDEVICE
*physDev
= (X11DRV_PDEVICE
*)dc
->physDev
;
3020 if( CHECK_PFONT(physDev
->font
) )
3022 fontObject
* pfo
= __PFONT(physDev
->font
);
3023 XFONT_GetTextMetrics( pfo
, metrics
);
3031 /***********************************************************************
3032 * X11DRV_GetCharWidth
3034 BOOL
X11DRV_GetCharWidth( DC
*dc
, UINT firstChar
, UINT lastChar
,
3037 X11DRV_PDEVICE
*physDev
= (X11DRV_PDEVICE
*)dc
->physDev
;
3038 fontObject
* pfo
= XFONT_GetFontObject( physDev
->font
);
3044 if (pfo
->fs
->per_char
== NULL
)
3045 for (i
= firstChar
; i
<= lastChar
; i
++)
3047 *buffer
++ = pfo
->fs
->min_bounds
.attributes
*
3048 pfo
->lpX11Trans
->pixelsize
/ 1000.0 * pfo
->rescale
;
3050 *buffer
++ = pfo
->fs
->min_bounds
.width
* pfo
->rescale
;
3053 XCharStruct
*cs
, *def
;
3054 static XCharStruct __null_char
= { 0, 0, 0, 0, 0, 0 };
3056 CI_GET_CHAR_INFO(pfo
->fs
, pfo
->fs
->default_char
, &__null_char
,
3059 for (i
= firstChar
; i
<= lastChar
; i
++)
3061 if (i
>= pfo
->fs
->min_char_or_byte2
&&
3062 i
<= pfo
->fs
->max_char_or_byte2
)
3064 cs
= &pfo
->fs
->per_char
[(i
- pfo
->fs
->min_char_or_byte2
)];
3065 if (CI_NONEXISTCHAR(cs
)) cs
= def
;
3068 *buffer
++ = max(cs
->attributes
, 0) *
3069 pfo
->lpX11Trans
->pixelsize
/ 1000.0 * pfo
->rescale
;
3071 *buffer
++ = max(cs
->width
, 0 ) * pfo
->rescale
;
3080 #endif /* !defined(X_DISPLAY_MISSING) */