user32/tests: Test pending redraw state with owner-drawn list box.
[wine.git] / dlls / gdi32 / font.c
blob7e087d68db30d6e144dab41305900fe3fcb85e20
1 /*
2 * GDI font objects
4 * Copyright 1993 Alexandre Julliard
5 * 1997 Alex Korobka
6 * Copyright 2002,2003 Shachar Shemesh
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <limits.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include "winerror.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winternl.h"
33 #include "winreg.h"
34 #include "ntgdi_private.h"
35 #include "resource.h"
36 #include "wine/exception.h"
37 #include "wine/heap.h"
38 #include "wine/rbtree.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(font);
43 static HKEY wine_fonts_key;
44 static HKEY wine_fonts_cache_key;
46 struct font_physdev
48 struct gdi_physdev dev;
49 struct gdi_font *font;
52 static inline struct font_physdev *get_font_dev( PHYSDEV dev )
54 return (struct font_physdev *)dev;
57 struct gdi_font_family
59 struct wine_rb_entry name_entry;
60 struct wine_rb_entry second_name_entry;
61 unsigned int refcount;
62 WCHAR family_name[LF_FACESIZE];
63 WCHAR second_name[LF_FACESIZE];
64 struct list faces;
65 struct gdi_font_family *replacement;
68 struct gdi_font_face
70 struct list entry;
71 unsigned int refcount;
72 WCHAR *style_name;
73 WCHAR *full_name;
74 WCHAR *file;
75 void *data_ptr;
76 SIZE_T data_size;
77 UINT face_index;
78 FONTSIGNATURE fs;
79 DWORD ntmFlags;
80 DWORD version;
81 DWORD flags; /* ADDFONT flags */
82 BOOL scalable;
83 struct bitmap_font_size size; /* set if face is a bitmap */
84 struct gdi_font_family *family;
85 struct gdi_font_enum_data *cached_enum_data;
86 struct wine_rb_entry full_name_entry;
89 static const struct font_backend_funcs *font_funcs;
91 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
93 static UINT font_smoothing = GGO_BITMAP;
94 static UINT subpixel_orientation = GGO_GRAY4_BITMAP;
95 static BOOL antialias_fakes = TRUE;
96 static struct font_gamma_ramp font_gamma_ramp;
98 static void add_face_to_cache( struct gdi_font_face *face );
99 static void remove_face_from_cache( struct gdi_font_face *face );
101 static inline WCHAR facename_tolower( WCHAR c )
103 if (c >= 'A' && c <= 'Z') return c - 'A' + 'a';
104 else if (c > 127) return RtlDowncaseUnicodeChar( c );
105 else return c;
108 static inline int facename_compare( const WCHAR *str1, const WCHAR *str2, SIZE_T len )
110 while (len--)
112 WCHAR c1 = facename_tolower( *str1++ ), c2 = facename_tolower( *str2++ );
113 if (c1 != c2) return c1 - c2;
114 else if (!c1) return 0;
116 return 0;
119 /* Device -> World size conversion */
121 /* Performs a device to world transformation on the specified width (which
122 * is in integer format).
124 static inline INT INTERNAL_XDSTOWS(DC *dc, INT width)
126 double floatWidth;
128 /* Perform operation with floating point */
129 floatWidth = (double)width * dc->xformVport2World.eM11;
130 /* Round to integers */
131 return GDI_ROUND(floatWidth);
134 /* Performs a device to world transformation on the specified size (which
135 * is in integer format).
137 static inline INT INTERNAL_YDSTOWS(DC *dc, INT height)
139 double floatHeight;
141 /* Perform operation with floating point */
142 floatHeight = (double)height * dc->xformVport2World.eM22;
143 /* Round to integers */
144 return GDI_ROUND(floatHeight);
147 /* scale width and height but don't mirror them */
149 static inline INT width_to_LP( DC *dc, INT width )
151 return GDI_ROUND( (double)width * fabs( dc->xformVport2World.eM11 ));
154 static inline INT height_to_LP( DC *dc, INT height )
156 return GDI_ROUND( (double)height * fabs( dc->xformVport2World.eM22 ));
159 static inline INT INTERNAL_YWSTODS(DC *dc, INT height)
161 POINT pt[2];
162 pt[0].x = pt[0].y = 0;
163 pt[1].x = 0;
164 pt[1].y = height;
165 lp_to_dp(dc, pt, 2);
166 return pt[1].y - pt[0].y;
169 static inline BOOL is_win9x(void)
171 return GetVersion() & 0x80000000;
174 static inline WCHAR *strdupW( const WCHAR *p )
176 WCHAR *ret;
177 DWORD len = (lstrlenW(p) + 1) * sizeof(WCHAR);
178 ret = HeapAlloc(GetProcessHeap(), 0, len);
179 memcpy(ret, p, len);
180 return ret;
183 static INT FONT_GetObjectW( HGDIOBJ handle, INT count, LPVOID buffer );
184 static BOOL FONT_DeleteObject( HGDIOBJ handle );
186 static const struct gdi_obj_funcs fontobj_funcs =
188 FONT_GetObjectW, /* pGetObjectW */
189 NULL, /* pUnrealizeObject */
190 FONT_DeleteObject /* pDeleteObject */
193 typedef struct
195 struct gdi_obj_header obj;
196 LOGFONTW logfont;
197 } FONTOBJ;
199 struct font_enum
201 LPLOGFONTW lpLogFontParam;
202 FONTENUMPROCW lpEnumFunc;
203 LPARAM lpData;
204 BOOL unicode;
205 HDC hdc;
206 INT retval;
210 * For TranslateCharsetInfo
212 #define MAXTCIINDEX 32
213 static const CHARSETINFO FONT_tci[MAXTCIINDEX] = {
214 /* ANSI */
215 { ANSI_CHARSET, 1252, {{0,0,0,0},{FS_LATIN1,0}} },
216 { EASTEUROPE_CHARSET, 1250, {{0,0,0,0},{FS_LATIN2,0}} },
217 { RUSSIAN_CHARSET, 1251, {{0,0,0,0},{FS_CYRILLIC,0}} },
218 { GREEK_CHARSET, 1253, {{0,0,0,0},{FS_GREEK,0}} },
219 { TURKISH_CHARSET, 1254, {{0,0,0,0},{FS_TURKISH,0}} },
220 { HEBREW_CHARSET, 1255, {{0,0,0,0},{FS_HEBREW,0}} },
221 { ARABIC_CHARSET, 1256, {{0,0,0,0},{FS_ARABIC,0}} },
222 { BALTIC_CHARSET, 1257, {{0,0,0,0},{FS_BALTIC,0}} },
223 { VIETNAMESE_CHARSET, 1258, {{0,0,0,0},{FS_VIETNAMESE,0}} },
224 /* reserved by ANSI */
225 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
226 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
227 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
228 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
229 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
230 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
231 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
232 /* ANSI and OEM */
233 { THAI_CHARSET, 874, {{0,0,0,0},{FS_THAI,0}} },
234 { SHIFTJIS_CHARSET, 932, {{0,0,0,0},{FS_JISJAPAN,0}} },
235 { GB2312_CHARSET, 936, {{0,0,0,0},{FS_CHINESESIMP,0}} },
236 { HANGEUL_CHARSET, 949, {{0,0,0,0},{FS_WANSUNG,0}} },
237 { CHINESEBIG5_CHARSET, 950, {{0,0,0,0},{FS_CHINESETRAD,0}} },
238 { JOHAB_CHARSET, 1361, {{0,0,0,0},{FS_JOHAB,0}} },
239 /* reserved for alternate ANSI and OEM */
240 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
241 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
242 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
243 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
244 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
245 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
246 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
247 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
248 /* reserved for system */
249 { DEFAULT_CHARSET, 0, {{0,0,0,0},{FS_LATIN1,0}} },
250 { SYMBOL_CHARSET, CP_SYMBOL, {{0,0,0,0},{FS_SYMBOL,0}} }
253 static const WCHAR * const default_serif_list[3] =
255 L"Times New Roman",
256 L"Liberation Serif",
257 L"Bitstream Vera Serif"
259 static const WCHAR * const default_fixed_list[3] =
261 L"Courier New",
262 L"Liberation Mono",
263 L"Bitstream Vera Sans Mono"
265 static const WCHAR * const default_sans_list[3] =
267 L"Arial",
268 L"Liberation Sans",
269 L"Bitstream Vera Sans"
271 static WCHAR ff_roman_default[LF_FACESIZE];
272 static WCHAR ff_modern_default[LF_FACESIZE];
273 static WCHAR ff_swiss_default[LF_FACESIZE];
275 static const struct nls_update_font_list
277 UINT ansi_cp, oem_cp;
278 const char *oem, *fixed, *system;
279 const char *courier, *serif, *small, *sserif_96, *sserif_120;
280 /* these are for font substitutes */
281 const char *shelldlg, *tmsrmn;
282 const char *fixed_0, *system_0, *courier_0, *serif_0, *small_0, *sserif_0, *helv_0, *tmsrmn_0;
283 struct subst { const char *from, *to; } arial_0, courier_new_0, times_new_roman_0;
284 } nls_update_font_list[] =
286 /* Latin 1 (United States) */
287 { 1252, 437, "vgaoem.fon", "vgafix.fon", "vgasys.fon",
288 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
289 "Tahoma","Times New Roman"
291 /* Latin 1 (Multilingual) */
292 { 1252, 850, "vga850.fon", "vgafix.fon", "vgasys.fon",
293 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
294 "Tahoma","Times New Roman" /* FIXME unverified */
296 /* Eastern Europe */
297 { 1250, 852, "vga852.fon", "vgafixe.fon", "vgasyse.fon",
298 "couree.fon", "serifee.fon", "smallee.fon", "sserifee.fon", "sseriffe.fon",
299 "Tahoma","Times New Roman", /* FIXME unverified */
300 "Fixedsys,238", "System,238",
301 "Courier New,238", "MS Serif,238", "Small Fonts,238",
302 "MS Sans Serif,238", "MS Sans Serif,238", "MS Serif,238",
303 { "Arial CE,0", "Arial,238" },
304 { "Courier New CE,0", "Courier New,238" },
305 { "Times New Roman CE,0", "Times New Roman,238" }
307 /* Cyrillic */
308 { 1251, 866, "vga866.fon", "vgafixr.fon", "vgasysr.fon",
309 "courer.fon", "serifer.fon", "smaller.fon", "sserifer.fon", "sseriffr.fon",
310 "Tahoma","Times New Roman", /* FIXME unverified */
311 "Fixedsys,204", "System,204",
312 "Courier New,204", "MS Serif,204", "Small Fonts,204",
313 "MS Sans Serif,204", "MS Sans Serif,204", "MS Serif,204",
314 { "Arial Cyr,0", "Arial,204" },
315 { "Courier New Cyr,0", "Courier New,204" },
316 { "Times New Roman Cyr,0", "Times New Roman,204" }
318 /* Greek */
319 { 1253, 737, "vga869.fon", "vgafixg.fon", "vgasysg.fon",
320 "coureg.fon", "serifeg.fon", "smalleg.fon", "sserifeg.fon", "sseriffg.fon",
321 "Tahoma","Times New Roman", /* FIXME unverified */
322 "Fixedsys,161", "System,161",
323 "Courier New,161", "MS Serif,161", "Small Fonts,161",
324 "MS Sans Serif,161", "MS Sans Serif,161", "MS Serif,161",
325 { "Arial Greek,0", "Arial,161" },
326 { "Courier New Greek,0", "Courier New,161" },
327 { "Times New Roman Greek,0", "Times New Roman,161" }
329 /* Turkish */
330 { 1254, 857, "vga857.fon", "vgafixt.fon", "vgasyst.fon",
331 "couret.fon", "serifet.fon", "smallet.fon", "sserifet.fon", "sserifft.fon",
332 "Tahoma","Times New Roman", /* FIXME unverified */
333 "Fixedsys,162", "System,162",
334 "Courier New,162", "MS Serif,162", "Small Fonts,162",
335 "MS Sans Serif,162", "MS Sans Serif,162", "MS Serif,162",
336 { "Arial Tur,0", "Arial,162" },
337 { "Courier New Tur,0", "Courier New,162" },
338 { "Times New Roman Tur,0", "Times New Roman,162" }
340 /* Hebrew */
341 { 1255, 862, "vgaoem.fon", "vgaf1255.fon", "vgas1255.fon",
342 "coue1255.fon", "sere1255.fon", "smae1255.fon", "ssee1255.fon", "ssef1255.fon",
343 "Tahoma","Times New Roman", /* FIXME unverified */
344 "Fixedsys,177", "System,177",
345 "Courier New,177", "MS Serif,177", "Small Fonts,177",
346 "MS Sans Serif,177", "MS Sans Serif,177", "MS Serif,177"
348 /* Arabic */
349 { 1256, 720, "vgaoem.fon", "vgaf1256.fon", "vgas1256.fon",
350 "coue1256.fon", "sere1256.fon", "smae1256.fon", "ssee1256.fon", "ssef1256.fon",
351 "Microsoft Sans Serif","Times New Roman",
352 "Fixedsys,178", "System,178",
353 "Courier New,178", "MS Serif,178", "Small Fonts,178",
354 "MS Sans Serif,178", "MS Sans Serif,178", "MS Serif,178"
356 /* Baltic */
357 { 1257, 775, "vga775.fon", "vgaf1257.fon", "vgas1257.fon",
358 "coue1257.fon", "sere1257.fon", "smae1257.fon", "ssee1257.fon", "ssef1257.fon",
359 "Tahoma","Times New Roman", /* FIXME unverified */
360 "Fixedsys,186", "System,186",
361 "Courier New,186", "MS Serif,186", "Small Fonts,186",
362 "MS Sans Serif,186", "MS Sans Serif,186", "MS Serif,186",
363 { "Arial Baltic,0", "Arial,186" },
364 { "Courier New Baltic,0", "Courier New,186" },
365 { "Times New Roman Baltic,0", "Times New Roman,186" }
367 /* Vietnamese */
368 { 1258, 1258, "vga850.fon", "vgafix.fon", "vgasys.fon",
369 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
370 "Tahoma","Times New Roman" /* FIXME unverified */
372 /* Thai */
373 { 874, 874, "vga850.fon", "vgaf874.fon", "vgas874.fon",
374 "coure.fon", "serife.fon", "smalle.fon", "ssee874.fon", "ssef874.fon",
375 "Tahoma","Times New Roman" /* FIXME unverified */
377 /* Japanese */
378 { 932, 932, "vga932.fon", "jvgafix.fon", "jvgasys.fon",
379 "coure.fon", "serife.fon", "jsmalle.fon", "sserife.fon", "sseriff.fon",
380 "MS UI Gothic","MS Serif"
382 /* Chinese Simplified */
383 { 936, 936, "vga936.fon", "svgafix.fon", "svgasys.fon",
384 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
385 "SimSun", "NSimSun"
387 /* Korean */
388 { 949, 949, "vga949.fon", "hvgafix.fon", "hvgasys.fon",
389 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
390 "Gulim", "Batang"
392 /* Chinese Traditional */
393 { 950, 950, "vga950.fon", "cvgafix.fon", "cvgasys.fon",
394 "coure.fon", "serife.fon", "smalle.fon", "sserife.fon", "sseriff.fon",
395 "PMingLiU", "MingLiU"
399 static inline BOOL is_dbcs_ansi_cp(UINT ansi_cp)
401 return ( ansi_cp == 932 /* CP932 for Japanese */
402 || ansi_cp == 936 /* CP936 for Chinese Simplified */
403 || ansi_cp == 949 /* CP949 for Korean */
404 || ansi_cp == 950 ); /* CP950 for Chinese Traditional */
407 static CRITICAL_SECTION font_cs;
408 static CRITICAL_SECTION_DEBUG critsect_debug =
410 0, 0, &font_cs,
411 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
412 0, 0, { (DWORD_PTR)(__FILE__ ": font_cs") }
414 static CRITICAL_SECTION font_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
416 #ifndef WINE_FONT_DIR
417 #define WINE_FONT_DIR "fonts"
418 #endif
420 #ifdef WORDS_BIGENDIAN
421 #define GET_BE_WORD(x) (x)
422 #define GET_BE_DWORD(x) (x)
423 #else
424 #define GET_BE_WORD(x) RtlUshortByteSwap(x)
425 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
426 #endif
428 static void get_fonts_data_dir_path( const WCHAR *file, WCHAR *path )
430 if (GetEnvironmentVariableW( L"WINEDATADIR", path, MAX_PATH ))
431 lstrcatW( path, L"\\" WINE_FONT_DIR "\\" );
432 else if (GetEnvironmentVariableW( L"WINEBUILDDIR", path, MAX_PATH ))
433 lstrcatW( path, L"\\fonts\\" );
435 lstrcatW( path, file );
436 if (path[5] == ':') memmove( path, path + 4, (lstrlenW(path) - 3) * sizeof(WCHAR) );
437 else path[1] = '\\'; /* change \??\ to \\?\ */
440 static void get_fonts_win_dir_path( const WCHAR *file, WCHAR *path )
442 GetWindowsDirectoryW( path, MAX_PATH );
443 lstrcatW( path, L"\\fonts\\" );
444 lstrcatW( path, file );
447 /* font substitutions */
449 struct gdi_font_subst
451 struct list entry;
452 int from_charset;
453 int to_charset;
454 WCHAR names[1];
457 static struct list font_subst_list = LIST_INIT(font_subst_list);
459 static inline WCHAR *get_subst_to_name( struct gdi_font_subst *subst )
461 return subst->names + lstrlenW( subst->names ) + 1;
464 static void dump_gdi_font_subst(void)
466 struct gdi_font_subst *subst;
468 LIST_FOR_EACH_ENTRY( subst, &font_subst_list, struct gdi_font_subst, entry )
470 if (subst->from_charset != -1 || subst->to_charset != -1)
471 TRACE("%s,%d -> %s,%d\n", debugstr_w(subst->names),
472 subst->from_charset, debugstr_w(get_subst_to_name(subst)), subst->to_charset);
473 else
474 TRACE("%s -> %s\n", debugstr_w(subst->names), debugstr_w(get_subst_to_name(subst)));
478 static const WCHAR *get_gdi_font_subst( const WCHAR *from_name, int from_charset, int *to_charset )
480 struct gdi_font_subst *subst;
482 LIST_FOR_EACH_ENTRY( subst, &font_subst_list, struct gdi_font_subst, entry )
484 if (!facename_compare( subst->names, from_name, -1 ) &&
485 (subst->from_charset == from_charset || subst->from_charset == -1))
487 if (to_charset) *to_charset = subst->to_charset;
488 return get_subst_to_name( subst );
491 return NULL;
494 static BOOL add_gdi_font_subst( const WCHAR *from_name, int from_charset, const WCHAR *to_name, int to_charset )
496 struct gdi_font_subst *subst;
497 int len = lstrlenW( from_name ) + lstrlenW( to_name ) + 2;
499 if (get_gdi_font_subst( from_name, from_charset, NULL )) return FALSE; /* already exists */
501 if (!(subst = HeapAlloc( GetProcessHeap(), 0,
502 offsetof( struct gdi_font_subst, names[len] ))))
503 return FALSE;
504 lstrcpyW( subst->names, from_name );
505 lstrcpyW( get_subst_to_name(subst), to_name );
506 subst->from_charset = from_charset;
507 subst->to_charset = to_charset;
508 list_add_tail( &font_subst_list, &subst->entry );
509 return TRUE;
512 static void load_gdi_font_subst(void)
514 HKEY hkey;
515 DWORD i = 0, type, dlen, vlen;
516 WCHAR value[64], data[64], *p;
518 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes",
519 &hkey)) return;
521 dlen = sizeof(data);
522 vlen = ARRAY_SIZE(value);
523 while (!RegEnumValueW( hkey, i++, value, &vlen, NULL, &type, (BYTE *)data, &dlen ))
525 int from_charset = -1, to_charset = -1;
527 TRACE("Got %s=%s\n", debugstr_w(value), debugstr_w(data));
528 if ((p = wcsrchr( value, ',' )) && p[1])
530 *p++ = 0;
531 from_charset = wcstol( p, NULL, 10 );
533 if ((p = wcsrchr( data, ',' )) && p[1])
535 *p++ = 0;
536 to_charset = wcstol( p, NULL, 10 );
539 /* Win 2000 doesn't allow mapping between different charsets
540 or mapping of DEFAULT_CHARSET */
541 if ((!from_charset || to_charset == from_charset) && to_charset != DEFAULT_CHARSET)
542 add_gdi_font_subst( value, from_charset, data, to_charset );
544 /* reset dlen and vlen */
545 dlen = sizeof(data);
546 vlen = ARRAY_SIZE(value);
548 RegCloseKey( hkey );
551 /* font families */
553 static int family_namecmp( const WCHAR *str1, const WCHAR *str2 )
555 int prio1, prio2, vert1 = (str1[0] == '@' ? 1 : 0), vert2 = (str2[0] == '@' ? 1 : 0);
557 if (!facename_compare( str1, ff_swiss_default, LF_FACESIZE - 1 )) prio1 = 0;
558 else if (!facename_compare( str1, ff_modern_default, LF_FACESIZE - 1 )) prio1 = 1;
559 else if (!facename_compare( str1, ff_roman_default, LF_FACESIZE - 1 )) prio1 = 2;
560 else prio1 = 3;
562 if (!facename_compare( str2, ff_swiss_default, LF_FACESIZE - 1 )) prio2 = 0;
563 else if (!facename_compare( str2, ff_modern_default, LF_FACESIZE - 1 )) prio2 = 1;
564 else if (!facename_compare( str2, ff_roman_default, LF_FACESIZE - 1 )) prio2 = 2;
565 else prio2 = 3;
567 if (prio1 != prio2) return prio1 - prio2;
568 if (vert1 != vert2) return vert1 - vert2;
569 return facename_compare( str1 + vert1, str2 + vert2, LF_FACESIZE - 1 );
572 static int family_name_compare( const void *key, const struct wine_rb_entry *entry )
574 const struct gdi_font_family *family = WINE_RB_ENTRY_VALUE( entry, const struct gdi_font_family, name_entry );
575 return family_namecmp( (const WCHAR *)key, family->family_name );
578 static int family_second_name_compare( const void *key, const struct wine_rb_entry *entry )
580 const struct gdi_font_family *family = WINE_RB_ENTRY_VALUE( entry, const struct gdi_font_family, second_name_entry );
581 return family_namecmp( (const WCHAR *)key, family->second_name );
584 static int face_full_name_compare( const void *key, const struct wine_rb_entry *entry )
586 const struct gdi_font_face *face = WINE_RB_ENTRY_VALUE( entry, const struct gdi_font_face, full_name_entry );
587 return facename_compare( (const WCHAR *)key, face->full_name, LF_FULLFACESIZE - 1 );
590 static struct wine_rb_tree family_name_tree = { family_name_compare };
591 static struct wine_rb_tree family_second_name_tree = { family_second_name_compare };
592 static struct wine_rb_tree face_full_name_tree = { face_full_name_compare };
594 static int face_is_in_full_name_tree( const struct gdi_font_face *face )
596 return face->full_name_entry.parent || face_full_name_tree.root == &face->full_name_entry;
599 static struct gdi_font_family *create_family( const WCHAR *name, const WCHAR *second_name )
601 struct gdi_font_family *family = HeapAlloc( GetProcessHeap(), 0, sizeof(*family) );
603 family->refcount = 1;
604 lstrcpynW( family->family_name, name, LF_FACESIZE );
605 if (second_name && second_name[0] && wcsicmp( name, second_name ))
607 lstrcpynW( family->second_name, second_name, LF_FACESIZE );
608 add_gdi_font_subst( second_name, -1, name, -1 );
610 else family->second_name[0] = 0;
611 list_init( &family->faces );
612 family->replacement = NULL;
613 wine_rb_put( &family_name_tree, family->family_name, &family->name_entry );
614 if (family->second_name[0]) wine_rb_put( &family_second_name_tree, family->second_name, &family->second_name_entry );
615 return family;
618 static void release_family( struct gdi_font_family *family )
620 if (--family->refcount) return;
621 assert( list_empty( &family->faces ));
622 wine_rb_remove( &family_name_tree, &family->name_entry );
623 if (family->second_name[0]) wine_rb_remove( &family_second_name_tree, &family->second_name_entry );
624 if (family->replacement) release_family( family->replacement );
625 HeapFree( GetProcessHeap(), 0, family );
628 static struct gdi_font_family *find_family_from_name( const WCHAR *name )
630 struct wine_rb_entry *entry;
631 if (!(entry = wine_rb_get( &family_name_tree, name ))) return NULL;
632 return WINE_RB_ENTRY_VALUE( entry, struct gdi_font_family, name_entry );
635 static struct gdi_font_family *find_family_from_any_name( const WCHAR *name )
637 struct wine_rb_entry *entry;
638 struct gdi_font_family *family;
639 if ((family = find_family_from_name( name ))) return family;
640 if (!(entry = wine_rb_get( &family_second_name_tree, name ))) return NULL;
641 return WINE_RB_ENTRY_VALUE( entry, struct gdi_font_family, second_name_entry );
644 static struct gdi_font_face *find_face_from_full_name( const WCHAR *full_name )
646 struct wine_rb_entry *entry;
647 if (!(entry = wine_rb_get( &face_full_name_tree, full_name ))) return NULL;
648 return WINE_RB_ENTRY_VALUE( entry, struct gdi_font_face, full_name_entry );
651 static const struct list *get_family_face_list( const struct gdi_font_family *family )
653 return family->replacement ? &family->replacement->faces : &family->faces;
656 static struct gdi_font_face *family_find_face_from_filename( struct gdi_font_family *family, const WCHAR *file_name )
658 struct gdi_font_face *face;
659 const WCHAR *file;
660 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
662 if (!face->file) continue;
663 file = wcsrchr(face->file, '\\');
664 if (!file) file = face->file;
665 else file++;
666 if (wcsicmp( file, file_name )) continue;
667 face->refcount++;
668 return face;
670 return NULL;
673 static struct gdi_font_face *find_face_from_filename( const WCHAR *file_name, const WCHAR *family_name )
675 struct gdi_font_family *family;
676 struct gdi_font_face *face;
678 TRACE( "looking for file %s name %s\n", debugstr_w(file_name), debugstr_w(family_name) );
680 if (!family_name)
682 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
683 if ((face = family_find_face_from_filename( family, file_name ))) return face;
684 return NULL;
687 if (!(family = find_family_from_name( family_name ))) return NULL;
688 return family_find_face_from_filename( family, file_name );
691 static BOOL add_family_replacement( const WCHAR *new_name, const WCHAR *replace )
693 struct gdi_font_family *new_family, *family;
694 struct gdi_font_face *face;
695 WCHAR new_name_vert[LF_FACESIZE], replace_vert[LF_FACESIZE];
697 if (!(family = find_family_from_any_name( replace )))
699 TRACE( "%s is not available. Skip this replacement.\n", debugstr_w(replace) );
700 return FALSE;
703 if (!(new_family = create_family( new_name, NULL ))) return FALSE;
704 new_family->replacement = family;
705 family->refcount++;
706 TRACE( "mapping %s to %s\n", debugstr_w(replace), debugstr_w(new_name) );
708 /* also add replacement for vertical font if necessary */
709 if (replace[0] == '@') return TRUE;
710 if (list_empty( &family->faces )) return TRUE;
711 face = LIST_ENTRY( list_head(&family->faces), struct gdi_font_face, entry );
712 if (!(face->fs.fsCsb[0] & FS_DBCS_MASK)) return TRUE;
714 new_name_vert[0] = '@';
715 lstrcpynW( new_name_vert + 1, new_name, LF_FACESIZE - 1 );
716 if (find_family_from_any_name( new_name_vert )) return TRUE; /* already exists */
718 replace_vert[0] = '@';
719 lstrcpynW( replace_vert + 1, replace, LF_FACESIZE - 1 );
720 add_family_replacement( new_name_vert, replace_vert );
721 return TRUE;
725 * The replacement list is a way to map an entire font
726 * family onto another family. For example adding
728 * [HKCU\Software\Wine\Fonts\Replacements]
729 * "Wingdings"="Winedings"
731 * would enumerate the Winedings font both as Winedings and
732 * Wingdings. However if a real Wingdings font is present the
733 * replacement does not take place.
735 static void load_gdi_font_replacements(void)
737 HKEY hkey;
738 DWORD i = 0, type, dlen, vlen;
739 WCHAR value[LF_FACESIZE], data[1024];
741 /* @@ Wine registry key: HKCU\Software\Wine\Fonts\Replacements */
742 if (RegOpenKeyW( wine_fonts_key, L"Replacements", &hkey )) return;
744 dlen = sizeof(data);
745 vlen = ARRAY_SIZE(value);
746 while (!RegEnumValueW( hkey, i++, value, &vlen, NULL, &type, (BYTE *)data, &dlen ))
748 /* "NewName"="Oldname" */
749 if (!find_family_from_any_name( value ))
751 if (type == REG_MULTI_SZ)
753 WCHAR *replace = data;
754 while (*replace)
756 if (add_family_replacement( value, replace )) break;
757 replace += lstrlenW(replace) + 1;
760 else if (type == REG_SZ) add_family_replacement( value, data );
762 else TRACE("%s is available. Skip this replacement.\n", debugstr_w(value));
764 /* reset dlen and vlen */
765 dlen = sizeof(data);
766 vlen = ARRAY_SIZE(value);
768 RegCloseKey( hkey );
771 static void dump_gdi_font_list(void)
773 struct gdi_font_family *family;
774 struct gdi_font_face *face;
776 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
778 TRACE( "Family: %s\n", debugstr_w(family->family_name) );
779 LIST_FOR_EACH_ENTRY( face, &family->faces, struct gdi_font_face, entry )
781 TRACE( "\t%s\t%s\t%08x", debugstr_w(face->style_name), debugstr_w(face->full_name),
782 face->fs.fsCsb[0] );
783 if (!face->scalable) TRACE(" %d", face->size.height );
784 TRACE("\n");
789 static BOOL enum_fallbacks( DWORD pitch_and_family, int index, WCHAR buffer[LF_FACESIZE] )
791 if (index < 3)
793 const WCHAR * const *defaults;
795 if ((pitch_and_family & FIXED_PITCH) || (pitch_and_family & 0xf0) == FF_MODERN)
796 defaults = default_fixed_list;
797 else if ((pitch_and_family & 0xf0) == FF_ROMAN)
798 defaults = default_serif_list;
799 else
800 defaults = default_sans_list;
801 lstrcpynW( buffer, defaults[index], LF_FACESIZE );
802 return TRUE;
804 return font_funcs->enum_family_fallbacks( pitch_and_family, index - 3, buffer );
807 static void set_default_family( DWORD pitch_and_family, WCHAR *default_name )
809 struct wine_rb_entry *entry;
810 WCHAR name[LF_FACESIZE];
811 int i = 0;
813 while (enum_fallbacks( pitch_and_family, i++, name ))
815 if (!(entry = wine_rb_get( &family_name_tree, name ))) continue;
816 wine_rb_remove( &family_name_tree, entry );
817 lstrcpynW( default_name, name, LF_FACESIZE - 1 );
818 wine_rb_put( &family_name_tree, name, entry );
819 return;
823 static void reorder_font_list(void)
825 set_default_family( FF_ROMAN, ff_roman_default );
826 set_default_family( FF_MODERN, ff_modern_default );
827 set_default_family( FF_SWISS, ff_swiss_default );
830 static void release_face( struct gdi_font_face *face )
832 if (--face->refcount) return;
833 if (face->family)
835 if (face->flags & ADDFONT_ADD_TO_CACHE) remove_face_from_cache( face );
836 list_remove( &face->entry );
837 release_family( face->family );
839 if (face_is_in_full_name_tree( face )) wine_rb_remove( &face_full_name_tree, &face->full_name_entry );
840 HeapFree( GetProcessHeap(), 0, face->file );
841 HeapFree( GetProcessHeap(), 0, face->style_name );
842 HeapFree( GetProcessHeap(), 0, face->full_name );
843 HeapFree( GetProcessHeap(), 0, face->cached_enum_data );
844 HeapFree( GetProcessHeap(), 0, face );
847 static int remove_font( const WCHAR *file, DWORD flags )
849 struct gdi_font_family *family, *family_next;
850 struct gdi_font_face *face, *face_next;
851 int count = 0;
853 EnterCriticalSection( &font_cs );
854 WINE_RB_FOR_EACH_ENTRY_DESTRUCTOR( family, family_next, &family_name_tree, struct gdi_font_family, name_entry )
856 family->refcount++;
857 LIST_FOR_EACH_ENTRY_SAFE( face, face_next, &family->faces, struct gdi_font_face, entry )
859 if (!face->file) continue;
860 if (LOWORD(face->flags) != LOWORD(flags)) continue;
861 if (!wcsicmp( face->file, file ))
863 TRACE( "removing matching face %s refcount %d\n", debugstr_w(face->file), face->refcount );
864 release_face( face );
865 count++;
868 release_family( family );
870 LeaveCriticalSection( &font_cs );
871 return count;
874 static inline BOOL faces_equal( const struct gdi_font_face *f1, const struct gdi_font_face *f2 )
876 if (facename_compare( f1->full_name, f2->full_name, -1 )) return FALSE;
877 if (f1->scalable) return TRUE;
878 if (f1->size.y_ppem != f2->size.y_ppem) return FALSE;
879 return !memcmp( &f1->fs, &f2->fs, sizeof(f1->fs) );
882 static inline int style_order( const struct gdi_font_face *face )
884 switch (face->ntmFlags & (NTM_REGULAR | NTM_BOLD | NTM_ITALIC))
886 case NTM_REGULAR:
887 return 0;
888 case NTM_BOLD:
889 return 1;
890 case NTM_ITALIC:
891 return 2;
892 case NTM_BOLD | NTM_ITALIC:
893 return 3;
894 default:
895 WARN( "Don't know how to order face %s with flags 0x%08x\n",
896 debugstr_w(face->full_name), face->ntmFlags );
897 return 9999;
901 static BOOL insert_face_in_family_list( struct gdi_font_face *face, struct gdi_font_family *family )
903 struct gdi_font_face *cursor;
905 LIST_FOR_EACH_ENTRY( cursor, &family->faces, struct gdi_font_face, entry )
907 if (faces_equal( face, cursor ))
909 TRACE( "Already loaded face %s in family %s, original version %x, new version %x\n",
910 debugstr_w(face->full_name), debugstr_w(family->family_name),
911 cursor->version, face->version );
913 if (face->file && cursor->file && !wcsicmp( face->file, cursor->file ))
915 cursor->refcount++;
916 TRACE("Font %s already in list, refcount now %d\n",
917 debugstr_w(face->file), cursor->refcount);
918 return FALSE;
920 if (face->version <= cursor->version)
922 TRACE("Original font %s is newer so skipping %s\n",
923 debugstr_w(cursor->file), debugstr_w(face->file));
924 return FALSE;
926 else
928 TRACE("Replacing original %s with %s\n",
929 debugstr_w(cursor->file), debugstr_w(face->file));
930 list_add_before( &cursor->entry, &face->entry );
931 face->family = family;
932 family->refcount++;
933 face->refcount++;
934 if (face_is_in_full_name_tree( cursor ))
936 wine_rb_replace( &face_full_name_tree, &cursor->full_name_entry, &face->full_name_entry );
937 memset( &cursor->full_name_entry, 0, sizeof(cursor->full_name_entry) );
939 release_face( cursor );
940 return TRUE;
943 if (style_order( face ) < style_order( cursor )) break;
946 TRACE( "Adding face %s in family %s from %s\n", debugstr_w(face->full_name),
947 debugstr_w(family->family_name), debugstr_w(face->file) );
948 list_add_before( &cursor->entry, &face->entry );
949 if (face->scalable) wine_rb_put( &face_full_name_tree, face->full_name, &face->full_name_entry );
950 face->family = family;
951 family->refcount++;
952 face->refcount++;
953 return TRUE;
956 static struct gdi_font_face *create_face( struct gdi_font_family *family, const WCHAR *style,
957 const WCHAR *fullname, const WCHAR *file,
958 void *data_ptr, SIZE_T data_size, UINT index, FONTSIGNATURE fs,
959 DWORD ntmflags, DWORD version, DWORD flags,
960 const struct bitmap_font_size *size )
962 struct gdi_font_face *face = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*face) );
964 face->refcount = 1;
965 face->style_name = strdupW( style );
966 face->full_name = strdupW( fullname );
967 face->face_index = index;
968 face->fs = fs;
969 face->ntmFlags = ntmflags;
970 face->version = version;
971 face->flags = flags;
972 face->data_ptr = data_ptr;
973 face->data_size = data_size;
974 if (file) face->file = strdupW( file );
975 if (size) face->size = *size;
976 else face->scalable = TRUE;
977 if (insert_face_in_family_list( face, family )) return face;
978 release_face( face );
979 return NULL;
982 static int CDECL add_gdi_face( const WCHAR *family_name, const WCHAR *second_name,
983 const WCHAR *style, const WCHAR *fullname, const WCHAR *file,
984 void *data_ptr, SIZE_T data_size, UINT index, FONTSIGNATURE fs,
985 DWORD ntmflags, DWORD version, DWORD flags,
986 const struct bitmap_font_size *size )
988 struct gdi_font_face *face;
989 struct gdi_font_family *family;
990 int ret = 0;
992 if ((family = find_family_from_name( family_name ))) family->refcount++;
993 else if (!(family = create_family( family_name, second_name ))) return ret;
995 if ((face = create_face( family, style, fullname, file, data_ptr, data_size,
996 index, fs, ntmflags, version, flags, size )))
998 if (flags & ADDFONT_ADD_TO_CACHE) add_face_to_cache( face );
999 release_face( face );
1001 release_family( family );
1002 ret++;
1004 if (fs.fsCsb[0] & FS_DBCS_MASK)
1006 WCHAR vert_family[LF_FACESIZE], vert_second[LF_FACESIZE], vert_full[LF_FULLFACESIZE];
1008 vert_family[0] = '@';
1009 lstrcpynW( vert_family + 1, family_name, LF_FACESIZE - 1 );
1011 if (second_name && second_name[0])
1013 vert_second[0] = '@';
1014 lstrcpynW( vert_second + 1, second_name, LF_FACESIZE - 1 );
1016 else vert_second[0] = 0;
1018 if (fullname)
1020 vert_full[0] = '@';
1021 lstrcpynW( vert_full + 1, fullname, LF_FULLFACESIZE - 1 );
1022 fullname = vert_full;
1025 if ((family = find_family_from_name( vert_family ))) family->refcount++;
1026 else if (!(family = create_family( vert_family, vert_second ))) return ret;
1028 if ((face = create_face( family, style, fullname, file, data_ptr, data_size,
1029 index, fs, ntmflags, version, flags | ADDFONT_VERTICAL_FONT, size )))
1031 if (flags & ADDFONT_ADD_TO_CACHE) add_face_to_cache( face );
1032 release_face( face );
1034 release_family( family );
1035 ret++;
1037 return ret;
1040 /* font cache */
1042 struct cached_face
1044 DWORD index;
1045 DWORD flags;
1046 DWORD ntmflags;
1047 DWORD version;
1048 struct bitmap_font_size size;
1049 FONTSIGNATURE fs;
1050 WCHAR full_name[1];
1051 /* WCHAR file_name[]; */
1054 static void load_face_from_cache( HKEY hkey_family, struct gdi_font_family *family,
1055 void *buffer, DWORD buffer_size, BOOL scalable )
1057 DWORD type, size, needed, index = 0;
1058 struct gdi_font_face *face;
1059 HKEY hkey_strike;
1060 WCHAR name[256];
1061 struct cached_face *cached = (struct cached_face *)buffer;
1063 size = sizeof(name);
1064 needed = buffer_size - sizeof(DWORD);
1065 while (!RegEnumValueW( hkey_family, index++, name, &size, NULL, &type, buffer, &needed ))
1067 if (type == REG_BINARY && needed > sizeof(*cached))
1069 ((DWORD *)buffer)[needed / sizeof(DWORD)] = 0;
1070 if ((face = create_face( family, name, cached->full_name,
1071 cached->full_name + lstrlenW(cached->full_name) + 1,
1072 NULL, 0, cached->index, cached->fs, cached->ntmflags, cached->version,
1073 cached->flags, scalable ? NULL : &cached->size )))
1075 if (!scalable)
1076 TRACE("Adding bitmap size h %d w %d size %d x_ppem %d y_ppem %d\n",
1077 face->size.height, face->size.width, face->size.size >> 6,
1078 face->size.x_ppem >> 6, face->size.y_ppem >> 6);
1080 TRACE("fsCsb = %08x %08x/%08x %08x %08x %08x\n",
1081 face->fs.fsCsb[0], face->fs.fsCsb[1],
1082 face->fs.fsUsb[0], face->fs.fsUsb[1],
1083 face->fs.fsUsb[2], face->fs.fsUsb[3]);
1085 release_face( face );
1088 size = sizeof(name);
1089 needed = buffer_size - sizeof(DWORD);
1092 /* load bitmap strikes */
1094 index = 0;
1095 needed = buffer_size;
1096 while (!RegEnumKeyExW( hkey_family, index++, buffer, &needed, NULL, NULL, NULL, NULL ))
1098 if (!RegOpenKeyExW( hkey_family, buffer, 0, KEY_ALL_ACCESS, &hkey_strike ))
1100 load_face_from_cache( hkey_strike, family, buffer, buffer_size, FALSE );
1101 RegCloseKey( hkey_strike );
1103 needed = buffer_size;
1107 static void load_font_list_from_cache(void)
1109 DWORD size, family_index = 0;
1110 struct gdi_font_family *family;
1111 HKEY hkey_family;
1112 WCHAR buffer[4096], second_name[LF_FACESIZE];
1114 size = sizeof(buffer);
1115 while (!RegEnumKeyExW( wine_fonts_cache_key, family_index++, buffer, &size, NULL, NULL, NULL, NULL ))
1117 RegOpenKeyExW( wine_fonts_cache_key, buffer, 0, KEY_ALL_ACCESS, &hkey_family );
1118 TRACE("opened family key %s\n", debugstr_w(buffer));
1119 size = sizeof(second_name);
1120 if (RegQueryValueExW( hkey_family, NULL, NULL, NULL, (BYTE *)second_name, &size ))
1121 second_name[0] = 0;
1123 family = create_family( buffer, second_name );
1125 load_face_from_cache( hkey_family, family, buffer, sizeof(buffer), TRUE );
1127 RegCloseKey( hkey_family );
1128 release_family( family );
1129 size = sizeof(buffer);
1133 static void add_face_to_cache( struct gdi_font_face *face )
1135 HKEY hkey_family, hkey_face;
1136 DWORD len, buffer[1024];
1137 struct cached_face *cached = (struct cached_face *)buffer;
1139 if (RegCreateKeyExW( wine_fonts_cache_key, face->family->family_name, 0, NULL, REG_OPTION_VOLATILE,
1140 KEY_ALL_ACCESS, NULL, &hkey_family, NULL ))
1141 return;
1143 if (face->family->second_name[0])
1144 RegSetValueExW( hkey_family, NULL, 0, REG_SZ, (BYTE *)face->family->second_name,
1145 (lstrlenW( face->family->second_name ) + 1) * sizeof(WCHAR) );
1147 if (!face->scalable)
1149 WCHAR name[10];
1151 swprintf( name, ARRAY_SIZE(name), L"%d", face->size.y_ppem );
1152 RegCreateKeyExW( hkey_family, name, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS,
1153 NULL, &hkey_face, NULL);
1155 else hkey_face = hkey_family;
1157 memset( cached, 0, sizeof(*cached) );
1158 cached->index = face->face_index;
1159 cached->flags = face->flags;
1160 cached->ntmflags = face->ntmFlags;
1161 cached->version = face->version;
1162 cached->fs = face->fs;
1163 if (!face->scalable) cached->size = face->size;
1164 lstrcpyW( cached->full_name, face->full_name );
1165 len = lstrlenW( face->full_name ) + 1;
1166 lstrcpyW( cached->full_name + len, face->file );
1167 len += lstrlenW( face->file ) + 1;
1169 RegSetValueExW( hkey_face, face->style_name, 0, REG_BINARY, (BYTE *)cached,
1170 offsetof( struct cached_face, full_name[len] ));
1172 if (hkey_face != hkey_family) RegCloseKey( hkey_face );
1173 RegCloseKey( hkey_family );
1176 static void remove_face_from_cache( struct gdi_font_face *face )
1178 HKEY hkey_family;
1180 if (RegOpenKeyExW( wine_fonts_cache_key, face->family->family_name, 0, KEY_ALL_ACCESS, &hkey_family ))
1181 return;
1183 if (!face->scalable)
1185 WCHAR name[10];
1186 swprintf( name, ARRAY_SIZE(name), L"%d", face->size.y_ppem );
1187 RegDeleteKeyW( hkey_family, name );
1189 else RegDeleteValueW( hkey_family, face->style_name );
1191 RegCloseKey( hkey_family );
1194 /* font links */
1196 struct gdi_font_link
1198 struct list entry;
1199 struct list links;
1200 WCHAR name[LF_FACESIZE];
1201 FONTSIGNATURE fs;
1204 struct gdi_font_link_entry
1206 struct list entry;
1207 FONTSIGNATURE fs;
1208 WCHAR family_name[LF_FACESIZE];
1211 static struct list font_links = LIST_INIT(font_links);
1213 static struct gdi_font_link *find_gdi_font_link( const WCHAR *name )
1215 struct gdi_font_link *link;
1217 LIST_FOR_EACH_ENTRY( link, &font_links, struct gdi_font_link, entry )
1218 if (!facename_compare( link->name, name, LF_FACESIZE - 1 )) return link;
1219 return NULL;
1222 static struct gdi_font_family *find_family_from_font_links( const WCHAR *name, const WCHAR *subst,
1223 FONTSIGNATURE fs )
1225 struct gdi_font_link *link;
1226 struct gdi_font_link_entry *entry;
1227 struct gdi_font_family *family;
1229 LIST_FOR_EACH_ENTRY( link, &font_links, struct gdi_font_link, entry )
1231 if (!facename_compare( link->name, name, LF_FACESIZE - 1 ) ||
1232 (subst && !facename_compare( link->name, subst, LF_FACESIZE - 1 )))
1234 TRACE("found entry in system list\n");
1235 LIST_FOR_EACH_ENTRY( entry, &link->links, struct gdi_font_link_entry, entry )
1237 const struct gdi_font_link *links;
1239 family = find_family_from_name( entry->family_name );
1240 if (!fs.fsCsb[0]) return family;
1241 if (fs.fsCsb[0] & entry->fs.fsCsb[0]) return family;
1242 if ((links = find_gdi_font_link( family->family_name )) && fs.fsCsb[0] & links->fs.fsCsb[0])
1243 return family;
1247 return NULL;
1250 static struct gdi_font_link *add_gdi_font_link( const WCHAR *name )
1252 struct gdi_font_link *link = find_gdi_font_link( name );
1254 if (link) return link;
1255 if ((link = HeapAlloc( GetProcessHeap(), 0, sizeof(*link) )))
1257 lstrcpynW( link->name, name, LF_FACESIZE );
1258 memset( &link->fs, 0, sizeof(link->fs) );
1259 list_init( &link->links );
1260 list_add_tail( &font_links, &link->entry );
1262 return link;
1265 static void add_gdi_font_link_entry( struct gdi_font_link *link, const WCHAR *family_name, FONTSIGNATURE fs )
1267 struct gdi_font_link_entry *entry;
1269 entry = HeapAlloc( GetProcessHeap(), 0, sizeof(*entry) );
1270 lstrcpynW( entry->family_name, family_name, LF_FACESIZE );
1271 entry->fs = fs;
1272 link->fs.fsCsb[0] |= fs.fsCsb[0];
1273 link->fs.fsCsb[1] |= fs.fsCsb[1];
1274 list_add_tail( &link->links, &entry->entry );
1277 static const WCHAR * const font_links_list[] =
1279 L"Lucida Sans Unicode",
1280 L"Microsoft Sans Serif",
1281 L"Tahoma"
1284 static const struct font_links_defaults_list
1286 /* Keyed off substitution for "MS Shell Dlg" */
1287 const WCHAR *shelldlg;
1288 /* Maximum of four substitutes, plus terminating NULL pointer */
1289 const WCHAR *substitutes[5];
1290 } font_links_defaults_list[] =
1292 /* Non East-Asian */
1293 { L"Tahoma", /* FIXME unverified ordering */
1294 { L"MS UI Gothic", L"SimSun", L"Gulim", L"PMingLiU", NULL }
1296 /* Below lists are courtesy of
1297 * http://blogs.msdn.com/michkap/archive/2005/06/18/430507.aspx
1299 /* Japanese */
1300 { L"MS UI Gothic",
1301 { L"MS UI Gothic", L"PMingLiU", L"SimSun", L"Gulim", NULL }
1303 /* Chinese Simplified */
1304 { L"SimSun",
1305 { L"SimSun", L"PMingLiU", L"MS UI Gothic", L"Batang", NULL }
1307 /* Korean */
1308 { L"Gulim",
1309 { L"Gulim", L"PMingLiU", L"MS UI Gothic", L"SimSun", NULL }
1311 /* Chinese Traditional */
1312 { L"PMingLiU",
1313 { L"PMingLiU", L"SimSun", L"MS UI Gothic", L"Batang", NULL }
1317 static void populate_system_links( const WCHAR *name, const WCHAR * const *values )
1319 struct gdi_font_family *family;
1320 struct gdi_font_face *face;
1321 struct gdi_font_link *font_link;
1322 const WCHAR *file, *value;
1324 /* Don't store fonts that are only substitutes for other fonts */
1325 if (get_gdi_font_subst( name, -1, NULL ))
1327 TRACE( "%s: Internal SystemLink entry for substituted font, ignoring\n", debugstr_w(name) );
1328 return;
1330 font_link = add_gdi_font_link( name );
1331 for ( ; *values; values++)
1333 if (!facename_compare( name, *values, -1 )) continue;
1334 if (!(value = get_gdi_font_subst( *values, -1, NULL ))) value = *values;
1335 if (!(family = find_family_from_name( value ))) continue;
1336 /* use first extant filename for this Family */
1337 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
1339 if (!face->file) continue;
1340 file = wcsrchr(face->file, '\\');
1341 if (!file) file = face->file;
1342 else file++;
1343 if ((face = find_face_from_filename( file, value )))
1345 add_gdi_font_link_entry( font_link, face->family->family_name, face->fs );
1346 TRACE( "added internal SystemLink for %s to %s in %s\n",
1347 debugstr_w(name), debugstr_w(value), debugstr_w(file) );
1349 else TRACE( "Unable to find file %s face name %s\n", debugstr_w(file), debugstr_w(value) );
1350 break;
1355 static void load_system_links(void)
1357 HKEY hkey;
1358 DWORD i, j;
1359 const WCHAR *shelldlg_name;
1360 struct gdi_font_link *font_link, *system_font_link;
1361 struct gdi_font_face *face;
1363 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE,
1364 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", &hkey ))
1366 WCHAR value[MAX_PATH], data[1024];
1367 DWORD type, val_len, data_len;
1368 WCHAR *entry, *next;
1370 val_len = ARRAY_SIZE(value);
1371 data_len = sizeof(data);
1372 i = 0;
1373 while (!RegEnumValueW( hkey, i++, value, &val_len, NULL, &type, (LPBYTE)data, &data_len))
1375 /* Don't store fonts that are only substitutes for other fonts */
1376 if (!get_gdi_font_subst( value, -1, NULL ))
1378 font_link = add_gdi_font_link( value );
1379 for (entry = data; (char *)entry < (char *)data + data_len && *entry; entry = next)
1381 const WCHAR *family_name = NULL;
1382 WCHAR *p;
1384 TRACE("%s: %s\n", debugstr_w(value), debugstr_w(entry));
1386 next = entry + lstrlenW(entry) + 1;
1387 if ((p = wcschr( entry, ',' )))
1389 *p++ = 0;
1390 while (iswspace(*p)) p++;
1391 if (!(family_name = get_gdi_font_subst( p, -1, NULL ))) family_name = p;
1393 if ((face = find_face_from_filename( entry, family_name )))
1395 add_gdi_font_link_entry( font_link, face->family->family_name, face->fs );
1396 TRACE("Adding file %s index %u\n", debugstr_w(face->file), face->face_index);
1398 else TRACE( "Unable to find file %s family %s\n",
1399 debugstr_w(entry), debugstr_w(family_name) );
1402 else TRACE("%s: SystemLink entry for substituted font, ignoring\n", debugstr_w(value));
1404 val_len = ARRAY_SIZE(value);
1405 data_len = sizeof(data);
1407 RegCloseKey( hkey );
1410 if ((shelldlg_name = get_gdi_font_subst( L"MS Shell Dlg", -1, NULL )))
1412 for (i = 0; i < ARRAY_SIZE(font_links_defaults_list); i++)
1414 const WCHAR *subst = get_gdi_font_subst( font_links_defaults_list[i].shelldlg, -1, NULL );
1416 if ((!facename_compare( font_links_defaults_list[i].shelldlg, shelldlg_name, -1 ) ||
1417 (subst && !facename_compare( subst, shelldlg_name, -1 ))))
1419 for (j = 0; j < ARRAY_SIZE(font_links_list); j++)
1420 populate_system_links( font_links_list[j], font_links_defaults_list[i].substitutes );
1421 if (!facename_compare(shelldlg_name, font_links_defaults_list[i].substitutes[0], -1))
1422 populate_system_links( shelldlg_name, font_links_defaults_list[i].substitutes );
1426 else WARN( "could not find FontSubstitute for MS Shell Dlg\n" );
1428 /* Explicitly add an entry for the system font, this links to Tahoma and any links
1429 that Tahoma has */
1431 system_font_link = add_gdi_font_link( L"System" );
1432 if ((face = find_face_from_filename( L"tahoma.ttf", L"Tahoma" )))
1434 add_gdi_font_link_entry( system_font_link, face->family->family_name, face->fs );
1435 TRACE("Found Tahoma in %s index %u\n", debugstr_w(face->file), face->face_index);
1437 if ((font_link = find_gdi_font_link( L"Tahoma" )))
1439 struct gdi_font_link_entry *entry;
1440 LIST_FOR_EACH_ENTRY( entry, &font_link->links, struct gdi_font_link_entry, entry )
1441 add_gdi_font_link_entry( system_font_link, entry->family_name, entry->fs );
1445 /* font matching */
1447 static BOOL can_select_face( const struct gdi_font_face *face, FONTSIGNATURE fs, BOOL can_use_bitmap )
1449 struct gdi_font_link *font_link;
1451 if (!face->scalable && !can_use_bitmap) return FALSE;
1452 if (!fs.fsCsb[0]) return TRUE;
1453 if (fs.fsCsb[0] & face->fs.fsCsb[0]) return TRUE;
1454 if (!(font_link = find_gdi_font_link( face->family->family_name ))) return FALSE;
1455 if (fs.fsCsb[0] & font_link->fs.fsCsb[0]) return TRUE;
1456 return FALSE;
1459 static struct gdi_font_face *find_best_matching_face( const struct gdi_font_family *family,
1460 const LOGFONTW *lf, FONTSIGNATURE fs,
1461 BOOL can_use_bitmap )
1463 struct gdi_font_face *face = NULL, *best = NULL, *best_bitmap = NULL;
1464 unsigned int best_score = 4;
1465 int best_diff = 0;
1466 int it = !!lf->lfItalic;
1467 int bd = lf->lfWeight > 550;
1468 int height = lf->lfHeight;
1470 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
1472 int italic = !!(face->ntmFlags & NTM_ITALIC);
1473 int bold = !!(face->ntmFlags & NTM_BOLD);
1474 int score = (italic ^ it) + (bold ^ bd);
1476 if (!can_select_face( face, fs, can_use_bitmap )) continue;
1477 if (score > best_score) continue;
1478 TRACE( "(it=%d, bd=%d) is selected for (it=%d, bd=%d)\n", italic, bold, it, bd );
1479 best_score = score;
1480 best = face;
1481 if (best->scalable && best_score == 0) break;
1482 if (!best->scalable)
1484 int diff;
1485 if (height > 0)
1486 diff = height - (signed int)best->size.height;
1487 else
1488 diff = -height - ((signed int)best->size.height - best->size.internal_leading);
1489 if (!best_bitmap ||
1490 (best_diff > 0 && diff >= 0 && diff < best_diff) ||
1491 (best_diff < 0 && diff > best_diff))
1493 TRACE( "%d is better for %d diff was %d\n", best->size.height, height, best_diff );
1494 best_diff = diff;
1495 best_bitmap = best;
1496 if (best_score == 0 && best_diff == 0) break;
1500 if (!best) return NULL;
1501 return best->scalable ? best : best_bitmap;
1504 static struct gdi_font_face *find_matching_face_by_name( const WCHAR *name, const WCHAR *subst,
1505 const LOGFONTW *lf, FONTSIGNATURE fs,
1506 BOOL can_use_bitmap )
1508 struct gdi_font_family *family;
1509 struct gdi_font_face *face;
1511 family = find_family_from_any_name( name );
1512 if (family && (face = find_best_matching_face( family, lf, fs, can_use_bitmap ))) return face;
1513 if (subst)
1515 family = find_family_from_any_name( subst );
1516 if (family && (face = find_best_matching_face( family, lf, fs, can_use_bitmap ))) return face;
1519 /* search by full face name */
1520 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
1521 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
1522 if (!facename_compare( face->full_name, name, LF_FACESIZE - 1 ) &&
1523 can_select_face( face, fs, can_use_bitmap ))
1524 return face;
1526 if ((family = find_family_from_font_links( name, subst, fs )))
1528 if ((face = find_best_matching_face( family, lf, fs, can_use_bitmap ))) return face;
1530 return NULL;
1533 static struct gdi_font_face *find_any_face( const LOGFONTW *lf, FONTSIGNATURE fs,
1534 BOOL can_use_bitmap, BOOL want_vertical )
1536 struct gdi_font_family *family;
1537 struct gdi_font_face *face;
1538 WCHAR name[LF_FACESIZE + 1];
1539 int i = 0;
1541 /* first try the family fallbacks */
1542 while (enum_fallbacks( lf->lfPitchAndFamily, i++, name ))
1544 if (want_vertical)
1546 memmove(name + 1, name, min(lstrlenW(name), LF_FACESIZE));
1547 name[0] = '@';
1550 if (!(family = find_family_from_any_name(name))) continue;
1551 if ((face = find_best_matching_face( family, lf, fs, FALSE ))) return face;
1553 /* otherwise try only scalable */
1554 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
1556 if ((family->family_name[0] == '@') == !want_vertical) continue;
1557 if ((face = find_best_matching_face( family, lf, fs, FALSE ))) return face;
1559 if (!can_use_bitmap) return NULL;
1560 /* then also bitmap fonts */
1561 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
1563 if ((family->family_name[0] == '@') == !want_vertical) continue;
1564 if ((face = find_best_matching_face( family, lf, fs, can_use_bitmap ))) return face;
1566 return NULL;
1569 static struct gdi_font_face *find_matching_face( const LOGFONTW *lf, CHARSETINFO *csi, BOOL can_use_bitmap,
1570 const WCHAR **orig_name )
1572 BOOL want_vertical = (lf->lfFaceName[0] == '@');
1573 struct gdi_font_face *face;
1575 if (!TranslateCharsetInfo( (DWORD *)(INT_PTR)lf->lfCharSet, csi, TCI_SRCCHARSET ))
1577 if (lf->lfCharSet != DEFAULT_CHARSET) FIXME( "Untranslated charset %d\n", lf->lfCharSet );
1578 csi->fs.fsCsb[0] = 0;
1581 if (lf->lfFaceName[0])
1583 int subst_charset;
1584 const WCHAR *subst = get_gdi_font_subst( lf->lfFaceName, lf->lfCharSet, &subst_charset );
1586 if (subst)
1588 TRACE( "substituting %s,%d -> %s,%d\n", debugstr_w(lf->lfFaceName), lf->lfCharSet,
1589 debugstr_w(subst), (subst_charset != -1) ? subst_charset : lf->lfCharSet );
1590 if (subst_charset != -1)
1591 TranslateCharsetInfo( (DWORD *)(INT_PTR)subst_charset, csi, TCI_SRCCHARSET );
1592 *orig_name = lf->lfFaceName;
1595 if ((face = find_matching_face_by_name( lf->lfFaceName, subst, lf, csi->fs, can_use_bitmap )))
1596 return face;
1598 *orig_name = NULL; /* substitution is no longer relevant */
1600 /* If requested charset was DEFAULT_CHARSET then try using charset
1601 corresponding to the current ansi codepage */
1602 if (!csi->fs.fsCsb[0])
1604 INT acp = GetACP();
1605 if (!TranslateCharsetInfo( (DWORD *)(INT_PTR)acp, csi, TCI_SRCCODEPAGE ))
1607 FIXME( "TCI failed on codepage %d\n", acp );
1608 csi->fs.fsCsb[0] = 0;
1612 if ((face = find_any_face( lf, csi->fs, can_use_bitmap, want_vertical ))) return face;
1613 if (csi->fs.fsCsb[0])
1615 csi->fs.fsCsb[0] = 0;
1616 if ((face = find_any_face( lf, csi->fs, can_use_bitmap, want_vertical ))) return face;
1618 if (want_vertical && (face = find_any_face( lf, csi->fs, can_use_bitmap, FALSE ))) return face;
1619 return NULL;
1622 /* realized font objects */
1624 #define FIRST_FONT_HANDLE 1
1625 #define MAX_FONT_HANDLES 256
1627 struct font_handle_entry
1629 struct gdi_font *font;
1630 WORD generation; /* generation count for reusing handle values */
1633 static struct font_handle_entry font_handles[MAX_FONT_HANDLES];
1634 static struct font_handle_entry *next_free;
1635 static struct font_handle_entry *next_unused = font_handles;
1637 static struct font_handle_entry *handle_entry( DWORD handle )
1639 unsigned int idx = LOWORD(handle) - FIRST_FONT_HANDLE;
1641 if (idx < MAX_FONT_HANDLES)
1643 if (!HIWORD( handle ) || HIWORD( handle ) == font_handles[idx].generation)
1644 return &font_handles[idx];
1646 if (handle) WARN( "invalid handle 0x%08x\n", handle );
1647 return NULL;
1650 static struct gdi_font *get_font_from_handle( DWORD handle )
1652 struct font_handle_entry *entry = handle_entry( handle );
1654 if (entry) return entry->font;
1655 SetLastError( ERROR_INVALID_PARAMETER );
1656 return NULL;
1659 static DWORD alloc_font_handle( struct gdi_font *font )
1661 struct font_handle_entry *entry;
1663 entry = next_free;
1664 if (entry)
1665 next_free = (struct font_handle_entry *)entry->font;
1666 else if (next_unused < font_handles + MAX_FONT_HANDLES)
1667 entry = next_unused++;
1668 else
1670 ERR( "out of realized font handles\n" );
1671 return 0;
1673 entry->font = font;
1674 if (++entry->generation == 0xffff) entry->generation = 1;
1675 return MAKELONG( entry - font_handles + FIRST_FONT_HANDLE, entry->generation );
1678 static void free_font_handle( DWORD handle )
1680 struct font_handle_entry *entry;
1682 if ((entry = handle_entry( handle )))
1684 entry->font = (struct gdi_font *)next_free;
1685 next_free = entry;
1689 static struct gdi_font *alloc_gdi_font( const WCHAR *file, void *data_ptr, SIZE_T data_size )
1691 UINT len = file ? lstrlenW(file) : 0;
1692 struct gdi_font *font = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1693 offsetof( struct gdi_font, file[len + 1] ));
1695 font->refcount = 1;
1696 font->matrix.eM11 = font->matrix.eM22 = 1.0;
1697 font->scale_y = 1;
1698 font->kern_count = -1;
1699 list_init( &font->child_fonts );
1701 if (file)
1703 WIN32_FILE_ATTRIBUTE_DATA info;
1704 if (GetFileAttributesExW( file, GetFileExInfoStandard, &info ))
1706 font->writetime = info.ftLastWriteTime;
1707 font->data_size = (LONGLONG)info.nFileSizeHigh << 32 | info.nFileSizeLow;
1708 memcpy( font->file, file, len * sizeof(WCHAR) );
1711 else
1713 font->data_ptr = data_ptr;
1714 font->data_size = data_size;
1717 font->handle = alloc_font_handle( font );
1718 return font;
1721 static void free_gdi_font( struct gdi_font *font )
1723 DWORD i;
1724 struct gdi_font *child, *child_next;
1726 if (font->private) font_funcs->destroy_font( font );
1727 free_font_handle( font->handle );
1728 LIST_FOR_EACH_ENTRY_SAFE( child, child_next, &font->child_fonts, struct gdi_font, entry )
1730 list_remove( &child->entry );
1731 free_gdi_font( child );
1733 for (i = 0; i < font->gm_size; i++) HeapFree( GetProcessHeap(), 0, font->gm[i] );
1734 HeapFree( GetProcessHeap(), 0, font->otm.otmpFamilyName );
1735 HeapFree( GetProcessHeap(), 0, font->otm.otmpStyleName );
1736 HeapFree( GetProcessHeap(), 0, font->otm.otmpFaceName );
1737 HeapFree( GetProcessHeap(), 0, font->otm.otmpFullName );
1738 HeapFree( GetProcessHeap(), 0, font->gm );
1739 HeapFree( GetProcessHeap(), 0, font->kern_pairs );
1740 HeapFree( GetProcessHeap(), 0, font->gsub_table );
1741 HeapFree( GetProcessHeap(), 0, font );
1744 static inline const WCHAR *get_gdi_font_name( struct gdi_font *font )
1746 return (WCHAR *)font->otm.otmpFamilyName;
1749 static struct gdi_font *create_gdi_font( const struct gdi_font_face *face, const WCHAR *family_name,
1750 const LOGFONTW *lf )
1752 struct gdi_font *font;
1754 if (!(font = alloc_gdi_font( face->file, face->data_ptr, face->data_size ))) return NULL;
1755 font->fs = face->fs;
1756 font->lf = *lf;
1757 font->fake_italic = (lf->lfItalic && !(face->ntmFlags & NTM_ITALIC));
1758 font->fake_bold = (lf->lfWeight > 550 && !(face->ntmFlags & NTM_BOLD));
1759 font->scalable = face->scalable;
1760 font->face_index = face->face_index;
1761 font->ntmFlags = face->ntmFlags;
1762 font->aa_flags = HIWORD( face->flags );
1763 if (!family_name) family_name = face->family->family_name;
1764 font->otm.otmpFamilyName = (char *)strdupW( family_name );
1765 font->otm.otmpStyleName = (char *)strdupW( face->style_name );
1766 font->otm.otmpFaceName = (char *)strdupW( face->full_name );
1767 return font;
1770 struct glyph_metrics
1772 GLYPHMETRICS gm;
1773 ABC abc; /* metrics of the unrotated char */
1774 BOOL init;
1777 #define GM_BLOCK_SIZE 128
1779 /* TODO: GGO format support */
1780 static BOOL get_gdi_font_glyph_metrics( struct gdi_font *font, UINT index, GLYPHMETRICS *gm, ABC *abc )
1782 UINT block = index / GM_BLOCK_SIZE;
1783 UINT entry = index % GM_BLOCK_SIZE;
1785 if (block < font->gm_size && font->gm[block] && font->gm[block][entry].init)
1787 *gm = font->gm[block][entry].gm;
1788 *abc = font->gm[block][entry].abc;
1790 TRACE( "cached gm: %u, %u, %s, %d, %d abc: %d, %u, %d\n",
1791 gm->gmBlackBoxX, gm->gmBlackBoxY, wine_dbgstr_point( &gm->gmptGlyphOrigin ),
1792 gm->gmCellIncX, gm->gmCellIncY, abc->abcA, abc->abcB, abc->abcC );
1793 return TRUE;
1796 return FALSE;
1799 static void set_gdi_font_glyph_metrics( struct gdi_font *font, UINT index,
1800 const GLYPHMETRICS *gm, const ABC *abc )
1802 UINT block = index / GM_BLOCK_SIZE;
1803 UINT entry = index % GM_BLOCK_SIZE;
1805 if (block >= font->gm_size)
1807 struct glyph_metrics **ptr;
1809 if (font->gm)
1810 ptr = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, font->gm, (block + 1) * sizeof(*ptr) );
1811 else
1812 ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, (block + 1) * sizeof(*ptr) );
1813 if (!ptr) return;
1814 font->gm_size = block + 1;
1815 font->gm = ptr;
1817 if (!font->gm[block])
1819 font->gm[block] = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**font->gm) * GM_BLOCK_SIZE );
1820 if (!font->gm[block]) return;
1822 font->gm[block][entry].gm = *gm;
1823 font->gm[block][entry].abc = *abc;
1824 font->gm[block][entry].init = TRUE;
1828 /* GSUB table support */
1830 typedef struct
1832 DWORD version;
1833 WORD ScriptList;
1834 WORD FeatureList;
1835 WORD LookupList;
1836 } GSUB_Header;
1838 typedef struct
1840 CHAR ScriptTag[4];
1841 WORD Script;
1842 } GSUB_ScriptRecord;
1844 typedef struct
1846 WORD ScriptCount;
1847 GSUB_ScriptRecord ScriptRecord[1];
1848 } GSUB_ScriptList;
1850 typedef struct
1852 CHAR LangSysTag[4];
1853 WORD LangSys;
1854 } GSUB_LangSysRecord;
1856 typedef struct
1858 WORD DefaultLangSys;
1859 WORD LangSysCount;
1860 GSUB_LangSysRecord LangSysRecord[1];
1861 } GSUB_Script;
1863 typedef struct
1865 WORD LookupOrder; /* Reserved */
1866 WORD ReqFeatureIndex;
1867 WORD FeatureCount;
1868 WORD FeatureIndex[1];
1869 } GSUB_LangSys;
1871 typedef struct
1873 CHAR FeatureTag[4];
1874 WORD Feature;
1875 } GSUB_FeatureRecord;
1877 typedef struct
1879 WORD FeatureCount;
1880 GSUB_FeatureRecord FeatureRecord[1];
1881 } GSUB_FeatureList;
1883 typedef struct
1885 WORD FeatureParams; /* Reserved */
1886 WORD LookupCount;
1887 WORD LookupListIndex[1];
1888 } GSUB_Feature;
1890 typedef struct
1892 WORD LookupCount;
1893 WORD Lookup[1];
1894 } GSUB_LookupList;
1896 typedef struct
1898 WORD LookupType;
1899 WORD LookupFlag;
1900 WORD SubTableCount;
1901 WORD SubTable[1];
1902 } GSUB_LookupTable;
1904 typedef struct
1906 WORD CoverageFormat;
1907 WORD GlyphCount;
1908 WORD GlyphArray[1];
1909 } GSUB_CoverageFormat1;
1911 typedef struct
1913 WORD Start;
1914 WORD End;
1915 WORD StartCoverageIndex;
1916 } GSUB_RangeRecord;
1918 typedef struct
1920 WORD CoverageFormat;
1921 WORD RangeCount;
1922 GSUB_RangeRecord RangeRecord[1];
1923 } GSUB_CoverageFormat2;
1925 typedef struct
1927 WORD SubstFormat; /* = 1 */
1928 WORD Coverage;
1929 WORD DeltaGlyphID;
1930 } GSUB_SingleSubstFormat1;
1932 typedef struct
1934 WORD SubstFormat; /* = 2 */
1935 WORD Coverage;
1936 WORD GlyphCount;
1937 WORD Substitute[1];
1938 } GSUB_SingleSubstFormat2;
1940 static GSUB_Script *GSUB_get_script_table( GSUB_Header *header, const char *tag )
1942 GSUB_ScriptList *script;
1943 GSUB_Script *deflt = NULL;
1944 int i;
1946 script = (GSUB_ScriptList *)((BYTE *)header + GET_BE_WORD(header->ScriptList));
1947 TRACE("%i scripts in this font\n", GET_BE_WORD(script->ScriptCount) );
1948 for (i = 0; i < GET_BE_WORD(script->ScriptCount); i++)
1950 int offset = GET_BE_WORD(script->ScriptRecord[i].Script);
1951 GSUB_Script *scr = (GSUB_Script *)((BYTE *)script + offset);
1952 if (!memcmp( script->ScriptRecord[i].ScriptTag, tag, 4 )) return scr;
1953 if (!memcmp( script->ScriptRecord[i].ScriptTag, "dflt", 4 )) deflt = scr;
1955 return deflt;
1958 static GSUB_LangSys *GSUB_get_lang_table( GSUB_Script *script, const char *tag )
1960 int i, offset;
1961 GSUB_LangSys *lang;
1963 TRACE("Deflang %x, LangCount %i\n",GET_BE_WORD(script->DefaultLangSys), GET_BE_WORD(script->LangSysCount));
1965 for (i = 0; i < GET_BE_WORD(script->LangSysCount) ; i++)
1967 offset = GET_BE_WORD(script->LangSysRecord[i].LangSys);
1968 lang = (GSUB_LangSys *)((BYTE *)script + offset);
1969 if (!memcmp( script->LangSysRecord[i].LangSysTag, tag, 4 )) return lang;
1971 offset = GET_BE_WORD(script->DefaultLangSys);
1972 if (offset) return (GSUB_LangSys *)((BYTE *)script + offset);
1973 return NULL;
1976 static GSUB_Feature *GSUB_get_feature( GSUB_Header *header, GSUB_LangSys *lang, const char *tag )
1978 int i;
1979 const GSUB_FeatureList *feature;
1981 feature = (GSUB_FeatureList *)((BYTE *)header + GET_BE_WORD(header->FeatureList));
1982 TRACE("%i features\n",GET_BE_WORD(lang->FeatureCount));
1983 for (i = 0; i < GET_BE_WORD(lang->FeatureCount); i++)
1985 int index = GET_BE_WORD(lang->FeatureIndex[i]);
1986 if (!memcmp( feature->FeatureRecord[index].FeatureTag, tag, 4 ))
1987 return (GSUB_Feature *)((BYTE *)feature + GET_BE_WORD(feature->FeatureRecord[index].Feature));
1989 return NULL;
1992 static const char *get_opentype_script( const struct gdi_font *font )
1995 * I am not sure if this is the correct way to generate our script tag
1997 switch (font->charset)
1999 case ANSI_CHARSET: return "latn";
2000 case BALTIC_CHARSET: return "latn"; /* ?? */
2001 case CHINESEBIG5_CHARSET: return "hani";
2002 case EASTEUROPE_CHARSET: return "latn"; /* ?? */
2003 case GB2312_CHARSET: return "hani";
2004 case GREEK_CHARSET: return "grek";
2005 case HANGUL_CHARSET: return "hang";
2006 case RUSSIAN_CHARSET: return "cyrl";
2007 case SHIFTJIS_CHARSET: return "kana";
2008 case TURKISH_CHARSET: return "latn"; /* ?? */
2009 case VIETNAMESE_CHARSET: return "latn";
2010 case JOHAB_CHARSET: return "latn"; /* ?? */
2011 case ARABIC_CHARSET: return "arab";
2012 case HEBREW_CHARSET: return "hebr";
2013 case THAI_CHARSET: return "thai";
2014 default: return "latn";
2018 static void *get_GSUB_vert_feature( struct gdi_font *font )
2020 GSUB_Header *header;
2021 GSUB_Script *script;
2022 GSUB_LangSys *language;
2023 GSUB_Feature *feature;
2024 DWORD length = font_funcs->get_font_data( font, MS_GSUB_TAG, 0, NULL, 0 );
2026 if (length == GDI_ERROR) return NULL;
2028 header = HeapAlloc( GetProcessHeap(), 0, length );
2029 font_funcs->get_font_data( font, MS_GSUB_TAG, 0, header, length );
2030 TRACE( "Loaded GSUB table of %i bytes\n", length );
2032 if ((script = GSUB_get_script_table( header, get_opentype_script(font) )))
2034 if ((language = GSUB_get_lang_table( script, "xxxx" ))) /* Need to get Lang tag */
2036 feature = GSUB_get_feature( header, language, "vrt2" );
2037 if (!feature) feature = GSUB_get_feature( header, language, "vert" );
2038 if (feature)
2040 font->gsub_table = header;
2041 return feature;
2043 TRACE("vrt2/vert feature not found\n");
2045 else TRACE("Language not found\n");
2047 else TRACE("Script not found\n");
2049 HeapFree( GetProcessHeap(), 0, header );
2050 return NULL;
2053 static int GSUB_is_glyph_covered( void *table, UINT glyph )
2055 GSUB_CoverageFormat1 *cf1 = table;
2057 if (GET_BE_WORD(cf1->CoverageFormat) == 1)
2059 int i, count = GET_BE_WORD(cf1->GlyphCount);
2061 TRACE("Coverage Format 1, %i glyphs\n",count);
2062 for (i = 0; i < count; i++) if (glyph == GET_BE_WORD(cf1->GlyphArray[i])) return i;
2063 return -1;
2065 else if (GET_BE_WORD(cf1->CoverageFormat) == 2)
2067 int i, count;
2068 GSUB_CoverageFormat2 *cf2 = table;
2070 count = GET_BE_WORD(cf2->RangeCount);
2071 TRACE("Coverage Format 2, %i ranges\n",count);
2072 for (i = 0; i < count; i++)
2074 if (glyph < GET_BE_WORD(cf2->RangeRecord[i].Start)) return -1;
2075 if ((glyph >= GET_BE_WORD(cf2->RangeRecord[i].Start)) &&
2076 (glyph <= GET_BE_WORD(cf2->RangeRecord[i].End)))
2078 return (GET_BE_WORD(cf2->RangeRecord[i].StartCoverageIndex) +
2079 glyph - GET_BE_WORD(cf2->RangeRecord[i].Start));
2082 return -1;
2084 else ERR("Unknown CoverageFormat %i\n",GET_BE_WORD(cf1->CoverageFormat));
2086 return -1;
2089 static UINT GSUB_apply_feature( GSUB_Header *header, GSUB_Feature *feature, UINT glyph )
2091 GSUB_LookupList *lookup = (GSUB_LookupList *)((BYTE *)header + GET_BE_WORD(header->LookupList));
2092 int i, j, offset;
2094 TRACE("%i lookups\n", GET_BE_WORD(feature->LookupCount));
2095 for (i = 0; i < GET_BE_WORD(feature->LookupCount); i++)
2097 GSUB_LookupTable *look;
2098 offset = GET_BE_WORD(lookup->Lookup[GET_BE_WORD(feature->LookupListIndex[i])]);
2099 look = (GSUB_LookupTable *)((BYTE *)lookup + offset);
2100 TRACE("type %i, flag %x, subtables %i\n",
2101 GET_BE_WORD(look->LookupType),GET_BE_WORD(look->LookupFlag),GET_BE_WORD(look->SubTableCount));
2102 if (GET_BE_WORD(look->LookupType) == 1)
2104 for (j = 0; j < GET_BE_WORD(look->SubTableCount); j++)
2106 GSUB_SingleSubstFormat1 *ssf1;
2107 offset = GET_BE_WORD(look->SubTable[j]);
2108 ssf1 = (GSUB_SingleSubstFormat1 *)((BYTE *)look + offset);
2109 if (GET_BE_WORD(ssf1->SubstFormat) == 1)
2111 int offset = GET_BE_WORD(ssf1->Coverage);
2112 TRACE(" subtype 1, delta %i\n", GET_BE_WORD(ssf1->DeltaGlyphID));
2113 if (GSUB_is_glyph_covered( (BYTE *) ssf1 + offset, glyph ) != -1)
2115 TRACE(" Glyph 0x%x ->",glyph);
2116 glyph += GET_BE_WORD(ssf1->DeltaGlyphID);
2117 TRACE(" 0x%x\n",glyph);
2120 else
2122 GSUB_SingleSubstFormat2 *ssf2;
2123 int index, offset;
2125 ssf2 = (GSUB_SingleSubstFormat2 *)ssf1;
2126 offset = GET_BE_WORD(ssf1->Coverage);
2127 TRACE(" subtype 2, glyph count %i\n", GET_BE_WORD(ssf2->GlyphCount));
2128 index = GSUB_is_glyph_covered( (BYTE *)ssf2 + offset, glyph );
2129 TRACE(" Coverage index %i\n",index);
2130 if (index != -1)
2132 TRACE(" Glyph is 0x%x ->",glyph);
2133 glyph = GET_BE_WORD(ssf2->Substitute[index]);
2134 TRACE("0x%x\n",glyph);
2139 else FIXME("We only handle SubType 1\n");
2141 return glyph;
2144 static UINT get_GSUB_vert_glyph( struct gdi_font *font, UINT glyph )
2146 if (!glyph) return glyph;
2147 if (!font->gsub_table) return glyph;
2148 return GSUB_apply_feature( font->gsub_table, font->vert_feature, glyph );
2151 static void add_child_font( struct gdi_font *font, const WCHAR *family_name )
2153 FONTSIGNATURE fs = {{0}};
2154 struct gdi_font *child;
2155 struct gdi_font_face *face;
2157 if (!(face = find_matching_face_by_name( family_name, NULL, &font->lf, fs, FALSE ))) return;
2159 if (!(child = create_gdi_font( face, family_name, &font->lf ))) return;
2160 child->matrix = font->matrix;
2161 child->can_use_bitmap = font->can_use_bitmap;
2162 child->scale_y = font->scale_y;
2163 child->aveWidth = font->aveWidth;
2164 child->charset = font->charset;
2165 child->codepage = font->codepage;
2166 child->base_font = font;
2167 list_add_tail( &font->child_fonts, &child->entry );
2168 TRACE( "created child font %p for base %p\n", child, font );
2171 static void create_child_font_list( struct gdi_font *font )
2173 struct gdi_font_link *font_link;
2174 struct gdi_font_link_entry *entry;
2175 const WCHAR* font_name;
2177 if (!(font_name = get_gdi_font_subst( get_gdi_font_name(font), -1, NULL )))
2178 font_name = get_gdi_font_name( font );
2180 if ((font_link = find_gdi_font_link( font_name )))
2182 TRACE("found entry in system list\n");
2183 LIST_FOR_EACH_ENTRY( entry, &font_link->links, struct gdi_font_link_entry, entry )
2184 add_child_font( font, entry->family_name );
2187 * if not SYMBOL or OEM then we also get all the fonts for Microsoft
2188 * Sans Serif. This is how asian windows get default fallbacks for fonts
2190 if (is_dbcs_ansi_cp(GetACP()) && font->charset != SYMBOL_CHARSET && font->charset != OEM_CHARSET &&
2191 facename_compare( font_name, L"Microsoft Sans Serif", -1 ) != 0)
2193 if ((font_link = find_gdi_font_link( L"Microsoft Sans Serif" )))
2195 TRACE("found entry in default fallback list\n");
2196 LIST_FOR_EACH_ENTRY( entry, &font_link->links, struct gdi_font_link_entry, entry )
2197 add_child_font( font, entry->family_name );
2202 /* font cache */
2204 static struct list gdi_font_list = LIST_INIT( gdi_font_list );
2205 static struct list unused_gdi_font_list = LIST_INIT( unused_gdi_font_list );
2206 static unsigned int unused_font_count;
2207 #define UNUSED_CACHE_SIZE 10
2209 static BOOL fontcmp( const struct gdi_font *font, DWORD hash, const LOGFONTW *lf,
2210 const FMAT2 *matrix, BOOL can_use_bitmap )
2212 if (font->hash != hash) return TRUE;
2213 if (memcmp( &font->matrix, matrix, sizeof(*matrix))) return TRUE;
2214 if (memcmp( &font->lf, lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
2215 if (!font->can_use_bitmap != !can_use_bitmap) return TRUE;
2216 return facename_compare( font->lf.lfFaceName, lf->lfFaceName, -1 );
2219 static DWORD hash_font( const LOGFONTW *lf, const FMAT2 *matrix, BOOL can_use_bitmap )
2221 DWORD hash = 0, *ptr, two_chars;
2222 WORD *pwc;
2223 unsigned int i;
2225 for (i = 0, ptr = (DWORD *)matrix; i < sizeof(*matrix) / sizeof(DWORD); i++, ptr++)
2226 hash ^= *ptr;
2227 for(i = 0, ptr = (DWORD *)lf; i < 7; i++, ptr++)
2228 hash ^= *ptr;
2229 for(i = 0, ptr = (DWORD *)lf->lfFaceName; i < LF_FACESIZE/2; i++, ptr++)
2231 two_chars = *ptr;
2232 pwc = (WCHAR *)&two_chars;
2233 if(!*pwc) break;
2234 *pwc = towupper(*pwc);
2235 pwc++;
2236 *pwc = towupper(*pwc);
2237 hash ^= two_chars;
2238 if(!*pwc) break;
2240 hash ^= !can_use_bitmap;
2241 return hash;
2244 static void cache_gdi_font( struct gdi_font *font )
2246 static DWORD cache_num = 1;
2248 font->cache_num = cache_num++;
2249 font->hash = hash_font( &font->lf, &font->matrix, font->can_use_bitmap );
2250 list_add_head( &gdi_font_list, &font->entry );
2251 TRACE( "font %p\n", font );
2254 static struct gdi_font *find_cached_gdi_font( const LOGFONTW *lf, const FMAT2 *matrix, BOOL can_use_bitmap )
2256 struct gdi_font *font;
2257 DWORD hash = hash_font( lf, matrix, can_use_bitmap );
2259 /* try the in-use list */
2260 LIST_FOR_EACH_ENTRY( font, &gdi_font_list, struct gdi_font, entry )
2262 if (fontcmp( font, hash, lf, matrix, can_use_bitmap )) continue;
2263 list_remove( &font->entry );
2264 list_add_head( &gdi_font_list, &font->entry );
2265 if (!font->refcount++)
2267 list_remove( &font->unused_entry );
2268 unused_font_count--;
2270 return font;
2272 return NULL;
2275 static void release_gdi_font( struct gdi_font *font )
2277 if (!font) return;
2278 if (--font->refcount) return;
2280 TRACE( "font %p\n", font );
2282 /* add it to the unused list */
2283 EnterCriticalSection( &font_cs );
2284 list_add_head( &unused_gdi_font_list, &font->unused_entry );
2285 if (unused_font_count > UNUSED_CACHE_SIZE)
2287 font = LIST_ENTRY( list_tail( &unused_gdi_font_list ), struct gdi_font, unused_entry );
2288 TRACE( "freeing %p\n", font );
2289 list_remove( &font->entry );
2290 list_remove( &font->unused_entry );
2291 free_gdi_font( font );
2293 else unused_font_count++;
2294 LeaveCriticalSection( &font_cs );
2297 static void add_font_list(HKEY hkey, const struct nls_update_font_list *fl, int dpi)
2299 const char *sserif = (dpi <= 108) ? fl->sserif_96 : fl->sserif_120;
2301 RegSetValueExA(hkey, "Courier", 0, REG_SZ, (const BYTE *)fl->courier, strlen(fl->courier)+1);
2302 RegSetValueExA(hkey, "MS Serif", 0, REG_SZ, (const BYTE *)fl->serif, strlen(fl->serif)+1);
2303 RegSetValueExA(hkey, "MS Sans Serif", 0, REG_SZ, (const BYTE *)sserif, strlen(sserif)+1);
2304 RegSetValueExA(hkey, "Small Fonts", 0, REG_SZ, (const BYTE *)fl->small, strlen(fl->small)+1);
2307 static void set_value_key(HKEY hkey, const char *name, const char *value)
2309 if (value)
2310 RegSetValueExA(hkey, name, 0, REG_SZ, (const BYTE *)value, strlen(value) + 1);
2311 else if (name)
2312 RegDeleteValueA(hkey, name);
2315 static void update_font_association_info(UINT current_ansi_codepage)
2317 if (is_dbcs_ansi_cp(current_ansi_codepage))
2319 HKEY hkey;
2320 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\FontAssoc", &hkey) == ERROR_SUCCESS)
2322 HKEY hsubkey;
2323 if (RegCreateKeyW(hkey, L"Associated Charset", &hsubkey) == ERROR_SUCCESS)
2325 switch (current_ansi_codepage)
2327 case 932:
2328 set_value_key(hsubkey, "ANSI(00)", "NO");
2329 set_value_key(hsubkey, "OEM(FF)", "NO");
2330 set_value_key(hsubkey, "SYMBOL(02)", "NO");
2331 break;
2332 case 936:
2333 case 949:
2334 case 950:
2335 set_value_key(hsubkey, "ANSI(00)", "YES");
2336 set_value_key(hsubkey, "OEM(FF)", "YES");
2337 set_value_key(hsubkey, "SYMBOL(02)", "NO");
2338 break;
2340 RegCloseKey(hsubkey);
2343 /* TODO: Associated DefaultFonts */
2345 RegCloseKey(hkey);
2348 else
2349 RegDeleteTreeW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\FontAssoc");
2352 static void set_multi_value_key(HKEY hkey, const WCHAR *name, const WCHAR *value, DWORD len)
2354 if (value)
2355 RegSetValueExW(hkey, name, 0, REG_MULTI_SZ, (const BYTE *)value, len);
2356 else if (name)
2357 RegDeleteValueW(hkey, name);
2360 static void update_font_system_link_info(UINT current_ansi_codepage)
2362 static const WCHAR system_link_simplified_chinese[] =
2363 L"SIMSUN.TTC,SimSun\0"
2364 L"MINGLIU.TTC,PMingLiu\0"
2365 L"MSGOTHIC.TTC,MS UI Gothic\0"
2366 L"BATANG.TTC,Batang\0";
2367 static const WCHAR system_link_traditional_chinese[] =
2368 L"MINGLIU.TTC,PMingLiu\0"
2369 L"SIMSUN.TTC,SimSun\0"
2370 L"MSGOTHIC.TTC,MS UI Gothic\0"
2371 L"BATANG.TTC,Batang\0";
2372 static const WCHAR system_link_japanese[] =
2373 L"MSGOTHIC.TTC,MS UI Gothic\0"
2374 L"MINGLIU.TTC,PMingLiU\0"
2375 L"SIMSUN.TTC,SimSun\0"
2376 L"GULIM.TTC,Gulim\0";
2377 static const WCHAR system_link_korean[] =
2378 L"GULIM.TTC,Gulim\0"
2379 L"MSGOTHIC.TTC,MS UI Gothic\0"
2380 L"MINGLIU.TTC,PMingLiU\0"
2381 L"SIMSUN.TTC,SimSun\0";
2382 static const WCHAR system_link_non_cjk[] =
2383 L"MSGOTHIC.TTC,MS UI Gothic\0"
2384 L"MINGLIU.TTC,PMingLiU\0"
2385 L"SIMSUN.TTC,SimSun\0"
2386 L"GULIM.TTC,Gulim\0";
2387 HKEY hkey;
2389 if (!RegCreateKeyW(HKEY_LOCAL_MACHINE,
2390 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", &hkey))
2392 const WCHAR *link;
2393 DWORD len;
2395 switch (current_ansi_codepage)
2397 case 932:
2398 link = system_link_japanese;
2399 len = sizeof(system_link_japanese);
2400 break;
2401 case 936:
2402 link = system_link_simplified_chinese;
2403 len = sizeof(system_link_simplified_chinese);
2404 break;
2405 case 949:
2406 link = system_link_korean;
2407 len = sizeof(system_link_korean);
2408 break;
2409 case 950:
2410 link = system_link_traditional_chinese;
2411 len = sizeof(system_link_traditional_chinese);
2412 break;
2413 default:
2414 link = system_link_non_cjk;
2415 len = sizeof(system_link_non_cjk);
2417 set_multi_value_key(hkey, L"Lucida Sans Unicode", link, len);
2418 set_multi_value_key(hkey, L"Microsoft Sans Serif", link, len);
2419 set_multi_value_key(hkey, L"Tahoma", link, len);
2420 RegCloseKey(hkey);
2424 static void update_codepage(void)
2426 char buf[40], cpbuf[40];
2427 HKEY hkey;
2428 DWORD len, type, size;
2429 UINT i, ansi_cp, oem_cp;
2430 DWORD screen_dpi, font_dpi = 0;
2431 BOOL done = FALSE;
2433 screen_dpi = get_dpi();
2434 if (!screen_dpi) screen_dpi = 96;
2436 size = sizeof(DWORD);
2437 if (RegQueryValueExW(wine_fonts_key, L"LogPixels", NULL, &type, (BYTE *)&font_dpi, &size) ||
2438 type != REG_DWORD || size != sizeof(DWORD))
2439 font_dpi = 0;
2441 ansi_cp = GetACP();
2442 oem_cp = GetOEMCP();
2443 sprintf( cpbuf, "%u,%u", ansi_cp, oem_cp );
2445 buf[0] = 0;
2446 len = sizeof(buf);
2447 if (!RegQueryValueExA(wine_fonts_key, "Codepages", 0, &type, (BYTE *)buf, &len) && type == REG_SZ)
2449 if (!strcmp( buf, cpbuf ) && screen_dpi == font_dpi) return; /* already set correctly */
2450 TRACE("updating registry, codepages/logpixels changed %s/%u -> %u,%u/%u\n",
2451 buf, font_dpi, ansi_cp, oem_cp, screen_dpi);
2453 else TRACE("updating registry, codepages/logpixels changed none -> %u,%u/%u\n",
2454 ansi_cp, oem_cp, screen_dpi);
2456 RegSetValueExA(wine_fonts_key, "Codepages", 0, REG_SZ, (const BYTE *)cpbuf, strlen(cpbuf)+1);
2457 RegSetValueExW(wine_fonts_key, L"LogPixels", 0, REG_DWORD, (const BYTE *)&screen_dpi, sizeof(screen_dpi));
2459 for (i = 0; i < ARRAY_SIZE(nls_update_font_list); i++)
2461 if (nls_update_font_list[i].ansi_cp == ansi_cp && nls_update_font_list[i].oem_cp == oem_cp)
2463 if (!RegCreateKeyW( HKEY_CURRENT_CONFIG, L"Software\\Fonts", &hkey ))
2465 RegSetValueExA(hkey, "OEMFONT.FON", 0, REG_SZ, (const BYTE *)nls_update_font_list[i].oem,
2466 strlen(nls_update_font_list[i].oem)+1);
2467 RegSetValueExA(hkey, "FIXEDFON.FON", 0, REG_SZ, (const BYTE *)nls_update_font_list[i].fixed,
2468 strlen(nls_update_font_list[i].fixed)+1);
2469 RegSetValueExA(hkey, "FONTS.FON", 0, REG_SZ, (const BYTE *)nls_update_font_list[i].system,
2470 strlen(nls_update_font_list[i].system)+1);
2471 RegCloseKey(hkey);
2473 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE,
2474 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", &hkey ))
2476 add_font_list(hkey, &nls_update_font_list[i], screen_dpi);
2477 RegCloseKey(hkey);
2479 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE,
2480 L"Software\\Microsoft\\Windows\\CurrentVersion\\Fonts", &hkey ))
2482 add_font_list(hkey, &nls_update_font_list[i], screen_dpi);
2483 RegCloseKey(hkey);
2485 /* Only update these if the Codepage changed. */
2486 if (strcmp( buf, cpbuf ) && !RegCreateKeyW( HKEY_LOCAL_MACHINE,
2487 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes", &hkey ))
2489 RegSetValueExA(hkey, "MS Shell Dlg", 0, REG_SZ, (const BYTE *)nls_update_font_list[i].shelldlg,
2490 strlen(nls_update_font_list[i].shelldlg)+1);
2491 RegSetValueExA(hkey, "Tms Rmn", 0, REG_SZ, (const BYTE *)nls_update_font_list[i].tmsrmn,
2492 strlen(nls_update_font_list[i].tmsrmn)+1);
2494 set_value_key(hkey, "Fixedsys,0", nls_update_font_list[i].fixed_0);
2495 set_value_key(hkey, "System,0", nls_update_font_list[i].system_0);
2496 set_value_key(hkey, "Courier,0", nls_update_font_list[i].courier_0);
2497 set_value_key(hkey, "MS Serif,0", nls_update_font_list[i].serif_0);
2498 set_value_key(hkey, "Small Fonts,0", nls_update_font_list[i].small_0);
2499 set_value_key(hkey, "MS Sans Serif,0", nls_update_font_list[i].sserif_0);
2500 set_value_key(hkey, "Helv,0", nls_update_font_list[i].helv_0);
2501 set_value_key(hkey, "Tms Rmn,0", nls_update_font_list[i].tmsrmn_0);
2503 set_value_key(hkey, nls_update_font_list[i].arial_0.from, nls_update_font_list[i].arial_0.to);
2504 set_value_key(hkey, nls_update_font_list[i].courier_new_0.from, nls_update_font_list[i].courier_new_0.to);
2505 set_value_key(hkey, nls_update_font_list[i].times_new_roman_0.from, nls_update_font_list[i].times_new_roman_0.to);
2507 RegCloseKey(hkey);
2509 done = TRUE;
2511 else
2513 /* Delete the FontSubstitutes from other locales */
2514 if (!RegCreateKeyW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes", &hkey ))
2516 set_value_key(hkey, nls_update_font_list[i].arial_0.from, NULL);
2517 set_value_key(hkey, nls_update_font_list[i].courier_new_0.from, NULL);
2518 set_value_key(hkey, nls_update_font_list[i].times_new_roman_0.from, NULL);
2519 RegCloseKey(hkey);
2523 if (!done)
2524 FIXME("there is no font defaults for codepages %u,%u\n", ansi_cp, oem_cp);
2526 /* update locale dependent font association info and font system link info in registry.
2527 update only when codepages changed, not logpixels. */
2528 if (strcmp(buf, cpbuf) != 0)
2530 update_font_association_info(ansi_cp);
2531 update_font_system_link_info(ansi_cp);
2536 /*************************************************************
2537 * font_CreateDC
2539 static BOOL CDECL font_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
2540 LPCWSTR output, const DEVMODEW *devmode )
2542 struct font_physdev *physdev;
2544 if (!font_funcs) return TRUE;
2545 if (!(physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) ))) return FALSE;
2546 push_dc_driver( dev, &physdev->dev, &font_driver );
2547 return TRUE;
2551 /*************************************************************
2552 * font_DeleteDC
2554 static BOOL CDECL font_DeleteDC( PHYSDEV dev )
2556 struct font_physdev *physdev = get_font_dev( dev );
2558 release_gdi_font( physdev->font );
2559 HeapFree( GetProcessHeap(), 0, physdev );
2560 return TRUE;
2564 struct gdi_font_enum_data
2566 ENUMLOGFONTEXW elf;
2567 NEWTEXTMETRICEXW ntm;
2570 struct enum_charset
2572 DWORD mask;
2573 DWORD charset;
2574 DWORD script;
2577 static int load_script_name( UINT id, WCHAR buffer[LF_FACESIZE] )
2579 HRSRC rsrc;
2580 HGLOBAL hMem;
2581 WCHAR *p;
2582 int i;
2584 id += IDS_FIRST_SCRIPT;
2585 rsrc = FindResourceW( gdi32_module, (LPCWSTR)(ULONG_PTR)((id >> 4) + 1), (LPCWSTR)6 /*RT_STRING*/ );
2586 if (!rsrc) return 0;
2587 hMem = LoadResource( gdi32_module, rsrc );
2588 if (!hMem) return 0;
2590 p = LockResource( hMem );
2591 id &= 0x000f;
2592 while (id--) p += *p + 1;
2594 i = min(LF_FACESIZE - 1, *p);
2595 memcpy(buffer, p + 1, i * sizeof(WCHAR));
2596 buffer[i] = 0;
2597 return i;
2600 static BOOL is_complex_script_ansi_cp( UINT ansi_cp )
2602 return (ansi_cp == 874 /* Thai */
2603 || ansi_cp == 1255 /* Hebrew */
2604 || ansi_cp == 1256 /* Arabic */
2608 /***************************************************
2609 * create_enum_charset_list
2611 * This function creates charset enumeration list because in DEFAULT_CHARSET
2612 * case, the ANSI codepage's charset takes precedence over other charsets.
2613 * Above rule doesn't apply if the ANSI codepage uses complex script (e.g. Thai).
2614 * This function works as a filter other than DEFAULT_CHARSET case.
2616 static DWORD create_enum_charset_list(DWORD charset, struct enum_charset *list)
2618 struct enum_charset *start = list;
2619 CHARSETINFO csi;
2620 int i;
2622 if (TranslateCharsetInfo( ULongToPtr(charset), &csi, TCI_SRCCHARSET ) && csi.fs.fsCsb[0] != 0)
2624 list->mask = csi.fs.fsCsb[0];
2625 list->charset = csi.ciCharset;
2626 for (i = 0; i < 32; i++) if (csi.fs.fsCsb[0] & (1u << i)) list->script = i;
2627 list++;
2629 else /* charset is DEFAULT_CHARSET or invalid. */
2631 int acp = GetACP();
2632 DWORD mask = 0;
2634 /* Set the current codepage's charset as the first element. */
2635 if (!is_complex_script_ansi_cp(acp) &&
2636 TranslateCharsetInfo( (DWORD *)(INT_PTR)acp, &csi, TCI_SRCCODEPAGE ) &&
2637 csi.fs.fsCsb[0] != 0)
2639 list->mask = csi.fs.fsCsb[0];
2640 list->charset = csi.ciCharset;
2641 for (i = 0; i < 32; i++) if (csi.fs.fsCsb[0] & (1u << i)) list->script = i;
2642 mask |= csi.fs.fsCsb[0];
2643 list++;
2646 /* Fill out left elements. */
2647 for (i = 0; i < 32; i++)
2649 FONTSIGNATURE fs;
2650 fs.fsCsb[0] = 1u << i;
2651 fs.fsCsb[1] = 0;
2652 if (fs.fsCsb[0] & mask) continue; /* skip, already added. */
2653 if (!TranslateCharsetInfo( fs.fsCsb, &csi, TCI_SRCFONTSIG ))
2654 continue; /* skip, this is an invalid fsCsb bit. */
2655 list->mask = fs.fsCsb[0];
2656 list->charset = csi.ciCharset;
2657 list->script = i;
2658 mask |= fs.fsCsb[0];
2659 list++;
2661 /* add catch all mask for remaining bits */
2662 if (~mask)
2664 list->mask = ~mask;
2665 list->charset = DEFAULT_CHARSET;
2666 list->script = IDS_OTHER - IDS_FIRST_SCRIPT;
2667 list++;
2670 return list - start;
2673 static UINT get_font_type( const NEWTEXTMETRICEXW *ntm )
2675 UINT ret = 0;
2677 if (ntm->ntmTm.tmPitchAndFamily & TMPF_TRUETYPE) ret |= TRUETYPE_FONTTYPE;
2678 if (ntm->ntmTm.tmPitchAndFamily & TMPF_DEVICE) ret |= DEVICE_FONTTYPE;
2679 if (!(ntm->ntmTm.tmPitchAndFamily & TMPF_VECTOR)) ret |= RASTER_FONTTYPE;
2680 return ret;
2683 static BOOL get_face_enum_data( struct gdi_font_face *face, ENUMLOGFONTEXW *elf, NEWTEXTMETRICEXW *ntm )
2685 struct gdi_font *font;
2686 LOGFONTW lf = { .lfHeight = -4096 /* preferable EM Square size */ };
2688 if (!face->scalable) lf.lfHeight = 0;
2690 if (!(font = create_gdi_font( face, NULL, &lf ))) return FALSE;
2692 if (!font_funcs->load_font( font ))
2694 free_gdi_font( font );
2695 return FALSE;
2698 if (font->scalable && -lf.lfHeight % font->otm.otmEMSquare != 0)
2700 /* reload with the original EM Square size */
2701 lf.lfHeight = -font->otm.otmEMSquare;
2702 free_gdi_font( font );
2704 if (!(font = create_gdi_font( face, NULL, &lf ))) return FALSE;
2705 if (!font_funcs->load_font( font ))
2707 free_gdi_font( font );
2708 return FALSE;
2712 if (font_funcs->set_outline_text_metrics( font ))
2714 static const DWORD ntm_ppem = 32;
2715 UINT cell_height;
2717 #define TM font->otm.otmTextMetrics
2718 #define SCALE_NTM(value) (MulDiv( ntm->ntmTm.tmHeight, (value), TM.tmHeight ))
2719 cell_height = TM.tmHeight / ( -lf.lfHeight / font->otm.otmEMSquare );
2720 ntm->ntmTm.tmHeight = MulDiv( ntm_ppem, cell_height, font->otm.otmEMSquare );
2721 ntm->ntmTm.tmAscent = SCALE_NTM( TM.tmAscent );
2722 ntm->ntmTm.tmDescent = ntm->ntmTm.tmHeight - ntm->ntmTm.tmAscent;
2723 ntm->ntmTm.tmInternalLeading = SCALE_NTM( TM.tmInternalLeading );
2724 ntm->ntmTm.tmExternalLeading = SCALE_NTM( TM.tmExternalLeading );
2725 ntm->ntmTm.tmAveCharWidth = SCALE_NTM( TM.tmAveCharWidth );
2726 ntm->ntmTm.tmMaxCharWidth = SCALE_NTM( TM.tmMaxCharWidth );
2728 memcpy((char *)&ntm->ntmTm + offsetof( TEXTMETRICW, tmWeight ),
2729 (const char *)&TM + offsetof( TEXTMETRICW, tmWeight ),
2730 sizeof(TEXTMETRICW) - offsetof( TEXTMETRICW, tmWeight ));
2731 ntm->ntmTm.ntmSizeEM = font->otm.otmEMSquare;
2732 ntm->ntmTm.ntmCellHeight = cell_height;
2733 ntm->ntmTm.ntmAvgWidth = font->ntmAvgWidth;
2734 #undef SCALE_NTM
2735 #undef TM
2737 else if (font_funcs->set_bitmap_text_metrics( font ))
2739 memcpy( &ntm->ntmTm, &font->otm.otmTextMetrics, sizeof(TEXTMETRICW) );
2740 ntm->ntmTm.ntmSizeEM = ntm->ntmTm.tmHeight - ntm->ntmTm.tmInternalLeading;
2741 ntm->ntmTm.ntmCellHeight = ntm->ntmTm.tmHeight;
2742 ntm->ntmTm.ntmAvgWidth = ntm->ntmTm.tmAveCharWidth;
2744 ntm->ntmTm.ntmFlags = font->ntmFlags;
2745 ntm->ntmFontSig = font->fs;
2747 elf->elfLogFont.lfEscapement = 0;
2748 elf->elfLogFont.lfOrientation = 0;
2749 elf->elfLogFont.lfHeight = ntm->ntmTm.tmHeight;
2750 elf->elfLogFont.lfWidth = ntm->ntmTm.tmAveCharWidth;
2751 elf->elfLogFont.lfWeight = ntm->ntmTm.tmWeight;
2752 elf->elfLogFont.lfItalic = ntm->ntmTm.tmItalic;
2753 elf->elfLogFont.lfUnderline = ntm->ntmTm.tmUnderlined;
2754 elf->elfLogFont.lfStrikeOut = ntm->ntmTm.tmStruckOut;
2755 elf->elfLogFont.lfCharSet = ntm->ntmTm.tmCharSet;
2756 elf->elfLogFont.lfOutPrecision = OUT_STROKE_PRECIS;
2757 elf->elfLogFont.lfClipPrecision = CLIP_STROKE_PRECIS;
2758 elf->elfLogFont.lfQuality = DRAFT_QUALITY;
2759 elf->elfLogFont.lfPitchAndFamily = (ntm->ntmTm.tmPitchAndFamily & 0xf1) + 1;
2760 lstrcpynW( elf->elfLogFont.lfFaceName, (WCHAR *)font->otm.otmpFamilyName, LF_FACESIZE );
2761 lstrcpynW( elf->elfFullName, (WCHAR *)font->otm.otmpFaceName, LF_FULLFACESIZE );
2762 lstrcpynW( elf->elfStyle, (WCHAR *)font->otm.otmpStyleName, LF_FACESIZE );
2764 free_gdi_font( font );
2765 return TRUE;
2768 static BOOL family_matches( struct gdi_font_family *family, const WCHAR *face_name )
2770 struct gdi_font_face *face;
2772 if (!facename_compare( face_name, family->family_name, LF_FACESIZE - 1 )) return TRUE;
2773 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
2774 if (!facename_compare( face_name, face->full_name, LF_FACESIZE - 1 )) return TRUE;
2775 return FALSE;
2778 static BOOL face_matches( const WCHAR *family_name, struct gdi_font_face *face, const WCHAR *face_name )
2780 if (!facename_compare( face_name, family_name, LF_FACESIZE - 1)) return TRUE;
2781 return !facename_compare( face_name, face->full_name, LF_FACESIZE - 1 );
2784 static BOOL enum_face_charsets( const struct gdi_font_family *family, struct gdi_font_face *face,
2785 struct enum_charset *list, DWORD count, FONTENUMPROCW proc, LPARAM lparam,
2786 const WCHAR *subst )
2788 ENUMLOGFONTEXW elf;
2789 NEWTEXTMETRICEXW ntm;
2790 DWORD type, i;
2792 if (!face->cached_enum_data)
2794 struct gdi_font_enum_data *data;
2796 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )) ||
2797 !get_face_enum_data( face, &data->elf, &data->ntm ))
2799 HeapFree( GetProcessHeap(), 0, data );
2800 return TRUE;
2802 face->cached_enum_data = data;
2805 elf = face->cached_enum_data->elf;
2806 ntm = face->cached_enum_data->ntm;
2807 type = get_font_type( &ntm );
2809 /* font replacement */
2810 if (family != face->family)
2812 lstrcpynW( elf.elfLogFont.lfFaceName, family->family_name, LF_FACESIZE );
2813 lstrcpynW( elf.elfFullName, face->full_name, LF_FULLFACESIZE );
2815 if (subst) lstrcpynW( elf.elfLogFont.lfFaceName, subst, LF_FACESIZE );
2817 for (i = 0; i < count; i++)
2819 if (face->fs.fsCsb[0] == 0) /* OEM */
2821 elf.elfLogFont.lfCharSet = ntm.ntmTm.tmCharSet = OEM_CHARSET;
2822 load_script_name( IDS_OEM_DOS - IDS_FIRST_SCRIPT, elf.elfScript );
2823 i = count; /* break out of loop after enumeration */
2825 else
2827 if (!(face->fs.fsCsb[0] & list[i].mask)) continue;
2828 /* use the DEFAULT_CHARSET case only if no other charset is present */
2829 if (list[i].charset == DEFAULT_CHARSET && (face->fs.fsCsb[0] & ~list[i].mask)) continue;
2830 elf.elfLogFont.lfCharSet = ntm.ntmTm.tmCharSet = list[i].charset;
2831 load_script_name( list[i].script, elf.elfScript );
2832 if (!elf.elfScript[0]) FIXME("Unknown elfscript for id %u\n", list[i].script);
2834 TRACE( "face %s full %s style %s charset = %d type %d script %s it %d weight %d ntmflags %08x\n",
2835 debugstr_w(elf.elfLogFont.lfFaceName), debugstr_w(elf.elfFullName), debugstr_w(elf.elfStyle),
2836 elf.elfLogFont.lfCharSet, type, debugstr_w(elf.elfScript),
2837 elf.elfLogFont.lfItalic, elf.elfLogFont.lfWeight, ntm.ntmTm.ntmFlags );
2838 /* release section before callback (FIXME) */
2839 LeaveCriticalSection( &font_cs );
2840 if (!proc( &elf.elfLogFont, (TEXTMETRICW *)&ntm, type, lparam )) return FALSE;
2841 EnterCriticalSection( &font_cs );
2843 return TRUE;
2846 /*************************************************************
2847 * font_EnumFonts
2849 static BOOL CDECL font_EnumFonts( PHYSDEV dev, LOGFONTW *lf, FONTENUMPROCW proc, LPARAM lparam )
2851 struct gdi_font_family *family;
2852 struct gdi_font_face *face;
2853 struct enum_charset enum_charsets[32];
2854 DWORD count, charset;
2856 charset = lf ? lf->lfCharSet : DEFAULT_CHARSET;
2858 count = create_enum_charset_list( charset, enum_charsets );
2860 EnterCriticalSection( &font_cs );
2862 if (lf && lf->lfFaceName[0])
2864 const WCHAR *face_name = get_gdi_font_subst( lf->lfFaceName, charset, NULL );
2865 const WCHAR *orig_name = NULL;
2867 TRACE( "facename = %s charset %d\n", debugstr_w(lf->lfFaceName), charset );
2868 if (face_name)
2870 orig_name = lf->lfFaceName;
2871 TRACE( "substituting %s -> %s\n", debugstr_w(lf->lfFaceName), debugstr_w(face_name) );
2873 else face_name = lf->lfFaceName;
2875 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
2877 if (!family_matches(family, face_name)) continue;
2878 LIST_FOR_EACH_ENTRY( face, get_family_face_list(family), struct gdi_font_face, entry )
2880 if (!face_matches( family->family_name, face, face_name )) continue;
2881 if (!enum_face_charsets( family, face, enum_charsets, count, proc, lparam, orig_name ))
2882 return FALSE;
2886 else
2888 TRACE( "charset %d\n", charset );
2889 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
2891 face = LIST_ENTRY( list_head(get_family_face_list(family)), struct gdi_font_face, entry );
2892 if (!enum_face_charsets( family, face, enum_charsets, count, proc, lparam, NULL ))
2893 return FALSE;
2896 LeaveCriticalSection( &font_cs );
2897 return TRUE;
2901 static BOOL check_unicode_tategaki( WCHAR ch )
2903 extern const unsigned short vertical_orientation_table[] DECLSPEC_HIDDEN;
2904 unsigned short orientation = vertical_orientation_table[vertical_orientation_table[vertical_orientation_table[ch >> 8]+((ch >> 4) & 0x0f)]+ (ch & 0xf)];
2906 /* We only reach this code if typographical substitution did not occur */
2907 /* Type: U or Type: Tu */
2908 return (orientation == 1 || orientation == 3);
2911 static UINT get_glyph_index_symbol( struct gdi_font *font, UINT glyph )
2913 UINT index;
2915 if (glyph < 0x100) glyph += 0xf000;
2916 /* there are a number of old pre-Unicode "broken" TTFs, which
2917 do have symbols at U+00XX instead of U+f0XX */
2918 index = glyph;
2919 font_funcs->get_glyph_index( font, &index, FALSE );
2920 if (!index)
2922 index = glyph - 0xf000;
2923 font_funcs->get_glyph_index( font, &index, FALSE );
2925 return index;
2928 static UINT get_glyph_index( struct gdi_font *font, UINT glyph )
2930 WCHAR wc = glyph;
2931 char ch;
2932 BOOL used;
2934 if (font_funcs->get_glyph_index( font, &glyph, TRUE )) return glyph;
2936 if (font->codepage == CP_SYMBOL)
2938 glyph = get_glyph_index_symbol( font, wc );
2939 if (!glyph)
2941 if (WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, &wc, 1, &ch, 1, NULL, NULL ))
2942 glyph = get_glyph_index_symbol( font, (unsigned char)ch );
2945 else if (WideCharToMultiByte( font->codepage, WC_NO_BEST_FIT_CHARS, &wc, 1, &ch, 1, NULL, &used ) && !used)
2947 glyph = (unsigned char)ch;
2948 font_funcs->get_glyph_index( font, &glyph, FALSE );
2950 else return 0;
2952 return glyph;
2955 static UINT get_glyph_index_linked( struct gdi_font **font, UINT glyph )
2957 struct gdi_font *child;
2958 UINT res;
2960 if ((res = get_glyph_index( *font, glyph ))) return res;
2961 if (glyph < 32) return 0; /* don't check linked fonts for control characters */
2963 LIST_FOR_EACH_ENTRY( child, &(*font)->child_fonts, struct gdi_font, entry )
2965 if (!child->private && !font_funcs->load_font( child )) continue;
2966 if ((res = get_glyph_index( child, glyph )))
2968 *font = child;
2969 return res;
2972 return 0;
2975 static DWORD get_glyph_outline( struct gdi_font *font, UINT glyph, UINT format,
2976 GLYPHMETRICS *gm_ret, ABC *abc_ret, DWORD buflen, void *buf,
2977 const MAT2 *mat )
2979 GLYPHMETRICS gm;
2980 ABC abc;
2981 DWORD ret = 1;
2982 UINT index = glyph;
2983 BOOL tategaki = (*get_gdi_font_name( font ) == '@');
2985 if (format & GGO_GLYPH_INDEX)
2987 /* Windows bitmap font, e.g. Small Fonts, uses ANSI character code
2988 as glyph index. "Treasure Adventure Game" depends on this. */
2989 font_funcs->get_glyph_index( font, &index, FALSE );
2990 format &= ~GGO_GLYPH_INDEX;
2991 /* TODO: Window also turns off tategaki for glyphs passed in by index
2992 if their unicode code points fall outside of the range that is
2993 rotated. */
2995 else
2997 index = get_glyph_index_linked( &font, glyph );
2998 if (tategaki)
3000 UINT orig = index;
3001 index = get_GSUB_vert_glyph( font, index );
3002 if (index == orig) tategaki = check_unicode_tategaki( glyph );
3006 if (mat && !memcmp( mat, &identity, sizeof(*mat) )) mat = NULL;
3008 if (format == GGO_METRICS && !mat && get_gdi_font_glyph_metrics( font, index, &gm, &abc ))
3009 goto done;
3011 ret = font_funcs->get_glyph_outline( font, index, format, &gm, &abc, buflen, buf, mat, tategaki );
3012 if (ret == GDI_ERROR) return ret;
3014 if ((format == GGO_METRICS || format == GGO_BITMAP || format == WINE_GGO_GRAY16_BITMAP) && !mat)
3015 set_gdi_font_glyph_metrics( font, index, &gm, &abc );
3017 done:
3018 if (gm_ret) *gm_ret = gm;
3019 if (abc_ret) *abc_ret = abc;
3020 return ret;
3024 /*************************************************************
3025 * font_FontIsLinked
3027 static BOOL CDECL font_FontIsLinked( PHYSDEV dev )
3029 struct font_physdev *physdev = get_font_dev( dev );
3031 if (!physdev->font)
3033 dev = GET_NEXT_PHYSDEV( dev, pFontIsLinked );
3034 return dev->funcs->pFontIsLinked( dev );
3036 return !list_empty( &physdev->font->child_fonts );
3040 /*************************************************************
3041 * font_GetCharABCWidths
3043 static BOOL CDECL font_GetCharABCWidths( PHYSDEV dev, UINT first, UINT last, ABC *buffer )
3045 struct font_physdev *physdev = get_font_dev( dev );
3046 UINT c;
3048 if (!physdev->font)
3050 dev = GET_NEXT_PHYSDEV( dev, pGetCharABCWidths );
3051 return dev->funcs->pGetCharABCWidths( dev, first, last, buffer );
3054 TRACE( "%p, %u, %u, %p\n", physdev->font, first, last, buffer );
3056 EnterCriticalSection( &font_cs );
3057 for (c = first; c <= last; c++, buffer++)
3058 get_glyph_outline( physdev->font, c, GGO_METRICS, NULL, buffer, 0, NULL, NULL );
3059 LeaveCriticalSection( &font_cs );
3060 return TRUE;
3064 /*************************************************************
3065 * font_GetCharABCWidthsI
3067 static BOOL CDECL font_GetCharABCWidthsI( PHYSDEV dev, UINT first, UINT count, WORD *gi, ABC *buffer )
3069 struct font_physdev *physdev = get_font_dev( dev );
3070 UINT c;
3072 if (!physdev->font)
3074 dev = GET_NEXT_PHYSDEV( dev, pGetCharABCWidthsI );
3075 return dev->funcs->pGetCharABCWidthsI( dev, first, count, gi, buffer );
3078 TRACE( "%p, %u, %u, %p\n", physdev->font, first, count, buffer );
3080 EnterCriticalSection( &font_cs );
3081 for (c = 0; c < count; c++, buffer++)
3082 get_glyph_outline( physdev->font, gi ? gi[c] : first + c, GGO_METRICS | GGO_GLYPH_INDEX,
3083 NULL, buffer, 0, NULL, NULL );
3084 LeaveCriticalSection( &font_cs );
3085 return TRUE;
3089 /*************************************************************
3090 * font_GetCharWidth
3092 static BOOL CDECL font_GetCharWidth( PHYSDEV dev, UINT first, UINT last, INT *buffer )
3094 struct font_physdev *physdev = get_font_dev( dev );
3095 ABC abc;
3096 UINT c;
3098 if (!physdev->font)
3100 dev = GET_NEXT_PHYSDEV( dev, pGetCharWidth );
3101 return dev->funcs->pGetCharWidth( dev, first, last, buffer );
3104 TRACE( "%p, %d, %d, %p\n", physdev->font, first, last, buffer );
3106 EnterCriticalSection( &font_cs );
3107 for (c = first; c <= last; c++)
3109 if (get_glyph_outline( physdev->font, c, GGO_METRICS, NULL, &abc, 0, NULL, NULL ) == GDI_ERROR)
3110 buffer[c - first] = 0;
3111 else
3112 buffer[c - first] = abc.abcA + abc.abcB + abc.abcC;
3114 LeaveCriticalSection( &font_cs );
3115 return TRUE;
3119 /*************************************************************
3120 * font_GetCharWidthInfo
3122 static BOOL CDECL font_GetCharWidthInfo( PHYSDEV dev, void *ptr )
3124 struct font_physdev *physdev = get_font_dev( dev );
3125 struct char_width_info *info = ptr;
3127 if (!physdev->font)
3129 dev = GET_NEXT_PHYSDEV( dev, pGetCharWidthInfo );
3130 return dev->funcs->pGetCharWidthInfo( dev, ptr );
3133 info->unk = 0;
3134 if (!physdev->font->scalable || !font_funcs->get_char_width_info( physdev->font, info ))
3135 info->lsb = info->rsb = 0;
3137 return TRUE;
3141 /*************************************************************
3142 * font_GetFontData
3144 static DWORD CDECL font_GetFontData( PHYSDEV dev, DWORD table, DWORD offset, void *buf, DWORD size )
3146 struct font_physdev *physdev = get_font_dev( dev );
3148 if (!physdev->font)
3150 dev = GET_NEXT_PHYSDEV( dev, pGetFontData );
3151 return dev->funcs->pGetFontData( dev, table, offset, buf, size );
3153 return font_funcs->get_font_data( physdev->font, table, offset, buf, size );
3157 /*************************************************************
3158 * font_GetFontRealizationInfo
3160 static BOOL CDECL font_GetFontRealizationInfo( PHYSDEV dev, void *ptr )
3162 struct font_physdev *physdev = get_font_dev( dev );
3163 struct font_realization_info *info = ptr;
3165 if (!physdev->font)
3167 dev = GET_NEXT_PHYSDEV( dev, pGetFontRealizationInfo );
3168 return dev->funcs->pGetFontRealizationInfo( dev, ptr );
3171 TRACE( "(%p, %p)\n", physdev->font, info);
3173 info->flags = 1;
3174 if (physdev->font->scalable) info->flags |= 2;
3176 info->cache_num = physdev->font->cache_num;
3177 info->instance_id = physdev->font->handle;
3178 if (info->size == sizeof(*info))
3180 info->file_count = 1;
3181 info->face_index = physdev->font->face_index;
3182 info->simulations = 0;
3183 if (physdev->font->fake_bold) info->simulations |= 0x1;
3184 if (physdev->font->fake_italic) info->simulations |= 0x2;
3186 return TRUE;
3190 /*************************************************************
3191 * font_GetFontUnicodeRanges
3193 static DWORD CDECL font_GetFontUnicodeRanges( PHYSDEV dev, GLYPHSET *glyphset )
3195 struct font_physdev *physdev = get_font_dev( dev );
3196 DWORD size, num_ranges;
3198 if (!physdev->font)
3200 dev = GET_NEXT_PHYSDEV( dev, pGetFontUnicodeRanges );
3201 return dev->funcs->pGetFontUnicodeRanges( dev, glyphset );
3204 num_ranges = font_funcs->get_unicode_ranges( physdev->font, glyphset );
3205 size = offsetof( GLYPHSET, ranges[num_ranges] );
3206 if (glyphset)
3208 glyphset->cbThis = size;
3209 glyphset->cRanges = num_ranges;
3210 glyphset->flAccel = 0;
3212 return size;
3216 /*************************************************************
3217 * font_GetGlyphIndices
3219 static DWORD CDECL font_GetGlyphIndices( PHYSDEV dev, const WCHAR *str, INT count, WORD *gi, DWORD flags )
3221 struct font_physdev *physdev = get_font_dev( dev );
3222 UINT default_char;
3223 char ch;
3224 BOOL used, got_default = FALSE;
3225 int i;
3227 if (!physdev->font)
3229 dev = GET_NEXT_PHYSDEV( dev, pGetGlyphIndices );
3230 return dev->funcs->pGetGlyphIndices( dev, str, count, gi, flags );
3233 if (flags & GGI_MARK_NONEXISTING_GLYPHS)
3235 default_char = 0xffff; /* XP would use 0x1f for bitmap fonts */
3236 got_default = TRUE;
3239 EnterCriticalSection( &font_cs );
3241 for (i = 0; i < count; i++)
3243 UINT glyph = str[i];
3245 if (!font_funcs->get_glyph_index( physdev->font, &glyph, TRUE ))
3247 glyph = 0;
3248 if (physdev->font->codepage == CP_SYMBOL)
3250 if (str[i] >= 0xf020 && str[i] <= 0xf100) glyph = str[i] - 0xf000;
3251 else if (str[i] < 0x100) glyph = str[i];
3253 else if (WideCharToMultiByte( physdev->font->codepage, WC_NO_BEST_FIT_CHARS, &str[i], 1,
3254 &ch, 1, NULL, &used ) && !used)
3255 glyph = (unsigned char)ch;
3257 if (!glyph)
3259 if (!got_default)
3261 default_char = font_funcs->get_default_glyph( physdev->font );
3262 got_default = TRUE;
3264 gi[i] = default_char;
3266 else gi[i] = get_GSUB_vert_glyph( physdev->font, glyph );
3269 LeaveCriticalSection( &font_cs );
3270 return count;
3274 /*************************************************************
3275 * font_GetGlyphOutline
3277 static DWORD CDECL font_GetGlyphOutline( PHYSDEV dev, UINT glyph, UINT format,
3278 GLYPHMETRICS *gm, DWORD buflen, void *buf, const MAT2 *mat )
3280 struct font_physdev *physdev = get_font_dev( dev );
3281 DWORD ret;
3283 if (!physdev->font)
3285 dev = GET_NEXT_PHYSDEV( dev, pGetGlyphOutline );
3286 return dev->funcs->pGetGlyphOutline( dev, glyph, format, gm, buflen, buf, mat );
3288 EnterCriticalSection( &font_cs );
3289 ret = get_glyph_outline( physdev->font, glyph, format, gm, NULL, buflen, buf, mat );
3290 LeaveCriticalSection( &font_cs );
3291 return ret;
3295 /*************************************************************
3296 * font_GetKerningPairs
3298 static DWORD CDECL font_GetKerningPairs( PHYSDEV dev, DWORD count, KERNINGPAIR *pairs )
3300 struct font_physdev *physdev = get_font_dev( dev );
3302 if (!physdev->font)
3304 dev = GET_NEXT_PHYSDEV( dev, pGetKerningPairs );
3305 return dev->funcs->pGetKerningPairs( dev, count, pairs );
3308 EnterCriticalSection( &font_cs );
3309 if (physdev->font->kern_count == -1)
3310 physdev->font->kern_count = font_funcs->get_kerning_pairs( physdev->font,
3311 &physdev->font->kern_pairs );
3312 LeaveCriticalSection( &font_cs );
3314 if (count && pairs)
3316 count = min( count, physdev->font->kern_count );
3317 memcpy( pairs, physdev->font->kern_pairs, count * sizeof(*pairs) );
3319 else count = physdev->font->kern_count;
3321 return count;
3325 static void scale_outline_font_metrics( const struct gdi_font *font, OUTLINETEXTMETRICW *otm )
3327 double scale_x, scale_y;
3329 if (font->aveWidth)
3331 scale_x = (double)font->aveWidth;
3332 scale_x /= (double)font->otm.otmTextMetrics.tmAveCharWidth;
3334 else
3335 scale_x = font->scale_y;
3337 scale_x *= fabs(font->matrix.eM11);
3338 scale_y = font->scale_y * fabs(font->matrix.eM22);
3340 /* Windows scales these values as signed integers even if they are unsigned */
3341 #define SCALE_X(x) (x) = GDI_ROUND((int)(x) * (scale_x))
3342 #define SCALE_Y(y) (y) = GDI_ROUND((int)(y) * (scale_y))
3344 SCALE_Y(otm->otmTextMetrics.tmHeight);
3345 SCALE_Y(otm->otmTextMetrics.tmAscent);
3346 SCALE_Y(otm->otmTextMetrics.tmDescent);
3347 SCALE_Y(otm->otmTextMetrics.tmInternalLeading);
3348 SCALE_Y(otm->otmTextMetrics.tmExternalLeading);
3350 SCALE_X(otm->otmTextMetrics.tmOverhang);
3351 if (font->fake_bold)
3353 if (!font->scalable) otm->otmTextMetrics.tmOverhang++;
3354 otm->otmTextMetrics.tmAveCharWidth++;
3355 otm->otmTextMetrics.tmMaxCharWidth++;
3357 SCALE_X(otm->otmTextMetrics.tmAveCharWidth);
3358 SCALE_X(otm->otmTextMetrics.tmMaxCharWidth);
3360 SCALE_Y(otm->otmAscent);
3361 SCALE_Y(otm->otmDescent);
3362 SCALE_Y(otm->otmLineGap);
3363 SCALE_Y(otm->otmsCapEmHeight);
3364 SCALE_Y(otm->otmsXHeight);
3365 SCALE_Y(otm->otmrcFontBox.top);
3366 SCALE_Y(otm->otmrcFontBox.bottom);
3367 SCALE_X(otm->otmrcFontBox.left);
3368 SCALE_X(otm->otmrcFontBox.right);
3369 SCALE_Y(otm->otmMacAscent);
3370 SCALE_Y(otm->otmMacDescent);
3371 SCALE_Y(otm->otmMacLineGap);
3372 SCALE_X(otm->otmptSubscriptSize.x);
3373 SCALE_Y(otm->otmptSubscriptSize.y);
3374 SCALE_X(otm->otmptSubscriptOffset.x);
3375 SCALE_Y(otm->otmptSubscriptOffset.y);
3376 SCALE_X(otm->otmptSuperscriptSize.x);
3377 SCALE_Y(otm->otmptSuperscriptSize.y);
3378 SCALE_X(otm->otmptSuperscriptOffset.x);
3379 SCALE_Y(otm->otmptSuperscriptOffset.y);
3380 SCALE_Y(otm->otmsStrikeoutSize);
3381 SCALE_Y(otm->otmsStrikeoutPosition);
3382 SCALE_Y(otm->otmsUnderscoreSize);
3383 SCALE_Y(otm->otmsUnderscorePosition);
3385 #undef SCALE_X
3386 #undef SCALE_Y
3389 /*************************************************************
3390 * font_GetOutlineTextMetrics
3392 static UINT CDECL font_GetOutlineTextMetrics( PHYSDEV dev, UINT size, OUTLINETEXTMETRICW *metrics )
3394 struct font_physdev *physdev = get_font_dev( dev );
3395 UINT ret = 0;
3397 if (!physdev->font)
3399 dev = GET_NEXT_PHYSDEV( dev, pGetOutlineTextMetrics );
3400 return dev->funcs->pGetOutlineTextMetrics( dev, size, metrics );
3403 if (!physdev->font->scalable) return 0;
3405 EnterCriticalSection( &font_cs );
3406 if (font_funcs->set_outline_text_metrics( physdev->font ))
3408 ret = physdev->font->otm.otmSize;
3409 if (metrics && size >= physdev->font->otm.otmSize)
3411 WCHAR *ptr = (WCHAR *)(metrics + 1);
3412 *metrics = physdev->font->otm;
3413 metrics->otmpFamilyName = (char *)ptr - (ULONG_PTR)metrics;
3414 lstrcpyW( ptr, (WCHAR *)physdev->font->otm.otmpFamilyName );
3415 ptr += lstrlenW(ptr) + 1;
3416 metrics->otmpStyleName = (char *)ptr - (ULONG_PTR)metrics;
3417 lstrcpyW( ptr, (WCHAR *)physdev->font->otm.otmpStyleName );
3418 ptr += lstrlenW(ptr) + 1;
3419 metrics->otmpFaceName = (char *)ptr - (ULONG_PTR)metrics;
3420 lstrcpyW( ptr, (WCHAR *)physdev->font->otm.otmpFaceName );
3421 ptr += lstrlenW(ptr) + 1;
3422 metrics->otmpFullName = (char *)ptr - (ULONG_PTR)metrics;
3423 lstrcpyW( ptr, (WCHAR *)physdev->font->otm.otmpFullName );
3424 scale_outline_font_metrics( physdev->font, metrics );
3427 LeaveCriticalSection( &font_cs );
3428 return ret;
3432 /*************************************************************
3433 * font_GetTextCharsetInfo
3435 static UINT CDECL font_GetTextCharsetInfo( PHYSDEV dev, FONTSIGNATURE *fs, DWORD flags )
3437 struct font_physdev *physdev = get_font_dev( dev );
3439 if (!physdev->font)
3441 dev = GET_NEXT_PHYSDEV( dev, pGetTextCharsetInfo );
3442 return dev->funcs->pGetTextCharsetInfo( dev, fs, flags );
3444 if (fs) *fs = physdev->font->fs;
3445 return physdev->font->charset;
3449 /*************************************************************
3450 * font_GetTextExtentExPoint
3452 static BOOL CDECL font_GetTextExtentExPoint( PHYSDEV dev, const WCHAR *str, INT count, INT *dxs )
3454 struct font_physdev *physdev = get_font_dev( dev );
3455 INT i, pos;
3456 ABC abc;
3458 if (!physdev->font)
3460 dev = GET_NEXT_PHYSDEV( dev, pGetTextExtentExPoint );
3461 return dev->funcs->pGetTextExtentExPoint( dev, str, count, dxs );
3464 TRACE( "%p, %s, %d\n", physdev->font, debugstr_wn(str, count), count );
3466 EnterCriticalSection( &font_cs );
3467 for (i = pos = 0; i < count; i++)
3469 get_glyph_outline( physdev->font, str[i], GGO_METRICS, NULL, &abc, 0, NULL, NULL );
3470 pos += abc.abcA + abc.abcB + abc.abcC;
3471 dxs[i] = pos;
3473 LeaveCriticalSection( &font_cs );
3474 return TRUE;
3478 /*************************************************************
3479 * font_GetTextExtentExPointI
3481 static BOOL CDECL font_GetTextExtentExPointI( PHYSDEV dev, const WORD *indices, INT count, INT *dxs )
3483 struct font_physdev *physdev = get_font_dev( dev );
3484 INT i, pos;
3485 ABC abc;
3487 if (!physdev->font)
3489 dev = GET_NEXT_PHYSDEV( dev, pGetTextExtentExPointI );
3490 return dev->funcs->pGetTextExtentExPointI( dev, indices, count, dxs );
3493 TRACE( "%p, %p, %d\n", physdev->font, indices, count );
3495 EnterCriticalSection( &font_cs );
3496 for (i = pos = 0; i < count; i++)
3498 get_glyph_outline( physdev->font, indices[i], GGO_METRICS | GGO_GLYPH_INDEX,
3499 NULL, &abc, 0, NULL, NULL );
3500 pos += abc.abcA + abc.abcB + abc.abcC;
3501 dxs[i] = pos;
3503 LeaveCriticalSection( &font_cs );
3504 return TRUE;
3508 /*************************************************************
3509 * font_GetTextFace
3511 static INT CDECL font_GetTextFace( PHYSDEV dev, INT count, WCHAR *str )
3513 struct font_physdev *physdev = get_font_dev( dev );
3514 INT len;
3516 if (!physdev->font)
3518 dev = GET_NEXT_PHYSDEV( dev, pGetTextFace );
3519 return dev->funcs->pGetTextFace( dev, count, str );
3521 len = lstrlenW( get_gdi_font_name(physdev->font) ) + 1;
3522 if (str)
3524 lstrcpynW( str, get_gdi_font_name(physdev->font), count );
3525 len = min( count, len );
3527 return len;
3531 static void scale_font_metrics( struct gdi_font *font, TEXTMETRICW *tm )
3533 double scale_x, scale_y;
3535 /* Make sure that the font has sane width/height ratio */
3536 if (font->aveWidth && (font->aveWidth + tm->tmHeight - 1) / tm->tmHeight > 100)
3538 WARN( "Ignoring too large font->aveWidth %d\n", font->aveWidth );
3539 font->aveWidth = 0;
3542 if (font->aveWidth)
3544 scale_x = (double)font->aveWidth;
3545 scale_x /= (double)font->otm.otmTextMetrics.tmAveCharWidth;
3547 else
3548 scale_x = font->scale_y;
3550 scale_x *= fabs(font->matrix.eM11);
3551 scale_y = font->scale_y * fabs(font->matrix.eM22);
3553 #define SCALE_X(x) (x) = GDI_ROUND((x) * scale_x)
3554 #define SCALE_Y(y) (y) = GDI_ROUND((y) * scale_y)
3556 SCALE_Y(tm->tmHeight);
3557 SCALE_Y(tm->tmAscent);
3558 SCALE_Y(tm->tmDescent);
3559 SCALE_Y(tm->tmInternalLeading);
3560 SCALE_Y(tm->tmExternalLeading);
3562 SCALE_X(tm->tmOverhang);
3563 if (font->fake_bold)
3565 if (!font->scalable) tm->tmOverhang++;
3566 tm->tmAveCharWidth++;
3567 tm->tmMaxCharWidth++;
3569 SCALE_X(tm->tmAveCharWidth);
3570 SCALE_X(tm->tmMaxCharWidth);
3572 #undef SCALE_X
3573 #undef SCALE_Y
3576 /*************************************************************
3577 * font_GetTextMetrics
3579 static BOOL CDECL font_GetTextMetrics( PHYSDEV dev, TEXTMETRICW *metrics )
3581 struct font_physdev *physdev = get_font_dev( dev );
3582 BOOL ret = FALSE;
3584 if (!physdev->font)
3586 dev = GET_NEXT_PHYSDEV( dev, pGetTextMetrics );
3587 return dev->funcs->pGetTextMetrics( dev, metrics );
3590 EnterCriticalSection( &font_cs );
3591 if (font_funcs->set_outline_text_metrics( physdev->font ) ||
3592 font_funcs->set_bitmap_text_metrics( physdev->font ))
3594 *metrics = physdev->font->otm.otmTextMetrics;
3595 scale_font_metrics( physdev->font, metrics );
3596 ret = TRUE;
3598 LeaveCriticalSection( &font_cs );
3599 return ret;
3603 static void get_nearest_charset( const WCHAR *family_name, struct gdi_font_face *face, CHARSETINFO *csi )
3605 /* Only get here if lfCharSet == DEFAULT_CHARSET or we couldn't find
3606 a single face with the requested charset. The idea is to check if
3607 the selected font supports the current ANSI codepage, if it does
3608 return the corresponding charset, else return the first charset */
3610 int i;
3612 if (TranslateCharsetInfo( (DWORD*)(INT_PTR)GetACP(), csi, TCI_SRCCODEPAGE ))
3614 const struct gdi_font_link *font_link;
3616 if (csi->fs.fsCsb[0] & face->fs.fsCsb[0]) return;
3617 font_link = find_gdi_font_link(family_name);
3618 if (font_link && (csi->fs.fsCsb[0] & font_link->fs.fsCsb[0])) return;
3620 for (i = 0; i < 32; i++)
3622 DWORD fs0 = 1u << i;
3623 if (face->fs.fsCsb[0] & fs0)
3625 if (TranslateCharsetInfo(&fs0, csi, TCI_SRCFONTSIG)) return;
3626 FIXME("TCI failing on %x\n", fs0);
3630 FIXME("returning DEFAULT_CHARSET face->fs.fsCsb[0] = %08x file = %s\n",
3631 face->fs.fsCsb[0], debugstr_w(face->file));
3632 csi->ciACP = GetACP();
3633 csi->ciCharset = DEFAULT_CHARSET;
3636 static struct gdi_font *select_font( LOGFONTW *lf, FMAT2 dcmat, BOOL can_use_bitmap )
3638 struct gdi_font *font;
3639 struct gdi_font_face *face;
3640 INT height;
3641 CHARSETINFO csi;
3642 const WCHAR *orig_name = NULL;
3644 /* If lfFaceName is "Symbol" then Windows fixes up lfCharSet to
3645 SYMBOL_CHARSET so that Symbol gets picked irrespective of the
3646 original value lfCharSet. Note this is a special case for
3647 Symbol and doesn't happen at least for "Wingdings*" */
3648 if (!facename_compare( lf->lfFaceName, L"Symbol", -1 )) lf->lfCharSet = SYMBOL_CHARSET;
3650 /* check the cache first */
3651 if ((font = find_cached_gdi_font( lf, &dcmat, can_use_bitmap )))
3653 TRACE( "returning cached gdiFont(%p)\n", font );
3654 return font;
3656 if (!(face = find_matching_face( lf, &csi, can_use_bitmap, &orig_name )))
3658 FIXME( "can't find a single appropriate font - bailing\n" );
3659 return NULL;
3661 height = lf->lfHeight;
3663 font = create_gdi_font( face, orig_name, lf );
3664 font->matrix = dcmat;
3665 font->can_use_bitmap = can_use_bitmap;
3666 if (!csi.fs.fsCsb[0]) get_nearest_charset( face->family->family_name, face, &csi );
3667 font->charset = csi.ciCharset;
3668 font->codepage = csi.ciACP;
3670 TRACE( "Chosen: %s (%s/%p:%u)\n", debugstr_w(face->full_name), debugstr_w(face->file),
3671 face->data_ptr, face->face_index );
3673 font->aveWidth = height ? lf->lfWidth : 0;
3674 if (!face->scalable)
3676 /* Windows uses integer scaling factors for bitmap fonts */
3677 INT scale, scaled_height, diff;
3678 struct gdi_font *cachedfont;
3680 if (height > 0)
3681 diff = height - (signed int)face->size.height;
3682 else
3683 diff = -height - ((signed int)face->size.height - face->size.internal_leading);
3685 /* FIXME: rotation of bitmap fonts is ignored */
3686 height = abs(GDI_ROUND( (double)height * font->matrix.eM22 ));
3687 if (font->aveWidth)
3688 font->aveWidth = (double)font->aveWidth * font->matrix.eM11;
3689 font->matrix.eM11 = font->matrix.eM22 = 1.0;
3690 dcmat.eM11 = dcmat.eM22 = 1.0;
3691 /* As we changed the matrix, we need to search the cache for the font again,
3692 * otherwise we might explode the cache. */
3693 if ((cachedfont = find_cached_gdi_font( lf, &dcmat, can_use_bitmap )))
3695 TRACE("Found cached font after non-scalable matrix rescale!\n");
3696 free_gdi_font( font );
3697 return cachedfont;
3700 if (height != 0) height = diff;
3701 height += face->size.height;
3703 scale = (height + face->size.height - 1) / face->size.height;
3704 scaled_height = scale * face->size.height;
3705 /* Only jump to the next height if the difference <= 25% original height */
3706 if (scale > 2 && scaled_height - height > face->size.height / 4) scale--;
3707 /* The jump between unscaled and doubled is delayed by 1 */
3708 else if (scale == 2 && scaled_height - height > (face->size.height / 4 - 1)) scale--;
3709 font->scale_y = scale;
3710 TRACE("font scale y: %d\n", font->scale_y);
3713 if (!font_funcs->load_font( font ))
3715 free_gdi_font( font );
3716 return NULL;
3719 if (face->flags & ADDFONT_VERTICAL_FONT) /* We need to try to load the GSUB table */
3720 font->vert_feature = get_GSUB_vert_feature( font );
3722 create_child_font_list( font );
3724 TRACE( "caching: gdiFont=%p\n", font );
3725 cache_gdi_font( font );
3726 return font;
3729 /*************************************************************
3730 * font_SelectFont
3732 static HFONT CDECL font_SelectFont( PHYSDEV dev, HFONT hfont, UINT *aa_flags )
3734 struct font_physdev *physdev = get_font_dev( dev );
3735 struct gdi_font *font = NULL, *prev = physdev->font;
3736 DC *dc = get_physdev_dc( dev );
3738 if (hfont)
3740 LOGFONTW lf;
3741 FMAT2 dcmat;
3742 BOOL can_use_bitmap = !!(GetDeviceCaps( dc->hSelf, TEXTCAPS ) & TC_RA_ABLE);
3744 GetObjectW( hfont, sizeof(lf), &lf );
3745 switch (lf.lfQuality)
3747 case NONANTIALIASED_QUALITY:
3748 if (!*aa_flags) *aa_flags = GGO_BITMAP;
3749 break;
3750 case ANTIALIASED_QUALITY:
3751 if (!*aa_flags) *aa_flags = GGO_GRAY4_BITMAP;
3752 break;
3755 lf.lfWidth = abs(lf.lfWidth);
3757 TRACE( "%s, h=%d, it=%d, weight=%d, PandF=%02x, charset=%d orient %d escapement %d\n",
3758 debugstr_w(lf.lfFaceName), lf.lfHeight, lf.lfItalic,
3759 lf.lfWeight, lf.lfPitchAndFamily, lf.lfCharSet, lf.lfOrientation,
3760 lf.lfEscapement );
3762 if (dc->attr->graphics_mode == GM_ADVANCED)
3764 memcpy( &dcmat, &dc->xformWorld2Vport, sizeof(FMAT2) );
3765 /* try to avoid not necessary glyph transformations */
3766 if (dcmat.eM21 == 0.0 && dcmat.eM12 == 0.0 && dcmat.eM11 == dcmat.eM22)
3768 lf.lfHeight *= fabs(dcmat.eM11);
3769 lf.lfWidth *= fabs(dcmat.eM11);
3770 dcmat.eM11 = dcmat.eM22 = dcmat.eM11 < 0 ? -1 : 1;
3773 else
3775 /* Windows 3.1 compatibility mode GM_COMPATIBLE has only limited font scaling abilities */
3776 dcmat.eM11 = dcmat.eM22 = 1.0;
3777 dcmat.eM21 = dcmat.eM12 = 0;
3778 lf.lfOrientation = lf.lfEscapement;
3779 if (dc->vport2WorldValid)
3781 if (dc->xformWorld2Vport.eM11 * dc->xformWorld2Vport.eM22 < 0)
3782 lf.lfOrientation = -lf.lfOrientation;
3783 lf.lfHeight *= fabs(dc->xformWorld2Vport.eM22);
3784 lf.lfWidth *= fabs(dc->xformWorld2Vport.eM22);
3787 TRACE( "DC transform %f %f %f %f\n", dcmat.eM11, dcmat.eM12, dcmat.eM21, dcmat.eM22 );
3789 EnterCriticalSection( &font_cs );
3791 font = select_font( &lf, dcmat, can_use_bitmap );
3793 if (font)
3795 if (!*aa_flags) *aa_flags = font->aa_flags;
3796 if (!*aa_flags)
3798 if (lf.lfQuality == CLEARTYPE_QUALITY || lf.lfQuality == CLEARTYPE_NATURAL_QUALITY)
3799 *aa_flags = subpixel_orientation;
3800 else
3801 *aa_flags = font_smoothing;
3803 *aa_flags = font_funcs->get_aa_flags( font, *aa_flags, antialias_fakes );
3805 TRACE( "%p %s %d aa %x\n", hfont, debugstr_w(lf.lfFaceName), lf.lfHeight, *aa_flags );
3806 LeaveCriticalSection( &font_cs );
3808 physdev->font = font;
3809 if (prev) release_gdi_font( prev );
3810 return font ? hfont : 0;
3814 const struct gdi_dc_funcs font_driver =
3816 NULL, /* pAbortDoc */
3817 NULL, /* pAbortPath */
3818 NULL, /* pAlphaBlend */
3819 NULL, /* pAngleArc */
3820 NULL, /* pArc */
3821 NULL, /* pArcTo */
3822 NULL, /* pBeginPath */
3823 NULL, /* pBlendImage */
3824 NULL, /* pChord */
3825 NULL, /* pCloseFigure */
3826 NULL, /* pCreateCompatibleDC */
3827 font_CreateDC, /* pCreateDC */
3828 font_DeleteDC, /* pDeleteDC */
3829 NULL, /* pDeleteObject */
3830 NULL, /* pDeviceCapabilities */
3831 NULL, /* pEllipse */
3832 NULL, /* pEndDoc */
3833 NULL, /* pEndPage */
3834 NULL, /* pEndPath */
3835 font_EnumFonts, /* pEnumFonts */
3836 NULL, /* pEnumICMProfiles */
3837 NULL, /* pExtDeviceMode */
3838 NULL, /* pExtEscape */
3839 NULL, /* pExtFloodFill */
3840 NULL, /* pExtTextOut */
3841 NULL, /* pFillPath */
3842 NULL, /* pFillRgn */
3843 NULL, /* pFlattenPath */
3844 font_FontIsLinked, /* pFontIsLinked */
3845 NULL, /* pFrameRgn */
3846 NULL, /* pGdiComment */
3847 NULL, /* pGetBoundsRect */
3848 font_GetCharABCWidths, /* pGetCharABCWidths */
3849 font_GetCharABCWidthsI, /* pGetCharABCWidthsI */
3850 font_GetCharWidth, /* pGetCharWidth */
3851 font_GetCharWidthInfo, /* pGetCharWidthInfo */
3852 NULL, /* pGetDeviceCaps */
3853 NULL, /* pGetDeviceGammaRamp */
3854 font_GetFontData, /* pGetFontData */
3855 font_GetFontRealizationInfo, /* pGetFontRealizationInfo */
3856 font_GetFontUnicodeRanges, /* pGetFontUnicodeRanges */
3857 font_GetGlyphIndices, /* pGetGlyphIndices */
3858 font_GetGlyphOutline, /* pGetGlyphOutline */
3859 NULL, /* pGetICMProfile */
3860 NULL, /* pGetImage */
3861 font_GetKerningPairs, /* pGetKerningPairs */
3862 NULL, /* pGetNearestColor */
3863 font_GetOutlineTextMetrics, /* pGetOutlineTextMetrics */
3864 NULL, /* pGetPixel */
3865 NULL, /* pGetSystemPaletteEntries */
3866 font_GetTextCharsetInfo, /* pGetTextCharsetInfo */
3867 font_GetTextExtentExPoint, /* pGetTextExtentExPoint */
3868 font_GetTextExtentExPointI, /* pGetTextExtentExPointI */
3869 font_GetTextFace, /* pGetTextFace */
3870 font_GetTextMetrics, /* pGetTextMetrics */
3871 NULL, /* pGradientFill */
3872 NULL, /* pInvertRgn */
3873 NULL, /* pLineTo */
3874 NULL, /* pModifyWorldTransform */
3875 NULL, /* pMoveTo */
3876 NULL, /* pPaintRgn */
3877 NULL, /* pPatBlt */
3878 NULL, /* pPie */
3879 NULL, /* pPolyBezier */
3880 NULL, /* pPolyBezierTo */
3881 NULL, /* pPolyDraw */
3882 NULL, /* pPolyPolygon */
3883 NULL, /* pPolyPolyline */
3884 NULL, /* pPolylineTo */
3885 NULL, /* pPutImage */
3886 NULL, /* pRealizeDefaultPalette */
3887 NULL, /* pRealizePalette */
3888 NULL, /* pRectangle */
3889 NULL, /* pResetDC */
3890 NULL, /* pRestoreDC */
3891 NULL, /* pRoundRect */
3892 NULL, /* pSelectBitmap */
3893 NULL, /* pSelectBrush */
3894 NULL, /* pSelectClipPath */
3895 font_SelectFont, /* pSelectFont */
3896 NULL, /* pSelectPen */
3897 NULL, /* pSetBkColor */
3898 NULL, /* pSetBoundsRect */
3899 NULL, /* pSetDCBrushColor */
3900 NULL, /* pSetDCPenColor */
3901 NULL, /* pSetDIBitsToDevice */
3902 NULL, /* pSetDeviceClipping */
3903 NULL, /* pSetDeviceGammaRamp */
3904 NULL, /* pSetPixel */
3905 NULL, /* pSetTextColor */
3906 NULL, /* pSetWorldTransform */
3907 NULL, /* pStartDoc */
3908 NULL, /* pStartPage */
3909 NULL, /* pStretchBlt */
3910 NULL, /* pStretchDIBits */
3911 NULL, /* pStrokeAndFillPath */
3912 NULL, /* pStrokePath */
3913 NULL, /* pUnrealizePalette */
3914 NULL, /* pWidenPath */
3915 NULL, /* pD3DKMTCheckVidPnExclusiveOwnership */
3916 NULL, /* pD3DKMTSetVidPnSourceOwner */
3917 NULL, /* wine_get_wgl_driver */
3918 NULL, /* wine_get_vulkan_driver */
3919 GDI_PRIORITY_FONT_DRV /* priority */
3922 static DWORD get_key_value( HKEY key, const WCHAR *name, DWORD *value )
3924 WCHAR buf[12];
3925 DWORD count = sizeof(buf), type, err;
3927 err = RegQueryValueExW( key, name, NULL, &type, (BYTE *)buf, &count );
3928 if (!err)
3930 if (type == REG_DWORD) memcpy( value, buf, sizeof(*value) );
3931 else *value = wcstol( buf, NULL, 10 );
3933 return err;
3936 static void init_font_options(void)
3938 HKEY key;
3939 DWORD i, type, size, val, gamma = 1400;
3940 WCHAR buffer[20];
3942 size = sizeof(buffer);
3943 if (!RegQueryValueExW( wine_fonts_key, L"AntialiasFakeBoldOrItalic", NULL,
3944 &type, (BYTE *)buffer, &size) && type == REG_SZ && size >= 1)
3946 antialias_fakes = (wcschr(L"yYtT1", buffer[0]) != NULL);
3949 if (!RegOpenKeyW( HKEY_CURRENT_USER, L"Control Panel\\Desktop", &key ))
3951 /* FIXME: handle vertical orientations even though Windows doesn't */
3952 if (!get_key_value( key, L"FontSmoothingOrientation", &val ))
3954 switch (val)
3956 case 0: /* FE_FONTSMOOTHINGORIENTATIONBGR */
3957 subpixel_orientation = WINE_GGO_HBGR_BITMAP;
3958 break;
3959 case 1: /* FE_FONTSMOOTHINGORIENTATIONRGB */
3960 subpixel_orientation = WINE_GGO_HRGB_BITMAP;
3961 break;
3964 if (!get_key_value( key, L"FontSmoothing", &val ) && val /* enabled */)
3966 if (!get_key_value( key, L"FontSmoothingType", &val ) && val == 2 /* FE_FONTSMOOTHINGCLEARTYPE */)
3967 font_smoothing = subpixel_orientation;
3968 else
3969 font_smoothing = GGO_GRAY4_BITMAP;
3971 if (!get_key_value( key, L"FontSmoothingGamma", &val ) && val)
3973 gamma = min( max( val, 1000 ), 2200 );
3975 RegCloseKey( key );
3978 /* Calibrating the difference between the registry value and the Wine gamma value.
3979 This looks roughly similar to Windows Native with the same registry value.
3980 MS GDI seems to be rasterizing the outline at a different rate than FreeType. */
3981 gamma = 1000 * gamma / 1400;
3982 if (gamma != 1000)
3984 for (i = 0; i < 256; i++)
3986 font_gamma_ramp.encode[i] = pow( i / 255., 1000. / gamma ) * 255. + .5;
3987 font_gamma_ramp.decode[i] = pow( i / 255., gamma / 1000. ) * 255. + .5;
3990 font_gamma_ramp.gamma = gamma;
3991 TRACE("gamma %d\n", font_gamma_ramp.gamma);
3995 static void FONT_LogFontAToW( const LOGFONTA *fontA, LPLOGFONTW fontW )
3997 memcpy(fontW, fontA, sizeof(LOGFONTA) - LF_FACESIZE);
3998 MultiByteToWideChar(CP_ACP, 0, fontA->lfFaceName, -1, fontW->lfFaceName,
3999 LF_FACESIZE);
4000 fontW->lfFaceName[LF_FACESIZE-1] = 0;
4003 static void FONT_LogFontWToA( const LOGFONTW *fontW, LPLOGFONTA fontA )
4005 memcpy(fontA, fontW, sizeof(LOGFONTA) - LF_FACESIZE);
4006 WideCharToMultiByte(CP_ACP, 0, fontW->lfFaceName, -1, fontA->lfFaceName,
4007 LF_FACESIZE, NULL, NULL);
4008 fontA->lfFaceName[LF_FACESIZE-1] = 0;
4011 static void FONT_EnumLogFontExWToA( const ENUMLOGFONTEXW *fontW, LPENUMLOGFONTEXA fontA )
4013 FONT_LogFontWToA( &fontW->elfLogFont, &fontA->elfLogFont );
4015 WideCharToMultiByte( CP_ACP, 0, fontW->elfFullName, -1,
4016 (LPSTR) fontA->elfFullName, LF_FULLFACESIZE, NULL, NULL );
4017 fontA->elfFullName[LF_FULLFACESIZE-1] = '\0';
4018 WideCharToMultiByte( CP_ACP, 0, fontW->elfStyle, -1,
4019 (LPSTR) fontA->elfStyle, LF_FACESIZE, NULL, NULL );
4020 fontA->elfStyle[LF_FACESIZE-1] = '\0';
4021 WideCharToMultiByte( CP_ACP, 0, fontW->elfScript, -1,
4022 (LPSTR) fontA->elfScript, LF_FACESIZE, NULL, NULL );
4023 fontA->elfScript[LF_FACESIZE-1] = '\0';
4026 static void FONT_EnumLogFontExAToW( const ENUMLOGFONTEXA *fontA, LPENUMLOGFONTEXW fontW )
4028 FONT_LogFontAToW( &fontA->elfLogFont, &fontW->elfLogFont );
4030 MultiByteToWideChar( CP_ACP, 0, (LPCSTR)fontA->elfFullName, -1,
4031 fontW->elfFullName, LF_FULLFACESIZE );
4032 fontW->elfFullName[LF_FULLFACESIZE-1] = '\0';
4033 MultiByteToWideChar( CP_ACP, 0, (LPCSTR)fontA->elfStyle, -1,
4034 fontW->elfStyle, LF_FACESIZE );
4035 fontW->elfStyle[LF_FACESIZE-1] = '\0';
4036 MultiByteToWideChar( CP_ACP, 0, (LPCSTR)fontA->elfScript, -1,
4037 fontW->elfScript, LF_FACESIZE );
4038 fontW->elfScript[LF_FACESIZE-1] = '\0';
4041 /***********************************************************************
4042 * TEXTMETRIC conversion functions.
4044 static void FONT_TextMetricWToA(const TEXTMETRICW *ptmW, LPTEXTMETRICA ptmA )
4046 ptmA->tmHeight = ptmW->tmHeight;
4047 ptmA->tmAscent = ptmW->tmAscent;
4048 ptmA->tmDescent = ptmW->tmDescent;
4049 ptmA->tmInternalLeading = ptmW->tmInternalLeading;
4050 ptmA->tmExternalLeading = ptmW->tmExternalLeading;
4051 ptmA->tmAveCharWidth = ptmW->tmAveCharWidth;
4052 ptmA->tmMaxCharWidth = ptmW->tmMaxCharWidth;
4053 ptmA->tmWeight = ptmW->tmWeight;
4054 ptmA->tmOverhang = ptmW->tmOverhang;
4055 ptmA->tmDigitizedAspectX = ptmW->tmDigitizedAspectX;
4056 ptmA->tmDigitizedAspectY = ptmW->tmDigitizedAspectY;
4057 ptmA->tmFirstChar = min(ptmW->tmFirstChar, 255);
4058 if (ptmW->tmCharSet == SYMBOL_CHARSET)
4060 ptmA->tmFirstChar = 0x1e;
4061 ptmA->tmLastChar = 0xff; /* win9x behaviour - we need the OS2 table data to calculate correctly */
4063 else if (ptmW->tmPitchAndFamily & TMPF_TRUETYPE)
4065 ptmA->tmFirstChar = ptmW->tmDefaultChar - 1;
4066 ptmA->tmLastChar = min(ptmW->tmLastChar, 0xff);
4068 else
4070 ptmA->tmFirstChar = min(ptmW->tmFirstChar, 0xff);
4071 ptmA->tmLastChar = min(ptmW->tmLastChar, 0xff);
4073 ptmA->tmDefaultChar = ptmW->tmDefaultChar;
4074 ptmA->tmBreakChar = ptmW->tmBreakChar;
4075 ptmA->tmItalic = ptmW->tmItalic;
4076 ptmA->tmUnderlined = ptmW->tmUnderlined;
4077 ptmA->tmStruckOut = ptmW->tmStruckOut;
4078 ptmA->tmPitchAndFamily = ptmW->tmPitchAndFamily;
4079 ptmA->tmCharSet = ptmW->tmCharSet;
4083 static void FONT_NewTextMetricExWToA(const NEWTEXTMETRICEXW *ptmW, NEWTEXTMETRICEXA *ptmA )
4085 FONT_TextMetricWToA((const TEXTMETRICW *)ptmW, (LPTEXTMETRICA)ptmA);
4086 ptmA->ntmTm.ntmFlags = ptmW->ntmTm.ntmFlags;
4087 ptmA->ntmTm.ntmSizeEM = ptmW->ntmTm.ntmSizeEM;
4088 ptmA->ntmTm.ntmCellHeight = ptmW->ntmTm.ntmCellHeight;
4089 ptmA->ntmTm.ntmAvgWidth = ptmW->ntmTm.ntmAvgWidth;
4090 memcpy(&ptmA->ntmFontSig, &ptmW->ntmFontSig, sizeof(FONTSIGNATURE));
4093 /* compute positions for text rendering, in device coords */
4094 static BOOL get_char_positions( DC *dc, const WCHAR *str, INT count, INT *dx, SIZE *size )
4096 TEXTMETRICW tm;
4097 PHYSDEV dev;
4099 size->cx = size->cy = 0;
4100 if (!count) return TRUE;
4102 dev = GET_DC_PHYSDEV( dc, pGetTextMetrics );
4103 dev->funcs->pGetTextMetrics( dev, &tm );
4105 dev = GET_DC_PHYSDEV( dc, pGetTextExtentExPoint );
4106 if (!dev->funcs->pGetTextExtentExPoint( dev, str, count, dx )) return FALSE;
4108 if (dc->breakExtra || dc->breakRem)
4110 int i, space = 0, rem = dc->breakRem;
4112 for (i = 0; i < count; i++)
4114 if (str[i] == tm.tmBreakChar)
4116 space += dc->breakExtra;
4117 if (rem > 0)
4119 space++;
4120 rem--;
4123 dx[i] += space;
4126 size->cx = dx[count - 1];
4127 size->cy = tm.tmHeight;
4128 return TRUE;
4131 /* compute positions for text rendering, in device coords */
4132 static BOOL get_char_positions_indices( DC *dc, const WORD *indices, INT count, INT *dx, SIZE *size )
4134 TEXTMETRICW tm;
4135 PHYSDEV dev;
4137 size->cx = size->cy = 0;
4138 if (!count) return TRUE;
4140 dev = GET_DC_PHYSDEV( dc, pGetTextMetrics );
4141 dev->funcs->pGetTextMetrics( dev, &tm );
4143 dev = GET_DC_PHYSDEV( dc, pGetTextExtentExPointI );
4144 if (!dev->funcs->pGetTextExtentExPointI( dev, indices, count, dx )) return FALSE;
4146 if (dc->breakExtra || dc->breakRem)
4148 WORD space_index;
4149 int i, space = 0, rem = dc->breakRem;
4151 dev = GET_DC_PHYSDEV( dc, pGetGlyphIndices );
4152 dev->funcs->pGetGlyphIndices( dev, &tm.tmBreakChar, 1, &space_index, 0 );
4154 for (i = 0; i < count; i++)
4156 if (indices[i] == space_index)
4158 space += dc->breakExtra;
4159 if (rem > 0)
4161 space++;
4162 rem--;
4165 dx[i] += space;
4168 size->cx = dx[count - 1];
4169 size->cy = tm.tmHeight;
4170 return TRUE;
4173 /***********************************************************************
4174 * GdiGetCodePage (GDI32.@)
4176 DWORD WINAPI GdiGetCodePage( HDC hdc )
4178 UINT cp = CP_ACP;
4179 DC *dc = get_dc_ptr( hdc );
4181 if (dc)
4183 cp = dc->font_code_page;
4184 release_dc_ptr( dc );
4186 return cp;
4189 /***********************************************************************
4190 * get_text_charset_info
4192 * Internal version of GetTextCharsetInfo() that takes a DC pointer.
4194 static UINT get_text_charset_info(DC *dc, FONTSIGNATURE *fs, DWORD flags)
4196 UINT ret = DEFAULT_CHARSET;
4197 PHYSDEV dev;
4199 dev = GET_DC_PHYSDEV( dc, pGetTextCharsetInfo );
4200 ret = dev->funcs->pGetTextCharsetInfo( dev, fs, flags );
4202 if (ret == DEFAULT_CHARSET && fs)
4203 memset(fs, 0, sizeof(FONTSIGNATURE));
4204 return ret;
4207 /***********************************************************************
4208 * NtGdiGetTextCharsetInfo (win32u.@)
4210 UINT WINAPI NtGdiGetTextCharsetInfo( HDC hdc, FONTSIGNATURE *fs, DWORD flags )
4212 UINT ret = DEFAULT_CHARSET;
4213 DC *dc = get_dc_ptr(hdc);
4215 if (dc)
4217 ret = get_text_charset_info( dc, fs, flags );
4218 release_dc_ptr( dc );
4220 return ret;
4223 /***********************************************************************
4224 * FONT_mbtowc
4226 * Returns a Unicode translation of str using the charset of the
4227 * currently selected font in hdc. If count is -1 then str is assumed
4228 * to be '\0' terminated, otherwise it contains the number of bytes to
4229 * convert. If plenW is non-NULL, on return it will point to the
4230 * number of WCHARs that have been written. If pCP is non-NULL, on
4231 * return it will point to the codepage used in the conversion. The
4232 * caller should free the returned LPWSTR from the process heap
4233 * itself.
4235 static LPWSTR FONT_mbtowc(HDC hdc, LPCSTR str, INT count, INT *plenW, UINT *pCP)
4237 UINT cp;
4238 INT lenW;
4239 LPWSTR strW;
4241 cp = GdiGetCodePage( hdc );
4243 if(count == -1) count = strlen(str);
4244 lenW = MultiByteToWideChar(cp, 0, str, count, NULL, 0);
4245 strW = HeapAlloc(GetProcessHeap(), 0, lenW*sizeof(WCHAR));
4246 MultiByteToWideChar(cp, 0, str, count, strW, lenW);
4247 TRACE("mapped %s -> %s\n", debugstr_an(str, count), debugstr_wn(strW, lenW));
4248 if(plenW) *plenW = lenW;
4249 if(pCP) *pCP = cp;
4250 return strW;
4253 /***********************************************************************
4254 * CreateFontIndirectExA (GDI32.@)
4256 HFONT WINAPI CreateFontIndirectExA( const ENUMLOGFONTEXDVA *penumexA )
4258 ENUMLOGFONTEXDVW enumexW;
4260 if (!penumexA) return 0;
4262 FONT_EnumLogFontExAToW( &penumexA->elfEnumLogfontEx, &enumexW.elfEnumLogfontEx );
4263 enumexW.elfDesignVector = penumexA->elfDesignVector;
4264 return CreateFontIndirectExW( &enumexW );
4267 /***********************************************************************
4268 * CreateFontIndirectExW (GDI32.@)
4270 HFONT WINAPI CreateFontIndirectExW( const ENUMLOGFONTEXDVW *penumex )
4272 HFONT hFont;
4273 FONTOBJ *fontPtr;
4274 const LOGFONTW *plf;
4276 if (!penumex) return 0;
4278 if (penumex->elfEnumLogfontEx.elfFullName[0] ||
4279 penumex->elfEnumLogfontEx.elfStyle[0] ||
4280 penumex->elfEnumLogfontEx.elfScript[0])
4282 FIXME("some fields ignored. fullname=%s, style=%s, script=%s\n",
4283 debugstr_w(penumex->elfEnumLogfontEx.elfFullName),
4284 debugstr_w(penumex->elfEnumLogfontEx.elfStyle),
4285 debugstr_w(penumex->elfEnumLogfontEx.elfScript));
4288 plf = &penumex->elfEnumLogfontEx.elfLogFont;
4289 if (!(fontPtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*fontPtr) ))) return 0;
4291 fontPtr->logfont = *plf;
4293 if (!(hFont = alloc_gdi_handle( &fontPtr->obj, NTGDI_OBJ_FONT, &fontobj_funcs )))
4295 HeapFree( GetProcessHeap(), 0, fontPtr );
4296 return 0;
4299 TRACE("(%d %d %d %d %x %d %x %d %d) %s %s %s %s => %p\n",
4300 plf->lfHeight, plf->lfWidth,
4301 plf->lfEscapement, plf->lfOrientation,
4302 plf->lfPitchAndFamily,
4303 plf->lfOutPrecision, plf->lfClipPrecision,
4304 plf->lfQuality, plf->lfCharSet,
4305 debugstr_w(plf->lfFaceName),
4306 plf->lfWeight > 400 ? "Bold" : "",
4307 plf->lfItalic ? "Italic" : "",
4308 plf->lfUnderline ? "Underline" : "", hFont);
4310 return hFont;
4313 /***********************************************************************
4314 * CreateFontIndirectA (GDI32.@)
4316 HFONT WINAPI CreateFontIndirectA( const LOGFONTA *plfA )
4318 LOGFONTW lfW;
4320 if (!plfA) return 0;
4322 FONT_LogFontAToW( plfA, &lfW );
4323 return CreateFontIndirectW( &lfW );
4326 /***********************************************************************
4327 * CreateFontIndirectW (GDI32.@)
4329 HFONT WINAPI CreateFontIndirectW( const LOGFONTW *plf )
4331 ENUMLOGFONTEXDVW exdv;
4333 if (!plf) return 0;
4335 exdv.elfEnumLogfontEx.elfLogFont = *plf;
4336 exdv.elfEnumLogfontEx.elfFullName[0] = 0;
4337 exdv.elfEnumLogfontEx.elfStyle[0] = 0;
4338 exdv.elfEnumLogfontEx.elfScript[0] = 0;
4339 return CreateFontIndirectExW( &exdv );
4342 /*************************************************************************
4343 * CreateFontA (GDI32.@)
4345 HFONT WINAPI CreateFontA( INT height, INT width, INT esc,
4346 INT orient, INT weight, DWORD italic,
4347 DWORD underline, DWORD strikeout, DWORD charset,
4348 DWORD outpres, DWORD clippres, DWORD quality,
4349 DWORD pitch, LPCSTR name )
4351 LOGFONTA logfont;
4353 logfont.lfHeight = height;
4354 logfont.lfWidth = width;
4355 logfont.lfEscapement = esc;
4356 logfont.lfOrientation = orient;
4357 logfont.lfWeight = weight;
4358 logfont.lfItalic = italic;
4359 logfont.lfUnderline = underline;
4360 logfont.lfStrikeOut = strikeout;
4361 logfont.lfCharSet = charset;
4362 logfont.lfOutPrecision = outpres;
4363 logfont.lfClipPrecision = clippres;
4364 logfont.lfQuality = quality;
4365 logfont.lfPitchAndFamily = pitch;
4367 if (name)
4368 lstrcpynA(logfont.lfFaceName,name,sizeof(logfont.lfFaceName));
4369 else
4370 logfont.lfFaceName[0] = '\0';
4372 return CreateFontIndirectA( &logfont );
4375 /*************************************************************************
4376 * CreateFontW (GDI32.@)
4378 HFONT WINAPI CreateFontW( INT height, INT width, INT esc,
4379 INT orient, INT weight, DWORD italic,
4380 DWORD underline, DWORD strikeout, DWORD charset,
4381 DWORD outpres, DWORD clippres, DWORD quality,
4382 DWORD pitch, LPCWSTR name )
4384 LOGFONTW logfont;
4386 logfont.lfHeight = height;
4387 logfont.lfWidth = width;
4388 logfont.lfEscapement = esc;
4389 logfont.lfOrientation = orient;
4390 logfont.lfWeight = weight;
4391 logfont.lfItalic = italic;
4392 logfont.lfUnderline = underline;
4393 logfont.lfStrikeOut = strikeout;
4394 logfont.lfCharSet = charset;
4395 logfont.lfOutPrecision = outpres;
4396 logfont.lfClipPrecision = clippres;
4397 logfont.lfQuality = quality;
4398 logfont.lfPitchAndFamily = pitch;
4400 if (name)
4401 lstrcpynW(logfont.lfFaceName, name, ARRAY_SIZE(logfont.lfFaceName));
4402 else
4403 logfont.lfFaceName[0] = '\0';
4405 return CreateFontIndirectW( &logfont );
4408 #define ASSOC_CHARSET_OEM 1
4409 #define ASSOC_CHARSET_ANSI 2
4410 #define ASSOC_CHARSET_SYMBOL 4
4412 static DWORD get_associated_charset_info(void)
4414 static DWORD associated_charset = -1;
4416 if (associated_charset == -1)
4418 HKEY hkey;
4419 WCHAR dataW[32];
4420 DWORD type, data_len;
4422 associated_charset = 0;
4424 if (RegOpenKeyW(HKEY_LOCAL_MACHINE,
4425 L"System\\CurrentControlSet\\Control\\FontAssoc\\Associated Charset", &hkey))
4426 return 0;
4428 data_len = sizeof(dataW);
4429 if (!RegQueryValueExW(hkey, L"ANSI(00)", NULL, &type, (LPBYTE)dataW, &data_len) &&
4430 type == REG_SZ && !wcsicmp(dataW, L"yes"))
4431 associated_charset |= ASSOC_CHARSET_ANSI;
4433 data_len = sizeof(dataW);
4434 if (!RegQueryValueExW(hkey, L"OEM(FF)", NULL, &type, (LPBYTE)dataW, &data_len) &&
4435 type == REG_SZ && !wcsicmp(dataW, L"yes"))
4436 associated_charset |= ASSOC_CHARSET_OEM;
4438 data_len = sizeof(dataW);
4439 if (!RegQueryValueExW(hkey, L"SYMBOL(02)", NULL, &type, (LPBYTE)dataW, &data_len) &&
4440 type == REG_SZ && !wcsicmp(dataW, L"yes"))
4441 associated_charset |= ASSOC_CHARSET_SYMBOL;
4443 RegCloseKey(hkey);
4445 TRACE("associated_charset = %d\n", associated_charset);
4448 return associated_charset;
4451 static void update_font_code_page( DC *dc, HANDLE font )
4453 CHARSETINFO csi;
4454 int charset = get_text_charset_info( dc, NULL, 0 );
4456 if (charset == ANSI_CHARSET && get_associated_charset_info() & ASSOC_CHARSET_ANSI)
4458 LOGFONTW lf;
4460 GetObjectW( font, sizeof(lf), &lf );
4461 if (!(lf.lfClipPrecision & CLIP_DFA_DISABLE))
4462 charset = DEFAULT_CHARSET;
4465 /* Hmm, nicely designed api this one! */
4466 if (TranslateCharsetInfo( ULongToPtr(charset), &csi, TCI_SRCCHARSET) )
4467 dc->font_code_page = csi.ciACP;
4468 else {
4469 switch(charset) {
4470 case OEM_CHARSET:
4471 dc->font_code_page = GetOEMCP();
4472 break;
4473 case DEFAULT_CHARSET:
4474 dc->font_code_page = GetACP();
4475 break;
4477 case VISCII_CHARSET:
4478 case TCVN_CHARSET:
4479 case KOI8_CHARSET:
4480 case ISO3_CHARSET:
4481 case ISO4_CHARSET:
4482 case ISO10_CHARSET:
4483 case CELTIC_CHARSET:
4484 /* FIXME: These have no place here, but because x11drv
4485 enumerates fonts with these (made up) charsets some apps
4486 might use them and then the FIXME below would become
4487 annoying. Now we could pick the intended codepage for
4488 each of these, but since it's broken anyway we'll just
4489 use CP_ACP and hope it'll go away...
4491 dc->font_code_page = CP_ACP;
4492 break;
4494 default:
4495 FIXME("Can't find codepage for charset %d\n", charset);
4496 dc->font_code_page = CP_ACP;
4497 break;
4501 TRACE("charset %d => cp %d\n", charset, dc->font_code_page);
4504 /***********************************************************************
4505 * NtGdiSelectFont (win32u.@)
4507 HGDIOBJ WINAPI NtGdiSelectFont( HDC hdc, HGDIOBJ handle )
4509 HGDIOBJ ret = 0;
4510 DC *dc = get_dc_ptr( hdc );
4511 PHYSDEV physdev;
4512 UINT aa_flags = 0;
4514 if (!dc) return 0;
4516 if (!GDI_inc_ref_count( handle ))
4518 release_dc_ptr( dc );
4519 return 0;
4522 physdev = GET_DC_PHYSDEV( dc, pSelectFont );
4523 if (physdev->funcs->pSelectFont( physdev, handle, &aa_flags ))
4525 ret = dc->hFont;
4526 dc->hFont = handle;
4527 dc->aa_flags = aa_flags ? aa_flags : GGO_BITMAP;
4528 update_font_code_page( dc, handle );
4529 if (dc->font_gamma_ramp == NULL)
4530 dc->font_gamma_ramp = &font_gamma_ramp;
4531 GDI_dec_ref_count( ret );
4533 else GDI_dec_ref_count( handle );
4535 release_dc_ptr( dc );
4536 return ret;
4540 /***********************************************************************
4541 * FONT_GetObjectW
4543 static INT FONT_GetObjectW( HGDIOBJ handle, INT count, LPVOID buffer )
4545 FONTOBJ *font = GDI_GetObjPtr( handle, NTGDI_OBJ_FONT );
4547 if (!font) return 0;
4548 if (buffer)
4550 if (count > sizeof(LOGFONTW)) count = sizeof(LOGFONTW);
4551 memcpy( buffer, &font->logfont, count );
4553 else count = sizeof(LOGFONTW);
4554 GDI_ReleaseObj( handle );
4555 return count;
4559 /***********************************************************************
4560 * FONT_DeleteObject
4562 static BOOL FONT_DeleteObject( HGDIOBJ handle )
4564 FONTOBJ *obj;
4566 if (!(obj = free_gdi_handle( handle ))) return FALSE;
4567 HeapFree( GetProcessHeap(), 0, obj );
4568 return TRUE;
4572 /***********************************************************************
4573 * FONT_EnumInstance
4575 * Note: plf is really an ENUMLOGFONTEXW, and ptm is a NEWTEXTMETRICEXW.
4576 * We have to use other types because of the FONTENUMPROCW definition.
4578 static INT CALLBACK FONT_EnumInstance( const LOGFONTW *plf, const TEXTMETRICW *ptm,
4579 DWORD fType, LPARAM lp )
4581 struct font_enum *pfe = (struct font_enum *)lp;
4582 INT ret = 1;
4584 /* lfCharSet is at the same offset in both LOGFONTA and LOGFONTW */
4585 if ((!pfe->lpLogFontParam ||
4586 pfe->lpLogFontParam->lfCharSet == DEFAULT_CHARSET ||
4587 pfe->lpLogFontParam->lfCharSet == plf->lfCharSet) &&
4588 (!(fType & RASTER_FONTTYPE) || GetDeviceCaps(pfe->hdc, TEXTCAPS) & TC_RA_ABLE) )
4590 /* convert font metrics */
4591 ENUMLOGFONTEXA logfont;
4592 NEWTEXTMETRICEXA tmA;
4594 if (!pfe->unicode)
4596 FONT_EnumLogFontExWToA( (const ENUMLOGFONTEXW *)plf, &logfont);
4597 FONT_NewTextMetricExWToA( (const NEWTEXTMETRICEXW *)ptm, &tmA );
4598 plf = (LOGFONTW *)&logfont.elfLogFont;
4599 ptm = (TEXTMETRICW *)&tmA;
4601 ret = pfe->lpEnumFunc( plf, ptm, fType, pfe->lpData );
4602 pfe->retval = ret;
4604 return ret;
4607 /***********************************************************************
4608 * FONT_EnumFontFamiliesEx
4610 static INT FONT_EnumFontFamiliesEx( HDC hDC, LPLOGFONTW plf, FONTENUMPROCW efproc,
4611 LPARAM lParam, BOOL unicode )
4613 INT ret = 0;
4614 DC *dc = get_dc_ptr( hDC );
4615 struct font_enum fe;
4617 if (dc)
4619 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEnumFonts );
4621 if (plf) TRACE("lfFaceName = %s lfCharset = %d\n", debugstr_w(plf->lfFaceName), plf->lfCharSet);
4622 fe.lpLogFontParam = plf;
4623 fe.lpEnumFunc = efproc;
4624 fe.lpData = lParam;
4625 fe.unicode = unicode;
4626 fe.hdc = hDC;
4627 fe.retval = 1;
4628 ret = physdev->funcs->pEnumFonts( physdev, plf, FONT_EnumInstance, (LPARAM)&fe );
4629 release_dc_ptr( dc );
4631 return ret ? fe.retval : 0;
4634 /***********************************************************************
4635 * EnumFontFamiliesExW (GDI32.@)
4637 INT WINAPI EnumFontFamiliesExW( HDC hDC, LPLOGFONTW plf,
4638 FONTENUMPROCW efproc,
4639 LPARAM lParam, DWORD dwFlags )
4641 return FONT_EnumFontFamiliesEx( hDC, plf, efproc, lParam, TRUE );
4644 /***********************************************************************
4645 * EnumFontFamiliesExA (GDI32.@)
4647 INT WINAPI EnumFontFamiliesExA( HDC hDC, LPLOGFONTA plf,
4648 FONTENUMPROCA efproc,
4649 LPARAM lParam, DWORD dwFlags)
4651 LOGFONTW lfW, *plfW;
4653 if (plf)
4655 FONT_LogFontAToW( plf, &lfW );
4656 plfW = &lfW;
4658 else plfW = NULL;
4660 return FONT_EnumFontFamiliesEx( hDC, plfW, (FONTENUMPROCW)efproc, lParam, FALSE );
4663 /***********************************************************************
4664 * EnumFontFamiliesA (GDI32.@)
4666 INT WINAPI EnumFontFamiliesA( HDC hDC, LPCSTR lpFamily,
4667 FONTENUMPROCA efproc, LPARAM lpData )
4669 LOGFONTA lf, *plf;
4671 if (lpFamily)
4673 if (!*lpFamily) return 1;
4674 lstrcpynA( lf.lfFaceName, lpFamily, LF_FACESIZE );
4675 lf.lfCharSet = DEFAULT_CHARSET;
4676 lf.lfPitchAndFamily = 0;
4677 plf = &lf;
4679 else plf = NULL;
4681 return EnumFontFamiliesExA( hDC, plf, efproc, lpData, 0 );
4684 /***********************************************************************
4685 * EnumFontFamiliesW (GDI32.@)
4687 INT WINAPI EnumFontFamiliesW( HDC hDC, LPCWSTR lpFamily,
4688 FONTENUMPROCW efproc, LPARAM lpData )
4690 LOGFONTW lf, *plf;
4692 if (lpFamily)
4694 if (!*lpFamily) return 1;
4695 lstrcpynW( lf.lfFaceName, lpFamily, LF_FACESIZE );
4696 lf.lfCharSet = DEFAULT_CHARSET;
4697 lf.lfPitchAndFamily = 0;
4698 plf = &lf;
4700 else plf = NULL;
4702 return EnumFontFamiliesExW( hDC, plf, efproc, lpData, 0 );
4705 /***********************************************************************
4706 * EnumFontsA (GDI32.@)
4708 INT WINAPI EnumFontsA( HDC hDC, LPCSTR lpName, FONTENUMPROCA efproc,
4709 LPARAM lpData )
4711 return EnumFontFamiliesA( hDC, lpName, efproc, lpData );
4714 /***********************************************************************
4715 * EnumFontsW (GDI32.@)
4717 INT WINAPI EnumFontsW( HDC hDC, LPCWSTR lpName, FONTENUMPROCW efproc,
4718 LPARAM lpData )
4720 return EnumFontFamiliesW( hDC, lpName, efproc, lpData );
4724 /***********************************************************************
4725 * NtGdiSetTextJustification (win32u.@)
4727 BOOL WINAPI NtGdiSetTextJustification( HDC hdc, INT extra, INT breaks )
4729 DC *dc;
4731 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
4733 extra = abs( (extra * dc->attr->vport_ext.cx + dc->attr->wnd_ext.cx / 2) /
4734 dc->attr->wnd_ext.cx );
4735 if (!extra) breaks = 0;
4736 if (breaks)
4738 dc->breakExtra = extra / breaks;
4739 dc->breakRem = extra - (breaks * dc->breakExtra);
4741 else
4743 dc->breakExtra = 0;
4744 dc->breakRem = 0;
4747 release_dc_ptr( dc );
4748 return TRUE;
4752 /***********************************************************************
4753 * GetTextFaceA (GDI32.@)
4755 INT WINAPI GetTextFaceA( HDC hdc, INT count, LPSTR name )
4757 INT res = GetTextFaceW(hdc, 0, NULL);
4758 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, res * 2 );
4759 GetTextFaceW( hdc, res, nameW );
4761 if (name)
4763 if (count)
4765 res = WideCharToMultiByte(CP_ACP, 0, nameW, -1, name, count, NULL, NULL);
4766 if (res == 0)
4767 res = count;
4768 name[count-1] = 0;
4769 /* GetTextFaceA does NOT include the nul byte in the return count. */
4770 res--;
4772 else
4773 res = 0;
4775 else
4776 res = WideCharToMultiByte( CP_ACP, 0, nameW, -1, NULL, 0, NULL, NULL);
4777 HeapFree( GetProcessHeap(), 0, nameW );
4778 return res;
4781 /***********************************************************************
4782 * GetTextFaceW (GDI32.@)
4784 INT WINAPI GetTextFaceW( HDC hdc, INT count, LPWSTR name )
4786 PHYSDEV dev;
4787 INT ret;
4789 DC * dc = get_dc_ptr( hdc );
4790 if (!dc) return 0;
4792 dev = GET_DC_PHYSDEV( dc, pGetTextFace );
4793 ret = dev->funcs->pGetTextFace( dev, count, name );
4794 release_dc_ptr( dc );
4795 return ret;
4799 /***********************************************************************
4800 * GetTextExtentPoint32A (GDI32.@)
4802 * See GetTextExtentPoint32W.
4804 BOOL WINAPI GetTextExtentPoint32A( HDC hdc, LPCSTR str, INT count,
4805 LPSIZE size )
4807 BOOL ret = FALSE;
4808 INT wlen;
4809 LPWSTR p;
4811 if (count < 0) return FALSE;
4813 p = FONT_mbtowc(hdc, str, count, &wlen, NULL);
4815 if (p)
4817 ret = GetTextExtentPoint32W( hdc, p, wlen, size );
4818 HeapFree( GetProcessHeap(), 0, p );
4821 TRACE("(%p %s %d %p): returning %d x %d\n",
4822 hdc, debugstr_an (str, count), count, size, size->cx, size->cy );
4823 return ret;
4827 /***********************************************************************
4828 * GetTextExtentPoint32W [GDI32.@]
4830 * Computes width/height for a string.
4832 * Computes width and height of the specified string.
4834 * RETURNS
4835 * Success: TRUE
4836 * Failure: FALSE
4838 BOOL WINAPI GetTextExtentPoint32W(
4839 HDC hdc, /* [in] Handle of device context */
4840 LPCWSTR str, /* [in] Address of text string */
4841 INT count, /* [in] Number of characters in string */
4842 LPSIZE size) /* [out] Address of structure for string size */
4844 return GetTextExtentExPointW(hdc, str, count, 0, NULL, NULL, size);
4847 /***********************************************************************
4848 * GetTextExtentExPointI [GDI32.@]
4850 * Computes width and height of the array of glyph indices.
4852 * PARAMS
4853 * hdc [I] Handle of device context.
4854 * indices [I] Glyph index array.
4855 * count [I] Number of glyphs in array.
4856 * max_ext [I] Maximum width in glyphs.
4857 * nfit [O] Maximum number of characters.
4858 * dxs [O] Partial string widths.
4859 * size [O] Returned string size.
4861 * RETURNS
4862 * Success: TRUE
4863 * Failure: FALSE
4865 BOOL WINAPI GetTextExtentExPointI( HDC hdc, const WORD *indices, INT count, INT max_ext,
4866 LPINT nfit, LPINT dxs, LPSIZE size )
4868 DC *dc;
4869 int i;
4870 BOOL ret;
4871 INT buffer[256], *pos = dxs;
4873 if (count < 0) return FALSE;
4875 dc = get_dc_ptr( hdc );
4876 if (!dc) return FALSE;
4878 if (!dxs)
4880 pos = buffer;
4881 if (count > 256 && !(pos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*pos) )))
4883 release_dc_ptr( dc );
4884 return FALSE;
4888 ret = get_char_positions_indices( dc, indices, count, pos, size );
4889 if (ret)
4891 if (dxs || nfit)
4893 for (i = 0; i < count; i++)
4895 unsigned int dx = abs( INTERNAL_XDSTOWS( dc, pos[i] )) +
4896 (i + 1) * dc->attr->char_extra;
4897 if (nfit && dx > (unsigned int)max_ext) break;
4898 if (dxs) dxs[i] = dx;
4900 if (nfit) *nfit = i;
4903 size->cx = abs( INTERNAL_XDSTOWS( dc, size->cx )) + count * dc->attr->char_extra;
4904 size->cy = abs( INTERNAL_YDSTOWS( dc, size->cy ));
4907 if (pos != buffer && pos != dxs) HeapFree( GetProcessHeap(), 0, pos );
4908 release_dc_ptr( dc );
4910 TRACE("(%p %p %d %p): returning %d x %d\n",
4911 hdc, indices, count, size, size->cx, size->cy );
4912 return ret;
4915 /***********************************************************************
4916 * GetTextExtentPointI [GDI32.@]
4918 * Computes width and height of the array of glyph indices.
4920 * PARAMS
4921 * hdc [I] Handle of device context.
4922 * indices [I] Glyph index array.
4923 * count [I] Number of glyphs in array.
4924 * size [O] Returned string size.
4926 * RETURNS
4927 * Success: TRUE
4928 * Failure: FALSE
4930 BOOL WINAPI GetTextExtentPointI( HDC hdc, const WORD *indices, INT count, LPSIZE size )
4932 return GetTextExtentExPointI( hdc, indices, count, 0, NULL, NULL, size );
4936 /***********************************************************************
4937 * GetTextExtentPointA (GDI32.@)
4939 BOOL WINAPI GetTextExtentPointA( HDC hdc, LPCSTR str, INT count,
4940 LPSIZE size )
4942 TRACE("not bug compatible.\n");
4943 return GetTextExtentPoint32A( hdc, str, count, size );
4946 /***********************************************************************
4947 * GetTextExtentPointW (GDI32.@)
4949 BOOL WINAPI GetTextExtentPointW( HDC hdc, LPCWSTR str, INT count,
4950 LPSIZE size )
4952 TRACE("not bug compatible.\n");
4953 return GetTextExtentPoint32W( hdc, str, count, size );
4957 /***********************************************************************
4958 * GetTextExtentExPointA (GDI32.@)
4960 BOOL WINAPI GetTextExtentExPointA( HDC hdc, LPCSTR str, INT count,
4961 INT maxExt, LPINT lpnFit,
4962 LPINT alpDx, LPSIZE size )
4964 BOOL ret;
4965 INT wlen;
4966 INT *walpDx = NULL;
4967 LPWSTR p = NULL;
4969 if (count < 0) return FALSE;
4970 if (maxExt < -1) return FALSE;
4972 if (alpDx)
4974 walpDx = HeapAlloc( GetProcessHeap(), 0, count * sizeof(INT) );
4975 if (!walpDx) return FALSE;
4978 p = FONT_mbtowc(hdc, str, count, &wlen, NULL);
4979 ret = GetTextExtentExPointW( hdc, p, wlen, maxExt, lpnFit, walpDx, size);
4980 if (walpDx)
4982 INT n = lpnFit ? *lpnFit : wlen;
4983 INT i, j;
4984 for(i = 0, j = 0; i < n; i++, j++)
4986 alpDx[j] = walpDx[i];
4987 if (IsDBCSLeadByte(str[j])) alpDx[++j] = walpDx[i];
4990 if (lpnFit) *lpnFit = WideCharToMultiByte(CP_ACP,0,p,*lpnFit,NULL,0,NULL,NULL);
4991 HeapFree( GetProcessHeap(), 0, p );
4992 HeapFree( GetProcessHeap(), 0, walpDx );
4993 return ret;
4997 /***********************************************************************
4998 * GetTextExtentExPointW (GDI32.@)
5000 * Return the size of the string as it would be if it was output properly by
5001 * e.g. TextOut.
5003 BOOL WINAPI GetTextExtentExPointW( HDC hdc, LPCWSTR str, INT count, INT max_ext,
5004 LPINT nfit, LPINT dxs, LPSIZE size )
5006 DC *dc;
5007 int i;
5008 BOOL ret;
5009 INT buffer[256], *pos = dxs;
5011 if (count < 0) return FALSE;
5013 dc = get_dc_ptr(hdc);
5014 if (!dc) return FALSE;
5016 if (!dxs)
5018 pos = buffer;
5019 if (count > 256 && !(pos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*pos) )))
5021 release_dc_ptr( dc );
5022 return FALSE;
5026 ret = get_char_positions( dc, str, count, pos, size );
5027 if (ret)
5029 if (dxs || nfit)
5031 for (i = 0; i < count; i++)
5033 unsigned int dx = abs( INTERNAL_XDSTOWS( dc, pos[i] )) +
5034 (i + 1) * dc->attr->char_extra;
5035 if (nfit && dx > (unsigned int)max_ext) break;
5036 if (dxs) dxs[i] = dx;
5038 if (nfit) *nfit = i;
5041 size->cx = abs( INTERNAL_XDSTOWS( dc, size->cx )) + count * dc->attr->char_extra;
5042 size->cy = abs( INTERNAL_YDSTOWS( dc, size->cy ));
5045 if (pos != buffer && pos != dxs) HeapFree( GetProcessHeap(), 0, pos );
5046 release_dc_ptr( dc );
5048 TRACE("(%p, %s, %d) returning %dx%d\n", hdc, debugstr_wn(str,count), max_ext, size->cx, size->cy );
5049 return ret;
5052 /***********************************************************************
5053 * GetTextMetricsA (GDI32.@)
5055 BOOL WINAPI GetTextMetricsA( HDC hdc, TEXTMETRICA *metrics )
5057 TEXTMETRICW tm32;
5059 if (!GetTextMetricsW( hdc, &tm32 )) return FALSE;
5060 FONT_TextMetricWToA( &tm32, metrics );
5061 return TRUE;
5064 /***********************************************************************
5065 * GetTextMetricsW (GDI32.@)
5067 BOOL WINAPI GetTextMetricsW( HDC hdc, TEXTMETRICW *metrics )
5069 PHYSDEV physdev;
5070 BOOL ret = FALSE;
5071 DC * dc = get_dc_ptr( hdc );
5072 if (!dc) return FALSE;
5074 physdev = GET_DC_PHYSDEV( dc, pGetTextMetrics );
5075 ret = physdev->funcs->pGetTextMetrics( physdev, metrics );
5077 if (ret)
5079 /* device layer returns values in device units
5080 * therefore we have to convert them to logical */
5082 metrics->tmDigitizedAspectX = GetDeviceCaps(hdc, LOGPIXELSX);
5083 metrics->tmDigitizedAspectY = GetDeviceCaps(hdc, LOGPIXELSY);
5084 metrics->tmHeight = height_to_LP( dc, metrics->tmHeight );
5085 metrics->tmAscent = height_to_LP( dc, metrics->tmAscent );
5086 metrics->tmDescent = height_to_LP( dc, metrics->tmDescent );
5087 metrics->tmInternalLeading = height_to_LP( dc, metrics->tmInternalLeading );
5088 metrics->tmExternalLeading = height_to_LP( dc, metrics->tmExternalLeading );
5089 metrics->tmAveCharWidth = width_to_LP( dc, metrics->tmAveCharWidth );
5090 metrics->tmMaxCharWidth = width_to_LP( dc, metrics->tmMaxCharWidth );
5091 metrics->tmOverhang = width_to_LP( dc, metrics->tmOverhang );
5092 ret = TRUE;
5094 TRACE("text metrics:\n"
5095 " Weight = %03i\t FirstChar = %i\t AveCharWidth = %i\n"
5096 " Italic = % 3i\t LastChar = %i\t\t MaxCharWidth = %i\n"
5097 " UnderLined = %01i\t DefaultChar = %i\t Overhang = %i\n"
5098 " StruckOut = %01i\t BreakChar = %i\t CharSet = %i\n"
5099 " PitchAndFamily = %02x\n"
5100 " --------------------\n"
5101 " InternalLeading = %i\n"
5102 " Ascent = %i\n"
5103 " Descent = %i\n"
5104 " Height = %i\n",
5105 metrics->tmWeight, metrics->tmFirstChar, metrics->tmAveCharWidth,
5106 metrics->tmItalic, metrics->tmLastChar, metrics->tmMaxCharWidth,
5107 metrics->tmUnderlined, metrics->tmDefaultChar, metrics->tmOverhang,
5108 metrics->tmStruckOut, metrics->tmBreakChar, metrics->tmCharSet,
5109 metrics->tmPitchAndFamily,
5110 metrics->tmInternalLeading,
5111 metrics->tmAscent,
5112 metrics->tmDescent,
5113 metrics->tmHeight );
5115 release_dc_ptr( dc );
5116 return ret;
5120 /***********************************************************************
5121 * GetOutlineTextMetricsA (GDI32.@)
5122 * Gets metrics for TrueType fonts.
5124 * NOTES
5125 * If the supplied buffer isn't big enough Windows partially fills it up to
5126 * its given length and returns that length.
5128 * RETURNS
5129 * Success: Non-zero or size of required buffer
5130 * Failure: 0
5132 UINT WINAPI GetOutlineTextMetricsA(
5133 HDC hdc, /* [in] Handle of device context */
5134 UINT cbData, /* [in] Size of metric data array */
5135 LPOUTLINETEXTMETRICA lpOTM) /* [out] Address of metric data array */
5137 char buf[512], *ptr;
5138 UINT ret, needed;
5139 OUTLINETEXTMETRICW *lpOTMW = (OUTLINETEXTMETRICW *)buf;
5140 OUTLINETEXTMETRICA *output = lpOTM;
5141 INT left, len;
5143 if((ret = GetOutlineTextMetricsW(hdc, 0, NULL)) == 0)
5144 return 0;
5145 if(ret > sizeof(buf))
5146 lpOTMW = HeapAlloc(GetProcessHeap(), 0, ret);
5147 GetOutlineTextMetricsW(hdc, ret, lpOTMW);
5149 needed = sizeof(OUTLINETEXTMETRICA);
5150 if(lpOTMW->otmpFamilyName)
5151 needed += WideCharToMultiByte(CP_ACP, 0,
5152 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFamilyName), -1,
5153 NULL, 0, NULL, NULL);
5154 if(lpOTMW->otmpFaceName)
5155 needed += WideCharToMultiByte(CP_ACP, 0,
5156 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFaceName), -1,
5157 NULL, 0, NULL, NULL);
5158 if(lpOTMW->otmpStyleName)
5159 needed += WideCharToMultiByte(CP_ACP, 0,
5160 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpStyleName), -1,
5161 NULL, 0, NULL, NULL);
5162 if(lpOTMW->otmpFullName)
5163 needed += WideCharToMultiByte(CP_ACP, 0,
5164 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFullName), -1,
5165 NULL, 0, NULL, NULL);
5167 if(!lpOTM) {
5168 ret = needed;
5169 goto end;
5172 TRACE("needed = %d\n", needed);
5173 if(needed > cbData)
5174 /* Since the supplied buffer isn't big enough, we'll alloc one
5175 that is and memcpy the first cbData bytes into the lpOTM at
5176 the end. */
5177 output = HeapAlloc(GetProcessHeap(), 0, needed);
5179 ret = output->otmSize = min(needed, cbData);
5180 FONT_TextMetricWToA( &lpOTMW->otmTextMetrics, &output->otmTextMetrics );
5181 output->otmFiller = 0;
5182 output->otmPanoseNumber = lpOTMW->otmPanoseNumber;
5183 output->otmfsSelection = lpOTMW->otmfsSelection;
5184 output->otmfsType = lpOTMW->otmfsType;
5185 output->otmsCharSlopeRise = lpOTMW->otmsCharSlopeRise;
5186 output->otmsCharSlopeRun = lpOTMW->otmsCharSlopeRun;
5187 output->otmItalicAngle = lpOTMW->otmItalicAngle;
5188 output->otmEMSquare = lpOTMW->otmEMSquare;
5189 output->otmAscent = lpOTMW->otmAscent;
5190 output->otmDescent = lpOTMW->otmDescent;
5191 output->otmLineGap = lpOTMW->otmLineGap;
5192 output->otmsCapEmHeight = lpOTMW->otmsCapEmHeight;
5193 output->otmsXHeight = lpOTMW->otmsXHeight;
5194 output->otmrcFontBox = lpOTMW->otmrcFontBox;
5195 output->otmMacAscent = lpOTMW->otmMacAscent;
5196 output->otmMacDescent = lpOTMW->otmMacDescent;
5197 output->otmMacLineGap = lpOTMW->otmMacLineGap;
5198 output->otmusMinimumPPEM = lpOTMW->otmusMinimumPPEM;
5199 output->otmptSubscriptSize = lpOTMW->otmptSubscriptSize;
5200 output->otmptSubscriptOffset = lpOTMW->otmptSubscriptOffset;
5201 output->otmptSuperscriptSize = lpOTMW->otmptSuperscriptSize;
5202 output->otmptSuperscriptOffset = lpOTMW->otmptSuperscriptOffset;
5203 output->otmsStrikeoutSize = lpOTMW->otmsStrikeoutSize;
5204 output->otmsStrikeoutPosition = lpOTMW->otmsStrikeoutPosition;
5205 output->otmsUnderscoreSize = lpOTMW->otmsUnderscoreSize;
5206 output->otmsUnderscorePosition = lpOTMW->otmsUnderscorePosition;
5209 ptr = (char*)(output + 1);
5210 left = needed - sizeof(*output);
5212 if(lpOTMW->otmpFamilyName) {
5213 output->otmpFamilyName = (LPSTR)(ptr - (char*)output);
5214 len = WideCharToMultiByte(CP_ACP, 0,
5215 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFamilyName), -1,
5216 ptr, left, NULL, NULL);
5217 left -= len;
5218 ptr += len;
5219 } else
5220 output->otmpFamilyName = 0;
5222 if(lpOTMW->otmpFaceName) {
5223 output->otmpFaceName = (LPSTR)(ptr - (char*)output);
5224 len = WideCharToMultiByte(CP_ACP, 0,
5225 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFaceName), -1,
5226 ptr, left, NULL, NULL);
5227 left -= len;
5228 ptr += len;
5229 } else
5230 output->otmpFaceName = 0;
5232 if(lpOTMW->otmpStyleName) {
5233 output->otmpStyleName = (LPSTR)(ptr - (char*)output);
5234 len = WideCharToMultiByte(CP_ACP, 0,
5235 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpStyleName), -1,
5236 ptr, left, NULL, NULL);
5237 left -= len;
5238 ptr += len;
5239 } else
5240 output->otmpStyleName = 0;
5242 if(lpOTMW->otmpFullName) {
5243 output->otmpFullName = (LPSTR)(ptr - (char*)output);
5244 len = WideCharToMultiByte(CP_ACP, 0,
5245 (WCHAR*)((char*)lpOTMW + (ptrdiff_t)lpOTMW->otmpFullName), -1,
5246 ptr, left, NULL, NULL);
5247 left -= len;
5248 } else
5249 output->otmpFullName = 0;
5251 assert(left == 0);
5253 if(output != lpOTM) {
5254 memcpy(lpOTM, output, cbData);
5255 HeapFree(GetProcessHeap(), 0, output);
5257 /* check if the string offsets really fit into the provided size */
5258 /* FIXME: should we check string length as well? */
5259 /* make sure that we don't read/write beyond the provided buffer */
5260 if (lpOTM->otmSize >= FIELD_OFFSET(OUTLINETEXTMETRICA, otmpFamilyName) + sizeof(LPSTR))
5262 if ((UINT_PTR)lpOTM->otmpFamilyName >= lpOTM->otmSize)
5263 lpOTM->otmpFamilyName = 0; /* doesn't fit */
5266 /* make sure that we don't read/write beyond the provided buffer */
5267 if (lpOTM->otmSize >= FIELD_OFFSET(OUTLINETEXTMETRICA, otmpFaceName) + sizeof(LPSTR))
5269 if ((UINT_PTR)lpOTM->otmpFaceName >= lpOTM->otmSize)
5270 lpOTM->otmpFaceName = 0; /* doesn't fit */
5273 /* make sure that we don't read/write beyond the provided buffer */
5274 if (lpOTM->otmSize >= FIELD_OFFSET(OUTLINETEXTMETRICA, otmpStyleName) + sizeof(LPSTR))
5276 if ((UINT_PTR)lpOTM->otmpStyleName >= lpOTM->otmSize)
5277 lpOTM->otmpStyleName = 0; /* doesn't fit */
5280 /* make sure that we don't read/write beyond the provided buffer */
5281 if (lpOTM->otmSize >= FIELD_OFFSET(OUTLINETEXTMETRICA, otmpFullName) + sizeof(LPSTR))
5283 if ((UINT_PTR)lpOTM->otmpFullName >= lpOTM->otmSize)
5284 lpOTM->otmpFullName = 0; /* doesn't fit */
5288 end:
5289 if(lpOTMW != (OUTLINETEXTMETRICW *)buf)
5290 HeapFree(GetProcessHeap(), 0, lpOTMW);
5292 return ret;
5296 /***********************************************************************
5297 * GetOutlineTextMetricsW [GDI32.@]
5299 UINT WINAPI GetOutlineTextMetricsW(
5300 HDC hdc, /* [in] Handle of device context */
5301 UINT cbData, /* [in] Size of metric data array */
5302 LPOUTLINETEXTMETRICW lpOTM) /* [out] Address of metric data array */
5304 DC *dc = get_dc_ptr( hdc );
5305 OUTLINETEXTMETRICW *output = lpOTM;
5306 PHYSDEV dev;
5307 UINT ret;
5309 TRACE("(%p,%d,%p)\n", hdc, cbData, lpOTM);
5310 if(!dc) return 0;
5312 dev = GET_DC_PHYSDEV( dc, pGetOutlineTextMetrics );
5313 ret = dev->funcs->pGetOutlineTextMetrics( dev, cbData, output );
5315 if (lpOTM && ret > cbData)
5317 output = HeapAlloc(GetProcessHeap(), 0, ret);
5318 ret = dev->funcs->pGetOutlineTextMetrics( dev, ret, output );
5321 if (lpOTM && ret)
5323 output->otmTextMetrics.tmDigitizedAspectX = GetDeviceCaps(hdc, LOGPIXELSX);
5324 output->otmTextMetrics.tmDigitizedAspectY = GetDeviceCaps(hdc, LOGPIXELSY);
5325 output->otmTextMetrics.tmHeight = height_to_LP( dc, output->otmTextMetrics.tmHeight );
5326 output->otmTextMetrics.tmAscent = height_to_LP( dc, output->otmTextMetrics.tmAscent );
5327 output->otmTextMetrics.tmDescent = height_to_LP( dc, output->otmTextMetrics.tmDescent );
5328 output->otmTextMetrics.tmInternalLeading = height_to_LP( dc, output->otmTextMetrics.tmInternalLeading );
5329 output->otmTextMetrics.tmExternalLeading = height_to_LP( dc, output->otmTextMetrics.tmExternalLeading );
5330 output->otmTextMetrics.tmAveCharWidth = width_to_LP( dc, output->otmTextMetrics.tmAveCharWidth );
5331 output->otmTextMetrics.tmMaxCharWidth = width_to_LP( dc, output->otmTextMetrics.tmMaxCharWidth );
5332 output->otmTextMetrics.tmOverhang = width_to_LP( dc, output->otmTextMetrics.tmOverhang );
5333 output->otmAscent = height_to_LP( dc, output->otmAscent);
5334 output->otmDescent = height_to_LP( dc, output->otmDescent);
5335 output->otmLineGap = INTERNAL_YDSTOWS(dc, output->otmLineGap);
5336 output->otmsCapEmHeight = INTERNAL_YDSTOWS(dc, output->otmsCapEmHeight);
5337 output->otmsXHeight = INTERNAL_YDSTOWS(dc, output->otmsXHeight);
5338 output->otmrcFontBox.top = height_to_LP( dc, output->otmrcFontBox.top);
5339 output->otmrcFontBox.bottom = height_to_LP( dc, output->otmrcFontBox.bottom);
5340 output->otmrcFontBox.left = width_to_LP( dc, output->otmrcFontBox.left);
5341 output->otmrcFontBox.right = width_to_LP( dc, output->otmrcFontBox.right);
5342 output->otmMacAscent = height_to_LP( dc, output->otmMacAscent);
5343 output->otmMacDescent = height_to_LP( dc, output->otmMacDescent);
5344 output->otmMacLineGap = INTERNAL_YDSTOWS(dc, output->otmMacLineGap);
5345 output->otmptSubscriptSize.x = width_to_LP( dc, output->otmptSubscriptSize.x);
5346 output->otmptSubscriptSize.y = height_to_LP( dc, output->otmptSubscriptSize.y);
5347 output->otmptSubscriptOffset.x = width_to_LP( dc, output->otmptSubscriptOffset.x);
5348 output->otmptSubscriptOffset.y = height_to_LP( dc, output->otmptSubscriptOffset.y);
5349 output->otmptSuperscriptSize.x = width_to_LP( dc, output->otmptSuperscriptSize.x);
5350 output->otmptSuperscriptSize.y = height_to_LP( dc, output->otmptSuperscriptSize.y);
5351 output->otmptSuperscriptOffset.x = width_to_LP( dc, output->otmptSuperscriptOffset.x);
5352 output->otmptSuperscriptOffset.y = height_to_LP( dc, output->otmptSuperscriptOffset.y);
5353 output->otmsStrikeoutSize = INTERNAL_YDSTOWS(dc, output->otmsStrikeoutSize);
5354 output->otmsStrikeoutPosition = height_to_LP( dc, output->otmsStrikeoutPosition);
5355 output->otmsUnderscoreSize = height_to_LP( dc, output->otmsUnderscoreSize);
5356 output->otmsUnderscorePosition = height_to_LP( dc, output->otmsUnderscorePosition);
5358 if(output != lpOTM)
5360 memcpy(lpOTM, output, cbData);
5361 HeapFree(GetProcessHeap(), 0, output);
5362 ret = cbData;
5365 release_dc_ptr(dc);
5366 return ret;
5369 static LPSTR FONT_GetCharsByRangeA(HDC hdc, UINT firstChar, UINT lastChar, PINT pByteLen)
5371 INT i, count = lastChar - firstChar + 1;
5372 UINT mbcp;
5373 UINT c;
5374 LPSTR str;
5376 if (count <= 0)
5377 return NULL;
5379 mbcp = GdiGetCodePage(hdc);
5380 switch (mbcp)
5382 case 932:
5383 case 936:
5384 case 949:
5385 case 950:
5386 case 1361:
5387 if (lastChar > 0xffff)
5388 return NULL;
5389 if ((firstChar ^ lastChar) > 0xff)
5390 return NULL;
5391 break;
5392 default:
5393 if (lastChar > 0xff)
5394 return NULL;
5395 mbcp = 0;
5396 break;
5399 str = HeapAlloc(GetProcessHeap(), 0, count * 2 + 1);
5400 if (str == NULL)
5401 return NULL;
5403 for(i = 0, c = firstChar; c <= lastChar; i++, c++)
5405 if (mbcp) {
5406 if (c > 0xff)
5407 str[i++] = (BYTE)(c >> 8);
5408 if (c <= 0xff && IsDBCSLeadByteEx(mbcp, c))
5409 str[i] = 0x1f; /* FIXME: use default character */
5410 else
5411 str[i] = (BYTE)c;
5413 else
5414 str[i] = (BYTE)c;
5416 str[i] = '\0';
5418 *pByteLen = i;
5420 return str;
5423 /***********************************************************************
5424 * GetCharWidthW (GDI32.@)
5425 * GetCharWidth32W (GDI32.@)
5427 BOOL WINAPI GetCharWidth32W( HDC hdc, UINT firstChar, UINT lastChar,
5428 LPINT buffer )
5430 UINT i;
5431 BOOL ret;
5432 PHYSDEV dev;
5433 DC * dc = get_dc_ptr( hdc );
5435 if (!dc) return FALSE;
5437 dev = GET_DC_PHYSDEV( dc, pGetCharWidth );
5438 ret = dev->funcs->pGetCharWidth( dev, firstChar, lastChar, buffer );
5440 if (ret)
5442 /* convert device units to logical */
5443 for( i = firstChar; i <= lastChar; i++, buffer++ )
5444 *buffer = width_to_LP( dc, *buffer );
5446 release_dc_ptr( dc );
5447 return ret;
5451 /***********************************************************************
5452 * GetCharWidthA (GDI32.@)
5453 * GetCharWidth32A (GDI32.@)
5455 BOOL WINAPI GetCharWidth32A( HDC hdc, UINT firstChar, UINT lastChar,
5456 LPINT buffer )
5458 INT i, wlen;
5459 LPSTR str;
5460 LPWSTR wstr;
5461 BOOL ret = TRUE;
5463 str = FONT_GetCharsByRangeA(hdc, firstChar, lastChar, &i);
5464 if(str == NULL)
5465 return FALSE;
5467 wstr = FONT_mbtowc(hdc, str, i, &wlen, NULL);
5469 for(i = 0; i < wlen; i++)
5471 if(!GetCharWidth32W(hdc, wstr[i], wstr[i], buffer))
5473 ret = FALSE;
5474 break;
5476 buffer++;
5479 HeapFree(GetProcessHeap(), 0, str);
5480 HeapFree(GetProcessHeap(), 0, wstr);
5482 return ret;
5486 /* helper for nulldrv_ExtTextOut */
5487 static DWORD get_glyph_bitmap( HDC hdc, UINT index, UINT flags, UINT aa_flags,
5488 GLYPHMETRICS *metrics, struct gdi_image_bits *image )
5490 UINT indices[3] = {0, 0, 0x20};
5491 unsigned int i;
5492 DWORD ret, size;
5493 int stride;
5495 indices[0] = index;
5496 if (flags & ETO_GLYPH_INDEX) aa_flags |= GGO_GLYPH_INDEX;
5498 for (i = 0; i < ARRAY_SIZE( indices ); i++)
5500 index = indices[i];
5501 ret = GetGlyphOutlineW( hdc, index, aa_flags, metrics, 0, NULL, &identity );
5502 if (ret != GDI_ERROR) break;
5505 if (ret == GDI_ERROR) return ERROR_NOT_FOUND;
5506 if (!image) return ERROR_SUCCESS;
5508 image->ptr = NULL;
5509 image->free = NULL;
5510 if (!ret) /* empty glyph */
5512 metrics->gmBlackBoxX = metrics->gmBlackBoxY = 0;
5513 return ERROR_SUCCESS;
5516 stride = get_dib_stride( metrics->gmBlackBoxX, 1 );
5517 size = metrics->gmBlackBoxY * stride;
5519 if (!(image->ptr = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
5520 image->is_copy = TRUE;
5521 image->free = free_heap_bits;
5523 ret = GetGlyphOutlineW( hdc, index, aa_flags, metrics, size, image->ptr, &identity );
5524 if (ret == GDI_ERROR)
5526 HeapFree( GetProcessHeap(), 0, image->ptr );
5527 return ERROR_NOT_FOUND;
5529 return ERROR_SUCCESS;
5532 /* helper for nulldrv_ExtTextOut */
5533 static RECT get_total_extents( HDC hdc, INT x, INT y, UINT flags, UINT aa_flags,
5534 LPCWSTR str, UINT count, const INT *dx )
5536 UINT i;
5537 RECT rect, bounds;
5539 reset_bounds( &bounds );
5540 for (i = 0; i < count; i++)
5542 GLYPHMETRICS metrics;
5544 if (get_glyph_bitmap( hdc, str[i], flags, aa_flags, &metrics, NULL )) continue;
5546 rect.left = x + metrics.gmptGlyphOrigin.x;
5547 rect.top = y - metrics.gmptGlyphOrigin.y;
5548 rect.right = rect.left + metrics.gmBlackBoxX;
5549 rect.bottom = rect.top + metrics.gmBlackBoxY;
5550 add_bounds_rect( &bounds, &rect );
5552 if (dx)
5554 if (flags & ETO_PDY)
5556 x += dx[ i * 2 ];
5557 y += dx[ i * 2 + 1];
5559 else x += dx[ i ];
5561 else
5563 x += metrics.gmCellIncX;
5564 y += metrics.gmCellIncY;
5567 return bounds;
5570 /* helper for nulldrv_ExtTextOut */
5571 static void draw_glyph( DC *dc, INT origin_x, INT origin_y, const GLYPHMETRICS *metrics,
5572 const struct gdi_image_bits *image, const RECT *clip )
5574 static const BYTE masks[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
5575 UINT i, count, max_count;
5576 LONG x, y;
5577 BYTE *ptr = image->ptr;
5578 int stride = get_dib_stride( metrics->gmBlackBoxX, 1 );
5579 POINT *pts;
5580 RECT rect, clipped_rect;
5582 rect.left = origin_x + metrics->gmptGlyphOrigin.x;
5583 rect.top = origin_y - metrics->gmptGlyphOrigin.y;
5584 rect.right = rect.left + metrics->gmBlackBoxX;
5585 rect.bottom = rect.top + metrics->gmBlackBoxY;
5586 if (!clip) clipped_rect = rect;
5587 else if (!intersect_rect( &clipped_rect, &rect, clip )) return;
5589 max_count = (metrics->gmBlackBoxX + 1) * metrics->gmBlackBoxY;
5590 pts = HeapAlloc( GetProcessHeap(), 0, max_count * sizeof(*pts) );
5591 if (!pts) return;
5593 count = 0;
5594 ptr += (clipped_rect.top - rect.top) * stride;
5595 for (y = clipped_rect.top; y < clipped_rect.bottom; y++, ptr += stride)
5597 for (x = clipped_rect.left - rect.left; x < clipped_rect.right - rect.left; x++)
5599 while (x < clipped_rect.right - rect.left && !(ptr[x / 8] & masks[x % 8])) x++;
5600 pts[count].x = rect.left + x;
5601 while (x < clipped_rect.right - rect.left && (ptr[x / 8] & masks[x % 8])) x++;
5602 pts[count + 1].x = rect.left + x;
5603 if (pts[count + 1].x > pts[count].x)
5605 pts[count].y = pts[count + 1].y = y;
5606 count += 2;
5610 assert( count <= max_count );
5611 dp_to_lp( dc, pts, count );
5612 for (i = 0; i < count; i += 2) Polyline( dc->hSelf, pts + i, 2 );
5613 HeapFree( GetProcessHeap(), 0, pts );
5616 /***********************************************************************
5617 * nulldrv_ExtTextOut
5619 BOOL CDECL nulldrv_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *rect,
5620 LPCWSTR str, UINT count, const INT *dx )
5622 DC *dc = get_nulldrv_dc( dev );
5623 UINT i;
5624 DWORD err;
5625 HGDIOBJ orig;
5626 HPEN pen;
5628 if (flags & ETO_OPAQUE)
5630 RECT rc = *rect;
5631 HBRUSH brush = CreateSolidBrush( GetNearestColor( dev->hdc, dc->attr->background_color ) );
5633 if (brush)
5635 orig = NtGdiSelectBrush( dev->hdc, brush );
5636 dp_to_lp( dc, (POINT *)&rc, 2 );
5637 NtGdiPatBlt( dev->hdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY );
5638 NtGdiSelectBrush( dev->hdc, orig );
5639 DeleteObject( brush );
5643 if (!count) return TRUE;
5645 if (dc->aa_flags != GGO_BITMAP)
5647 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
5648 BITMAPINFO *info = (BITMAPINFO *)buffer;
5649 struct gdi_image_bits bits;
5650 struct bitblt_coords src, dst;
5651 PHYSDEV dst_dev;
5652 /* FIXME Subpixel modes */
5653 UINT aa_flags = GGO_GRAY4_BITMAP;
5655 dst_dev = GET_DC_PHYSDEV( dc, pPutImage );
5656 src.visrect = get_total_extents( dev->hdc, x, y, flags, aa_flags, str, count, dx );
5657 if (flags & ETO_CLIPPED) intersect_rect( &src.visrect, &src.visrect, rect );
5658 if (!clip_visrect( dc, &src.visrect, &src.visrect )) return TRUE;
5660 /* FIXME: check for ETO_OPAQUE and avoid GetImage */
5661 src.x = src.visrect.left;
5662 src.y = src.visrect.top;
5663 src.width = src.visrect.right - src.visrect.left;
5664 src.height = src.visrect.bottom - src.visrect.top;
5665 dst = src;
5666 if ((flags & ETO_OPAQUE) && (src.visrect.left >= rect->left) && (src.visrect.top >= rect->top) &&
5667 (src.visrect.right <= rect->right) && (src.visrect.bottom <= rect->bottom))
5669 /* we can avoid the GetImage, just query the needed format */
5670 memset( &info->bmiHeader, 0, sizeof(info->bmiHeader) );
5671 info->bmiHeader.biSize = sizeof(info->bmiHeader);
5672 info->bmiHeader.biWidth = src.width;
5673 info->bmiHeader.biHeight = -src.height;
5674 info->bmiHeader.biSizeImage = get_dib_image_size( info );
5675 err = dst_dev->funcs->pPutImage( dst_dev, 0, info, NULL, NULL, NULL, 0 );
5676 if (!err || err == ERROR_BAD_FORMAT)
5678 /* make the source rectangle relative to the source bits */
5679 src.x = src.y = 0;
5680 src.visrect.left = src.visrect.top = 0;
5681 src.visrect.right = src.width;
5682 src.visrect.bottom = src.height;
5684 bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
5685 if (!bits.ptr) return ERROR_OUTOFMEMORY;
5686 bits.is_copy = TRUE;
5687 bits.free = free_heap_bits;
5688 err = ERROR_SUCCESS;
5691 else
5693 PHYSDEV src_dev = GET_DC_PHYSDEV( dc, pGetImage );
5694 err = src_dev->funcs->pGetImage( src_dev, info, &bits, &src );
5695 if (!err && !bits.is_copy)
5697 void *ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
5698 if (!ptr)
5700 if (bits.free) bits.free( &bits );
5701 return ERROR_OUTOFMEMORY;
5703 memcpy( ptr, bits.ptr, info->bmiHeader.biSizeImage );
5704 if (bits.free) bits.free( &bits );
5705 bits.ptr = ptr;
5706 bits.is_copy = TRUE;
5707 bits.free = free_heap_bits;
5710 if (!err)
5712 /* make x,y relative to the image bits */
5713 x += src.visrect.left - dst.visrect.left;
5714 y += src.visrect.top - dst.visrect.top;
5715 render_aa_text_bitmapinfo( dc, info, &bits, &src, x, y, flags,
5716 aa_flags, str, count, dx );
5717 err = dst_dev->funcs->pPutImage( dst_dev, 0, info, &bits, &src, &dst, SRCCOPY );
5718 if (bits.free) bits.free( &bits );
5719 return !err;
5723 pen = CreatePen( PS_SOLID, 1, dc->attr->text_color );
5724 orig = NtGdiSelectPen( dev->hdc, pen );
5726 for (i = 0; i < count; i++)
5728 GLYPHMETRICS metrics;
5729 struct gdi_image_bits image;
5731 err = get_glyph_bitmap( dev->hdc, str[i], flags, GGO_BITMAP, &metrics, &image );
5732 if (err) continue;
5734 if (image.ptr) draw_glyph( dc, x, y, &metrics, &image, (flags & ETO_CLIPPED) ? rect : NULL );
5735 if (image.free) image.free( &image );
5737 if (dx)
5739 if (flags & ETO_PDY)
5741 x += dx[ i * 2 ];
5742 y += dx[ i * 2 + 1];
5744 else x += dx[ i ];
5746 else
5748 x += metrics.gmCellIncX;
5749 y += metrics.gmCellIncY;
5753 NtGdiSelectPen( dev->hdc, orig );
5754 DeleteObject( pen );
5755 return TRUE;
5759 /***********************************************************************
5760 * ExtTextOutA (GDI32.@)
5762 * See ExtTextOutW.
5764 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
5765 const RECT *lprect, LPCSTR str, UINT count, const INT *lpDx )
5767 INT wlen;
5768 UINT codepage;
5769 LPWSTR p;
5770 BOOL ret;
5771 LPINT lpDxW = NULL;
5773 if (count > INT_MAX) return FALSE;
5775 if (flags & ETO_GLYPH_INDEX)
5776 return ExtTextOutW( hdc, x, y, flags, lprect, (LPCWSTR)str, count, lpDx );
5778 p = FONT_mbtowc(hdc, str, count, &wlen, &codepage);
5780 if (lpDx) {
5781 unsigned int i = 0, j = 0;
5783 /* allocate enough for a ETO_PDY */
5784 lpDxW = HeapAlloc( GetProcessHeap(), 0, 2*wlen*sizeof(INT));
5785 while(i < count) {
5786 if(IsDBCSLeadByteEx(codepage, str[i]))
5788 if(flags & ETO_PDY)
5790 lpDxW[j++] = lpDx[i * 2] + lpDx[(i + 1) * 2];
5791 lpDxW[j++] = lpDx[i * 2 + 1] + lpDx[(i + 1) * 2 + 1];
5793 else
5794 lpDxW[j++] = lpDx[i] + lpDx[i + 1];
5795 i = i + 2;
5797 else
5799 if(flags & ETO_PDY)
5801 lpDxW[j++] = lpDx[i * 2];
5802 lpDxW[j++] = lpDx[i * 2 + 1];
5804 else
5805 lpDxW[j++] = lpDx[i];
5806 i = i + 1;
5811 ret = ExtTextOutW( hdc, x, y, flags, lprect, p, wlen, lpDxW );
5813 HeapFree( GetProcessHeap(), 0, p );
5814 HeapFree( GetProcessHeap(), 0, lpDxW );
5815 return ret;
5818 /***********************************************************************
5819 * get_line_width
5821 * Scale the underline / strikeout line width.
5823 static inline int get_line_width( DC *dc, int metric_size )
5825 int width = abs( INTERNAL_YWSTODS( dc, metric_size ));
5826 if (width == 0) width = 1;
5827 if (metric_size < 0) width = -width;
5828 return width;
5831 /***********************************************************************
5832 * NtGdiExtTextOutW (win32u.@)
5834 * Draws text using the currently selected font, background color, and text color.
5837 * PARAMS
5838 * x,y [I] coordinates of string
5839 * flags [I]
5840 * ETO_GRAYED - undocumented on MSDN
5841 * ETO_OPAQUE - use background color for fill the rectangle
5842 * ETO_CLIPPED - clipping text to the rectangle
5843 * ETO_GLYPH_INDEX - Buffer is of glyph locations in fonts rather
5844 * than encoded characters. Implies ETO_IGNORELANGUAGE
5845 * ETO_RTLREADING - Paragraph is basically a right-to-left paragraph.
5846 * Affects BiDi ordering
5847 * ETO_IGNORELANGUAGE - Undocumented in MSDN - instructs ExtTextOut not to do BiDi reordering
5848 * ETO_PDY - unimplemented
5849 * ETO_NUMERICSLATIN - unimplemented always assumed -
5850 * do not translate numbers into locale representations
5851 * ETO_NUMERICSLOCAL - unimplemented - Numerals in Arabic/Farsi context should assume local form
5852 * lprect [I] dimensions for clipping or/and opaquing
5853 * str [I] text string
5854 * count [I] number of symbols in string
5855 * lpDx [I] optional parameter with distance between drawing characters
5857 * RETURNS
5858 * Success: TRUE
5859 * Failure: FALSE
5861 BOOL WINAPI NtGdiExtTextOutW( HDC hdc, INT x, INT y, UINT flags, const RECT *lprect,
5862 const WCHAR *str, UINT count, const INT *lpDx, DWORD cp )
5864 BOOL ret = FALSE;
5865 LPWSTR reordered_str = (LPWSTR)str;
5866 WORD *glyphs = NULL;
5867 UINT align;
5868 DWORD layout;
5869 POINT pt;
5870 TEXTMETRICW tm;
5871 LOGFONTW lf;
5872 double cosEsc, sinEsc;
5873 INT char_extra;
5874 SIZE sz;
5875 RECT rc;
5876 POINT *deltas = NULL, width = {0, 0};
5877 DWORD type;
5878 DC * dc = get_dc_ptr( hdc );
5879 PHYSDEV physdev;
5880 INT breakRem;
5881 static int quietfixme = 0;
5883 if (!dc) return FALSE;
5884 if (count > INT_MAX) return FALSE;
5886 align = dc->attr->text_align;
5887 breakRem = dc->breakRem;
5888 layout = dc->attr->layout;
5890 if (quietfixme == 0 && flags & (ETO_NUMERICSLOCAL | ETO_NUMERICSLATIN))
5892 FIXME("flags ETO_NUMERICSLOCAL | ETO_NUMERICSLATIN unimplemented\n");
5893 quietfixme = 1;
5896 update_dc( dc );
5897 physdev = GET_DC_PHYSDEV( dc, pExtTextOut );
5898 type = GetObjectType(hdc);
5899 if(type == OBJ_METADC || type == OBJ_ENHMETADC)
5901 ret = physdev->funcs->pExtTextOut( physdev, x, y, flags, lprect, str, count, lpDx );
5902 release_dc_ptr( dc );
5903 return ret;
5906 if (flags & ETO_RTLREADING) align |= TA_RTLREADING;
5907 if (layout & LAYOUT_RTL)
5909 if ((align & TA_CENTER) != TA_CENTER) align ^= TA_RIGHT;
5910 align ^= TA_RTLREADING;
5913 if( !(flags & (ETO_GLYPH_INDEX | ETO_IGNORELANGUAGE)) && count > 0 )
5915 INT cGlyphs;
5916 reordered_str = HeapAlloc(GetProcessHeap(), 0, count*sizeof(WCHAR));
5918 BIDI_Reorder( hdc, str, count, GCP_REORDER,
5919 (align & TA_RTLREADING) ? WINE_GCPW_FORCE_RTL : WINE_GCPW_FORCE_LTR,
5920 reordered_str, count, NULL, &glyphs, &cGlyphs);
5922 flags |= ETO_IGNORELANGUAGE;
5923 if (glyphs)
5925 flags |= ETO_GLYPH_INDEX;
5926 if (cGlyphs != count)
5927 count = cGlyphs;
5930 else if(flags & ETO_GLYPH_INDEX)
5931 glyphs = reordered_str;
5933 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
5934 wine_dbgstr_rect(lprect), debugstr_wn(str, count), count, lpDx);
5935 TRACE("align = %x bkmode = %x mapmode = %x\n", align, dc->attr->background_mode,
5936 dc->attr->map_mode);
5938 if(align & TA_UPDATECP)
5940 pt = dc->attr->cur_pos;
5941 x = pt.x;
5942 y = pt.y;
5945 GetTextMetricsW(hdc, &tm);
5946 GetObjectW(dc->hFont, sizeof(lf), &lf);
5948 if(!(tm.tmPitchAndFamily & TMPF_VECTOR)) /* Non-scalable fonts shouldn't be rotated */
5949 lf.lfEscapement = 0;
5951 if ((dc->attr->graphics_mode == GM_COMPATIBLE) &&
5952 (dc->vport2WorldValid && dc->xformWorld2Vport.eM11 * dc->xformWorld2Vport.eM22 < 0))
5954 lf.lfEscapement = -lf.lfEscapement;
5957 if(lf.lfEscapement != 0)
5959 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
5960 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
5962 else
5964 cosEsc = 1;
5965 sinEsc = 0;
5968 if (lprect && (flags & (ETO_OPAQUE | ETO_CLIPPED)))
5970 rc = *lprect;
5971 lp_to_dp(dc, (POINT*)&rc, 2);
5972 order_rect( &rc );
5973 if (flags & ETO_OPAQUE)
5974 physdev->funcs->pExtTextOut( physdev, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL );
5976 else flags &= ~ETO_CLIPPED;
5978 if(count == 0)
5980 ret = TRUE;
5981 goto done;
5984 pt.x = x;
5985 pt.y = y;
5986 lp_to_dp(dc, &pt, 1);
5987 x = pt.x;
5988 y = pt.y;
5990 char_extra = GetTextCharacterExtra(hdc);
5991 if (char_extra && lpDx && GetDeviceCaps( hdc, TECHNOLOGY ) == DT_RASPRINTER)
5992 char_extra = 0; /* Printer drivers don't add char_extra if lpDx is supplied */
5994 if(char_extra || dc->breakExtra || breakRem || lpDx || lf.lfEscapement != 0)
5996 UINT i;
5997 POINT total = {0, 0}, desired[2];
5999 deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*deltas));
6000 if (lpDx)
6002 if (flags & ETO_PDY)
6004 for (i = 0; i < count; i++)
6006 deltas[i].x = lpDx[i * 2] + char_extra;
6007 deltas[i].y = -lpDx[i * 2 + 1];
6010 else
6012 for (i = 0; i < count; i++)
6014 deltas[i].x = lpDx[i] + char_extra;
6015 deltas[i].y = 0;
6019 else
6021 INT *dx = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dx) );
6023 if (flags & ETO_GLYPH_INDEX)
6024 GetTextExtentExPointI( hdc, glyphs, count, -1, NULL, dx, &sz );
6025 else
6026 GetTextExtentExPointW( hdc, reordered_str, count, -1, NULL, dx, &sz );
6028 deltas[0].x = dx[0];
6029 deltas[0].y = 0;
6030 for (i = 1; i < count; i++)
6032 deltas[i].x = dx[i] - dx[i - 1];
6033 deltas[i].y = 0;
6035 HeapFree( GetProcessHeap(), 0, dx );
6038 for(i = 0; i < count; i++)
6040 total.x += deltas[i].x;
6041 total.y += deltas[i].y;
6043 desired[0].x = desired[0].y = 0;
6045 desired[1].x = cosEsc * total.x + sinEsc * total.y;
6046 desired[1].y = -sinEsc * total.x + cosEsc * total.y;
6048 lp_to_dp(dc, desired, 2);
6049 desired[1].x -= desired[0].x;
6050 desired[1].y -= desired[0].y;
6052 if (dc->attr->graphics_mode == GM_COMPATIBLE)
6054 if (dc->vport2WorldValid && dc->xformWorld2Vport.eM11 < 0)
6055 desired[1].x = -desired[1].x;
6056 if (dc->vport2WorldValid && dc->xformWorld2Vport.eM22 < 0)
6057 desired[1].y = -desired[1].y;
6060 deltas[i].x = desired[1].x - width.x;
6061 deltas[i].y = desired[1].y - width.y;
6063 width = desired[1];
6065 flags |= ETO_PDY;
6067 else
6069 POINT desired[2];
6071 if(flags & ETO_GLYPH_INDEX)
6072 GetTextExtentPointI(hdc, glyphs, count, &sz);
6073 else
6074 GetTextExtentPointW(hdc, reordered_str, count, &sz);
6075 desired[0].x = desired[0].y = 0;
6076 desired[1].x = sz.cx;
6077 desired[1].y = 0;
6078 lp_to_dp(dc, desired, 2);
6079 desired[1].x -= desired[0].x;
6080 desired[1].y -= desired[0].y;
6082 if (dc->attr->graphics_mode == GM_COMPATIBLE)
6084 if (dc->vport2WorldValid && dc->xformWorld2Vport.eM11 < 0)
6085 desired[1].x = -desired[1].x;
6086 if (dc->vport2WorldValid && dc->xformWorld2Vport.eM22 < 0)
6087 desired[1].y = -desired[1].y;
6089 width = desired[1];
6092 tm.tmAscent = abs(INTERNAL_YWSTODS(dc, tm.tmAscent));
6093 tm.tmDescent = abs(INTERNAL_YWSTODS(dc, tm.tmDescent));
6094 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
6096 case TA_LEFT:
6097 if (align & TA_UPDATECP)
6099 pt.x = x + width.x;
6100 pt.y = y + width.y;
6101 dp_to_lp(dc, &pt, 1);
6102 MoveToEx(hdc, pt.x, pt.y, NULL);
6104 break;
6106 case TA_CENTER:
6107 x -= width.x / 2;
6108 y -= width.y / 2;
6109 break;
6111 case TA_RIGHT:
6112 x -= width.x;
6113 y -= width.y;
6114 if (align & TA_UPDATECP)
6116 pt.x = x;
6117 pt.y = y;
6118 dp_to_lp(dc, &pt, 1);
6119 MoveToEx(hdc, pt.x, pt.y, NULL);
6121 break;
6124 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
6126 case TA_TOP:
6127 y += tm.tmAscent * cosEsc;
6128 x += tm.tmAscent * sinEsc;
6129 break;
6131 case TA_BOTTOM:
6132 y -= tm.tmDescent * cosEsc;
6133 x -= tm.tmDescent * sinEsc;
6134 break;
6136 case TA_BASELINE:
6137 break;
6140 if (dc->attr->background_mode != TRANSPARENT)
6142 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE)))
6144 if(!(flags & ETO_OPAQUE) || !lprect ||
6145 x < rc.left || x + width.x >= rc.right ||
6146 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom)
6148 RECT text_box;
6149 text_box.left = x;
6150 text_box.right = x + width.x;
6151 text_box.top = y - tm.tmAscent;
6152 text_box.bottom = y + tm.tmDescent;
6154 if (flags & ETO_CLIPPED) intersect_rect( &text_box, &text_box, &rc );
6155 if (!is_rect_empty( &text_box ))
6156 physdev->funcs->pExtTextOut( physdev, 0, 0, ETO_OPAQUE, &text_box, NULL, 0, NULL );
6161 ret = physdev->funcs->pExtTextOut( physdev, x, y, (flags & ~ETO_OPAQUE), &rc,
6162 glyphs ? glyphs : reordered_str, count, (INT*)deltas );
6164 done:
6165 HeapFree(GetProcessHeap(), 0, deltas);
6166 if(glyphs != reordered_str)
6167 HeapFree(GetProcessHeap(), 0, glyphs);
6168 if(reordered_str != str)
6169 HeapFree(GetProcessHeap(), 0, reordered_str);
6171 if (ret && (lf.lfUnderline || lf.lfStrikeOut))
6173 int underlinePos, strikeoutPos;
6174 int underlineWidth, strikeoutWidth;
6175 UINT size = GetOutlineTextMetricsW(hdc, 0, NULL);
6176 OUTLINETEXTMETRICW* otm = NULL;
6177 POINT pts[5];
6178 HPEN hpen = NtGdiSelectPen(hdc, GetStockObject(NULL_PEN));
6179 HBRUSH hbrush = CreateSolidBrush( dc->attr->text_color );
6181 hbrush = NtGdiSelectBrush(hdc, hbrush);
6183 if(!size)
6185 underlinePos = 0;
6186 underlineWidth = tm.tmAscent / 20 + 1;
6187 strikeoutPos = tm.tmAscent / 2;
6188 strikeoutWidth = underlineWidth;
6190 else
6192 otm = HeapAlloc(GetProcessHeap(), 0, size);
6193 GetOutlineTextMetricsW(hdc, size, otm);
6194 underlinePos = abs( INTERNAL_YWSTODS( dc, otm->otmsUnderscorePosition ));
6195 if (otm->otmsUnderscorePosition < 0) underlinePos = -underlinePos;
6196 underlineWidth = get_line_width( dc, otm->otmsUnderscoreSize );
6197 strikeoutPos = abs( INTERNAL_YWSTODS( dc, otm->otmsStrikeoutPosition ));
6198 if (otm->otmsStrikeoutPosition < 0) strikeoutPos = -strikeoutPos;
6199 strikeoutWidth = get_line_width( dc, otm->otmsStrikeoutSize );
6200 HeapFree(GetProcessHeap(), 0, otm);
6204 if (lf.lfUnderline)
6206 const UINT cnt = 5;
6207 pts[0].x = x - (underlinePos + underlineWidth / 2) * sinEsc;
6208 pts[0].y = y - (underlinePos + underlineWidth / 2) * cosEsc;
6209 pts[1].x = x + width.x - (underlinePos + underlineWidth / 2) * sinEsc;
6210 pts[1].y = y + width.y - (underlinePos + underlineWidth / 2) * cosEsc;
6211 pts[2].x = pts[1].x + underlineWidth * sinEsc;
6212 pts[2].y = pts[1].y + underlineWidth * cosEsc;
6213 pts[3].x = pts[0].x + underlineWidth * sinEsc;
6214 pts[3].y = pts[0].y + underlineWidth * cosEsc;
6215 pts[4].x = pts[0].x;
6216 pts[4].y = pts[0].y;
6217 dp_to_lp(dc, pts, 5);
6218 NtGdiPolyPolyDraw( hdc, pts, &cnt, 1, NtGdiPolyPolygon );
6221 if (lf.lfStrikeOut)
6223 const UINT cnt = 5;
6224 pts[0].x = x - (strikeoutPos + strikeoutWidth / 2) * sinEsc;
6225 pts[0].y = y - (strikeoutPos + strikeoutWidth / 2) * cosEsc;
6226 pts[1].x = x + width.x - (strikeoutPos + strikeoutWidth / 2) * sinEsc;
6227 pts[1].y = y + width.y - (strikeoutPos + strikeoutWidth / 2) * cosEsc;
6228 pts[2].x = pts[1].x + strikeoutWidth * sinEsc;
6229 pts[2].y = pts[1].y + strikeoutWidth * cosEsc;
6230 pts[3].x = pts[0].x + strikeoutWidth * sinEsc;
6231 pts[3].y = pts[0].y + strikeoutWidth * cosEsc;
6232 pts[4].x = pts[0].x;
6233 pts[4].y = pts[0].y;
6234 dp_to_lp(dc, pts, 5);
6235 NtGdiPolyPolyDraw( hdc, pts, &cnt, 1, NtGdiPolyPolygon );
6238 NtGdiSelectPen(hdc, hpen);
6239 hbrush = NtGdiSelectBrush(hdc, hbrush);
6240 DeleteObject(hbrush);
6243 release_dc_ptr( dc );
6245 return ret;
6249 /***********************************************************************
6250 * TextOutA (GDI32.@)
6252 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
6254 return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
6258 /***********************************************************************
6259 * TextOutW (GDI32.@)
6261 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
6263 return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
6267 /***********************************************************************
6268 * PolyTextOutA (GDI32.@)
6270 * See PolyTextOutW.
6272 BOOL WINAPI PolyTextOutA( HDC hdc, const POLYTEXTA *pptxt, INT cStrings )
6274 for (; cStrings>0; cStrings--, pptxt++)
6275 if (!ExtTextOutA( hdc, pptxt->x, pptxt->y, pptxt->uiFlags, &pptxt->rcl, pptxt->lpstr, pptxt->n, pptxt->pdx ))
6276 return FALSE;
6277 return TRUE;
6282 /***********************************************************************
6283 * PolyTextOutW (GDI32.@)
6285 * Draw several Strings
6287 * RETURNS
6288 * TRUE: Success.
6289 * FALSE: Failure.
6291 BOOL WINAPI PolyTextOutW( HDC hdc, const POLYTEXTW *pptxt, INT cStrings )
6293 for (; cStrings>0; cStrings--, pptxt++)
6294 if (!ExtTextOutW( hdc, pptxt->x, pptxt->y, pptxt->uiFlags, &pptxt->rcl, pptxt->lpstr, pptxt->n, pptxt->pdx ))
6295 return FALSE;
6296 return TRUE;
6300 /***********************************************************************
6301 * GetAspectRatioFilterEx (GDI32.@)
6303 BOOL WINAPI GetAspectRatioFilterEx( HDC hdc, LPSIZE pAspectRatio )
6305 FIXME("(%p, %p): -- Empty Stub !\n", hdc, pAspectRatio);
6306 return FALSE;
6310 /***********************************************************************
6311 * GetCharABCWidthsA (GDI32.@)
6313 * See GetCharABCWidthsW.
6315 BOOL WINAPI GetCharABCWidthsA(HDC hdc, UINT firstChar, UINT lastChar,
6316 LPABC abc )
6318 INT i, wlen;
6319 LPSTR str;
6320 LPWSTR wstr;
6321 BOOL ret = TRUE;
6323 str = FONT_GetCharsByRangeA(hdc, firstChar, lastChar, &i);
6324 if (str == NULL)
6325 return FALSE;
6327 wstr = FONT_mbtowc(hdc, str, i, &wlen, NULL);
6328 if (wstr == NULL)
6330 HeapFree(GetProcessHeap(), 0, str);
6331 return FALSE;
6334 for(i = 0; i < wlen; i++)
6336 if(!GetCharABCWidthsW(hdc, wstr[i], wstr[i], abc))
6338 ret = FALSE;
6339 break;
6341 abc++;
6344 HeapFree(GetProcessHeap(), 0, str);
6345 HeapFree(GetProcessHeap(), 0, wstr);
6347 return ret;
6351 /******************************************************************************
6352 * GetCharABCWidthsW [GDI32.@]
6354 * Retrieves widths of characters in range.
6356 * PARAMS
6357 * hdc [I] Handle of device context
6358 * firstChar [I] First character in range to query
6359 * lastChar [I] Last character in range to query
6360 * abc [O] Address of character-width structure
6362 * NOTES
6363 * Only works with TrueType fonts
6365 * RETURNS
6366 * Success: TRUE
6367 * Failure: FALSE
6369 BOOL WINAPI GetCharABCWidthsW( HDC hdc, UINT firstChar, UINT lastChar,
6370 LPABC abc )
6372 DC *dc = get_dc_ptr(hdc);
6373 PHYSDEV dev;
6374 unsigned int i;
6375 BOOL ret;
6376 TEXTMETRICW tm;
6378 if (!dc) return FALSE;
6380 if (!abc)
6382 release_dc_ptr( dc );
6383 return FALSE;
6386 /* unlike GetCharABCWidthsFloatW, this one is supposed to fail on non-scalable fonts */
6387 dev = GET_DC_PHYSDEV( dc, pGetTextMetrics );
6388 if (!dev->funcs->pGetTextMetrics( dev, &tm ) || !(tm.tmPitchAndFamily & TMPF_VECTOR))
6390 release_dc_ptr( dc );
6391 return FALSE;
6394 dev = GET_DC_PHYSDEV( dc, pGetCharABCWidths );
6395 ret = dev->funcs->pGetCharABCWidths( dev, firstChar, lastChar, abc );
6396 if (ret)
6398 /* convert device units to logical */
6399 for( i = firstChar; i <= lastChar; i++, abc++ ) {
6400 abc->abcA = width_to_LP(dc, abc->abcA);
6401 abc->abcB = width_to_LP(dc, abc->abcB);
6402 abc->abcC = width_to_LP(dc, abc->abcC);
6406 release_dc_ptr( dc );
6407 return ret;
6411 /******************************************************************************
6412 * GetCharABCWidthsI [GDI32.@]
6414 * Retrieves widths of characters in range.
6416 * PARAMS
6417 * hdc [I] Handle of device context
6418 * firstChar [I] First glyphs in range to query
6419 * count [I] Last glyphs in range to query
6420 * pgi [i] Array of glyphs to query
6421 * abc [O] Address of character-width structure
6423 * NOTES
6424 * Only works with TrueType fonts
6426 * RETURNS
6427 * Success: TRUE
6428 * Failure: FALSE
6430 BOOL WINAPI GetCharABCWidthsI( HDC hdc, UINT firstChar, UINT count,
6431 LPWORD pgi, LPABC abc)
6433 DC *dc = get_dc_ptr(hdc);
6434 PHYSDEV dev;
6435 unsigned int i;
6436 BOOL ret;
6438 if (!dc) return FALSE;
6440 if (!abc)
6442 release_dc_ptr( dc );
6443 return FALSE;
6446 dev = GET_DC_PHYSDEV( dc, pGetCharABCWidthsI );
6447 ret = dev->funcs->pGetCharABCWidthsI( dev, firstChar, count, pgi, abc );
6448 if (ret)
6450 /* convert device units to logical */
6451 for( i = 0; i < count; i++, abc++ ) {
6452 abc->abcA = width_to_LP(dc, abc->abcA);
6453 abc->abcB = width_to_LP(dc, abc->abcB);
6454 abc->abcC = width_to_LP(dc, abc->abcC);
6458 release_dc_ptr( dc );
6459 return ret;
6463 /***********************************************************************
6464 * GetGlyphOutlineA (GDI32.@)
6466 DWORD WINAPI GetGlyphOutlineA( HDC hdc, UINT uChar, UINT fuFormat,
6467 LPGLYPHMETRICS lpgm, DWORD cbBuffer,
6468 LPVOID lpBuffer, const MAT2 *lpmat2 )
6470 if (!lpmat2) return GDI_ERROR;
6472 if(!(fuFormat & GGO_GLYPH_INDEX)) {
6473 UINT cp;
6474 int len;
6475 char mbchs[2];
6476 WCHAR wChar;
6478 cp = GdiGetCodePage(hdc);
6479 if (IsDBCSLeadByteEx(cp, uChar >> 8)) {
6480 len = 2;
6481 mbchs[0] = (uChar & 0xff00) >> 8;
6482 mbchs[1] = (uChar & 0xff);
6483 } else {
6484 len = 1;
6485 mbchs[0] = (uChar & 0xff);
6487 wChar = 0;
6488 MultiByteToWideChar(cp, 0, mbchs, len, &wChar, 1);
6489 uChar = wChar;
6492 return GetGlyphOutlineW(hdc, uChar, fuFormat, lpgm, cbBuffer, lpBuffer,
6493 lpmat2);
6496 /***********************************************************************
6497 * GetGlyphOutlineW (GDI32.@)
6499 DWORD WINAPI GetGlyphOutlineW( HDC hdc, UINT uChar, UINT fuFormat,
6500 LPGLYPHMETRICS lpgm, DWORD cbBuffer,
6501 LPVOID lpBuffer, const MAT2 *lpmat2 )
6503 DC *dc;
6504 DWORD ret;
6505 PHYSDEV dev;
6507 TRACE("(%p, %04x, %04x, %p, %d, %p, %p)\n",
6508 hdc, uChar, fuFormat, lpgm, cbBuffer, lpBuffer, lpmat2 );
6510 if (!lpmat2) return GDI_ERROR;
6512 dc = get_dc_ptr(hdc);
6513 if(!dc) return GDI_ERROR;
6515 uChar &= 0xffff;
6517 dev = GET_DC_PHYSDEV( dc, pGetGlyphOutline );
6518 ret = dev->funcs->pGetGlyphOutline( dev, uChar, fuFormat, lpgm, cbBuffer, lpBuffer, lpmat2 );
6519 release_dc_ptr( dc );
6520 return ret;
6524 /***********************************************************************
6525 * CreateScalableFontResourceA (GDI32.@)
6527 BOOL WINAPI CreateScalableFontResourceA( DWORD fHidden,
6528 LPCSTR lpszResourceFile,
6529 LPCSTR lpszFontFile,
6530 LPCSTR lpszCurrentPath )
6532 LPWSTR lpszResourceFileW = NULL;
6533 LPWSTR lpszFontFileW = NULL;
6534 LPWSTR lpszCurrentPathW = NULL;
6535 int len;
6536 BOOL ret;
6538 if (lpszResourceFile)
6540 len = MultiByteToWideChar(CP_ACP, 0, lpszResourceFile, -1, NULL, 0);
6541 lpszResourceFileW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
6542 MultiByteToWideChar(CP_ACP, 0, lpszResourceFile, -1, lpszResourceFileW, len);
6545 if (lpszFontFile)
6547 len = MultiByteToWideChar(CP_ACP, 0, lpszFontFile, -1, NULL, 0);
6548 lpszFontFileW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
6549 MultiByteToWideChar(CP_ACP, 0, lpszFontFile, -1, lpszFontFileW, len);
6552 if (lpszCurrentPath)
6554 len = MultiByteToWideChar(CP_ACP, 0, lpszCurrentPath, -1, NULL, 0);
6555 lpszCurrentPathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
6556 MultiByteToWideChar(CP_ACP, 0, lpszCurrentPath, -1, lpszCurrentPathW, len);
6559 ret = CreateScalableFontResourceW(fHidden, lpszResourceFileW,
6560 lpszFontFileW, lpszCurrentPathW);
6562 HeapFree(GetProcessHeap(), 0, lpszResourceFileW);
6563 HeapFree(GetProcessHeap(), 0, lpszFontFileW);
6564 HeapFree(GetProcessHeap(), 0, lpszCurrentPathW);
6566 return ret;
6569 #define NE_FFLAGS_LIBMODULE 0x8000
6570 #define NE_OSFLAGS_WINDOWS 0x02
6572 static const char dos_string[0x40] = "This is a TrueType resource file";
6573 static const char FONTRES[] = {'F','O','N','T','R','E','S',':'};
6575 #include <pshpack1.h>
6576 struct fontdir
6578 WORD num_of_resources;
6579 WORD res_id;
6580 WORD dfVersion;
6581 DWORD dfSize;
6582 CHAR dfCopyright[60];
6583 WORD dfType;
6584 WORD dfPoints;
6585 WORD dfVertRes;
6586 WORD dfHorizRes;
6587 WORD dfAscent;
6588 WORD dfInternalLeading;
6589 WORD dfExternalLeading;
6590 BYTE dfItalic;
6591 BYTE dfUnderline;
6592 BYTE dfStrikeOut;
6593 WORD dfWeight;
6594 BYTE dfCharSet;
6595 WORD dfPixWidth;
6596 WORD dfPixHeight;
6597 BYTE dfPitchAndFamily;
6598 WORD dfAvgWidth;
6599 WORD dfMaxWidth;
6600 BYTE dfFirstChar;
6601 BYTE dfLastChar;
6602 BYTE dfDefaultChar;
6603 BYTE dfBreakChar;
6604 WORD dfWidthBytes;
6605 DWORD dfDevice;
6606 DWORD dfFace;
6607 DWORD dfReserved;
6608 CHAR szFaceName[LF_FACESIZE];
6610 #include <poppack.h>
6612 #include <pshpack2.h>
6614 struct ne_typeinfo
6616 WORD type_id;
6617 WORD count;
6618 DWORD res;
6621 struct ne_nameinfo
6623 WORD off;
6624 WORD len;
6625 WORD flags;
6626 WORD id;
6627 DWORD res;
6630 struct rsrc_tab
6632 WORD align;
6633 struct ne_typeinfo fontdir_type;
6634 struct ne_nameinfo fontdir_name;
6635 struct ne_typeinfo scalable_type;
6636 struct ne_nameinfo scalable_name;
6637 WORD end_of_rsrc;
6638 BYTE fontdir_res_name[8];
6641 #include <poppack.h>
6643 static BOOL create_fot( const WCHAR *resource, const WCHAR *font_file, const struct fontdir *fontdir )
6645 BOOL ret = FALSE;
6646 HANDLE file;
6647 DWORD size, written;
6648 BYTE *ptr, *start;
6649 BYTE import_name_len, res_name_len, non_res_name_len, font_file_len;
6650 char *font_fileA, *last_part, *ext;
6651 IMAGE_DOS_HEADER dos;
6652 IMAGE_OS2_HEADER ne =
6654 IMAGE_OS2_SIGNATURE, 5, 1, 0, 0, 0, NE_FFLAGS_LIBMODULE, 0,
6655 0, 0, 0, 0, 0, 0,
6656 0, sizeof(ne), sizeof(ne), 0, 0, 0, 0,
6657 0, 4, 2, NE_OSFLAGS_WINDOWS, 0, 0, 0, 0, 0x300
6659 struct rsrc_tab rsrc_tab =
6662 { 0x8007, 1, 0 },
6663 { 0, 0, 0x0c50, 0x2c, 0 },
6664 { 0x80cc, 1, 0 },
6665 { 0, 0, 0x0c50, 0x8001, 0 },
6667 { 7,'F','O','N','T','D','I','R'}
6670 memset( &dos, 0, sizeof(dos) );
6671 dos.e_magic = IMAGE_DOS_SIGNATURE;
6672 dos.e_lfanew = sizeof(dos) + sizeof(dos_string);
6674 /* import name is last part\0, resident name is last part without extension
6675 non-resident name is "FONTRES:" + lfFaceName */
6677 font_file_len = WideCharToMultiByte( CP_ACP, 0, font_file, -1, NULL, 0, NULL, NULL );
6678 font_fileA = HeapAlloc( GetProcessHeap(), 0, font_file_len );
6679 WideCharToMultiByte( CP_ACP, 0, font_file, -1, font_fileA, font_file_len, NULL, NULL );
6681 last_part = strrchr( font_fileA, '\\' );
6682 if (last_part) last_part++;
6683 else last_part = font_fileA;
6684 import_name_len = strlen( last_part ) + 1;
6686 ext = strchr( last_part, '.' );
6687 if (ext) res_name_len = ext - last_part;
6688 else res_name_len = import_name_len - 1;
6690 non_res_name_len = sizeof( FONTRES ) + strlen( fontdir->szFaceName );
6692 ne.ne_cbnrestab = 1 + non_res_name_len + 2 + 1; /* len + string + (WORD) ord_num + 1 byte eod */
6693 ne.ne_restab = ne.ne_rsrctab + sizeof(rsrc_tab);
6694 ne.ne_modtab = ne.ne_imptab = ne.ne_restab + 1 + res_name_len + 2 + 3; /* len + string + (WORD) ord_num + 3 bytes eod */
6695 ne.ne_enttab = ne.ne_imptab + 1 + import_name_len; /* len + string */
6696 ne.ne_cbenttab = 2;
6697 ne.ne_nrestab = ne.ne_enttab + ne.ne_cbenttab + 2 + dos.e_lfanew; /* there are 2 bytes of 0 after entry tab */
6699 rsrc_tab.scalable_name.off = (ne.ne_nrestab + ne.ne_cbnrestab + 0xf) >> 4;
6700 rsrc_tab.scalable_name.len = (font_file_len + 0xf) >> 4;
6701 rsrc_tab.fontdir_name.off = rsrc_tab.scalable_name.off + rsrc_tab.scalable_name.len;
6702 rsrc_tab.fontdir_name.len = (fontdir->dfSize + 0xf) >> 4;
6704 size = (rsrc_tab.fontdir_name.off + rsrc_tab.fontdir_name.len) << 4;
6705 start = ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
6707 if (!ptr)
6709 HeapFree( GetProcessHeap(), 0, font_fileA );
6710 return FALSE;
6713 memcpy( ptr, &dos, sizeof(dos) );
6714 memcpy( ptr + sizeof(dos), dos_string, sizeof(dos_string) );
6715 memcpy( ptr + dos.e_lfanew, &ne, sizeof(ne) );
6717 ptr = start + dos.e_lfanew + ne.ne_rsrctab;
6718 memcpy( ptr, &rsrc_tab, sizeof(rsrc_tab) );
6720 ptr = start + dos.e_lfanew + ne.ne_restab;
6721 *ptr++ = res_name_len;
6722 memcpy( ptr, last_part, res_name_len );
6724 ptr = start + dos.e_lfanew + ne.ne_imptab;
6725 *ptr++ = import_name_len;
6726 memcpy( ptr, last_part, import_name_len );
6728 ptr = start + ne.ne_nrestab;
6729 *ptr++ = non_res_name_len;
6730 memcpy( ptr, FONTRES, sizeof(FONTRES) );
6731 memcpy( ptr + sizeof(FONTRES), fontdir->szFaceName, strlen( fontdir->szFaceName ) );
6733 ptr = start + (rsrc_tab.scalable_name.off << 4);
6734 memcpy( ptr, font_fileA, font_file_len );
6736 ptr = start + (rsrc_tab.fontdir_name.off << 4);
6737 memcpy( ptr, fontdir, fontdir->dfSize );
6739 file = CreateFileW( resource, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
6740 if (file != INVALID_HANDLE_VALUE)
6742 if (WriteFile( file, start, size, &written, NULL ) && written == size)
6743 ret = TRUE;
6744 CloseHandle( file );
6747 HeapFree( GetProcessHeap(), 0, start );
6748 HeapFree( GetProcessHeap(), 0, font_fileA );
6750 return ret;
6753 /***********************************************************************
6754 * CreateScalableFontResourceW (GDI32.@)
6756 BOOL WINAPI CreateScalableFontResourceW( DWORD hidden, LPCWSTR resource_file,
6757 LPCWSTR font_file, LPCWSTR font_path )
6759 struct fontdir fontdir = { 0 };
6760 struct gdi_font *font = NULL;
6761 WCHAR path[MAX_PATH];
6763 TRACE("(%d, %s, %s, %s)\n", hidden, debugstr_w(resource_file),
6764 debugstr_w(font_file), debugstr_w(font_path) );
6766 if (!font_funcs) return FALSE;
6768 if (!font_file) goto done;
6769 if (font_path && font_path[0])
6771 int len = lstrlenW( font_path ) + lstrlenW( font_file ) + 2;
6772 if (len > MAX_PATH) goto done;
6773 lstrcpynW( path, font_path, MAX_PATH );
6774 lstrcatW( path, L"\\" );
6775 lstrcatW( path, font_file );
6777 else if (!GetFullPathNameW( font_file, MAX_PATH, path, NULL )) goto done;
6779 if (!(font = alloc_gdi_font( path, NULL, 0 ))) goto done;
6780 font->lf.lfHeight = 100;
6781 if (!font_funcs->load_font( font )) goto done;
6782 if (!font_funcs->set_outline_text_metrics( font )) goto done;
6784 if (!(font->otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE)) goto done;
6786 fontdir.num_of_resources = 1;
6787 fontdir.res_id = 0;
6788 fontdir.dfVersion = 0x200;
6789 fontdir.dfSize = sizeof(fontdir);
6790 strcpy( fontdir.dfCopyright, "Wine fontdir" );
6791 fontdir.dfType = 0x4003; /* 0x0080 set if private */
6792 fontdir.dfPoints = font->otm.otmEMSquare;
6793 fontdir.dfVertRes = 72;
6794 fontdir.dfHorizRes = 72;
6795 fontdir.dfAscent = font->otm.otmTextMetrics.tmAscent;
6796 fontdir.dfInternalLeading = font->otm.otmTextMetrics.tmInternalLeading;
6797 fontdir.dfExternalLeading = font->otm.otmTextMetrics.tmExternalLeading;
6798 fontdir.dfItalic = font->otm.otmTextMetrics.tmItalic;
6799 fontdir.dfUnderline = font->otm.otmTextMetrics.tmUnderlined;
6800 fontdir.dfStrikeOut = font->otm.otmTextMetrics.tmStruckOut;
6801 fontdir.dfWeight = font->otm.otmTextMetrics.tmWeight;
6802 fontdir.dfCharSet = font->otm.otmTextMetrics.tmCharSet;
6803 fontdir.dfPixWidth = 0;
6804 fontdir.dfPixHeight = font->otm.otmTextMetrics.tmHeight;
6805 fontdir.dfPitchAndFamily = font->otm.otmTextMetrics.tmPitchAndFamily;
6806 fontdir.dfAvgWidth = font->otm.otmTextMetrics.tmAveCharWidth;
6807 fontdir.dfMaxWidth = font->otm.otmTextMetrics.tmMaxCharWidth;
6808 fontdir.dfFirstChar = font->otm.otmTextMetrics.tmFirstChar;
6809 fontdir.dfLastChar = font->otm.otmTextMetrics.tmLastChar;
6810 fontdir.dfDefaultChar = font->otm.otmTextMetrics.tmDefaultChar;
6811 fontdir.dfBreakChar = font->otm.otmTextMetrics.tmBreakChar;
6812 fontdir.dfWidthBytes = 0;
6813 fontdir.dfDevice = 0;
6814 fontdir.dfFace = FIELD_OFFSET( struct fontdir, szFaceName );
6815 fontdir.dfReserved = 0;
6816 WideCharToMultiByte( CP_ACP, 0, (WCHAR *)font->otm.otmpFamilyName, -1,
6817 fontdir.szFaceName, LF_FACESIZE, NULL, NULL );
6818 free_gdi_font( font );
6820 if (hidden) fontdir.dfType |= 0x80;
6821 return create_fot( resource_file, font_file, &fontdir );
6823 done:
6824 if (font) free_gdi_font( font );
6825 SetLastError( ERROR_INVALID_PARAMETER );
6826 return FALSE;
6829 /*************************************************************************
6830 * GetKerningPairsA (GDI32.@)
6832 DWORD WINAPI GetKerningPairsA( HDC hDC, DWORD cPairs,
6833 LPKERNINGPAIR kern_pairA )
6835 UINT cp;
6836 CPINFO cpi;
6837 DWORD i, total_kern_pairs, kern_pairs_copied = 0;
6838 KERNINGPAIR *kern_pairW;
6840 if (!cPairs && kern_pairA)
6842 SetLastError(ERROR_INVALID_PARAMETER);
6843 return 0;
6846 cp = GdiGetCodePage(hDC);
6848 /* GetCPInfo() will fail on CP_SYMBOL, and WideCharToMultiByte is supposed
6849 * to fail on an invalid character for CP_SYMBOL.
6851 cpi.DefaultChar[0] = 0;
6852 if (cp != CP_SYMBOL && !GetCPInfo(cp, &cpi))
6854 FIXME("Can't find codepage %u info\n", cp);
6855 return 0;
6858 total_kern_pairs = GetKerningPairsW(hDC, 0, NULL);
6859 if (!total_kern_pairs) return 0;
6861 kern_pairW = HeapAlloc(GetProcessHeap(), 0, total_kern_pairs * sizeof(*kern_pairW));
6862 GetKerningPairsW(hDC, total_kern_pairs, kern_pairW);
6864 for (i = 0; i < total_kern_pairs; i++)
6866 char first, second;
6868 if (!WideCharToMultiByte(cp, 0, &kern_pairW[i].wFirst, 1, &first, 1, NULL, NULL))
6869 continue;
6871 if (!WideCharToMultiByte(cp, 0, &kern_pairW[i].wSecond, 1, &second, 1, NULL, NULL))
6872 continue;
6874 if (first == cpi.DefaultChar[0] || second == cpi.DefaultChar[0])
6875 continue;
6877 if (kern_pairA)
6879 if (kern_pairs_copied >= cPairs) break;
6881 kern_pairA->wFirst = (BYTE)first;
6882 kern_pairA->wSecond = (BYTE)second;
6883 kern_pairA->iKernAmount = kern_pairW[i].iKernAmount;
6884 kern_pairA++;
6886 kern_pairs_copied++;
6889 HeapFree(GetProcessHeap(), 0, kern_pairW);
6891 return kern_pairs_copied;
6894 /*************************************************************************
6895 * GetKerningPairsW (GDI32.@)
6897 DWORD WINAPI GetKerningPairsW( HDC hDC, DWORD cPairs,
6898 LPKERNINGPAIR lpKerningPairs )
6900 DC *dc;
6901 DWORD ret;
6902 PHYSDEV dev;
6904 TRACE("(%p,%d,%p)\n", hDC, cPairs, lpKerningPairs);
6906 if (!cPairs && lpKerningPairs)
6908 SetLastError(ERROR_INVALID_PARAMETER);
6909 return 0;
6912 dc = get_dc_ptr(hDC);
6913 if (!dc) return 0;
6915 dev = GET_DC_PHYSDEV( dc, pGetKerningPairs );
6916 ret = dev->funcs->pGetKerningPairs( dev, cPairs, lpKerningPairs );
6917 release_dc_ptr( dc );
6918 return ret;
6921 /*************************************************************************
6922 * TranslateCharsetInfo [GDI32.@]
6924 * Fills a CHARSETINFO structure for a character set, code page, or
6925 * font. This allows making the correspondence between different labels
6926 * (character set, Windows, ANSI, and OEM codepages, and Unicode ranges)
6927 * of the same encoding.
6929 * Only one codepage will be set in lpCs->fs. If TCI_SRCFONTSIG is used,
6930 * only one codepage should be set in *lpSrc.
6932 * RETURNS
6933 * TRUE on success, FALSE on failure.
6936 BOOL WINAPI TranslateCharsetInfo(
6937 LPDWORD lpSrc, /* [in]
6938 if flags == TCI_SRCFONTSIG: pointer to fsCsb of a FONTSIGNATURE
6939 if flags == TCI_SRCCHARSET: a character set value
6940 if flags == TCI_SRCCODEPAGE: a code page value
6942 LPCHARSETINFO lpCs, /* [out] structure to receive charset information */
6943 DWORD flags /* [in] determines interpretation of lpSrc */)
6945 int index = 0;
6946 switch (flags) {
6947 case TCI_SRCFONTSIG:
6948 while (index < MAXTCIINDEX && !(*lpSrc>>index & 0x0001)) index++;
6949 break;
6950 case TCI_SRCCODEPAGE:
6951 while (index < MAXTCIINDEX && PtrToUlong(lpSrc) != FONT_tci[index].ciACP) index++;
6952 break;
6953 case TCI_SRCCHARSET:
6954 while (index < MAXTCIINDEX && PtrToUlong(lpSrc) != FONT_tci[index].ciCharset) index++;
6955 break;
6956 default:
6957 return FALSE;
6959 if (index >= MAXTCIINDEX || FONT_tci[index].ciCharset == DEFAULT_CHARSET) return FALSE;
6960 *lpCs = FONT_tci[index];
6961 return TRUE;
6964 /*************************************************************************
6965 * GetFontLanguageInfo (GDI32.@)
6967 DWORD WINAPI GetFontLanguageInfo(HDC hdc)
6969 FONTSIGNATURE fontsig;
6970 static const DWORD GCP_DBCS_MASK=FS_JISJAPAN|FS_CHINESESIMP|FS_WANSUNG|FS_CHINESETRAD|FS_JOHAB,
6971 GCP_DIACRITIC_MASK=0x00000000,
6972 FLI_GLYPHS_MASK=0x00000000,
6973 GCP_GLYPHSHAPE_MASK=FS_ARABIC,
6974 GCP_KASHIDA_MASK=0x00000000,
6975 GCP_LIGATE_MASK=0x00000000,
6976 GCP_REORDER_MASK=FS_HEBREW|FS_ARABIC;
6978 DWORD result=0;
6980 NtGdiGetTextCharsetInfo( hdc, &fontsig, 0 );
6981 /* We detect each flag we return using a bitmask on the Codepage Bitfields */
6983 if( (fontsig.fsCsb[0]&GCP_DBCS_MASK)!=0 )
6984 result|=GCP_DBCS;
6986 if( (fontsig.fsCsb[0]&GCP_DIACRITIC_MASK)!=0 )
6987 result|=GCP_DIACRITIC;
6989 if( (fontsig.fsCsb[0]&FLI_GLYPHS_MASK)!=0 )
6990 result|=FLI_GLYPHS;
6992 if( (fontsig.fsCsb[0]&GCP_GLYPHSHAPE_MASK)!=0 )
6993 result|=GCP_GLYPHSHAPE;
6995 if( (fontsig.fsCsb[0]&GCP_KASHIDA_MASK)!=0 )
6996 result|=GCP_KASHIDA;
6998 if( (fontsig.fsCsb[0]&GCP_LIGATE_MASK)!=0 )
6999 result|=GCP_LIGATE;
7001 if( GetKerningPairsW( hdc, 0, NULL ) )
7002 result|=GCP_USEKERNING;
7004 /* this might need a test for a HEBREW- or ARABIC_CHARSET as well */
7005 if( GetTextAlign( hdc) & TA_RTLREADING )
7006 if( (fontsig.fsCsb[0]&GCP_REORDER_MASK)!=0 )
7007 result|=GCP_REORDER;
7009 return result;
7013 /*************************************************************************
7014 * GetFontData [GDI32.@]
7016 * Retrieve data for TrueType font.
7018 * RETURNS
7020 * success: Number of bytes returned
7021 * failure: GDI_ERROR
7023 * NOTES
7025 * Calls SetLastError()
7028 DWORD WINAPI GetFontData(HDC hdc, DWORD table, DWORD offset,
7029 LPVOID buffer, DWORD length)
7031 DC *dc = get_dc_ptr(hdc);
7032 PHYSDEV dev;
7033 DWORD ret;
7035 if(!dc) return GDI_ERROR;
7037 dev = GET_DC_PHYSDEV( dc, pGetFontData );
7038 ret = dev->funcs->pGetFontData( dev, table, offset, buffer, length );
7039 release_dc_ptr( dc );
7040 return ret;
7043 /*************************************************************************
7044 * GetGlyphIndicesA [GDI32.@]
7046 DWORD WINAPI GetGlyphIndicesA(HDC hdc, LPCSTR lpstr, INT count,
7047 LPWORD pgi, DWORD flags)
7049 DWORD ret;
7050 WCHAR *lpstrW;
7051 INT countW;
7053 TRACE("(%p, %s, %d, %p, 0x%x)\n",
7054 hdc, debugstr_an(lpstr, count), count, pgi, flags);
7056 lpstrW = FONT_mbtowc(hdc, lpstr, count, &countW, NULL);
7057 ret = GetGlyphIndicesW(hdc, lpstrW, countW, pgi, flags);
7058 HeapFree(GetProcessHeap(), 0, lpstrW);
7060 return ret;
7063 /*************************************************************************
7064 * GetGlyphIndicesW [GDI32.@]
7066 DWORD WINAPI GetGlyphIndicesW(HDC hdc, LPCWSTR lpstr, INT count,
7067 LPWORD pgi, DWORD flags)
7069 DC *dc = get_dc_ptr(hdc);
7070 PHYSDEV dev;
7071 DWORD ret;
7073 TRACE("(%p, %s, %d, %p, 0x%x)\n",
7074 hdc, debugstr_wn(lpstr, count), count, pgi, flags);
7076 if(!dc) return GDI_ERROR;
7078 dev = GET_DC_PHYSDEV( dc, pGetGlyphIndices );
7079 ret = dev->funcs->pGetGlyphIndices( dev, lpstr, count, pgi, flags );
7080 release_dc_ptr( dc );
7081 return ret;
7084 /*************************************************************************
7085 * GetCharacterPlacementA [GDI32.@]
7087 * See GetCharacterPlacementW.
7089 * NOTES:
7090 * the web browser control of ie4 calls this with dwFlags=0
7092 DWORD WINAPI
7093 GetCharacterPlacementA(HDC hdc, LPCSTR lpString, INT uCount,
7094 INT nMaxExtent, GCP_RESULTSA *lpResults,
7095 DWORD dwFlags)
7097 WCHAR *lpStringW;
7098 INT uCountW;
7099 GCP_RESULTSW resultsW;
7100 DWORD ret;
7101 UINT font_cp;
7103 TRACE("%s, %d, %d, 0x%08x\n",
7104 debugstr_an(lpString, uCount), uCount, nMaxExtent, dwFlags);
7106 lpStringW = FONT_mbtowc(hdc, lpString, uCount, &uCountW, &font_cp);
7108 if (!lpResults)
7110 ret = GetCharacterPlacementW(hdc, lpStringW, uCountW, nMaxExtent, NULL, dwFlags);
7111 HeapFree(GetProcessHeap(), 0, lpStringW);
7112 return ret;
7115 /* both structs are equal in size */
7116 memcpy(&resultsW, lpResults, sizeof(resultsW));
7118 if(lpResults->lpOutString)
7119 resultsW.lpOutString = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*uCountW);
7121 ret = GetCharacterPlacementW(hdc, lpStringW, uCountW, nMaxExtent, &resultsW, dwFlags);
7123 lpResults->nGlyphs = resultsW.nGlyphs;
7124 lpResults->nMaxFit = resultsW.nMaxFit;
7126 if(lpResults->lpOutString) {
7127 WideCharToMultiByte(font_cp, 0, resultsW.lpOutString, uCountW,
7128 lpResults->lpOutString, uCount, NULL, NULL );
7131 HeapFree(GetProcessHeap(), 0, lpStringW);
7132 HeapFree(GetProcessHeap(), 0, resultsW.lpOutString);
7134 return ret;
7137 static int kern_pair(const KERNINGPAIR *kern, int count, WCHAR c1, WCHAR c2)
7139 int i;
7141 for (i = 0; i < count; i++)
7143 if (kern[i].wFirst == c1 && kern[i].wSecond == c2)
7144 return kern[i].iKernAmount;
7147 return 0;
7150 static int *kern_string(HDC hdc, const WCHAR *str, int len, int *kern_total)
7152 int i, count;
7153 KERNINGPAIR *kern = NULL;
7154 int *ret;
7156 *kern_total = 0;
7158 ret = heap_alloc(len * sizeof(*ret));
7159 if (!ret) return NULL;
7161 count = GetKerningPairsW(hdc, 0, NULL);
7162 if (count)
7164 kern = heap_alloc(count * sizeof(*kern));
7165 if (!kern)
7167 heap_free(ret);
7168 return NULL;
7171 GetKerningPairsW(hdc, count, kern);
7174 for (i = 0; i < len - 1; i++)
7176 ret[i] = kern_pair(kern, count, str[i], str[i + 1]);
7177 *kern_total += ret[i];
7180 ret[len - 1] = 0; /* no kerning for last element */
7182 heap_free(kern);
7183 return ret;
7186 /*************************************************************************
7187 * GetCharacterPlacementW [GDI32.@]
7189 * Retrieve information about a string. This includes the width, reordering,
7190 * Glyphing and so on.
7192 * RETURNS
7194 * The width and height of the string if successful, 0 if failed.
7196 * BUGS
7198 * All flags except GCP_REORDER are not yet implemented.
7199 * Reordering is not 100% compliant to the Windows BiDi method.
7200 * Caret positioning is not yet implemented for BiDi.
7201 * Classes are not yet implemented.
7204 DWORD WINAPI
7205 GetCharacterPlacementW(
7206 HDC hdc, /* [in] Device context for which the rendering is to be done */
7207 LPCWSTR lpString, /* [in] The string for which information is to be returned */
7208 INT uCount, /* [in] Number of WORDS in string. */
7209 INT nMaxExtent, /* [in] Maximum extent the string is to take (in HDC logical units) */
7210 GCP_RESULTSW *lpResults, /* [in/out] A pointer to a GCP_RESULTSW struct */
7211 DWORD dwFlags /* [in] Flags specifying how to process the string */
7214 DWORD ret=0;
7215 SIZE size;
7216 UINT i, nSet;
7217 int *kern = NULL, kern_total = 0;
7219 TRACE("%s, %d, %d, 0x%08x\n",
7220 debugstr_wn(lpString, uCount), uCount, nMaxExtent, dwFlags);
7222 if (!uCount)
7223 return 0;
7225 if (!lpResults)
7226 return GetTextExtentPoint32W(hdc, lpString, uCount, &size) ? MAKELONG(size.cx, size.cy) : 0;
7228 TRACE("lStructSize=%d, lpOutString=%p, lpOrder=%p, lpDx=%p, lpCaretPos=%p\n"
7229 "lpClass=%p, lpGlyphs=%p, nGlyphs=%u, nMaxFit=%d\n",
7230 lpResults->lStructSize, lpResults->lpOutString, lpResults->lpOrder,
7231 lpResults->lpDx, lpResults->lpCaretPos, lpResults->lpClass,
7232 lpResults->lpGlyphs, lpResults->nGlyphs, lpResults->nMaxFit);
7234 if (dwFlags & ~(GCP_REORDER | GCP_USEKERNING))
7235 FIXME("flags 0x%08x ignored\n", dwFlags);
7236 if (lpResults->lpClass)
7237 FIXME("classes not implemented\n");
7238 if (lpResults->lpCaretPos && (dwFlags & GCP_REORDER))
7239 FIXME("Caret positions for complex scripts not implemented\n");
7241 nSet = (UINT)uCount;
7242 if (nSet > lpResults->nGlyphs)
7243 nSet = lpResults->nGlyphs;
7245 /* return number of initialized fields */
7246 lpResults->nGlyphs = nSet;
7248 if (!(dwFlags & GCP_REORDER))
7250 /* Treat the case where no special handling was requested in a fastpath way */
7251 /* copy will do if the GCP_REORDER flag is not set */
7252 if (lpResults->lpOutString)
7253 memcpy( lpResults->lpOutString, lpString, nSet * sizeof(WCHAR));
7255 if (lpResults->lpOrder)
7257 for (i = 0; i < nSet; i++)
7258 lpResults->lpOrder[i] = i;
7261 else
7263 BIDI_Reorder(NULL, lpString, uCount, dwFlags, WINE_GCPW_FORCE_LTR, lpResults->lpOutString,
7264 nSet, lpResults->lpOrder, NULL, NULL );
7267 if (dwFlags & GCP_USEKERNING)
7269 kern = kern_string(hdc, lpString, nSet, &kern_total);
7270 if (!kern)
7272 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
7273 return 0;
7277 /* FIXME: Will use the placement chars */
7278 if (lpResults->lpDx)
7280 int c;
7281 for (i = 0; i < nSet; i++)
7283 if (GetCharWidth32W(hdc, lpString[i], lpString[i], &c))
7285 lpResults->lpDx[i] = c;
7286 if (dwFlags & GCP_USEKERNING)
7287 lpResults->lpDx[i] += kern[i];
7292 if (lpResults->lpCaretPos && !(dwFlags & GCP_REORDER))
7294 int pos = 0;
7296 lpResults->lpCaretPos[0] = 0;
7297 for (i = 0; i < nSet - 1; i++)
7299 if (dwFlags & GCP_USEKERNING)
7300 pos += kern[i];
7302 if (GetTextExtentPoint32W(hdc, &lpString[i], 1, &size))
7303 lpResults->lpCaretPos[i + 1] = (pos += size.cx);
7307 if (lpResults->lpGlyphs)
7308 GetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);
7310 if (GetTextExtentPoint32W(hdc, lpString, uCount, &size))
7311 ret = MAKELONG(size.cx + kern_total, size.cy);
7313 heap_free(kern);
7315 return ret;
7318 /*************************************************************************
7319 * GetCharABCWidthsFloatA [GDI32.@]
7321 * See GetCharABCWidthsFloatW.
7323 BOOL WINAPI GetCharABCWidthsFloatA( HDC hdc, UINT first, UINT last, LPABCFLOAT abcf )
7325 INT i, wlen;
7326 LPSTR str;
7327 LPWSTR wstr;
7328 BOOL ret = TRUE;
7330 str = FONT_GetCharsByRangeA(hdc, first, last, &i);
7331 if (str == NULL)
7332 return FALSE;
7334 wstr = FONT_mbtowc( hdc, str, i, &wlen, NULL );
7336 for (i = 0; i < wlen; i++)
7338 if (!GetCharABCWidthsFloatW( hdc, wstr[i], wstr[i], abcf ))
7340 ret = FALSE;
7341 break;
7343 abcf++;
7346 HeapFree( GetProcessHeap(), 0, str );
7347 HeapFree( GetProcessHeap(), 0, wstr );
7349 return ret;
7352 /*************************************************************************
7353 * GetCharABCWidthsFloatW [GDI32.@]
7355 * Retrieves widths of a range of characters.
7357 * PARAMS
7358 * hdc [I] Handle to device context.
7359 * first [I] First character in range to query.
7360 * last [I] Last character in range to query.
7361 * abcf [O] Array of LPABCFLOAT structures.
7363 * RETURNS
7364 * Success: TRUE
7365 * Failure: FALSE
7367 BOOL WINAPI GetCharABCWidthsFloatW( HDC hdc, UINT first, UINT last, LPABCFLOAT abcf )
7369 UINT i;
7370 ABC *abc;
7371 PHYSDEV dev;
7372 BOOL ret = FALSE;
7373 DC *dc = get_dc_ptr( hdc );
7375 TRACE("%p, %d, %d, %p\n", hdc, first, last, abcf);
7377 if (!dc) return FALSE;
7379 if (!abcf) goto done;
7380 if (!(abc = HeapAlloc( GetProcessHeap(), 0, (last - first + 1) * sizeof(*abc) ))) goto done;
7382 dev = GET_DC_PHYSDEV( dc, pGetCharABCWidths );
7383 ret = dev->funcs->pGetCharABCWidths( dev, first, last, abc );
7384 if (ret)
7386 /* convert device units to logical */
7387 FLOAT scale = fabs( dc->xformVport2World.eM11 );
7388 for (i = first; i <= last; i++, abcf++)
7390 abcf->abcfA = abc[i - first].abcA * scale;
7391 abcf->abcfB = abc[i - first].abcB * scale;
7392 abcf->abcfC = abc[i - first].abcC * scale;
7395 HeapFree( GetProcessHeap(), 0, abc );
7397 done:
7398 release_dc_ptr( dc );
7399 return ret;
7402 /*************************************************************************
7403 * GetCharWidthFloatA [GDI32.@]
7405 BOOL WINAPI GetCharWidthFloatA( HDC hdc, UINT first, UINT last, float *buffer )
7407 WCHAR *wstr;
7408 int i, wlen;
7409 char *str;
7411 if (!(str = FONT_GetCharsByRangeA( hdc, first, last, &i )))
7412 return FALSE;
7413 wstr = FONT_mbtowc( hdc, str, i, &wlen, NULL );
7414 heap_free(str);
7416 for (i = 0; i < wlen; ++i)
7418 if (!GetCharWidthFloatW( hdc, wstr[i], wstr[i], &buffer[i] ))
7420 heap_free(wstr);
7421 return FALSE;
7424 heap_free(wstr);
7425 return TRUE;
7428 /*************************************************************************
7429 * GetCharWidthFloatW [GDI32.@]
7431 BOOL WINAPI GetCharWidthFloatW( HDC hdc, UINT first, UINT last, float *buffer )
7433 DC *dc = get_dc_ptr( hdc );
7434 int *ibuffer;
7435 PHYSDEV dev;
7436 BOOL ret;
7437 UINT i;
7439 TRACE("dc %p, first %#x, last %#x, buffer %p\n", dc, first, last, buffer);
7441 if (!dc) return FALSE;
7443 if (!(ibuffer = heap_alloc( (last - first + 1) * sizeof(int) )))
7445 release_dc_ptr( dc );
7446 return FALSE;
7449 dev = GET_DC_PHYSDEV( dc, pGetCharWidth );
7450 if ((ret = dev->funcs->pGetCharWidth( dev, first, last, ibuffer )))
7452 float scale = fabs( dc->xformVport2World.eM11 ) / 16.0f;
7453 for (i = first; i <= last; ++i)
7454 buffer[i - first] = ibuffer[i - first] * scale;
7457 heap_free(ibuffer);
7458 return ret;
7461 /***********************************************************************
7463 * Font Resource API *
7465 ***********************************************************************/
7467 /***********************************************************************
7468 * AddFontResourceA (GDI32.@)
7470 INT WINAPI AddFontResourceA( LPCSTR str )
7472 return AddFontResourceExA( str, 0, NULL);
7475 /***********************************************************************
7476 * AddFontResourceW (GDI32.@)
7478 INT WINAPI AddFontResourceW( LPCWSTR str )
7480 return AddFontResourceExW(str, 0, NULL);
7484 /***********************************************************************
7485 * AddFontResourceExA (GDI32.@)
7487 INT WINAPI AddFontResourceExA( LPCSTR str, DWORD fl, PVOID pdv )
7489 DWORD len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
7490 LPWSTR strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
7491 INT ret;
7493 MultiByteToWideChar(CP_ACP, 0, str, -1, strW, len);
7494 ret = AddFontResourceExW(strW, fl, pdv);
7495 HeapFree(GetProcessHeap(), 0, strW);
7496 return ret;
7499 static BOOL CALLBACK load_enumed_resource(HMODULE hModule, LPCWSTR type, LPWSTR name, LONG_PTR lParam)
7501 HRSRC rsrc = FindResourceW(hModule, name, type);
7502 HGLOBAL hMem = LoadResource(hModule, rsrc);
7503 LPVOID *pMem = LockResource(hMem);
7504 int *num_total = (int *)lParam;
7505 DWORD num_in_res;
7507 TRACE("Found resource %s - trying to load\n", wine_dbgstr_w(type));
7508 if (!AddFontMemResourceEx(pMem, SizeofResource(hModule, rsrc), NULL, &num_in_res))
7510 ERR("Failed to load PE font resource mod=%p ptr=%p\n", hModule, hMem);
7511 return FALSE;
7514 *num_total += num_in_res;
7515 return TRUE;
7518 static void *map_file( const WCHAR *filename, LARGE_INTEGER *size )
7520 HANDLE file, mapping;
7521 void *ptr;
7523 file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
7524 if (file == INVALID_HANDLE_VALUE) return NULL;
7526 if (!GetFileSizeEx( file, size ) || size->u.HighPart)
7528 CloseHandle( file );
7529 return NULL;
7532 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
7533 CloseHandle( file );
7534 if (!mapping) return NULL;
7536 ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
7537 CloseHandle( mapping );
7539 return ptr;
7542 static void *find_resource( BYTE *ptr, WORD type, DWORD rsrc_off, DWORD size, DWORD *len )
7544 WORD align, type_id, count;
7545 DWORD res_off;
7547 if (size < rsrc_off + 10) return NULL;
7548 align = *(WORD *)(ptr + rsrc_off);
7549 rsrc_off += 2;
7550 type_id = *(WORD *)(ptr + rsrc_off);
7551 while (type_id && type_id != type)
7553 count = *(WORD *)(ptr + rsrc_off + 2);
7554 rsrc_off += 8 + count * 12;
7555 if (size < rsrc_off + 8) return NULL;
7556 type_id = *(WORD *)(ptr + rsrc_off);
7558 if (!type_id) return NULL;
7559 count = *(WORD *)(ptr + rsrc_off + 2);
7560 if (size < rsrc_off + 8 + count * 12) return NULL;
7561 res_off = *(WORD *)(ptr + rsrc_off + 8) << align;
7562 *len = *(WORD *)(ptr + rsrc_off + 10) << align;
7563 if (size < res_off + *len) return NULL;
7564 return ptr + res_off;
7567 static WCHAR *get_scalable_filename( const WCHAR *res, BOOL *hidden )
7569 LARGE_INTEGER size;
7570 BYTE *ptr = map_file( res, &size );
7571 const IMAGE_DOS_HEADER *dos;
7572 const IMAGE_OS2_HEADER *ne;
7573 WORD *fontdir;
7574 char *data;
7575 WCHAR *name = NULL;
7576 DWORD len;
7578 if (!ptr) return NULL;
7580 if (size.u.LowPart < sizeof( *dos )) goto fail;
7581 dos = (const IMAGE_DOS_HEADER *)ptr;
7582 if (dos->e_magic != IMAGE_DOS_SIGNATURE) goto fail;
7583 if (size.u.LowPart < dos->e_lfanew + sizeof( *ne )) goto fail;
7584 ne = (const IMAGE_OS2_HEADER *)(ptr + dos->e_lfanew);
7586 fontdir = find_resource( ptr, 0x8007, dos->e_lfanew + ne->ne_rsrctab, size.u.LowPart, &len );
7587 if (!fontdir) goto fail;
7588 *hidden = (fontdir[35] & 0x80) != 0; /* fontdir->dfType */
7590 data = find_resource( ptr, 0x80cc, dos->e_lfanew + ne->ne_rsrctab, size.u.LowPart, &len );
7591 if (!data) goto fail;
7592 if (!memchr( data, 0, len )) goto fail;
7594 len = MultiByteToWideChar( CP_ACP, 0, data, -1, NULL, 0 );
7595 name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
7596 if (name) MultiByteToWideChar( CP_ACP, 0, data, -1, name, len );
7598 fail:
7599 UnmapViewOfFile( ptr );
7600 return name;
7603 static int add_system_font_resource( const WCHAR *file, DWORD flags )
7605 WCHAR path[MAX_PATH];
7606 int ret;
7608 /* try in %WINDIR%/fonts, needed for Fotobuch Designer */
7609 get_fonts_win_dir_path( file, path );
7610 EnterCriticalSection( &font_cs );
7611 ret = font_funcs->add_font( path, flags );
7612 LeaveCriticalSection( &font_cs );
7613 /* try in datadir/fonts (or builddir/fonts), needed for Magic the Gathering Online */
7614 if (!ret)
7616 get_fonts_data_dir_path( file, path );
7617 EnterCriticalSection( &font_cs );
7618 ret = font_funcs->add_font( path, flags );
7619 LeaveCriticalSection( &font_cs );
7621 return ret;
7624 static BOOL remove_system_font_resource( LPCWSTR file, DWORD flags )
7626 WCHAR path[MAX_PATH];
7627 int ret;
7629 get_fonts_win_dir_path( file, path );
7630 if (!(ret = remove_font( path, flags )))
7632 get_fonts_data_dir_path( file, path );
7633 ret = remove_font( path, flags );
7635 return ret;
7638 static int add_font_resource( LPCWSTR file, DWORD flags )
7640 WCHAR path[MAX_PATH];
7641 int ret = 0;
7643 if (GetFullPathNameW( file, MAX_PATH, path, NULL ))
7645 DWORD addfont_flags = ADDFONT_ALLOW_BITMAP | ADDFONT_ADD_RESOURCE;
7647 if (!(flags & FR_PRIVATE)) addfont_flags |= ADDFONT_ADD_TO_CACHE;
7648 EnterCriticalSection( &font_cs );
7649 ret = font_funcs->add_font( path, addfont_flags );
7650 LeaveCriticalSection( &font_cs );
7653 if (!ret && !wcschr( file, '\\' ))
7654 ret = add_system_font_resource( file, ADDFONT_ALLOW_BITMAP | ADDFONT_ADD_RESOURCE );
7656 return ret;
7659 static BOOL remove_font_resource( LPCWSTR file, DWORD flags )
7661 WCHAR path[MAX_PATH];
7662 BOOL ret = FALSE;
7664 if (GetFullPathNameW( file, MAX_PATH, path, NULL ))
7666 DWORD addfont_flags = ADDFONT_ALLOW_BITMAP | ADDFONT_ADD_RESOURCE;
7668 if (!(flags & FR_PRIVATE)) addfont_flags |= ADDFONT_ADD_TO_CACHE;
7669 ret = remove_font( path, addfont_flags );
7672 if (!ret && !wcschr( file, '\\' ))
7673 ret = remove_system_font_resource( file, ADDFONT_ALLOW_BITMAP | ADDFONT_ADD_RESOURCE );
7675 return ret;
7678 static void load_system_bitmap_fonts(void)
7680 static const WCHAR * const fonts[] = { L"FONTS.FON", L"OEMFONT.FON", L"FIXEDFON.FON" };
7681 HKEY hkey;
7682 WCHAR data[MAX_PATH];
7683 DWORD i, dlen, type;
7685 if (RegOpenKeyW( HKEY_CURRENT_CONFIG, L"Software\\Fonts", &hkey )) return;
7686 for (i = 0; i < ARRAY_SIZE(fonts); i++)
7688 dlen = sizeof(data);
7689 if (!RegQueryValueExW( hkey, fonts[i], 0, &type, (BYTE *)data, &dlen ) && type == REG_SZ)
7690 add_system_font_resource( data, ADDFONT_ALLOW_BITMAP );
7692 RegCloseKey( hkey );
7695 static void load_directory_fonts( WCHAR *path, UINT flags )
7697 HANDLE handle;
7698 WIN32_FIND_DATAW data;
7699 WCHAR *p;
7701 p = path + lstrlenW(path) - 1;
7702 TRACE( "loading fonts from %s\n", debugstr_w(path) );
7703 handle = FindFirstFileW( path, &data );
7704 if (handle == INVALID_HANDLE_VALUE) return;
7707 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
7708 lstrcpyW( p, data.cFileName );
7709 font_funcs->add_font( path, flags );
7710 } while (FindNextFileW( handle, &data ));
7711 FindClose( handle );
7714 static void load_file_system_fonts(void)
7716 WCHAR *ptr, *next, path[MAX_PATH], value[1024];
7717 DWORD len = ARRAY_SIZE(value);
7719 /* Windows directory */
7720 get_fonts_win_dir_path( L"*", path );
7721 load_directory_fonts( path, 0 );
7723 /* Wine data directory */
7724 get_fonts_data_dir_path( L"*", path );
7725 load_directory_fonts( path, ADDFONT_EXTERNAL_FONT );
7727 /* custom paths */
7728 /* @@ Wine registry key: HKCU\Software\Wine\Fonts */
7729 if (!RegQueryValueExW( wine_fonts_key, L"Path", NULL, NULL, (BYTE *)value, &len ))
7731 for (ptr = value; ptr; ptr = next)
7733 if ((next = wcschr( ptr, ';' ))) *next++ = 0;
7734 if (next && next - ptr < 2) continue;
7735 lstrcpynW( path, ptr, MAX_PATH - 2 );
7736 lstrcatW( path, L"\\*" );
7737 load_directory_fonts( path, ADDFONT_EXTERNAL_FONT );
7742 struct external_key
7744 struct list entry;
7745 WCHAR value[LF_FULLFACESIZE + 12];
7748 static void update_external_font_keys(void)
7750 struct list external_keys = LIST_INIT(external_keys);
7751 HKEY winnt_key = 0, win9x_key = 0;
7752 struct gdi_font_family *family;
7753 struct external_key *key, *next;
7754 struct gdi_font_face *face;
7755 DWORD len, i = 0, type, dlen, vlen;
7756 WCHAR value[LF_FULLFACESIZE + 12], path[MAX_PATH], *tmp;
7757 WCHAR *file;
7758 HKEY hkey;
7760 RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
7761 0, NULL, 0, KEY_ALL_ACCESS, NULL, &winnt_key, NULL );
7762 RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Fonts",
7763 0, NULL, 0, KEY_ALL_ACCESS, NULL, &win9x_key, NULL );
7765 /* enumerate the fonts and add external ones to the two keys */
7767 if (RegCreateKeyW( wine_fonts_key, L"External Fonts", &hkey )) return;
7769 vlen = ARRAY_SIZE(value);
7770 dlen = sizeof(path);
7771 while (!RegEnumValueW( hkey, i++, value, &vlen, NULL, &type, (BYTE *)path, &dlen ))
7773 if (type != REG_SZ) goto next;
7774 if ((tmp = wcsrchr( value, ' ' )) && !facename_compare( tmp, L" (TrueType)", -1 )) *tmp = 0;
7775 if ((face = find_face_from_full_name( value )) && !wcsicmp( face->file, path ))
7777 face->flags |= ADDFONT_EXTERNAL_FOUND;
7778 goto next;
7780 if (tmp && !*tmp) *tmp = ' ';
7781 if (!(key = HeapAlloc( GetProcessHeap(), 0, sizeof(*key) ))) break;
7782 lstrcpyW( key->value, value );
7783 list_add_tail( &external_keys, &key->entry );
7784 next:
7785 vlen = ARRAY_SIZE(value);
7786 dlen = sizeof(path);
7789 WINE_RB_FOR_EACH_ENTRY( family, &family_name_tree, struct gdi_font_family, name_entry )
7791 LIST_FOR_EACH_ENTRY( face, &family->faces, struct gdi_font_face, entry )
7793 if (!(face->flags & ADDFONT_EXTERNAL_FONT)) continue;
7794 if ((face->flags & ADDFONT_EXTERNAL_FOUND)) continue;
7796 lstrcpyW( value, face->full_name );
7797 if (face->scalable) lstrcatW( value, L" (TrueType)" );
7799 if (GetFullPathNameW( face->file, MAX_PATH, path, NULL ))
7800 file = path;
7801 else if ((file = wcsrchr( face->file, '\\' )))
7802 file++;
7803 else
7804 file = face->file;
7806 len = (lstrlenW(file) + 1) * sizeof(WCHAR);
7807 RegSetValueExW( winnt_key, value, 0, REG_SZ, (BYTE *)file, len );
7808 RegSetValueExW( win9x_key, value, 0, REG_SZ, (BYTE *)file, len );
7809 RegSetValueExW( hkey, value, 0, REG_SZ, (BYTE *)file, len );
7812 LIST_FOR_EACH_ENTRY_SAFE( key, next, &external_keys, struct external_key, entry )
7814 RegDeleteValueW( win9x_key, key->value );
7815 RegDeleteValueW( winnt_key, key->value );
7816 RegDeleteValueW( hkey, key->value );
7817 list_remove( &key->entry );
7818 HeapFree( GetProcessHeap(), 0, key );
7820 RegCloseKey( win9x_key );
7821 RegCloseKey( winnt_key );
7822 RegCloseKey( hkey );
7825 static void load_registry_fonts(void)
7827 WCHAR value[LF_FULLFACESIZE + 12], data[MAX_PATH], *tmp;
7828 DWORD i = 0, type, dlen, vlen;
7829 HKEY hkey;
7831 /* Look under HKLM\Software\Microsoft\Windows[ NT]\CurrentVersion\Fonts
7832 for any fonts not installed in %WINDOWSDIR%\Fonts. They will have their
7833 full path as the entry. Also look for any .fon fonts, since ReadFontDir
7834 will skip these. */
7835 if (RegOpenKeyW( HKEY_LOCAL_MACHINE,
7836 is_win9x() ? L"Software\\Microsoft\\Windows\\CurrentVersion\\Fonts" :
7837 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", &hkey ))
7838 return;
7840 vlen = ARRAY_SIZE(value);
7841 dlen = sizeof(data);
7842 while (!RegEnumValueW( hkey, i++, value, &vlen, NULL, &type, NULL, NULL ))
7844 if (type != REG_SZ) goto next;
7845 dlen /= sizeof(WCHAR);
7846 if ((tmp = wcsrchr( value, ' ' )) && !facename_compare( tmp, L" (TrueType)", -1 )) *tmp = 0;
7847 if (find_face_from_full_name( value )) goto next;
7848 if (tmp && !*tmp) *tmp = ' ';
7850 if (RegQueryValueExW( hkey, value, NULL, NULL, (LPBYTE)data, &dlen ))
7852 WARN( "Unable to get face path %s\n", debugstr_w(value) );
7853 goto next;
7856 dlen /= sizeof(WCHAR);
7857 if (data[0] && data[1] == ':')
7858 add_font_resource( data, ADDFONT_ALLOW_BITMAP );
7859 else if (dlen >= 6 && !wcsicmp( data + dlen - 5, L".fon" ))
7860 add_system_font_resource( data, ADDFONT_ALLOW_BITMAP );
7861 next:
7862 vlen = ARRAY_SIZE(value);
7863 dlen = sizeof(data);
7865 RegCloseKey( hkey );
7868 static const struct font_callback_funcs callback_funcs = { add_gdi_face };
7870 /***********************************************************************
7871 * font_init
7873 void font_init(void)
7875 HANDLE mutex;
7876 DWORD disposition;
7878 if (RegCreateKeyExW( HKEY_CURRENT_USER, L"Software\\Wine\\Fonts", 0, NULL, 0,
7879 KEY_ALL_ACCESS, NULL, &wine_fonts_key, NULL ))
7880 return;
7882 init_font_options();
7883 update_codepage();
7884 if (__wine_init_unix_lib( gdi32_module, DLL_PROCESS_ATTACH, &callback_funcs, &font_funcs )) return;
7886 load_system_bitmap_fonts();
7887 load_file_system_fonts();
7888 font_funcs->load_fonts();
7890 if (!(mutex = CreateMutexW( NULL, FALSE, L"__WINE_FONT_MUTEX__" ))) return;
7891 WaitForSingleObject( mutex, INFINITE );
7893 RegCreateKeyExW( wine_fonts_key, L"Cache", 0, NULL, REG_OPTION_VOLATILE,
7894 KEY_ALL_ACCESS, NULL, &wine_fonts_cache_key, &disposition );
7896 if (disposition == REG_CREATED_NEW_KEY)
7898 load_registry_fonts();
7899 update_external_font_keys();
7902 ReleaseMutex( mutex );
7904 if (disposition != REG_CREATED_NEW_KEY)
7906 load_registry_fonts();
7907 load_font_list_from_cache();
7910 reorder_font_list();
7911 load_gdi_font_subst();
7912 load_gdi_font_replacements();
7913 load_system_links();
7914 dump_gdi_font_list();
7915 dump_gdi_font_subst();
7918 /***********************************************************************
7919 * AddFontResourceExW (GDI32.@)
7921 INT WINAPI AddFontResourceExW( LPCWSTR str, DWORD flags, PVOID pdv )
7923 int ret;
7924 WCHAR *filename;
7925 BOOL hidden;
7927 if (!font_funcs) return 1;
7928 if (!(ret = add_font_resource( str, flags )))
7930 /* FreeType <2.3.5 has problems reading resources wrapped in PE files. */
7931 HMODULE hModule = LoadLibraryExW(str, NULL, LOAD_LIBRARY_AS_DATAFILE);
7932 if (hModule != NULL)
7934 int num_resources = 0;
7935 LPWSTR rt_font = (LPWSTR)((ULONG_PTR)8); /* we don't want to include winuser.h */
7937 TRACE("WineEngAddFontResourceEx failed on PE file %s - trying to load resources manually\n",
7938 wine_dbgstr_w(str));
7939 if (EnumResourceNamesW(hModule, rt_font, load_enumed_resource, (LONG_PTR)&num_resources))
7940 ret = num_resources;
7941 FreeLibrary(hModule);
7943 else if ((filename = get_scalable_filename( str, &hidden )) != NULL)
7945 if (hidden) flags |= FR_PRIVATE | FR_NOT_ENUM;
7946 ret = add_font_resource( filename, flags );
7947 HeapFree( GetProcessHeap(), 0, filename );
7950 return ret;
7953 /***********************************************************************
7954 * RemoveFontResourceA (GDI32.@)
7956 BOOL WINAPI RemoveFontResourceA( LPCSTR str )
7958 return RemoveFontResourceExA(str, 0, 0);
7961 /***********************************************************************
7962 * RemoveFontResourceW (GDI32.@)
7964 BOOL WINAPI RemoveFontResourceW( LPCWSTR str )
7966 return RemoveFontResourceExW(str, 0, 0);
7969 /***********************************************************************
7970 * AddFontMemResourceEx (GDI32.@)
7972 HANDLE WINAPI AddFontMemResourceEx( PVOID ptr, DWORD size, PVOID pdv, DWORD *pcFonts )
7974 HANDLE ret;
7975 DWORD num_fonts;
7976 void *copy;
7978 if (!ptr || !size || !pcFonts)
7980 SetLastError(ERROR_INVALID_PARAMETER);
7981 return NULL;
7983 if (!font_funcs) return NULL;
7984 if (!(copy = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
7985 memcpy( copy, ptr, size );
7987 EnterCriticalSection( &font_cs );
7988 num_fonts = font_funcs->add_mem_font( copy, size, ADDFONT_ALLOW_BITMAP | ADDFONT_ADD_RESOURCE );
7989 LeaveCriticalSection( &font_cs );
7991 if (!num_fonts)
7993 HeapFree( GetProcessHeap(), 0, copy );
7994 return NULL;
7997 /* FIXME: is the handle only for use in RemoveFontMemResourceEx or should it be a true handle?
7998 * For now return something unique but quite random
8000 ret = (HANDLE)((INT_PTR)copy ^ 0x87654321);
8002 __TRY
8004 *pcFonts = num_fonts;
8006 __EXCEPT_PAGE_FAULT
8008 WARN("page fault while writing to *pcFonts (%p)\n", pcFonts);
8009 RemoveFontMemResourceEx( ret );
8010 ret = 0;
8012 __ENDTRY
8013 TRACE( "Returning handle %p\n", ret );
8014 return ret;
8017 /***********************************************************************
8018 * RemoveFontMemResourceEx (GDI32.@)
8020 BOOL WINAPI RemoveFontMemResourceEx( HANDLE fh )
8022 FIXME("(%p) stub\n", fh);
8023 return TRUE;
8026 /***********************************************************************
8027 * RemoveFontResourceExA (GDI32.@)
8029 BOOL WINAPI RemoveFontResourceExA( LPCSTR str, DWORD fl, PVOID pdv )
8031 DWORD len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
8032 LPWSTR strW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
8033 INT ret;
8035 MultiByteToWideChar(CP_ACP, 0, str, -1, strW, len);
8036 ret = RemoveFontResourceExW(strW, fl, pdv);
8037 HeapFree(GetProcessHeap(), 0, strW);
8038 return ret;
8041 /***********************************************************************
8042 * RemoveFontResourceExW (GDI32.@)
8044 BOOL WINAPI RemoveFontResourceExW( LPCWSTR str, DWORD flags, PVOID pdv )
8046 int ret;
8047 WCHAR *filename;
8048 BOOL hidden;
8050 if (!font_funcs) return TRUE;
8052 if (!(ret = remove_font_resource( str, flags )))
8054 /* FreeType <2.3.5 has problems reading resources wrapped in PE files. */
8055 HMODULE hModule = LoadLibraryExW(str, NULL, LOAD_LIBRARY_AS_DATAFILE);
8056 if (hModule != NULL)
8058 WARN("Can't unload resources from PE file %s\n", wine_dbgstr_w(str));
8059 FreeLibrary(hModule);
8061 else if ((filename = get_scalable_filename( str, &hidden )) != NULL)
8063 if (hidden) flags |= FR_PRIVATE | FR_NOT_ENUM;
8064 ret = remove_font_resource( filename, flags );
8065 HeapFree( GetProcessHeap(), 0, filename );
8068 return ret;
8071 /***********************************************************************
8072 * GetFontResourceInfoW (GDI32.@)
8074 BOOL WINAPI GetFontResourceInfoW( LPCWSTR str, LPDWORD size, PVOID buffer, DWORD type )
8076 FIXME("%s %p(%d) %p %d\n", debugstr_w(str), size, size ? *size : 0, buffer, type);
8077 return FALSE;
8080 /***********************************************************************
8081 * GetTextCharset (GDI32.@)
8083 UINT WINAPI GetTextCharset(HDC hdc)
8085 /* MSDN docs say this is equivalent */
8086 return NtGdiGetTextCharsetInfo( hdc, NULL, 0 );
8089 /***********************************************************************
8090 * GdiGetCharDimensions (GDI32.@)
8092 * Gets the average width of the characters in the English alphabet.
8094 * PARAMS
8095 * hdc [I] Handle to the device context to measure on.
8096 * lptm [O] Pointer to memory to store the text metrics into.
8097 * height [O] On exit, the maximum height of characters in the English alphabet.
8099 * RETURNS
8100 * The average width of characters in the English alphabet.
8102 * NOTES
8103 * This function is used by the dialog manager to get the size of a dialog
8104 * unit. It should also be used by other pieces of code that need to know
8105 * the size of a dialog unit in logical units without having access to the
8106 * window handle of the dialog.
8107 * Windows caches the font metrics from this function, but we don't and
8108 * there doesn't appear to be an immediate advantage to do so.
8110 * SEE ALSO
8111 * GetTextExtentPointW, GetTextMetricsW, MapDialogRect.
8113 LONG WINAPI GdiGetCharDimensions(HDC hdc, LPTEXTMETRICW lptm, LONG *height)
8115 SIZE sz;
8117 if(lptm && !GetTextMetricsW(hdc, lptm)) return 0;
8119 if(!GetTextExtentPointW(hdc, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 52, &sz))
8120 return 0;
8122 if (height) *height = sz.cy;
8123 return (sz.cx / 26 + 1) / 2;
8126 BOOL WINAPI EnableEUDC(BOOL fEnableEUDC)
8128 FIXME("(%d): stub\n", fEnableEUDC);
8129 return FALSE;
8132 /***********************************************************************
8133 * GetCharWidthI (GDI32.@)
8135 * Retrieve widths of characters.
8137 * PARAMS
8138 * hdc [I] Handle to a device context.
8139 * first [I] First glyph in range to query.
8140 * count [I] Number of glyph indices to query.
8141 * glyphs [I] Array of glyphs to query.
8142 * buffer [O] Buffer to receive character widths.
8144 * NOTES
8145 * Only works with TrueType fonts.
8147 * RETURNS
8148 * Success: TRUE
8149 * Failure: FALSE
8151 BOOL WINAPI GetCharWidthI(HDC hdc, UINT first, UINT count, LPWORD glyphs, LPINT buffer)
8153 ABC *abc;
8154 unsigned int i;
8156 TRACE("(%p, %d, %d, %p, %p)\n", hdc, first, count, glyphs, buffer);
8158 if (!(abc = HeapAlloc(GetProcessHeap(), 0, count * sizeof(ABC))))
8159 return FALSE;
8161 if (!GetCharABCWidthsI(hdc, first, count, glyphs, abc))
8163 HeapFree(GetProcessHeap(), 0, abc);
8164 return FALSE;
8167 for (i = 0; i < count; i++)
8168 buffer[i] = abc[i].abcA + abc[i].abcB + abc[i].abcC;
8170 HeapFree(GetProcessHeap(), 0, abc);
8171 return TRUE;
8174 /***********************************************************************
8175 * GetFontUnicodeRanges (GDI32.@)
8177 * Retrieve a list of supported Unicode characters in a font.
8179 * PARAMS
8180 * hdc [I] Handle to a device context.
8181 * lpgs [O] GLYPHSET structure specifying supported character ranges.
8183 * RETURNS
8184 * Success: Number of bytes written to the buffer pointed to by lpgs.
8185 * Failure: 0
8188 DWORD WINAPI GetFontUnicodeRanges(HDC hdc, LPGLYPHSET lpgs)
8190 DWORD ret;
8191 PHYSDEV dev;
8192 DC *dc = get_dc_ptr(hdc);
8194 TRACE("(%p, %p)\n", hdc, lpgs);
8196 if (!dc) return 0;
8198 dev = GET_DC_PHYSDEV( dc, pGetFontUnicodeRanges );
8199 ret = dev->funcs->pGetFontUnicodeRanges( dev, lpgs );
8200 release_dc_ptr(dc);
8201 return ret;
8205 /*************************************************************
8206 * FontIsLinked (GDI32.@)
8208 BOOL WINAPI FontIsLinked(HDC hdc)
8210 DC *dc = get_dc_ptr(hdc);
8211 PHYSDEV dev;
8212 BOOL ret;
8214 if (!dc) return FALSE;
8215 dev = GET_DC_PHYSDEV( dc, pFontIsLinked );
8216 ret = dev->funcs->pFontIsLinked( dev );
8217 release_dc_ptr(dc);
8218 TRACE("returning %d\n", ret);
8219 return ret;
8222 /*************************************************************
8223 * GetFontRealizationInfo (GDI32.@)
8225 BOOL WINAPI GetFontRealizationInfo(HDC hdc, struct font_realization_info *info)
8227 BOOL is_v0 = info->size == FIELD_OFFSET(struct font_realization_info, file_count);
8228 PHYSDEV dev;
8229 BOOL ret;
8230 DC *dc;
8232 if (info->size != sizeof(*info) && !is_v0)
8233 return FALSE;
8235 dc = get_dc_ptr(hdc);
8236 if (!dc) return FALSE;
8237 dev = GET_DC_PHYSDEV( dc, pGetFontRealizationInfo );
8238 ret = dev->funcs->pGetFontRealizationInfo( dev, info );
8239 release_dc_ptr(dc);
8240 return ret;
8243 /*************************************************************************
8244 * GetRasterizerCaps (GDI32.@)
8246 BOOL WINAPI GetRasterizerCaps( LPRASTERIZER_STATUS lprs, UINT cbNumBytes)
8248 lprs->nSize = sizeof(RASTERIZER_STATUS);
8249 lprs->wFlags = font_funcs ? (TT_AVAILABLE | TT_ENABLED) : 0;
8250 lprs->nLanguageID = 0;
8251 return TRUE;
8254 /*************************************************************************
8255 * GetFontFileData (GDI32.@)
8257 BOOL WINAPI GetFontFileData( DWORD instance_id, DWORD file_index, UINT64 offset, void *buff, DWORD buff_size )
8259 struct gdi_font *font;
8260 DWORD tag = 0, size;
8261 BOOL ret = FALSE;
8263 if (!font_funcs) return FALSE;
8264 EnterCriticalSection( &font_cs );
8265 if ((font = get_font_from_handle( instance_id )))
8267 if (font->ttc_item_offset) tag = MS_TTCF_TAG;
8268 size = font_funcs->get_font_data( font, tag, 0, NULL, 0 );
8269 if (size != GDI_ERROR && size >= buff_size && offset <= size - buff_size)
8270 ret = font_funcs->get_font_data( font, tag, offset, buff, buff_size ) != GDI_ERROR;
8271 else
8272 SetLastError( ERROR_INVALID_PARAMETER );
8274 LeaveCriticalSection( &font_cs );
8275 return ret;
8278 /* Undocumented structure filled in by GetFontFileInfo */
8279 struct font_fileinfo
8281 FILETIME writetime;
8282 LARGE_INTEGER size;
8283 WCHAR path[1];
8286 /*************************************************************************
8287 * GetFontFileInfo (GDI32.@)
8289 BOOL WINAPI GetFontFileInfo( DWORD instance_id, DWORD file_index, struct font_fileinfo *info,
8290 SIZE_T size, SIZE_T *needed )
8292 SIZE_T required_size = 0;
8293 struct gdi_font *font;
8294 BOOL ret = FALSE;
8296 EnterCriticalSection( &font_cs );
8298 if ((font = get_font_from_handle( instance_id )))
8300 required_size = sizeof(*info) + lstrlenW( font->file ) * sizeof(WCHAR);
8301 if (required_size <= size)
8303 info->writetime = font->writetime;
8304 info->size.QuadPart = font->data_size;
8305 lstrcpyW( info->path, font->file );
8306 ret = TRUE;
8308 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
8311 LeaveCriticalSection( &font_cs );
8312 if (needed) *needed = required_size;
8313 return ret;
8316 struct realization_info
8318 DWORD flags; /* 1 for bitmap fonts, 3 for scalable fonts */
8319 DWORD cache_num; /* keeps incrementing - num of fonts that have been created allowing for caching?? */
8320 DWORD instance_id; /* identifies a realized font instance */
8323 /*************************************************************
8324 * GdiRealizationInfo (GDI32.@)
8326 * Returns a structure that contains some font information.
8328 BOOL WINAPI GdiRealizationInfo(HDC hdc, struct realization_info *info)
8330 struct font_realization_info ri;
8331 BOOL ret;
8333 ri.size = sizeof(ri);
8334 ret = GetFontRealizationInfo( hdc, &ri );
8335 if (ret)
8337 info->flags = ri.flags;
8338 info->cache_num = ri.cache_num;
8339 info->instance_id = ri.instance_id;
8342 return ret;
8345 /*************************************************************
8346 * GetCharWidthInfo (GDI32.@)
8349 BOOL WINAPI GetCharWidthInfo(HDC hdc, struct char_width_info *info)
8351 PHYSDEV dev;
8352 BOOL ret;
8353 DC *dc;
8355 dc = get_dc_ptr(hdc);
8356 if (!dc) return FALSE;
8357 dev = GET_DC_PHYSDEV( dc, pGetCharWidthInfo );
8358 ret = dev->funcs->pGetCharWidthInfo( dev, info );
8360 if (ret)
8362 info->lsb = width_to_LP( dc, info->lsb );
8363 info->rsb = width_to_LP( dc, info->rsb );
8365 release_dc_ptr(dc);
8366 return ret;