Get rid of some direct accesses to the DC structure from outside GDI.
[wine/multimedia.git] / dlls / x11drv / xfont.c
blobc7e6102552c51b7ef6fd8f8af80b7937fad0c5ae
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 "gdi.h"
46 #include "wine/library.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(font);
52 #define X_PFONT_MAGIC (0xFADE0000)
53 #define X_FMC_MAGIC (0x0000CAFE)
55 #define MAX_FONTS 1024*16
56 #define MAX_LFD_LENGTH 256
57 #define TILDE '~'
58 #define HYPHEN '-'
60 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
61 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
62 #define MIN_FONT_SIZE 2 /* Min size in pixels */
63 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
65 #define REMOVE_SUBSETS 1
66 #define UNMARK_SUBSETS 0
68 #define FONTCACHE 32 /* dynamic font cache size */
70 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
72 typedef struct __fontAlias
74 LPSTR faTypeFace;
75 LPSTR faAlias;
76 struct __fontAlias* next;
77 } fontAlias;
79 static fontAlias *aliasTable = NULL;
81 static const char* INIFontMetrics = "cachedmetrics.";
82 static const char* INIFontSection = "Software\\Wine\\Wine\\Config\\fonts";
83 static const char* INIAliasSection = "Alias";
84 static const char* INIIgnoreSection = "Ignore";
85 static const char* INIDefault = "Default";
86 static const char* INIDefaultFixed = "DefaultFixed";
87 static const char* INIResolution = "Resolution";
88 static const char* INIGlobalMetrics = "FontMetrics";
89 static const char* INIDefaultSerif = "DefaultSerif";
90 static const char* INIDefaultSansSerif = "DefaultSansSerif";
93 /* FIXME - are there any more Latin charsets ? */
94 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
95 #define IS_LATIN_CHARSET(ch) \
96 ((ch)==ANSI_CHARSET ||\
97 (ch)==EE_CHARSET ||\
98 (ch)==ISO3_CHARSET ||\
99 (ch)==ISO4_CHARSET ||\
100 (ch)==RUSSIAN_CHARSET ||\
101 (ch)==ARABIC_CHARSET ||\
102 (ch)==GREEK_CHARSET ||\
103 (ch)==HEBREW_CHARSET ||\
104 (ch)==TURKISH_CHARSET ||\
105 (ch)==ISO10_CHARSET ||\
106 (ch)==BALTIC_CHARSET ||\
107 (ch)==CELTIC_CHARSET)
109 /* suffix-charset mapping tables - must be less than 254 entries long */
111 typedef struct __sufch
113 LPCSTR psuffix;
114 WORD charset; /* hibyte != 0 means *internal* charset */
115 WORD codepage;
116 WORD cptable;
117 } SuffixCharset;
119 static const SuffixCharset sufch_ansi[] = {
120 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
121 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
123 static const SuffixCharset sufch_iso646[] = {
124 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
125 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
127 static const SuffixCharset sufch_iso8859[] = {
128 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
129 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
130 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
131 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
132 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
133 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
134 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
135 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
136 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
137 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
138 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
139 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
140 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
141 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
142 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
143 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
145 static const SuffixCharset sufch_microsoft[] = {
146 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
147 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
148 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
149 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
150 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
151 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
152 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
153 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
154 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
155 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
156 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
158 static const SuffixCharset sufch_tcvn[] = {
159 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
160 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
162 static const SuffixCharset sufch_tis620[] = {
163 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
164 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
166 static const SuffixCharset sufch_viscii[] = {
167 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
168 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
170 static const SuffixCharset sufch_windows[] = {
171 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
172 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
173 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
174 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
175 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
176 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
177 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
178 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
179 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
181 static const SuffixCharset sufch_koi8[] = {
182 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
183 { "ru", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
184 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
185 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
187 static const SuffixCharset sufch_jisx0201[] = {
188 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
189 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
191 static const SuffixCharset sufch_jisx0208[] = {
192 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
193 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
195 static const SuffixCharset sufch_jisx0212[] = {
196 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
197 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
199 static const SuffixCharset sufch_ksc5601[] = {
200 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
201 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
203 static const SuffixCharset sufch_gb2312[] = {
204 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
205 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
207 static const SuffixCharset sufch_big5[] = {
208 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
209 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
211 static const SuffixCharset sufch_unicode[] = {
212 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
213 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
215 static const SuffixCharset sufch_iso10646[] = {
216 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
217 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
219 static const SuffixCharset sufch_dec[] = {
220 { "dectech", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
221 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
223 /* Each of these must be matched explicitly */
224 static const SuffixCharset sufch_any[] = {
225 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
226 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
229 typedef struct __fet
231 LPCSTR prefix;
232 const SuffixCharset* sufch;
233 struct __fet* next;
234 } fontEncodingTemplate;
236 /* Note: we can attach additional encoding mappings to the end
237 * of this table at runtime.
239 static fontEncodingTemplate __fETTable[] = {
240 { "ansi", sufch_ansi, &__fETTable[1] },
241 { "ascii", sufch_ansi, &__fETTable[2] },
242 { "iso646.1991", sufch_iso646, &__fETTable[3] },
243 { "iso8859", sufch_iso8859, &__fETTable[4] },
244 { "microsoft", sufch_microsoft, &__fETTable[5] },
245 { "tcvn", sufch_tcvn, &__fETTable[6] },
246 { "tis620.2533", sufch_tis620, &__fETTable[7] },
247 { "viscii1.1", sufch_viscii, &__fETTable[8] },
248 { "windows", sufch_windows, &__fETTable[9] },
249 { "koi8", sufch_koi8, &__fETTable[10]},
250 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
251 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
252 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
253 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
254 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
255 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
256 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
257 { "big5", sufch_big5, &__fETTable[18]},
258 { "unicode", sufch_unicode, &__fETTable[19]},
259 { "iso10646", sufch_iso10646, &__fETTable[20]},
260 { "cp", sufch_windows, &__fETTable[21]},
261 { "dec", sufch_dec, &__fETTable[22]},
262 /* NULL prefix matches anything so put it last */
263 { NULL, sufch_any, NULL },
265 static fontEncodingTemplate* fETTable = __fETTable;
267 /* a charset database for known facenames */
268 struct CharsetBindingInfo
270 const char* pszFaceName;
271 BYTE charset;
273 static const struct CharsetBindingInfo charsetbindings[] =
275 /* special facenames */
276 { "System", DEFAULT_CHARSET },
277 { "FixedSys", DEFAULT_CHARSET },
279 /* known facenames */
280 { "MS Serif", ANSI_CHARSET },
281 { "MS Sans Serif", ANSI_CHARSET },
282 { "Courier", ANSI_CHARSET },
283 { "Symbol", SYMBOL_CHARSET },
285 { "Arial", ANSI_CHARSET },
286 { "Arial Greek", GREEK_CHARSET },
287 { "Arial Tur", TURKISH_CHARSET },
288 { "Arial Baltic", BALTIC_CHARSET },
289 { "Arial CE", EASTEUROPE_CHARSET },
290 { "Arial Cyr", RUSSIAN_CHARSET },
291 { "Courier New", ANSI_CHARSET },
292 { "Courier New Greek", GREEK_CHARSET },
293 { "Courier New Tur", TURKISH_CHARSET },
294 { "Courier New Baltic", BALTIC_CHARSET },
295 { "Courier New CE", EASTEUROPE_CHARSET },
296 { "Courier New Cyr", RUSSIAN_CHARSET },
297 { "Times New Roman", ANSI_CHARSET },
298 { "Times New Roman Greek", GREEK_CHARSET },
299 { "Times New Roman Tur", TURKISH_CHARSET },
300 { "Times New Roman Baltic", BALTIC_CHARSET },
301 { "Times New Roman CE", EASTEUROPE_CHARSET },
302 { "Times New Roman Cyr", RUSSIAN_CHARSET },
304 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
305 SHIFTJIS_CHARSET }, /* MS gothic */
306 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
307 SHIFTJIS_CHARSET }, /* MS P gothic */
308 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
309 SHIFTJIS_CHARSET }, /* MS mincho */
310 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
311 SHIFTJIS_CHARSET }, /* MS P mincho */
312 { "GulimChe", HANGEUL_CHARSET },
313 { "MS Song", GB2312_CHARSET },
314 { "MS Hei", GB2312_CHARSET },
315 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
316 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
318 { NULL, 0 }
322 static int DefResolution = 0;
324 static CRITICAL_SECTION crtsc_fonts_X11;
325 static CRITICAL_SECTION_DEBUG critsect_debug =
327 0, 0, &crtsc_fonts_X11,
328 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
329 0, 0, { 0, (DWORD)(__FILE__ ": crtsc_fonts_X11") }
331 static CRITICAL_SECTION crtsc_fonts_X11 = { &critsect_debug, -1, 0, 0, 0, 0 };
333 static fontResource* fontList = NULL;
334 static fontObject* fontCache = NULL; /* array */
335 static int fontCacheSize = FONTCACHE;
336 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
338 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
339 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
340 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
342 /***********************************************************************
343 * Helper macros from X distribution
346 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
347 (((cs)->rbearing|(cs)->lbearing| \
348 (cs)->ascent|(cs)->descent) == 0))
350 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
352 cs = def; \
353 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
354 if (fs->per_char == NULL) { \
355 cs = &fs->min_bounds; \
356 } else { \
357 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
358 if (CI_NONEXISTCHAR(cs)) cs = def; \
363 #define CI_GET_DEFAULT_INFO(fs,cs) \
364 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
367 /***********************************************************************
368 * is_stock_font
370 inline static BOOL is_stock_font( HFONT font )
372 int i;
373 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
375 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
377 return FALSE;
381 static void FONT_LogFontWTo16( const LOGFONTW* font32, LPLOGFONT16 font16 )
383 font16->lfHeight = font32->lfHeight;
384 font16->lfWidth = font32->lfWidth;
385 font16->lfEscapement = font32->lfEscapement;
386 font16->lfOrientation = font32->lfOrientation;
387 font16->lfWeight = font32->lfWeight;
388 font16->lfItalic = font32->lfItalic;
389 font16->lfUnderline = font32->lfUnderline;
390 font16->lfStrikeOut = font32->lfStrikeOut;
391 font16->lfCharSet = font32->lfCharSet;
392 font16->lfOutPrecision = font32->lfOutPrecision;
393 font16->lfClipPrecision = font32->lfClipPrecision;
394 font16->lfQuality = font32->lfQuality;
395 font16->lfPitchAndFamily = font32->lfPitchAndFamily;
396 WideCharToMultiByte( CP_ACP, 0, font32->lfFaceName, -1,
397 font16->lfFaceName, LF_FACESIZE, NULL, NULL );
398 font16->lfFaceName[LF_FACESIZE-1] = 0;
402 /***********************************************************************
403 * Checksums
405 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
407 CHAR font[LF_FACESIZE];
408 UINT16 checksum = 0;
409 UINT16 *ptr;
410 int i;
412 ptr = (UINT16 *)plf;
413 for (i = 0; i < 9; i++) checksum ^= *ptr++;
414 for (i = 0; i < LF_FACESIZE; i++)
416 font[i] = tolower(plf->lfFaceName[i]);
417 if (!font[i] || font[i] == ' ') break;
419 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
420 return checksum;
423 static UINT16 __genericCheckSum( const void *ptr, int size )
425 unsigned int checksum = 0;
426 const char *p = (const char *)ptr;
427 while (size-- > 0)
428 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
430 return checksum & 0xffff;
433 /*************************************************************************
434 * LFD parse/compose routines
436 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
437 * make a copy first
439 * These functions also do TILDE to HYPHEN conversion
441 static BOOL LFD_Parse(LPSTR lpFont, LFD *lfd)
443 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
444 int i;
445 if (*lpch != HYPHEN)
447 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
448 return FALSE;
451 field_start = ++lpch;
452 for( i = 0; i < LFD_FIELDS; )
454 if (*lpch == HYPHEN)
456 *lpch = '\0';
457 lfd_fld[i] = field_start;
458 i++;
459 field_start = ++lpch;
461 else if (!*lpch)
463 lfd_fld[i] = field_start;
464 i++;
465 break;
467 else if (*lpch == TILDE)
469 *lpch = HYPHEN;
470 ++lpch;
472 else
473 ++lpch;
475 /* Fill in the empty fields with NULLS */
476 for (; i< LFD_FIELDS; i++)
477 lfd_fld[i] = NULL;
478 if (*lpch)
479 WARN("Extra ignored in font '%s'\n", lpFont);
481 lfd->foundry = lfd_fld[0];
482 lfd->family = lfd_fld[1];
483 lfd->weight = lfd_fld[2];
484 lfd->slant = lfd_fld[3];
485 lfd->set_width = lfd_fld[4];
486 lfd->add_style = lfd_fld[5];
487 lfd->pixel_size = lfd_fld[6];
488 lfd->point_size = lfd_fld[7];
489 lfd->resolution_x = lfd_fld[8];
490 lfd->resolution_y = lfd_fld[9];
491 lfd->spacing = lfd_fld[10];
492 lfd->average_width = lfd_fld[11];
493 lfd->charset_registry = lfd_fld[12];
494 lfd->charset_encoding = lfd_fld[13];
495 return TRUE;
499 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
501 const char* lfd_fld[LFD_FIELDS];
502 int i;
504 if (!buf_size)
505 return; /* Don't be silly */
507 lfd_fld[0] = lfd->foundry;
508 lfd_fld[1] = lfd->family;
509 lfd_fld[2] = lfd->weight ;
510 lfd_fld[3] = lfd->slant ;
511 lfd_fld[4] = lfd->set_width ;
512 lfd_fld[5] = lfd->add_style;
513 lfd_fld[6] = lfd->pixel_size;
514 lfd_fld[7] = lfd->point_size;
515 lfd_fld[8] = lfd->resolution_x;
516 lfd_fld[9] = lfd->resolution_y ;
517 lfd_fld[10] = lfd->spacing ;
518 lfd_fld[11] = lfd->average_width ;
519 lfd_fld[12] = lfd->charset_registry ;
520 lfd_fld[13] = lfd->charset_encoding ;
522 buf_size--; /* Room for the terminator */
524 for (i = 0; i < LFD_FIELDS; i++)
526 const char* sp = lfd_fld[i];
527 if (!sp || !buf_size)
528 break;
530 *dp++ = HYPHEN;
531 buf_size--;
532 while (buf_size > 0 && *sp)
534 *dp = (*sp == HYPHEN) ? TILDE : *sp;
535 buf_size--;
536 dp++; sp++;
539 *dp = '\0';
543 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
545 int j = strlen(lpStr);
546 if( j == 1 && *lpStr == '0')
547 fi->fi_flags |= FI_POLYWEIGHT;
548 else if( j == 4 )
550 if( !strcasecmp( "bold", lpStr) )
551 fi->df.dfWeight = FW_BOLD;
552 else if( !strcasecmp( "demi", lpStr) )
554 fi->fi_flags |= FI_FW_DEMI;
555 fi->df.dfWeight = FW_DEMIBOLD;
557 else if( !strcasecmp( "book", lpStr) )
559 fi->fi_flags |= FI_FW_BOOK;
560 fi->df.dfWeight = FW_REGULAR;
563 else if( j == 5 )
565 if( !strcasecmp( "light", lpStr) )
566 fi->df.dfWeight = FW_LIGHT;
567 else if( !strcasecmp( "black", lpStr) )
568 fi->df.dfWeight = FW_BLACK;
570 else if( j == 6 && !strcasecmp( "medium", lpStr) )
571 fi->df.dfWeight = FW_REGULAR;
572 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
573 fi->df.dfWeight = FW_DEMIBOLD;
574 else
575 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
576 * from the weight property */
579 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
581 int l = strlen(lpStr);
582 if( l == 1 )
584 switch( tolower( *lpStr ) )
586 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
587 default:
588 case 'r': fi->df.dfItalic = 0;
589 break;
590 case 'o':
591 fi->fi_flags |= FI_OBLIQUE;
592 case 'i': fi->df.dfItalic = 1;
593 break;
595 return FALSE;
597 return TRUE;
600 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
602 int j = strlen(lpstr);
603 if( j > 3 ) /* find out is there "sans" or "script" */
605 j = 0;
607 if( strstr(lpstr, "sans") )
609 fi->df.dfPitchAndFamily |= FF_SWISS;
610 j = 1;
612 if( strstr(lpstr, "script") )
614 fi->df.dfPitchAndFamily |= FF_SCRIPT;
615 j = 1;
617 if( !j && dec_style_check )
618 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
622 /*************************************************************************
623 * LFD_InitFontInfo
625 * INIT ONLY
627 * Fill in some fields in the fontInfo struct.
629 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
631 int i, j, dec_style_check, scalability;
632 fontEncodingTemplate* boba;
633 const char* ridiculous = "font '%s' has ridiculous %s\n";
634 const char* lpstr;
636 if (!lfd->charset_registry)
638 WARN("font '%s' does not have enough fields\n", fullname);
639 return FALSE;
642 memset(fi, 0, sizeof(fontInfo) );
644 /* weight name - */
645 LFD_GetWeight( fi, lfd->weight);
647 /* slant - */
648 dec_style_check = LFD_GetSlant( fi, lfd->slant);
650 /* width name - */
651 lpstr = lfd->set_width;
652 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
653 dec_style_check = TRUE;
654 else
655 fi->fi_flags |= FI_NORMAL;
657 /* style - */
658 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
660 /* pixel & decipoint height, and res_x & y */
662 scalability = 0;
664 j = strlen(lfd->pixel_size);
665 if( j == 0 || j > 3 )
667 WARN(ridiculous, fullname, "pixel_size");
668 return FALSE;
670 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
671 scalability++;
673 j = strlen(lfd->point_size);
674 if( j == 0 || j > 3 )
676 WARN(ridiculous, fullname, "point_size");
677 return FALSE;
679 if( !(atoi(lfd->point_size)) )
680 scalability++;
682 j = strlen(lfd->resolution_x);
683 if( j == 0 || j > 3 )
685 WARN(ridiculous, fullname, "resolution_x");
686 return FALSE;
688 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
689 scalability++;
691 j = strlen(lfd->resolution_y);
692 if( j == 0 || j > 3 )
694 WARN(ridiculous, fullname, "resolution_y");
695 return FALSE;
697 if( !(atoi(lfd->resolution_y)) )
698 scalability++;
700 /* Check scalability */
701 switch (scalability)
703 case 0: /* Bitmap */
704 break;
705 case 4: /* Scalable */
706 fi->fi_flags |= FI_SCALABLE;
707 break;
708 case 2:
709 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
710 TRACE("Skipping scalable bitmap '%s'\n", fullname);
711 return FALSE;
712 default:
713 WARN("Font '%s' has weird scalability\n", fullname);
714 return FALSE;
717 /* spacing - */
718 lpstr = lfd->spacing;
719 switch( *lpstr )
721 case '\0':
722 WARN("font '%s' has no spacing\n", fullname);
723 return FALSE;
725 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
726 break;
727 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
728 fi->fi_flags |= FI_FIXEDEX;
729 /* fall through */
730 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
731 break;
732 default:
733 /* Of course this line does nothing */
734 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
737 /* average width - */
738 lpstr = lfd->average_width;
739 j = strlen(lpstr);
740 if( j == 0 || j > 3 )
742 WARN(ridiculous, fullname, "average_width");
743 return FALSE;
746 if( !(atoi(lpstr)) && !scalability )
748 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
749 return FALSE;
752 /* charset registry, charset encoding - */
753 lpstr = lfd->charset_registry;
754 if( strstr(lpstr, "ksc") ||
755 strstr(lpstr, "gb2312") ||
756 strstr(lpstr, "big5") )
758 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
761 fi->df.dfCharSet = ANSI_CHARSET;
763 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
765 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
767 if (lfd->charset_encoding)
769 for( j = 0; boba->sufch[j].psuffix; j++ )
771 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
773 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
774 fi->internal_charset = boba->sufch[j].charset;
775 fi->codepage = boba->sufch[j].codepage;
776 fi->cptable = boba->sufch[j].cptable;
777 goto done;
781 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
782 fi->internal_charset = boba->sufch[j].charset;
783 fi->codepage = boba->sufch[j].codepage;
784 fi->cptable = boba->sufch[j].cptable;
785 if (boba->prefix)
787 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
788 fullname, lfd->charset_encoding, boba->prefix);
789 j = 254;
791 else
793 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
794 fullname, lfd->charset_registry, lfd->charset_encoding);
795 j = 255;
798 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
799 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
800 goto done;
802 else if (boba->prefix)
804 WARN("font '%s' has known registry '%s' and no character encoding\n",
805 fullname, lpstr);
806 for( j = 0; boba->sufch[j].psuffix; j++ )
808 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
809 fi->internal_charset = boba->sufch[j].charset;
810 fi->codepage = boba->sufch[j].codepage;
811 fi->cptable = boba->sufch[j].cptable;
812 j = 255;
813 goto done;
817 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
818 return FALSE;
820 done:
821 /* i - index into fETTable
822 * j - index into suffix array for fETTable[i]
823 * except:
824 * 254 - found encoding prefix, unknown suffix
825 * 255 - no encoding match at all.
827 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
829 return TRUE;
833 /*************************************************************************
834 * LFD_AngleMatrix
836 * make a matrix suitable for LFD based on theta radians
838 static void LFD_AngleMatrix( char* buffer, int h, double theta)
840 double matrix[4];
841 matrix[0] = h*cos(theta);
842 matrix[1] = h*sin(theta);
843 matrix[2] = -h*sin(theta);
844 matrix[3] = h*cos(theta);
845 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
848 /*************************************************************************
849 * LFD_ComposeLFD
851 * Note: uRelax is a treatment not a cure. Font mapping algorithm
852 * should be bulletproof enough to allow us to avoid hacks like
853 * this despite LFD being so braindead.
855 static BOOL LFD_ComposeLFD( const fontObject* fo,
856 INT height, LPSTR lpLFD, UINT uRelax )
858 int i, h;
859 const char *any = "*";
860 char h_string[64], resx_string[64], resy_string[64];
861 LFD aLFD;
863 /* Get the worst case over with first */
865 /* RealizeFont() will call us repeatedly with increasing uRelax
866 * until XLoadFont() succeeds.
867 * to avoid an infinite loop; these will always match
869 if (uRelax >= 5)
871 if (uRelax == 5)
872 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
873 else
874 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
875 return TRUE;
878 /* read foundry + family from fo */
879 aLFD.foundry = fo->fr->resource->foundry;
880 aLFD.family = fo->fr->resource->family;
882 /* add weight */
883 switch( fo->fi->df.dfWeight )
885 case FW_BOLD:
886 aLFD.weight = "bold"; break;
887 case FW_REGULAR:
888 if( fo->fi->fi_flags & FI_FW_BOOK )
889 aLFD.weight = "book";
890 else
891 aLFD.weight = "medium";
892 break;
893 case FW_DEMIBOLD:
894 aLFD.weight = "demi";
895 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
896 aLFD.weight = "bold";
897 break;
898 case FW_BLACK:
899 aLFD.weight = "black"; break;
900 case FW_LIGHT:
901 aLFD.weight = "light"; break;
902 default:
903 aLFD.weight = any;
906 /* add slant */
907 if( fo->fi->df.dfItalic )
908 if( fo->fi->fi_flags & FI_OBLIQUE )
909 aLFD.slant = "o";
910 else
911 aLFD.slant = "i";
912 else
913 aLFD.slant = (uRelax < 1) ? "r" : any;
915 /* add width */
916 if( fo->fi->fi_flags & FI_NORMAL )
917 aLFD.set_width = "normal";
918 else
919 aLFD.set_width = any;
921 /* ignore style */
922 aLFD.add_style = any;
924 /* add pixelheight
926 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
928 if( fo->fo_flags & FO_SYNTH_HEIGHT )
929 h = fo->fi->lfd_height;
930 else
932 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
933 h /= fo->fi->df.dfPixHeight;
935 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
936 h = MIN_FONT_SIZE;
937 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
939 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
940 h = MAX_FONT_SIZE;
943 if (uRelax <= 2)
944 /* handle rotated fonts */
945 if (fo->lf.lfEscapement) {
946 /* escapement is in tenths of degrees, theta is in radians */
947 double theta = M_PI*fo->lf.lfEscapement/1800.;
948 LFD_AngleMatrix(h_string, h, theta);
950 else
952 sprintf(h_string, "%d", h);
954 else
955 sprintf(h_string, "%d", fo->fi->lfd_height);
957 aLFD.pixel_size = h_string;
958 aLFD.point_size = any;
960 /* resolution_x,y, average width */
961 /* FIXME - Why do some font servers ignore average width ?
962 * so that you have to mess around with res_y
964 aLFD.average_width = any;
965 if (uRelax <= 3)
967 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
968 aLFD.resolution_x = resx_string;
970 strcpy(resy_string, resx_string);
971 if( uRelax == 0 && text_caps & TC_SF_X_YINDEP )
973 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
975 int resy = 0.5 + fo->fi->lfd_resolution *
976 (fo->fi->df.dfAvgWidth * height) /
977 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
978 sprintf(resy_string, "%d", resy);
980 else
982 /* FIXME - synth width */
985 aLFD.resolution_y = resy_string;
987 else
989 aLFD.resolution_x = aLFD.resolution_y = any;
992 /* spacing */
994 const char* w;
996 if( fo->fi->fi_flags & FI_FIXEDPITCH )
997 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
998 else
999 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
1001 aLFD.spacing = (uRelax <= 1) ? w : any;
1004 /* encoding */
1006 if (uRelax <= 4)
1008 fontEncodingTemplate* boba = fETTable;
1010 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
1011 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
1013 i = fo->fi->fi_encoding & 255;
1014 switch( i )
1016 default:
1017 aLFD.charset_encoding = boba->sufch[i].psuffix;
1018 break;
1020 case 254:
1021 aLFD.charset_encoding = any;
1022 break;
1024 case 255: /* no suffix - it ends eg "-ascii" */
1025 aLFD.charset_encoding = NULL;
1026 break;
1029 else
1031 aLFD.charset_registry = any;
1032 aLFD.charset_encoding = any;
1035 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1037 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1038 return TRUE;
1042 /***********************************************************************
1043 * X Font Resources
1045 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1046 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1048 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1049 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1051 unsigned long height;
1052 unsigned min = (unsigned char)pFI->dfFirstChar;
1053 unsigned max = (unsigned char)pFI->dfLastChar;
1054 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1056 if( pEL ) *pEL = 0;
1058 if(XFT) {
1059 wine_tsx11_lock();
1060 if(XGetFontProperty((XFontStruct*)x_fs, x11drv_atom(RAW_CAP_HEIGHT), &height))
1061 *pIL = XFT->ascent -
1062 (INT)(XFT->pixelsize / 1000.0 * height);
1063 else
1064 *pIL = 0;
1065 wine_tsx11_unlock();
1066 return;
1069 wine_tsx11_lock();
1070 if( XGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1072 if( x_fs->per_char )
1073 if( bIsLatin && ((unsigned char)'X' <= (max - min)) )
1074 height = x_fs->per_char['X' - min].ascent;
1075 else
1076 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1077 height = x_fs->max_bounds.ascent;
1078 else
1080 height = x_fs->ascent;
1081 if( pEL )
1082 *pEL = x_fs->max_bounds.ascent - height;
1084 else
1085 height = x_fs->min_bounds.ascent;
1087 wine_tsx11_unlock();
1089 *pIL = x_fs->ascent - height;
1092 /***********************************************************************
1093 * XFONT_CharWidth
1095 static int XFONT_CharWidth(const XFontStruct* x_fs,
1096 const XFONTTRANS *XFT, int ch)
1098 if(!XFT)
1099 return x_fs->per_char[ch].width;
1100 else
1101 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1104 /***********************************************************************
1105 * XFONT_GetAvgCharWidth
1107 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1108 const XFONTTRANS *XFT)
1110 unsigned min = (unsigned char)pFI->dfFirstChar;
1111 unsigned max = (unsigned char)pFI->dfLastChar;
1113 INT avg;
1115 if( x_fs->per_char )
1117 int width = 0, chars = 0, j;
1118 if( (IS_LATIN_CHARSET(pFI->dfCharSet) ||
1119 pFI->dfCharSet == DEFAULT_CHARSET) &&
1120 (max - min) >= (unsigned char)'z' )
1122 /* FIXME - should use a weighted average */
1123 for( j = 0; j < 26; j++ )
1124 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1125 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1126 chars = 52;
1128 else /* unweighted average of everything */
1130 for( j = 0, max -= min; j <= max; j++ )
1131 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1133 width += XFONT_CharWidth(x_fs, XFT, j);
1134 chars++;
1137 if (chars) avg = (width + (chars-1))/ chars; /* always round up*/
1138 else avg = 0; /* No characters exist at all */
1140 else /* uniform width */
1141 avg = x_fs->min_bounds.width;
1143 TRACE(" retuning %d\n",avg);
1144 return avg;
1147 /***********************************************************************
1148 * XFONT_GetMaxCharWidth
1150 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1152 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1153 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1154 int maxwidth, j;
1156 if(!XFT || !xfs->per_char)
1157 return abs(xfs->max_bounds.width);
1159 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1160 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1161 if(maxwidth < xfs->per_char[j].attributes)
1162 maxwidth = xfs->per_char[j].attributes;
1164 maxwidth *= XFT->pixelsize / 1000.0;
1165 return maxwidth;
1168 /***********************************************************************
1169 * XFONT_SetFontMetric
1171 * INIT ONLY
1173 * Initializes IFONTINFO16.
1175 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1177 unsigned min, max;
1178 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1179 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1181 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1182 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1184 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1185 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1187 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1188 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1189 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1191 if( xfs->min_bounds.width != xfs->max_bounds.width )
1192 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1193 if( fi->fi_flags & FI_SCALABLE )
1195 fi->df.dfType = DEVICE_FONTTYPE;
1196 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1198 else if( fi->fi_flags & FI_TRUETYPE )
1199 fi->df.dfType = TRUETYPE_FONTTYPE;
1200 else
1201 fi->df.dfType = RASTER_FONTTYPE;
1203 fi->df.dfFace = fr->lfFaceName;
1206 /***********************************************************************
1207 * XFONT_GetFontMetric
1209 * Retrieve font metric info (enumeration).
1211 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1212 LPENUMLOGFONTEXW pLF,
1213 NEWTEXTMETRICEXW *pTM )
1215 memset( pLF, 0, sizeof(*pLF) );
1216 memset( pTM, 0, sizeof(*pTM) );
1218 #define plf ((LPLOGFONTW)pLF)
1219 #define ptm ((LPNEWTEXTMETRICW)pTM)
1220 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1221 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1222 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1223 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1224 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1225 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1226 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1228 /* convert pitch values */
1230 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1231 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1233 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1234 plf->lfFaceName, LF_FACESIZE);
1236 /* FIXME: fill in rest of plF values */
1237 strcpyW(pLF->elfFullName, plf->lfFaceName);
1238 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1239 pLF->elfStyle, LF_FACESIZE);
1240 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1241 "Symbol" : "Roman", -1,
1242 pLF->elfScript, LF_FACESIZE);
1244 #undef plf
1246 ptm->tmAscent = pfi->df.dfAscent;
1247 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1248 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1249 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1250 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1251 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1253 ptm->tmFirstChar = pfi->df.dfFirstChar;
1254 ptm->tmLastChar = pfi->df.dfLastChar;
1255 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1256 ptm->tmBreakChar = pfi->df.dfBreakChar;
1258 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1259 debugstr_w(pLF->elfLogFont.lfFaceName),
1260 debugstr_w(pLF->elfFullName));
1262 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1263 /* return font type */
1264 return pfi->df.dfType;
1265 #undef ptm
1269 /***********************************************************************
1270 * XFONT_FixupFlags
1272 * INIT ONLY
1274 * dfPitchAndFamily flags for some common typefaces.
1276 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1278 switch( lfFaceName[0] )
1280 case 'a':
1281 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1282 return FF_SWISS;
1283 break;
1284 case 'h':
1285 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1286 return FF_SWISS;
1287 break;
1288 case 'c':
1289 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1290 return FF_MODERN;
1292 if (!strcasecmp(lfFaceName, "Charter") )
1293 return FF_ROMAN;
1294 break;
1295 case 'p':
1296 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1297 return FF_ROMAN;
1298 break;
1299 case 't':
1300 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1301 return FF_ROMAN;
1302 break;
1303 case 'u':
1304 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1305 return FF_ROMAN;
1306 break;
1307 case 'z':
1308 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1309 return FF_DECORATIVE;
1311 return 0;
1314 /***********************************************************************
1315 * XFONT_SameFoundryAndFamily
1317 * INIT ONLY
1319 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1321 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1322 !strcasecmp( lfd1->family, lfd2->family ) );
1325 /***********************************************************************
1326 * XFONT_InitialCapitals
1328 * INIT ONLY
1330 * Upper case first letters of words & remove multiple spaces
1332 static void XFONT_InitialCapitals(LPSTR lpch)
1334 int i;
1335 BOOL up;
1336 char* lpstr = lpch;
1338 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1340 if( isspace(*lpch) )
1342 if (!up) /* Not already got one */
1344 *lpstr++ = ' ';
1345 up = TRUE;
1348 else if( isalpha(*lpch) && up )
1350 *lpstr++ = toupper(*lpch);
1351 up = FALSE;
1353 else
1355 *lpstr++ = *lpch;
1356 up = FALSE;
1360 /* Remove possible trailing space */
1361 if (up && i > 0)
1362 --lpstr;
1363 *lpstr = '\0';
1367 /***********************************************************************
1368 * XFONT_WindowsNames
1370 * INIT ONLY
1372 * Build generic Windows aliases for X font names.
1374 * -misc-fixed- -> "Fixed"
1375 * -sony-fixed- -> "Sony Fixed", etc...
1377 static void XFONT_WindowsNames(void)
1379 fontResource* fr;
1381 for( fr = fontList; fr ; fr = fr->next )
1383 fontResource* pfr;
1384 char* lpch;
1386 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1388 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1389 if( pfr->fr_flags & FR_NAMESET )
1391 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1392 break;
1395 lpch = fr->lfFaceName;
1396 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1397 /* prepend vendor name */
1398 (pfr==fr) ? "" : fr->resource->foundry,
1399 fr->resource->family);
1400 XFONT_InitialCapitals(fr->lfFaceName);
1402 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1403 if( bFamilyStyle)
1405 fontInfo* fi;
1406 for( fi = fr->fi ; fi ; fi = fi->next )
1407 fi->df.dfPitchAndFamily |= bFamilyStyle;
1411 TRACE("typeface '%s'\n", fr->lfFaceName);
1413 fr->fr_flags |= FR_NAMESET;
1417 /***********************************************************************
1418 * XFONT_LoadDefaultLFD
1420 * Move lfd to the head of fontList to make it more likely to be matched
1422 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1425 fontResource *fr, *pfr;
1426 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1428 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1430 if( fr )
1432 fr->next = pfr->next;
1433 pfr->next = fontList;
1434 fontList = pfr;
1436 break;
1438 fr = pfr;
1440 if (!pfr)
1441 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1442 lfd->foundry, lfd->family);
1446 /***********************************************************************
1447 * XFONT_LoadDefault
1449 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1451 char buffer[MAX_LFD_LENGTH];
1452 HKEY hkey;
1454 buffer[0] = 0;
1455 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1457 DWORD type, count = sizeof(buffer);
1458 RegQueryValueExA(hkey, ini, 0, &type, buffer, &count);
1459 RegCloseKey(hkey);
1461 if (*buffer)
1463 LFD lfd;
1464 char* pch = buffer;
1465 while( *pch && isspace(*pch) ) pch++;
1467 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1468 if (LFD_Parse(pch, &lfd) && lfd.foundry && lfd.family)
1469 XFONT_LoadDefaultLFD(&lfd, fonttype);
1470 else
1471 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1476 /***********************************************************************
1477 * XFONT_LoadDefaults
1479 static void XFONT_LoadDefaults(void)
1481 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1482 XFONT_LoadDefault(INIDefault, "");
1485 /***********************************************************************
1486 * XFONT_CreateAlias
1488 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1490 int j;
1491 fontAlias *pfa, *prev = NULL;
1493 for(pfa = aliasTable; pfa; pfa = pfa->next)
1495 /* check if we already got one */
1496 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1498 TRACE("redundant alias '%s' -> '%s'\n",
1499 lpAlias, lpTypeFace );
1500 return NULL;
1502 prev = pfa;
1505 j = strlen(lpTypeFace) + 1;
1506 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1507 j + strlen(lpAlias) + 1 );
1508 if (pfa)
1510 if (!prev)
1511 aliasTable = pfa;
1512 else
1513 prev->next = pfa;
1515 pfa->next = NULL;
1516 pfa->faTypeFace = (LPSTR)(pfa + 1);
1517 strcpy( pfa->faTypeFace, lpTypeFace );
1518 pfa->faAlias = pfa->faTypeFace + j;
1519 strcpy( pfa->faAlias, lpAlias );
1521 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1523 return pfa;
1525 return NULL;
1529 /***********************************************************************
1530 * XFONT_LoadAlias
1532 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1534 fontResource *fr, *frMatch = NULL;
1535 if (!lfd->foundry || ! lfd->family)
1537 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1538 return;
1540 for (fr = fontList; fr ; fr = fr->next)
1542 if(!strcasecmp(fr->resource->family, lpAlias))
1544 /* alias is not needed since the real font is present */
1545 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1546 return;
1548 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1550 frMatch = fr;
1551 break;
1555 if( frMatch )
1557 if( bSubst )
1559 fontAlias *pfa, *prev = NULL;
1561 for(pfa = aliasTable; pfa; pfa = pfa->next)
1563 /* Remove lpAlias from aliasTable - we should free the old entry */
1564 if(!strcmp(lpAlias, pfa->faAlias))
1566 if(prev)
1567 prev->next = pfa->next;
1568 else
1569 aliasTable = pfa->next;
1572 /* Update any references to the substituted font in aliasTable */
1573 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1575 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1576 strcpy( pfa->faTypeFace, lpAlias );
1578 prev = pfa;
1581 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1583 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1584 frMatch->fr_flags |= FR_NAMESET;
1586 else
1588 /* create new entry in the alias table */
1589 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1592 else
1594 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1598 /***********************************************************************
1599 * Just a copy of PROFILE_GetStringItem
1601 * Convenience function that turns a string 'xxx, yyy, zzz' into
1602 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1604 static char *XFONT_GetStringItem( char *start )
1606 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1607 char *lpchX, *lpch;
1609 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1611 if( *lpchX == ',' )
1613 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1614 while( *(++lpchX) )
1615 if( !XFONT_isspace(*lpchX) ) return lpchX;
1617 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1618 else lpch = NULL;
1620 if( lpch ) *lpch = '\0';
1621 return NULL;
1622 #undef XFONT_isspace
1625 /***********************************************************************
1626 * XFONT_LoadAliases
1628 * INIT ONLY
1630 * Create font aliases for some standard windows fonts using user's
1631 * default choice of (sans-)serif fonts
1633 * Read user-defined aliases from config file. Format is as follows
1635 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1637 * Example:
1638 * Alias0 = Arial, -adobe-helvetica-
1639 * Alias1 = Times New Roman, -bitstream-courier-, 1
1640 * ...
1642 * Note that from 970817 and on we have built-in alias templates that take
1643 * care of the necessary Windows typefaces.
1645 typedef struct
1647 LPSTR fatResource;
1648 LPSTR fatAlias;
1649 } aliasTemplate;
1651 static void XFONT_LoadAliases(void)
1653 char *lpResource;
1654 char buffer[MAX_LFD_LENGTH];
1655 int i = 0;
1656 LFD lfd;
1657 HKEY hkey;
1659 /* built-ins first */
1660 strcpy(buffer, "-bitstream-charter-");
1661 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1663 DWORD type, count = sizeof(buffer);
1664 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, buffer, &count);
1665 RegCloseKey(hkey);
1667 TRACE("Using '%s' as default serif font\n", buffer);
1668 if (LFD_Parse(buffer, &lfd))
1670 /* NB XFONT_InitialCapitals should not change these standard aliases */
1671 XFONT_LoadAlias( &lfd, "Tms Roman", FALSE);
1672 XFONT_LoadAlias( &lfd, "MS Serif", FALSE);
1673 XFONT_LoadAlias( &lfd, "Times New Roman", FALSE);
1675 XFONT_LoadDefaultLFD( &lfd, "serif ");
1678 strcpy(buffer, "-adobe-helvetica-");
1679 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1681 DWORD type, count = sizeof(buffer);
1682 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, buffer, &count);
1683 RegCloseKey(hkey);
1685 TRACE("Using '%s' as default sans serif font\n", buffer);
1686 if (LFD_Parse(buffer, &lfd))
1688 XFONT_LoadAlias( &lfd, "Helv", FALSE);
1689 XFONT_LoadAlias( &lfd, "MS Sans Serif", FALSE);
1690 XFONT_LoadAlias( &lfd, "MS Shell Dlg", FALSE);
1691 XFONT_LoadAlias( &lfd, "System", FALSE);
1692 XFONT_LoadAlias( &lfd, "Arial", FALSE);
1694 XFONT_LoadDefaultLFD( &lfd, "sans serif ");
1697 /* then user specified aliases */
1700 BOOL bSubst;
1701 char subsection[32];
1702 snprintf( subsection, sizeof(subsection), "%s%i", INIAliasSection, i++ );
1704 buffer[0] = 0;
1705 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1707 DWORD type, count = sizeof(buffer);
1708 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1709 RegCloseKey(hkey);
1712 if (!buffer[0])
1713 break;
1715 XFONT_InitialCapitals(buffer);
1716 lpResource = XFONT_GetStringItem( buffer );
1717 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1718 if( lpResource && *lpResource )
1720 if (LFD_Parse(lpResource, &lfd)) XFONT_LoadAlias(&lfd, buffer, bSubst);
1722 else
1723 WARN("malformed font alias '%s'\n", buffer );
1725 while(TRUE);
1728 /***********************************************************************
1729 * XFONT_UnAlias
1731 * Convert an (potential) alias into a real windows name
1734 static LPCSTR XFONT_UnAlias(char* font)
1736 if (font[0])
1738 fontAlias* fa;
1739 XFONT_InitialCapitals(font); /* to remove extra white space */
1741 for( fa = aliasTable; fa; fa = fa->next )
1742 /* use case insensitive matching to handle eg "MS Sans Serif" */
1743 if( !strcasecmp( fa->faAlias, font ) )
1745 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1746 strcpy(font, fa->faTypeFace);
1747 return fa->faAlias;
1748 break;
1751 return NULL;
1754 /***********************************************************************
1755 * XFONT_RemoveFontResource
1757 * Caller should check if the font resource is in use. If it is it should
1758 * set FR_REMOVED flag to delay removal until the resource is not in use
1759 * any more.
1761 void XFONT_RemoveFontResource( fontResource** ppfr )
1763 fontResource* pfr = *ppfr;
1764 #if 0
1765 fontInfo* pfi;
1767 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1768 while( pfr->fi )
1770 pfi = pfr->fi->next;
1771 HeapFree( GetProcessHeap(), 0, pfr->fi );
1772 pfr->fi = pfi;
1774 HeapFree( GetProcessHeap(), 0, pfr );
1775 #endif
1776 *ppfr = pfr->next;
1779 /***********************************************************************
1780 * XFONT_LoadIgnores
1782 * INIT ONLY
1784 * Removes specified fonts from the font table to prevent Wine from
1785 * using it.
1787 * Ignore# = [LFD font name]
1789 * Example:
1790 * Ignore0 = -misc-nil-
1791 * Ignore1 = -sun-open look glyph-
1792 * ...
1795 static void XFONT_LoadIgnore(char* lfdname)
1797 fontResource** ppfr;
1798 LFD lfd;
1800 if (LFD_Parse(lfdname, &lfd) && lfd.foundry && lfd.family)
1802 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1804 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, &lfd) )
1806 TRACE("Ignoring '-%s-%s-'\n",
1807 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1809 XFONT_RemoveFontResource( ppfr );
1810 break;
1814 else
1815 WARN("Malformed font resource\n");
1818 static void XFONT_LoadIgnores(void)
1820 int i = 0;
1821 char subsection[32];
1822 char buffer[MAX_LFD_LENGTH];
1824 /* Standard one that noone wants */
1825 strcpy(buffer, "-misc-nil-");
1826 XFONT_LoadIgnore(buffer);
1828 /* Others from INI file */
1831 HKEY hkey;
1832 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1834 buffer[0] = 0;
1835 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1837 DWORD type, count = sizeof(buffer);
1838 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1839 RegCloseKey(hkey);
1842 if( buffer[0] )
1844 char* pch = buffer;
1845 while( *pch && isspace(*pch) ) pch++;
1846 XFONT_LoadIgnore(pch);
1848 else
1849 break;
1850 } while(TRUE);
1854 /***********************************************************************
1855 * XFONT_UserMetricsCache
1857 * Returns expanded name for the cachedmetrics file.
1858 * Now it also appends the current value of the $DISPLAY variable.
1860 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1862 const char *confdir = wine_get_config_dir();
1863 const char *display_name = XDisplayName(NULL);
1864 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1865 int display = 0;
1866 int screen = 0;
1867 char *p, *ext;
1870 ** Normalize the display name, since on Red Hat systems, DISPLAY
1871 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1872 ** after this code, all of the above will resolve to ':0.0'.
1874 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1875 p = strchr(display_name, ':');
1876 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1878 if ((len > *buf_size) &&
1879 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1881 ERR("out of memory\n");
1882 ExitProcess(1);
1884 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1886 ext = buffer + strlen(buffer);
1887 strcpy( ext, display_name );
1889 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1890 sprintf( p, ":%d.%d", display, screen );
1891 return buffer;
1895 /***********************************************************************
1896 * X Font Matching
1898 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1900 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1902 INT m;
1904 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1906 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1907 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1908 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1910 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1911 && fi->lfd_height != match->lfd_height) ||
1912 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1913 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1915 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1916 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1918 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1919 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1920 else if( m >= 0 ) return 1; /* 'match' is better */
1922 return -1; /* 'fi' is better */
1925 /***********************************************************************
1926 * XFONT_CheckFIList
1928 * REMOVE_SUBSETS - attach new fi and purge subsets
1929 * UNMARK_SUBSETS - remove subset flags from all fi entries
1931 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1933 int i = 0;
1934 fontInfo* pfi, *prev;
1936 for( prev = NULL, pfi = fr->fi; pfi; )
1938 if( action == REMOVE_SUBSETS )
1940 if( pfi->fi_flags & FI_SUBSET )
1942 fontInfo* subset = pfi;
1944 i++;
1945 fr->fi_count--;
1946 if( prev ) prev->next = pfi = pfi->next;
1947 else fr->fi = pfi = pfi->next;
1948 HeapFree( GetProcessHeap(), 0, subset );
1949 continue;
1952 else pfi->fi_flags &= ~FI_SUBSET;
1954 prev = pfi;
1955 pfi = pfi->next;
1958 if( action == REMOVE_SUBSETS ) /* also add the superset */
1960 if( fi->fi_flags & FI_SCALABLE )
1962 fi->next = fr->fi;
1963 fr->fi = fi;
1965 else if( prev ) prev->next = fi; else fr->fi = fi;
1966 fr->fi_count++;
1969 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1972 /***********************************************************************
1973 * XFONT_FindFIList
1975 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1977 while( pfr )
1979 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1980 pfr = pfr->next;
1982 /* Give the app back the font name it asked for. Encarta checks this. */
1983 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1984 return pfr;
1987 /***********************************************************************
1988 * XFONT_FixupPointSize
1990 static void XFONT_FixupPointSize(fontInfo* fi)
1992 #define df (fi->df)
1993 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1994 df.dfPoints = (INT16)
1995 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1996 df.dfVertRes );
1997 #undef df
2000 /***********************************************************************
2001 * XFONT_BuildMetrics
2003 * Build font metrics from X font
2005 static int XLoadQueryFont_ErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
2007 return 1;
2010 /* XLoadQueryFont has the bad habit of crashing when loading a bad font... */
2011 static XFontStruct *safe_XLoadQueryFont(Display *display, char *name)
2013 XFontStruct *ret;
2015 X11DRV_expect_error(display, XLoadQueryFont_ErrorHandler, NULL);
2016 ret = XLoadQueryFont(display, name);
2017 if (X11DRV_check_error()) ret = NULL;
2018 return ret;
2022 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
2024 int i;
2025 fontInfo* fi = NULL;
2026 fontResource* fr, *pfr;
2027 int n_ff = 0;
2029 MESSAGE("Building font metrics. This may take some time...\n");
2030 for( i = 0; i < x_count; i++ )
2032 char* typeface;
2033 LFD lfd;
2034 int j;
2035 char buffer[MAX_LFD_LENGTH];
2036 char* lpstr;
2037 XFontStruct* x_fs;
2038 fontInfo* pfi;
2040 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
2041 strcpy( typeface, x_pattern[i] );
2042 if (i % 10 == 0) MESSAGE("Font metrics: %.1f%% done\n", 100.0 * i / x_count);
2044 if (!LFD_Parse(typeface, &lfd))
2046 HeapFree(GetProcessHeap(), 0, typeface);
2047 continue;
2050 /* find a family to insert into */
2052 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2054 if( XFONT_SameFoundryAndFamily(fr->resource, &lfd))
2055 break;
2056 pfr = fr;
2059 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2061 if( !LFD_InitFontInfo( fi, &lfd, x_pattern[i]) )
2062 goto nextfont;
2064 if( !fr ) /* add new family */
2066 n_ff++;
2067 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2068 if (fr)
2070 memset(fr, 0, sizeof(fontResource));
2072 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2073 memset(fr->resource, 0, sizeof(LFD));
2075 TRACE("family: -%s-%s-\n", lfd.foundry, lfd.family );
2076 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.foundry)+1);
2077 strcpy( (char *)fr->resource->foundry, lfd.foundry );
2078 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd.family)+1);
2079 strcpy( (char *)fr->resource->family, lfd.family );
2080 fr->resource->weight = "";
2082 if( pfr ) pfr->next = fr;
2083 else fontList = fr;
2085 else
2086 WARN("Not enough memory for a new font family\n");
2089 /* check if we already have something better than "fi" */
2091 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2092 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2093 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2094 if( j > 0 ) goto nextfont;
2096 /* add new font instance "fi" to the "fr" font resource */
2098 if( fi->fi_flags & FI_SCALABLE )
2100 LFD lfd1;
2101 char pxl_string[4], res_string[4];
2102 /* set scalable font height to get an basis for extrapolation */
2104 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2105 fi->lfd_resolution = res;
2107 sprintf(pxl_string, "%d", fi->lfd_height);
2108 sprintf(res_string, "%d", fi->lfd_resolution);
2110 lfd1 = lfd;
2111 lfd1.pixel_size = pxl_string;
2112 lfd1.point_size = "*";
2113 lfd1.resolution_x = res_string;
2114 lfd1.resolution_y = res_string;
2116 LFD_UnParse(buffer, sizeof(buffer), &lfd1);
2118 lpstr = buffer;
2120 else lpstr = x_pattern[i];
2122 /* X11 may return an error on some bad fonts... So be prepared to handle these. */
2123 if ((x_fs = safe_XLoadQueryFont(gdi_display, lpstr)) != 0)
2125 XFONT_SetFontMetric( fi, fr, x_fs );
2126 wine_tsx11_lock();
2127 XFreeFont( gdi_display, x_fs );
2128 wine_tsx11_unlock();
2130 XFONT_FixupPointSize(fi);
2132 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2134 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2135 fi = NULL; /* preventing reuse */
2137 else
2139 ERR("failed to load %s\n", lpstr );
2141 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2143 nextfont:
2144 HeapFree(GetProcessHeap(), 0, typeface);
2146 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
2148 /* Scan through the font list and remove FontResource(s) (fr)
2149 * that have no associated Fontinfo(s) (fi).
2150 * This code is necessary because XFONT_ReadCachedMetrics
2151 * assumes that there is at least one fi associated with a fr.
2152 * This assumption is invalid for TT font
2153 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2156 fr = fontList;
2158 while (!fr->fi_count)
2160 fontList = fr->next;
2162 HeapFree(GetProcessHeap(), 0, fr->resource);
2163 HeapFree(GetProcessHeap(), 0, fr);
2165 fr = fontList;
2166 n_ff--;
2169 fr = fontList;
2171 while (fr->next)
2173 if (!fr->next->fi_count)
2175 pfr = fr->next;
2176 fr->next = fr->next->next;
2178 HeapFree(GetProcessHeap(), 0, pfr->resource);
2179 HeapFree(GetProcessHeap(), 0, pfr);
2181 n_ff--;
2183 else
2184 fr = fr->next;
2187 MESSAGE("Font metrics: 100.0%% done\n");
2188 return n_ff;
2191 /***********************************************************************
2192 * XFONT_ReadCachedMetrics
2194 * INIT ONLY
2196 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2198 if( fd >= 0 )
2200 unsigned u;
2201 int i, j;
2203 /* read checksums */
2204 read( fd, &u, sizeof(unsigned) );
2205 read( fd, &i, sizeof(int) );
2207 if( u == x_checksum && i == x_count )
2209 off_t length, offset = 3 * sizeof(int);
2211 /* read total size */
2212 read( fd, &i, sizeof(int) );
2213 length = lseek( fd, 0, SEEK_END );
2215 if( length == (i + offset) )
2217 lseek( fd, offset, SEEK_SET );
2218 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2219 if( fontList )
2221 fontResource* pfr = fontList;
2222 fontInfo* pfi = NULL;
2224 TRACE("Reading cached font metrics:\n");
2226 read( fd, fontList, i); /* read all metrics at once */
2227 while( offset < length )
2229 offset += sizeof(fontResource) + sizeof(fontInfo);
2230 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2231 j = 1;
2232 while( TRUE )
2234 if( offset > length ||
2235 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2236 (int)(pfi->next) != j++ )
2238 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);
2239 goto fail;
2242 if( pfi->df.dfPixHeight == 0 )
2244 TRACE("error: dfPixHeight==0\n");
2245 goto fail;
2248 pfi->df.dfFace = pfr->lfFaceName;
2249 if( pfi->fi_flags & FI_SCALABLE )
2251 /* we can pretend we got this font for any resolution */
2252 pfi->lfd_resolution = res;
2253 XFONT_FixupPointSize(pfi);
2255 pfi->next = pfi + 1;
2257 if( j > pfr->fi_count ) break;
2259 pfi = pfi->next;
2260 offset += sizeof(fontInfo);
2262 pfi->next = NULL;
2263 if( pfr->next )
2265 pfr->next = (fontResource*)(pfi + 1);
2266 pfr = pfr->next;
2268 else break;
2270 if( pfr->next == NULL &&
2271 *(int*)(pfi + 1) == X_FMC_MAGIC )
2273 /* read LFD stubs */
2274 char* lpch = (char*)((int*)(pfi + 1) + 1);
2275 offset += sizeof(int);
2276 for( pfr = fontList; pfr; pfr = pfr->next )
2278 size_t len = strlen(lpch) + 1;
2279 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2280 pfr->resource = HeapAlloc(GetProcessHeap(),0,sizeof(LFD));
2281 if (!LFD_Parse(lpch, pfr->resource))
2283 HeapFree( GetProcessHeap(), 0, pfr->resource );
2284 pfr->resource = NULL;
2286 lpch += len;
2287 offset += len;
2288 if (offset > length)
2290 TRACE("error: offset=%ld length=%ld\n",(long)offset,(long)length);
2291 goto fail;
2294 close( fd );
2295 return TRUE;
2298 } else {
2299 TRACE("Wrong length: %ld!=%ld\n",(long)length,(long)(i+offset));
2301 } else {
2302 TRACE("Checksum (%x vs. %x) or count (%d vs. %d) mismatch\n",
2303 u,x_checksum,i,x_count);
2305 fail:
2306 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2307 fontList = NULL;
2308 close( fd );
2310 return FALSE;
2313 /***********************************************************************
2314 * XFONT_WriteCachedMetrics
2316 * INIT ONLY
2318 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2320 fontResource* pfr;
2321 fontInfo* pfi;
2323 if( fd >= 0 )
2325 int i, j, k;
2326 char buffer[MAX_LFD_LENGTH];
2328 /* font metrics file:
2330 * +0000 x_checksum
2331 * +0004 x_count
2332 * +0008 total size to load
2333 * +000C prepackaged font metrics
2334 * ...
2335 * +...x X_FMC_MAGIC
2336 * +...x + 4 LFD stubs
2339 write( fd, &x_checksum, sizeof(unsigned) );
2340 write( fd, &x_count, sizeof(int) );
2342 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2344 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2345 i += strlen( buffer) + 1;
2346 j += pfr->fi_count;
2348 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2349 write( fd, &i, sizeof(int) );
2351 TRACE("Writing font cache:\n");
2353 for( pfr = fontList; pfr; pfr = pfr->next )
2355 fontInfo fi;
2357 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2359 i = write( fd, pfr, sizeof(fontResource) );
2360 if( i == sizeof(fontResource) )
2362 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2364 fi = *pfi;
2366 fi.df.dfFace = NULL;
2367 fi.next = (fontInfo*)k; /* loader checks this */
2369 j = write( fd, &fi, sizeof(fi) );
2370 k++;
2372 if( j == sizeof(fontInfo) ) continue;
2374 break;
2376 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2378 i = j = X_FMC_MAGIC;
2379 write( fd, &i, sizeof(int) );
2380 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2382 LFD_UnParse(buffer, sizeof(buffer), pfr->resource);
2383 i = strlen( buffer ) + 1;
2384 j = write( fd, buffer, i );
2387 close( fd );
2388 return ( i == j );
2390 return FALSE;
2393 /***********************************************************************
2394 * XFONT_GetPointResolution()
2396 * INIT ONLY
2398 * Here we initialize DefResolution which is used in the
2399 * XFONT_Match() penalty function. We also load the point
2400 * resolution value (higher values result in larger fonts).
2402 static int XFONT_GetPointResolution( int *log_pixels_x, int *log_pixels_y )
2404 int i, j, point_resolution, num = 3;
2405 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2406 int best = 0, best_diff = 65536;
2407 HKEY hkey;
2409 point_resolution = 0;
2411 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2413 char buffer[20];
2414 DWORD type, count = sizeof(buffer);
2415 if(!RegQueryValueExA(hkey, INIResolution, 0, &type, buffer, &count))
2416 point_resolution = atoi(buffer);
2417 RegCloseKey(hkey);
2420 if( !point_resolution )
2421 point_resolution = *log_pixels_y;
2422 else
2423 *log_pixels_x = *log_pixels_y = point_resolution;
2426 /* FIXME We can only really guess at a best DefResolution
2427 * - this should be configurable
2429 for( i = best = 0; i < num; i++ )
2431 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2432 if( j < best_diff )
2434 best = i;
2435 best_diff = j;
2438 DefResolution = allowed_xfont_resolutions[best];
2439 return point_resolution;
2443 /***********************************************************************
2444 * XFONT_Match
2446 * Compute the matching score between the logical font and the device font.
2448 * contributions from highest to lowest:
2449 * charset
2450 * fixed pitch
2451 * height
2452 * family flags (only when the facename is not present)
2453 * width
2454 * weight, italics, underlines, strikeouts
2456 * NOTE: you can experiment with different penalty weights to see what happens.
2457 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2459 static UINT XFONT_Match( fontMatch* pfm )
2461 fontInfo* pfi = pfm->pfi; /* device font to match */
2462 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2463 UINT penalty = 0;
2464 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from text_caps */
2465 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2466 int d = 0, height;
2468 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2469 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2470 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2471 (pfi->df.dfItalic) ? "Italic" : "" );
2473 pfm->flags &= FO_MATCH_MASK;
2475 /* Charset */
2476 /* pfm->internal_charset: given(required) charset */
2477 /* pfi->internal_charset: charset of this font */
2478 if (pfi->internal_charset == DEFAULT_CHARSET)
2480 /* special case(unicode font) */
2481 /* priority: unmatched charset < unicode < matched charset */
2482 penalty += 0x50;
2484 else
2486 if( pfm->internal_charset == DEFAULT_CHARSET )
2489 if (pfi->internal_charset != ANSI_CHARSET)
2490 penalty += 0x200;
2492 if ( pfi->codepage != GetACP() )
2493 penalty += 0x200;
2495 else if (pfm->internal_charset != pfi->internal_charset)
2497 if ( pfi->internal_charset & 0xff00 )
2498 penalty += 0x1000; /* internal charset - should not be used */
2499 else
2500 penalty += 0x200;
2504 /* Height */
2505 height = -1;
2507 if( plf->lfHeight > 0 )
2509 int h = pfi->df.dfPixHeight;
2510 d = h - plf->lfHeight;
2511 height = plf->lfHeight;
2513 else
2515 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2516 if (h)
2518 d = h + plf->lfHeight;
2519 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2521 else
2523 ERR("PixHeight == InternalLeading\n");
2524 penalty += 0x1000; /* don't want this */
2529 if( height == 0 )
2530 pfm->height = 1; /* Very small */
2531 else if( d )
2533 if( bScale )
2534 pfm->height = height;
2535 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2537 if( d > 0 ) /* do not shrink raster fonts */
2539 pfm->height = pfi->df.dfPixHeight;
2540 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2542 else /* expand only in integer multiples */
2544 pfm->height = height - height%pfi->df.dfPixHeight;
2545 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2548 else /* can't be scaled at all */
2550 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2551 pfm->height = pfi->df.dfPixHeight;
2552 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2555 else
2556 pfm->height = pfi->df.dfPixHeight;
2558 /* Pitch and Family */
2559 if( pfm->flags & FO_MATCH_PAF ) {
2560 int family = plf->lfPitchAndFamily & FF_FAMILY;
2562 /* TMPF_FIXED_PITCH means exactly the opposite */
2563 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2564 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2565 } else /* Variable is the default */
2566 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2568 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2569 penalty += 0x10;
2572 /* Width */
2573 if( plf->lfWidth )
2575 int h;
2576 if( bR6 || bScale ) h = 0;
2577 else
2579 /* FIXME: not complete */
2581 pfm->flags |= FO_SYNTH_WIDTH;
2582 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2584 penalty += h * ( d ) ? 0x2 : 0x1 ;
2586 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2588 /* Weight */
2589 if( plf->lfWeight != FW_DONTCARE )
2591 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2592 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2593 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2595 /* Italic */
2596 if( plf->lfItalic != pfi->df.dfItalic )
2598 penalty += 0x4;
2599 pfm->flags |= FO_SYNTH_ITALIC;
2601 /* Underline */
2602 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2604 /* Strikeout */
2605 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2608 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2609 penalty++;
2611 TRACE(" returning %i\n", penalty );
2613 return penalty;
2616 /***********************************************************************
2617 * XFONT_MatchFIList
2619 * Scan a particular font resource for the best match.
2621 static UINT XFONT_MatchFIList( fontMatch* pfm )
2623 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2624 UINT current_score, score = (UINT)(-1);
2625 fontMatch fm = *pfm;
2627 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2629 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2630 continue;
2632 current_score = XFONT_Match( &fm );
2633 if( score > current_score )
2635 *pfm = fm;
2636 score = current_score;
2639 return score;
2642 /***********************************************************************
2643 * XFONT_is_ansi_charset
2645 * returns TRUE if the given charset is ANSI_CHARSET.
2647 static BOOL XFONT_is_ansi_charset( UINT charset )
2649 return ((charset == ANSI_CHARSET) ||
2650 (charset == DEFAULT_CHARSET && GetACP() == 1252));
2653 /***********************************************************************
2654 * XFONT_MatchDeviceFont
2656 * Scan font resource tree.
2659 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2661 fontMatch fm = *pfm;
2662 UINT current_score, score = (UINT)(-1);
2663 fontResource** ppfr;
2665 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2666 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2667 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2669 pfm->pfi = NULL;
2671 /* the following hard-coded font binding assumes 'ANSI_CHARSET'. */
2672 if( !fm.plf->lfFaceName[0] &&
2673 XFONT_is_ansi_charset(fm.plf->lfCharSet) )
2675 switch(fm.plf->lfPitchAndFamily & 0xf0)
2677 case FF_MODERN:
2678 strcpy(fm.plf->lfFaceName, "Courier New");
2679 break;
2680 case FF_ROMAN:
2681 strcpy(fm.plf->lfFaceName, "Times New Roman");
2682 break;
2683 case FF_SWISS:
2684 strcpy(fm.plf->lfFaceName, "Arial");
2685 break;
2686 default:
2687 if((fm.plf->lfPitchAndFamily & 0x0f) == FIXED_PITCH)
2688 strcpy(fm.plf->lfFaceName, "Courier New");
2689 else
2690 strcpy(fm.plf->lfFaceName, "Arial");
2691 break;
2695 if( fm.plf->lfFaceName[0] )
2697 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2698 if( fm.pfr ) /* match family */
2700 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2702 if( fm.pfr->fr_flags & FR_REMOVED )
2703 fm.pfr = 0;
2704 else
2706 XFONT_MatchFIList( &fm );
2707 *pfm = fm;
2708 if (pfm->pfi)
2709 return;
2713 /* get charset if lfFaceName is one of known facenames. */
2715 const struct CharsetBindingInfo* pcharsetbindings;
2717 pcharsetbindings = &charsetbindings[0];
2718 while ( pcharsetbindings->pszFaceName != NULL )
2720 if ( !strcmp( pcharsetbindings->pszFaceName,
2721 fm.plf->lfFaceName ) )
2723 fm.internal_charset = pcharsetbindings->charset;
2724 break;
2726 pcharsetbindings ++;
2728 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2732 /* match all available fonts */
2734 fm.flags |= FO_MATCH_PAF;
2735 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2737 if( (*ppfr)->fr_flags & FR_REMOVED )
2739 if( (*ppfr)->fo_count == 0 )
2740 XFONT_RemoveFontResource( ppfr );
2741 continue;
2744 fm.pfr = *ppfr;
2746 TRACE("%s\n", fm.pfr->lfFaceName );
2748 current_score = XFONT_MatchFIList( &fm );
2749 if( current_score < score )
2751 score = current_score;
2752 *pfm = fm;
2758 /***********************************************************************
2759 * X Font Cache
2761 static void XFONT_GrowFreeList(int start, int end)
2763 /* add all entries from 'start' up to and including 'end' */
2765 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2767 fontCache[end].lru = fontLF;
2768 fontCache[end].count = -1;
2769 fontLF = start;
2770 while( start < end )
2772 fontCache[start].count = -1;
2773 fontCache[start].lru = start + 1;
2774 start++;
2778 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2780 UINT16 cs = __lfCheckSum( plf );
2781 int i = fontMRU, prev = -1;
2783 *checksum = cs;
2784 while( i >= 0 )
2786 if( fontCache[i].lfchecksum == cs &&
2787 !(fontCache[i].fo_flags & FO_REMOVED) )
2789 /* FIXME: something more intelligent here ? */
2791 if( !memcmp( plf, &fontCache[i].lf,
2792 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2793 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2795 /* remove temporarily from the lru list */
2796 if( prev >= 0 )
2797 fontCache[prev].lru = fontCache[i].lru;
2798 else
2799 fontMRU = (INT16)fontCache[i].lru;
2800 return (fontCache + i);
2803 prev = i;
2804 i = (INT16)fontCache[i].lru;
2806 return NULL;
2809 static fontObject* XFONT_GetCacheEntry(void)
2811 int i;
2813 if( fontLF == -1 )
2815 int prev_i, prev_j, j;
2817 TRACE("font cache is full\n");
2819 /* lookup the least recently used font */
2821 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2823 if( fontCache[i].count <= 0 &&
2824 !(fontCache[i].fo_flags & FO_SYSTEM) )
2826 prev_j = prev_i;
2827 j = i;
2829 prev_i = i;
2832 if( j >= 0 ) /* unload font */
2834 /* detach from the lru list */
2836 TRACE("\tfreeing entry %i\n", j );
2838 fontCache[j].fr->fo_count--;
2840 if( prev_j >= 0 )
2841 fontCache[prev_j].lru = fontCache[j].lru;
2842 else fontMRU = (INT16)fontCache[j].lru;
2844 /* FIXME: lpXForm, lpPixmap */
2845 if(fontCache[j].lpX11Trans)
2846 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2848 wine_tsx11_lock();
2849 XFreeFont( gdi_display, fontCache[j].fs );
2850 wine_tsx11_unlock();
2852 memset( fontCache + j, 0, sizeof(fontObject) );
2853 return (fontCache + j);
2855 else /* expand cache */
2857 fontObject* newCache;
2859 prev_i = fontCacheSize + FONTCACHE;
2861 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2863 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2864 fontCache, prev_i * sizeof(fontObject))) )
2866 i = fontCacheSize;
2867 fontCacheSize = prev_i;
2868 fontCache = newCache;
2869 XFONT_GrowFreeList( i, fontCacheSize - 1);
2871 else return NULL;
2875 /* detach from the free list */
2877 i = fontLF;
2878 fontLF = (INT16)fontCache[i].lru;
2879 fontCache[i].count = 0;
2880 return (fontCache + i);
2883 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2885 UINT u = (UINT)(pfo - fontCache);
2886 int i;
2887 int ret;
2889 if( u < fontCacheSize )
2891 ret = --fontCache[u].count;
2892 if ( ret == 0 )
2894 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2896 if( CHECK_PFONT(pfo->prefobjs[i]) )
2897 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2901 return ret;
2904 return -1;
2907 /***********************************************************************
2908 * X11DRV_FONT_InitX11Metrics
2910 * Initialize font resource list and allocate font cache.
2912 void X11DRV_FONT_InitX11Metrics( void )
2914 char** x_pattern;
2915 unsigned x_checksum;
2916 int i, x_count, fd, buf_size;
2917 char *buffer;
2918 HKEY hkey;
2921 wine_tsx11_lock();
2922 x_pattern = XListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2923 wine_tsx11_unlock();
2925 TRACE("Font Mapper: initializing %i x11 fonts\n", x_count);
2926 if (x_count == MAX_FONTS)
2927 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2929 for( i = x_checksum = 0; i < x_count; i++ )
2931 int j;
2932 #if 0
2933 printf("%i\t: %s\n", i, x_pattern[i] );
2934 #endif
2936 j = strlen( x_pattern[i] );
2937 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2939 x_checksum |= X_PFONT_MAGIC;
2940 buf_size = 128;
2941 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2943 /* deal with systemwide font metrics cache */
2945 buffer[0] = 0;
2946 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2948 DWORD type, count = buf_size;
2949 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, buffer, &count);
2950 RegCloseKey(hkey);
2953 if( buffer[0] )
2955 fd = open( buffer, O_RDONLY );
2956 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2958 if (fontList == NULL)
2960 /* try per-user */
2961 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2962 if( buffer[0] )
2964 fd = open( buffer, O_RDONLY );
2965 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2969 if( fontList == NULL ) /* build metrics from scratch */
2971 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2972 if( buffer[0] ) /* update cached metrics */
2974 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2975 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2977 WARN("Unable to write to fontcache '%s'\n", buffer);
2978 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2983 wine_tsx11_lock();
2984 XFreeFontNames(x_pattern);
2986 /* check if we're dealing with X11 R6 server */
2988 XFontStruct* x_fs;
2989 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2990 if( (x_fs = safe_XLoadQueryFont(gdi_display, buffer)) )
2992 text_caps |= TC_SF_X_YINDEP;
2993 XFreeFont(gdi_display, x_fs);
2996 wine_tsx11_unlock();
2998 HeapFree(GetProcessHeap(), 0, buffer);
3000 XFONT_WindowsNames();
3001 XFONT_LoadAliases();
3002 XFONT_LoadDefaults();
3003 XFONT_LoadIgnores();
3005 /* fontList initialization is over, allocate X font cache */
3007 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
3008 XFONT_GrowFreeList(0, fontCacheSize - 1);
3010 TRACE("done!\n");
3013 /***********************************************************************
3014 * X11DRV_FONT_Init
3016 void X11DRV_FONT_Init( int *log_pixels_x, int *log_pixels_y )
3018 XFONT_GetPointResolution( log_pixels_x, log_pixels_y );
3020 if(using_client_side_fonts)
3021 text_caps |= TC_VA_ABLE;
3023 return;
3026 /**********************************************************************
3027 * XFONT_SetX11Trans
3029 static BOOL XFONT_SetX11Trans( fontObject *pfo )
3031 char *fontName;
3032 Atom nameAtom;
3033 LFD lfd;
3035 wine_tsx11_lock();
3036 XGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
3037 fontName = XGetAtomName( gdi_display, nameAtom );
3038 if (!LFD_Parse(fontName, &lfd) || lfd.pixel_size[0] != '[')
3040 XFree(fontName);
3041 wine_tsx11_unlock();
3042 return FALSE;
3044 #define PX pfo->lpX11Trans
3046 sscanf(lfd.pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
3047 XFree(fontName);
3049 XGetFontProperty( pfo->fs, x11drv_atom(RAW_ASCENT), &PX->RAW_ASCENT );
3050 XGetFontProperty( pfo->fs, x11drv_atom(RAW_DESCENT), &PX->RAW_DESCENT );
3051 wine_tsx11_unlock();
3053 PX->pixelsize = hypot(PX->a, PX->b);
3054 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
3055 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
3057 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
3058 PX->a, PX->b, PX->c, PX->d,
3059 PX->RAW_ASCENT, PX->RAW_DESCENT);
3061 #undef PX
3062 return TRUE;
3065 /***********************************************************************
3066 * X Device Font Objects
3068 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
3069 LPCSTR* faceMatched, BOOL bSubFont,
3070 WORD internal_charset,
3071 WORD* pcharsetMatched )
3073 UINT16 checksum;
3074 INT index = 0;
3075 fontObject* pfo;
3077 pfo = XFONT_LookupCachedFont( plf, &checksum );
3078 if( !pfo )
3080 fontMatch fm;
3081 INT i;
3083 fm.pfr = NULL;
3084 fm.pfi = NULL;
3085 fm.height = 0;
3086 fm.flags = 0;
3087 fm.plf = plf;
3088 fm.internal_charset = internal_charset;
3090 if( text_caps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
3092 /* allocate new font cache entry */
3094 if( (pfo = XFONT_GetCacheEntry()) )
3096 /* initialize entry and load font */
3097 char lpLFD[MAX_LFD_LENGTH];
3098 UINT uRelaxLevel = 0;
3100 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
3101 ERR(
3102 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
3103 plf->lfHeight);
3104 pfo->rescale = fabs(plf->lfHeight / 100.0);
3105 if(plf->lfHeight > 0) plf->lfHeight = 100;
3106 else plf->lfHeight = -100;
3107 } else
3108 pfo->rescale = 1.0;
3110 XFONT_MatchDeviceFont( fontList, &fm );
3111 pfo->fr = fm.pfr;
3112 pfo->fi = fm.pfi;
3113 pfo->fr->fo_count++;
3114 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3116 pfo->lf = *plf;
3117 pfo->lfchecksum = checksum;
3121 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3122 if( (pfo->fs = safe_XLoadQueryFont( gdi_display, lpLFD )) ) break;
3123 } while( uRelaxLevel );
3126 if(pfo->lf.lfEscapement != 0) {
3127 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3128 if(!XFONT_SetX11Trans( pfo )) {
3129 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3130 pfo->lpX11Trans = NULL;
3133 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3134 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3135 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3136 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3138 /* FIXME: If we've got a soft font or
3139 * there are FO_SYNTH_... flags for the
3140 * non PROOF_QUALITY request, the engine
3141 * should rasterize characters into mono
3142 * pixmaps and store them in the pfo->lpPixmap
3143 * array (pfo->fs should be updated as well).
3144 * array (pfo->fs should be updated as well).
3145 * X11DRV_ExtTextOut() must be heavily modified
3146 * to support pixmap blitting and FO_SYNTH_...
3147 * styles.
3150 pfo->lpPixmap = NULL;
3152 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3153 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3155 /* special treatment for DBCS that needs multiple fonts */
3156 /* All member of pfo must be set correctly. */
3157 if ( bSubFont == FALSE )
3159 WORD charset_sub;
3160 WORD charsetMatchedSub;
3161 LOGFONT16 lfSub;
3162 LPCSTR faceMatchedSub;
3164 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3166 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3167 penum_subfont_charset( i );
3168 if ( charset_sub == DEFAULT_CHARSET ) break;
3170 lfSub = *plf;
3171 lfSub.lfWidth = 0;
3172 lfSub.lfHeight=plf->lfHeight;
3173 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3174 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3175 /* this font has sub font */
3176 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3177 pfo->prefobjs[i] =
3178 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3179 TRUE, charset_sub,
3180 &charsetMatchedSub );
3181 /* FIXME: check charsetMatchedSub */
3186 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3188 UINT current_score, score = (UINT)(-1);
3190 i = index = fontMRU;
3191 fm.flags |= FO_MATCH_PAF;
3194 pfo = fontCache + i;
3195 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3197 current_score = XFONT_Match( &fm );
3198 if( current_score < score ) index = i;
3200 i = pfo->lru;
3201 } while( i >= 0 );
3202 pfo = fontCache + index;
3203 goto END;
3207 /* attach at the head of the lru list */
3208 pfo->lru = fontMRU;
3209 index = fontMRU = (pfo - fontCache);
3211 END:
3212 pfo->count++;
3214 TRACE("physfont %i\n", index);
3215 *faceMatched = pfo->fi->df.dfFace;
3216 *pcharsetMatched = pfo->fi->internal_charset;
3218 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3221 /***********************************************************************
3222 * XFONT_GetFontObject
3224 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3226 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3227 return NULL;
3230 /***********************************************************************
3231 * XFONT_GetFontStruct
3233 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3235 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3236 return NULL;
3239 /***********************************************************************
3240 * XFONT_GetFontInfo
3242 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3244 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3245 return NULL;
3250 /* X11DRV Interface ****************************************************
3252 * Exposed via the dc->funcs dispatch table. *
3254 ***********************************************************************/
3255 /***********************************************************************
3256 * SelectFont (X11DRV.@)
3258 HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont )
3260 LOGFONTW logfont;
3261 LOGFONT16 lf;
3263 TRACE("hdc=%p, hfont=%p\n", physDev->hdc, hfont);
3265 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return HGDI_ERROR;
3267 TRACE("dc->gdiFont = %p\n", physDev->dc->gdiFont);
3269 if(physDev->dc->gdiFont && using_client_side_fonts) {
3270 X11DRV_XRender_SelectFont(physDev, hfont);
3271 return FALSE;
3274 EnterCriticalSection( &crtsc_fonts_X11 );
3276 if(fontList == NULL) X11DRV_FONT_InitX11Metrics();
3278 if( CHECK_PFONT(physDev->font) )
3279 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3281 FONT_LogFontWTo16(&logfont, &lf);
3283 /* stock fonts ignore the mapping mode */
3284 if (!is_stock_font( hfont ))
3286 /* Make sure we don't change the sign when converting to device coords */
3287 /* FIXME - check that the other drivers do this correctly */
3288 if (lf.lfWidth)
3290 INT width = X11DRV_XWStoDS( physDev, lf.lfWidth );
3291 lf.lfWidth = (lf.lfWidth < 0) ? -abs(width) : abs(width);
3292 if (lf.lfWidth == 0)
3293 lf.lfWidth = 1; /* Minimum width */
3295 if (lf.lfHeight)
3297 INT height = X11DRV_YWStoDS( physDev, lf.lfHeight );
3298 lf.lfHeight = (lf.lfHeight < 0) ? -abs(height) : abs(height);
3299 if (lf.lfHeight == 0)
3300 lf.lfHeight = MIN_FONT_SIZE;
3304 if (!lf.lfHeight)
3305 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(physDev->hdc,LOGPIXELSY) + (72>>1)) / 72;
3308 /* Fixup aliases before passing to RealizeFont */
3309 /* alias = Windows name in the alias table */
3310 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3311 LPCSTR faceMatched;
3312 WORD charsetMatched;
3314 TRACE("hfont=%p\n", hfont); /* to connect with the trace from RealizeFont */
3315 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3316 FALSE, lf.lfCharSet,
3317 &charsetMatched );
3319 /* set face to the requested facename if it matched
3320 * so that GetTextFace can get the correct face name
3322 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3323 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3324 logfont.lfFaceName, LF_FACESIZE);
3325 else
3326 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3327 logfont.lfFaceName, LF_FACESIZE);
3330 * In X, some encodings may have the same lfFaceName.
3331 * for example:
3332 * -misc-fixed-*-iso8859-1
3333 * -misc-fixed-*-jisx0208.1990-0
3334 * so charset should be saved...
3336 logfont.lfCharSet = charsetMatched;
3339 LeaveCriticalSection( &crtsc_fonts_X11 );
3341 return (HFONT)1; /* Use a device font */
3345 /***********************************************************************
3347 * X11DRV_EnumDeviceFonts
3349 BOOL X11DRV_EnumDeviceFonts( X11DRV_PDEVICE *physDev, LPLOGFONTW plf,
3350 FONTENUMPROCW proc, LPARAM lp )
3352 ENUMLOGFONTEXW lf;
3353 NEWTEXTMETRICEXW tm;
3354 fontResource* pfr = fontList;
3355 BOOL b, bRet = 0;
3357 /* don't enumerate x11 fonts if we're using client side fonts */
3358 if (physDev->dc->gdiFont) return FALSE;
3360 if( plf->lfFaceName[0] )
3362 char facename[LF_FACESIZE+1];
3363 WideCharToMultiByte( CP_ACP, 0, plf->lfFaceName, -1,
3364 facename, sizeof(facename), NULL, NULL );
3365 /* enum all entries in this resource */
3366 pfr = XFONT_FindFIList( pfr, facename );
3367 if( pfr )
3369 fontInfo* pfi;
3370 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3372 /* Note: XFONT_GetFontMetric() will have to
3373 release the crit section, font list will
3374 have to be retraversed on return */
3376 if(plf->lfCharSet == DEFAULT_CHARSET ||
3377 plf->lfCharSet == pfi->df.dfCharSet) {
3378 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3379 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3380 bRet = b;
3381 else break;
3386 else /* enum first entry in each resource */
3387 for( ; pfr ; pfr = pfr->next )
3389 if(pfr->fi)
3391 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm,
3392 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3393 bRet = b;
3394 else break;
3397 return bRet;
3401 /***********************************************************************
3402 * X11DRV_GetTextMetrics
3404 BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
3406 if( CHECK_PFONT(physDev->font) )
3408 fontObject* pfo = __PFONT(physDev->font);
3409 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsW( pfo, metrics );
3410 return TRUE;
3412 return FALSE;
3416 /***********************************************************************
3417 * X11DRV_GetCharWidth
3419 BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
3420 LPINT buffer )
3422 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3424 if( pfo )
3426 int i;
3428 if (pfo->fs->per_char == NULL)
3429 for (i = firstChar; i <= lastChar; i++)
3430 if(pfo->lpX11Trans)
3431 *buffer++ = pfo->fs->min_bounds.attributes *
3432 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3433 else
3434 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3435 else
3437 XCharStruct *cs, *def;
3438 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3440 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3441 def);
3443 for (i = firstChar; i <= lastChar; i++)
3445 WCHAR wch = i;
3446 BYTE ch;
3447 UINT ch_f; /* character code in the font encoding */
3448 WideCharToMultiByte( pfo->fi->codepage, 0, &wch, 1, &ch, 1, NULL, NULL );
3449 ch_f = ch;
3450 if (ch_f >= pfo->fs->min_char_or_byte2 &&
3451 ch_f <= pfo->fs->max_char_or_byte2)
3453 cs = &pfo->fs->per_char[(ch_f - pfo->fs->min_char_or_byte2)];
3454 if (CI_NONEXISTCHAR(cs)) cs = def;
3455 } else cs = def;
3456 if(pfo->lpX11Trans)
3457 *buffer++ = max(cs->attributes, 0) *
3458 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3459 else
3460 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3464 return TRUE;
3466 return FALSE;