WTSFreeMemory stub.
[wine/hacks.git] / dlls / x11drv / xfont.c
blobb00ef46f86916a0433bc4eed6ea0083c036d8527
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
21 * have to be changed for that), dynamic font loading (FreeType).
24 #include "config.h"
25 #include "wine/port.h"
27 #include <ctype.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <math.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winnls.h"
43 #include "winreg.h"
44 #include "x11font.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
56 #define TILDE '~'
57 #define HYPHEN '-'
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
73 LPSTR faTypeFace;
74 LPSTR faAlias;
75 struct __fontAlias* next;
76 } fontAlias;
78 static fontAlias *aliasTable = NULL;
80 static const char* INIFontMetrics = "cachedmetrics.";
81 static const char* INIFontSection = "Software\\Wine\\Wine\\Config\\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 ||\
95 (ch)==EE_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
111 LPCSTR psuffix;
112 WORD charset; /* hibyte != 0 means *internal* charset */
113 WORD codepage;
114 WORD cptable;
115 } SuffixCharset;
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 }};
228 typedef struct __fet
230 LPCSTR prefix;
231 const SuffixCharset* sufch;
232 struct __fet* next;
233 } fontEncodingTemplate;
235 /* Note: we can attach additional encoding mappings to the end
236 * of this table at runtime.
238 static 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 },
264 static fontEncodingTemplate* fETTable = __fETTable;
266 /* a charset database for known facenames */
267 struct CharsetBindingInfo
269 const char* pszFaceName;
270 BYTE charset;
272 static const struct CharsetBindingInfo charsetbindings[] =
274 /* special facenames */
275 { "System", DEFAULT_CHARSET },
276 { "FixedSys", DEFAULT_CHARSET },
278 /* known facenames */
279 { "MS Serif", ANSI_CHARSET },
280 { "MS Sans Serif", ANSI_CHARSET },
281 { "Courier", ANSI_CHARSET },
282 { "Symbol", SYMBOL_CHARSET },
284 { "Arial", ANSI_CHARSET },
285 { "Arial Greek", GREEK_CHARSET },
286 { "Arial Tur", TURKISH_CHARSET },
287 { "Arial Baltic", BALTIC_CHARSET },
288 { "Arial CE", EASTEUROPE_CHARSET },
289 { "Arial Cyr", RUSSIAN_CHARSET },
290 { "Courier New", ANSI_CHARSET },
291 { "Courier New Greek", GREEK_CHARSET },
292 { "Courier New Tur", TURKISH_CHARSET },
293 { "Courier New Baltic", BALTIC_CHARSET },
294 { "Courier New CE", EASTEUROPE_CHARSET },
295 { "Courier New Cyr", RUSSIAN_CHARSET },
296 { "Times New Roman", ANSI_CHARSET },
297 { "Times New Roman Greek", GREEK_CHARSET },
298 { "Times New Roman Tur", TURKISH_CHARSET },
299 { "Times New Roman Baltic", BALTIC_CHARSET },
300 { "Times New Roman CE", EASTEUROPE_CHARSET },
301 { "Times New Roman Cyr", RUSSIAN_CHARSET },
303 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
304 SHIFTJIS_CHARSET }, /* MS gothic */
305 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
306 SHIFTJIS_CHARSET }, /* MS P gothic */
307 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
308 SHIFTJIS_CHARSET }, /* MS mincho */
309 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
310 SHIFTJIS_CHARSET }, /* MS P mincho */
311 { "GulimChe", HANGEUL_CHARSET },
312 { "MS Song", GB2312_CHARSET },
313 { "MS Hei", GB2312_CHARSET },
314 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
315 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
317 { NULL, 0 }
321 static int DefResolution = 0;
323 static CRITICAL_SECTION crtsc_fonts_X11;
324 static CRITICAL_SECTION_DEBUG critsect_debug =
326 0, 0, &crtsc_fonts_X11,
327 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
328 0, 0, { 0, (DWORD)(__FILE__ ": crtsc_fonts_X11") }
330 static CRITICAL_SECTION crtsc_fonts_X11 = { &critsect_debug, -1, 0, 0, 0, 0 };
332 static fontResource* fontList = NULL;
333 static fontObject* fontCache = NULL; /* array */
334 static unsigned int fontCacheSize = FONTCACHE;
335 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
337 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
338 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
339 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
341 /***********************************************************************
342 * Helper macros from X distribution
345 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
346 (((cs)->rbearing|(cs)->lbearing| \
347 (cs)->ascent|(cs)->descent) == 0))
349 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
351 cs = def; \
352 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
353 if (fs->per_char == NULL) { \
354 cs = &fs->min_bounds; \
355 } else { \
356 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
357 if (CI_NONEXISTCHAR(cs)) cs = def; \
362 #define CI_GET_DEFAULT_INFO(fs,cs) \
363 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
366 /***********************************************************************
367 * is_stock_font
369 inline static BOOL is_stock_font( HFONT font )
371 int i;
372 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
374 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
376 return FALSE;
380 static void FONT_LogFontWTo16( const LOGFONTW* font32, LPLOGFONT16 font16 )
382 font16->lfHeight = font32->lfHeight;
383 font16->lfWidth = font32->lfWidth;
384 font16->lfEscapement = font32->lfEscapement;
385 font16->lfOrientation = font32->lfOrientation;
386 font16->lfWeight = font32->lfWeight;
387 font16->lfItalic = font32->lfItalic;
388 font16->lfUnderline = font32->lfUnderline;
389 font16->lfStrikeOut = font32->lfStrikeOut;
390 font16->lfCharSet = font32->lfCharSet;
391 font16->lfOutPrecision = font32->lfOutPrecision;
392 font16->lfClipPrecision = font32->lfClipPrecision;
393 font16->lfQuality = font32->lfQuality;
394 font16->lfPitchAndFamily = font32->lfPitchAndFamily;
395 WideCharToMultiByte( CP_ACP, 0, font32->lfFaceName, -1,
396 font16->lfFaceName, LF_FACESIZE, NULL, NULL );
397 font16->lfFaceName[LF_FACESIZE-1] = 0;
401 /***********************************************************************
402 * Checksums
404 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
406 CHAR font[LF_FACESIZE];
407 UINT16 checksum = 0;
408 UINT16 *ptr;
409 int i;
411 ptr = (UINT16 *)plf;
412 for (i = 0; i < 9; i++) checksum ^= *ptr++;
413 for (i = 0; i < LF_FACESIZE; i++)
415 font[i] = tolower(plf->lfFaceName[i]);
416 if (!font[i] || font[i] == ' ') break;
418 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
419 return checksum;
422 static UINT16 __genericCheckSum( const void *ptr, int size )
424 unsigned int checksum = 0;
425 const char *p = (const char *)ptr;
426 while (size-- > 0)
427 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
429 return checksum & 0xffff;
432 /*************************************************************************
433 * LFD parse/compose routines
435 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
436 * make a copy first
438 * These functions also do TILDE to HYPHEN conversion
440 static BOOL LFD_Parse(LPSTR lpFont, LFD *lfd)
442 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
443 int i;
444 if (*lpch != HYPHEN)
446 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
447 return FALSE;
450 field_start = ++lpch;
451 for( i = 0; i < LFD_FIELDS; )
453 if (*lpch == HYPHEN)
455 *lpch = '\0';
456 lfd_fld[i] = field_start;
457 i++;
458 field_start = ++lpch;
460 else if (!*lpch)
462 lfd_fld[i] = field_start;
463 i++;
464 break;
466 else if (*lpch == TILDE)
468 *lpch = HYPHEN;
469 ++lpch;
471 else
472 ++lpch;
474 /* Fill in the empty fields with NULLS */
475 for (; i< LFD_FIELDS; i++)
476 lfd_fld[i] = NULL;
477 if (*lpch)
478 WARN("Extra ignored in font '%s'\n", lpFont);
480 lfd->foundry = lfd_fld[0];
481 lfd->family = lfd_fld[1];
482 lfd->weight = lfd_fld[2];
483 lfd->slant = lfd_fld[3];
484 lfd->set_width = lfd_fld[4];
485 lfd->add_style = lfd_fld[5];
486 lfd->pixel_size = lfd_fld[6];
487 lfd->point_size = lfd_fld[7];
488 lfd->resolution_x = lfd_fld[8];
489 lfd->resolution_y = lfd_fld[9];
490 lfd->spacing = lfd_fld[10];
491 lfd->average_width = lfd_fld[11];
492 lfd->charset_registry = lfd_fld[12];
493 lfd->charset_encoding = lfd_fld[13];
494 return TRUE;
498 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
500 const char* lfd_fld[LFD_FIELDS];
501 int i;
503 if (!buf_size)
504 return; /* Don't be silly */
506 lfd_fld[0] = lfd->foundry;
507 lfd_fld[1] = lfd->family;
508 lfd_fld[2] = lfd->weight ;
509 lfd_fld[3] = lfd->slant ;
510 lfd_fld[4] = lfd->set_width ;
511 lfd_fld[5] = lfd->add_style;
512 lfd_fld[6] = lfd->pixel_size;
513 lfd_fld[7] = lfd->point_size;
514 lfd_fld[8] = lfd->resolution_x;
515 lfd_fld[9] = lfd->resolution_y ;
516 lfd_fld[10] = lfd->spacing ;
517 lfd_fld[11] = lfd->average_width ;
518 lfd_fld[12] = lfd->charset_registry ;
519 lfd_fld[13] = lfd->charset_encoding ;
521 buf_size--; /* Room for the terminator */
523 for (i = 0; i < LFD_FIELDS; i++)
525 const char* sp = lfd_fld[i];
526 if (!sp || !buf_size)
527 break;
529 *dp++ = HYPHEN;
530 buf_size--;
531 while (buf_size > 0 && *sp)
533 *dp = (*sp == HYPHEN) ? TILDE : *sp;
534 buf_size--;
535 dp++; sp++;
538 *dp = '\0';
542 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
544 int j = strlen(lpStr);
545 if( j == 1 && *lpStr == '0')
546 fi->fi_flags |= FI_POLYWEIGHT;
547 else if( j == 4 )
549 if( !strcasecmp( "bold", lpStr) )
550 fi->df.dfWeight = FW_BOLD;
551 else if( !strcasecmp( "demi", lpStr) )
553 fi->fi_flags |= FI_FW_DEMI;
554 fi->df.dfWeight = FW_DEMIBOLD;
556 else if( !strcasecmp( "book", lpStr) )
558 fi->fi_flags |= FI_FW_BOOK;
559 fi->df.dfWeight = FW_REGULAR;
562 else if( j == 5 )
564 if( !strcasecmp( "light", lpStr) )
565 fi->df.dfWeight = FW_LIGHT;
566 else if( !strcasecmp( "black", lpStr) )
567 fi->df.dfWeight = FW_BLACK;
569 else if( j == 6 && !strcasecmp( "medium", lpStr) )
570 fi->df.dfWeight = FW_REGULAR;
571 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
572 fi->df.dfWeight = FW_DEMIBOLD;
573 else
574 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
575 * from the weight property */
578 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
580 int l = strlen(lpStr);
581 if( l == 1 )
583 switch( tolower( *lpStr ) )
585 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
586 default:
587 case 'r': fi->df.dfItalic = 0;
588 break;
589 case 'o':
590 fi->fi_flags |= FI_OBLIQUE;
591 case 'i': fi->df.dfItalic = 1;
592 break;
594 return FALSE;
596 return TRUE;
599 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
601 int j = strlen(lpstr);
602 if( j > 3 ) /* find out is there "sans" or "script" */
604 j = 0;
606 if( strstr(lpstr, "sans") )
608 fi->df.dfPitchAndFamily |= FF_SWISS;
609 j = 1;
611 if( strstr(lpstr, "script") )
613 fi->df.dfPitchAndFamily |= FF_SCRIPT;
614 j = 1;
616 if( !j && dec_style_check )
617 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
621 /*************************************************************************
622 * LFD_InitFontInfo
624 * INIT ONLY
626 * Fill in some fields in the fontInfo struct.
628 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
630 int i, j, dec_style_check, scalability;
631 fontEncodingTemplate* boba;
632 const char* ridiculous = "font '%s' has ridiculous %s\n";
633 const char* lpstr;
635 if (!lfd->charset_registry)
637 WARN("font '%s' does not have enough fields\n", fullname);
638 return FALSE;
641 memset(fi, 0, sizeof(fontInfo) );
643 /* weight name - */
644 LFD_GetWeight( fi, lfd->weight);
646 /* slant - */
647 dec_style_check = LFD_GetSlant( fi, lfd->slant);
649 /* width name - */
650 lpstr = lfd->set_width;
651 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
652 dec_style_check = TRUE;
653 else
654 fi->fi_flags |= FI_NORMAL;
656 /* style - */
657 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
659 /* pixel & decipoint height, and res_x & y */
661 scalability = 0;
663 j = strlen(lfd->pixel_size);
664 if( j == 0 || j > 3 )
666 WARN(ridiculous, fullname, "pixel_size");
667 return FALSE;
669 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
670 scalability++;
672 j = strlen(lfd->point_size);
673 if( j == 0 || j > 3 )
675 WARN(ridiculous, fullname, "point_size");
676 return FALSE;
678 if( !(atoi(lfd->point_size)) )
679 scalability++;
681 j = strlen(lfd->resolution_x);
682 if( j == 0 || j > 3 )
684 WARN(ridiculous, fullname, "resolution_x");
685 return FALSE;
687 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
688 scalability++;
690 j = strlen(lfd->resolution_y);
691 if( j == 0 || j > 3 )
693 WARN(ridiculous, fullname, "resolution_y");
694 return FALSE;
696 if( !(atoi(lfd->resolution_y)) )
697 scalability++;
699 /* Check scalability */
700 switch (scalability)
702 case 0: /* Bitmap */
703 break;
704 case 4: /* Scalable */
705 fi->fi_flags |= FI_SCALABLE;
706 break;
707 case 2:
708 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
709 TRACE("Skipping scalable bitmap '%s'\n", fullname);
710 return FALSE;
711 default:
712 WARN("Font '%s' has weird scalability\n", fullname);
713 return FALSE;
716 /* spacing - */
717 lpstr = lfd->spacing;
718 switch( *lpstr )
720 case '\0':
721 WARN("font '%s' has no spacing\n", fullname);
722 return FALSE;
724 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
725 break;
726 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
727 fi->fi_flags |= FI_FIXEDEX;
728 /* fall through */
729 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
730 break;
731 default:
732 /* Of course this line does nothing */
733 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
736 /* average width - */
737 lpstr = lfd->average_width;
738 j = strlen(lpstr);
739 if( j == 0 || j > 3 )
741 WARN(ridiculous, fullname, "average_width");
742 return FALSE;
745 if( !(atoi(lpstr)) && !scalability )
747 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
748 return FALSE;
751 /* charset registry, charset encoding - */
752 lpstr = lfd->charset_registry;
753 if( strstr(lpstr, "ksc") ||
754 strstr(lpstr, "gb2312") ||
755 strstr(lpstr, "big5") )
757 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
760 fi->df.dfCharSet = ANSI_CHARSET;
762 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
764 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
766 if (lfd->charset_encoding)
768 for( j = 0; boba->sufch[j].psuffix; j++ )
770 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
772 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
773 fi->internal_charset = boba->sufch[j].charset;
774 fi->codepage = boba->sufch[j].codepage;
775 fi->cptable = boba->sufch[j].cptable;
776 goto done;
780 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
781 fi->internal_charset = boba->sufch[j].charset;
782 fi->codepage = boba->sufch[j].codepage;
783 fi->cptable = boba->sufch[j].cptable;
784 if (boba->prefix)
786 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
787 fullname, lfd->charset_encoding, boba->prefix);
788 j = 254;
790 else
792 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
793 fullname, lfd->charset_registry, lfd->charset_encoding);
794 j = 255;
797 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
798 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
799 goto done;
801 else if (boba->prefix)
803 WARN("font '%s' has known registry '%s' and no character encoding\n",
804 fullname, lpstr);
805 for( j = 0; boba->sufch[j].psuffix; j++ )
807 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
808 fi->internal_charset = boba->sufch[j].charset;
809 fi->codepage = boba->sufch[j].codepage;
810 fi->cptable = boba->sufch[j].cptable;
811 j = 255;
812 goto done;
816 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
817 return FALSE;
819 done:
820 /* i - index into fETTable
821 * j - index into suffix array for fETTable[i]
822 * except:
823 * 254 - found encoding prefix, unknown suffix
824 * 255 - no encoding match at all.
826 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
828 return TRUE;
832 /*************************************************************************
833 * LFD_AngleMatrix
835 * make a matrix suitable for LFD based on theta radians
837 static void LFD_AngleMatrix( char* buffer, int h, double theta)
839 double matrix[4];
840 matrix[0] = h*cos(theta);
841 matrix[1] = h*sin(theta);
842 matrix[2] = -h*sin(theta);
843 matrix[3] = h*cos(theta);
844 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
847 /*************************************************************************
848 * LFD_ComposeLFD
850 * Note: uRelax is a treatment not a cure. Font mapping algorithm
851 * should be bulletproof enough to allow us to avoid hacks like
852 * this despite LFD being so braindead.
854 static BOOL LFD_ComposeLFD( const fontObject* fo,
855 INT height, LPSTR lpLFD, UINT uRelax )
857 int i, h;
858 const char *any = "*";
859 char h_string[64], resx_string[64], resy_string[64];
860 LFD aLFD;
862 /* Get the worst case over with first */
864 /* RealizeFont() will call us repeatedly with increasing uRelax
865 * until XLoadFont() succeeds.
866 * to avoid an infinite loop; these will always match
868 if (uRelax >= 5)
870 if (uRelax == 5)
871 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
872 else
873 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
874 return TRUE;
877 /* read foundry + family from fo */
878 aLFD.foundry = fo->fr->resource->foundry;
879 aLFD.family = fo->fr->resource->family;
881 /* add weight */
882 switch( fo->fi->df.dfWeight )
884 case FW_BOLD:
885 aLFD.weight = "bold"; break;
886 case FW_REGULAR:
887 if( fo->fi->fi_flags & FI_FW_BOOK )
888 aLFD.weight = "book";
889 else
890 aLFD.weight = "medium";
891 break;
892 case FW_DEMIBOLD:
893 aLFD.weight = "demi";
894 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
895 aLFD.weight = "bold";
896 break;
897 case FW_BLACK:
898 aLFD.weight = "black"; break;
899 case FW_LIGHT:
900 aLFD.weight = "light"; break;
901 default:
902 aLFD.weight = any;
905 /* add slant */
906 if( fo->fi->df.dfItalic )
907 if( fo->fi->fi_flags & FI_OBLIQUE )
908 aLFD.slant = "o";
909 else
910 aLFD.slant = "i";
911 else
912 aLFD.slant = (uRelax < 1) ? "r" : any;
914 /* add width */
915 if( fo->fi->fi_flags & FI_NORMAL )
916 aLFD.set_width = "normal";
917 else
918 aLFD.set_width = any;
920 /* ignore style */
921 aLFD.add_style = any;
923 /* add pixelheight
925 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
927 if( fo->fo_flags & FO_SYNTH_HEIGHT )
928 h = fo->fi->lfd_height;
929 else
931 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
932 h /= fo->fi->df.dfPixHeight;
934 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
935 h = MIN_FONT_SIZE;
936 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
938 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
939 h = MAX_FONT_SIZE;
942 if (uRelax <= 2)
943 /* handle rotated fonts */
944 if (fo->lf.lfEscapement) {
945 /* escapement is in tenths of degrees, theta is in radians */
946 double theta = M_PI*fo->lf.lfEscapement/1800.;
947 LFD_AngleMatrix(h_string, h, theta);
949 else
951 sprintf(h_string, "%d", h);
953 else
954 sprintf(h_string, "%d", fo->fi->lfd_height);
956 aLFD.pixel_size = h_string;
957 aLFD.point_size = any;
959 /* resolution_x,y, average width */
960 /* FIXME - Why do some font servers ignore average width ?
961 * so that you have to mess around with res_y
963 aLFD.average_width = any;
964 if (uRelax <= 3)
966 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
967 aLFD.resolution_x = resx_string;
969 strcpy(resy_string, resx_string);
970 if( uRelax == 0 && text_caps & TC_SF_X_YINDEP )
972 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
974 int resy = 0.5 + fo->fi->lfd_resolution *
975 (fo->fi->df.dfAvgWidth * height) /
976 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
977 sprintf(resy_string, "%d", resy);
979 else
981 /* FIXME - synth width */
984 aLFD.resolution_y = resy_string;
986 else
988 aLFD.resolution_x = aLFD.resolution_y = any;
991 /* spacing */
993 const char* w;
995 if( fo->fi->fi_flags & FI_FIXEDPITCH )
996 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
997 else
998 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
1000 aLFD.spacing = (uRelax <= 1) ? w : any;
1003 /* encoding */
1005 if (uRelax <= 4)
1007 fontEncodingTemplate* boba = fETTable;
1009 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
1010 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
1012 i = fo->fi->fi_encoding & 255;
1013 switch( i )
1015 default:
1016 aLFD.charset_encoding = boba->sufch[i].psuffix;
1017 break;
1019 case 254:
1020 aLFD.charset_encoding = any;
1021 break;
1023 case 255: /* no suffix - it ends eg "-ascii" */
1024 aLFD.charset_encoding = NULL;
1025 break;
1028 else
1030 aLFD.charset_registry = any;
1031 aLFD.charset_encoding = any;
1034 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1036 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1037 return TRUE;
1041 /***********************************************************************
1042 * X Font Resources
1044 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1045 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1047 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1048 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1050 unsigned long height;
1051 unsigned min = (unsigned char)pFI->dfFirstChar;
1052 unsigned max = (unsigned char)pFI->dfLastChar;
1053 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1055 if( pEL ) *pEL = 0;
1057 if(XFT) {
1058 wine_tsx11_lock();
1059 if(XGetFontProperty((XFontStruct*)x_fs, x11drv_atom(RAW_CAP_HEIGHT), &height))
1060 *pIL = XFT->ascent -
1061 (INT)(XFT->pixelsize / 1000.0 * height);
1062 else
1063 *pIL = 0;
1064 wine_tsx11_unlock();
1065 return;
1068 wine_tsx11_lock();
1069 if( XGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1071 if( x_fs->per_char )
1072 if( bIsLatin && ((unsigned char)'X' <= (max - min)) )
1073 height = x_fs->per_char['X' - min].ascent;
1074 else
1075 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1076 height = x_fs->max_bounds.ascent;
1077 else
1079 height = x_fs->ascent;
1080 if( pEL )
1081 *pEL = x_fs->max_bounds.ascent - height;
1083 else
1084 height = x_fs->min_bounds.ascent;
1086 wine_tsx11_unlock();
1088 *pIL = x_fs->ascent - height;
1091 /***********************************************************************
1092 * XFONT_CharWidth
1094 static int XFONT_CharWidth(const XFontStruct* x_fs,
1095 const XFONTTRANS *XFT, int ch)
1097 if(!XFT)
1098 return x_fs->per_char[ch].width;
1099 else
1100 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1103 /***********************************************************************
1104 * XFONT_GetAvgCharWidth
1106 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1107 const XFONTTRANS *XFT)
1109 unsigned min = (unsigned char)pFI->dfFirstChar;
1110 unsigned max = (unsigned char)pFI->dfLastChar;
1112 INT avg;
1114 if( x_fs->per_char )
1116 unsigned int width = 0, chars = 0, j;
1117 if( (IS_LATIN_CHARSET(pFI->dfCharSet) ||
1118 pFI->dfCharSet == DEFAULT_CHARSET) &&
1119 (max - min) >= (unsigned char)'z' )
1121 /* FIXME - should use a weighted average */
1122 for( j = 0; j < 26; j++ )
1123 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1124 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1125 chars = 52;
1127 else /* unweighted average of everything */
1129 for( j = 0, max -= min; j <= max; j++ )
1130 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1132 width += XFONT_CharWidth(x_fs, XFT, j);
1133 chars++;
1136 if (chars) avg = (width + (chars-1))/ chars; /* always round up*/
1137 else avg = 0; /* No characters exist at all */
1139 else /* uniform width */
1140 avg = x_fs->min_bounds.width;
1142 TRACE(" retuning %d\n",avg);
1143 return avg;
1146 /***********************************************************************
1147 * XFONT_GetMaxCharWidth
1149 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1151 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1152 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1153 unsigned int maxwidth, j;
1155 if(!XFT || !xfs->per_char)
1156 return abs(xfs->max_bounds.width);
1158 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1159 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1160 if(maxwidth < xfs->per_char[j].attributes)
1161 maxwidth = xfs->per_char[j].attributes;
1163 maxwidth *= XFT->pixelsize / 1000.0;
1164 return maxwidth;
1167 /***********************************************************************
1168 * XFONT_SetFontMetric
1170 * INIT ONLY
1172 * Initializes IFONTINFO16.
1174 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1176 unsigned min, max;
1177 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1178 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1180 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1181 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1183 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1184 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1186 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1187 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1188 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1190 if( xfs->min_bounds.width != xfs->max_bounds.width )
1191 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1192 if( fi->fi_flags & FI_SCALABLE )
1194 fi->df.dfType = DEVICE_FONTTYPE;
1195 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1197 else if( fi->fi_flags & FI_TRUETYPE )
1198 fi->df.dfType = TRUETYPE_FONTTYPE;
1199 else
1200 fi->df.dfType = RASTER_FONTTYPE;
1202 fi->df.dfFace = fr->lfFaceName;
1205 /***********************************************************************
1206 * XFONT_GetFontMetric
1208 * Retrieve font metric info (enumeration).
1210 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1211 LPENUMLOGFONTEXW pLF,
1212 NEWTEXTMETRICEXW *pTM )
1214 memset( pLF, 0, sizeof(*pLF) );
1215 memset( pTM, 0, sizeof(*pTM) );
1217 #define plf ((LPLOGFONTW)pLF)
1218 #define ptm ((LPNEWTEXTMETRICW)pTM)
1219 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1220 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1221 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1222 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1223 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1224 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1225 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1227 /* convert pitch values */
1229 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1230 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1232 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1233 plf->lfFaceName, LF_FACESIZE);
1235 /* FIXME: fill in rest of plF values */
1236 strcpyW(pLF->elfFullName, plf->lfFaceName);
1237 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1238 pLF->elfStyle, LF_FACESIZE);
1239 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1240 "Symbol" : "Roman", -1,
1241 pLF->elfScript, LF_FACESIZE);
1243 #undef plf
1245 ptm->tmAscent = pfi->df.dfAscent;
1246 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1247 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1248 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1249 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1250 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1252 ptm->tmFirstChar = pfi->df.dfFirstChar;
1253 ptm->tmLastChar = pfi->df.dfLastChar;
1254 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1255 ptm->tmBreakChar = pfi->df.dfBreakChar;
1257 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1258 debugstr_w(pLF->elfLogFont.lfFaceName),
1259 debugstr_w(pLF->elfFullName));
1261 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1262 /* return font type */
1263 return pfi->df.dfType;
1264 #undef ptm
1268 /***********************************************************************
1269 * XFONT_FixupFlags
1271 * INIT ONLY
1273 * dfPitchAndFamily flags for some common typefaces.
1275 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1277 switch( lfFaceName[0] )
1279 case 'a':
1280 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1281 return FF_SWISS;
1282 break;
1283 case 'h':
1284 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1285 return FF_SWISS;
1286 break;
1287 case 'c':
1288 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1289 return FF_MODERN;
1291 if (!strcasecmp(lfFaceName, "Charter") )
1292 return FF_ROMAN;
1293 break;
1294 case 'p':
1295 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1296 return FF_ROMAN;
1297 break;
1298 case 't':
1299 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1300 return FF_ROMAN;
1301 break;
1302 case 'u':
1303 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1304 return FF_ROMAN;
1305 break;
1306 case 'z':
1307 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1308 return FF_DECORATIVE;
1310 return 0;
1313 /***********************************************************************
1314 * XFONT_SameFoundryAndFamily
1316 * INIT ONLY
1318 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1320 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1321 !strcasecmp( lfd1->family, lfd2->family ) );
1324 /***********************************************************************
1325 * XFONT_InitialCapitals
1327 * INIT ONLY
1329 * Upper case first letters of words & remove multiple spaces
1331 static void XFONT_InitialCapitals(LPSTR lpch)
1333 int i;
1334 BOOL up;
1335 char* lpstr = lpch;
1337 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1339 if( isspace(*lpch) )
1341 if (!up) /* Not already got one */
1343 *lpstr++ = ' ';
1344 up = TRUE;
1347 else if( isalpha(*lpch) && up )
1349 *lpstr++ = toupper(*lpch);
1350 up = FALSE;
1352 else
1354 *lpstr++ = *lpch;
1355 up = FALSE;
1359 /* Remove possible trailing space */
1360 if (up && i > 0)
1361 --lpstr;
1362 *lpstr = '\0';
1366 /***********************************************************************
1367 * XFONT_WindowsNames
1369 * INIT ONLY
1371 * Build generic Windows aliases for X font names.
1373 * -misc-fixed- -> "Fixed"
1374 * -sony-fixed- -> "Sony Fixed", etc...
1376 static void XFONT_WindowsNames(void)
1378 fontResource* fr;
1380 for( fr = fontList; fr ; fr = fr->next )
1382 fontResource* pfr;
1383 char* lpch;
1385 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1387 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1388 if( pfr->fr_flags & FR_NAMESET )
1390 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1391 break;
1394 lpch = fr->lfFaceName;
1395 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1396 /* prepend vendor name */
1397 (pfr==fr) ? "" : fr->resource->foundry,
1398 fr->resource->family);
1399 XFONT_InitialCapitals(fr->lfFaceName);
1401 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1402 if( bFamilyStyle)
1404 fontInfo* fi;
1405 for( fi = fr->fi ; fi ; fi = fi->next )
1406 fi->df.dfPitchAndFamily |= bFamilyStyle;
1410 TRACE("typeface '%s'\n", fr->lfFaceName);
1412 fr->fr_flags |= FR_NAMESET;
1416 /***********************************************************************
1417 * XFONT_LoadDefaultLFD
1419 * Move lfd to the head of fontList to make it more likely to be matched
1421 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1424 fontResource *fr, *pfr;
1425 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1427 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1429 if( fr )
1431 fr->next = pfr->next;
1432 pfr->next = fontList;
1433 fontList = pfr;
1435 break;
1437 fr = pfr;
1439 if (!pfr)
1440 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1441 lfd->foundry, lfd->family);
1445 /***********************************************************************
1446 * XFONT_LoadDefault
1448 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1450 char buffer[MAX_LFD_LENGTH];
1451 HKEY hkey;
1453 buffer[0] = 0;
1454 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1456 DWORD type, count = sizeof(buffer);
1457 RegQueryValueExA(hkey, ini, 0, &type, buffer, &count);
1458 RegCloseKey(hkey);
1460 if (*buffer)
1462 LFD lfd;
1463 char* pch = buffer;
1464 while( *pch && isspace(*pch) ) pch++;
1466 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1467 if (LFD_Parse(pch, &lfd) && lfd.foundry && lfd.family)
1468 XFONT_LoadDefaultLFD(&lfd, fonttype);
1469 else
1470 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1475 /***********************************************************************
1476 * XFONT_LoadDefaults
1478 static void XFONT_LoadDefaults(void)
1480 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1481 XFONT_LoadDefault(INIDefault, "");
1484 /***********************************************************************
1485 * XFONT_CreateAlias
1487 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1489 int j;
1490 fontAlias *pfa, *prev = NULL;
1492 for(pfa = aliasTable; pfa; pfa = pfa->next)
1494 /* check if we already got one */
1495 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1497 TRACE("redundant alias '%s' -> '%s'\n",
1498 lpAlias, lpTypeFace );
1499 return NULL;
1501 prev = pfa;
1504 j = strlen(lpTypeFace) + 1;
1505 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1506 j + strlen(lpAlias) + 1 );
1507 if (pfa)
1509 if (!prev)
1510 aliasTable = pfa;
1511 else
1512 prev->next = pfa;
1514 pfa->next = NULL;
1515 pfa->faTypeFace = (LPSTR)(pfa + 1);
1516 strcpy( pfa->faTypeFace, lpTypeFace );
1517 pfa->faAlias = pfa->faTypeFace + j;
1518 strcpy( pfa->faAlias, lpAlias );
1520 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1522 return pfa;
1524 return NULL;
1528 /***********************************************************************
1529 * XFONT_LoadAlias
1531 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1533 fontResource *fr, *frMatch = NULL;
1534 if (!lfd->foundry || ! lfd->family)
1536 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1537 return;
1539 for (fr = fontList; fr ; fr = fr->next)
1541 if(!strcasecmp(fr->resource->family, lpAlias))
1543 /* alias is not needed since the real font is present */
1544 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1545 return;
1547 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1549 frMatch = fr;
1550 break;
1554 if( frMatch )
1556 if( bSubst )
1558 fontAlias *pfa, *prev = NULL;
1560 for(pfa = aliasTable; pfa; pfa = pfa->next)
1562 /* Remove lpAlias from aliasTable - we should free the old entry */
1563 if(!strcmp(lpAlias, pfa->faAlias))
1565 if(prev)
1566 prev->next = pfa->next;
1567 else
1568 aliasTable = pfa->next;
1571 /* Update any references to the substituted font in aliasTable */
1572 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1574 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1575 strcpy( pfa->faTypeFace, lpAlias );
1577 prev = pfa;
1580 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1582 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1583 frMatch->fr_flags |= FR_NAMESET;
1585 else
1587 /* create new entry in the alias table */
1588 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1591 else
1593 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1597 /***********************************************************************
1598 * Just a copy of PROFILE_GetStringItem
1600 * Convenience function that turns a string 'xxx, yyy, zzz' into
1601 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1603 static char *XFONT_GetStringItem( char *start )
1605 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1606 char *lpchX, *lpch;
1608 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1610 if( *lpchX == ',' )
1612 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1613 while( *(++lpchX) )
1614 if( !XFONT_isspace(*lpchX) ) return lpchX;
1616 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1617 else lpch = NULL;
1619 if( lpch ) *lpch = '\0';
1620 return NULL;
1621 #undef XFONT_isspace
1624 /***********************************************************************
1625 * XFONT_LoadAliases
1627 * INIT ONLY
1629 * Create font aliases for some standard windows fonts using user's
1630 * default choice of (sans-)serif fonts
1632 * Read user-defined aliases from config file. Format is as follows
1634 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1636 * Example:
1637 * Alias0 = Arial, -adobe-helvetica-
1638 * Alias1 = Times New Roman, -bitstream-courier-, 1
1639 * ...
1641 * Note that from 970817 and on we have built-in alias templates that take
1642 * care of the necessary Windows typefaces.
1644 typedef struct
1646 LPSTR fatResource;
1647 LPSTR fatAlias;
1648 } aliasTemplate;
1650 static void XFONT_LoadAliases(void)
1652 char *lpResource;
1653 char buffer[MAX_LFD_LENGTH];
1654 int i = 0;
1655 LFD lfd;
1656 HKEY hkey;
1658 /* built-ins first */
1659 strcpy(buffer, "-bitstream-charter-");
1660 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1662 DWORD type, count = sizeof(buffer);
1663 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, buffer, &count);
1664 RegCloseKey(hkey);
1666 TRACE("Using '%s' as default serif font\n", buffer);
1667 if (LFD_Parse(buffer, &lfd))
1669 /* NB XFONT_InitialCapitals should not change these standard aliases */
1670 XFONT_LoadAlias( &lfd, "Tms Roman", FALSE);
1671 XFONT_LoadAlias( &lfd, "MS Serif", FALSE);
1672 XFONT_LoadAlias( &lfd, "Times New Roman", FALSE);
1674 XFONT_LoadDefaultLFD( &lfd, "serif ");
1677 strcpy(buffer, "-adobe-helvetica-");
1678 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1680 DWORD type, count = sizeof(buffer);
1681 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, buffer, &count);
1682 RegCloseKey(hkey);
1684 TRACE("Using '%s' as default sans serif font\n", buffer);
1685 if (LFD_Parse(buffer, &lfd))
1687 XFONT_LoadAlias( &lfd, "Helv", FALSE);
1688 XFONT_LoadAlias( &lfd, "MS Sans Serif", FALSE);
1689 XFONT_LoadAlias( &lfd, "MS Shell Dlg", FALSE);
1690 XFONT_LoadAlias( &lfd, "System", FALSE);
1691 XFONT_LoadAlias( &lfd, "Arial", FALSE);
1693 XFONT_LoadDefaultLFD( &lfd, "sans serif ");
1696 /* then user specified aliases */
1699 BOOL bSubst;
1700 char subsection[32];
1701 snprintf( subsection, sizeof(subsection), "%s%i", INIAliasSection, i++ );
1703 buffer[0] = 0;
1704 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1706 DWORD type, count = sizeof(buffer);
1707 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1708 RegCloseKey(hkey);
1711 if (!buffer[0])
1712 break;
1714 XFONT_InitialCapitals(buffer);
1715 lpResource = XFONT_GetStringItem( buffer );
1716 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1717 if( lpResource && *lpResource )
1719 if (LFD_Parse(lpResource, &lfd)) XFONT_LoadAlias(&lfd, buffer, bSubst);
1721 else
1722 WARN("malformed font alias '%s'\n", buffer );
1724 while(TRUE);
1727 /***********************************************************************
1728 * XFONT_UnAlias
1730 * Convert an (potential) alias into a real windows name
1733 static LPCSTR XFONT_UnAlias(char* font)
1735 if (font[0])
1737 fontAlias* fa;
1738 XFONT_InitialCapitals(font); /* to remove extra white space */
1740 for( fa = aliasTable; fa; fa = fa->next )
1741 /* use case insensitive matching to handle eg "MS Sans Serif" */
1742 if( !strcasecmp( fa->faAlias, font ) )
1744 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1745 strcpy(font, fa->faTypeFace);
1746 return fa->faAlias;
1749 return NULL;
1752 /***********************************************************************
1753 * XFONT_RemoveFontResource
1755 * Caller should check if the font resource is in use. If it is it should
1756 * set FR_REMOVED flag to delay removal until the resource is not in use
1757 * any more.
1759 static void XFONT_RemoveFontResource( fontResource** ppfr )
1761 fontResource* pfr = *ppfr;
1762 #if 0
1763 fontInfo* pfi;
1765 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1766 while( pfr->fi )
1768 pfi = pfr->fi->next;
1769 HeapFree( GetProcessHeap(), 0, pfr->fi );
1770 pfr->fi = pfi;
1772 HeapFree( GetProcessHeap(), 0, pfr );
1773 #endif
1774 *ppfr = pfr->next;
1777 /***********************************************************************
1778 * XFONT_LoadIgnores
1780 * INIT ONLY
1782 * Removes specified fonts from the font table to prevent Wine from
1783 * using it.
1785 * Ignore# = [LFD font name]
1787 * Example:
1788 * Ignore0 = -misc-nil-
1789 * Ignore1 = -sun-open look glyph-
1790 * ...
1793 static void XFONT_LoadIgnore(char* lfdname)
1795 fontResource** ppfr;
1796 LFD lfd;
1798 if (LFD_Parse(lfdname, &lfd) && lfd.foundry && lfd.family)
1800 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1802 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, &lfd) )
1804 TRACE("Ignoring '-%s-%s-'\n",
1805 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1807 XFONT_RemoveFontResource( ppfr );
1808 break;
1812 else
1813 WARN("Malformed font resource\n");
1816 static void XFONT_LoadIgnores(void)
1818 int i = 0;
1819 char subsection[32];
1820 char buffer[MAX_LFD_LENGTH];
1822 /* Standard one that noone wants */
1823 strcpy(buffer, "-misc-nil-");
1824 XFONT_LoadIgnore(buffer);
1826 /* Others from INI file */
1829 HKEY hkey;
1830 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1832 buffer[0] = 0;
1833 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1835 DWORD type, count = sizeof(buffer);
1836 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1837 RegCloseKey(hkey);
1840 if( buffer[0] )
1842 char* pch = buffer;
1843 while( *pch && isspace(*pch) ) pch++;
1844 XFONT_LoadIgnore(pch);
1846 else
1847 break;
1848 } while(TRUE);
1852 /***********************************************************************
1853 * XFONT_UserMetricsCache
1855 * Returns expanded name for the cachedmetrics file.
1856 * Now it also appends the current value of the $DISPLAY variable.
1858 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1860 const char *confdir = wine_get_config_dir();
1861 const char *display_name = XDisplayName(NULL);
1862 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1863 int display = 0;
1864 int screen = 0;
1865 char *p, *ext;
1868 ** Normalize the display name, since on Red Hat systems, DISPLAY
1869 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1870 ** after this code, all of the above will resolve to ':0.0'.
1872 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1873 p = strchr(display_name, ':');
1874 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1876 if ((len > *buf_size) &&
1877 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1879 ERR("out of memory\n");
1880 ExitProcess(1);
1882 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1884 ext = buffer + strlen(buffer);
1885 strcpy( ext, display_name );
1887 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1888 sprintf( p, ":%d.%d", display, screen );
1889 return buffer;
1893 /***********************************************************************
1894 * X Font Matching
1896 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1898 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1900 INT m;
1902 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1904 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1905 m = (const BYTE*)&fi->df.dfPixWidth - (const BYTE*)&fi->df.dfItalic;
1906 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1908 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1909 && fi->lfd_height != match->lfd_height) ||
1910 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1911 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1913 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1914 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1916 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1917 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1918 else if( m >= 0 ) return 1; /* 'match' is better */
1920 return -1; /* 'fi' is better */
1923 /***********************************************************************
1924 * XFONT_CheckFIList
1926 * REMOVE_SUBSETS - attach new fi and purge subsets
1927 * UNMARK_SUBSETS - remove subset flags from all fi entries
1929 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1931 int i = 0;
1932 fontInfo* pfi, *prev;
1934 for( prev = NULL, pfi = fr->fi; pfi; )
1936 if( action == REMOVE_SUBSETS )
1938 if( pfi->fi_flags & FI_SUBSET )
1940 fontInfo* subset = pfi;
1942 i++;
1943 fr->fi_count--;
1944 if( prev ) prev->next = pfi = pfi->next;
1945 else fr->fi = pfi = pfi->next;
1946 HeapFree( GetProcessHeap(), 0, subset );
1947 continue;
1950 else pfi->fi_flags &= ~FI_SUBSET;
1952 prev = pfi;
1953 pfi = pfi->next;
1956 if( action == REMOVE_SUBSETS ) /* also add the superset */
1958 if( fi->fi_flags & FI_SCALABLE )
1960 fi->next = fr->fi;
1961 fr->fi = fi;
1963 else if( prev ) prev->next = fi; else fr->fi = fi;
1964 fr->fi_count++;
1967 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1970 /***********************************************************************
1971 * XFONT_FindFIList
1973 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1975 while( pfr )
1977 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1978 pfr = pfr->next;
1980 /* Give the app back the font name it asked for. Encarta checks this. */
1981 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1982 return pfr;
1985 /***********************************************************************
1986 * XFONT_FixupPointSize
1988 static void XFONT_FixupPointSize(fontInfo* fi)
1990 #define df (fi->df)
1991 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1992 df.dfPoints = (INT16)
1993 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1994 df.dfVertRes );
1995 #undef df
1998 /***********************************************************************
1999 * XFONT_BuildMetrics
2001 * Build font metrics from X font
2003 static int XLoadQueryFont_ErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
2005 return 1;
2008 /* XLoadQueryFont has the bad habit of crashing when loading a bad font... */
2009 static XFontStruct *safe_XLoadQueryFont(Display *display, char *name)
2011 XFontStruct *ret;
2013 X11DRV_expect_error(display, XLoadQueryFont_ErrorHandler, NULL);
2014 ret = XLoadQueryFont(display, name);
2015 if (X11DRV_check_error()) ret = NULL;
2016 return ret;
2020 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
2022 int i;
2023 fontInfo* fi = NULL;
2024 fontResource* fr, *pfr;
2025 int n_ff = 0;
2027 MESSAGE("Building font metrics. This may take some time...\n");
2028 for( i = 0; i < x_count; i++ )
2030 char* typeface;
2031 LFD lfd;
2032 int j;
2033 char buffer[MAX_LFD_LENGTH];
2034 char* lpstr;
2035 XFontStruct* x_fs;
2036 fontInfo* pfi;
2038 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
2039 strcpy( typeface, x_pattern[i] );
2040 if (i % 10 == 0) MESSAGE("Font metrics: %.1f%% done\n", 100.0 * i / x_count);
2042 if (!LFD_Parse(typeface, &lfd))
2044 HeapFree(GetProcessHeap(), 0, typeface);
2045 continue;
2048 /* find a family to insert into */
2050 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2052 if( XFONT_SameFoundryAndFamily(fr->resource, &lfd))
2053 break;
2054 pfr = fr;
2057 if( !fi ) fi = HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2059 if( !LFD_InitFontInfo( fi, &lfd, x_pattern[i]) )
2060 goto nextfont;
2062 if( !fr ) /* add new family */
2064 n_ff++;
2065 fr = HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2066 if (fr)
2068 memset(fr, 0, sizeof(fontResource));
2070 fr->resource = HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2071 memset(fr->resource, 0, sizeof(LFD));
2073 TRACE("family: -%s-%s-\n", lfd.foundry, lfd.family );
2074 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.foundry)+1);
2075 strcpy( (char *)fr->resource->foundry, lfd.foundry );
2076 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.family)+1);
2077 strcpy( (char *)fr->resource->family, lfd.family );
2078 fr->resource->weight = "";
2080 if( pfr ) pfr->next = fr;
2081 else fontList = fr;
2083 else
2084 WARN("Not enough memory for a new font family\n");
2087 /* check if we already have something better than "fi" */
2089 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2090 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2091 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2092 if( j > 0 ) goto nextfont;
2094 /* add new font instance "fi" to the "fr" font resource */
2096 if( fi->fi_flags & FI_SCALABLE )
2098 LFD lfd1;
2099 char pxl_string[4], res_string[4];
2100 /* set scalable font height to get a basis for extrapolation */
2102 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2103 fi->lfd_resolution = res;
2105 sprintf(pxl_string, "%d", fi->lfd_height);
2106 sprintf(res_string, "%d", fi->lfd_resolution);
2108 lfd1 = lfd;
2109 lfd1.pixel_size = pxl_string;
2110 lfd1.point_size = "*";
2111 lfd1.resolution_x = res_string;
2112 lfd1.resolution_y = res_string;
2114 LFD_UnParse(buffer, sizeof(buffer), &lfd1);
2116 lpstr = buffer;
2118 else lpstr = x_pattern[i];
2120 /* X11 may return an error on some bad fonts... So be prepared to handle these. */
2121 if ((x_fs = safe_XLoadQueryFont(gdi_display, lpstr)) != 0)
2123 XFONT_SetFontMetric( fi, fr, x_fs );
2124 wine_tsx11_lock();
2125 XFreeFont( gdi_display, x_fs );
2126 wine_tsx11_unlock();
2128 XFONT_FixupPointSize(fi);
2130 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2132 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2133 fi = NULL; /* preventing reuse */
2135 else
2137 ERR("failed to load %s\n", lpstr );
2139 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2141 nextfont:
2142 HeapFree(GetProcessHeap(), 0, typeface);
2144 HeapFree(GetProcessHeap(), 0, fi);
2146 /* Scan through the font list and remove FontResource(s) (fr)
2147 * that have no associated Fontinfo(s) (fi).
2148 * This code is necessary because XFONT_ReadCachedMetrics
2149 * assumes that there is at least one fi associated with a fr.
2150 * This assumption is invalid for TT font
2151 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2154 fr = fontList;
2156 while (!fr->fi_count)
2158 fontList = fr->next;
2160 HeapFree(GetProcessHeap(), 0, fr->resource);
2161 HeapFree(GetProcessHeap(), 0, fr);
2163 fr = fontList;
2164 n_ff--;
2167 fr = fontList;
2169 while (fr->next)
2171 if (!fr->next->fi_count)
2173 pfr = fr->next;
2174 fr->next = fr->next->next;
2176 HeapFree(GetProcessHeap(), 0, pfr->resource);
2177 HeapFree(GetProcessHeap(), 0, pfr);
2179 n_ff--;
2181 else
2182 fr = fr->next;
2185 MESSAGE("Font metrics: 100.0%% done\n");
2186 return n_ff;
2189 /***********************************************************************
2190 * XFONT_ReadCachedMetrics
2192 * INIT ONLY
2194 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2196 if( fd >= 0 )
2198 unsigned u;
2199 int i, j;
2201 /* read checksums */
2202 read( fd, &u, sizeof(unsigned) );
2203 read( fd, &i, sizeof(int) );
2205 if( u == x_checksum && i == x_count )
2207 off_t length, offset = 3 * sizeof(int);
2209 /* read total size */
2210 read( fd, &i, sizeof(int) );
2211 length = lseek( fd, 0, SEEK_END );
2213 if( length == (i + offset) )
2215 lseek( fd, offset, SEEK_SET );
2216 fontList = HeapAlloc( GetProcessHeap(), 0, i);
2217 if( fontList )
2219 fontResource* pfr = fontList;
2220 fontInfo* pfi = NULL;
2222 TRACE("Reading cached font metrics:\n");
2224 read( fd, fontList, i); /* read all metrics at once */
2225 while( offset < length )
2227 offset += sizeof(fontResource) + sizeof(fontInfo);
2228 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2229 j = 1;
2230 while( TRUE )
2232 if( offset > length ||
2233 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2234 (int)(pfi->next) != j++ )
2236 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);
2237 goto fail;
2240 if( pfi->df.dfPixHeight == 0 )
2242 TRACE("error: dfPixHeight==0\n");
2243 goto fail;
2246 pfi->df.dfFace = pfr->lfFaceName;
2247 if( pfi->fi_flags & FI_SCALABLE )
2249 /* we can pretend we got this font for any resolution */
2250 pfi->lfd_resolution = res;
2251 XFONT_FixupPointSize(pfi);
2253 pfi->next = pfi + 1;
2255 if( j > pfr->fi_count ) break;
2257 pfi = pfi->next;
2258 offset += sizeof(fontInfo);
2260 pfi->next = NULL;
2261 if( pfr->next )
2263 pfr->next = (fontResource*)(pfi + 1);
2264 pfr = pfr->next;
2266 else break;
2268 if( pfr->next == NULL &&
2269 *(int*)(pfi + 1) == X_FMC_MAGIC )
2271 /* read LFD stubs */
2272 char* lpch = (char*)((int*)(pfi + 1) + 1);
2273 offset += sizeof(int);
2274 for( pfr = fontList; pfr; pfr = pfr->next )
2276 size_t len = strlen(lpch) + 1;
2277 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2278 pfr->resource = HeapAlloc(GetProcessHeap(),0,sizeof(LFD));
2279 if (!LFD_Parse(lpch, pfr->resource))
2281 HeapFree( GetProcessHeap(), 0, pfr->resource );
2282 pfr->resource = NULL;
2284 lpch += len;
2285 offset += len;
2286 if (offset > length)
2288 TRACE("error: offset=%ld length=%ld\n",(long)offset,(long)length);
2289 goto fail;
2292 close( fd );
2293 return TRUE;
2296 } else {
2297 TRACE("Wrong length: %ld!=%ld\n",(long)length,(long)(i+offset));
2299 } else {
2300 TRACE("Checksum (%x vs. %x) or count (%d vs. %d) mismatch\n",
2301 u,x_checksum,i,x_count);
2303 fail:
2304 HeapFree( GetProcessHeap(), 0, fontList );
2305 fontList = NULL;
2306 close( fd );
2308 return FALSE;
2311 /***********************************************************************
2312 * XFONT_WriteCachedMetrics
2314 * INIT ONLY
2316 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2318 fontResource* pfr;
2319 fontInfo* pfi;
2321 if( fd >= 0 )
2323 int i, j, k;
2324 char buffer[MAX_LFD_LENGTH];
2326 /* font metrics file:
2328 * +0000 x_checksum
2329 * +0004 x_count
2330 * +0008 total size to load
2331 * +000C prepackaged font metrics
2332 * ...
2333 * +...x X_FMC_MAGIC
2334 * +...x + 4 LFD stubs
2337 write( fd, &x_checksum, sizeof(unsigned) );
2338 write( fd, &x_count, sizeof(int) );
2340 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2342 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2343 i += strlen( buffer) + 1;
2344 j += pfr->fi_count;
2346 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2347 write( fd, &i, sizeof(int) );
2349 TRACE("Writing font cache:\n");
2351 for( pfr = fontList; pfr; pfr = pfr->next )
2353 fontInfo fi;
2355 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2357 i = write( fd, pfr, sizeof(fontResource) );
2358 if( i == sizeof(fontResource) )
2360 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2362 fi = *pfi;
2364 fi.df.dfFace = NULL;
2365 fi.next = (fontInfo*)k; /* loader checks this */
2367 j = write( fd, &fi, sizeof(fi) );
2368 k++;
2370 if( j == sizeof(fontInfo) ) continue;
2372 break;
2374 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2376 i = j = X_FMC_MAGIC;
2377 write( fd, &i, sizeof(int) );
2378 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2380 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2381 i = strlen( buffer ) + 1;
2382 j = write( fd, buffer, i );
2385 close( fd );
2386 return ( i == j );
2388 return FALSE;
2391 /***********************************************************************
2392 * XFONT_GetDefResolution()
2394 * INIT ONLY
2396 * Here we initialize DefResolution which is used in the
2397 * XFONT_Match() penalty function, based on the values of log_pixels
2399 static int XFONT_GetDefResolution( int log_pixels_x, int log_pixels_y )
2401 int i, j, num = 3;
2402 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2403 int best = 0, best_diff = 65536;
2405 /* FIXME We can only really guess at a best DefResolution
2406 * - this should be configurable
2408 for( i = best = 0; i < num; i++ )
2410 j = abs( log_pixels_x - allowed_xfont_resolutions[i] );
2411 if( j < best_diff )
2413 best = i;
2414 best_diff = j;
2417 DefResolution = allowed_xfont_resolutions[best];
2418 return DefResolution;
2422 /***********************************************************************
2423 * XFONT_Match
2425 * Compute the matching score between the logical font and the device font.
2427 * contributions from highest to lowest:
2428 * charset
2429 * fixed pitch
2430 * height
2431 * family flags (only when the facename is not present)
2432 * width
2433 * weight, italics, underlines, strikeouts
2435 * NOTE: you can experiment with different penalty weights to see what happens.
2436 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2438 static UINT XFONT_Match( fontMatch* pfm )
2440 fontInfo* pfi = pfm->pfi; /* device font to match */
2441 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2442 UINT penalty = 0;
2443 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from text_caps */
2444 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2445 int d = 0, height;
2447 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2448 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2449 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2450 (pfi->df.dfItalic) ? "Italic" : "" );
2452 pfm->flags &= FO_MATCH_MASK;
2454 /* Charset */
2455 /* pfm->internal_charset: given(required) charset */
2456 /* pfi->internal_charset: charset of this font */
2457 if (pfi->internal_charset == DEFAULT_CHARSET)
2459 /* special case(unicode font) */
2460 /* priority: unmatched charset < unicode < matched charset */
2461 penalty += 0x50;
2463 else
2465 if( pfm->internal_charset == DEFAULT_CHARSET )
2468 if (pfi->internal_charset != ANSI_CHARSET)
2469 penalty += 0x200;
2471 if ( pfi->codepage != GetACP() )
2472 penalty += 0x200;
2474 else if (pfm->internal_charset != pfi->internal_charset)
2476 if ( pfi->internal_charset & 0xff00 )
2477 penalty += 0x1000; /* internal charset - should not be used */
2478 else
2479 penalty += 0x200;
2483 /* Height */
2484 height = -1;
2486 if( plf->lfHeight > 0 )
2488 int h = pfi->df.dfPixHeight;
2489 d = h - plf->lfHeight;
2490 height = plf->lfHeight;
2492 else
2494 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2495 if (h)
2497 d = h + plf->lfHeight;
2498 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2500 else
2502 ERR("PixHeight == InternalLeading\n");
2503 penalty += 0x1000; /* don't want this */
2508 if( height == 0 )
2509 pfm->height = 1; /* Very small */
2510 else if( d )
2512 if( bScale )
2513 pfm->height = height;
2514 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2516 if( d > 0 ) /* do not shrink raster fonts */
2518 pfm->height = pfi->df.dfPixHeight;
2519 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2521 else /* expand only in integer multiples */
2523 pfm->height = height - height%pfi->df.dfPixHeight;
2524 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2527 else /* can't be scaled at all */
2529 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2530 pfm->height = pfi->df.dfPixHeight;
2531 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2534 else
2535 pfm->height = pfi->df.dfPixHeight;
2537 /* Pitch and Family */
2538 if( pfm->flags & FO_MATCH_PAF ) {
2539 int family = plf->lfPitchAndFamily & FF_FAMILY;
2541 /* TMPF_FIXED_PITCH means exactly the opposite */
2542 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2543 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2544 } else /* Variable is the default */
2545 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2547 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2548 penalty += 0x10;
2551 /* Width */
2552 if( plf->lfWidth )
2554 int h;
2555 if( bR6 || bScale ) h = 0;
2556 else
2558 /* FIXME: not complete */
2560 pfm->flags |= FO_SYNTH_WIDTH;
2561 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2563 penalty += h * ( d ) ? 0x2 : 0x1 ;
2565 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2567 /* Weight */
2568 if( plf->lfWeight != FW_DONTCARE )
2570 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2571 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2572 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2574 /* Italic */
2575 if( plf->lfItalic != pfi->df.dfItalic )
2577 penalty += 0x4;
2578 pfm->flags |= FO_SYNTH_ITALIC;
2580 /* Underline */
2581 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2583 /* Strikeout */
2584 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2587 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2588 penalty++;
2590 TRACE(" returning %i\n", penalty );
2592 return penalty;
2595 /***********************************************************************
2596 * XFONT_MatchFIList
2598 * Scan a particular font resource for the best match.
2600 static UINT XFONT_MatchFIList( fontMatch* pfm )
2602 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2603 UINT current_score, score = (UINT)(-1);
2604 fontMatch fm = *pfm;
2606 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2608 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2609 continue;
2611 current_score = XFONT_Match( &fm );
2612 if( score > current_score )
2614 *pfm = fm;
2615 score = current_score;
2618 return score;
2621 /***********************************************************************
2622 * XFONT_is_ansi_charset
2624 * returns TRUE if the given charset is ANSI_CHARSET.
2626 static BOOL XFONT_is_ansi_charset( UINT charset )
2628 return ((charset == ANSI_CHARSET) ||
2629 (charset == DEFAULT_CHARSET && GetACP() == 1252));
2632 /***********************************************************************
2633 * XFONT_MatchDeviceFont
2635 * Scan font resource tree.
2638 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2640 fontMatch fm = *pfm;
2641 UINT current_score, score = (UINT)(-1);
2642 fontResource** ppfr;
2644 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2645 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2646 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2648 pfm->pfi = NULL;
2650 /* the following hard-coded font binding assumes 'ANSI_CHARSET'. */
2651 if( !fm.plf->lfFaceName[0] &&
2652 XFONT_is_ansi_charset(fm.plf->lfCharSet) )
2654 switch(fm.plf->lfPitchAndFamily & 0xf0)
2656 case FF_MODERN:
2657 strcpy(fm.plf->lfFaceName, "Courier New");
2658 break;
2659 case FF_ROMAN:
2660 strcpy(fm.plf->lfFaceName, "Times New Roman");
2661 break;
2662 case FF_SWISS:
2663 strcpy(fm.plf->lfFaceName, "Arial");
2664 break;
2665 default:
2666 if((fm.plf->lfPitchAndFamily & 0x0f) == FIXED_PITCH)
2667 strcpy(fm.plf->lfFaceName, "Courier New");
2668 else
2669 strcpy(fm.plf->lfFaceName, "Arial");
2670 break;
2674 if( fm.plf->lfFaceName[0] )
2676 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2677 if( fm.pfr ) /* match family */
2679 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2681 if( fm.pfr->fr_flags & FR_REMOVED )
2682 fm.pfr = 0;
2683 else
2685 XFONT_MatchFIList( &fm );
2686 *pfm = fm;
2687 if (pfm->pfi)
2688 return;
2692 /* get charset if lfFaceName is one of known facenames. */
2694 const struct CharsetBindingInfo* pcharsetbindings;
2696 pcharsetbindings = &charsetbindings[0];
2697 while ( pcharsetbindings->pszFaceName != NULL )
2699 if ( !strcmp( pcharsetbindings->pszFaceName,
2700 fm.plf->lfFaceName ) )
2702 fm.internal_charset = pcharsetbindings->charset;
2703 break;
2705 pcharsetbindings ++;
2707 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2711 /* match all available fonts */
2713 fm.flags |= FO_MATCH_PAF;
2714 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2716 if( (*ppfr)->fr_flags & FR_REMOVED )
2718 if( (*ppfr)->fo_count == 0 )
2719 XFONT_RemoveFontResource( ppfr );
2720 continue;
2723 fm.pfr = *ppfr;
2725 TRACE("%s\n", fm.pfr->lfFaceName );
2727 current_score = XFONT_MatchFIList( &fm );
2728 if( current_score < score )
2730 score = current_score;
2731 *pfm = fm;
2737 /***********************************************************************
2738 * X Font Cache
2740 static void XFONT_GrowFreeList(int start, int end)
2742 /* add all entries from 'start' up to and including 'end' */
2744 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2746 fontCache[end].lru = fontLF;
2747 fontCache[end].count = -1;
2748 fontLF = start;
2749 while( start < end )
2751 fontCache[start].count = -1;
2752 fontCache[start].lru = start + 1;
2753 start++;
2757 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2759 UINT16 cs = __lfCheckSum( plf );
2760 int i = fontMRU, prev = -1;
2762 *checksum = cs;
2763 while( i >= 0 )
2765 if( fontCache[i].lfchecksum == cs &&
2766 !(fontCache[i].fo_flags & FO_REMOVED) )
2768 /* FIXME: something more intelligent here ? */
2770 if( !memcmp( plf, &fontCache[i].lf,
2771 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2772 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2774 /* remove temporarily from the lru list */
2775 if( prev >= 0 )
2776 fontCache[prev].lru = fontCache[i].lru;
2777 else
2778 fontMRU = (INT16)fontCache[i].lru;
2779 return (fontCache + i);
2782 prev = i;
2783 i = (INT16)fontCache[i].lru;
2785 return NULL;
2788 static fontObject* XFONT_GetCacheEntry(void)
2790 int i;
2792 if( fontLF == -1 )
2794 int prev_i, prev_j, j;
2796 TRACE("font cache is full\n");
2798 /* lookup the least recently used font */
2800 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2802 if( fontCache[i].count <= 0 &&
2803 !(fontCache[i].fo_flags & FO_SYSTEM) )
2805 prev_j = prev_i;
2806 j = i;
2808 prev_i = i;
2811 if( j >= 0 ) /* unload font */
2813 /* detach from the lru list */
2815 TRACE("\tfreeing entry %i\n", j );
2817 fontCache[j].fr->fo_count--;
2819 if( prev_j >= 0 )
2820 fontCache[prev_j].lru = fontCache[j].lru;
2821 else fontMRU = (INT16)fontCache[j].lru;
2823 /* FIXME: lpXForm, lpPixmap */
2824 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2826 wine_tsx11_lock();
2827 XFreeFont( gdi_display, fontCache[j].fs );
2828 wine_tsx11_unlock();
2830 memset( fontCache + j, 0, sizeof(fontObject) );
2831 return (fontCache + j);
2833 else /* expand cache */
2835 fontObject* newCache;
2837 prev_i = fontCacheSize + FONTCACHE;
2839 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2841 if( (newCache = HeapReAlloc(GetProcessHeap(), 0, fontCache, prev_i * sizeof(fontObject))) )
2843 i = fontCacheSize;
2844 fontCacheSize = prev_i;
2845 fontCache = newCache;
2846 XFONT_GrowFreeList( i, fontCacheSize - 1);
2848 else return NULL;
2852 /* detach from the free list */
2854 i = fontLF;
2855 fontLF = (INT16)fontCache[i].lru;
2856 fontCache[i].count = 0;
2857 return (fontCache + i);
2860 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2862 UINT u = (UINT)(pfo - fontCache);
2863 int i;
2864 int ret;
2866 if( u < fontCacheSize )
2868 ret = --fontCache[u].count;
2869 if ( ret == 0 )
2871 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2873 if( CHECK_PFONT(pfo->prefobjs[i]) )
2874 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2878 return ret;
2881 return -1;
2884 /***********************************************************************
2885 * X11DRV_FONT_InitX11Metrics
2887 * Initialize font resource list and allocate font cache.
2889 void X11DRV_FONT_InitX11Metrics( void )
2891 char** x_pattern;
2892 unsigned x_checksum;
2893 int i, x_count, fd, buf_size;
2894 char *buffer;
2895 HKEY hkey;
2898 wine_tsx11_lock();
2899 x_pattern = XListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2900 wine_tsx11_unlock();
2902 TRACE("Font Mapper: initializing %i x11 fonts\n", x_count);
2903 if (x_count == MAX_FONTS)
2904 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2906 for( i = x_checksum = 0; i < x_count; i++ )
2908 int j;
2909 #if 0
2910 printf("%i\t: %s\n", i, x_pattern[i] );
2911 #endif
2913 j = strlen( x_pattern[i] );
2914 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2916 x_checksum |= X_PFONT_MAGIC;
2917 buf_size = 128;
2918 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2920 /* deal with systemwide font metrics cache */
2922 buffer[0] = 0;
2923 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2925 DWORD type, count = buf_size;
2926 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, buffer, &count);
2927 RegCloseKey(hkey);
2930 if( buffer[0] )
2932 fd = open( buffer, O_RDONLY );
2933 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2935 if (fontList == NULL)
2937 /* try per-user */
2938 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2939 if( buffer[0] )
2941 fd = open( buffer, O_RDONLY );
2942 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2946 if( fontList == NULL ) /* build metrics from scratch */
2948 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2949 if( buffer[0] ) /* update cached metrics */
2951 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2952 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2954 WARN("Unable to write to fontcache '%s'\n", buffer);
2955 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2960 wine_tsx11_lock();
2961 XFreeFontNames(x_pattern);
2963 /* check if we're dealing with X11 R6 server */
2965 XFontStruct* x_fs;
2966 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2967 if( (x_fs = safe_XLoadQueryFont(gdi_display, buffer)) )
2969 text_caps |= TC_SF_X_YINDEP;
2970 XFreeFont(gdi_display, x_fs);
2973 wine_tsx11_unlock();
2975 HeapFree(GetProcessHeap(), 0, buffer);
2977 XFONT_WindowsNames();
2978 XFONT_LoadAliases();
2979 XFONT_LoadDefaults();
2980 XFONT_LoadIgnores();
2982 /* fontList initialization is over, allocate X font cache */
2984 fontCache = HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2985 XFONT_GrowFreeList(0, fontCacheSize - 1);
2987 TRACE("done!\n");
2990 /***********************************************************************
2991 * X11DRV_FONT_Init
2993 void X11DRV_FONT_Init( int log_pixels_x, int log_pixels_y )
2995 XFONT_GetDefResolution( log_pixels_x, log_pixels_y );
2997 if(using_client_side_fonts)
2998 text_caps |= TC_VA_ABLE;
3000 return;
3003 /**********************************************************************
3004 * XFONT_SetX11Trans
3006 static BOOL XFONT_SetX11Trans( fontObject *pfo )
3008 char *fontName;
3009 Atom nameAtom;
3010 LFD lfd;
3012 wine_tsx11_lock();
3013 XGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
3014 fontName = XGetAtomName( gdi_display, nameAtom );
3015 if (!LFD_Parse(fontName, &lfd) || lfd.pixel_size[0] != '[')
3017 XFree(fontName);
3018 wine_tsx11_unlock();
3019 return FALSE;
3021 #define PX pfo->lpX11Trans
3023 sscanf(lfd.pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
3024 XFree(fontName);
3026 XGetFontProperty( pfo->fs, x11drv_atom(RAW_ASCENT), &PX->RAW_ASCENT );
3027 XGetFontProperty( pfo->fs, x11drv_atom(RAW_DESCENT), &PX->RAW_DESCENT );
3028 wine_tsx11_unlock();
3030 PX->pixelsize = hypot(PX->a, PX->b);
3031 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
3032 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
3034 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
3035 PX->a, PX->b, PX->c, PX->d,
3036 PX->RAW_ASCENT, PX->RAW_DESCENT);
3038 #undef PX
3039 return TRUE;
3042 /***********************************************************************
3043 * X Device Font Objects
3045 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
3046 LPCSTR* faceMatched, BOOL bSubFont,
3047 WORD internal_charset,
3048 WORD* pcharsetMatched )
3050 UINT16 checksum;
3051 INT index = 0;
3052 fontObject* pfo;
3054 pfo = XFONT_LookupCachedFont( plf, &checksum );
3055 if( !pfo )
3057 fontMatch fm;
3058 INT i;
3060 fm.pfr = NULL;
3061 fm.pfi = NULL;
3062 fm.height = 0;
3063 fm.flags = 0;
3064 fm.plf = plf;
3065 fm.internal_charset = internal_charset;
3067 if( text_caps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
3069 /* allocate new font cache entry */
3071 if( (pfo = XFONT_GetCacheEntry()) )
3073 /* initialize entry and load font */
3074 char lpLFD[MAX_LFD_LENGTH];
3075 UINT uRelaxLevel = 0;
3077 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
3078 ERR(
3079 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
3080 plf->lfHeight);
3081 pfo->rescale = fabs(plf->lfHeight / 100.0);
3082 if(plf->lfHeight > 0) plf->lfHeight = 100;
3083 else plf->lfHeight = -100;
3084 } else
3085 pfo->rescale = 1.0;
3087 XFONT_MatchDeviceFont( fontList, &fm );
3088 pfo->fr = fm.pfr;
3089 pfo->fi = fm.pfi;
3090 pfo->fr->fo_count++;
3091 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3093 pfo->lf = *plf;
3094 pfo->lfchecksum = checksum;
3098 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3099 if( (pfo->fs = safe_XLoadQueryFont( gdi_display, lpLFD )) ) break;
3100 } while( uRelaxLevel );
3103 if(pfo->lf.lfEscapement != 0) {
3104 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3105 if(!XFONT_SetX11Trans( pfo )) {
3106 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3107 pfo->lpX11Trans = NULL;
3110 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3111 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3112 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3113 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3115 /* FIXME: If we've got a soft font or
3116 * there are FO_SYNTH_... flags for the
3117 * non PROOF_QUALITY request, the engine
3118 * should rasterize characters into mono
3119 * pixmaps and store them in the pfo->lpPixmap
3120 * array (pfo->fs should be updated as well).
3121 * array (pfo->fs should be updated as well).
3122 * X11DRV_ExtTextOut() must be heavily modified
3123 * to support pixmap blitting and FO_SYNTH_...
3124 * styles.
3127 pfo->lpPixmap = NULL;
3129 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3130 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3132 /* special treatment for DBCS that needs multiple fonts */
3133 /* All member of pfo must be set correctly. */
3134 if ( bSubFont == FALSE )
3136 WORD charset_sub;
3137 WORD charsetMatchedSub;
3138 LOGFONT16 lfSub;
3139 LPCSTR faceMatchedSub;
3141 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3143 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3144 penum_subfont_charset( i );
3145 if ( charset_sub == DEFAULT_CHARSET ) break;
3147 lfSub = *plf;
3148 lfSub.lfWidth = 0;
3149 lfSub.lfHeight=plf->lfHeight;
3150 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3151 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3152 /* this font has sub font */
3153 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3154 pfo->prefobjs[i] =
3155 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3156 TRUE, charset_sub,
3157 &charsetMatchedSub );
3158 /* FIXME: check charsetMatchedSub */
3163 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3165 UINT current_score, score = (UINT)(-1);
3167 i = index = fontMRU;
3168 fm.flags |= FO_MATCH_PAF;
3171 pfo = fontCache + i;
3172 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3174 current_score = XFONT_Match( &fm );
3175 if( current_score < score ) index = i;
3177 i = pfo->lru;
3178 } while( i >= 0 );
3179 pfo = fontCache + index;
3180 goto END;
3184 /* attach at the head of the lru list */
3185 pfo->lru = fontMRU;
3186 index = fontMRU = (pfo - fontCache);
3188 END:
3189 pfo->count++;
3191 TRACE("physfont %i\n", index);
3192 *faceMatched = pfo->fi->df.dfFace;
3193 *pcharsetMatched = pfo->fi->internal_charset;
3195 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3198 /***********************************************************************
3199 * XFONT_GetFontObject
3201 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3203 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3204 return NULL;
3207 /***********************************************************************
3208 * XFONT_GetFontStruct
3210 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3212 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3213 return NULL;
3216 /***********************************************************************
3217 * XFONT_GetFontInfo
3219 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3221 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3222 return NULL;
3227 /* X11DRV Interface ****************************************************
3229 * Exposed via the dc->funcs dispatch table. *
3231 ***********************************************************************/
3232 /***********************************************************************
3233 * SelectFont (X11DRV.@)
3235 HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont, HANDLE gdiFont )
3237 LOGFONTW logfont;
3238 LOGFONT16 lf;
3240 TRACE("hdc=%p, hfont=%p\n", physDev->hdc, hfont);
3242 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return HGDI_ERROR;
3244 TRACE("gdiFont = %p\n", gdiFont);
3246 if(gdiFont && using_client_side_fonts) {
3247 X11DRV_XRender_SelectFont(physDev, hfont);
3248 physDev->has_gdi_font = TRUE;
3249 return FALSE;
3252 EnterCriticalSection( &crtsc_fonts_X11 );
3254 if(fontList == NULL) X11DRV_FONT_InitX11Metrics();
3256 if( CHECK_PFONT(physDev->font) )
3257 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3259 FONT_LogFontWTo16(&logfont, &lf);
3261 /* stock fonts ignore the mapping mode */
3262 if (!is_stock_font( hfont ))
3264 /* Make sure we don't change the sign when converting to device coords */
3265 /* FIXME - check that the other drivers do this correctly */
3266 if (lf.lfWidth)
3268 INT width = X11DRV_XWStoDS( physDev, lf.lfWidth );
3269 lf.lfWidth = (lf.lfWidth < 0) ? -abs(width) : abs(width);
3270 if (lf.lfWidth == 0)
3271 lf.lfWidth = 1; /* Minimum width */
3273 if (lf.lfHeight)
3275 INT height = X11DRV_YWStoDS( physDev, lf.lfHeight );
3276 lf.lfHeight = (lf.lfHeight < 0) ? -abs(height) : abs(height);
3277 if (lf.lfHeight == 0)
3278 lf.lfHeight = MIN_FONT_SIZE;
3282 if (!lf.lfHeight)
3283 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(physDev->hdc,LOGPIXELSY) + (72>>1)) / 72;
3286 /* Fixup aliases before passing to RealizeFont */
3287 /* alias = Windows name in the alias table */
3288 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3289 LPCSTR faceMatched;
3290 WORD charsetMatched;
3292 TRACE("hfont=%p\n", hfont); /* to connect with the trace from RealizeFont */
3293 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3294 FALSE, lf.lfCharSet,
3295 &charsetMatched );
3297 /* set face to the requested facename if it matched
3298 * so that GetTextFace can get the correct face name
3300 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3301 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3302 logfont.lfFaceName, LF_FACESIZE);
3303 else
3304 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3305 logfont.lfFaceName, LF_FACESIZE);
3308 * In X, some encodings may have the same lfFaceName.
3309 * for example:
3310 * -misc-fixed-*-iso8859-1
3311 * -misc-fixed-*-jisx0208.1990-0
3312 * so charset should be saved...
3314 logfont.lfCharSet = charsetMatched;
3317 LeaveCriticalSection( &crtsc_fonts_X11 );
3319 physDev->has_gdi_font = FALSE;
3320 return (HFONT)1; /* Use a device font */
3324 /***********************************************************************
3326 * X11DRV_EnumDeviceFonts
3328 BOOL X11DRV_EnumDeviceFonts( X11DRV_PDEVICE *physDev, LPLOGFONTW plf,
3329 FONTENUMPROCW proc, LPARAM lp )
3331 ENUMLOGFONTEXW lf;
3332 NEWTEXTMETRICEXW tm;
3333 fontResource* pfr = fontList;
3334 BOOL b, bRet = 0;
3336 /* don't enumerate x11 fonts if we're using client side fonts */
3337 if (physDev->has_gdi_font) return FALSE;
3339 if( plf->lfFaceName[0] )
3341 char facename[LF_FACESIZE+1];
3342 WideCharToMultiByte( CP_ACP, 0, plf->lfFaceName, -1,
3343 facename, sizeof(facename), NULL, NULL );
3344 /* enum all entries in this resource */
3345 pfr = XFONT_FindFIList( pfr, facename );
3346 if( pfr )
3348 fontInfo* pfi;
3349 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3351 /* Note: XFONT_GetFontMetric() will have to
3352 release the crit section, font list will
3353 have to be retraversed on return */
3355 if(plf->lfCharSet == DEFAULT_CHARSET ||
3356 plf->lfCharSet == pfi->df.dfCharSet) {
3357 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3358 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3359 bRet = b;
3360 else break;
3365 else /* enum first entry in each resource */
3366 for( ; pfr ; pfr = pfr->next )
3368 if(pfr->fi)
3370 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3371 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3372 bRet = b;
3373 else break;
3376 return bRet;
3380 /***********************************************************************
3381 * X11DRV_GetTextMetrics
3383 BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
3385 if( CHECK_PFONT(physDev->font) )
3387 fontObject* pfo = __PFONT(physDev->font);
3388 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsW( pfo, metrics );
3389 return TRUE;
3391 return FALSE;
3395 /***********************************************************************
3396 * X11DRV_GetCharWidth
3398 BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
3399 LPINT buffer )
3401 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3403 if( pfo )
3405 unsigned int i;
3407 if (pfo->fs->per_char == NULL)
3408 for (i = firstChar; i <= lastChar; i++)
3409 if(pfo->lpX11Trans)
3410 *buffer++ = pfo->fs->min_bounds.attributes *
3411 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3412 else
3413 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3414 else
3416 XCharStruct *cs, *def;
3417 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3419 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3420 def);
3422 for (i = firstChar; i <= lastChar; i++)
3424 WCHAR wch = i;
3425 BYTE ch;
3426 UINT ch_f; /* character code in the font encoding */
3427 WideCharToMultiByte( pfo->fi->codepage, 0, &wch, 1, &ch, 1, NULL, NULL );
3428 ch_f = ch;
3429 if (ch_f >= pfo->fs->min_char_or_byte2 &&
3430 ch_f <= pfo->fs->max_char_or_byte2)
3432 cs = &pfo->fs->per_char[(ch_f - pfo->fs->min_char_or_byte2)];
3433 if (CI_NONEXISTCHAR(cs)) cs = def;
3434 } else cs = def;
3435 if(pfo->lpX11Trans)
3436 *buffer++ = max(cs->attributes, 0) *
3437 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3438 else
3439 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3443 return TRUE;
3445 return FALSE;