Support viewports the D3D7 way.
[wine/wine64.git] / graphics / x11drv / xfont.c
blob04d9ca0701b6143d7e42c3f4b6e3d6b0f65f5b31
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 <X11/Xatom.h>
29 #include "ts_xlib.h"
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <math.h>
42 #include "windef.h"
43 #include "wingdi.h"
44 #include "winnls.h"
45 #include "winreg.h"
46 #include "x11font.h"
47 #include "wine/library.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(font);
53 #define X_PFONT_MAGIC (0xFADE0000)
54 #define X_FMC_MAGIC (0x0000CAFE)
56 #define MAX_FONTS 1024*16
57 #define MAX_LFD_LENGTH 256
58 #define TILDE '~'
59 #define HYPHEN '-'
61 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
62 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
63 #define MIN_FONT_SIZE 2 /* Min size in pixels */
64 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
66 #define REMOVE_SUBSETS 1
67 #define UNMARK_SUBSETS 0
69 #define FONTCACHE 32 /* dynamic font cache size */
71 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
73 typedef struct __fontAlias
75 LPSTR faTypeFace;
76 LPSTR faAlias;
77 struct __fontAlias* next;
78 } fontAlias;
80 static fontAlias *aliasTable = NULL;
82 static const char* INIFontMetrics = "cachedmetrics.";
83 static const char* INIFontSection = "Software\\Wine\\Wine\\Config\\fonts";
84 static const char* INIAliasSection = "Alias";
85 static const char* INIIgnoreSection = "Ignore";
86 static const char* INIDefault = "Default";
87 static const char* INIDefaultFixed = "DefaultFixed";
88 static const char* INIResolution = "Resolution";
89 static const char* INIGlobalMetrics = "FontMetrics";
90 static const char* INIDefaultSerif = "DefaultSerif";
91 static const char* INIDefaultSansSerif = "DefaultSansSerif";
94 /* FIXME - are there any more Latin charsets ? */
95 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
96 #define IS_LATIN_CHARSET(ch) \
97 ((ch)==ANSI_CHARSET ||\
98 (ch)==EE_CHARSET ||\
99 (ch)==ISO3_CHARSET ||\
100 (ch)==ISO4_CHARSET ||\
101 (ch)==RUSSIAN_CHARSET ||\
102 (ch)==ARABIC_CHARSET ||\
103 (ch)==GREEK_CHARSET ||\
104 (ch)==HEBREW_CHARSET ||\
105 (ch)==TURKISH_CHARSET ||\
106 (ch)==ISO10_CHARSET ||\
107 (ch)==BALTIC_CHARSET ||\
108 (ch)==CELTIC_CHARSET)
110 /* suffix-charset mapping tables - must be less than 254 entries long */
112 typedef struct __sufch
114 LPCSTR psuffix;
115 WORD charset; /* hibyte != 0 means *internal* charset */
116 WORD codepage;
117 WORD cptable;
118 } SuffixCharset;
120 static const SuffixCharset sufch_ansi[] = {
121 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
122 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
124 static const SuffixCharset sufch_iso646[] = {
125 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
126 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
128 static const SuffixCharset sufch_iso8859[] = {
129 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
130 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
131 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
132 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
133 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
134 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
135 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
136 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
137 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
138 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
139 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
140 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
141 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
142 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
143 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
144 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
146 static const SuffixCharset sufch_microsoft[] = {
147 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
148 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
149 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
150 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
151 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
152 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
153 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
154 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
155 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
156 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
157 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
159 static const SuffixCharset sufch_tcvn[] = {
160 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
161 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
163 static const SuffixCharset sufch_tis620[] = {
164 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
165 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
167 static const SuffixCharset sufch_viscii[] = {
168 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
169 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
171 static const SuffixCharset sufch_windows[] = {
172 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
173 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
174 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
175 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
176 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
177 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
178 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
179 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
180 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
182 static const SuffixCharset sufch_koi8[] = {
183 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
184 { "ru", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
185 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
186 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
188 static const SuffixCharset sufch_jisx0201[] = {
189 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
190 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
192 static const SuffixCharset sufch_jisx0208[] = {
193 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
194 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
196 static const SuffixCharset sufch_jisx0212[] = {
197 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
198 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
200 static const SuffixCharset sufch_ksc5601[] = {
201 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
202 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
204 static const SuffixCharset sufch_gb2312[] = {
205 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
206 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
208 static const SuffixCharset sufch_big5[] = {
209 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
210 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
212 static const SuffixCharset sufch_unicode[] = {
213 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
214 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
216 static const SuffixCharset sufch_iso10646[] = {
217 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
218 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
220 static const SuffixCharset sufch_dec[] = {
221 { "dectech", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
222 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
224 /* Each of these must be matched explicitly */
225 static const SuffixCharset sufch_any[] = {
226 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
227 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
230 typedef struct __fet
232 LPSTR prefix;
233 const SuffixCharset* sufch;
234 struct __fet* next;
235 } fontEncodingTemplate;
237 /* Note: we can attach additional encoding mappings to the end
238 * of this table at runtime.
240 static fontEncodingTemplate __fETTable[] = {
241 { "ansi", sufch_ansi, &__fETTable[1] },
242 { "ascii", sufch_ansi, &__fETTable[2] },
243 { "iso646.1991", sufch_iso646, &__fETTable[3] },
244 { "iso8859", sufch_iso8859, &__fETTable[4] },
245 { "microsoft", sufch_microsoft, &__fETTable[5] },
246 { "tcvn", sufch_tcvn, &__fETTable[6] },
247 { "tis620.2533", sufch_tis620, &__fETTable[7] },
248 { "viscii1.1", sufch_viscii, &__fETTable[8] },
249 { "windows", sufch_windows, &__fETTable[9] },
250 { "koi8", sufch_koi8, &__fETTable[10]},
251 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
252 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
253 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
254 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
255 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
256 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
257 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
258 { "big5", sufch_big5, &__fETTable[18]},
259 { "unicode", sufch_unicode, &__fETTable[19]},
260 { "iso10646", sufch_iso10646, &__fETTable[20]},
261 { "cp", sufch_windows, &__fETTable[21]},
262 { "dec", sufch_dec, &__fETTable[22]},
263 /* NULL prefix matches anything so put it last */
264 { NULL, sufch_any, NULL },
266 static fontEncodingTemplate* fETTable = __fETTable;
268 /* a charset database for known facenames */
269 struct CharsetBindingInfo
271 const char* pszFaceName;
272 BYTE charset;
274 static const struct CharsetBindingInfo charsetbindings[] =
276 /* special facenames */
277 { "System", DEFAULT_CHARSET },
278 { "FixedSys", DEFAULT_CHARSET },
280 /* known facenames */
281 { "MS Serif", ANSI_CHARSET },
282 { "MS Sans Serif", ANSI_CHARSET },
283 { "Courier", ANSI_CHARSET },
284 { "Symbol", SYMBOL_CHARSET },
286 { "Arial", ANSI_CHARSET },
287 { "Arial Greek", GREEK_CHARSET },
288 { "Arial Tur", TURKISH_CHARSET },
289 { "Arial Baltic", BALTIC_CHARSET },
290 { "Arial CE", EASTEUROPE_CHARSET },
291 { "Arial Cyr", RUSSIAN_CHARSET },
292 { "Courier New", ANSI_CHARSET },
293 { "Courier New Greek", GREEK_CHARSET },
294 { "Courier New Tur", TURKISH_CHARSET },
295 { "Courier New Baltic", BALTIC_CHARSET },
296 { "Courier New CE", EASTEUROPE_CHARSET },
297 { "Courier New Cyr", RUSSIAN_CHARSET },
298 { "Times New Roman", ANSI_CHARSET },
299 { "Times New Roman Greek", GREEK_CHARSET },
300 { "Times New Roman Tur", TURKISH_CHARSET },
301 { "Times New Roman Baltic", BALTIC_CHARSET },
302 { "Times New Roman CE", EASTEUROPE_CHARSET },
303 { "Times New Roman Cyr", RUSSIAN_CHARSET },
305 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
306 SHIFTJIS_CHARSET }, /* MS gothic */
307 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
308 SHIFTJIS_CHARSET }, /* MS P gothic */
309 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
310 SHIFTJIS_CHARSET }, /* MS mincho */
311 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
312 SHIFTJIS_CHARSET }, /* MS P mincho */
313 { "GulimChe", HANGEUL_CHARSET },
314 { "MS Song", GB2312_CHARSET },
315 { "MS Hei", GB2312_CHARSET },
316 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
317 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
319 { NULL, 0 }
323 static int DefResolution = 0;
325 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT("crtsc_fonts_X11");
327 static fontResource* fontList = NULL;
328 static fontObject* fontCache = NULL; /* array */
329 static int fontCacheSize = FONTCACHE;
330 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
332 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
333 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
334 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
336 static Atom RAW_ASCENT;
337 static Atom RAW_DESCENT;
339 /***********************************************************************
340 * Helper macros from X distribution
343 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
344 (((cs)->rbearing|(cs)->lbearing| \
345 (cs)->ascent|(cs)->descent) == 0))
347 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
349 cs = def; \
350 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
351 if (fs->per_char == NULL) { \
352 cs = &fs->min_bounds; \
353 } else { \
354 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
355 if (CI_NONEXISTCHAR(cs)) cs = def; \
360 #define CI_GET_DEFAULT_INFO(fs,cs) \
361 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
364 /***********************************************************************
365 * is_stock_font
367 inline static BOOL is_stock_font( HFONT font )
369 int i;
370 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
372 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
374 return FALSE;
378 static void FONT_LogFontWTo16( const LOGFONTW* font32, LPLOGFONT16 font16 )
380 font16->lfHeight = font32->lfHeight;
381 font16->lfWidth = font32->lfWidth;
382 font16->lfEscapement = font32->lfEscapement;
383 font16->lfOrientation = font32->lfOrientation;
384 font16->lfWeight = font32->lfWeight;
385 font16->lfItalic = font32->lfItalic;
386 font16->lfUnderline = font32->lfUnderline;
387 font16->lfStrikeOut = font32->lfStrikeOut;
388 font16->lfCharSet = font32->lfCharSet;
389 font16->lfOutPrecision = font32->lfOutPrecision;
390 font16->lfClipPrecision = font32->lfClipPrecision;
391 font16->lfQuality = font32->lfQuality;
392 font16->lfPitchAndFamily = font32->lfPitchAndFamily;
393 WideCharToMultiByte( CP_ACP, 0, font32->lfFaceName, -1,
394 font16->lfFaceName, LF_FACESIZE, NULL, NULL );
395 font16->lfFaceName[LF_FACESIZE-1] = 0;
399 /***********************************************************************
400 * Checksums
402 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
404 CHAR font[LF_FACESIZE];
405 UINT16 checksum = 0;
406 UINT16 *ptr;
407 int i;
409 ptr = (UINT16 *)plf;
410 for (i = 0; i < 9; i++) checksum ^= *ptr++;
411 for (i = 0; i < LF_FACESIZE; i++)
413 font[i] = tolower(plf->lfFaceName[i]);
414 if (!font[i] || font[i] == ' ') break;
416 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
417 return checksum;
420 static UINT16 __genericCheckSum( const void *ptr, int size )
422 unsigned int checksum = 0;
423 const char *p = (const char *)ptr;
424 while (size-- > 0)
425 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
427 return checksum & 0xffff;
430 /*************************************************************************
431 * LFD parse/compose routines
433 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
434 * make a copy first
436 * These functions also do TILDE to HYPHEN conversion
438 static LFD* LFD_Parse(LPSTR lpFont)
440 LFD* lfd;
441 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
442 int i;
443 if (*lpch != HYPHEN)
445 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
446 return NULL;
449 field_start = ++lpch;
450 for( i = 0; i < LFD_FIELDS; )
452 if (*lpch == HYPHEN)
454 *lpch = '\0';
455 lfd_fld[i] = field_start;
456 i++;
457 field_start = ++lpch;
459 else if (!*lpch)
461 lfd_fld[i] = field_start;
462 i++;
463 break;
465 else if (*lpch == TILDE)
467 *lpch = HYPHEN;
468 ++lpch;
470 else
471 ++lpch;
473 /* Fill in the empty fields with NULLS */
474 for (; i< LFD_FIELDS; i++)
475 lfd_fld[i] = NULL;
476 if (*lpch)
477 WARN("Extra ignored in font '%s'\n", lpFont);
479 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
480 if (lfd)
482 lfd->foundry = lfd_fld[0];
483 lfd->family = lfd_fld[1];
484 lfd->weight = lfd_fld[2];
485 lfd->slant = lfd_fld[3];
486 lfd->set_width = lfd_fld[4];
487 lfd->add_style = lfd_fld[5];
488 lfd->pixel_size = lfd_fld[6];
489 lfd->point_size = lfd_fld[7];
490 lfd->resolution_x = lfd_fld[8];
491 lfd->resolution_y = lfd_fld[9];
492 lfd->spacing = lfd_fld[10];
493 lfd->average_width = lfd_fld[11];
494 lfd->charset_registry = lfd_fld[12];
495 lfd->charset_encoding = lfd_fld[13];
497 return lfd;
501 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
503 const char* lfd_fld[LFD_FIELDS];
504 int i;
506 if (!buf_size)
507 return; /* Dont be silly */
509 lfd_fld[0] = lfd->foundry;
510 lfd_fld[1] = lfd->family;
511 lfd_fld[2] = lfd->weight ;
512 lfd_fld[3] = lfd->slant ;
513 lfd_fld[4] = lfd->set_width ;
514 lfd_fld[5] = lfd->add_style;
515 lfd_fld[6] = lfd->pixel_size;
516 lfd_fld[7] = lfd->point_size;
517 lfd_fld[8] = lfd->resolution_x;
518 lfd_fld[9] = lfd->resolution_y ;
519 lfd_fld[10] = lfd->spacing ;
520 lfd_fld[11] = lfd->average_width ;
521 lfd_fld[12] = lfd->charset_registry ;
522 lfd_fld[13] = lfd->charset_encoding ;
524 buf_size--; /* Room for the terminator */
526 for (i = 0; i < LFD_FIELDS; i++)
528 const char* sp = lfd_fld[i];
529 if (!sp || !buf_size)
530 break;
532 *dp++ = HYPHEN;
533 buf_size--;
534 while (buf_size > 0 && *sp)
536 *dp = (*sp == HYPHEN) ? TILDE : *sp;
537 buf_size--;
538 dp++; sp++;
541 *dp = '\0';
545 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
547 int j = strlen(lpStr);
548 if( j == 1 && *lpStr == '0')
549 fi->fi_flags |= FI_POLYWEIGHT;
550 else if( j == 4 )
552 if( !strcasecmp( "bold", lpStr) )
553 fi->df.dfWeight = FW_BOLD;
554 else if( !strcasecmp( "demi", lpStr) )
556 fi->fi_flags |= FI_FW_DEMI;
557 fi->df.dfWeight = FW_DEMIBOLD;
559 else if( !strcasecmp( "book", lpStr) )
561 fi->fi_flags |= FI_FW_BOOK;
562 fi->df.dfWeight = FW_REGULAR;
565 else if( j == 5 )
567 if( !strcasecmp( "light", lpStr) )
568 fi->df.dfWeight = FW_LIGHT;
569 else if( !strcasecmp( "black", lpStr) )
570 fi->df.dfWeight = FW_BLACK;
572 else if( j == 6 && !strcasecmp( "medium", lpStr) )
573 fi->df.dfWeight = FW_REGULAR;
574 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
575 fi->df.dfWeight = FW_DEMIBOLD;
576 else
577 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
578 * from the weight property */
581 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
583 int l = strlen(lpStr);
584 if( l == 1 )
586 switch( tolower( *lpStr ) )
588 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
589 default:
590 case 'r': fi->df.dfItalic = 0;
591 break;
592 case 'o':
593 fi->fi_flags |= FI_OBLIQUE;
594 case 'i': fi->df.dfItalic = 1;
595 break;
597 return FALSE;
599 return TRUE;
602 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
604 int j = strlen(lpstr);
605 if( j > 3 ) /* find out is there "sans" or "script" */
607 j = 0;
609 if( strstr(lpstr, "sans") )
611 fi->df.dfPitchAndFamily |= FF_SWISS;
612 j = 1;
614 if( strstr(lpstr, "script") )
616 fi->df.dfPitchAndFamily |= FF_SCRIPT;
617 j = 1;
619 if( !j && dec_style_check )
620 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
624 /*************************************************************************
625 * LFD_InitFontInfo
627 * INIT ONLY
629 * Fill in some fields in the fontInfo struct.
631 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
633 int i, j, dec_style_check, scalability;
634 fontEncodingTemplate* boba;
635 const char* ridiculous = "font '%s' has ridiculous %s\n";
636 const char* lpstr;
638 if (!lfd->charset_registry)
640 WARN("font '%s' does not have enough fields\n", fullname);
641 return FALSE;
644 memset(fi, 0, sizeof(fontInfo) );
646 /* weight name - */
647 LFD_GetWeight( fi, lfd->weight);
649 /* slant - */
650 dec_style_check = LFD_GetSlant( fi, lfd->slant);
652 /* width name - */
653 lpstr = lfd->set_width;
654 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
655 dec_style_check = TRUE;
656 else
657 fi->fi_flags |= FI_NORMAL;
659 /* style - */
660 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
662 /* pixel & decipoint height, and res_x & y */
664 scalability = 0;
666 j = strlen(lfd->pixel_size);
667 if( j == 0 || j > 3 )
669 WARN(ridiculous, fullname, "pixel_size");
670 return FALSE;
672 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
673 scalability++;
675 j = strlen(lfd->point_size);
676 if( j == 0 || j > 3 )
678 WARN(ridiculous, fullname, "point_size");
679 return FALSE;
681 if( !(atoi(lfd->point_size)) )
682 scalability++;
684 j = strlen(lfd->resolution_x);
685 if( j == 0 || j > 3 )
687 WARN(ridiculous, fullname, "resolution_x");
688 return FALSE;
690 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
691 scalability++;
693 j = strlen(lfd->resolution_y);
694 if( j == 0 || j > 3 )
696 WARN(ridiculous, fullname, "resolution_y");
697 return FALSE;
699 if( !(atoi(lfd->resolution_y)) )
700 scalability++;
702 /* Check scalability */
703 switch (scalability)
705 case 0: /* Bitmap */
706 break;
707 case 4: /* Scalable */
708 fi->fi_flags |= FI_SCALABLE;
709 break;
710 case 2:
711 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
712 TRACE("Skipping scalable bitmap '%s'\n", fullname);
713 return FALSE;
714 default:
715 WARN("Font '%s' has weird scalability\n", fullname);
716 return FALSE;
719 /* spacing - */
720 lpstr = lfd->spacing;
721 switch( *lpstr )
723 case '\0':
724 WARN("font '%s' has no spacing\n", fullname);
725 return FALSE;
727 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
728 break;
729 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
730 fi->fi_flags |= FI_FIXEDEX;
731 /* fall through */
732 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
733 break;
734 default:
735 /* Of course this line does nothing */
736 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
739 /* average width - */
740 lpstr = lfd->average_width;
741 j = strlen(lpstr);
742 if( j == 0 || j > 3 )
744 WARN(ridiculous, fullname, "average_width");
745 return FALSE;
748 if( !(atoi(lpstr)) && !scalability )
750 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
751 return FALSE;
754 /* charset registry, charset encoding - */
755 lpstr = lfd->charset_registry;
756 if( strstr(lpstr, "ksc") ||
757 strstr(lpstr, "gb2312") ||
758 strstr(lpstr, "big5") )
760 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
763 fi->df.dfCharSet = ANSI_CHARSET;
765 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
767 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
769 if (lfd->charset_encoding)
771 for( j = 0; boba->sufch[j].psuffix; j++ )
773 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
775 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
776 fi->internal_charset = boba->sufch[j].charset;
777 fi->codepage = boba->sufch[j].codepage;
778 fi->cptable = boba->sufch[j].cptable;
779 goto done;
783 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
784 fi->internal_charset = boba->sufch[j].charset;
785 fi->codepage = boba->sufch[j].codepage;
786 fi->cptable = boba->sufch[j].cptable;
787 if (boba->prefix)
789 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
790 fullname, lfd->charset_encoding, boba->prefix);
791 j = 254;
793 else
795 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
796 fullname, lfd->charset_registry, lfd->charset_encoding);
797 j = 255;
800 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
801 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
802 goto done;
804 else if (boba->prefix)
806 WARN("font '%s' has known registry '%s' and no character encoding\n",
807 fullname, lpstr);
808 for( j = 0; boba->sufch[j].psuffix; j++ )
810 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
811 fi->internal_charset = boba->sufch[j].charset;
812 fi->codepage = boba->sufch[j].codepage;
813 fi->cptable = boba->sufch[j].cptable;
814 j = 255;
815 goto done;
819 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
820 return FALSE;
822 done:
823 /* i - index into fETTable
824 * j - index into suffix array for fETTable[i]
825 * except:
826 * 254 - found encoding prefix, unknown suffix
827 * 255 - no encoding match at all.
829 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
831 return TRUE;
835 /*************************************************************************
836 * LFD_AngleMatrix
838 * make a matrix suitable for LFD based on theta radians
840 static void LFD_AngleMatrix( char* buffer, int h, double theta)
842 double matrix[4];
843 matrix[0] = h*cos(theta);
844 matrix[1] = h*sin(theta);
845 matrix[2] = -h*sin(theta);
846 matrix[3] = h*cos(theta);
847 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
850 /*************************************************************************
851 * LFD_ComposeLFD
853 * Note: uRelax is a treatment not a cure. Font mapping algorithm
854 * should be bulletproof enough to allow us to avoid hacks like
855 * this despite LFD being so braindead.
857 static BOOL LFD_ComposeLFD( const fontObject* fo,
858 INT height, LPSTR lpLFD, UINT uRelax )
860 int i, h;
861 char *any = "*";
862 char h_string[64], resx_string[64], resy_string[64];
863 LFD aLFD;
865 /* Get the worst case over with first */
867 /* RealizeFont() will call us repeatedly with increasing uRelax
868 * until XLoadFont() succeeds.
869 * to avoid an infinite loop; these will always match
871 if (uRelax >= 5)
873 if (uRelax == 5)
874 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
875 else
876 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
877 return TRUE;
880 /* read foundry + family from fo */
881 aLFD.foundry = fo->fr->resource->foundry;
882 aLFD.family = fo->fr->resource->family;
884 /* add weight */
885 switch( fo->fi->df.dfWeight )
887 case FW_BOLD:
888 aLFD.weight = "bold"; break;
889 case FW_REGULAR:
890 if( fo->fi->fi_flags & FI_FW_BOOK )
891 aLFD.weight = "book";
892 else
893 aLFD.weight = "medium";
894 break;
895 case FW_DEMIBOLD:
896 aLFD.weight = "demi";
897 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
898 aLFD.weight = "bold";
899 break;
900 case FW_BLACK:
901 aLFD.weight = "black"; break;
902 case FW_LIGHT:
903 aLFD.weight = "light"; break;
904 default:
905 aLFD.weight = any;
908 /* add slant */
909 if( fo->fi->df.dfItalic )
910 if( fo->fi->fi_flags & FI_OBLIQUE )
911 aLFD.slant = "o";
912 else
913 aLFD.slant = "i";
914 else
915 aLFD.slant = (uRelax < 1) ? "r" : any;
917 /* add width */
918 if( fo->fi->fi_flags & FI_NORMAL )
919 aLFD.set_width = "normal";
920 else
921 aLFD.set_width = any;
923 /* ignore style */
924 aLFD.add_style = any;
926 /* add pixelheight
928 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
930 if( fo->fo_flags & FO_SYNTH_HEIGHT )
931 h = fo->fi->lfd_height;
932 else
934 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
935 h /= fo->fi->df.dfPixHeight;
937 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
938 h = MIN_FONT_SIZE;
939 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
941 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
942 h = MAX_FONT_SIZE;
945 if (uRelax <= 2)
946 /* handle rotated fonts */
947 if (fo->lf.lfEscapement) {
948 /* escapement is in tenths of degrees, theta is in radians */
949 double theta = M_PI*fo->lf.lfEscapement/1800.;
950 LFD_AngleMatrix(h_string, h, theta);
952 else
954 sprintf(h_string, "%d", h);
956 else
957 sprintf(h_string, "%d", fo->fi->lfd_height);
959 aLFD.pixel_size = h_string;
960 aLFD.point_size = any;
962 /* resolution_x,y, average width */
963 /* FOX ME - Why do some font servers ignore average width ?
964 * so that you have to mess around with res_y
966 aLFD.average_width = any;
967 if (uRelax <= 3)
969 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
970 aLFD.resolution_x = resx_string;
972 strcpy(resy_string, resx_string);
973 if( uRelax == 0 && text_caps & TC_SF_X_YINDEP )
975 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
977 int resy = 0.5 + fo->fi->lfd_resolution *
978 (fo->fi->df.dfAvgWidth * height) /
979 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
980 sprintf(resy_string, "%d", resy);
982 else
984 /* FIXME - synth width */
987 aLFD.resolution_y = resy_string;
989 else
991 aLFD.resolution_x = aLFD.resolution_y = any;
994 /* spacing */
996 char* w;
998 if( fo->fi->fi_flags & FI_FIXEDPITCH )
999 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
1000 else
1001 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
1003 aLFD.spacing = (uRelax <= 1) ? w : any;
1006 /* encoding */
1008 if (uRelax <= 4)
1010 fontEncodingTemplate* boba = fETTable;
1012 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
1013 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
1015 i = fo->fi->fi_encoding & 255;
1016 switch( i )
1018 default:
1019 aLFD.charset_encoding = boba->sufch[i].psuffix;
1020 break;
1022 case 254:
1023 aLFD.charset_encoding = any;
1024 break;
1026 case 255: /* no suffix - it ends eg "-ascii" */
1027 aLFD.charset_encoding = NULL;
1028 break;
1031 else
1033 aLFD.charset_registry = any;
1034 aLFD.charset_encoding = any;
1037 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1039 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1040 return TRUE;
1044 /***********************************************************************
1045 * X Font Resources
1047 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1048 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1050 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1051 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1053 unsigned long height;
1054 unsigned min = (unsigned char)pFI->dfFirstChar;
1055 unsigned max = (unsigned char)pFI->dfLastChar;
1056 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1058 if( pEL ) *pEL = 0;
1060 if(XFT) {
1061 Atom RAW_CAP_HEIGHT = TSXInternAtom(gdi_display, "RAW_CAP_HEIGHT", TRUE);
1062 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
1063 *pIL = XFT->ascent -
1064 (INT)(XFT->pixelsize / 1000.0 * height);
1065 else
1066 *pIL = 0;
1067 return;
1070 if( TSXGetFontProperty((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;
1088 *pIL = x_fs->ascent - height;
1091 /***********************************************************************
1092 * XFONT_CharWidth
1094 static int XFONT_CharWidth(const XFontStruct* x_fs,
1095 const XFONTTRANS *XFT, int ch)
1097 if(!XFT)
1098 return x_fs->per_char[ch].width;
1099 else
1100 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1103 /***********************************************************************
1104 * XFONT_GetAvgCharWidth
1106 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1107 const XFONTTRANS *XFT)
1109 unsigned min = (unsigned char)pFI->dfFirstChar;
1110 unsigned max = (unsigned char)pFI->dfLastChar;
1112 INT avg;
1114 if( x_fs->per_char )
1116 int width = 0, chars = 0, j;
1117 if( (IS_LATIN_CHARSET(pFI->dfCharSet) ||
1118 pFI->dfCharSet == DEFAULT_CHARSET) &&
1119 (max - min) >= (unsigned char)'z' )
1121 /* FIXME - should use a weighted average */
1122 for( j = 0; j < 26; j++ )
1123 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1124 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1125 chars = 52;
1127 else /* unweighted average of everything */
1129 for( j = 0, max -= min; j <= max; j++ )
1130 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1132 width += XFONT_CharWidth(x_fs, XFT, j);
1133 chars++;
1136 if (chars) avg = (width + (chars-1))/ chars; /* always round up*/
1137 else avg = 0; /* No characters exist at all */
1139 else /* uniform width */
1140 avg = x_fs->min_bounds.width;
1142 TRACE(" retuning %d\n",avg);
1143 return avg;
1146 /***********************************************************************
1147 * XFONT_GetMaxCharWidth
1149 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1151 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1152 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1153 int maxwidth, j;
1155 if(!XFT || !xfs->per_char)
1156 return abs(xfs->max_bounds.width);
1158 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1159 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1160 if(maxwidth < xfs->per_char[j].attributes)
1161 maxwidth = xfs->per_char[j].attributes;
1163 maxwidth *= XFT->pixelsize / 1000.0;
1164 return maxwidth;
1167 /***********************************************************************
1168 * XFONT_SetFontMetric
1170 * INIT ONLY
1172 * Initializes IFONTINFO16.
1174 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1176 unsigned min, max;
1177 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1178 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1180 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1181 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1183 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1184 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1186 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1187 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1188 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1190 if( xfs->min_bounds.width != xfs->max_bounds.width )
1191 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1192 if( fi->fi_flags & FI_SCALABLE )
1194 fi->df.dfType = DEVICE_FONTTYPE;
1195 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1197 else if( fi->fi_flags & FI_TRUETYPE )
1198 fi->df.dfType = TRUETYPE_FONTTYPE;
1199 else
1200 fi->df.dfType = RASTER_FONTTYPE;
1202 fi->df.dfFace = fr->lfFaceName;
1205 /***********************************************************************
1206 * XFONT_GetFontMetric
1208 * Retrieve font metric info (enumeration).
1210 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1211 LPENUMLOGFONTEXW pLF,
1212 NEWTEXTMETRICEXW *pTM )
1214 memset( pLF, 0, sizeof(*pLF) );
1215 memset( pTM, 0, sizeof(*pTM) );
1217 #define plf ((LPLOGFONTW)pLF)
1218 #define ptm ((LPNEWTEXTMETRICW)pTM)
1219 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1220 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1221 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1222 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1223 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1224 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1225 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1227 /* convert pitch values */
1229 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1230 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1232 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1233 plf->lfFaceName, LF_FACESIZE);
1235 /* FIXME: fill in rest of plF values */
1236 strcpyW(pLF->elfFullName, plf->lfFaceName);
1237 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1238 pLF->elfStyle, LF_FACESIZE);
1239 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1240 "Symbol" : "Roman", -1,
1241 pLF->elfScript, LF_FACESIZE);
1243 #undef plf
1245 ptm->tmAscent = pfi->df.dfAscent;
1246 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1247 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1248 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1249 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1250 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1252 ptm->tmFirstChar = pfi->df.dfFirstChar;
1253 ptm->tmLastChar = pfi->df.dfLastChar;
1254 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1255 ptm->tmBreakChar = pfi->df.dfBreakChar;
1257 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1258 debugstr_w(pLF->elfLogFont.lfFaceName),
1259 debugstr_w(pLF->elfFullName));
1261 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1262 /* return font type */
1263 return pfi->df.dfType;
1264 #undef ptm
1268 /***********************************************************************
1269 * XFONT_FixupFlags
1271 * INIT ONLY
1273 * dfPitchAndFamily flags for some common typefaces.
1275 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1277 switch( lfFaceName[0] )
1279 case 'a':
1280 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1281 return FF_SWISS;
1282 break;
1283 case 'h':
1284 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1285 return FF_SWISS;
1286 break;
1287 case 'c':
1288 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1289 return FF_MODERN;
1291 if (!strcasecmp(lfFaceName, "Charter") )
1292 return FF_ROMAN;
1293 break;
1294 case 'p':
1295 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1296 return FF_ROMAN;
1297 break;
1298 case 't':
1299 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1300 return FF_ROMAN;
1301 break;
1302 case 'u':
1303 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1304 return FF_ROMAN;
1305 break;
1306 case 'z':
1307 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1308 return FF_DECORATIVE;
1310 return 0;
1313 /***********************************************************************
1314 * XFONT_SameFoundryAndFamily
1316 * INIT ONLY
1318 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1320 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1321 !strcasecmp( lfd1->family, lfd2->family ) );
1324 /***********************************************************************
1325 * XFONT_InitialCapitals
1327 * INIT ONLY
1329 * Upper case first letters of words & remove multiple spaces
1331 static void XFONT_InitialCapitals(LPSTR lpch)
1333 int i;
1334 BOOL up;
1335 char* lpstr = lpch;
1337 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1339 if( isspace(*lpch) )
1341 if (!up) /* Not already got one */
1343 *lpstr++ = ' ';
1344 up = TRUE;
1347 else if( isalpha(*lpch) && up )
1349 *lpstr++ = toupper(*lpch);
1350 up = FALSE;
1352 else
1354 *lpstr++ = *lpch;
1355 up = FALSE;
1359 /* Remove possible trailing space */
1360 if (up && i > 0)
1361 --lpstr;
1362 *lpstr = '\0';
1366 /***********************************************************************
1367 * XFONT_WindowsNames
1369 * INIT ONLY
1371 * Build generic Windows aliases for X font names.
1373 * -misc-fixed- -> "Fixed"
1374 * -sony-fixed- -> "Sony Fixed", etc...
1376 static void XFONT_WindowsNames(void)
1378 fontResource* fr;
1380 for( fr = fontList; fr ; fr = fr->next )
1382 fontResource* pfr;
1383 char* lpch;
1385 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1387 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1388 if( pfr->fr_flags & FR_NAMESET )
1390 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1391 break;
1394 lpch = fr->lfFaceName;
1395 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1396 /* prepend vendor name */
1397 (pfr==fr) ? "" : fr->resource->foundry,
1398 fr->resource->family);
1399 XFONT_InitialCapitals(fr->lfFaceName);
1401 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1402 if( bFamilyStyle)
1404 fontInfo* fi;
1405 for( fi = fr->fi ; fi ; fi = fi->next )
1406 fi->df.dfPitchAndFamily |= bFamilyStyle;
1410 TRACE("typeface '%s'\n", fr->lfFaceName);
1412 fr->fr_flags |= FR_NAMESET;
1416 /***********************************************************************
1417 * XFONT_LoadDefaultLFD
1419 * Move lfd to the head of fontList to make it more likely to be matched
1421 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1424 fontResource *fr, *pfr;
1425 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1427 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1429 if( fr )
1431 fr->next = pfr->next;
1432 pfr->next = fontList;
1433 fontList = pfr;
1435 break;
1437 fr = pfr;
1439 if (!pfr)
1440 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1441 lfd->foundry, lfd->family);
1445 /***********************************************************************
1446 * XFONT_LoadDefault
1448 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1450 char buffer[MAX_LFD_LENGTH];
1451 HKEY hkey;
1453 buffer[0] = 0;
1454 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1456 DWORD type, count = sizeof(buffer);
1457 RegQueryValueExA(hkey, ini, 0, &type, buffer, &count);
1458 RegCloseKey(hkey);
1460 if (*buffer)
1462 LFD* lfd;
1463 char* pch = buffer;
1464 while( *pch && isspace(*pch) ) pch++;
1466 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1467 lfd = LFD_Parse(pch);
1468 if (lfd && lfd->foundry && lfd->family)
1469 XFONT_LoadDefaultLFD(lfd, fonttype);
1470 else
1471 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1472 HeapFree(GetProcessHeap(), 0, lfd);
1477 /***********************************************************************
1478 * XFONT_LoadDefaults
1480 static void XFONT_LoadDefaults(void)
1482 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1483 XFONT_LoadDefault(INIDefault, "");
1486 /***********************************************************************
1487 * XFONT_CreateAlias
1489 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1491 int j;
1492 fontAlias *pfa, *prev = NULL;
1494 for(pfa = aliasTable; pfa; pfa = pfa->next)
1496 /* check if we already got one */
1497 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1499 TRACE("redundant alias '%s' -> '%s'\n",
1500 lpAlias, lpTypeFace );
1501 return NULL;
1503 prev = pfa;
1506 j = strlen(lpTypeFace) + 1;
1507 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1508 j + strlen(lpAlias) + 1 );
1509 if (pfa)
1511 if (!prev)
1512 aliasTable = pfa;
1513 else
1514 prev->next = pfa;
1516 pfa->next = NULL;
1517 pfa->faTypeFace = (LPSTR)(pfa + 1);
1518 strcpy( pfa->faTypeFace, lpTypeFace );
1519 pfa->faAlias = pfa->faTypeFace + j;
1520 strcpy( pfa->faAlias, lpAlias );
1522 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1524 return pfa;
1526 return NULL;
1530 /***********************************************************************
1531 * XFONT_LoadAlias
1533 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1535 fontResource *fr, *frMatch = NULL;
1536 if (!lfd->foundry || ! lfd->family)
1538 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1539 return;
1541 for (fr = fontList; fr ; fr = fr->next)
1543 if(!strcasecmp(fr->resource->family, lpAlias))
1545 /* alias is not needed since the real font is present */
1546 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1547 return;
1549 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1551 frMatch = fr;
1552 break;
1556 if( frMatch )
1558 if( bSubst )
1560 fontAlias *pfa, *prev = NULL;
1562 for(pfa = aliasTable; pfa; pfa = pfa->next)
1564 /* Remove lpAlias from aliasTable - we should free the old entry */
1565 if(!strcmp(lpAlias, pfa->faAlias))
1567 if(prev)
1568 prev->next = pfa->next;
1569 else
1570 aliasTable = pfa->next;
1573 /* Update any references to the substituted font in aliasTable */
1574 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1576 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1577 strcpy( pfa->faTypeFace, lpAlias );
1579 prev = pfa;
1582 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1584 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1585 frMatch->fr_flags |= FR_NAMESET;
1587 else
1589 /* create new entry in the alias table */
1590 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1593 else
1595 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1599 /***********************************************************************
1600 * Just a copy of PROFILE_GetStringItem
1602 * Convenience function that turns a string 'xxx, yyy, zzz' into
1603 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1605 static char *XFONT_GetStringItem( char *start )
1607 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1608 char *lpchX, *lpch;
1610 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1612 if( *lpchX == ',' )
1614 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1615 while( *(++lpchX) )
1616 if( !XFONT_isspace(*lpchX) ) return lpchX;
1618 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1619 else lpch = NULL;
1621 if( lpch ) *lpch = '\0';
1622 return NULL;
1623 #undef XFONT_isspace
1626 /***********************************************************************
1627 * XFONT_LoadAliases
1629 * INIT ONLY
1631 * Create font aliases for some standard windows fonts using user's
1632 * default choice of (sans-)serif fonts
1634 * Read user-defined aliases from wine.conf. Format is as follows
1636 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1638 * Example:
1639 * Alias0 = Arial, -adobe-helvetica-
1640 * Alias1 = Times New Roman, -bitstream-courier-, 1
1641 * ...
1643 * Note that from 970817 and on we have built-in alias templates that take
1644 * care of the necessary Windows typefaces.
1646 typedef struct
1648 LPSTR fatResource;
1649 LPSTR fatAlias;
1650 } aliasTemplate;
1652 static void XFONT_LoadAliases(void)
1654 char *lpResource;
1655 char buffer[MAX_LFD_LENGTH];
1656 int i = 0;
1657 LFD* lfd;
1658 HKEY hkey;
1660 /* built-ins first */
1661 strcpy(buffer, "-bitstream-charter-");
1662 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1664 DWORD type, count = sizeof(buffer);
1665 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, buffer, &count);
1666 RegCloseKey(hkey);
1668 TRACE("Using '%s' as default serif font\n", buffer);
1669 lfd = LFD_Parse(buffer);
1670 /* NB XFONT_InitialCapitals should not change these standard aliases */
1671 if (lfd)
1673 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1674 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1675 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1677 XFONT_LoadDefaultLFD( lfd, "serif ");
1678 HeapFree(GetProcessHeap(), 0, lfd);
1681 strcpy(buffer, "-adobe-helvetica-");
1682 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1684 DWORD type, count = sizeof(buffer);
1685 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, buffer, &count);
1686 RegCloseKey(hkey);
1688 TRACE("Using '%s' as default sans serif font\n", buffer);
1689 lfd = LFD_Parse(buffer);
1690 if (lfd)
1692 XFONT_LoadAlias( lfd, "Helv", FALSE);
1693 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1694 XFONT_LoadAlias( lfd, "MS Shell Dlg", FALSE);
1695 XFONT_LoadAlias( lfd, "System", FALSE);
1696 XFONT_LoadAlias( lfd, "Arial", FALSE);
1698 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1699 HeapFree(GetProcessHeap(), 0, lfd);
1702 /* then user specified aliases */
1705 BOOL bSubst;
1706 char subsection[32];
1707 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1709 buffer[0] = 0;
1710 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1712 DWORD type, count = sizeof(buffer);
1713 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1714 RegCloseKey(hkey);
1717 if (!buffer[0])
1718 break;
1720 XFONT_InitialCapitals(buffer);
1721 lpResource = XFONT_GetStringItem( buffer );
1722 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1723 if( lpResource && *lpResource )
1725 lfd = LFD_Parse(lpResource);
1726 if (lfd)
1728 XFONT_LoadAlias(lfd, buffer, bSubst);
1729 HeapFree(GetProcessHeap(), 0, lfd);
1732 else
1733 WARN("malformed font alias '%s'\n", buffer );
1735 while(TRUE);
1738 /***********************************************************************
1739 * XFONT_UnAlias
1741 * Convert an (potential) alias into a real windows name
1744 static LPCSTR XFONT_UnAlias(char* font)
1746 if (font[0])
1748 fontAlias* fa;
1749 XFONT_InitialCapitals(font); /* to remove extra white space */
1751 for( fa = aliasTable; fa; fa = fa->next )
1752 /* use case insensitive matching to handle eg "MS Sans Serif" */
1753 if( !strcasecmp( fa->faAlias, font ) )
1755 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1756 strcpy(font, fa->faTypeFace);
1757 return fa->faAlias;
1758 break;
1761 return NULL;
1764 /***********************************************************************
1765 * XFONT_RemoveFontResource
1767 * Caller should check if the font resource is in use. If it is it should
1768 * set FR_REMOVED flag to delay removal until the resource is not in use
1769 * any more.
1771 void XFONT_RemoveFontResource( fontResource** ppfr )
1773 fontResource* pfr = *ppfr;
1774 #if 0
1775 fontInfo* pfi;
1777 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1778 while( pfr->fi )
1780 pfi = pfr->fi->next;
1781 HeapFree( GetProcessHeap(), 0, pfr->fi );
1782 pfr->fi = pfi;
1784 HeapFree( GetProcessHeap(), 0, pfr );
1785 #endif
1786 *ppfr = pfr->next;
1789 /***********************************************************************
1790 * XFONT_LoadIgnores
1792 * INIT ONLY
1794 * Removes specified fonts from the font table to prevent Wine from
1795 * using it.
1797 * Ignore# = [LFD font name]
1799 * Example:
1800 * Ignore0 = -misc-nil-
1801 * Ignore1 = -sun-open look glyph-
1802 * ...
1805 static void XFONT_LoadIgnore(char* lfdname)
1807 fontResource** ppfr;
1809 LFD* lfd = LFD_Parse(lfdname);
1810 if (lfd && lfd->foundry && lfd->family)
1812 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1814 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1816 TRACE("Ignoring '-%s-%s-'\n",
1817 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1819 XFONT_RemoveFontResource( ppfr );
1820 break;
1824 else
1825 WARN("Malformed font resource\n");
1827 HeapFree(GetProcessHeap(), 0, lfd);
1830 static void XFONT_LoadIgnores(void)
1832 int i = 0;
1833 char subsection[32];
1834 char buffer[MAX_LFD_LENGTH];
1836 /* Standard one that noone wants */
1837 strcpy(buffer, "-misc-nil-");
1838 XFONT_LoadIgnore(buffer);
1840 /* Others from INI file */
1843 HKEY hkey;
1844 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1846 buffer[0] = 0;
1847 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1849 DWORD type, count = sizeof(buffer);
1850 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1851 RegCloseKey(hkey);
1854 if( buffer[0] )
1856 char* pch = buffer;
1857 while( *pch && isspace(*pch) ) pch++;
1858 XFONT_LoadIgnore(pch);
1860 else
1861 break;
1862 } while(TRUE);
1866 /***********************************************************************
1867 * XFONT_UserMetricsCache
1869 * Returns expanded name for the cachedmetrics file.
1870 * Now it also appends the current value of the $DISPLAY variable.
1872 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1874 const char *confdir = wine_get_config_dir();
1875 const char *display_name = XDisplayName(NULL);
1876 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1877 int display = 0;
1878 int screen = 0;
1879 char *p, *ext;
1882 ** Normalize the display name, since on Red Hat systems, DISPLAY
1883 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1884 ** after this code, all of the above will resolve to ':0.0'.
1886 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1887 p = strchr(display_name, ':');
1888 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1890 if ((len > *buf_size) &&
1891 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1893 ERR("out of memory\n");
1894 ExitProcess(1);
1896 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1898 ext = buffer + strlen(buffer);
1899 strcpy( ext, display_name );
1901 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1902 sprintf( p, ":%d.%d", display, screen );
1903 return buffer;
1907 /***********************************************************************
1908 * X Font Matching
1910 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1912 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1914 INT m;
1916 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1918 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1919 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1920 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1922 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1923 && fi->lfd_height != match->lfd_height) ||
1924 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1925 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1927 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1928 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1930 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1931 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1932 else if( m >= 0 ) return 1; /* 'match' is better */
1934 return -1; /* 'fi' is better */
1937 /***********************************************************************
1938 * XFONT_CheckFIList
1940 * REMOVE_SUBSETS - attach new fi and purge subsets
1941 * UNMARK_SUBSETS - remove subset flags from all fi entries
1943 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1945 int i = 0;
1946 fontInfo* pfi, *prev;
1948 for( prev = NULL, pfi = fr->fi; pfi; )
1950 if( action == REMOVE_SUBSETS )
1952 if( pfi->fi_flags & FI_SUBSET )
1954 fontInfo* subset = pfi;
1956 i++;
1957 fr->fi_count--;
1958 if( prev ) prev->next = pfi = pfi->next;
1959 else fr->fi = pfi = pfi->next;
1960 HeapFree( GetProcessHeap(), 0, subset );
1961 continue;
1964 else pfi->fi_flags &= ~FI_SUBSET;
1966 prev = pfi;
1967 pfi = pfi->next;
1970 if( action == REMOVE_SUBSETS ) /* also add the superset */
1972 if( fi->fi_flags & FI_SCALABLE )
1974 fi->next = fr->fi;
1975 fr->fi = fi;
1977 else if( prev ) prev->next = fi; else fr->fi = fi;
1978 fr->fi_count++;
1981 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1984 /***********************************************************************
1985 * XFONT_FindFIList
1987 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1989 while( pfr )
1991 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1992 pfr = pfr->next;
1994 /* Give the app back the font name it asked for. Encarta checks this. */
1995 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1996 return pfr;
1999 /***********************************************************************
2000 * XFONT_FixupPointSize
2002 static void XFONT_FixupPointSize(fontInfo* fi)
2004 #define df (fi->df)
2005 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
2006 df.dfPoints = (INT16)
2007 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
2008 df.dfVertRes );
2009 #undef df
2012 /***********************************************************************
2013 * XFONT_BuildMetrics
2015 * Build font metrics from X font
2017 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
2019 int i;
2020 fontInfo* fi = NULL;
2021 fontResource* fr, *pfr;
2022 int n_ff = 0;
2024 MESSAGE("Building font metrics. This may take some time...\n");
2025 for( i = 0; i < x_count; i++ )
2027 char* typeface;
2028 LFD* lfd;
2029 int j;
2030 char buffer[MAX_LFD_LENGTH];
2031 char* lpstr;
2032 XFontStruct* x_fs;
2033 fontInfo* pfi;
2035 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
2036 strcpy( typeface, x_pattern[i] );
2037 if (i % 10 == 0) MESSAGE("Font metrics: %.1f%% done\n", 100.0 * i / x_count);
2039 lfd = LFD_Parse(typeface);
2040 if (!lfd)
2042 HeapFree(GetProcessHeap(), 0, typeface);
2043 continue;
2046 /* find a family to insert into */
2048 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2050 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
2051 break;
2052 pfr = fr;
2055 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2057 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
2058 goto nextfont;
2060 if( !fr ) /* add new family */
2062 n_ff++;
2063 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2064 if (fr)
2066 memset(fr, 0, sizeof(fontResource));
2068 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2069 memset(fr->resource, 0, sizeof(LFD));
2071 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
2072 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->foundry)+1);
2073 strcpy( (char *)fr->resource->foundry, lfd->foundry );
2074 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->family)+1);
2075 strcpy( (char *)fr->resource->family, lfd->family );
2076 fr->resource->weight = "";
2078 if( pfr ) pfr->next = fr;
2079 else fontList = fr;
2081 else
2082 WARN("Not enough memory for a new font family\n");
2085 /* check if we already have something better than "fi" */
2087 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2088 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2089 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2090 if( j > 0 ) goto nextfont;
2092 /* add new font instance "fi" to the "fr" font resource */
2094 if( fi->fi_flags & FI_SCALABLE )
2096 LFD lfd1;
2097 char pxl_string[4], res_string[4];
2098 /* set scalable font height to get an basis for extrapolation */
2100 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2101 fi->lfd_resolution = res;
2103 sprintf(pxl_string, "%d", fi->lfd_height);
2104 sprintf(res_string, "%d", fi->lfd_resolution);
2106 lfd1 = *lfd;
2107 lfd1.pixel_size = pxl_string;
2108 lfd1.point_size = "*";
2109 lfd1.resolution_x = res_string;
2110 lfd1.resolution_y = res_string;
2112 LFD_UnParse(buffer, sizeof buffer, &lfd1);
2114 lpstr = buffer;
2116 else lpstr = x_pattern[i];
2118 if( (x_fs = TSXLoadQueryFont(gdi_display, lpstr)) )
2120 XFONT_SetFontMetric( fi, fr, x_fs );
2121 TSXFreeFont( gdi_display, x_fs );
2123 XFONT_FixupPointSize(fi);
2125 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2127 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2128 fi = NULL; /* preventing reuse */
2130 else
2132 ERR("failed to load %s\n", lpstr );
2134 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2136 nextfont:
2137 HeapFree(GetProcessHeap(), 0, lfd);
2138 HeapFree(GetProcessHeap(), 0, typeface);
2140 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
2142 /* Scan through the font list and remove FontResource(s) (fr)
2143 * that have no associated Fontinfo(s) (fi).
2144 * This code is necessary because XFONT_ReadCachedMetrics
2145 * assumes that there is at least one fi associated with a fr.
2146 * This assumption is invalid for TT font
2147 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2150 fr = fontList;
2152 while (!fr->fi_count)
2154 fontList = fr->next;
2156 HeapFree(GetProcessHeap(), 0, fr->resource);
2157 HeapFree(GetProcessHeap(), 0, fr);
2159 fr = fontList;
2160 n_ff--;
2163 fr = fontList;
2165 while (fr->next)
2167 if (!fr->next->fi_count)
2169 pfr = fr->next;
2170 fr->next = fr->next->next;
2172 HeapFree(GetProcessHeap(), 0, pfr->resource);
2173 HeapFree(GetProcessHeap(), 0, pfr);
2175 n_ff--;
2177 else
2178 fr = fr->next;
2181 MESSAGE("Font metrics: 100.0%% done\n");
2182 return n_ff;
2185 /***********************************************************************
2186 * XFONT_ReadCachedMetrics
2188 * INIT ONLY
2190 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2192 if( fd >= 0 )
2194 unsigned u;
2195 int i, j;
2197 /* read checksums */
2198 read( fd, &u, sizeof(unsigned) );
2199 read( fd, &i, sizeof(int) );
2201 if( u == x_checksum && i == x_count )
2203 off_t length, offset = 3 * sizeof(int);
2205 /* read total size */
2206 read( fd, &i, sizeof(int) );
2207 length = lseek( fd, 0, SEEK_END );
2209 if( length == (i + offset) )
2211 lseek( fd, offset, SEEK_SET );
2212 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2213 if( fontList )
2215 fontResource* pfr = fontList;
2216 fontInfo* pfi = NULL;
2218 TRACE("Reading cached font metrics:\n");
2220 read( fd, fontList, i); /* read all metrics at once */
2221 while( offset < length )
2223 offset += sizeof(fontResource) + sizeof(fontInfo);
2224 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2225 j = 1;
2226 while( TRUE )
2228 if( offset > length ||
2229 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2230 (int)(pfi->next) != j++ )
2232 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);
2233 goto fail;
2236 if( pfi->df.dfPixHeight == 0 )
2238 TRACE("error: dfPixHeight==0\n");
2239 goto fail;
2242 pfi->df.dfFace = pfr->lfFaceName;
2243 if( pfi->fi_flags & FI_SCALABLE )
2245 /* we can pretend we got this font for any resolution */
2246 pfi->lfd_resolution = res;
2247 XFONT_FixupPointSize(pfi);
2249 pfi->next = pfi + 1;
2251 if( j > pfr->fi_count ) break;
2253 pfi = pfi->next;
2254 offset += sizeof(fontInfo);
2256 pfi->next = NULL;
2257 if( pfr->next )
2259 pfr->next = (fontResource*)(pfi + 1);
2260 pfr = pfr->next;
2262 else break;
2264 if( pfr->next == NULL &&
2265 *(int*)(pfi + 1) == X_FMC_MAGIC )
2267 /* read LFD stubs */
2268 char* lpch = (char*)((int*)(pfi + 1) + 1);
2269 offset += sizeof(int);
2270 for( pfr = fontList; pfr; pfr = pfr->next )
2272 size_t len = strlen(lpch) + 1;
2273 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2274 pfr->resource = LFD_Parse(lpch);
2275 lpch += len;
2276 offset += len;
2277 if (offset > length)
2279 TRACE("error: offset=%ld length=%ld\n",(long)offset,(long)length);
2280 goto fail;
2283 close( fd );
2284 return TRUE;
2287 } else {
2288 TRACE("Wrong length: %ld!=%ld\n",(long)length,(long)(i+offset));
2290 } else {
2291 TRACE("Checksum (%x vs. %x) or count (%d vs. %d) mismatch\n",
2292 u,x_checksum,i,x_count);
2294 fail:
2295 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2296 fontList = NULL;
2297 close( fd );
2299 return FALSE;
2302 /***********************************************************************
2303 * XFONT_WriteCachedMetrics
2305 * INIT ONLY
2307 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2309 fontResource* pfr;
2310 fontInfo* pfi;
2312 if( fd >= 0 )
2314 int i, j, k;
2315 char buffer[MAX_LFD_LENGTH];
2317 /* font metrics file:
2319 * +0000 x_checksum
2320 * +0004 x_count
2321 * +0008 total size to load
2322 * +000C prepackaged font metrics
2323 * ...
2324 * +...x X_FMC_MAGIC
2325 * +...x + 4 LFD stubs
2328 write( fd, &x_checksum, sizeof(unsigned) );
2329 write( fd, &x_count, sizeof(int) );
2331 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2333 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2334 i += strlen( buffer) + 1;
2335 j += pfr->fi_count;
2337 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2338 write( fd, &i, sizeof(int) );
2340 TRACE("Writing font cache:\n");
2342 for( pfr = fontList; pfr; pfr = pfr->next )
2344 fontInfo fi;
2346 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2348 i = write( fd, pfr, sizeof(fontResource) );
2349 if( i == sizeof(fontResource) )
2351 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2353 fi = *pfi;
2355 fi.df.dfFace = NULL;
2356 fi.next = (fontInfo*)k; /* loader checks this */
2358 j = write( fd, &fi, sizeof(fi) );
2359 k++;
2361 if( j == sizeof(fontInfo) ) continue;
2363 break;
2365 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2367 i = j = X_FMC_MAGIC;
2368 write( fd, &i, sizeof(int) );
2369 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2371 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2372 i = strlen( buffer ) + 1;
2373 j = write( fd, buffer, i );
2376 close( fd );
2377 return ( i == j );
2379 return FALSE;
2382 /***********************************************************************
2383 * XFONT_GetPointResolution()
2385 * INIT ONLY
2387 * Here we initialize DefResolution which is used in the
2388 * XFONT_Match() penalty function. We also load the point
2389 * resolution value (higher values result in larger fonts).
2391 static int XFONT_GetPointResolution( int *log_pixels_x, int *log_pixels_y )
2393 int i, j, point_resolution, num = 3;
2394 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2395 int best = 0, best_diff = 65536;
2396 HKEY hkey;
2398 point_resolution = 0;
2400 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2402 char buffer[20];
2403 DWORD type, count = sizeof(buffer);
2404 if(!RegQueryValueExA(hkey, INIResolution, 0, &type, buffer, &count))
2405 point_resolution = atoi(buffer);
2406 RegCloseKey(hkey);
2409 if( !point_resolution )
2410 point_resolution = *log_pixels_y;
2411 else
2412 *log_pixels_x = *log_pixels_y = point_resolution;
2415 /* FIXME We can only really guess at a best DefResolution
2416 * - this should be configurable
2418 for( i = best = 0; i < num; i++ )
2420 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2421 if( j < best_diff )
2423 best = i;
2424 best_diff = j;
2427 DefResolution = allowed_xfont_resolutions[best];
2428 return point_resolution;
2432 /***********************************************************************
2433 * XFONT_Match
2435 * Compute the matching score between the logical font and the device font.
2437 * contributions from highest to lowest:
2438 * charset
2439 * fixed pitch
2440 * height
2441 * family flags (only when the facename is not present)
2442 * width
2443 * weight, italics, underlines, strikeouts
2445 * NOTE: you can experiment with different penalty weights to see what happens.
2446 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2448 static UINT XFONT_Match( fontMatch* pfm )
2450 fontInfo* pfi = pfm->pfi; /* device font to match */
2451 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2452 UINT penalty = 0;
2453 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from text_caps */
2454 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2455 int d = 0, height;
2457 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2458 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2459 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2460 (pfi->df.dfItalic) ? "Italic" : "" );
2462 pfm->flags &= FO_MATCH_MASK;
2464 /* Charset */
2465 /* pfm->internal_charset: given(required) charset */
2466 /* pfi->internal_charset: charset of this font */
2467 if (pfi->internal_charset == DEFAULT_CHARSET)
2469 /* special case(unicode font) */
2470 /* priority: unmatched charset < unicode < matched charset */
2471 penalty += 0x50;
2473 else
2475 if( pfm->internal_charset == DEFAULT_CHARSET )
2478 if (pfi->internal_charset != ANSI_CHARSET)
2479 penalty += 0x200;
2481 if ( pfi->codepage != GetACP() )
2482 penalty += 0x200;
2484 else if (pfm->internal_charset != pfi->internal_charset)
2486 if ( pfi->internal_charset & 0xff00 )
2487 penalty += 0x1000; /* internal charset - should not be used */
2488 else
2489 penalty += 0x200;
2493 /* Height */
2494 height = -1;
2496 if( plf->lfHeight > 0 )
2498 int h = pfi->df.dfPixHeight;
2499 d = h - plf->lfHeight;
2500 height = plf->lfHeight;
2502 else
2504 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2505 if (h)
2507 d = h + plf->lfHeight;
2508 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2510 else
2512 ERR("PixHeight == InternalLeading\n");
2513 penalty += 0x1000; /* dont want this */
2518 if( height == 0 )
2519 pfm->height = 1; /* Very small */
2520 else if( d )
2522 if( bScale )
2523 pfm->height = height;
2524 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2526 if( d > 0 ) /* do not shrink raster fonts */
2528 pfm->height = pfi->df.dfPixHeight;
2529 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2531 else /* expand only in integer multiples */
2533 pfm->height = height - height%pfi->df.dfPixHeight;
2534 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2537 else /* can't be scaled at all */
2539 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2540 pfm->height = pfi->df.dfPixHeight;
2541 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2544 else
2545 pfm->height = pfi->df.dfPixHeight;
2547 /* Pitch and Family */
2548 if( pfm->flags & FO_MATCH_PAF ) {
2549 int family = plf->lfPitchAndFamily & FF_FAMILY;
2551 /* TMPF_FIXED_PITCH means exactly the opposite */
2552 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2553 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2554 } else /* Variable is the default */
2555 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2557 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2558 penalty += 0x10;
2561 /* Width */
2562 if( plf->lfWidth )
2564 int h;
2565 if( bR6 || bScale ) h = 0;
2566 else
2568 /* FIXME: not complete */
2570 pfm->flags |= FO_SYNTH_WIDTH;
2571 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2573 penalty += h * ( d ) ? 0x2 : 0x1 ;
2575 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2577 /* Weight */
2578 if( plf->lfWeight != FW_DONTCARE )
2580 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2581 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2582 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2584 /* Italic */
2585 if( plf->lfItalic != pfi->df.dfItalic )
2587 penalty += 0x4;
2588 pfm->flags |= FO_SYNTH_ITALIC;
2590 /* Underline */
2591 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2593 /* Strikeout */
2594 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2597 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2598 penalty++;
2600 TRACE(" returning %i\n", penalty );
2602 return penalty;
2605 /***********************************************************************
2606 * XFONT_MatchFIList
2608 * Scan a particular font resource for the best match.
2610 static UINT XFONT_MatchFIList( fontMatch* pfm )
2612 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2613 UINT current_score, score = (UINT)(-1);
2614 fontMatch fm = *pfm;
2616 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2618 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2619 continue;
2621 current_score = XFONT_Match( &fm );
2622 if( score > current_score )
2624 *pfm = fm;
2625 score = current_score;
2628 return score;
2631 /***********************************************************************
2632 * XFONT_is_ansi_charset
2634 * returns TRUE if the given charset is ANSI_CHARSET.
2636 static BOOL XFONT_is_ansi_charset( UINT charset )
2638 return ((charset == ANSI_CHARSET) ||
2639 (charset == DEFAULT_CHARSET && GetACP() == 1252));
2642 /***********************************************************************
2643 * XFONT_MatchDeviceFont
2645 * Scan font resource tree.
2648 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2650 fontMatch fm = *pfm;
2651 UINT current_score, score = (UINT)(-1);
2652 fontResource** ppfr;
2654 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2655 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2656 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2658 pfm->pfi = NULL;
2660 /* the following hard-coded font binding assumes 'ANSI_CHARSET'. */
2661 if( !fm.plf->lfFaceName[0] &&
2662 XFONT_is_ansi_charset(fm.plf->lfCharSet) )
2664 switch(fm.plf->lfPitchAndFamily & 0xf0)
2666 case FF_MODERN:
2667 strcpy(fm.plf->lfFaceName, "Courier New");
2668 break;
2669 case FF_ROMAN:
2670 strcpy(fm.plf->lfFaceName, "Times New Roman");
2671 break;
2672 case FF_SWISS:
2673 strcpy(fm.plf->lfFaceName, "Arial");
2674 break;
2675 default:
2676 if((fm.plf->lfPitchAndFamily & 0x0f) == FIXED_PITCH)
2677 strcpy(fm.plf->lfFaceName, "Courier New");
2678 else
2679 strcpy(fm.plf->lfFaceName, "Arial");
2680 break;
2684 if( fm.plf->lfFaceName[0] )
2686 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2687 if( fm.pfr ) /* match family */
2689 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2691 if( fm.pfr->fr_flags & FR_REMOVED )
2692 fm.pfr = 0;
2693 else
2695 XFONT_MatchFIList( &fm );
2696 *pfm = fm;
2697 if (pfm->pfi)
2698 return;
2702 /* get charset if lfFaceName is one of known facenames. */
2704 const struct CharsetBindingInfo* pcharsetbindings;
2706 pcharsetbindings = &charsetbindings[0];
2707 while ( pcharsetbindings->pszFaceName != NULL )
2709 if ( !strcmp( pcharsetbindings->pszFaceName,
2710 fm.plf->lfFaceName ) )
2712 fm.internal_charset = pcharsetbindings->charset;
2713 break;
2715 pcharsetbindings ++;
2717 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2721 /* match all available fonts */
2723 fm.flags |= FO_MATCH_PAF;
2724 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2726 if( (*ppfr)->fr_flags & FR_REMOVED )
2728 if( (*ppfr)->fo_count == 0 )
2729 XFONT_RemoveFontResource( ppfr );
2730 continue;
2733 fm.pfr = *ppfr;
2735 TRACE("%s\n", fm.pfr->lfFaceName );
2737 current_score = XFONT_MatchFIList( &fm );
2738 if( current_score < score )
2740 score = current_score;
2741 *pfm = fm;
2747 /***********************************************************************
2748 * X Font Cache
2750 static void XFONT_GrowFreeList(int start, int end)
2752 /* add all entries from 'start' up to and including 'end' */
2754 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2756 fontCache[end].lru = fontLF;
2757 fontCache[end].count = -1;
2758 fontLF = start;
2759 while( start < end )
2761 fontCache[start].count = -1;
2762 fontCache[start].lru = start + 1;
2763 start++;
2767 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2769 UINT16 cs = __lfCheckSum( plf );
2770 int i = fontMRU, prev = -1;
2772 *checksum = cs;
2773 while( i >= 0 )
2775 if( fontCache[i].lfchecksum == cs &&
2776 !(fontCache[i].fo_flags & FO_REMOVED) )
2778 /* FIXME: something more intelligent here ? */
2780 if( !memcmp( plf, &fontCache[i].lf,
2781 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2782 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2784 /* remove temporarily from the lru list */
2785 if( prev >= 0 )
2786 fontCache[prev].lru = fontCache[i].lru;
2787 else
2788 fontMRU = (INT16)fontCache[i].lru;
2789 return (fontCache + i);
2792 prev = i;
2793 i = (INT16)fontCache[i].lru;
2795 return NULL;
2798 static fontObject* XFONT_GetCacheEntry(void)
2800 int i;
2802 if( fontLF == -1 )
2804 int prev_i, prev_j, j;
2806 TRACE("font cache is full\n");
2808 /* lookup the least recently used font */
2810 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2812 if( fontCache[i].count <= 0 &&
2813 !(fontCache[i].fo_flags & FO_SYSTEM) )
2815 prev_j = prev_i;
2816 j = i;
2818 prev_i = i;
2821 if( j >= 0 ) /* unload font */
2823 /* detach from the lru list */
2825 TRACE("\tfreeing entry %i\n", j );
2827 fontCache[j].fr->fo_count--;
2829 if( prev_j >= 0 )
2830 fontCache[prev_j].lru = fontCache[j].lru;
2831 else fontMRU = (INT16)fontCache[j].lru;
2833 /* FIXME: lpXForm, lpPixmap */
2834 if(fontCache[j].lpX11Trans)
2835 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2837 TSXFreeFont( gdi_display, fontCache[j].fs );
2839 memset( fontCache + j, 0, sizeof(fontObject) );
2840 return (fontCache + j);
2842 else /* expand cache */
2844 fontObject* newCache;
2846 prev_i = fontCacheSize + FONTCACHE;
2848 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2850 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2851 fontCache, prev_i)) )
2853 i = fontCacheSize;
2854 fontCacheSize = prev_i;
2855 fontCache = newCache;
2856 XFONT_GrowFreeList( i, fontCacheSize - 1);
2858 else return NULL;
2862 /* detach from the free list */
2864 i = fontLF;
2865 fontLF = (INT16)fontCache[i].lru;
2866 fontCache[i].count = 0;
2867 return (fontCache + i);
2870 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2872 UINT u = (UINT)(pfo - fontCache);
2873 int i;
2874 int ret;
2876 if( u < fontCacheSize )
2878 ret = --fontCache[u].count;
2879 if ( ret == 0 )
2881 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2883 if( CHECK_PFONT(pfo->prefobjs[i]) )
2884 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2888 return ret;
2891 return -1;
2894 /***********************************************************************
2895 * X11DRV_FONT_InitX11Metrics
2897 * Initialize font resource list and allocate font cache.
2899 void X11DRV_FONT_InitX11Metrics( void )
2901 char** x_pattern;
2902 unsigned x_checksum;
2903 int i, x_count, fd, buf_size;
2904 char *buffer;
2905 HKEY hkey;
2908 x_pattern = TSXListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2910 TRACE("Font Mapper: initializing %i x11 fonts\n", x_count);
2911 if (x_count == MAX_FONTS)
2912 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2914 for( i = x_checksum = 0; i < x_count; i++ )
2916 int j;
2917 #if 0
2918 printf("%i\t: %s\n", i, x_pattern[i] );
2919 #endif
2921 j = strlen( x_pattern[i] );
2922 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2924 x_checksum |= X_PFONT_MAGIC;
2925 buf_size = 128;
2926 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2928 /* deal with systemwide font metrics cache */
2930 buffer[0] = 0;
2931 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2933 DWORD type, count = buf_size;
2934 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, buffer, &count);
2935 RegCloseKey(hkey);
2938 if( buffer[0] )
2940 fd = open( buffer, O_RDONLY );
2941 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2943 if (fontList == NULL)
2945 /* try per-user */
2946 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2947 if( buffer[0] )
2949 fd = open( buffer, O_RDONLY );
2950 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2954 if( fontList == NULL ) /* build metrics from scratch */
2956 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2957 if( buffer[0] ) /* update cached metrics */
2959 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2960 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2962 WARN("Unable to write to fontcache '%s'\n", buffer);
2963 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2968 TSXFreeFontNames(x_pattern);
2970 /* check if we're dealing with X11 R6 server */
2972 XFontStruct* x_fs;
2973 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2974 if( (x_fs = TSXLoadQueryFont(gdi_display, buffer)) )
2976 text_caps |= TC_SF_X_YINDEP;
2977 TSXFreeFont(gdi_display, x_fs);
2980 HeapFree(GetProcessHeap(), 0, buffer);
2982 XFONT_WindowsNames();
2983 XFONT_LoadAliases();
2984 XFONT_LoadDefaults();
2985 XFONT_LoadIgnores();
2987 /* fontList initialization is over, allocate X font cache */
2989 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2990 XFONT_GrowFreeList(0, fontCacheSize - 1);
2992 TRACE("done!\n");
2994 /* update text caps parameter */
2996 RAW_ASCENT = TSXInternAtom(gdi_display, "RAW_ASCENT", TRUE);
2997 RAW_DESCENT = TSXInternAtom(gdi_display, "RAW_DESCENT", TRUE);
2998 return;
3001 /***********************************************************************
3002 * X11DRV_FONT_Init
3004 void X11DRV_FONT_Init( int *log_pixels_x, int *log_pixels_y )
3006 XFONT_GetPointResolution( log_pixels_x, log_pixels_y );
3008 if(using_client_side_fonts)
3009 text_caps |= TC_VA_ABLE;
3011 return;
3014 /**********************************************************************
3015 * XFONT_SetX11Trans
3017 static BOOL XFONT_SetX11Trans( fontObject *pfo )
3019 char *fontName;
3020 Atom nameAtom;
3021 LFD* lfd;
3023 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
3024 fontName = TSXGetAtomName( gdi_display, nameAtom );
3025 lfd = LFD_Parse(fontName);
3026 if (!lfd)
3028 TSXFree(fontName);
3029 return FALSE;
3032 if (lfd->pixel_size[0] != '[') {
3033 HeapFree(GetProcessHeap(), 0, lfd);
3034 TSXFree(fontName);
3035 return FALSE;
3038 #define PX pfo->lpX11Trans
3040 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
3041 TSXFree(fontName);
3042 HeapFree(GetProcessHeap(), 0, lfd);
3044 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
3045 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
3047 PX->pixelsize = hypot(PX->a, PX->b);
3048 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
3049 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
3051 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
3052 PX->a, PX->b, PX->c, PX->d,
3053 PX->RAW_ASCENT, PX->RAW_DESCENT);
3055 #undef PX
3056 return TRUE;
3059 /***********************************************************************
3060 * X Device Font Objects
3062 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
3063 LPCSTR* faceMatched, BOOL bSubFont,
3064 WORD internal_charset,
3065 WORD* pcharsetMatched )
3067 UINT16 checksum;
3068 INT index = 0;
3069 fontObject* pfo;
3071 pfo = XFONT_LookupCachedFont( plf, &checksum );
3072 if( !pfo )
3074 fontMatch fm;
3075 INT i;
3077 fm.pfr = NULL;
3078 fm.pfi = NULL;
3079 fm.height = 0;
3080 fm.flags = 0;
3081 fm.plf = plf;
3082 fm.internal_charset = internal_charset;
3084 if( text_caps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
3086 /* allocate new font cache entry */
3088 if( (pfo = XFONT_GetCacheEntry()) )
3090 /* initialize entry and load font */
3091 char lpLFD[MAX_LFD_LENGTH];
3092 UINT uRelaxLevel = 0;
3094 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
3095 ERR(
3096 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
3097 plf->lfHeight);
3098 pfo->rescale = fabs(plf->lfHeight / 100.0);
3099 if(plf->lfHeight > 0) plf->lfHeight = 100;
3100 else plf->lfHeight = -100;
3101 } else
3102 pfo->rescale = 1.0;
3104 XFONT_MatchDeviceFont( fontList, &fm );
3105 pfo->fr = fm.pfr;
3106 pfo->fi = fm.pfi;
3107 pfo->fr->fo_count++;
3108 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3110 pfo->lf = *plf;
3111 pfo->lfchecksum = checksum;
3115 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3116 if( (pfo->fs = TSXLoadQueryFont( gdi_display, lpLFD )) ) break;
3117 } while( uRelaxLevel );
3120 if(pfo->lf.lfEscapement != 0) {
3121 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3122 if(!XFONT_SetX11Trans( pfo )) {
3123 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3124 pfo->lpX11Trans = NULL;
3127 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3128 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3129 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3130 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3132 /* FIXME: If we've got a soft font or
3133 * there are FO_SYNTH_... flags for the
3134 * non PROOF_QUALITY request, the engine
3135 * should rasterize characters into mono
3136 * pixmaps and store them in the pfo->lpPixmap
3137 * array (pfo->fs should be updated as well).
3138 * array (pfo->fs should be updated as well).
3139 * X11DRV_ExtTextOut() must be heavily modified
3140 * to support pixmap blitting and FO_SYNTH_...
3141 * styles.
3144 pfo->lpPixmap = NULL;
3146 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3147 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3149 /* special treatment for DBCS that needs multiple fonts */
3150 /* All member of pfo must be set correctly. */
3151 if ( bSubFont == FALSE )
3153 WORD charset_sub;
3154 WORD charsetMatchedSub;
3155 LOGFONT16 lfSub;
3156 LPCSTR faceMatchedSub;
3158 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3160 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3161 penum_subfont_charset( i );
3162 if ( charset_sub == DEFAULT_CHARSET ) break;
3164 lfSub = *plf;
3165 lfSub.lfWidth = 0;
3166 lfSub.lfHeight=plf->lfHeight;
3167 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3168 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3169 /* this font has sub font */
3170 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3171 pfo->prefobjs[i] =
3172 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3173 TRUE, charset_sub,
3174 &charsetMatchedSub );
3175 /* FIXME: check charsetMatchedSub */
3180 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3182 UINT current_score, score = (UINT)(-1);
3184 i = index = fontMRU;
3185 fm.flags |= FO_MATCH_PAF;
3188 pfo = fontCache + i;
3189 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3191 current_score = XFONT_Match( &fm );
3192 if( current_score < score ) index = i;
3194 i = pfo->lru;
3195 } while( i >= 0 );
3196 pfo = fontCache + index;
3197 goto END;
3201 /* attach at the head of the lru list */
3202 pfo->lru = fontMRU;
3203 index = fontMRU = (pfo - fontCache);
3205 END:
3206 pfo->count++;
3208 TRACE("physfont %i\n", index);
3209 *faceMatched = pfo->fi->df.dfFace;
3210 *pcharsetMatched = pfo->fi->internal_charset;
3212 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3215 /***********************************************************************
3216 * XFONT_GetFontObject
3218 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3220 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3221 return NULL;
3224 /***********************************************************************
3225 * XFONT_GetFontStruct
3227 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3229 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3230 return NULL;
3233 /***********************************************************************
3234 * XFONT_GetFontInfo
3236 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3238 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3239 return NULL;
3244 /* X11DRV Interface ****************************************************
3246 * Exposed via the dc->funcs dispatch table. *
3248 ***********************************************************************/
3249 /***********************************************************************
3250 * SelectFont (X11DRV.@)
3252 HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont )
3254 LOGFONTW logfont;
3255 LOGFONT16 lf;
3256 DC *dc = physDev->dc;
3258 TRACE("dc=%p, hfont=%p\n", dc, hfont);
3260 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return HGDI_ERROR;
3262 TRACE("dc->gdiFont = %p\n", dc->gdiFont);
3264 if(dc->gdiFont && using_client_side_fonts) {
3265 X11DRV_XRender_SelectFont(physDev, hfont);
3266 return FALSE;
3269 EnterCriticalSection( &crtsc_fonts_X11 );
3271 if(fontList == NULL) X11DRV_FONT_InitX11Metrics();
3273 if( CHECK_PFONT(physDev->font) )
3274 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3276 FONT_LogFontWTo16(&logfont, &lf);
3278 /* stock fonts ignore the mapping mode */
3279 if (!is_stock_font( hfont ))
3281 /* Make sure we don't change the sign when converting to device coords */
3282 /* FIXME - check that the other drivers do this correctly */
3283 if (lf.lfWidth)
3285 lf.lfWidth = GDI_ROUND((FLOAT)lf.lfWidth * fabs(dc->xformWorld2Vport.eM11));
3286 if (lf.lfWidth == 0)
3287 lf.lfWidth = 1; /* Minimum width */
3289 if (lf.lfHeight)
3291 lf.lfHeight = GDI_ROUND((FLOAT)lf.lfHeight * fabs(dc->xformWorld2Vport.eM22));
3293 if (lf.lfHeight == 0)
3294 lf.lfHeight = MIN_FONT_SIZE;
3298 if (!lf.lfHeight)
3299 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(dc->hSelf,LOGPIXELSY) + (72>>1)) / 72;
3302 /* Fixup aliases before passing to RealizeFont */
3303 /* alias = Windows name in the alias table */
3304 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3305 LPCSTR faceMatched;
3306 WORD charsetMatched;
3308 TRACE("hfont=%p\n", hfont); /* to connect with the trace from RealizeFont */
3309 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3310 FALSE, lf.lfCharSet,
3311 &charsetMatched );
3313 /* set face to the requested facename if it matched
3314 * so that GetTextFace can get the correct face name
3316 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3317 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3318 logfont.lfFaceName, LF_FACESIZE);
3319 else
3320 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3321 logfont.lfFaceName, LF_FACESIZE);
3324 * In X, some encodings may have the same lfFaceName.
3325 * for example:
3326 * -misc-fixed-*-iso8859-1
3327 * -misc-fixed-*-jisx0208.1990-0
3328 * so charset should be saved...
3330 logfont.lfCharSet = charsetMatched;
3333 LeaveCriticalSection( &crtsc_fonts_X11 );
3335 return (HFONT)1; /* Use a device font */
3339 /***********************************************************************
3341 * X11DRV_EnumDeviceFonts
3343 BOOL X11DRV_EnumDeviceFonts( X11DRV_PDEVICE *physDev, LPLOGFONTW plf,
3344 DEVICEFONTENUMPROC proc, LPARAM lp )
3346 ENUMLOGFONTEXW lf;
3347 NEWTEXTMETRICEXW tm;
3348 fontResource* pfr = fontList;
3349 BOOL b, bRet = 0;
3351 /* don't enumerate x11 fonts if we're using client side fonts */
3352 if (physDev->dc->gdiFont) return FALSE;
3354 if( plf->lfFaceName[0] )
3356 char facename[LF_FACESIZE+1];
3357 WideCharToMultiByte( CP_ACP, 0, plf->lfFaceName, -1,
3358 facename, sizeof(facename), NULL, NULL );
3359 /* enum all entries in this resource */
3360 pfr = XFONT_FindFIList( pfr, facename );
3361 if( pfr )
3363 fontInfo* pfi;
3364 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3366 /* Note: XFONT_GetFontMetric() will have to
3367 release the crit section, font list will
3368 have to be retraversed on return */
3370 if(plf->lfCharSet == DEFAULT_CHARSET ||
3371 plf->lfCharSet == pfi->df.dfCharSet) {
3372 if( (b = (*proc)( &lf, &tm,
3373 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3374 bRet = b;
3375 else break;
3380 else /* enum first entry in each resource */
3381 for( ; pfr ; pfr = pfr->next )
3383 if(pfr->fi)
3385 if( (b = (*proc)( &lf, &tm,
3386 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3387 bRet = b;
3388 else break;
3391 return bRet;
3395 /***********************************************************************
3396 * X11DRV_GetTextMetrics
3398 BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
3400 if( CHECK_PFONT(physDev->font) )
3402 fontObject* pfo = __PFONT(physDev->font);
3403 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsW( pfo, metrics );
3404 return TRUE;
3406 return FALSE;
3410 /***********************************************************************
3411 * X11DRV_GetCharWidth
3413 BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
3414 LPINT buffer )
3416 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3418 if( pfo )
3420 int i;
3422 if (pfo->fs->per_char == NULL)
3423 for (i = firstChar; i <= lastChar; i++)
3424 if(pfo->lpX11Trans)
3425 *buffer++ = pfo->fs->min_bounds.attributes *
3426 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3427 else
3428 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3429 else
3431 XCharStruct *cs, *def;
3432 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3434 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3435 def);
3437 for (i = firstChar; i <= lastChar; i++)
3439 WCHAR wch = i;
3440 BYTE ch;
3441 UINT ch_f; /* character code in the font encoding */
3442 WideCharToMultiByte( pfo->fi->codepage, 0, &wch, 1, &ch, 1, NULL, NULL );
3443 ch_f = ch;
3444 if (ch_f >= pfo->fs->min_char_or_byte2 &&
3445 ch_f <= pfo->fs->max_char_or_byte2)
3447 cs = &pfo->fs->per_char[(ch_f - pfo->fs->min_char_or_byte2)];
3448 if (CI_NONEXISTCHAR(cs)) cs = def;
3449 } else cs = def;
3450 if(pfo->lpX11Trans)
3451 *buffer++ = max(cs->attributes, 0) *
3452 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3453 else
3454 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3458 return TRUE;
3460 return FALSE;