2 * X11 physical font objects
4 * Copyright 1997 Alex Korobka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
21 * have to be changed for that), dynamic font loading (FreeType).
25 #include "wine/port.h"
35 #include <sys/types.h>
45 #include "wine/library.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(font);
51 #define X_PFONT_MAGIC (0xFADE0000)
52 #define X_FMC_MAGIC (0x0000CAFE)
54 #define MAX_FONTS 1024*16
55 #define MAX_LFD_LENGTH 256
59 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
60 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
61 #define MIN_FONT_SIZE 2 /* Min size in pixels */
62 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
64 #define REMOVE_SUBSETS 1
65 #define UNMARK_SUBSETS 0
67 #define FONTCACHE 32 /* dynamic font cache size */
69 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
71 typedef struct __fontAlias
75 struct __fontAlias* next;
78 static fontAlias *aliasTable = NULL;
80 static const char INIFontMetrics[] = "cachedmetrics.";
81 static const char INIFontSection[] = "Software\\Wine\\X11 Driver\\Fonts";
82 static const char INIAliasSection[] = "Alias";
83 static const char INIIgnoreSection[] = "Ignore";
84 static const char INIDefault[] = "Default";
85 static const char INIDefaultFixed[] = "DefaultFixed";
86 static const char INIGlobalMetrics[] = "FontMetrics";
87 static const char INIDefaultSerif[] = "DefaultSerif";
88 static const char INIDefaultSansSerif[] = "DefaultSansSerif";
91 /* FIXME - are there any more Latin charsets ? */
92 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
93 #define IS_LATIN_CHARSET(ch) \
94 ((ch)==ANSI_CHARSET ||\
96 (ch)==ISO3_CHARSET ||\
97 (ch)==ISO4_CHARSET ||\
98 (ch)==RUSSIAN_CHARSET ||\
99 (ch)==ARABIC_CHARSET ||\
100 (ch)==GREEK_CHARSET ||\
101 (ch)==HEBREW_CHARSET ||\
102 (ch)==TURKISH_CHARSET ||\
103 (ch)==ISO10_CHARSET ||\
104 (ch)==BALTIC_CHARSET ||\
105 (ch)==CELTIC_CHARSET)
107 /* suffix-charset mapping tables - must be less than 254 entries long */
109 typedef struct __sufch
112 WORD charset; /* hibyte != 0 means *internal* charset */
117 static const SuffixCharset sufch_ansi[] = {
118 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
119 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
121 static const SuffixCharset sufch_iso646[] = {
122 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
123 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
125 static const SuffixCharset sufch_iso8859[] = {
126 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
127 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
128 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
129 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
130 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
131 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
132 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
133 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
134 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
135 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
136 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
137 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
138 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
139 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
140 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
141 { "16", ISO3_CHARSET, 28606, X11DRV_CPTABLE_SBCS },
142 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
144 static const SuffixCharset sufch_microsoft[] = {
145 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
146 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
147 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
148 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
149 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
150 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
151 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
152 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
153 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
154 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
155 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
157 static const SuffixCharset sufch_tcvn[] = {
158 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
159 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
161 static const SuffixCharset sufch_tis620[] = {
162 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
163 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
165 static const SuffixCharset sufch_viscii[] = {
166 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
167 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
169 static const SuffixCharset sufch_windows[] = {
170 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
171 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
172 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
173 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
174 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
175 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
176 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
177 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
178 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
180 static const SuffixCharset sufch_koi8[] = {
181 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
182 { "ru", RUSSIAN_CHARSET, 21866, X11DRV_CPTABLE_SBCS },
183 { "u", RUSSIAN_CHARSET, 21866, X11DRV_CPTABLE_SBCS },
184 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
186 static const SuffixCharset sufch_jisx0201[] = {
187 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
188 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
190 static const SuffixCharset sufch_jisx0208[] = {
191 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
192 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
194 static const SuffixCharset sufch_jisx0212[] = {
195 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
196 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
198 static const SuffixCharset sufch_ksc5601[] = {
199 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
200 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
202 static const SuffixCharset sufch_gb2312[] = {
203 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
204 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
206 static const SuffixCharset sufch_big5[] = {
207 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
208 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
210 static const SuffixCharset sufch_unicode[] = {
211 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
212 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
214 static const SuffixCharset sufch_iso10646[] = {
215 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
216 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
218 static const SuffixCharset sufch_dec[] = {
219 { "dectech", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
220 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
222 /* Each of these must be matched explicitly */
223 static const SuffixCharset sufch_any[] = {
224 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
225 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
231 const SuffixCharset* sufch;
232 const struct __fet* next;
233 } fontEncodingTemplate;
235 /* Note: we can attach additional encoding mappings to the end
236 * of this table at runtime.
238 static const fontEncodingTemplate fETTable[] = {
239 { "ansi", sufch_ansi, &fETTable[1] },
240 { "ascii", sufch_ansi, &fETTable[2] },
241 { "iso646.1991", sufch_iso646, &fETTable[3] },
242 { "iso8859", sufch_iso8859, &fETTable[4] },
243 { "microsoft", sufch_microsoft, &fETTable[5] },
244 { "tcvn", sufch_tcvn, &fETTable[6] },
245 { "tis620.2533", sufch_tis620, &fETTable[7] },
246 { "viscii1.1", sufch_viscii, &fETTable[8] },
247 { "windows", sufch_windows, &fETTable[9] },
248 { "koi8", sufch_koi8, &fETTable[10]},
249 { "jisx0201.1976",sufch_jisx0201, &fETTable[11]},
250 { "jisc6226.1978",sufch_jisx0208, &fETTable[12]},
251 { "jisx0208.1983",sufch_jisx0208, &fETTable[13]},
252 { "jisx0208.1990",sufch_jisx0208, &fETTable[14]},
253 { "jisx0212.1990",sufch_jisx0212, &fETTable[15]},
254 { "ksc5601.1987", sufch_ksc5601, &fETTable[16]},
255 { "gb2312.1980", sufch_gb2312, &fETTable[17]},
256 { "big5", sufch_big5, &fETTable[18]},
257 { "unicode", sufch_unicode, &fETTable[19]},
258 { "iso10646", sufch_iso10646, &fETTable[20]},
259 { "cp", sufch_windows, &fETTable[21]},
260 { "dec", sufch_dec, &fETTable[22]},
261 /* NULL prefix matches anything so put it last */
262 { NULL, sufch_any, NULL },
265 /* a charset database for known facenames */
266 struct CharsetBindingInfo
268 const char* pszFaceName;
271 static const struct CharsetBindingInfo charsetbindings[] =
273 /* special facenames */
274 { "System", DEFAULT_CHARSET },
275 { "FixedSys", DEFAULT_CHARSET },
277 /* known facenames */
278 { "MS Serif", ANSI_CHARSET },
279 { "MS Sans Serif", ANSI_CHARSET },
280 { "Courier", ANSI_CHARSET },
281 { "Symbol", SYMBOL_CHARSET },
283 { "Arial", ANSI_CHARSET },
284 { "Arial Greek", GREEK_CHARSET },
285 { "Arial Tur", TURKISH_CHARSET },
286 { "Arial Baltic", BALTIC_CHARSET },
287 { "Arial CE", EASTEUROPE_CHARSET },
288 { "Arial Cyr", RUSSIAN_CHARSET },
289 { "Courier New", ANSI_CHARSET },
290 { "Courier New Greek", GREEK_CHARSET },
291 { "Courier New Tur", TURKISH_CHARSET },
292 { "Courier New Baltic", BALTIC_CHARSET },
293 { "Courier New CE", EASTEUROPE_CHARSET },
294 { "Courier New Cyr", RUSSIAN_CHARSET },
295 { "Times New Roman", ANSI_CHARSET },
296 { "Times New Roman Greek", GREEK_CHARSET },
297 { "Times New Roman Tur", TURKISH_CHARSET },
298 { "Times New Roman Baltic", BALTIC_CHARSET },
299 { "Times New Roman CE", EASTEUROPE_CHARSET },
300 { "Times New Roman Cyr", RUSSIAN_CHARSET },
302 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
303 SHIFTJIS_CHARSET }, /* MS gothic */
304 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
305 SHIFTJIS_CHARSET }, /* MS P gothic */
306 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
307 SHIFTJIS_CHARSET }, /* MS mincho */
308 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
309 SHIFTJIS_CHARSET }, /* MS P mincho */
310 { "GulimChe", HANGEUL_CHARSET },
311 { "MS Song", GB2312_CHARSET },
312 { "MS Hei", GB2312_CHARSET },
313 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
314 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
320 static int DefResolution = 0;
322 static CRITICAL_SECTION crtsc_fonts_X11;
323 static CRITICAL_SECTION_DEBUG critsect_debug =
325 0, 0, &crtsc_fonts_X11,
326 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
327 0, 0, { (DWORD_PTR)(__FILE__ ": crtsc_fonts_X11") }
329 static CRITICAL_SECTION crtsc_fonts_X11 = { &critsect_debug, -1, 0, 0, 0, 0 };
331 static fontResource* fontList = NULL;
332 static fontObject* fontCache = NULL; /* array */
333 static unsigned int fontCacheSize = FONTCACHE;
334 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
336 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
337 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
338 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
340 /***********************************************************************
341 * Helper macros from X distribution
344 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
345 (((cs)->rbearing|(cs)->lbearing| \
346 (cs)->ascent|(cs)->descent) == 0))
348 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
351 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
352 if (fs->per_char == NULL) { \
353 cs = &fs->min_bounds; \
355 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
356 if (CI_NONEXISTCHAR(cs)) cs = def; \
361 #define CI_GET_DEFAULT_INFO(fs,cs) \
362 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
365 /***********************************************************************
368 inline static BOOL is_stock_font( HFONT font )
371 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
373 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
379 static void FONT_LogFontWTo16( const LOGFONTW* font32, LPLOGFONT16 font16 )
381 font16->lfHeight = font32->lfHeight;
382 font16->lfWidth = font32->lfWidth;
383 font16->lfEscapement = font32->lfEscapement;
384 font16->lfOrientation = font32->lfOrientation;
385 font16->lfWeight = font32->lfWeight;
386 font16->lfItalic = font32->lfItalic;
387 font16->lfUnderline = font32->lfUnderline;
388 font16->lfStrikeOut = font32->lfStrikeOut;
389 font16->lfCharSet = font32->lfCharSet;
390 font16->lfOutPrecision = font32->lfOutPrecision;
391 font16->lfClipPrecision = font32->lfClipPrecision;
392 font16->lfQuality = font32->lfQuality;
393 font16->lfPitchAndFamily = font32->lfPitchAndFamily;
394 WideCharToMultiByte( CP_ACP, 0, font32->lfFaceName, -1,
395 font16->lfFaceName, LF_FACESIZE, NULL, NULL );
396 font16->lfFaceName[LF_FACESIZE-1] = 0;
400 /***********************************************************************
403 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
405 CHAR font[LF_FACESIZE];
411 for (i = 0; i < 9; i++) checksum ^= *ptr++;
412 for (i = 0; i < LF_FACESIZE; i++)
414 font[i] = tolower(plf->lfFaceName[i]);
415 if (!font[i] || font[i] == ' ') break;
417 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
421 static UINT16 __genericCheckSum( const void *ptr, int size )
423 unsigned int checksum = 0;
424 const char *p = (const char *)ptr;
426 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
428 return checksum & 0xffff;
431 /*************************************************************************
432 * LFD parse/compose routines
434 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
437 * These functions also do TILDE to HYPHEN conversion
439 static BOOL LFD_Parse(LPSTR lpFont, LFD *lfd)
441 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
445 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
449 field_start = ++lpch;
450 for( i = 0; i < LFD_FIELDS; )
455 lfd_fld[i] = field_start;
457 field_start = ++lpch;
461 lfd_fld[i] = field_start;
465 else if (*lpch == TILDE)
473 /* Fill in the empty fields with NULLS */
474 for (; i< LFD_FIELDS; i++)
477 WARN("Extra ignored in font '%s'\n", lpFont);
479 lfd->foundry = lfd_fld[0];
480 lfd->family = lfd_fld[1];
481 lfd->weight = lfd_fld[2];
482 lfd->slant = lfd_fld[3];
483 lfd->set_width = lfd_fld[4];
484 lfd->add_style = lfd_fld[5];
485 lfd->pixel_size = lfd_fld[6];
486 lfd->point_size = lfd_fld[7];
487 lfd->resolution_x = lfd_fld[8];
488 lfd->resolution_y = lfd_fld[9];
489 lfd->spacing = lfd_fld[10];
490 lfd->average_width = lfd_fld[11];
491 lfd->charset_registry = lfd_fld[12];
492 lfd->charset_encoding = lfd_fld[13];
497 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
499 const char* lfd_fld[LFD_FIELDS];
503 return; /* Don't be silly */
505 lfd_fld[0] = lfd->foundry;
506 lfd_fld[1] = lfd->family;
507 lfd_fld[2] = lfd->weight ;
508 lfd_fld[3] = lfd->slant ;
509 lfd_fld[4] = lfd->set_width ;
510 lfd_fld[5] = lfd->add_style;
511 lfd_fld[6] = lfd->pixel_size;
512 lfd_fld[7] = lfd->point_size;
513 lfd_fld[8] = lfd->resolution_x;
514 lfd_fld[9] = lfd->resolution_y ;
515 lfd_fld[10] = lfd->spacing ;
516 lfd_fld[11] = lfd->average_width ;
517 lfd_fld[12] = lfd->charset_registry ;
518 lfd_fld[13] = lfd->charset_encoding ;
520 buf_size--; /* Room for the terminator */
522 for (i = 0; i < LFD_FIELDS; i++)
524 const char* sp = lfd_fld[i];
525 if (!sp || !buf_size)
530 while (buf_size > 0 && *sp)
532 *dp = (*sp == HYPHEN) ? TILDE : *sp;
541 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
543 int j = strlen(lpStr);
544 if( j == 1 && *lpStr == '0')
545 fi->fi_flags |= FI_POLYWEIGHT;
548 if( !strcasecmp( "bold", lpStr) )
549 fi->df.dfWeight = FW_BOLD;
550 else if( !strcasecmp( "demi", lpStr) )
552 fi->fi_flags |= FI_FW_DEMI;
553 fi->df.dfWeight = FW_DEMIBOLD;
555 else if( !strcasecmp( "book", lpStr) )
557 fi->fi_flags |= FI_FW_BOOK;
558 fi->df.dfWeight = FW_REGULAR;
563 if( !strcasecmp( "light", lpStr) )
564 fi->df.dfWeight = FW_LIGHT;
565 else if( !strcasecmp( "black", lpStr) )
566 fi->df.dfWeight = FW_BLACK;
568 else if( j == 6 && !strcasecmp( "medium", lpStr) )
569 fi->df.dfWeight = FW_REGULAR;
570 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
571 fi->df.dfWeight = FW_DEMIBOLD;
573 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
574 * from the weight property */
577 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
579 int l = strlen(lpStr);
582 switch( tolower( *lpStr ) )
584 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
586 case 'r': fi->df.dfItalic = 0;
589 fi->fi_flags |= FI_OBLIQUE;
590 case 'i': fi->df.dfItalic = 1;
598 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
600 int j = strlen(lpstr);
601 if( j > 3 ) /* find out is there "sans" or "script" */
605 if( strstr(lpstr, "sans") )
607 fi->df.dfPitchAndFamily |= FF_SWISS;
610 if( strstr(lpstr, "script") )
612 fi->df.dfPitchAndFamily |= FF_SCRIPT;
615 if( !j && dec_style_check )
616 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
620 /*************************************************************************
625 * Fill in some fields in the fontInfo struct.
627 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
629 int i, j, dec_style_check, scalability;
630 const fontEncodingTemplate* boba;
631 const char* ridiculous = "font '%s' has ridiculous %s\n";
634 if (!lfd->charset_registry)
636 WARN("font '%s' does not have enough fields\n", fullname);
640 memset(fi, 0, sizeof(fontInfo) );
643 LFD_GetWeight( fi, lfd->weight);
646 dec_style_check = LFD_GetSlant( fi, lfd->slant);
649 lpstr = lfd->set_width;
650 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
651 dec_style_check = TRUE;
653 fi->fi_flags |= FI_NORMAL;
656 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
658 /* pixel & decipoint height, and res_x & y */
662 j = strlen(lfd->pixel_size);
663 if( j == 0 || j > 3 )
665 WARN(ridiculous, fullname, "pixel_size");
668 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
671 j = strlen(lfd->point_size);
672 if( j == 0 || j > 3 )
674 WARN(ridiculous, fullname, "point_size");
677 if( !(atoi(lfd->point_size)) )
680 j = strlen(lfd->resolution_x);
681 if( j == 0 || j > 3 )
683 WARN(ridiculous, fullname, "resolution_x");
686 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
689 j = strlen(lfd->resolution_y);
690 if( j == 0 || j > 3 )
692 WARN(ridiculous, fullname, "resolution_y");
695 if( !(atoi(lfd->resolution_y)) )
698 /* Check scalability */
703 case 4: /* Scalable */
704 fi->fi_flags |= FI_SCALABLE;
707 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
708 TRACE("Skipping scalable bitmap '%s'\n", fullname);
711 WARN("Font '%s' has weird scalability\n", fullname);
716 lpstr = lfd->spacing;
720 WARN("font '%s' has no spacing\n", fullname);
723 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
725 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
726 fi->fi_flags |= FI_FIXEDEX;
728 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
731 /* Of course this line does nothing */
732 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
735 /* average width - */
736 lpstr = lfd->average_width;
738 if( j == 0 || j > 3 )
740 WARN(ridiculous, fullname, "average_width");
744 if( !(atoi(lpstr)) && !scalability )
746 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
750 /* charset registry, charset encoding - */
751 lpstr = lfd->charset_registry;
752 if( strstr(lpstr, "ksc") ||
753 strstr(lpstr, "gb2312") ||
754 strstr(lpstr, "big5") )
756 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
759 fi->df.dfCharSet = ANSI_CHARSET;
761 for( i = 0, boba = &fETTable[0]; boba; boba = boba->next, i++ )
763 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
765 if (lfd->charset_encoding)
767 for( j = 0; boba->sufch[j].psuffix; j++ )
769 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
771 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
772 fi->internal_charset = boba->sufch[j].charset;
773 fi->codepage = boba->sufch[j].codepage;
774 fi->cptable = boba->sufch[j].cptable;
779 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
780 fi->internal_charset = boba->sufch[j].charset;
781 fi->codepage = boba->sufch[j].codepage;
782 fi->cptable = boba->sufch[j].cptable;
785 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
786 fullname, lfd->charset_encoding, boba->prefix);
791 FIXME("font '%s' has unknown registry '%s' and character encoding '%s'\n",
792 fullname, lfd->charset_registry, lfd->charset_encoding);
796 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
797 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
800 else if (boba->prefix)
802 WARN("font '%s' has known registry '%s' and no character encoding\n",
804 for( j = 0; boba->sufch[j].psuffix; j++ )
806 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
807 fi->internal_charset = boba->sufch[j].charset;
808 fi->codepage = boba->sufch[j].codepage;
809 fi->cptable = boba->sufch[j].cptable;
815 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
819 /* i - index into fETTable
820 * j - index into suffix array for fETTable[i]
822 * 254 - found encoding prefix, unknown suffix
823 * 255 - no encoding match at all.
825 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
831 /*************************************************************************
834 * make a matrix suitable for LFD based on theta radians
836 static void LFD_AngleMatrix( char* buffer, int h, double theta)
839 matrix[0] = h*cos(theta);
840 matrix[1] = h*sin(theta);
841 matrix[2] = -h*sin(theta);
842 matrix[3] = h*cos(theta);
843 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
846 /*************************************************************************
849 * Note: uRelax is a treatment not a cure. Font mapping algorithm
850 * should be bulletproof enough to allow us to avoid hacks like
851 * this despite LFD being so braindead.
853 static BOOL LFD_ComposeLFD( const fontObject* fo,
854 INT height, LPSTR lpLFD, UINT uRelax )
857 const char *any = "*";
858 char h_string[64], resx_string[64], resy_string[64];
861 /* Get the worst case over with first */
863 /* RealizeFont() will call us repeatedly with increasing uRelax
864 * until XLoadFont() succeeds.
865 * to avoid an infinite loop; these will always match
870 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
872 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
876 /* read foundry + family from fo */
877 aLFD.foundry = fo->fr->resource->foundry;
878 aLFD.family = fo->fr->resource->family;
881 switch( fo->fi->df.dfWeight )
884 aLFD.weight = "bold"; break;
886 if( fo->fi->fi_flags & FI_FW_BOOK )
887 aLFD.weight = "book";
889 aLFD.weight = "medium";
892 aLFD.weight = "demi";
893 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
894 aLFD.weight = "bold";
897 aLFD.weight = "black"; break;
899 aLFD.weight = "light"; break;
905 if( fo->fi->df.dfItalic )
906 if( fo->fi->fi_flags & FI_OBLIQUE )
911 aLFD.slant = (uRelax <= 1) ? "r" : any;
914 if( fo->fi->fi_flags & FI_NORMAL )
915 aLFD.set_width = "normal";
917 aLFD.set_width = any;
920 aLFD.add_style = any;
924 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
926 if( fo->fo_flags & FO_SYNTH_HEIGHT )
927 h = fo->fi->lfd_height;
930 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
931 h /= fo->fi->df.dfPixHeight;
933 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
935 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
937 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
942 /* handle rotated fonts */
943 if (fo->lf.lfEscapement) {
944 /* escapement is in tenths of degrees, theta is in radians */
945 double theta = M_PI*fo->lf.lfEscapement/1800.;
946 LFD_AngleMatrix(h_string, h, theta);
950 sprintf(h_string, "%d", h);
953 sprintf(h_string, "%d", fo->fi->lfd_height);
955 aLFD.pixel_size = h_string;
956 aLFD.point_size = any;
958 /* resolution_x,y, average width */
959 /* FIXME - Why do some font servers ignore average width ?
960 * so that you have to mess around with res_y
962 aLFD.average_width = any;
965 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
966 aLFD.resolution_x = resx_string;
968 strcpy(resy_string, resx_string);
969 if( uRelax == 0 && text_caps & TC_SF_X_YINDEP )
971 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
973 int resy = 0.5 + fo->fi->lfd_resolution *
974 (fo->fi->df.dfAvgWidth * height) /
975 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
976 sprintf(resy_string, "%d", resy);
980 /* FIXME - synth width */
983 aLFD.resolution_y = resy_string;
987 aLFD.resolution_x = aLFD.resolution_y = any;
994 if( fo->fi->fi_flags & FI_FIXEDPITCH )
995 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
997 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
999 aLFD.spacing = (uRelax <= 2) ? w : any;
1006 const fontEncodingTemplate* boba = &fETTable[0];
1008 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
1009 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
1011 i = fo->fi->fi_encoding & 255;
1015 aLFD.charset_encoding = boba->sufch[i].psuffix;
1019 aLFD.charset_encoding = any;
1022 case 255: /* no suffix - it ends eg "-ascii" */
1023 aLFD.charset_encoding = NULL;
1029 aLFD.charset_registry = any;
1030 aLFD.charset_encoding = any;
1033 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1035 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1040 /***********************************************************************
1043 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1044 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1046 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1047 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1049 unsigned long height;
1050 unsigned min = (unsigned char)pFI->dfFirstChar;
1051 unsigned max = (unsigned char)pFI->dfLastChar;
1052 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1058 if(XGetFontProperty((XFontStruct*)x_fs, x11drv_atom(RAW_CAP_HEIGHT), &height))
1059 *pIL = XFT->ascent -
1060 (INT)(XFT->pixelsize / 1000.0 * height);
1063 wine_tsx11_unlock();
1068 if( XGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1070 if( x_fs->per_char )
1071 if( bIsLatin && ((unsigned char)'X' <= (max - min)) )
1072 height = x_fs->per_char['X' - min].ascent;
1074 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1075 height = x_fs->max_bounds.ascent;
1078 height = x_fs->ascent;
1080 *pEL = x_fs->max_bounds.ascent - height;
1083 height = x_fs->min_bounds.ascent;
1085 wine_tsx11_unlock();
1087 *pIL = x_fs->ascent - height;
1090 /***********************************************************************
1093 static int XFONT_CharWidth(const XFontStruct* x_fs,
1094 const XFONTTRANS *XFT, int ch)
1097 return x_fs->per_char[ch].width;
1099 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1102 /***********************************************************************
1103 * XFONT_GetAvgCharWidth
1105 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1106 const XFONTTRANS *XFT)
1108 unsigned min = (unsigned char)pFI->dfFirstChar;
1109 unsigned max = (unsigned char)pFI->dfLastChar;
1113 if( x_fs->per_char )
1115 unsigned int width = 0, chars = 0, j;
1116 if( (IS_LATIN_CHARSET(pFI->dfCharSet) ||
1117 pFI->dfCharSet == DEFAULT_CHARSET) &&
1118 (max - min) >= (unsigned char)'z' )
1120 /* FIXME - should use a weighted average */
1121 for( j = 0; j < 26; j++ )
1122 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1123 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1126 else /* unweighted average of everything */
1128 for( j = 0, max -= min; j <= max; j++ )
1129 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1131 width += XFONT_CharWidth(x_fs, XFT, j);
1135 if (chars) avg = (width + (chars-1))/ chars; /* always round up*/
1136 else avg = 0; /* No characters exist at all */
1138 else /* uniform width */
1139 avg = x_fs->min_bounds.width;
1141 TRACE(" retuning %d\n",avg);
1145 /***********************************************************************
1146 * XFONT_GetMaxCharWidth
1148 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1150 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1151 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1152 unsigned int maxwidth, j;
1154 if(!XFT || !xfs->per_char)
1155 return abs(xfs->max_bounds.width);
1157 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1158 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1159 if(maxwidth < xfs->per_char[j].attributes)
1160 maxwidth = xfs->per_char[j].attributes;
1162 maxwidth *= XFT->pixelsize / 1000.0;
1166 /***********************************************************************
1167 * XFONT_SetFontMetric
1171 * Initializes IFONTINFO16.
1173 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1176 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1177 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1179 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1180 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1182 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1183 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1185 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1186 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1187 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1189 if( xfs->min_bounds.width != xfs->max_bounds.width )
1190 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1191 if( fi->fi_flags & FI_SCALABLE )
1193 fi->df.dfType = DEVICE_FONTTYPE;
1194 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1196 else if( fi->fi_flags & FI_TRUETYPE )
1197 fi->df.dfType = TRUETYPE_FONTTYPE;
1199 fi->df.dfType = RASTER_FONTTYPE;
1201 fi->df.dfFace = fr->lfFaceName;
1204 /***********************************************************************
1205 * XFONT_GetFontMetric
1207 * Retrieve font metric info (enumeration).
1209 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1210 LPENUMLOGFONTEXW pLF,
1211 NEWTEXTMETRICEXW *pTM )
1213 memset( pLF, 0, sizeof(*pLF) );
1214 memset( pTM, 0, sizeof(*pTM) );
1216 #define plf ((LPLOGFONTW)pLF)
1217 #define ptm ((LPNEWTEXTMETRICW)pTM)
1218 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1219 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1220 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1221 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1222 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1223 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1224 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1226 /* convert pitch values */
1228 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1229 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1231 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1232 plf->lfFaceName, LF_FACESIZE);
1234 /* FIXME: fill in rest of plF values */
1235 strcpyW(pLF->elfFullName, plf->lfFaceName);
1236 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1237 pLF->elfStyle, LF_FACESIZE);
1238 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1239 "Symbol" : "Roman", -1,
1240 pLF->elfScript, LF_FACESIZE);
1244 ptm->tmAscent = pfi->df.dfAscent;
1245 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1246 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1247 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1248 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1249 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1251 ptm->tmFirstChar = pfi->df.dfFirstChar;
1252 ptm->tmLastChar = pfi->df.dfLastChar;
1253 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1254 ptm->tmBreakChar = pfi->df.dfBreakChar;
1256 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1257 debugstr_w(pLF->elfLogFont.lfFaceName),
1258 debugstr_w(pLF->elfFullName));
1260 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1261 /* return font type */
1262 return pfi->df.dfType;
1267 /***********************************************************************
1272 * dfPitchAndFamily flags for some common typefaces.
1274 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1276 switch( lfFaceName[0] )
1279 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1283 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1287 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1290 if (!strcasecmp(lfFaceName, "Charter") )
1294 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1298 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1302 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1306 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1307 return FF_DECORATIVE;
1312 /***********************************************************************
1313 * XFONT_SameFoundryAndFamily
1317 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1319 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1320 !strcasecmp( lfd1->family, lfd2->family ) );
1323 /***********************************************************************
1324 * XFONT_InitialCapitals
1328 * Upper case first letters of words & remove multiple spaces
1330 static void XFONT_InitialCapitals(LPSTR lpch)
1336 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1338 if( isspace(*lpch) )
1340 if (!up) /* Not already got one */
1346 else if( isalpha(*lpch) && up )
1348 *lpstr++ = toupper(*lpch);
1358 /* Remove possible trailing space */
1365 /***********************************************************************
1366 * XFONT_WindowsNames
1370 * Build generic Windows aliases for X font names.
1372 * -misc-fixed- -> "Fixed"
1373 * -sony-fixed- -> "Sony Fixed", etc...
1375 static void XFONT_WindowsNames(void)
1379 for( fr = fontList; fr ; fr = fr->next )
1384 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1386 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1387 if( pfr->fr_flags & FR_NAMESET )
1389 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1393 lpch = fr->lfFaceName;
1394 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1395 /* prepend vendor name */
1396 (pfr==fr) ? "" : fr->resource->foundry,
1397 fr->resource->family);
1398 XFONT_InitialCapitals(fr->lfFaceName);
1400 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1404 for( fi = fr->fi ; fi ; fi = fi->next )
1405 fi->df.dfPitchAndFamily |= bFamilyStyle;
1409 TRACE("typeface '%s'\n", fr->lfFaceName);
1411 fr->fr_flags |= FR_NAMESET;
1415 /***********************************************************************
1416 * XFONT_LoadDefaultLFD
1418 * Move lfd to the head of fontList to make it more likely to be matched
1420 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1423 fontResource *fr, *pfr;
1424 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1426 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1430 fr->next = pfr->next;
1431 pfr->next = fontList;
1439 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1440 lfd->foundry, lfd->family);
1444 /***********************************************************************
1447 static void XFONT_LoadDefault( HKEY hkey, LPCSTR ini, LPCSTR fonttype)
1449 char buffer[MAX_LFD_LENGTH];
1450 DWORD type, count = sizeof(buffer);
1453 RegQueryValueExA(hkey, ini, 0, &type, (LPBYTE)buffer, &count);
1459 while( *pch && isspace(*pch) ) pch++;
1461 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1462 if (LFD_Parse(pch, &lfd) && lfd.foundry && lfd.family)
1463 XFONT_LoadDefaultLFD(&lfd, fonttype);
1465 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1469 /***********************************************************************
1470 * XFONT_LoadDefaults
1472 static void XFONT_LoadDefaults( HKEY hkey )
1474 XFONT_LoadDefault( hkey, INIDefaultFixed, "fixed ");
1475 XFONT_LoadDefault( hkey, INIDefault, "");
1478 /***********************************************************************
1481 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1484 fontAlias *pfa, *prev = NULL;
1486 for(pfa = aliasTable; pfa; pfa = pfa->next)
1488 /* check if we already got one */
1489 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1491 TRACE("redundant alias '%s' -> '%s'\n",
1492 lpAlias, lpTypeFace );
1498 j = strlen(lpTypeFace) + 1;
1499 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1500 j + strlen(lpAlias) + 1 );
1509 pfa->faTypeFace = (LPSTR)(pfa + 1);
1510 strcpy( pfa->faTypeFace, lpTypeFace );
1511 pfa->faAlias = pfa->faTypeFace + j;
1512 strcpy( pfa->faAlias, lpAlias );
1514 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1522 /***********************************************************************
1525 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1527 fontResource *fr, *frMatch = NULL;
1528 if (!lfd->foundry || ! lfd->family)
1530 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1533 for (fr = fontList; fr ; fr = fr->next)
1535 if(!strcasecmp(fr->resource->family, lpAlias))
1537 /* alias is not needed since the real font is present */
1538 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1541 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1552 fontAlias *pfa, *prev = NULL;
1554 for(pfa = aliasTable; pfa; pfa = pfa->next)
1556 /* Remove lpAlias from aliasTable - we should free the old entry */
1557 if(!strcmp(lpAlias, pfa->faAlias))
1560 prev->next = pfa->next;
1562 aliasTable = pfa->next;
1565 /* Update any references to the substituted font in aliasTable */
1566 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1568 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1569 strcpy( pfa->faTypeFace, lpAlias );
1574 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1576 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1577 frMatch->fr_flags |= FR_NAMESET;
1581 /* create new entry in the alias table */
1582 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1587 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1591 /***********************************************************************
1592 * Just a copy of PROFILE_GetStringItem
1594 * Convenience function that turns a string 'xxx, yyy, zzz' into
1595 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1597 static char *XFONT_GetStringItem( char *start )
1599 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1602 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1606 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1608 if( !XFONT_isspace(*lpchX) ) return lpchX;
1610 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1613 if( lpch ) *lpch = '\0';
1615 #undef XFONT_isspace
1618 /***********************************************************************
1623 * Create font aliases for some standard windows fonts using user's
1624 * default choice of (sans-)serif fonts
1626 * Read user-defined aliases from config file. Format is as follows
1628 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1631 * Alias0 = Arial, -adobe-helvetica-
1632 * Alias1 = Times New Roman, -bitstream-courier-, 1
1635 * Note that from 970817 and on we have built-in alias templates that take
1636 * care of the necessary Windows typefaces.
1644 static void XFONT_LoadAliases( HKEY hkey )
1647 char buffer[MAX_LFD_LENGTH];
1651 /* built-ins first */
1652 strcpy(buffer, "-bitstream-charter-");
1655 DWORD type, count = sizeof(buffer);
1656 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, (LPBYTE)buffer, &count);
1658 TRACE("Using '%s' as default serif font\n", buffer);
1659 if (LFD_Parse(buffer, &lfd))
1661 /* NB XFONT_InitialCapitals should not change these standard aliases */
1662 XFONT_LoadAlias( &lfd, "Tms Roman", FALSE);
1663 XFONT_LoadAlias( &lfd, "MS Serif", FALSE);
1664 XFONT_LoadAlias( &lfd, "Times New Roman", FALSE);
1666 XFONT_LoadDefaultLFD( &lfd, "serif ");
1669 strcpy(buffer, "-adobe-helvetica-");
1672 DWORD type, count = sizeof(buffer);
1673 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, (LPBYTE)buffer, &count);
1675 TRACE("Using '%s' as default sans serif font\n", buffer);
1676 if (LFD_Parse(buffer, &lfd))
1678 XFONT_LoadAlias( &lfd, "Helv", FALSE);
1679 XFONT_LoadAlias( &lfd, "MS Sans Serif", FALSE);
1680 XFONT_LoadAlias( &lfd, "MS Shell Dlg", FALSE);
1681 XFONT_LoadAlias( &lfd, "System", FALSE);
1682 XFONT_LoadAlias( &lfd, "Arial", FALSE);
1684 XFONT_LoadDefaultLFD( &lfd, "sans serif ");
1687 /* then user specified aliases */
1691 char subsection[32];
1692 snprintf( subsection, sizeof(subsection), "%s%i", INIAliasSection, i++ );
1697 DWORD type, count = sizeof(buffer);
1698 RegQueryValueExA(hkey, subsection, 0, &type, (LPBYTE)buffer, &count);
1704 XFONT_InitialCapitals(buffer);
1705 lpResource = XFONT_GetStringItem( buffer );
1706 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1707 if( lpResource && *lpResource )
1709 if (LFD_Parse(lpResource, &lfd)) XFONT_LoadAlias(&lfd, buffer, bSubst);
1712 WARN("malformed font alias '%s'\n", buffer );
1717 /***********************************************************************
1720 * Convert an (potential) alias into a real windows name
1723 static LPCSTR XFONT_UnAlias(char* font)
1728 XFONT_InitialCapitals(font); /* to remove extra white space */
1730 for( fa = aliasTable; fa; fa = fa->next )
1731 /* use case insensitive matching to handle eg "MS Sans Serif" */
1732 if( !strcasecmp( fa->faAlias, font ) )
1734 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1735 strcpy(font, fa->faTypeFace);
1742 /***********************************************************************
1743 * XFONT_RemoveFontResource
1745 * Caller should check if the font resource is in use. If it is it should
1746 * set FR_REMOVED flag to delay removal until the resource is not in use
1749 static void XFONT_RemoveFontResource( fontResource** ppfr )
1751 fontResource* pfr = *ppfr;
1755 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1758 pfi = pfr->fi->next;
1759 HeapFree( GetProcessHeap(), 0, pfr->fi );
1762 HeapFree( GetProcessHeap(), 0, pfr );
1767 /***********************************************************************
1772 * Removes specified fonts from the font table to prevent Wine from
1775 * Ignore# = [LFD font name]
1778 * Ignore0 = -misc-nil-
1779 * Ignore1 = -sun-open look glyph-
1783 static void XFONT_LoadIgnore(char* lfdname)
1785 fontResource** ppfr;
1788 if (LFD_Parse(lfdname, &lfd) && lfd.foundry && lfd.family)
1790 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1792 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, &lfd) )
1794 TRACE("Ignoring '-%s-%s-'\n",
1795 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1797 XFONT_RemoveFontResource( ppfr );
1803 WARN("Malformed font resource\n");
1806 static void XFONT_LoadIgnores( HKEY hkey )
1809 char subsection[32];
1810 char buffer[MAX_LFD_LENGTH];
1812 /* Standard one that no one wants */
1813 strcpy(buffer, "-misc-nil-");
1814 XFONT_LoadIgnore(buffer);
1816 /* Others from INI file */
1820 DWORD type, count = sizeof(buffer);
1821 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1823 if (!RegQueryValueExA(hkey, subsection, 0, &type, (LPBYTE)buffer, &count))
1826 while( *pch && isspace(*pch) ) pch++;
1827 XFONT_LoadIgnore(pch);
1835 /***********************************************************************
1836 * XFONT_UserMetricsCache
1838 * Returns expanded name for the cachedmetrics file.
1839 * Now it also appends the current value of the $DISPLAY variable.
1841 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1843 const char *confdir = wine_get_config_dir();
1844 const char *display_name = XDisplayName(NULL);
1845 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1851 ** Normalize the display name, since on Red Hat systems, DISPLAY
1852 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1853 ** after this code, all of the above will resolve to ':0.0'.
1855 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1856 p = strchr(display_name, ':');
1857 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1859 if ((len > *buf_size) &&
1860 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1862 ERR("out of memory\n");
1865 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1867 ext = buffer + strlen(buffer);
1868 strcpy( ext, display_name );
1870 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1871 sprintf( p, ":%d.%d", display, screen );
1876 /***********************************************************************
1879 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1881 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1885 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1887 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1888 m = (const BYTE*)&fi->df.dfPixWidth - (const BYTE*)&fi->df.dfItalic;
1889 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1891 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1892 && fi->lfd_height != match->lfd_height) ||
1893 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1894 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1896 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1897 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1899 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1900 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1901 else if( m >= 0 ) return 1; /* 'match' is better */
1903 return -1; /* 'fi' is better */
1906 /***********************************************************************
1909 * REMOVE_SUBSETS - attach new fi and purge subsets
1910 * UNMARK_SUBSETS - remove subset flags from all fi entries
1912 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1915 fontInfo* pfi, *prev;
1917 for( prev = NULL, pfi = fr->fi; pfi; )
1919 if( action == REMOVE_SUBSETS )
1921 if( pfi->fi_flags & FI_SUBSET )
1923 fontInfo* subset = pfi;
1927 if( prev ) prev->next = pfi = pfi->next;
1928 else fr->fi = pfi = pfi->next;
1929 HeapFree( GetProcessHeap(), 0, subset );
1933 else pfi->fi_flags &= ~FI_SUBSET;
1939 if( action == REMOVE_SUBSETS ) /* also add the superset */
1941 if( fi->fi_flags & FI_SCALABLE )
1946 else if( prev ) prev->next = fi; else fr->fi = fi;
1950 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1953 /***********************************************************************
1956 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1960 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1963 /* Give the app back the font name it asked for. Encarta checks this. */
1964 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1968 /***********************************************************************
1969 * XFONT_FixupPointSize
1971 static void XFONT_FixupPointSize(fontInfo* fi)
1974 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1975 df.dfPoints = (INT16)
1976 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1981 /***********************************************************************
1982 * XFONT_BuildMetrics
1984 * Build font metrics from X font
1986 static int XLoadQueryFont_ErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
1991 /* XLoadQueryFont has the bad habit of crashing when loading a bad font... */
1992 static XFontStruct *safe_XLoadQueryFont(Display *display, char *name)
1996 X11DRV_expect_error(display, XLoadQueryFont_ErrorHandler, NULL);
1997 ret = XLoadQueryFont(display, name);
1998 if (X11DRV_check_error()) ret = NULL;
2003 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
2006 fontInfo* fi = NULL;
2007 fontResource* fr, *pfr;
2010 MESSAGE("Building font metrics. This may take some time...\n");
2011 for( i = 0; i < x_count; i++ )
2016 char buffer[MAX_LFD_LENGTH];
2021 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
2022 strcpy( typeface, x_pattern[i] );
2023 if (i % 10 == 0) MESSAGE("Font metrics: %.1f%% done\n", 100.0 * i / x_count);
2025 if (!LFD_Parse(typeface, &lfd))
2027 HeapFree(GetProcessHeap(), 0, typeface);
2031 /* find a family to insert into */
2033 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2035 if( XFONT_SameFoundryAndFamily(fr->resource, &lfd))
2040 if( !fi ) fi = HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2042 if( !LFD_InitFontInfo( fi, &lfd, x_pattern[i]) )
2045 if( !fr ) /* add new family */
2048 fr = HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2051 memset(fr, 0, sizeof(fontResource));
2053 fr->resource = HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2054 memset(fr->resource, 0, sizeof(LFD));
2056 TRACE("family: -%s-%s-\n", lfd.foundry, lfd.family );
2057 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.foundry)+1);
2058 strcpy( (char *)fr->resource->foundry, lfd.foundry );
2059 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.family)+1);
2060 strcpy( (char *)fr->resource->family, lfd.family );
2061 fr->resource->weight = "";
2063 if( pfr ) pfr->next = fr;
2067 WARN("Not enough memory for a new font family\n");
2070 /* check if we already have something better than "fi" */
2072 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2073 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2074 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2075 if( j > 0 ) goto nextfont;
2077 /* add new font instance "fi" to the "fr" font resource */
2079 if( fi->fi_flags & FI_SCALABLE )
2082 char pxl_string[4], res_string[4];
2083 /* set scalable font height to get a basis for extrapolation */
2085 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2086 fi->lfd_resolution = res;
2088 sprintf(pxl_string, "%d", fi->lfd_height);
2089 sprintf(res_string, "%d", fi->lfd_resolution);
2092 lfd1.pixel_size = pxl_string;
2093 lfd1.point_size = "*";
2094 lfd1.resolution_x = res_string;
2095 lfd1.resolution_y = res_string;
2097 LFD_UnParse(buffer, sizeof(buffer), &lfd1);
2101 else lpstr = x_pattern[i];
2103 /* X11 may return an error on some bad fonts... So be prepared to handle these. */
2104 if ((x_fs = safe_XLoadQueryFont(gdi_display, lpstr)) != 0)
2106 XFONT_SetFontMetric( fi, fr, x_fs );
2108 XFreeFont( gdi_display, x_fs );
2109 wine_tsx11_unlock();
2111 XFONT_FixupPointSize(fi);
2113 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2115 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2116 fi = NULL; /* preventing reuse */
2120 ERR("failed to load %s\n", lpstr );
2122 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2125 HeapFree(GetProcessHeap(), 0, typeface);
2127 HeapFree(GetProcessHeap(), 0, fi);
2129 /* Scan through the font list and remove FontResource(s) (fr)
2130 * that have no associated Fontinfo(s) (fi).
2131 * This code is necessary because XFONT_ReadCachedMetrics
2132 * assumes that there is at least one fi associated with a fr.
2133 * This assumption is invalid for TT font
2134 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2139 while (!fr->fi_count)
2141 fontList = fr->next;
2143 HeapFree(GetProcessHeap(), 0, fr->resource);
2144 HeapFree(GetProcessHeap(), 0, fr);
2154 if (!fr->next->fi_count)
2157 fr->next = fr->next->next;
2159 HeapFree(GetProcessHeap(), 0, pfr->resource);
2160 HeapFree(GetProcessHeap(), 0, pfr);
2168 MESSAGE("Font metrics: 100.0%% done\n");
2172 /***********************************************************************
2173 * XFONT_ReadCachedMetrics
2177 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2184 /* read checksums */
2185 read( fd, &u, sizeof(unsigned) );
2186 read( fd, &i, sizeof(int) );
2188 if( u == x_checksum && i == x_count )
2190 off_t length, offset = 3 * sizeof(int);
2192 /* read total size */
2193 read( fd, &i, sizeof(int) );
2194 length = lseek( fd, 0, SEEK_END );
2196 if( length == (i + offset) )
2198 lseek( fd, offset, SEEK_SET );
2199 fontList = HeapAlloc( GetProcessHeap(), 0, i);
2202 fontResource* pfr = fontList;
2203 fontInfo* pfi = NULL;
2205 TRACE("Reading cached font metrics:\n");
2207 read( fd, fontList, i); /* read all metrics at once */
2208 while( offset < length )
2210 offset += sizeof(fontResource) + sizeof(fontInfo);
2211 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2215 if( offset > length ||
2216 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2217 (int)(pfi->next) != j++ )
2219 TRACE("error: offset=%ld length=%ld cptable=%d pfi->next=%d j=%d\n",(long)offset,(long)length,pfi->cptable,(int)pfi->next,j-1);
2223 if( pfi->df.dfPixHeight == 0 )
2225 TRACE("error: dfPixHeight==0\n");
2229 pfi->df.dfFace = pfr->lfFaceName;
2230 if( pfi->fi_flags & FI_SCALABLE )
2232 /* we can pretend we got this font for any resolution */
2233 pfi->lfd_resolution = res;
2234 XFONT_FixupPointSize(pfi);
2236 pfi->next = pfi + 1;
2238 if( j > pfr->fi_count ) break;
2241 offset += sizeof(fontInfo);
2246 pfr->next = (fontResource*)(pfi + 1);
2251 if( pfr->next == NULL &&
2252 *(int*)(pfi + 1) == X_FMC_MAGIC )
2254 /* read LFD stubs */
2255 char* lpch = (char*)((int*)(pfi + 1) + 1);
2256 offset += sizeof(int);
2257 for( pfr = fontList; pfr; pfr = pfr->next )
2259 size_t len = strlen(lpch) + 1;
2260 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2261 pfr->resource = HeapAlloc(GetProcessHeap(),0,sizeof(LFD));
2262 if (!LFD_Parse(lpch, pfr->resource))
2264 HeapFree( GetProcessHeap(), 0, pfr->resource );
2265 pfr->resource = NULL;
2269 if (offset > length)
2271 TRACE("error: offset=%ld length=%ld\n",(long)offset,(long)length);
2280 TRACE("Wrong length: %ld!=%ld\n",(long)length,(long)(i+offset));
2283 TRACE("Checksum (%x vs. %x) or count (%d vs. %d) mismatch\n",
2284 u,x_checksum,i,x_count);
2287 HeapFree( GetProcessHeap(), 0, fontList );
2294 /***********************************************************************
2295 * XFONT_WriteCachedMetrics
2299 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2307 char buffer[MAX_LFD_LENGTH];
2309 /* font metrics file:
2313 * +0008 total size to load
2314 * +000C prepackaged font metrics
2317 * +...x + 4 LFD stubs
2320 write( fd, &x_checksum, sizeof(unsigned) );
2321 write( fd, &x_count, sizeof(int) );
2323 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2325 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2326 i += strlen( buffer) + 1;
2329 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2330 write( fd, &i, sizeof(int) );
2332 TRACE("Writing font cache:\n");
2334 for( pfr = fontList; pfr; pfr = pfr->next )
2338 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2340 i = write( fd, pfr, sizeof(fontResource) );
2341 if( i == sizeof(fontResource) )
2343 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2347 fi.df.dfFace = NULL;
2348 fi.next = (fontInfo*)k; /* loader checks this */
2350 j = write( fd, &fi, sizeof(fi) );
2353 if( j == sizeof(fontInfo) ) continue;
2357 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2359 i = j = X_FMC_MAGIC;
2360 write( fd, &i, sizeof(int) );
2361 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2363 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2364 i = strlen( buffer ) + 1;
2365 j = write( fd, buffer, i );
2374 /***********************************************************************
2375 * XFONT_GetDefResolution()
2379 * Here we initialize DefResolution which is used in the
2380 * XFONT_Match() penalty function, based on the values of log_pixels
2382 static int XFONT_GetDefResolution( int log_pixels_x, int log_pixels_y )
2385 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2386 int best = 0, best_diff = 65536;
2388 /* FIXME We can only really guess at a best DefResolution
2389 * - this should be configurable
2391 for( i = best = 0; i < num; i++ )
2393 j = abs( log_pixels_x - allowed_xfont_resolutions[i] );
2400 DefResolution = allowed_xfont_resolutions[best];
2401 return DefResolution;
2405 /***********************************************************************
2408 * Compute the matching score between the logical font and the device font.
2410 * contributions from highest to lowest:
2414 * family flags (only when the facename is not present)
2416 * weight, italics, underlines, strikeouts
2418 * NOTE: you can experiment with different penalty weights to see what happens.
2419 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2421 static UINT XFONT_Match( fontMatch* pfm )
2423 fontInfo* pfi = pfm->pfi; /* device font to match */
2424 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2426 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from text_caps */
2427 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2430 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2431 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2432 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2433 (pfi->df.dfItalic) ? "Italic" : "" );
2435 pfm->flags &= FO_MATCH_MASK;
2438 /* pfm->internal_charset: given(required) charset */
2439 /* pfi->internal_charset: charset of this font */
2440 if (pfi->internal_charset == DEFAULT_CHARSET)
2442 /* special case(unicode font) */
2443 /* priority: unmatched charset < unicode < matched charset */
2448 if( pfm->internal_charset == DEFAULT_CHARSET )
2451 if (pfi->internal_charset != ANSI_CHARSET)
2454 if ( pfi->codepage != GetACP() )
2457 else if (pfm->internal_charset != pfi->internal_charset)
2459 if ( pfi->internal_charset & 0xff00 )
2460 penalty += 0x1000; /* internal charset - should not be used */
2469 if( plf->lfHeight > 0 )
2471 int h = pfi->df.dfPixHeight;
2472 d = h - plf->lfHeight;
2473 height = plf->lfHeight;
2477 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2480 d = h + plf->lfHeight;
2481 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2485 ERR("PixHeight == InternalLeading\n");
2486 penalty += 0x1000; /* don't want this */
2492 pfm->height = 1; /* Very small */
2496 pfm->height = height;
2497 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2499 if( d > 0 ) /* do not shrink raster fonts */
2501 pfm->height = pfi->df.dfPixHeight;
2502 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2504 else /* expand only in integer multiples */
2506 pfm->height = height - height%pfi->df.dfPixHeight;
2507 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2510 else /* can't be scaled at all */
2512 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2513 pfm->height = pfi->df.dfPixHeight;
2514 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2518 pfm->height = pfi->df.dfPixHeight;
2520 /* Pitch and Family */
2521 if( pfm->flags & FO_MATCH_PAF ) {
2522 int family = plf->lfPitchAndFamily & FF_FAMILY;
2524 /* TMPF_FIXED_PITCH means exactly the opposite */
2525 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2526 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2527 } else /* Variable is the default */
2528 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2530 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2538 if( bR6 || bScale ) h = 0;
2541 /* FIXME: not complete */
2543 pfm->flags |= FO_SYNTH_WIDTH;
2544 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2546 penalty += h * ( d ) ? 0x2 : 0x1 ;
2548 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2551 if( plf->lfWeight != FW_DONTCARE )
2553 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2554 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2555 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2558 if( plf->lfItalic != pfi->df.dfItalic )
2561 pfm->flags |= FO_SYNTH_ITALIC;
2564 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2567 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2570 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2573 TRACE(" returning %i\n", penalty );
2578 /***********************************************************************
2581 * Scan a particular font resource for the best match.
2583 static UINT XFONT_MatchFIList( fontMatch* pfm )
2585 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2586 UINT current_score, score = (UINT)(-1);
2587 fontMatch fm = *pfm;
2589 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2591 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2594 current_score = XFONT_Match( &fm );
2595 if( score > current_score )
2598 score = current_score;
2604 /***********************************************************************
2605 * XFONT_is_ansi_charset
2607 * returns TRUE if the given charset is ANSI_CHARSET.
2609 static BOOL XFONT_is_ansi_charset( UINT charset )
2611 return ((charset == ANSI_CHARSET) ||
2612 (charset == DEFAULT_CHARSET && GetACP() == 1252));
2615 /***********************************************************************
2616 * XFONT_MatchDeviceFont
2618 * Scan font resource tree.
2621 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2623 fontMatch fm = *pfm;
2624 UINT current_score, score = (UINT)(-1);
2625 fontResource** ppfr;
2627 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2628 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2629 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2633 /* the following hard-coded font binding assumes 'ANSI_CHARSET'. */
2634 if( !fm.plf->lfFaceName[0] &&
2635 XFONT_is_ansi_charset(fm.plf->lfCharSet) )
2637 switch(fm.plf->lfPitchAndFamily & 0xf0)
2640 strcpy(fm.plf->lfFaceName, "Courier New");
2643 strcpy(fm.plf->lfFaceName, "Times New Roman");
2646 strcpy(fm.plf->lfFaceName, "Arial");
2649 if((fm.plf->lfPitchAndFamily & 0x0f) == FIXED_PITCH)
2650 strcpy(fm.plf->lfFaceName, "Courier New");
2652 strcpy(fm.plf->lfFaceName, "Arial");
2657 if( fm.plf->lfFaceName[0] )
2659 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2660 if( fm.pfr ) /* match family */
2662 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2664 if( fm.pfr->fr_flags & FR_REMOVED )
2668 XFONT_MatchFIList( &fm );
2675 /* get charset if lfFaceName is one of known facenames. */
2677 const struct CharsetBindingInfo* pcharsetbindings;
2679 pcharsetbindings = &charsetbindings[0];
2680 while ( pcharsetbindings->pszFaceName != NULL )
2682 if ( !strcmp( pcharsetbindings->pszFaceName,
2683 fm.plf->lfFaceName ) )
2685 fm.internal_charset = pcharsetbindings->charset;
2688 pcharsetbindings ++;
2690 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2694 /* match all available fonts */
2696 fm.flags |= FO_MATCH_PAF;
2697 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2699 if( (*ppfr)->fr_flags & FR_REMOVED )
2701 if( (*ppfr)->fo_count == 0 )
2702 XFONT_RemoveFontResource( ppfr );
2708 TRACE("%s\n", fm.pfr->lfFaceName );
2710 current_score = XFONT_MatchFIList( &fm );
2711 if( current_score < score )
2713 score = current_score;
2720 /***********************************************************************
2723 static void XFONT_GrowFreeList(int start, int end)
2725 /* add all entries from 'start' up to and including 'end' */
2727 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2729 fontCache[end].lru = fontLF;
2730 fontCache[end].count = -1;
2732 while( start < end )
2734 fontCache[start].count = -1;
2735 fontCache[start].lru = start + 1;
2740 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2742 UINT16 cs = __lfCheckSum( plf );
2743 int i = fontMRU, prev = -1;
2748 if( fontCache[i].lfchecksum == cs &&
2749 !(fontCache[i].fo_flags & FO_REMOVED) )
2751 /* FIXME: something more intelligent here ? */
2753 if( !memcmp( plf, &fontCache[i].lf,
2754 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2755 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2757 /* remove temporarily from the lru list */
2759 fontCache[prev].lru = fontCache[i].lru;
2761 fontMRU = (INT16)fontCache[i].lru;
2762 return (fontCache + i);
2766 i = (INT16)fontCache[i].lru;
2771 static fontObject* XFONT_GetCacheEntry(void)
2777 int prev_i, prev_j, j;
2779 TRACE("font cache is full\n");
2781 /* lookup the least recently used font */
2783 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2785 if( fontCache[i].count <= 0 &&
2786 !(fontCache[i].fo_flags & FO_SYSTEM) )
2794 if( j >= 0 ) /* unload font */
2796 /* detach from the lru list */
2798 TRACE("\tfreeing entry %i\n", j );
2800 fontCache[j].fr->fo_count--;
2803 fontCache[prev_j].lru = fontCache[j].lru;
2804 else fontMRU = (INT16)fontCache[j].lru;
2806 /* FIXME: lpXForm, lpPixmap */
2807 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2810 XFreeFont( gdi_display, fontCache[j].fs );
2811 wine_tsx11_unlock();
2813 memset( fontCache + j, 0, sizeof(fontObject) );
2814 return (fontCache + j);
2816 else /* expand cache */
2818 fontObject* newCache;
2820 prev_i = fontCacheSize + FONTCACHE;
2822 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2824 if( (newCache = HeapReAlloc(GetProcessHeap(), 0, fontCache, prev_i * sizeof(fontObject))) )
2827 fontCacheSize = prev_i;
2828 fontCache = newCache;
2829 XFONT_GrowFreeList( i, fontCacheSize - 1);
2835 /* detach from the free list */
2838 fontLF = (INT16)fontCache[i].lru;
2839 fontCache[i].count = 0;
2840 return (fontCache + i);
2843 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2845 UINT u = (UINT)(pfo - fontCache);
2849 if( u < fontCacheSize )
2851 ret = --fontCache[u].count;
2854 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2856 if( CHECK_PFONT(pfo->prefobjs[i]) )
2857 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2867 /***********************************************************************
2868 * X11DRV_FONT_InitX11Metrics
2870 * Initialize font resource list and allocate font cache.
2872 static void X11DRV_FONT_InitX11Metrics( void )
2875 unsigned x_checksum;
2876 int i, x_count, fd, buf_size;
2882 x_pattern = XListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2883 wine_tsx11_unlock();
2885 TRACE("Font Mapper: initializing %i x11 fonts\n", x_count);
2886 if (x_count == MAX_FONTS)
2887 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2889 for( i = x_checksum = 0; i < x_count; i++ )
2893 printf("%i\t: %s\n", i, x_pattern[i] );
2896 j = strlen( x_pattern[i] );
2897 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2899 x_checksum |= X_PFONT_MAGIC;
2901 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2903 /* deal with systemwide font metrics cache */
2906 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver\Fonts */
2907 if (RegOpenKeyA(HKEY_CURRENT_USER, INIFontSection, &hkey)) hkey = 0;
2910 DWORD type, count = buf_size;
2911 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, (LPBYTE)buffer, &count);
2916 fd = open( buffer, O_RDONLY );
2917 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2919 if (fontList == NULL)
2922 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2925 fd = open( buffer, O_RDONLY );
2926 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2930 if( fontList == NULL ) /* build metrics from scratch */
2932 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2933 if( buffer[0] ) /* update cached metrics */
2935 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2936 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2938 WARN("Unable to write to fontcache '%s'\n", buffer);
2939 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2945 XFreeFontNames(x_pattern);
2947 /* check if we're dealing with X11 R6 server */
2950 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2951 if( (x_fs = safe_XLoadQueryFont(gdi_display, buffer)) )
2953 text_caps |= TC_SF_X_YINDEP;
2954 XFreeFont(gdi_display, x_fs);
2957 wine_tsx11_unlock();
2959 HeapFree(GetProcessHeap(), 0, buffer);
2961 XFONT_WindowsNames();
2962 XFONT_LoadAliases( hkey );
2963 if (hkey) XFONT_LoadDefaults( hkey );
2964 XFONT_LoadIgnores( hkey );
2966 /* fontList initialization is over, allocate X font cache */
2968 fontCache = HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2969 XFONT_GrowFreeList(0, fontCacheSize - 1);
2972 if (hkey) RegCloseKey(hkey);
2975 /***********************************************************************
2978 void X11DRV_FONT_Init( int log_pixels_x, int log_pixels_y )
2980 XFONT_GetDefResolution( log_pixels_x, log_pixels_y );
2982 if(using_client_side_fonts)
2983 text_caps |= TC_VA_ABLE;
2988 /**********************************************************************
2991 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2998 XGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2999 fontName = XGetAtomName( gdi_display, nameAtom );
3000 if (!LFD_Parse(fontName, &lfd) || lfd.pixel_size[0] != '[')
3003 wine_tsx11_unlock();
3006 #define PX pfo->lpX11Trans
3008 sscanf(lfd.pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
3011 XGetFontProperty( pfo->fs, x11drv_atom(RAW_ASCENT), &PX->RAW_ASCENT );
3012 XGetFontProperty( pfo->fs, x11drv_atom(RAW_DESCENT), &PX->RAW_DESCENT );
3013 wine_tsx11_unlock();
3015 PX->pixelsize = hypot(PX->a, PX->b);
3016 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
3017 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
3019 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
3020 PX->a, PX->b, PX->c, PX->d,
3021 PX->RAW_ASCENT, PX->RAW_DESCENT);
3027 /***********************************************************************
3028 * X Device Font Objects
3030 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
3031 LPCSTR* faceMatched, BOOL bSubFont,
3032 WORD internal_charset,
3033 WORD* pcharsetMatched )
3039 pfo = XFONT_LookupCachedFont( plf, &checksum );
3050 fm.internal_charset = internal_charset;
3052 if( text_caps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
3054 /* allocate new font cache entry */
3056 if( (pfo = XFONT_GetCacheEntry()) )
3058 /* initialize entry and load font */
3059 char lpLFD[MAX_LFD_LENGTH];
3060 UINT uRelaxLevel = 0;
3062 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
3064 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics\n",
3066 pfo->rescale = fabs(plf->lfHeight / 100.0);
3067 if(plf->lfHeight > 0) plf->lfHeight = 100;
3068 else plf->lfHeight = -100;
3072 XFONT_MatchDeviceFont( fontList, &fm );
3075 pfo->fr->fo_count++;
3076 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3079 pfo->lfchecksum = checksum;
3083 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3084 if( (pfo->fs = safe_XLoadQueryFont( gdi_display, lpLFD )) ) break;
3085 } while( uRelaxLevel );
3088 if(pfo->lf.lfEscapement != 0) {
3089 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3090 if(!XFONT_SetX11Trans( pfo )) {
3091 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3092 pfo->lpX11Trans = NULL;
3095 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3096 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3097 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3098 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3100 /* FIXME: If we've got a soft font or
3101 * there are FO_SYNTH_... flags for the
3102 * non PROOF_QUALITY request, the engine
3103 * should rasterize characters into mono
3104 * pixmaps and store them in the pfo->lpPixmap
3105 * array (pfo->fs should be updated as well).
3106 * array (pfo->fs should be updated as well).
3107 * X11DRV_ExtTextOut() must be heavily modified
3108 * to support pixmap blitting and FO_SYNTH_...
3112 pfo->lpPixmap = NULL;
3114 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3115 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3117 /* special treatment for DBCS that needs multiple fonts */
3118 /* All member of pfo must be set correctly. */
3119 if ( bSubFont == FALSE )
3122 WORD charsetMatchedSub;
3124 LPCSTR faceMatchedSub;
3126 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3128 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3129 penum_subfont_charset( i );
3130 if ( charset_sub == DEFAULT_CHARSET ) break;
3134 lfSub.lfHeight=plf->lfHeight;
3135 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3136 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3137 /* this font has sub font */
3138 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3140 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3142 &charsetMatchedSub );
3143 /* FIXME: check charsetMatchedSub */
3148 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3150 UINT current_score, score = (UINT)(-1);
3152 i = index = fontMRU;
3153 fm.flags |= FO_MATCH_PAF;
3156 pfo = fontCache + i;
3157 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3159 current_score = XFONT_Match( &fm );
3160 if( current_score < score ) index = i;
3164 pfo = fontCache + index;
3169 /* attach at the head of the lru list */
3171 index = fontMRU = (pfo - fontCache);
3176 TRACE("physfont %i\n", index);
3177 *faceMatched = pfo->fi->df.dfFace;
3178 *pcharsetMatched = pfo->fi->internal_charset;
3180 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3183 /***********************************************************************
3184 * XFONT_GetFontObject
3186 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3188 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3192 /***********************************************************************
3193 * XFONT_GetFontStruct
3195 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3197 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3201 /***********************************************************************
3204 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3206 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3212 /* X11DRV Interface ****************************************************
3214 * Exposed via the dc->funcs dispatch table. *
3216 ***********************************************************************/
3217 /***********************************************************************
3218 * SelectFont (X11DRV.@)
3220 HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont, HANDLE gdiFont )
3225 TRACE("hdc=%p, hfont=%p\n", physDev->hdc, hfont);
3227 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return HGDI_ERROR;
3229 TRACE("gdiFont = %p\n", gdiFont);
3231 if(gdiFont && using_client_side_fonts) {
3232 X11DRV_XRender_SelectFont(physDev, hfont);
3233 physDev->has_gdi_font = TRUE;
3237 EnterCriticalSection( &crtsc_fonts_X11 );
3239 if(fontList == NULL) X11DRV_FONT_InitX11Metrics();
3241 if( CHECK_PFONT(physDev->font) )
3242 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3244 FONT_LogFontWTo16(&logfont, &lf);
3246 /* stock fonts ignore the mapping mode */
3247 if (!is_stock_font( hfont ))
3249 /* Make sure we don't change the sign when converting to device coords */
3250 /* FIXME - check that the other drivers do this correctly */
3253 INT width = X11DRV_XWStoDS( physDev, lf.lfWidth );
3254 lf.lfWidth = (lf.lfWidth < 0) ? -abs(width) : abs(width);
3255 if (lf.lfWidth == 0)
3256 lf.lfWidth = 1; /* Minimum width */
3260 INT height = X11DRV_YWStoDS( physDev, lf.lfHeight );
3261 lf.lfHeight = (lf.lfHeight < 0) ? -abs(height) : abs(height);
3262 if (lf.lfHeight == 0)
3263 lf.lfHeight = MIN_FONT_SIZE;
3268 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(physDev->hdc,LOGPIXELSY) + (72>>1)) / 72;
3271 /* Fixup aliases before passing to RealizeFont */
3272 /* alias = Windows name in the alias table */
3273 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3275 WORD charsetMatched;
3277 TRACE("hfont=%p\n", hfont); /* to connect with the trace from RealizeFont */
3278 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3279 FALSE, lf.lfCharSet,
3282 /* set face to the requested facename if it matched
3283 * so that GetTextFace can get the correct face name
3285 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3286 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3287 logfont.lfFaceName, LF_FACESIZE);
3289 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3290 logfont.lfFaceName, LF_FACESIZE);
3293 * In X, some encodings may have the same lfFaceName.
3295 * -misc-fixed-*-iso8859-1
3296 * -misc-fixed-*-jisx0208.1990-0
3297 * so charset should be saved...
3299 logfont.lfCharSet = charsetMatched;
3302 LeaveCriticalSection( &crtsc_fonts_X11 );
3304 physDev->has_gdi_font = FALSE;
3305 return (HFONT)1; /* Use a device font */
3309 /***********************************************************************
3311 * X11DRV_EnumDeviceFonts
3313 BOOL X11DRV_EnumDeviceFonts( X11DRV_PDEVICE *physDev, LPLOGFONTW plf,
3314 FONTENUMPROCW proc, LPARAM lp )
3317 NEWTEXTMETRICEXW tm;
3318 fontResource* pfr = fontList;
3321 /* don't enumerate x11 fonts if we're using client side fonts */
3322 if (physDev->has_gdi_font) return FALSE;
3324 if( plf->lfFaceName[0] )
3326 char facename[LF_FACESIZE+1];
3327 WideCharToMultiByte( CP_ACP, 0, plf->lfFaceName, -1,
3328 facename, sizeof(facename), NULL, NULL );
3329 /* enum all entries in this resource */
3330 pfr = XFONT_FindFIList( pfr, facename );
3334 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3336 /* Note: XFONT_GetFontMetric() will have to
3337 release the crit section, font list will
3338 have to be retraversed on return */
3340 if(plf->lfCharSet == DEFAULT_CHARSET ||
3341 plf->lfCharSet == pfi->df.dfCharSet) {
3342 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3343 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3350 else /* enum first entry in each resource */
3351 for( ; pfr ; pfr = pfr->next )
3355 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3356 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3365 /***********************************************************************
3366 * X11DRV_GetTextMetrics
3368 BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
3370 if( CHECK_PFONT(physDev->font) )
3372 fontObject* pfo = __PFONT(physDev->font);
3373 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsW( pfo, metrics );
3380 /***********************************************************************
3381 * X11DRV_GetCharWidth
3383 BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
3386 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3392 if (pfo->fs->per_char == NULL)
3393 for (i = firstChar; i <= lastChar; i++)
3395 *buffer++ = pfo->fs->min_bounds.attributes *
3396 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3398 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3401 XCharStruct *cs, *def;
3402 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3404 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3407 for (i = firstChar; i <= lastChar; i++)
3411 UINT ch_f; /* character code in the font encoding */
3412 WideCharToMultiByte( pfo->fi->codepage, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
3414 if (ch_f >= pfo->fs->min_char_or_byte2 &&
3415 ch_f <= pfo->fs->max_char_or_byte2)
3417 cs = &pfo->fs->per_char[(ch_f - pfo->fs->min_char_or_byte2)];
3418 if (CI_NONEXISTCHAR(cs)) cs = def;
3421 *buffer++ = max(cs->attributes, 0) *
3422 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3424 *buffer++ = max(cs->width, 0 ) * pfo->rescale;