* alloc.c, font.c, font.h, ftfont.c, ftxfont.c, macfont.m,
[bpt/emacs.git] / src / w32font.c
CommitLineData
b46a6a83 1/* Font backend for the Microsoft Windows API.
ab422c4d 2 Copyright (C) 2007-2013 Free Software Foundation, Inc.
f7a84cb4
JR
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
f7a84cb4 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
f7a84cb4
JR
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f7a84cb4
JR
18
19#include <config.h>
20#include <windows.h>
0fda9b75 21#include <stdio.h>
09fe06b7 22#include <math.h>
0500dcc9 23#include <ctype.h>
6fe9826d 24#include <commdlg.h>
f7a84cb4
JR
25
26#include "lisp.h"
27#include "w32term.h"
28#include "frame.h"
29#include "dispextern.h"
30#include "character.h"
31#include "charset.h"
e6df5336 32#include "coding.h"
f7a84cb4
JR
33#include "fontset.h"
34#include "font.h"
46fd1ded 35#include "w32font.h"
f7a84cb4 36
91583281
JR
37/* Cleartype available on Windows XP, cleartype_natural from XP SP1.
38 The latter does not try to fit cleartype smoothed fonts into the
39 same bounding box as the non-antialiased version of the font.
40 */
41#ifndef CLEARTYPE_QUALITY
42#define CLEARTYPE_QUALITY 5
43#endif
44#ifndef CLEARTYPE_NATURAL_QUALITY
45#define CLEARTYPE_NATURAL_QUALITY 6
46#endif
47
57016d37
JR
48/* VIETNAMESE_CHARSET and JOHAB_CHARSET are not defined in some versions
49 of MSVC headers. */
50#ifndef VIETNAMESE_CHARSET
51#define VIETNAMESE_CHARSET 163
52#endif
53#ifndef JOHAB_CHARSET
54#define JOHAB_CHARSET 130
55#endif
56
91583281 57Lisp_Object Qgdi;
34fd2d28
JR
58Lisp_Object Quniscribe;
59static Lisp_Object QCformat;
9e1a2995
JR
60static Lisp_Object Qmonospace, Qsansserif, Qmono, Qsans, Qsans_serif;
61static Lisp_Object Qserif, Qscript, Qdecorative;
62static Lisp_Object Qraster, Qoutline, Qunknown;
d205d43b 63
91583281 64/* antialiasing */
91583281
JR
65static Lisp_Object Qstandard, Qsubpixel, Qnatural;
66
8f112d52 67/* languages */
314d66f4 68static Lisp_Object Qzh;
8f112d52 69
d205d43b 70/* scripts */
a11889ab 71static Lisp_Object Qgreek, Qcoptic, Qcyrillic, Qarmenian, Qhebrew;
d205d43b
JR
72static Lisp_Object Qarabic, Qsyriac, Qnko, Qthaana, Qdevanagari, Qbengali;
73static Lisp_Object Qgurmukhi, Qgujarati, Qoriya, Qtamil, Qtelugu;
74static Lisp_Object Qkannada, Qmalayalam, Qsinhala, Qthai, Qlao;
75static Lisp_Object Qtibetan, Qmyanmar, Qgeorgian, Qhangul, Qethiopic;
76static Lisp_Object Qcherokee, Qcanadian_aboriginal, Qogham, Qrunic;
8dc2e44a 77static Lisp_Object Qkhmer, Qmongolian, Qbraille, Qhan;
d205d43b
JR
78static Lisp_Object Qideographic_description, Qcjk_misc, Qkana, Qbopomofo;
79static Lisp_Object Qkanbun, Qyi, Qbyzantine_musical_symbol;
174f1c74 80static Lisp_Object Qmusical_symbol, Qmathematical, Qcham, Qphonetic;
56df6710
JR
81/* Not defined in characters.el, but referenced in fontset.el. */
82static Lisp_Object Qbalinese, Qbuginese, Qbuhid, Qcuneiform, Qcypriot;
83static Lisp_Object Qdeseret, Qglagolitic, Qgothic, Qhanunoo, Qkharoshthi;
84static Lisp_Object Qlimbu, Qlinear_b, Qold_italic, Qold_persian, Qosmanya;
85static Lisp_Object Qphags_pa, Qphoenician, Qshavian, Qsyloti_nagri;
86static Lisp_Object Qtagalog, Qtagbanwa, Qtai_le, Qtifinagh, Qugaritic;
d205d43b 87
57016d37
JR
88/* W32 charsets: for use in Vw32_charset_info_alist. */
89static Lisp_Object Qw32_charset_ansi, Qw32_charset_default;
90static Lisp_Object Qw32_charset_symbol, Qw32_charset_shiftjis;
91static Lisp_Object Qw32_charset_hangeul, Qw32_charset_gb2312;
92static Lisp_Object Qw32_charset_chinesebig5, Qw32_charset_oem;
93static Lisp_Object Qw32_charset_easteurope, Qw32_charset_turkish;
94static Lisp_Object Qw32_charset_baltic, Qw32_charset_russian;
95static Lisp_Object Qw32_charset_arabic, Qw32_charset_greek;
96static Lisp_Object Qw32_charset_hebrew, Qw32_charset_vietnamese;
97static Lisp_Object Qw32_charset_thai, Qw32_charset_johab, Qw32_charset_mac;
98
d205d43b
JR
99/* Font spacing symbols - defined in font.c. */
100extern Lisp_Object Qc, Qp, Qm;
f7a84cb4 101
a10c8269 102static void fill_in_logfont (struct frame *, LOGFONT *, Lisp_Object);
f7a84cb4 103
f57e2426
J
104static BYTE w32_antialias_type (Lisp_Object);
105static Lisp_Object lispy_antialias_type (BYTE);
91583281 106
f57e2426
J
107static Lisp_Object font_supported_scripts (FONTSIGNATURE *);
108static int w32font_full_name (LOGFONT *, Lisp_Object, int, char *, int);
109static void compute_metrics (HDC, struct w32font_info *, unsigned int,
110 struct w32_metric_cache *);
f7a84cb4 111
f57e2426 112static Lisp_Object w32_registry (LONG, DWORD);
f7a84cb4
JR
113
114/* EnumFontFamiliesEx callbacks. */
f57e2426
J
115static int CALLBACK add_font_entity_to_list (ENUMLOGFONTEX *,
116 NEWTEXTMETRICEX *,
117 DWORD, LPARAM);
118static int CALLBACK add_one_font_entity_to_list (ENUMLOGFONTEX *,
f7a84cb4 119 NEWTEXTMETRICEX *,
f57e2426
J
120 DWORD, LPARAM);
121static int CALLBACK add_font_name_to_list (ENUMLOGFONTEX *,
122 NEWTEXTMETRICEX *,
123 DWORD, LPARAM);
f7a84cb4 124
d205d43b
JR
125/* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
126 of what we really want. */
127struct font_callback_data
128{
129 /* The logfont we are matching against. EnumFontFamiliesEx only matches
130 face name and charset, so we need to manually match everything else
131 in the callback function. */
132 LOGFONT pattern;
133 /* The original font spec or entity. */
134 Lisp_Object orig_font_spec;
135 /* The frame the font is being loaded on. */
136 Lisp_Object frame;
137 /* The list to add matches to. */
138 Lisp_Object list;
46fd1ded
JR
139 /* Whether to match only opentype fonts. */
140 int opentype_only;
d205d43b
JR
141};
142
143/* Handles the problem that EnumFontFamiliesEx will not return all
144 style variations if the font name is not specified. */
f57e2426 145static void list_all_matching_fonts (struct font_callback_data *);
d205d43b 146
db4f02f2
EZ
147static BOOL g_b_init_is_w9x;
148static BOOL g_b_init_get_outline_metrics_w;
149static BOOL g_b_init_get_text_metrics_w;
150static BOOL g_b_init_get_glyph_outline_w;
151static BOOL g_b_init_get_glyph_outline_w;
579ca67b 152static BOOL g_b_init_get_char_width_32_w;
db4f02f2
EZ
153
154typedef UINT (WINAPI * GetOutlineTextMetricsW_Proc) (
155 HDC hdc,
156 UINT cbData,
157 LPOUTLINETEXTMETRICW lpotmw);
158typedef BOOL (WINAPI * GetTextMetricsW_Proc) (
159 HDC hdc,
160 LPTEXTMETRICW lptmw);
161typedef DWORD (WINAPI * GetGlyphOutlineW_Proc) (
162 HDC hdc,
163 UINT uChar,
164 UINT uFormat,
165 LPGLYPHMETRICS lpgm,
166 DWORD cbBuffer,
167 LPVOID lpvBuffer,
168 const MAT2 *lpmat2);
579ca67b
TS
169typedef BOOL (WINAPI * GetCharWidth32W_Proc) (
170 HDC hdc,
171 UINT uFirstChar,
172 UINT uLastChar,
173 LPINT lpBuffer);
db4f02f2
EZ
174
175/* Several "wide" functions we use to support the font backends are
176 unavailable on Windows 9X, unless UNICOWS.DLL is installed (their
177 versions in the default libraries are non-functional stubs). On NT
178 and later systems, these functions are in GDI32.DLL. The following
179 helper function attempts to load UNICOWS.DLL on Windows 9X, and
180 refuses to let Emacs start up if that library is not found. On NT
181 and later versions, it simply loads GDI32.DLL, which should always
182 be available. */
183static HMODULE
184w32_load_unicows_or_gdi32 (void)
185{
186 static BOOL is_9x = 0;
187 OSVERSIONINFO os_ver;
188 HMODULE ret;
189 if (g_b_init_is_w9x == 0)
190 {
191 g_b_init_is_w9x = 1;
192 ZeroMemory (&os_ver, sizeof (OSVERSIONINFO));
193 os_ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
194 if (GetVersionEx (&os_ver))
195 is_9x = (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
196 }
197 if (is_9x)
198 {
199 ret = LoadLibrary ("Unicows.dll");
200 if (!ret)
201 {
202 int button;
203
204 button = MessageBox (NULL,
205 "Emacs cannot load the UNICOWS.DLL library.\n"
206 "This library is essential for using Emacs\n"
207 "on this system. You need to install it.\n\n"
208 "However, you can still use Emacs by invoking\n"
209 "it with the '-nw' command-line option.\n\n"
210 "Emacs will exit when you click OK.",
211 "Emacs cannot load UNICOWS.DLL",
212 MB_ICONERROR | MB_TASKMODAL
213 | MB_SETFOREGROUND | MB_OK);
214 switch (button)
215 {
216 case IDOK:
217 default:
218 exit (1);
219 }
220 }
221 }
222 else
223 ret = LoadLibrary ("Gdi32.dll");
c20992f4 224 return ret;
db4f02f2
EZ
225}
226
227/* The following 3 functions call the problematic "wide" APIs via
228 function pointers, to avoid linking against the non-standard
229 libunicows on W9X. */
230static UINT WINAPI
231get_outline_metrics_w(HDC hdc, UINT cbData, LPOUTLINETEXTMETRICW lpotmw)
232{
233 static GetOutlineTextMetricsW_Proc s_pfn_Get_Outline_Text_MetricsW = NULL;
234 HMODULE hm_unicows = NULL;
235 if (g_b_init_get_outline_metrics_w == 0)
236 {
237 g_b_init_get_outline_metrics_w = 1;
238 hm_unicows = w32_load_unicows_or_gdi32 ();
239 if (hm_unicows)
240 s_pfn_Get_Outline_Text_MetricsW = (GetOutlineTextMetricsW_Proc)
241 GetProcAddress (hm_unicows, "GetOutlineTextMetricsW");
242 }
4e6a86c6 243 eassert (s_pfn_Get_Outline_Text_MetricsW != NULL);
db4f02f2
EZ
244 return s_pfn_Get_Outline_Text_MetricsW (hdc, cbData, lpotmw);
245}
246
247static BOOL WINAPI
248get_text_metrics_w(HDC hdc, LPTEXTMETRICW lptmw)
249{
250 static GetTextMetricsW_Proc s_pfn_Get_Text_MetricsW = NULL;
251 HMODULE hm_unicows = NULL;
252 if (g_b_init_get_text_metrics_w == 0)
253 {
254 g_b_init_get_text_metrics_w = 1;
255 hm_unicows = w32_load_unicows_or_gdi32 ();
256 if (hm_unicows)
257 s_pfn_Get_Text_MetricsW = (GetTextMetricsW_Proc)
258 GetProcAddress (hm_unicows, "GetTextMetricsW");
259 }
4e6a86c6 260 eassert (s_pfn_Get_Text_MetricsW != NULL);
db4f02f2
EZ
261 return s_pfn_Get_Text_MetricsW (hdc, lptmw);
262}
263
264static DWORD WINAPI
265get_glyph_outline_w (HDC hdc, UINT uChar, UINT uFormat, LPGLYPHMETRICS lpgm,
266 DWORD cbBuffer, LPVOID lpvBuffer, const MAT2 *lpmat2)
267{
268 static GetGlyphOutlineW_Proc s_pfn_Get_Glyph_OutlineW = NULL;
269 HMODULE hm_unicows = NULL;
270 if (g_b_init_get_glyph_outline_w == 0)
271 {
272 g_b_init_get_glyph_outline_w = 1;
273 hm_unicows = w32_load_unicows_or_gdi32 ();
274 if (hm_unicows)
275 s_pfn_Get_Glyph_OutlineW = (GetGlyphOutlineW_Proc)
276 GetProcAddress (hm_unicows, "GetGlyphOutlineW");
277 }
4e6a86c6 278 eassert (s_pfn_Get_Glyph_OutlineW != NULL);
db4f02f2
EZ
279 return s_pfn_Get_Glyph_OutlineW (hdc, uChar, uFormat, lpgm, cbBuffer,
280 lpvBuffer, lpmat2);
281}
f7a84cb4 282
579ca67b
TS
283static DWORD WINAPI get_char_width_32_w (HDC hdc, UINT uFirstChar,
284 UINT uLastChar, LPINT lpBuffer)
285{
286 static GetCharWidth32W_Proc s_pfn_Get_Char_Width_32W = NULL;
287 HMODULE hm_unicows = NULL;
288 if (g_b_init_get_char_width_32_w == 0)
289 {
290 g_b_init_get_char_width_32_w = 1;
291 hm_unicows = w32_load_unicows_or_gdi32 ();
292 if (hm_unicows)
293 s_pfn_Get_Char_Width_32W = (GetCharWidth32W_Proc)
294 GetProcAddress (hm_unicows, "GetCharWidth32W");
295 }
296 eassert (s_pfn_Get_Char_Width_32W != NULL);
297 return s_pfn_Get_Char_Width_32W (hdc, uFirstChar, uLastChar, lpBuffer);
298}
299
f7a84cb4 300static int
b56ceb92 301memq_no_quit (Lisp_Object elt, Lisp_Object list)
f7a84cb4
JR
302{
303 while (CONSP (list) && ! EQ (XCAR (list), elt))
304 list = XCDR (list);
305 return (CONSP (list));
306}
307
e6df5336 308Lisp_Object
b56ceb92 309intern_font_name (char * string)
e6df5336 310{
e8df9267
DA
311 Lisp_Object str = DECODE_SYSTEM (build_string (string));
312 int len = SCHARS (str);
313 Lisp_Object obarray = check_obarray (Vobarray);
b46a6a83 314 Lisp_Object tem = oblookup (obarray, SDATA (str), len, len);
e8df9267
DA
315 /* This code is similar to intern function from lread.c. */
316 return SYMBOLP (tem) ? tem : Fintern (str, obarray);
e6df5336
JR
317}
318
f7a84cb4
JR
319/* w32 implementation of get_cache for font backend.
320 Return a cache of font-entities on FRAME. The cache must be a
321 cons whose cdr part is the actual cache area. */
46fd1ded 322Lisp_Object
a10c8269 323w32font_get_cache (struct frame *f)
f7a84cb4 324{
aad3612f 325 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
f7a84cb4
JR
326
327 return (dpyinfo->name_list_element);
328}
329
330/* w32 implementation of list for font backend.
331 List fonts exactly matching with FONT_SPEC on FRAME. The value
332 is a vector of font-entities. This is the sole API that
333 allocates font-entities. */
20399669 334static Lisp_Object
fdb396e2 335w32font_list (struct frame *f, Lisp_Object font_spec)
f7a84cb4 336{
fdb396e2 337 Lisp_Object fonts = w32font_list_internal (f, font_spec, 0);
678dca3d 338 FONT_ADD_LOG ("w32font-list", font_spec, fonts);
07d9ba9b 339 return fonts;
f7a84cb4
JR
340}
341
342/* w32 implementation of match for font backend.
343 Return a font entity most closely matching with FONT_SPEC on
4c36be58 344 FRAME. The closeness is determined by the font backend, thus
f7a84cb4 345 `face-font-selection-order' is ignored here. */
20399669 346static Lisp_Object
fdb396e2 347w32font_match (struct frame *f, Lisp_Object font_spec)
f7a84cb4 348{
fdb396e2 349 Lisp_Object entity = w32font_match_internal (f, font_spec, 0);
678dca3d 350 FONT_ADD_LOG ("w32font-match", font_spec, entity);
07d9ba9b 351 return entity;
f7a84cb4
JR
352}
353
f7a84cb4
JR
354/* w32 implementation of list_family for font backend.
355 List available families. The value is a list of family names
356 (symbols). */
20399669 357static Lisp_Object
fdb396e2 358w32font_list_family (struct frame *f)
f7a84cb4
JR
359{
360 Lisp_Object list = Qnil;
361 LOGFONT font_match_pattern;
362 HDC dc;
f7a84cb4 363
72af86bd 364 memset (&font_match_pattern, 0, sizeof (font_match_pattern));
56df6710 365 font_match_pattern.lfCharSet = DEFAULT_CHARSET;
f7a84cb4
JR
366
367 dc = get_frame_dc (f);
368
369 EnumFontFamiliesEx (dc, &font_match_pattern,
370 (FONTENUMPROC) add_font_name_to_list,
371 (LPARAM) &list, 0);
372 release_frame_dc (f, dc);
373
374 return list;
375}
376
377/* w32 implementation of open for font backend.
378 Open a font specified by FONT_ENTITY on frame F.
379 If the font is scalable, open it with PIXEL_SIZE. */
5f18d119 380static Lisp_Object
a10c8269 381w32font_open (struct frame *f, Lisp_Object font_entity, int pixel_size)
f7a84cb4 382{
bd187c49
JR
383 Lisp_Object font_object
384 = font_make_object (VECSIZE (struct w32font_info),
385 font_entity, pixel_size);
386 struct w32font_info *w32_font
387 = (struct w32font_info *) XFONT_OBJECT (font_object);
f7a84cb4 388
4b135503 389 ASET (font_object, FONT_TYPE_INDEX, Qgdi);
f7a84cb4 390
5f18d119 391 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
f7a84cb4 392 {
5f18d119 393 return Qnil;
f7a84cb4
JR
394 }
395
bd187c49
JR
396 /* GDI backend does not use glyph indices. */
397 w32_font->glyph_idx = 0;
398
5f18d119 399 return font_object;
f7a84cb4
JR
400}
401
78e0b35c 402/* w32 implementation of close for font_backend. */
46fd1ded 403void
78e0b35c 404w32font_close (struct font *font)
f7a84cb4 405{
5f18d119 406 struct w32font_info *w32_font = (struct w32font_info *) font;
c35f9821 407
78e0b35c 408 if (w32_font->hfont)
c35f9821 409 {
78e0b35c
DA
410 /* Delete the GDI font object. */
411 DeleteObject (w32_font->hfont);
412 w32_font->hfont = NULL;
413
414 /* Free all the cached metrics. */
415 if (w32_font->cached_metrics)
416 {
6702c559
EZ
417 int i;
418
78e0b35c
DA
419 for (i = 0; i < w32_font->n_cache_blocks; i++)
420 xfree (w32_font->cached_metrics[i]);
421 xfree (w32_font->cached_metrics);
422 w32_font->cached_metrics = NULL;
423 }
c35f9821 424 }
f7a84cb4
JR
425}
426
427/* w32 implementation of has_char for font backend.
428 Optional.
429 If FONT_ENTITY has a glyph for character C (Unicode code point),
430 return 1. If not, return 0. If a font must be opened to check
431 it, return -1. */
46fd1ded 432int
b56ceb92 433w32font_has_char (Lisp_Object entity, int c)
f7a84cb4 434{
e6eee6ae
JR
435 /* We can't be certain about which characters a font will support until
436 we open it. Checking the scripts that the font supports turns out
437 to not be reliable. */
438 return -1;
439
440#if 0
d205d43b 441 Lisp_Object supported_scripts, extra, script;
f7a84cb4
JR
442 DWORD mask;
443
01dbeb0b
JR
444 extra = AREF (entity, FONT_EXTRA_INDEX);
445 if (!CONSP (extra))
446 return -1;
447
d205d43b 448 supported_scripts = assq_no_quit (QCscript, extra);
6a8082b5
JR
449 /* If font doesn't claim to support any scripts, then we can't be certain
450 until we open it. */
d205d43b 451 if (!CONSP (supported_scripts))
f7a84cb4
JR
452 return -1;
453
d205d43b 454 supported_scripts = XCDR (supported_scripts);
f7a84cb4 455
d205d43b 456 script = CHAR_TABLE_REF (Vchar_script_table, c);
f7a84cb4 457
6a8082b5
JR
458 /* If we don't know what script the character is from, then we can't be
459 certain until we open it. Also if the font claims support for the script
460 the character is from, it may only have partial coverage, so we still
461 can't be certain until we open the font. */
462 if (NILP (script) || memq_no_quit (script, supported_scripts))
463 return -1;
464
465 /* Font reports what scripts it supports, and none of them are the script
e6eee6ae
JR
466 the character is from. But we still can't be certain, as some fonts
467 will contain some/most/all of the characters in that script without
468 claiming support for it. */
469 return -1;
470#endif
f7a84cb4
JR
471}
472
473/* w32 implementation of encode_char for font backend.
51e4f4a8 474 Return a glyph code of FONT for character C (Unicode code point).
bd187c49
JR
475 If FONT doesn't have such a glyph, return FONT_INVALID_CODE.
476
fe7a3057 477 For speed, the gdi backend uses Unicode (Emacs calls encode_char
bd187c49
JR
478 far too often for it to be efficient). But we still need to detect
479 which characters are not supported by the font.
480 */
34fd2d28 481static unsigned
b56ceb92 482w32font_encode_char (struct font *font, int c)
f7a84cb4 483{
bd187c49 484 struct w32font_info * w32_font = (struct w32font_info *)font;
34fd2d28 485
bd187c49
JR
486 if (c < w32_font->metrics.tmFirstChar
487 || c > w32_font->metrics.tmLastChar)
488 return FONT_INVALID_CODE;
34fd2d28 489 else
bd187c49 490 return c;
f7a84cb4
JR
491}
492
493/* w32 implementation of text_extents for font backend.
494 Perform the size computation of glyphs of FONT and fillin members
495 of METRICS. The glyphs are specified by their glyph codes in
78573c57 496 CODE (length NGLYPHS). Apparently metrics can be NULL, in this
f7a84cb4 497 case just return the overall width. */
46fd1ded 498int
b56ceb92
JB
499w32font_text_extents (struct font *font, unsigned *code,
500 int nglyphs, struct font_metrics *metrics)
f7a84cb4
JR
501{
502 int i;
2a36efcf
JR
503 HFONT old_font = NULL;
504 HDC dc = NULL;
46fd1ded 505 struct frame * f;
f7a84cb4 506 int total_width = 0;
f31cf550 507 WORD *wcode;
d205d43b 508 SIZE size;
f7a84cb4 509
1b5defe6
JR
510 struct w32font_info *w32_font = (struct w32font_info *) font;
511
f7a84cb4
JR
512 if (metrics)
513 {
72af86bd 514 memset (metrics, 0, sizeof (struct font_metrics));
388c38f9
JR
515 metrics->ascent = font->ascent;
516 metrics->descent = font->descent;
f7a84cb4
JR
517
518 for (i = 0; i < nglyphs; i++)
519 {
8f112d52
JR
520 struct w32_metric_cache *char_metric;
521 int block = *(code + i) / CACHE_BLOCKSIZE;
522 int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
523
524 if (block >= w32_font->n_cache_blocks)
525 {
526 if (!w32_font->cached_metrics)
527 w32_font->cached_metrics
528 = xmalloc ((block + 1)
9cac6313 529 * sizeof (struct w32_metric_cache *));
8f112d52
JR
530 else
531 w32_font->cached_metrics
532 = xrealloc (w32_font->cached_metrics,
533 (block + 1)
9cac6313 534 * sizeof (struct w32_metric_cache *));
72af86bd
AS
535 memset (w32_font->cached_metrics + w32_font->n_cache_blocks, 0,
536 ((block + 1 - w32_font->n_cache_blocks)
537 * sizeof (struct w32_metric_cache *)));
8f112d52
JR
538 w32_font->n_cache_blocks = block + 1;
539 }
540
541 if (!w32_font->cached_metrics[block])
542 {
543 w32_font->cached_metrics[block]
23f86fce 544 = xzalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
8f112d52
JR
545 }
546
547 char_metric = w32_font->cached_metrics[block] + pos_in_block;
548
549 if (char_metric->status == W32METRIC_NO_ATTEMPT)
550 {
551 if (dc == NULL)
552 {
553 /* TODO: Frames can come and go, and their fonts
554 outlive them. So we can't cache the frame in the
555 font structure. Use selected_frame until the API
556 is updated to pass in a frame. */
557 f = XFRAME (selected_frame);
9c623c85 558
2a36efcf 559 dc = get_frame_dc (f);
1b5defe6 560 old_font = SelectObject (dc, w32_font->hfont);
8f112d52
JR
561 }
562 compute_metrics (dc, w32_font, *(code + i), char_metric);
563 }
78573c57 564
8f112d52
JR
565 if (char_metric->status == W32METRIC_SUCCESS)
566 {
567 metrics->lbearing = min (metrics->lbearing,
568 metrics->width + char_metric->lbearing);
569 metrics->rbearing = max (metrics->rbearing,
570 metrics->width + char_metric->rbearing);
571 metrics->width += char_metric->width;
572 }
573 else
574 /* If we couldn't get metrics for a char,
575 use alternative method. */
576 break;
577 }
78573c57
JR
578 /* If we got through everything, return. */
579 if (i == nglyphs)
580 {
2a36efcf
JR
581 if (dc != NULL)
582 {
583 /* Restore state and release DC. */
584 SelectObject (dc, old_font);
585 release_frame_dc (f, dc);
586 }
78573c57
JR
587
588 return metrics->width;
589 }
f7a84cb4 590 }
78573c57 591
56df6710
JR
592 /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
593 fallback on other methods that will at least give some of the metric
594 information. */
b003e5ff 595
f31cf550
JR
596 /* Make array big enough to hold surrogates. */
597 wcode = alloca (nglyphs * sizeof (WORD) * 2);
598 for (i = 0; i < nglyphs; i++)
599 {
600 if (code[i] < 0x10000)
601 wcode[i] = code[i];
602 else
603 {
604 DWORD surrogate = code[i] - 0x10000;
605
606 /* High surrogate: U+D800 - U+DBFF. */
607 wcode[i++] = 0xD800 + ((surrogate >> 10) & 0x03FF);
608 /* Low surrogate: U+DC00 - U+DFFF. */
609 wcode[i] = 0xDC00 + (surrogate & 0x03FF);
610 /* An extra glyph. wcode is already double the size of code to
611 cope with this. */
612 nglyphs++;
613 }
614 }
615
2a36efcf
JR
616 if (dc == NULL)
617 {
8f112d52
JR
618 /* TODO: Frames can come and go, and their fonts outlive
619 them. So we can't cache the frame in the font structure. Use
620 selected_frame until the API is updated to pass in a
621 frame. */
622 f = XFRAME (selected_frame);
623
2a36efcf 624 dc = get_frame_dc (f);
1b5defe6 625 old_font = SelectObject (dc, w32_font->hfont);
2a36efcf
JR
626 }
627
d205d43b 628 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
f7a84cb4 629 {
d205d43b 630 total_width = size.cx;
f7a84cb4
JR
631 }
632
fe7a3057 633 /* On 95/98/ME, only some Unicode functions are available, so fallback
56df6710 634 on doing a dummy draw to find the total width. */
d205d43b 635 if (!total_width)
f7a84cb4
JR
636 {
637 RECT rect;
5f18d119 638 rect.top = 0; rect.bottom = font->height; rect.left = 0; rect.right = 1;
f7a84cb4
JR
639 DrawTextW (dc, wcode, nglyphs, &rect,
640 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
641 total_width = rect.right;
642 }
d205d43b 643
56df6710 644 /* Give our best estimate of the metrics, based on what we know. */
78573c57
JR
645 if (metrics)
646 {
79ca7db1 647 metrics->width = total_width - w32_font->metrics.tmOverhang;
78573c57 648 metrics->lbearing = 0;
79ca7db1 649 metrics->rbearing = total_width;
78573c57
JR
650 }
651
f7a84cb4
JR
652 /* Restore state and release DC. */
653 SelectObject (dc, old_font);
46fd1ded 654 release_frame_dc (f, dc);
f7a84cb4
JR
655
656 return total_width;
657}
658
659/* w32 implementation of draw for font backend.
660 Optional.
661 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
a864ef14
PE
662 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND,
663 fill the background in advance. It is assured that WITH_BACKGROUND
664 is false when (FROM > 0 || TO < S->nchars).
a74ddbda
JR
665
666 TODO: Currently this assumes that the colors and fonts are already
667 set in the DC. This seems to be true now, but maybe only due to
668 the old font code setting it up. It may be safer to resolve faces
669 and fonts in here and set them explicitly
670*/
671
46fd1ded 672int
b56ceb92 673w32font_draw (struct glyph_string *s, int from, int to,
a864ef14 674 int x, int y, bool with_background)
f7a84cb4 675{
34fd2d28 676 UINT options;
f70eb806 677 HRGN orig_clip = NULL;
9c88f339 678 int len = to - from;
5f18d119 679 struct w32font_info *w32font = (struct w32font_info *) s->font;
34fd2d28
JR
680
681 options = w32font->glyph_idx;
5c2c9c79 682
5c2c9c79
JR
683 if (s->num_clips > 0)
684 {
685 HRGN new_clip = CreateRectRgnIndirect (s->clip);
686
f70eb806
JR
687 /* Save clip region for later restoration. */
688 orig_clip = CreateRectRgn (0, 0, 0, 0);
ed3751c8 689 if (!GetClipRgn (s->hdc, orig_clip))
f70eb806
JR
690 {
691 DeleteObject (orig_clip);
692 orig_clip = NULL;
693 }
694
5c2c9c79
JR
695 if (s->num_clips > 1)
696 {
697 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
698
699 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
700 DeleteObject (clip2);
701 }
702
703 SelectClipRgn (s->hdc, new_clip);
704 DeleteObject (new_clip);
705 }
f7a84cb4 706
fb3b8017
JR
707 /* Using OPAQUE background mode can clear more background than expected
708 when Cleartype is used. Draw the background manually to avoid this. */
709 SetBkMode (s->hdc, TRANSPARENT);
f7a84cb4
JR
710 if (with_background)
711 {
1065a502
JR
712 HBRUSH brush;
713 RECT rect;
5f18d119 714 struct font *font = s->font;
1065a502
JR
715
716 brush = CreateSolidBrush (s->gc->background);
717 rect.left = x;
fb3b8017 718 rect.top = y - font->ascent;
1065a502 719 rect.right = x + s->width;
fb3b8017 720 rect.bottom = y + font->descent;
1065a502 721 FillRect (s->hdc, &rect, brush);
f2b25c0e 722 DeleteObject (brush);
f7a84cb4 723 }
040fe918 724
de63f07f
KH
725 if (s->padding_p)
726 {
9c88f339 727 int i;
de63f07f
KH
728
729 for (i = 0; i < len; i++)
730 ExtTextOutW (s->hdc, x + i, y, options, NULL,
8501c48b 731 s->char2b + from + i, 1, NULL);
de63f07f
KH
732 }
733 else
9c88f339 734 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, len, NULL);
5c2c9c79
JR
735
736 /* Restore clip region. */
737 if (s->num_clips > 0)
f70eb806
JR
738 SelectClipRgn (s->hdc, orig_clip);
739
740 if (orig_clip)
741 DeleteObject (orig_clip);
9c88f339
JB
742
743 return len;
f7a84cb4
JR
744}
745
746/* w32 implementation of free_entity for font backend.
747 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
748 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
20399669
JR
749static void
750w32font_free_entity (Lisp_Object entity);
f7a84cb4
JR
751 */
752
753/* w32 implementation of prepare_face for font backend.
754 Optional (if FACE->extra is not used).
755 Prepare FACE for displaying characters by FONT on frame F by
756 storing some data in FACE->extra. If successful, return 0.
757 Otherwise, return -1.
20399669 758static int
a10c8269 759w32font_prepare_face (struct frame *f, struct face *face);
f7a84cb4
JR
760 */
761/* w32 implementation of done_face for font backend.
762 Optional.
763 Done FACE for displaying characters by FACE->font on frame F.
20399669 764static void
a10c8269 765w32font_done_face (struct frame *f, struct face *face); */
f7a84cb4
JR
766
767/* w32 implementation of get_bitmap for font backend.
768 Optional.
769 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
f2b25c0e 770 intended that this method is called from the other font-driver
f7a84cb4 771 for actual drawing.
20399669
JR
772static int
773w32font_get_bitmap (struct font *font, unsigned code,
774 struct font_bitmap *bitmap, int bits_per_pixel);
f7a84cb4
JR
775 */
776/* w32 implementation of free_bitmap for font backend.
777 Optional.
778 Free bitmap data in BITMAP.
20399669
JR
779static void
780w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
f7a84cb4
JR
781 */
782/* w32 implementation of get_outline for font backend.
783 Optional.
784 Return an outline data for glyph-code CODE of FONT. The format
785 of the outline data depends on the font-driver.
20399669
JR
786static void *
787w32font_get_outline (struct font *font, unsigned code);
f7a84cb4
JR
788 */
789/* w32 implementation of free_outline for font backend.
790 Optional.
791 Free OUTLINE (that is obtained by the above method).
20399669
JR
792static void
793w32font_free_outline (struct font *font, void *outline);
f7a84cb4
JR
794 */
795/* w32 implementation of anchor_point for font backend.
796 Optional.
797 Get coordinates of the INDEXth anchor point of the glyph whose
798 code is CODE. Store the coordinates in *X and *Y. Return 0 if
bbd240ce 799 the operations was successful. Otherwise return -1.
20399669
JR
800static int
801w32font_anchor_point (struct font *font, unsigned code,
f7a84cb4
JR
802 int index, int *x, int *y);
803 */
804/* w32 implementation of otf_capability for font backend.
805 Optional.
806 Return a list describing which scripts/languages FONT
807 supports by which GSUB/GPOS features of OpenType tables.
20399669
JR
808static Lisp_Object
809w32font_otf_capability (struct font *font);
f7a84cb4
JR
810 */
811/* w32 implementation of otf_drive for font backend.
812 Optional.
813 Apply FONT's OTF-FEATURES to the glyph string.
814
815 FEATURES specifies which OTF features to apply in this format:
816 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
817 See the documentation of `font-drive-otf' for the detail.
818
819 This method applies the specified features to the codes in the
820 elements of GSTRING-IN (between FROMth and TOth). The output
821 codes are stored in GSTRING-OUT at the IDXth element and the
822 following elements.
823
824 Return the number of output codes. If none of the features are
825 applicable to the input data, return 0. If GSTRING-OUT is too
826 short, return -1.
20399669
JR
827static int
828w32font_otf_drive (struct font *font, Lisp_Object features,
829 Lisp_Object gstring_in, int from, int to,
830 Lisp_Object gstring_out, int idx,
a864ef14 831 bool alternate_subst);
f7a84cb4
JR
832 */
833
46fd1ded
JR
834/* Internal implementation of w32font_list.
835 Additional parameter opentype_only restricts the returned fonts to
836 opentype fonts, which can be used with the Uniscribe backend. */
837Lisp_Object
fdb396e2 838w32font_list_internal (struct frame *f, Lisp_Object font_spec, int opentype_only)
46fd1ded
JR
839{
840 struct font_callback_data match_data;
841 HDC dc;
46fd1ded
JR
842
843 match_data.orig_font_spec = font_spec;
844 match_data.list = Qnil;
fdb396e2 845 XSETFRAME (match_data.frame, f);
46fd1ded 846
72af86bd 847 memset (&match_data.pattern, 0, sizeof (LOGFONT));
46fd1ded
JR
848 fill_in_logfont (f, &match_data.pattern, font_spec);
849
19ae3e61
JR
850 /* If the charset is unrecognized, then we won't find a font, so don't
851 waste time looking for one. */
852 if (match_data.pattern.lfCharSet == DEFAULT_CHARSET)
853 {
854 Lisp_Object spec_charset = AREF (font_spec, FONT_REGISTRY_INDEX);
855 if (!NILP (spec_charset)
856 && !EQ (spec_charset, Qiso10646_1)
857 && !EQ (spec_charset, Qunicode_bmp)
858 && !EQ (spec_charset, Qunicode_sip)
859 && !EQ (spec_charset, Qunknown))
860 return Qnil;
861 }
862
46fd1ded
JR
863 match_data.opentype_only = opentype_only;
864 if (opentype_only)
865 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
866
867 if (match_data.pattern.lfFaceName[0] == '\0')
868 {
869 /* EnumFontFamiliesEx does not take other fields into account if
870 font name is blank, so need to use two passes. */
871 list_all_matching_fonts (&match_data);
872 }
873 else
874 {
875 dc = get_frame_dc (f);
876
877 EnumFontFamiliesEx (dc, &match_data.pattern,
878 (FONTENUMPROC) add_font_entity_to_list,
879 (LPARAM) &match_data, 0);
880 release_frame_dc (f, dc);
881 }
882
19ae3e61 883 return match_data.list;
46fd1ded
JR
884}
885
886/* Internal implementation of w32font_match.
887 Additional parameter opentype_only restricts the returned fonts to
888 opentype fonts, which can be used with the Uniscribe backend. */
889Lisp_Object
fdb396e2 890w32font_match_internal (struct frame *f, Lisp_Object font_spec, int opentype_only)
46fd1ded
JR
891{
892 struct font_callback_data match_data;
893 HDC dc;
46fd1ded
JR
894
895 match_data.orig_font_spec = font_spec;
fdb396e2 896 XSETFRAME (match_data.frame, f);
46fd1ded
JR
897 match_data.list = Qnil;
898
72af86bd 899 memset (&match_data.pattern, 0, sizeof (LOGFONT));
46fd1ded
JR
900 fill_in_logfont (f, &match_data.pattern, font_spec);
901
902 match_data.opentype_only = opentype_only;
903 if (opentype_only)
904 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
905
906 dc = get_frame_dc (f);
907
908 EnumFontFamiliesEx (dc, &match_data.pattern,
909 (FONTENUMPROC) add_one_font_entity_to_list,
910 (LPARAM) &match_data, 0);
911 release_frame_dc (f, dc);
912
913 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
914}
915
f0121ad2 916int
a10c8269 917w32font_open_internal (struct frame *f, Lisp_Object font_entity,
b56ceb92 918 int pixel_size, Lisp_Object font_object)
f0121ad2 919{
9c88f339 920 int len, size;
f0121ad2
JR
921 LOGFONT logfont;
922 HDC dc;
923 HFONT hfont, old_font;
924 Lisp_Object val, extra;
5f18d119
KH
925 struct w32font_info *w32_font;
926 struct font * font;
bd187c49 927 OUTLINETEXTMETRICW* metrics = NULL;
5f18d119
KH
928
929 w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
930 font = (struct font *) w32_font;
f0121ad2 931
f0121ad2
JR
932 if (!font)
933 return 0;
934
72af86bd 935 memset (&logfont, 0, sizeof (logfont));
f0121ad2
JR
936 fill_in_logfont (f, &logfont, font_entity);
937
1519d52e
JR
938 /* Prefer truetype fonts, to avoid known problems with type1 fonts, and
939 limitations in bitmap fonts. */
940 val = AREF (font_entity, FONT_FOUNDRY_INDEX);
941 if (!EQ (val, Qraster))
942 logfont.lfOutPrecision = OUT_TT_PRECIS;
943
f0121ad2
JR
944 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
945 if (!size)
946 size = pixel_size;
947
948 logfont.lfHeight = -size;
949 hfont = CreateFontIndirect (&logfont);
950
951 if (hfont == NULL)
952 return 0;
953
f0121ad2
JR
954 /* Get the metrics for this font. */
955 dc = get_frame_dc (f);
956 old_font = SelectObject (dc, hfont);
957
5f18d119 958 /* Try getting the outline metrics (only works for truetype fonts). */
db4f02f2 959 len = get_outline_metrics_w (dc, 0, NULL);
5f18d119
KH
960 if (len)
961 {
bd187c49 962 metrics = (OUTLINETEXTMETRICW *) alloca (len);
db4f02f2 963 if (get_outline_metrics_w (dc, len, metrics))
72af86bd
AS
964 memcpy (&w32_font->metrics, &metrics->otmTextMetrics,
965 sizeof (TEXTMETRICW));
5f18d119
KH
966 else
967 metrics = NULL;
968 }
f0121ad2 969
943f2093 970 if (!metrics)
db4f02f2 971 get_text_metrics_w (dc, &w32_font->metrics);
b003e5ff 972
8f112d52
JR
973 w32_font->cached_metrics = NULL;
974 w32_font->n_cache_blocks = 0;
54efdcd1 975
f0121ad2
JR
976 SelectObject (dc, old_font);
977 release_frame_dc (f, dc);
34fd2d28 978
1b5defe6 979 w32_font->hfont = hfont;
f0121ad2 980
5ace1ec1
JR
981 {
982 char *name;
983
984 /* We don't know how much space we need for the full name, so start with
985 96 bytes and go up in steps of 32. */
986 len = 96;
c35f9821 987 name = alloca (len);
67997c79
JR
988 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
989 name, len) < 0)
5ace1ec1 990 {
c35f9821
JR
991 len += 32;
992 name = alloca (len);
5ace1ec1
JR
993 }
994 if (name)
5f18d119 995 font->props[FONT_FULLNAME_INDEX]
e6df5336 996 = DECODE_SYSTEM (build_string (name));
5ace1ec1 997 else
e6df5336
JR
998 font->props[FONT_FULLNAME_INDEX]
999 = DECODE_SYSTEM (build_string (logfont.lfFaceName));
5ace1ec1 1000 }
5f18d119
KH
1001
1002 font->max_width = w32_font->metrics.tmMaxCharWidth;
b003e5ff 1003 /* Parts of Emacs display assume that height = ascent + descent...
9196133b 1004 so height is defined later, after ascent and descent.
5f18d119 1005 font->height = w32_font->metrics.tmHeight
f0121ad2 1006 + w32_font->metrics.tmExternalLeading;
9196133b
JR
1007 */
1008
5f18d119
KH
1009 font->space_width = font->average_width = w32_font->metrics.tmAveCharWidth;
1010
1011 font->vertical_centering = 0;
5f18d119
KH
1012 font->baseline_offset = 0;
1013 font->relative_compose = 0;
1014 font->default_ascent = w32_font->metrics.tmAscent;
f0121ad2
JR
1015 font->pixel_size = size;
1016 font->driver = &w32font_driver;
34fd2d28
JR
1017 /* Use format cached during list, as the information we have access to
1018 here is incomplete. */
1019 extra = AREF (font_entity, FONT_EXTRA_INDEX);
1020 if (CONSP (extra))
1021 {
1022 val = assq_no_quit (QCformat, extra);
1023 if (CONSP (val))
5f18d119 1024 font->props[FONT_FORMAT_INDEX] = XCDR (val);
34fd2d28 1025 else
5f18d119 1026 font->props[FONT_FORMAT_INDEX] = Qunknown;
34fd2d28
JR
1027 }
1028 else
5f18d119 1029 font->props[FONT_FORMAT_INDEX] = Qunknown;
34fd2d28 1030
5f18d119 1031 font->props[FONT_FILE_INDEX] = Qnil;
f0121ad2
JR
1032 font->encoding_charset = -1;
1033 font->repertory_charset = -1;
04b65d2b 1034 /* TODO: do we really want the minimum width here, which could be negative? */
5f18d119 1035 font->min_width = font->space_width;
f0121ad2
JR
1036 font->ascent = w32_font->metrics.tmAscent;
1037 font->descent = w32_font->metrics.tmDescent;
9196133b 1038 font->height = font->ascent + font->descent;
5f18d119
KH
1039
1040 if (metrics)
1041 {
1042 font->underline_thickness = metrics->otmsUnderscoreSize;
1043 font->underline_position = -metrics->otmsUnderscorePosition;
1044 }
1045 else
1046 {
1047 font->underline_thickness = 0;
1048 font->underline_position = -1;
1049 }
f0121ad2 1050
5f18d119
KH
1051 /* For temporary compatibility with legacy code that expects the
1052 name to be usable in x-list-fonts. Eventually we expect to change
1053 x-list-fonts and other places that use fonts so that this can be
1054 an fcname or similar. */
a30e1957 1055 font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
04b65d2b 1056
f0121ad2
JR
1057 return 1;
1058}
1059
f7a84cb4
JR
1060/* Callback function for EnumFontFamiliesEx.
1061 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
20399669 1062static int CALLBACK
b56ceb92
JB
1063add_font_name_to_list (ENUMLOGFONTEX *logical_font,
1064 NEWTEXTMETRICEX *physical_font,
1065 DWORD font_type, LPARAM list_object)
f7a84cb4
JR
1066{
1067 Lisp_Object* list = (Lisp_Object *) list_object;
20a2b756
JR
1068 Lisp_Object family;
1069
1070 /* Skip vertical fonts (intended only for printing) */
1071 if (logical_font->elfLogFont.lfFaceName[0] == '@')
1072 return 1;
1073
e6df5336 1074 family = intern_font_name (logical_font->elfLogFont.lfFaceName);
f7a84cb4
JR
1075 if (! memq_no_quit (family, *list))
1076 *list = Fcons (family, *list);
1077
1078 return 1;
1079}
1080
f57e2426
J
1081static int w32_decode_weight (int);
1082static int w32_encode_weight (int);
5f18d119 1083
f7a84cb4 1084/* Convert an enumerated Windows font to an Emacs font entity. */
20399669 1085static Lisp_Object
b56ceb92
JB
1086w32_enumfont_pattern_entity (Lisp_Object frame,
1087 ENUMLOGFONTEX *logical_font,
1088 NEWTEXTMETRICEX *physical_font,
1089 DWORD font_type,
1090 LOGFONT *requested_font,
1091 Lisp_Object backend)
f7a84cb4
JR
1092{
1093 Lisp_Object entity, tem;
1094 LOGFONT *lf = (LOGFONT*) logical_font;
1095 BYTE generic_type;
56df6710 1096 DWORD full_type = physical_font->ntmTm.ntmFlags;
f7a84cb4 1097
5f18d119 1098 entity = font_make_entity ();
f7a84cb4 1099
34fd2d28 1100 ASET (entity, FONT_TYPE_INDEX, backend);
c960bff8 1101 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
f7a84cb4
JR
1102 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
1103
1104 /* Foundry is difficult to get in readable form on Windows.
d205d43b 1105 But Emacs crashes if it is not set, so set it to something more
9c623c85 1106 generic. These values make xlfds compatible with Emacs 22. */
d205d43b
JR
1107 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
1108 tem = Qraster;
1109 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
1110 tem = Qoutline;
1111 else
1112 tem = Qunknown;
1113
1114 ASET (entity, FONT_FOUNDRY_INDEX, tem);
1115
1116 /* Save the generic family in the extra info, as it is likely to be
1117 useful to users looking for a close match. */
f7a84cb4
JR
1118 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
1119 if (generic_type == FF_DECORATIVE)
1120 tem = Qdecorative;
1121 else if (generic_type == FF_MODERN)
9e1a2995 1122 tem = Qmono;
f7a84cb4 1123 else if (generic_type == FF_ROMAN)
d205d43b 1124 tem = Qserif;
f7a84cb4
JR
1125 else if (generic_type == FF_SCRIPT)
1126 tem = Qscript;
1127 else if (generic_type == FF_SWISS)
9e1a2995 1128 tem = Qsans;
f7a84cb4 1129 else
5f18d119 1130 tem = Qnil;
9e1a2995
JR
1131
1132 ASET (entity, FONT_ADSTYLE_INDEX, tem);
f7a84cb4 1133
d205d43b 1134 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
5f18d119 1135 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_PROPORTIONAL));
d205d43b 1136 else
ed18e612 1137 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_CHARCELL));
f7a84cb4 1138
91583281
JR
1139 if (requested_font->lfQuality != DEFAULT_QUALITY)
1140 {
1141 font_put_extra (entity, QCantialias,
1142 lispy_antialias_type (requested_font->lfQuality));
1143 }
f7a84cb4 1144 ASET (entity, FONT_FAMILY_INDEX,
e6df5336 1145 intern_font_name (lf->lfFaceName));
f7a84cb4 1146
5f18d119
KH
1147 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX,
1148 make_number (w32_decode_weight (lf->lfWeight)));
1149 FONT_SET_STYLE (entity, FONT_SLANT_INDEX,
1150 make_number (lf->lfItalic ? 200 : 100));
d205d43b
JR
1151 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
1152 to get it. */
5f18d119 1153 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (100));
f7a84cb4 1154
040fe918 1155 if (font_type & RASTER_FONTTYPE)
597634cb
JR
1156 ASET (entity, FONT_SIZE_INDEX,
1157 make_number (physical_font->ntmTm.tmHeight
1158 + physical_font->ntmTm.tmExternalLeading));
040fe918
JR
1159 else
1160 ASET (entity, FONT_SIZE_INDEX, make_number (0));
f7a84cb4 1161
fe7a3057 1162 /* Cache Unicode codepoints covered by this font, as there is no other way
f7a84cb4 1163 of getting this information easily. */
040fe918 1164 if (font_type & TRUETYPE_FONTTYPE)
f7a84cb4 1165 {
aee986fd
JR
1166 tem = font_supported_scripts (&physical_font->ntmFontSig);
1167 if (!NILP (tem))
1168 font_put_extra (entity, QCscript, tem);
f7a84cb4 1169 }
d205d43b 1170
34fd2d28
JR
1171 /* This information is not fully available when opening fonts, so
1172 save it here. Only Windows 2000 and later return information
1173 about opentype and type1 fonts, so need a fallback for detecting
1174 truetype so that this information is not any worse than we could
1175 have obtained later. */
56df6710
JR
1176 if (EQ (backend, Quniscribe) && (full_type & NTMFLAGS_OPENTYPE))
1177 tem = intern ("opentype");
1178 else if (font_type & TRUETYPE_FONTTYPE)
34fd2d28 1179 tem = intern ("truetype");
34fd2d28
JR
1180 else if (full_type & NTM_PS_OPENTYPE)
1181 tem = intern ("postscript");
56df6710
JR
1182 else if (full_type & NTM_TYPE1)
1183 tem = intern ("type1");
34fd2d28
JR
1184 else if (font_type & RASTER_FONTTYPE)
1185 tem = intern ("w32bitmap");
1186 else
1187 tem = intern ("w32vector");
1188
1189 font_put_extra (entity, QCformat, tem);
1190
f7a84cb4
JR
1191 return entity;
1192}
1193
d205d43b
JR
1194
1195/* Convert generic families to the family portion of lfPitchAndFamily. */
24f981c9 1196static BYTE
d205d43b
JR
1197w32_generic_family (Lisp_Object name)
1198{
1199 /* Generic families. */
1200 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1201 return FF_MODERN;
9e1a2995 1202 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
d205d43b
JR
1203 return FF_SWISS;
1204 else if (EQ (name, Qserif))
1205 return FF_ROMAN;
1206 else if (EQ (name, Qdecorative))
1207 return FF_DECORATIVE;
1208 else if (EQ (name, Qscript))
1209 return FF_SCRIPT;
1210 else
1211 return FF_DONTCARE;
1212}
1213
1214static int
b56ceb92 1215logfonts_match (LOGFONT *font, LOGFONT *pattern)
d205d43b
JR
1216{
1217 /* Only check height for raster fonts. */
1218 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1219 && font->lfHeight != pattern->lfHeight)
1220 return 0;
1221
1222 /* Have some flexibility with weights. */
1223 if (pattern->lfWeight
1224 && ((font->lfWeight < (pattern->lfWeight - 150))
1225 || font->lfWeight > (pattern->lfWeight + 150)))
1226 return 0;
1227
1228 /* Charset and face should be OK. Italic has to be checked
1229 against the original spec, in case we don't have any preference. */
1230 return 1;
1231}
1232
4c100a01
JR
1233/* Codepage Bitfields in FONTSIGNATURE struct. */
1234#define CSB_JAPANESE (1 << 17)
1235#define CSB_KOREAN ((1 << 19) | (1 << 21))
1236#define CSB_CHINESE ((1 << 18) | (1 << 20))
1237
d205d43b 1238static int
b56ceb92
JB
1239font_matches_spec (DWORD type, NEWTEXTMETRICEX *font,
1240 Lisp_Object spec, Lisp_Object backend,
1241 LOGFONT *logfont)
d205d43b
JR
1242{
1243 Lisp_Object extra, val;
1244
1245 /* Check italic. Can't check logfonts, since it is a boolean field,
1246 so there is no difference between "non-italic" and "don't care". */
5f18d119
KH
1247 {
1248 int slant = FONT_SLANT_NUMERIC (spec);
1249
1250 if (slant >= 0
1251 && ((slant > 150 && !font->ntmTm.tmItalic)
1252 || (slant <= 150 && font->ntmTm.tmItalic)))
1253 return 0;
1254 }
d205d43b 1255
4f2a2ee2
JR
1256 /* Check adstyle against generic family. */
1257 val = AREF (spec, FONT_ADSTYLE_INDEX);
1258 if (!NILP (val))
1259 {
1260 BYTE family = w32_generic_family (val);
1261 if (family != FF_DONTCARE
1262 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1263 return 0;
1264 }
1265
5f18d119
KH
1266 /* Check spacing */
1267 val = AREF (spec, FONT_SPACING_INDEX);
1268 if (INTEGERP (val))
1269 {
1270 int spacing = XINT (val);
1271 int proportional = (spacing < FONT_SPACING_MONO);
1272
1273 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1274 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1275 return 0;
1276 }
1277
d205d43b
JR
1278 /* Check extra parameters. */
1279 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1280 CONSP (extra); extra = XCDR (extra))
1281 {
1282 Lisp_Object extra_entry;
1283 extra_entry = XCAR (extra);
1284 if (CONSP (extra_entry))
1285 {
1286 Lisp_Object key = XCAR (extra_entry);
d205d43b 1287
5f18d119
KH
1288 val = XCDR (extra_entry);
1289 if (EQ (key, QCscript) && SYMBOLP (val))
d205d43b
JR
1290 {
1291 /* Only truetype fonts will have information about what
1292 scripts they support. This probably means the user
7877f373
JB
1293 will have to force Emacs to use raster, PostScript
1294 or ATM fonts for non-ASCII text. */
d205d43b
JR
1295 if (type & TRUETYPE_FONTTYPE)
1296 {
1297 Lisp_Object support
1298 = font_supported_scripts (&font->ntmFontSig);
1299 if (! memq_no_quit (val, support))
1300 return 0;
22610910
JR
1301
1302 /* Avoid using non-Japanese fonts for Japanese, even
1303 if they claim they are capable, due to known
1304 breakage in Vista and Windows 7 fonts
1305 (bug#6029). */
1306 if (EQ (val, Qkana)
1307 && (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET
1308 || !(font->ntmFontSig.fsCsb[0] & CSB_JAPANESE)))
1309 return 0;
d205d43b
JR
1310 }
1311 else
1312 {
1313 /* Return specific matches, but play it safe. Fonts
1314 that cover more than their charset would suggest
1315 are likely to be truetype or opentype fonts,
1316 covered above. */
1317 if (EQ (val, Qlatin))
1318 {
1319 /* Although every charset but symbol, thai and
1320 arabic contains the basic ASCII set of latin
1321 characters, Emacs expects much more. */
1322 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1323 return 0;
1324 }
1325 else if (EQ (val, Qsymbol))
1326 {
1327 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1328 return 0;
1329 }
1330 else if (EQ (val, Qcyrillic))
1331 {
1332 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1333 return 0;
1334 }
1335 else if (EQ (val, Qgreek))
1336 {
1337 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1338 return 0;
1339 }
1340 else if (EQ (val, Qarabic))
1341 {
1342 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1343 return 0;
1344 }
1345 else if (EQ (val, Qhebrew))
1346 {
1347 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1348 return 0;
1349 }
1350 else if (EQ (val, Qthai))
1351 {
1352 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1353 return 0;
1354 }
1355 else if (EQ (val, Qkana))
1356 {
1357 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1358 return 0;
1359 }
1360 else if (EQ (val, Qbopomofo))
1361 {
1362 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1363 return 0;
1364 }
1365 else if (EQ (val, Qhangul))
1366 {
1367 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1368 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1369 return 0;
1370 }
1371 else if (EQ (val, Qhan))
1372 {
1373 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1374 && font->ntmTm.tmCharSet != GB2312_CHARSET
1375 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1376 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1377 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1378 return 0;
1379 }
1380 else
56df6710
JR
1381 /* Other scripts unlikely to be handled by non-truetype
1382 fonts. */
d205d43b
JR
1383 return 0;
1384 }
1385 }
5f18d119 1386 else if (EQ (key, QClang) && SYMBOLP (val))
8f112d52 1387 {
4c100a01 1388 /* Just handle the CJK languages here, as the lang
8f112d52
JR
1389 parameter is used to select a font with appropriate
1390 glyphs in the cjk unified ideographs block. Other fonts
1391 support for a language can be solely determined by
1392 its character coverage. */
1393 if (EQ (val, Qja))
1394 {
4c100a01 1395 if (!(font->ntmFontSig.fsCsb[0] & CSB_JAPANESE))
8f112d52
JR
1396 return 0;
1397 }
1398 else if (EQ (val, Qko))
1399 {
4c100a01 1400 if (!(font->ntmFontSig.fsCsb[0] & CSB_KOREAN))
8f112d52
JR
1401 return 0;
1402 }
1403 else if (EQ (val, Qzh))
1404 {
4c100a01
JR
1405 if (!(font->ntmFontSig.fsCsb[0] & CSB_CHINESE))
1406 return 0;
8f112d52
JR
1407 }
1408 else
4c100a01
JR
1409 /* Any other language, we don't recognize it. Only the above
1410 currently appear in fontset.el, so it isn't worth
1411 creating a mapping table of codepages/scripts to languages
1412 or opening the font to see if there are any language tags
b46a6a83 1413 in it that the Windows API does not expose. Fontset
8f112d52
JR
1414 spec should have a fallback, as some backends do
1415 not recognize language at all. */
1416 return 0;
1417 }
56df6710
JR
1418 else if (EQ (key, QCotf) && CONSP (val))
1419 {
1420 /* OTF features only supported by the uniscribe backend. */
1421 if (EQ (backend, Quniscribe))
1422 {
1423 if (!uniscribe_check_otf (logfont, val))
1424 return 0;
1425 }
1426 else
1427 return 0;
1428 }
d205d43b
JR
1429 }
1430 }
1431 return 1;
1432}
1433
c960bff8 1434static int
b56ceb92 1435w32font_coverage_ok (FONTSIGNATURE * coverage, BYTE charset)
c960bff8
JR
1436{
1437 DWORD subrange1 = coverage->fsUsb[1];
1438
1439#define SUBRANGE1_HAN_MASK 0x08000000
1440#define SUBRANGE1_HANGEUL_MASK 0x01000000
1441#define SUBRANGE1_JAPANESE_MASK (0x00060000 | SUBRANGE1_HAN_MASK)
1442
1443 if (charset == GB2312_CHARSET || charset == CHINESEBIG5_CHARSET)
1444 {
1445 return (subrange1 & SUBRANGE1_HAN_MASK) == SUBRANGE1_HAN_MASK;
1446 }
1447 else if (charset == SHIFTJIS_CHARSET)
1448 {
1449 return (subrange1 & SUBRANGE1_JAPANESE_MASK) == SUBRANGE1_JAPANESE_MASK;
1450 }
1451 else if (charset == HANGEUL_CHARSET)
1452 {
1453 return (subrange1 & SUBRANGE1_HANGEUL_MASK) == SUBRANGE1_HANGEUL_MASK;
1454 }
1455
1456 return 1;
1457}
1458
0fda9b75
DC
1459#ifndef WINDOWSNT
1460#define _strlwr strlwr
1461#endif /* !WINDOWSNT */
b0857706
JR
1462
1463static int
b56ceb92 1464check_face_name (LOGFONT *font, char *full_name)
b0857706
JR
1465{
1466 char full_iname[LF_FULLFACESIZE+1];
1467
1468 /* Just check for names known to cause problems, since the full name
1469 can contain expanded abbreviations, prefixed foundry, postfixed
1470 style, the latter of which sometimes differs from the style indicated
1471 in the shorter name (eg Lt becomes Light or even Extra Light) */
1472
1473 /* Helvetica is mapped to Arial in Windows, but if a Type-1 Helvetica is
1474 installed, we run into problems with the Uniscribe backend which tries
1475 to avoid non-truetype fonts, and ends up mixing the Type-1 Helvetica
7877f373 1476 with Arial's characteristics, since that attempt to use TrueType works
b0857706 1477 some places, but not others. */
c285743c 1478 if (!xstrcasecmp (font->lfFaceName, "helvetica"))
b0857706
JR
1479 {
1480 strncpy (full_iname, full_name, LF_FULLFACESIZE);
1481 full_iname[LF_FULLFACESIZE] = 0;
1482 _strlwr (full_iname);
c285743c 1483 return strstr ("helvetica", full_iname) != NULL;
b0857706 1484 }
631ea4fb
JR
1485 /* Same for Helv. */
1486 if (!xstrcasecmp (font->lfFaceName, "helv"))
1487 {
1488 strncpy (full_iname, full_name, LF_FULLFACESIZE);
1489 full_iname[LF_FULLFACESIZE] = 0;
1490 _strlwr (full_iname);
1491 return strstr ("helv", full_iname) != NULL;
1492 }
c285743c
JR
1493
1494 /* Since Times is mapped to Times New Roman, a substring
1495 match is not sufficient to filter out the bogus match. */
1496 else if (!xstrcasecmp (font->lfFaceName, "times"))
1497 return xstrcasecmp (full_name, "times") == 0;
b0857706
JR
1498
1499 return 1;
1500}
1501
1502
f7a84cb4 1503/* Callback function for EnumFontFamiliesEx.
91af3942 1504 * Checks if a font matches everything we are trying to check against,
d205d43b
JR
1505 * and if so, adds it to a list. Both the data we are checking against
1506 * and the list to which the fonts are added are passed in via the
1507 * lparam argument, in the form of a font_callback_data struct. */
20399669 1508static int CALLBACK
b56ceb92
JB
1509add_font_entity_to_list (ENUMLOGFONTEX *logical_font,
1510 NEWTEXTMETRICEX *physical_font,
1511 DWORD font_type, LPARAM lParam)
f7a84cb4 1512{
d205d43b
JR
1513 struct font_callback_data *match_data
1514 = (struct font_callback_data *) lParam;
56df6710 1515 Lisp_Object backend = match_data->opentype_only ? Quniscribe : Qgdi;
19ae3e61
JR
1516 Lisp_Object entity;
1517
1518 int is_unicode = physical_font->ntmFontSig.fsUsb[3]
1519 || physical_font->ntmFontSig.fsUsb[2]
1520 || physical_font->ntmFontSig.fsUsb[1]
1521 || physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff;
1522
1523 /* Skip non matching fonts. */
1524
1525 /* For uniscribe backend, consider only truetype or opentype fonts
fe7a3057 1526 that have some Unicode coverage. */
19ae3e61 1527 if (match_data->opentype_only
22610910 1528 && ((!(physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
19ae3e61
JR
1529 && !(font_type & TRUETYPE_FONTTYPE))
1530 || !is_unicode))
1531 return 1;
1532
1533 /* Ensure a match. */
1534 if (!logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1535 || !font_matches_spec (font_type, physical_font,
1536 match_data->orig_font_spec, backend,
1537 &logical_font->elfLogFont)
1538 || !w32font_coverage_ok (&physical_font->ntmFontSig,
1539 match_data->pattern.lfCharSet))
1540 return 1;
f7a84cb4 1541
19ae3e61
JR
1542 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1543 We limit this to raster fonts, because the test can catch some
1544 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1545 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1546 therefore get through this test. Since full names can be prefixed
1547 by a foundry, we accept raster fonts if the font name is found
1548 anywhere within the full name. */
1549 if ((logical_font->elfLogFont.lfOutPrecision == OUT_STRING_PRECIS
c73abf54
JR
1550 && !strstr (logical_font->elfFullName,
1551 logical_font->elfLogFont.lfFaceName))
b0857706
JR
1552 /* Check for well known substitutions that mess things up in the
1553 presence of Type-1 fonts of the same name. */
631ea4fb
JR
1554 || (!check_face_name (&logical_font->elfLogFont,
1555 logical_font->elfFullName)))
19ae3e61
JR
1556 return 1;
1557
1558 /* Make a font entity for the font. */
1559 entity = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1560 physical_font, font_type,
1561 &match_data->pattern,
1562 backend);
1563
1564 if (!NILP (entity))
d205d43b 1565 {
19ae3e61
JR
1566 Lisp_Object spec_charset = AREF (match_data->orig_font_spec,
1567 FONT_REGISTRY_INDEX);
1568
fe7a3057 1569 /* iso10646-1 fonts must contain Unicode mapping tables. */
19ae3e61
JR
1570 if (EQ (spec_charset, Qiso10646_1))
1571 {
1572 if (!is_unicode)
9d32f818 1573 return 1;
19ae3e61
JR
1574 }
1575 /* unicode-bmp fonts must contain characters from the BMP. */
1576 else if (EQ (spec_charset, Qunicode_bmp))
1577 {
1578 if (!physical_font->ntmFontSig.fsUsb[3]
1579 && !(physical_font->ntmFontSig.fsUsb[2] & 0xFFFFFF9E)
1580 && !(physical_font->ntmFontSig.fsUsb[1] & 0xE81FFFFF)
1581 && !(physical_font->ntmFontSig.fsUsb[0] & 0x007F001F))
1582 return 1;
1583 }
fe7a3057 1584 /* unicode-sip fonts must contain characters in Unicode plane 2.
19ae3e61
JR
1585 so look for bit 57 (surrogates) in the Unicode subranges, plus
1586 the bits for CJK ranges that include those characters. */
1587 else if (EQ (spec_charset, Qunicode_sip))
1588 {
22610910
JR
1589 if (!(physical_font->ntmFontSig.fsUsb[1] & 0x02000000)
1590 || !(physical_font->ntmFontSig.fsUsb[1] & 0x28000000))
19ae3e61
JR
1591 return 1;
1592 }
1593
1594 /* This font matches. */
1595
1596 /* If registry was specified, ensure it is reported as the same. */
1597 if (!NILP (spec_charset))
22610910
JR
1598 {
1599 /* Avoid using non-Japanese fonts for Japanese, even if they
1600 claim they are capable, due to known breakage in Vista
1601 and Windows 7 fonts (bug#6029). */
1602 if (logical_font->elfLogFont.lfCharSet == SHIFTJIS_CHARSET
1603 && !(physical_font->ntmFontSig.fsCsb[0] & CSB_JAPANESE))
1604 return 1;
1605 else
1606 ASET (entity, FONT_REGISTRY_INDEX, spec_charset);
1607 }
19ae3e61 1608 /* Otherwise if using the uniscribe backend, report ANSI and DEFAULT
fe7a3057 1609 fonts as Unicode and skip other charsets. */
19ae3e61
JR
1610 else if (match_data->opentype_only)
1611 {
1612 if (logical_font->elfLogFont.lfCharSet == ANSI_CHARSET
1613 || logical_font->elfLogFont.lfCharSet == DEFAULT_CHARSET)
1614 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
1615 else
1616 return 1;
1617 }
1618
1619 /* Add this font to the list. */
1620 match_data->list = Fcons (entity, match_data->list);
d205d43b 1621 }
f7a84cb4
JR
1622 return 1;
1623}
1624
1625/* Callback function for EnumFontFamiliesEx.
d205d43b 1626 * Terminates the search once we have a match. */
20399669 1627static int CALLBACK
b56ceb92
JB
1628add_one_font_entity_to_list (ENUMLOGFONTEX *logical_font,
1629 NEWTEXTMETRICEX *physical_font,
1630 DWORD font_type, LPARAM lParam)
f7a84cb4 1631{
d205d43b
JR
1632 struct font_callback_data *match_data
1633 = (struct font_callback_data *) lParam;
1634 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1635
1636 /* If we have a font in the list, terminate the search. */
4624b6e3 1637 return NILP (match_data->list);
f7a84cb4
JR
1638}
1639
57016d37
JR
1640/* Old function to convert from x to w32 charset, from w32fns.c. */
1641static LONG
b56ceb92 1642x_to_w32_charset (char * lpcs)
57016d37
JR
1643{
1644 Lisp_Object this_entry, w32_charset;
1645 char *charset;
1646 int len = strlen (lpcs);
1647
1648 /* Support "*-#nnn" format for unknown charsets. */
1649 if (strncmp (lpcs, "*-#", 3) == 0)
1650 return atoi (lpcs + 3);
1651
fe7a3057 1652 /* All Windows fonts qualify as Unicode. */
57016d37
JR
1653 if (!strncmp (lpcs, "iso10646", 8))
1654 return DEFAULT_CHARSET;
1655
1656 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
1657 charset = alloca (len + 1);
1658 strcpy (charset, lpcs);
1659 lpcs = strchr (charset, '*');
1660 if (lpcs)
1661 *lpcs = '\0';
1662
1663 /* Look through w32-charset-info-alist for the character set.
1664 Format of each entry is
1665 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1666 */
1667 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
1668
1669 if (NILP (this_entry))
1670 {
1671 /* At startup, we want iso8859-1 fonts to come up properly. */
1672 if (xstrcasecmp (charset, "iso8859-1") == 0)
1673 return ANSI_CHARSET;
1674 else
1675 return DEFAULT_CHARSET;
1676 }
1677
1678 w32_charset = Fcar (Fcdr (this_entry));
1679
1680 /* Translate Lisp symbol to number. */
1681 if (EQ (w32_charset, Qw32_charset_ansi))
1682 return ANSI_CHARSET;
1683 if (EQ (w32_charset, Qw32_charset_symbol))
1684 return SYMBOL_CHARSET;
1685 if (EQ (w32_charset, Qw32_charset_shiftjis))
1686 return SHIFTJIS_CHARSET;
1687 if (EQ (w32_charset, Qw32_charset_hangeul))
1688 return HANGEUL_CHARSET;
1689 if (EQ (w32_charset, Qw32_charset_chinesebig5))
1690 return CHINESEBIG5_CHARSET;
1691 if (EQ (w32_charset, Qw32_charset_gb2312))
1692 return GB2312_CHARSET;
1693 if (EQ (w32_charset, Qw32_charset_oem))
1694 return OEM_CHARSET;
1695 if (EQ (w32_charset, Qw32_charset_johab))
1696 return JOHAB_CHARSET;
1697 if (EQ (w32_charset, Qw32_charset_easteurope))
1698 return EASTEUROPE_CHARSET;
1699 if (EQ (w32_charset, Qw32_charset_turkish))
1700 return TURKISH_CHARSET;
1701 if (EQ (w32_charset, Qw32_charset_baltic))
1702 return BALTIC_CHARSET;
1703 if (EQ (w32_charset, Qw32_charset_russian))
1704 return RUSSIAN_CHARSET;
1705 if (EQ (w32_charset, Qw32_charset_arabic))
1706 return ARABIC_CHARSET;
1707 if (EQ (w32_charset, Qw32_charset_greek))
1708 return GREEK_CHARSET;
1709 if (EQ (w32_charset, Qw32_charset_hebrew))
1710 return HEBREW_CHARSET;
1711 if (EQ (w32_charset, Qw32_charset_vietnamese))
1712 return VIETNAMESE_CHARSET;
1713 if (EQ (w32_charset, Qw32_charset_thai))
1714 return THAI_CHARSET;
1715 if (EQ (w32_charset, Qw32_charset_mac))
1716 return MAC_CHARSET;
1717
1718 return DEFAULT_CHARSET;
1719}
1720
1721
f7a84cb4 1722/* Convert a Lisp font registry (symbol) to a windows charset. */
20399669 1723static LONG
b56ceb92 1724registry_to_w32_charset (Lisp_Object charset)
f7a84cb4
JR
1725{
1726 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1727 || EQ (charset, Qunicode_sip))
1728 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1729 else if (EQ (charset, Qiso8859_1))
1730 return ANSI_CHARSET;
040fe918
JR
1731 else if (SYMBOLP (charset))
1732 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
f7a84cb4
JR
1733 else
1734 return DEFAULT_CHARSET;
1735}
1736
57016d37
JR
1737/* Old function to convert from w32 to x charset, from w32fns.c. */
1738static char *
b56ceb92 1739w32_to_x_charset (int fncharset, char *matching)
57016d37
JR
1740{
1741 static char buf[32];
1742 Lisp_Object charset_type;
1743 int match_len = 0;
1744
1745 if (matching)
1746 {
1747 /* If fully specified, accept it as it is. Otherwise use a
1748 substring match. */
1749 char *wildcard = strchr (matching, '*');
1750 if (wildcard)
1751 *wildcard = '\0';
1752 else if (strchr (matching, '-'))
1753 return matching;
1754
1755 match_len = strlen (matching);
1756 }
1757
1758 switch (fncharset)
1759 {
1760 case ANSI_CHARSET:
1761 /* Handle startup case of w32-charset-info-alist not
1762 being set up yet. */
1763 if (NILP (Vw32_charset_info_alist))
1764 return "iso8859-1";
1765 charset_type = Qw32_charset_ansi;
1766 break;
1767 case DEFAULT_CHARSET:
1768 charset_type = Qw32_charset_default;
1769 break;
1770 case SYMBOL_CHARSET:
1771 charset_type = Qw32_charset_symbol;
1772 break;
1773 case SHIFTJIS_CHARSET:
1774 charset_type = Qw32_charset_shiftjis;
1775 break;
1776 case HANGEUL_CHARSET:
1777 charset_type = Qw32_charset_hangeul;
1778 break;
1779 case GB2312_CHARSET:
1780 charset_type = Qw32_charset_gb2312;
1781 break;
1782 case CHINESEBIG5_CHARSET:
1783 charset_type = Qw32_charset_chinesebig5;
1784 break;
1785 case OEM_CHARSET:
1786 charset_type = Qw32_charset_oem;
1787 break;
1788 case EASTEUROPE_CHARSET:
1789 charset_type = Qw32_charset_easteurope;
1790 break;
1791 case TURKISH_CHARSET:
1792 charset_type = Qw32_charset_turkish;
1793 break;
1794 case BALTIC_CHARSET:
1795 charset_type = Qw32_charset_baltic;
1796 break;
1797 case RUSSIAN_CHARSET:
1798 charset_type = Qw32_charset_russian;
1799 break;
1800 case ARABIC_CHARSET:
1801 charset_type = Qw32_charset_arabic;
1802 break;
1803 case GREEK_CHARSET:
1804 charset_type = Qw32_charset_greek;
1805 break;
1806 case HEBREW_CHARSET:
1807 charset_type = Qw32_charset_hebrew;
1808 break;
1809 case VIETNAMESE_CHARSET:
1810 charset_type = Qw32_charset_vietnamese;
1811 break;
1812 case THAI_CHARSET:
1813 charset_type = Qw32_charset_thai;
1814 break;
1815 case MAC_CHARSET:
1816 charset_type = Qw32_charset_mac;
1817 break;
1818 case JOHAB_CHARSET:
1819 charset_type = Qw32_charset_johab;
1820 break;
1821
1822 default:
1823 /* Encode numerical value of unknown charset. */
1824 sprintf (buf, "*-#%u", fncharset);
1825 return buf;
1826 }
1827
1828 {
1829 Lisp_Object rest;
1830 char * best_match = NULL;
1831 int matching_found = 0;
1832
1833 /* Look through w32-charset-info-alist for the character set.
1834 Prefer ISO codepages, and prefer lower numbers in the ISO
1835 range. Only return charsets for codepages which are installed.
1836
1837 Format of each entry is
1838 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1839 */
1840 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
1841 {
1842 char * x_charset;
1843 Lisp_Object w32_charset;
1844 Lisp_Object codepage;
1845
1846 Lisp_Object this_entry = XCAR (rest);
1847
1848 /* Skip invalid entries in alist. */
1849 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
1850 || !CONSP (XCDR (this_entry))
1851 || !SYMBOLP (XCAR (XCDR (this_entry))))
1852 continue;
1853
1854 x_charset = SDATA (XCAR (this_entry));
1855 w32_charset = XCAR (XCDR (this_entry));
1856 codepage = XCDR (XCDR (this_entry));
1857
1858 /* Look for Same charset and a valid codepage (or non-int
1859 which means ignore). */
1860 if (EQ (w32_charset, charset_type)
1861 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
1862 || IsValidCodePage (XINT (codepage))))
1863 {
1864 /* If we don't have a match already, then this is the
1865 best. */
1866 if (!best_match)
1867 {
1868 best_match = x_charset;
1869 if (matching && !strnicmp (x_charset, matching, match_len))
1870 matching_found = 1;
1871 }
1872 /* If we already found a match for MATCHING, then
1873 only consider other matches. */
1874 else if (matching_found
1875 && strnicmp (x_charset, matching, match_len))
1876 continue;
1877 /* If this matches what we want, and the best so far doesn't,
1878 then this is better. */
1879 else if (!matching_found && matching
1880 && !strnicmp (x_charset, matching, match_len))
1881 {
1882 best_match = x_charset;
1883 matching_found = 1;
1884 }
1885 /* If this is fully specified, and the best so far isn't,
1886 then this is better. */
1887 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
1888 /* If this is an ISO codepage, and the best so far isn't,
1889 then this is better, but only if it fully specifies the
1890 encoding. */
1891 || (strnicmp (best_match, "iso", 3) != 0
1892 && strnicmp (x_charset, "iso", 3) == 0
1893 && strchr (x_charset, '-')))
1894 best_match = x_charset;
1895 /* If both are ISO8859 codepages, choose the one with the
1896 lowest number in the encoding field. */
1897 else if (strnicmp (best_match, "iso8859-", 8) == 0
1898 && strnicmp (x_charset, "iso8859-", 8) == 0)
1899 {
1900 int best_enc = atoi (best_match + 8);
1901 int this_enc = atoi (x_charset + 8);
1902 if (this_enc > 0 && this_enc < best_enc)
1903 best_match = x_charset;
1904 }
1905 }
1906 }
1907
1908 /* If no match, encode the numeric value. */
1909 if (!best_match)
1910 {
1911 sprintf (buf, "*-#%u", fncharset);
1912 return buf;
1913 }
1914
1915 strncpy (buf, best_match, 31);
1916 /* If the charset is not fully specified, put -0 on the end. */
1917 if (!strchr (best_match, '-'))
1918 {
1919 int pos = strlen (best_match);
1920 /* Charset specifiers shouldn't be very long. If it is a made
1921 up one, truncating it should not do any harm since it isn't
1922 recognized anyway. */
1923 if (pos > 29)
1924 pos = 29;
1925 strcpy (buf + pos, "-0");
1926 }
1927 buf[31] = '\0';
1928 return buf;
1929 }
1930}
1931
20399669 1932static Lisp_Object
b56ceb92 1933w32_registry (LONG w32_charset, DWORD font_type)
f7a84cb4 1934{
aee986fd
JR
1935 char *charset;
1936
fe7a3057 1937 /* If charset is defaulted, charset is Unicode or unknown, depending on
aee986fd 1938 font type. */
c960bff8 1939 if (w32_charset == DEFAULT_CHARSET)
aee986fd 1940 return font_type == TRUETYPE_FONTTYPE ? Qiso10646_1 : Qunknown;
c960bff8 1941
aee986fd 1942 charset = w32_to_x_charset (w32_charset, NULL);
ed3751c8 1943 return font_intern_prop (charset, strlen (charset), 1);
f7a84cb4
JR
1944}
1945
5f18d119 1946static int
b56ceb92 1947w32_decode_weight (int fnweight)
5f18d119
KH
1948{
1949 if (fnweight >= FW_HEAVY) return 210;
1950 if (fnweight >= FW_EXTRABOLD) return 205;
1951 if (fnweight >= FW_BOLD) return 200;
1952 if (fnweight >= FW_SEMIBOLD) return 180;
1953 if (fnweight >= FW_NORMAL) return 100;
1954 if (fnweight >= FW_LIGHT) return 50;
1955 if (fnweight >= FW_EXTRALIGHT) return 40;
9d4f32e8 1956 if (fnweight > FW_THIN) return 20;
5f18d119
KH
1957 return 0;
1958}
1959
1960static int
b56ceb92 1961w32_encode_weight (int n)
5f18d119
KH
1962{
1963 if (n >= 210) return FW_HEAVY;
1964 if (n >= 205) return FW_EXTRABOLD;
1965 if (n >= 200) return FW_BOLD;
1966 if (n >= 180) return FW_SEMIBOLD;
1967 if (n >= 100) return FW_NORMAL;
1968 if (n >= 50) return FW_LIGHT;
1969 if (n >= 40) return FW_EXTRALIGHT;
9d4f32e8 1970 if (n >= 20) return FW_THIN;
5f18d119
KH
1971 return 0;
1972}
1973
3ef8c1b4
JR
1974/* Convert a Windows font weight into one of the weights supported
1975 by fontconfig (see font.c:font_parse_fcname). */
1976static Lisp_Object
b56ceb92 1977w32_to_fc_weight (int n)
3ef8c1b4
JR
1978{
1979 if (n >= FW_EXTRABOLD) return intern ("black");
9d4f32e8
JB
1980 if (n >= FW_BOLD) return intern ("bold");
1981 if (n >= FW_SEMIBOLD) return intern ("demibold");
1982 if (n >= FW_NORMAL) return intern ("medium");
3ef8c1b4
JR
1983 return intern ("light");
1984}
1985
f7a84cb4 1986/* Fill in all the available details of LOGFONT from FONT_SPEC. */
20399669 1987static void
a10c8269 1988fill_in_logfont (struct frame *f, LOGFONT *logfont, Lisp_Object font_spec)
f7a84cb4 1989{
d205d43b 1990 Lisp_Object tmp, extra;
42143acd 1991 int dpi = FRAME_RES_Y (f);
f7a84cb4 1992
5f18d119
KH
1993 tmp = AREF (font_spec, FONT_DPI_INDEX);
1994 if (INTEGERP (tmp))
d205d43b 1995 {
5f18d119
KH
1996 dpi = XINT (tmp);
1997 }
1998 else if (FLOATP (tmp))
1999 {
2000 dpi = (int) (XFLOAT_DATA (tmp) + 0.5);
d205d43b 2001 }
f7a84cb4
JR
2002
2003 /* Height */
2004 tmp = AREF (font_spec, FONT_SIZE_INDEX);
2005 if (INTEGERP (tmp))
040fe918 2006 logfont->lfHeight = -1 * XINT (tmp);
f7a84cb4 2007 else if (FLOATP (tmp))
d205d43b 2008 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
f7a84cb4
JR
2009
2010 /* Escapement */
2011
2012 /* Orientation */
2013
2014 /* Weight */
2015 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
2016 if (INTEGERP (tmp))
5f18d119 2017 logfont->lfWeight = w32_encode_weight (FONT_WEIGHT_NUMERIC (font_spec));
f7a84cb4
JR
2018
2019 /* Italic */
2020 tmp = AREF (font_spec, FONT_SLANT_INDEX);
2021 if (INTEGERP (tmp))
2022 {
5f18d119 2023 int slant = FONT_SLANT_NUMERIC (font_spec);
f7a84cb4
JR
2024 logfont->lfItalic = slant > 150 ? 1 : 0;
2025 }
2026
2027 /* Underline */
2028
2029 /* Strikeout */
2030
2031 /* Charset */
2032 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
2033 if (! NILP (tmp))
d205d43b 2034 logfont->lfCharSet = registry_to_w32_charset (tmp);
56df6710
JR
2035 else
2036 logfont->lfCharSet = DEFAULT_CHARSET;
f7a84cb4
JR
2037
2038 /* Out Precision */
91583281 2039
f7a84cb4 2040 /* Clip Precision */
91583281
JR
2041
2042 /* Quality */
040fe918
JR
2043 logfont->lfQuality = DEFAULT_QUALITY;
2044
d205d43b
JR
2045 /* Generic Family and Face Name */
2046 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
2047
f7a84cb4 2048 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
d205d43b
JR
2049 if (! NILP (tmp))
2050 {
2051 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
2052 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
2053 ; /* Font name was generic, don't fill in font name. */
2054 /* Font families are interned, but allow for strings also in case of
2055 user input. */
2056 else if (SYMBOLP (tmp))
bf98199c
JM
2057 {
2058 strncpy (logfont->lfFaceName,
2059 SDATA (ENCODE_SYSTEM (SYMBOL_NAME (tmp))), LF_FACESIZE);
2060 logfont->lfFaceName[LF_FACESIZE-1] = '\0';
2061 }
d205d43b 2062 }
f7a84cb4 2063
9e1a2995
JR
2064 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
2065 if (!NILP (tmp))
2066 {
2067 /* Override generic family. */
2068 BYTE family = w32_generic_family (tmp);
2069 if (family != FF_DONTCARE)
2070 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
2071 }
2072
5f18d119
KH
2073 /* Set pitch based on the spacing property. */
2074 tmp = AREF (font_spec, FONT_SPACING_INDEX);
2075 if (INTEGERP (tmp))
2076 {
2077 int spacing = XINT (tmp);
2078 if (spacing < FONT_SPACING_MONO)
2079 logfont->lfPitchAndFamily
657d08d3 2080 = (logfont->lfPitchAndFamily & 0xF0) | VARIABLE_PITCH;
5f18d119
KH
2081 else
2082 logfont->lfPitchAndFamily
657d08d3 2083 = (logfont->lfPitchAndFamily & 0xF0) | FIXED_PITCH;
5f18d119
KH
2084 }
2085
d205d43b 2086 /* Process EXTRA info. */
5f18d119
KH
2087 for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
2088 CONSP (extra); extra = XCDR (extra))
d205d43b
JR
2089 {
2090 tmp = XCAR (extra);
2091 if (CONSP (tmp))
2092 {
2093 Lisp_Object key, val;
2094 key = XCAR (tmp), val = XCDR (tmp);
fe7a3057 2095 /* Only use QCscript if charset is not provided, or is Unicode
d205d43b
JR
2096 and a single script is specified. This is rather crude,
2097 and is only used to narrow down the fonts returned where
2098 there is a definite match. Some scripts, such as latin, han,
2099 cjk-misc match multiple lfCharSet values, so we can't pre-filter
2100 them. */
5f18d119 2101 if (EQ (key, QCscript)
d205d43b
JR
2102 && logfont->lfCharSet == DEFAULT_CHARSET
2103 && SYMBOLP (val))
2104 {
2105 if (EQ (val, Qgreek))
2106 logfont->lfCharSet = GREEK_CHARSET;
2107 else if (EQ (val, Qhangul))
2108 logfont->lfCharSet = HANGUL_CHARSET;
2109 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
2110 logfont->lfCharSet = SHIFTJIS_CHARSET;
2111 else if (EQ (val, Qbopomofo))
2112 logfont->lfCharSet = CHINESEBIG5_CHARSET;
2113 /* GB 18030 supports tibetan, yi, mongolian,
2114 fonts that support it should show up if we ask for
2115 GB2312 fonts. */
2116 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
2117 || EQ (val, Qmongolian))
2118 logfont->lfCharSet = GB2312_CHARSET;
2119 else if (EQ (val, Qhebrew))
2120 logfont->lfCharSet = HEBREW_CHARSET;
2121 else if (EQ (val, Qarabic))
2122 logfont->lfCharSet = ARABIC_CHARSET;
2123 else if (EQ (val, Qthai))
2124 logfont->lfCharSet = THAI_CHARSET;
d205d43b 2125 }
91583281
JR
2126 else if (EQ (key, QCantialias) && SYMBOLP (val))
2127 {
2128 logfont->lfQuality = w32_antialias_type (val);
2129 }
d205d43b
JR
2130 }
2131 }
f7a84cb4
JR
2132}
2133
20399669 2134static void
b56ceb92 2135list_all_matching_fonts (struct font_callback_data *match_data)
f7a84cb4
JR
2136{
2137 HDC dc;
fdb396e2 2138 Lisp_Object families = w32font_list_family (XFRAME (match_data->frame));
d205d43b 2139 struct frame *f = XFRAME (match_data->frame);
f7a84cb4
JR
2140
2141 dc = get_frame_dc (f);
2142
2143 while (!NILP (families))
2144 {
e6df5336
JR
2145 /* Only fonts from the current locale are given localized names
2146 on Windows, so we can keep backwards compatibility with
2147 Windows 9x/ME by using non-Unicode font enumeration without
2148 sacrificing internationalization here. */
d205d43b 2149 char *name;
f7a84cb4
JR
2150 Lisp_Object family = CAR (families);
2151 families = CDR (families);
d205d43b
JR
2152 if (NILP (family))
2153 continue;
8f112d52 2154 else if (SYMBOLP (family))
e6df5336 2155 name = SDATA (ENCODE_SYSTEM (SYMBOL_NAME (family)));
8f112d52
JR
2156 else
2157 continue;
d205d43b
JR
2158
2159 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
2160 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
2161
2162 EnumFontFamiliesEx (dc, &match_data->pattern,
2163 (FONTENUMPROC) add_font_entity_to_list,
2164 (LPARAM) match_data, 0);
f7a84cb4
JR
2165 }
2166
2167 release_frame_dc (f, dc);
2168}
2169
91583281 2170static Lisp_Object
b56ceb92 2171lispy_antialias_type (BYTE type)
91583281
JR
2172{
2173 Lisp_Object lispy;
2174
2175 switch (type)
2176 {
2177 case NONANTIALIASED_QUALITY:
2178 lispy = Qnone;
2179 break;
2180 case ANTIALIASED_QUALITY:
2181 lispy = Qstandard;
2182 break;
2183 case CLEARTYPE_QUALITY:
2184 lispy = Qsubpixel;
2185 break;
2186 case CLEARTYPE_NATURAL_QUALITY:
2187 lispy = Qnatural;
2188 break;
2189 default:
2190 lispy = Qnil;
2191 break;
2192 }
2193 return lispy;
2194}
2195
2196/* Convert antialiasing symbols to lfQuality */
2197static BYTE
b56ceb92 2198w32_antialias_type (Lisp_Object type)
91583281
JR
2199{
2200 if (EQ (type, Qnone))
2201 return NONANTIALIASED_QUALITY;
2202 else if (EQ (type, Qstandard))
2203 return ANTIALIASED_QUALITY;
2204 else if (EQ (type, Qsubpixel))
2205 return CLEARTYPE_QUALITY;
2206 else if (EQ (type, Qnatural))
2207 return CLEARTYPE_NATURAL_QUALITY;
2208 else
2209 return DEFAULT_QUALITY;
2210}
2211
d205d43b
JR
2212/* Return a list of all the scripts that the font supports. */
2213static Lisp_Object
2214font_supported_scripts (FONTSIGNATURE * sig)
f7a84cb4 2215{
d205d43b
JR
2216 DWORD * subranges = sig->fsUsb;
2217 Lisp_Object supported = Qnil;
2218
2219 /* Match a single subrange. SYM is set if bit N is set in subranges. */
2220#define SUBRANGE(n,sym) \
2221 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
2222 supported = Fcons ((sym), supported)
2223
2224 /* Match multiple subranges. SYM is set if any MASK bit is set in
2225 subranges[0 - 3]. */
2226#define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
2227 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
2228 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
2229 supported = Fcons ((sym), supported)
2230
56df6710
JR
2231 SUBRANGE (0, Qlatin);
2232 /* The following count as latin too, ASCII should be present in these fonts,
2233 so don't need to mark them separately. */
2234 /* 1: Latin-1 supplement, 2: Latin Extended A, 3: Latin Extended B. */
2235 SUBRANGE (4, Qphonetic);
dbdb7031 2236 /* 5: Spacing and tone modifiers, 6: Combining Diacritical Marks. */
d205d43b
JR
2237 SUBRANGE (7, Qgreek);
2238 SUBRANGE (8, Qcoptic);
2239 SUBRANGE (9, Qcyrillic);
2240 SUBRANGE (10, Qarmenian);
2241 SUBRANGE (11, Qhebrew);
174f1c74 2242 /* 12: Vai. */
d205d43b
JR
2243 SUBRANGE (13, Qarabic);
2244 SUBRANGE (14, Qnko);
2245 SUBRANGE (15, Qdevanagari);
2246 SUBRANGE (16, Qbengali);
2247 SUBRANGE (17, Qgurmukhi);
2248 SUBRANGE (18, Qgujarati);
2249 SUBRANGE (19, Qoriya);
2250 SUBRANGE (20, Qtamil);
2251 SUBRANGE (21, Qtelugu);
2252 SUBRANGE (22, Qkannada);
2253 SUBRANGE (23, Qmalayalam);
2254 SUBRANGE (24, Qthai);
2255 SUBRANGE (25, Qlao);
2256 SUBRANGE (26, Qgeorgian);
56df6710
JR
2257 SUBRANGE (27, Qbalinese);
2258 /* 28: Hangul Jamo. */
2259 /* 29: Latin Extended, 30: Greek Extended, 31: Punctuation. */
2260 /* 32-47: Symbols (defined below). */
d205d43b 2261 SUBRANGE (48, Qcjk_misc);
56df6710
JR
2262 /* Match either 49: katakana or 50: hiragana for kana. */
2263 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
d205d43b 2264 SUBRANGE (51, Qbopomofo);
56df6710
JR
2265 /* 52: Compatibility Jamo */
2266 SUBRANGE (53, Qphags_pa);
2267 /* 54: Enclosed CJK letters and months, 55: CJK Compatibility. */
d205d43b 2268 SUBRANGE (56, Qhangul);
56df6710
JR
2269 /* 57: Surrogates. */
2270 SUBRANGE (58, Qphoenician);
d205d43b 2271 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
56df6710
JR
2272 SUBRANGE (59, Qideographic_description); /* Windows lumps this in. */
2273 SUBRANGE (59, Qkanbun); /* And this. */
2274 /* 60: Private use, 61: CJK strokes and compatibility. */
2275 /* 62: Alphabetic Presentation, 63: Arabic Presentation A. */
2276 /* 64: Combining half marks, 65: Vertical and CJK compatibility. */
2277 /* 66: Small forms, 67: Arabic Presentation B, 68: Half and Full width. */
2278 /* 69: Specials. */
d205d43b
JR
2279 SUBRANGE (70, Qtibetan);
2280 SUBRANGE (71, Qsyriac);
2281 SUBRANGE (72, Qthaana);
2282 SUBRANGE (73, Qsinhala);
2283 SUBRANGE (74, Qmyanmar);
2284 SUBRANGE (75, Qethiopic);
2285 SUBRANGE (76, Qcherokee);
2286 SUBRANGE (77, Qcanadian_aboriginal);
2287 SUBRANGE (78, Qogham);
2288 SUBRANGE (79, Qrunic);
2289 SUBRANGE (80, Qkhmer);
2290 SUBRANGE (81, Qmongolian);
2291 SUBRANGE (82, Qbraille);
2292 SUBRANGE (83, Qyi);
56df6710
JR
2293 SUBRANGE (84, Qbuhid);
2294 SUBRANGE (84, Qhanunoo);
2295 SUBRANGE (84, Qtagalog);
2296 SUBRANGE (84, Qtagbanwa);
2297 SUBRANGE (85, Qold_italic);
2298 SUBRANGE (86, Qgothic);
2299 SUBRANGE (87, Qdeseret);
d205d43b
JR
2300 SUBRANGE (88, Qbyzantine_musical_symbol);
2301 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
d205d43b 2302 SUBRANGE (89, Qmathematical);
56df6710
JR
2303 /* 90: Private use, 91: Variation selectors, 92: Tags. */
2304 SUBRANGE (93, Qlimbu);
2305 SUBRANGE (94, Qtai_le);
2306 /* 95: New Tai Le */
2307 SUBRANGE (90, Qbuginese);
2308 SUBRANGE (97, Qglagolitic);
2309 SUBRANGE (98, Qtifinagh);
2310 /* 99: Yijing Hexagrams. */
2311 SUBRANGE (100, Qsyloti_nagri);
2312 SUBRANGE (101, Qlinear_b);
2313 /* 102: Ancient Greek Numbers. */
2314 SUBRANGE (103, Qugaritic);
2315 SUBRANGE (104, Qold_persian);
2316 SUBRANGE (105, Qshavian);
2317 SUBRANGE (106, Qosmanya);
2318 SUBRANGE (107, Qcypriot);
2319 SUBRANGE (108, Qkharoshthi);
2320 /* 109: Tai Xuan Jing. */
2321 SUBRANGE (110, Qcuneiform);
174f1c74
JR
2322 /* 111: Counting Rods, 112: Sundanese, 113: Lepcha, 114: Ol Chiki. */
2323 /* 115: Saurashtra, 116: Kayah Li, 117: Rejang. */
2324 SUBRANGE (118, Qcham);
2325 /* 119: Ancient symbols, 120: Phaistos Disc. */
dbdb7031 2326 /* 121: Carian, Lycian, Lydian, 122: Dominoes, Mahjong tiles. */
174f1c74 2327 /* 123-127: Reserved. */
d205d43b
JR
2328
2329 /* There isn't really a main symbol range, so include symbol if any
2330 relevant range is set. */
2331 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
2332
174f1c74 2333 /* Missing: Tai Viet (U+AA80-U+AADF). */
d205d43b
JR
2334#undef SUBRANGE
2335#undef MASK_ANY
2336
2337 return supported;
f7a84cb4
JR
2338}
2339
67997c79
JR
2340/* Generate a full name for a Windows font.
2341 The full name is in fcname format, with weight, slant and antialiasing
2342 specified if they are not "normal". */
2343static int
b56ceb92
JB
2344w32font_full_name (LOGFONT * font, Lisp_Object font_obj,
2345 int pixel_size, char *name, int nbytes)
67997c79 2346{
a823468b 2347 int len, height, outline;
67997c79
JR
2348 char *p;
2349 Lisp_Object antialiasing, weight = Qnil;
2350
a823468b
JR
2351 len = strlen (font->lfFaceName);
2352
2353 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
2354
2355 /* Represent size of scalable fonts by point size. But use pixelsize for
2356 raster fonts to indicate that they are exactly that size. */
2357 if (outline)
2358 len += 11; /* -SIZE */
2359 else
6fe9826d 2360 len += 21;
67997c79
JR
2361
2362 if (font->lfItalic)
2363 len += 7; /* :italic */
2364
2365 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2366 {
3ef8c1b4
JR
2367 weight = w32_to_fc_weight (font->lfWeight);
2368 len += 1 + SBYTES (SYMBOL_NAME (weight)); /* :WEIGHT */
67997c79
JR
2369 }
2370
2371 antialiasing = lispy_antialias_type (font->lfQuality);
2372 if (! NILP (antialiasing))
2373 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
2374
2375 /* Check that the buffer is big enough */
2376 if (len > nbytes)
2377 return -1;
2378
2379 p = name;
2380 p += sprintf (p, "%s", font->lfFaceName);
2381
a823468b
JR
2382 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
2383
2384 if (height > 0)
2385 {
2386 if (outline)
2387 {
2388 float pointsize = height * 72.0 / one_w32_display_info.resy;
7145be81
JR
2389 /* Round to nearest half point. floor is used, since round is not
2390 supported in MS library. */
2391 pointsize = floor (pointsize * 2 + 0.5) / 2;
a823468b
JR
2392 p += sprintf (p, "-%1.1f", pointsize);
2393 }
2394 else
2395 p += sprintf (p, ":pixelsize=%d", height);
2396 }
67997c79 2397
3ef8c1b4
JR
2398 if (SYMBOLP (weight) && ! NILP (weight))
2399 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2400
67997c79
JR
2401 if (font->lfItalic)
2402 p += sprintf (p, ":italic");
2403
67997c79
JR
2404 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
2405 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
2406
2407 return (p - name);
2408}
f7a84cb4 2409
6fe9826d
JR
2410/* Convert a logfont and point size into a fontconfig style font name.
2411 POINTSIZE is in tenths of points.
2412 If SIZE indicates the size of buffer FCNAME, into which the font name
2413 is written. If the buffer is not large enough to contain the name,
2414 the function returns -1, otherwise it returns the number of bytes
2415 written to FCNAME. */
b56ceb92
JB
2416static int
2417logfont_to_fcname (LOGFONT* font, int pointsize, char *fcname, int size)
6fe9826d
JR
2418{
2419 int len, height;
2420 char *p = fcname;
2421 Lisp_Object weight = Qnil;
2422
2423 len = strlen (font->lfFaceName) + 2;
2424 height = pointsize / 10;
2425 while (height /= 10)
2426 len++;
2427
2428 if (pointsize % 10)
2429 len += 2;
2430
2431 if (font->lfItalic)
2432 len += 7; /* :italic */
2433 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2434 {
3ef8c1b4
JR
2435 weight = w32_to_fc_weight (font->lfWeight);
2436 len += SBYTES (SYMBOL_NAME (weight)) + 1;
6fe9826d
JR
2437 }
2438
2439 if (len > size)
2440 return -1;
2441
2442 p += sprintf (p, "%s-%d", font->lfFaceName, pointsize / 10);
2443 if (pointsize % 10)
2444 p += sprintf (p, ".%d", pointsize % 10);
2445
3ef8c1b4
JR
2446 if (SYMBOLP (weight) && !NILP (weight))
2447 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2448
6fe9826d
JR
2449 if (font->lfItalic)
2450 p += sprintf (p, ":italic");
2451
6fe9826d
JR
2452 return (p - fcname);
2453}
34fd2d28 2454
9c623c85 2455static void
b56ceb92
JB
2456compute_metrics (HDC dc, struct w32font_info *w32_font, unsigned int code,
2457 struct w32_metric_cache *metrics)
34fd2d28
JR
2458{
2459 GLYPHMETRICS gm;
2460 MAT2 transform;
8f112d52 2461 unsigned int options = GGO_METRICS;
579ca67b 2462 INT width;
8f112d52
JR
2463
2464 if (w32_font->glyph_idx)
2465 options |= GGO_GLYPH_INDEX;
34fd2d28 2466
72af86bd 2467 memset (&transform, 0, sizeof (transform));
34fd2d28
JR
2468 transform.eM11.value = 1;
2469 transform.eM22.value = 1;
8f112d52 2470
db4f02f2 2471 if (get_glyph_outline_w (dc, code, options, &gm, 0, NULL, &transform)
8f112d52
JR
2472 != GDI_ERROR)
2473 {
2474 metrics->lbearing = gm.gmptGlyphOrigin.x;
2475 metrics->rbearing = gm.gmptGlyphOrigin.x + gm.gmBlackBoxX;
2476 metrics->width = gm.gmCellIncX;
2477 metrics->status = W32METRIC_SUCCESS;
2478 }
579ca67b
TS
2479 else if (get_char_width_32_w (dc, code, code, &width) != 0)
2480 {
2481 metrics->lbearing = 0;
2482 metrics->rbearing = width;
2483 metrics->width = width;
2484 metrics->status = W32METRIC_SUCCESS;
2485 }
9c623c85
JB
2486 else
2487 metrics->status = W32METRIC_FAIL;
34fd2d28
JR
2488}
2489
6fe9826d
JR
2490DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
2491 doc: /* Read a font name using a W32 font selection dialog.
2492Return fontconfig style font string corresponding to the selection.
2493
2494If FRAME is omitted or nil, it defaults to the selected frame.
1ada3dab 2495If EXCLUDE-PROPORTIONAL is non-nil, exclude proportional fonts
6fe9826d 2496in the font selection dialog. */)
5842a27b 2497 (Lisp_Object frame, Lisp_Object exclude_proportional)
6fe9826d 2498{
a10c8269 2499 struct frame *f = decode_window_system_frame (frame);
6fe9826d
JR
2500 CHOOSEFONT cf;
2501 LOGFONT lf;
2502 TEXTMETRIC tm;
2503 HDC hdc;
2504 HANDLE oldobj;
2505 char buf[100];
2506
72af86bd
AS
2507 memset (&cf, 0, sizeof (cf));
2508 memset (&lf, 0, sizeof (lf));
6fe9826d
JR
2509
2510 cf.lStructSize = sizeof (cf);
2511 cf.hwndOwner = FRAME_W32_WINDOW (f);
2512 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
2513
1ada3dab 2514 /* If exclude_proportional is non-nil, limit the selection to
6fe9826d 2515 monospaced fonts. */
1ada3dab 2516 if (!NILP (exclude_proportional))
6fe9826d
JR
2517 cf.Flags |= CF_FIXEDPITCHONLY;
2518
2519 cf.lpLogFont = &lf;
2520
2521 /* Initialize as much of the font details as we can from the current
2522 default font. */
2523 hdc = GetDC (FRAME_W32_WINDOW (f));
c35f9821 2524 oldobj = SelectObject (hdc, FONT_HANDLE (FRAME_FONT (f)));
6fe9826d
JR
2525 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
2526 if (GetTextMetrics (hdc, &tm))
2527 {
2528 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
2529 lf.lfWeight = tm.tmWeight;
2530 lf.lfItalic = tm.tmItalic;
2531 lf.lfUnderline = tm.tmUnderlined;
2532 lf.lfStrikeOut = tm.tmStruckOut;
2533 lf.lfCharSet = tm.tmCharSet;
2534 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
2535 }
2536 SelectObject (hdc, oldobj);
2537 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
2538
2539 if (!ChooseFont (&cf)
2540 || logfont_to_fcname (&lf, cf.iPointSize, buf, 100) < 0)
2541 return Qnil;
2542
e6df5336 2543 return DECODE_SYSTEM (build_string (buf));
6fe9826d
JR
2544}
2545
3106121c 2546static const char *const w32font_booleans [] = {
9fa82824
DP
2547 NULL,
2548};
2549
3106121c 2550static const char *const w32font_non_booleans [] = {
9fa82824
DP
2551 ":script",
2552 ":antialias",
2553 ":style",
2554 NULL,
2555};
2556
2557static void
220d91b8 2558w32font_filter_properties (Lisp_Object font, Lisp_Object alist)
9fa82824
DP
2559{
2560 font_filter_properties (font, alist, w32font_booleans, w32font_non_booleans);
2561}
2562
f7a84cb4
JR
2563struct font_driver w32font_driver =
2564 {
8eac0c84 2565 0, /* Qgdi */
5f18d119 2566 0, /* case insensitive */
f7a84cb4
JR
2567 w32font_get_cache,
2568 w32font_list,
2569 w32font_match,
2570 w32font_list_family,
2571 NULL, /* free_entity */
2572 w32font_open,
2573 w32font_close,
2574 NULL, /* prepare_face */
2575 NULL, /* done_face */
2576 w32font_has_char,
2577 w32font_encode_char,
2578 w32font_text_extents,
2579 w32font_draw,
2580 NULL, /* get_bitmap */
2581 NULL, /* free_bitmap */
2582 NULL, /* get_outline */
2583 NULL, /* free_outline */
2584 NULL, /* anchor_point */
2585 NULL, /* otf_capability */
5b0c3446
JR
2586 NULL, /* otf_drive */
2587 NULL, /* start_for_frame */
2588 NULL, /* end_for_frame */
637fa988
JD
2589 NULL, /* shape */
2590 NULL, /* check */
2591 NULL, /* get_variation_glyphs */
9fa82824 2592 w32font_filter_properties,
9c88f339 2593 NULL, /* cached_font_ok */
f7a84cb4
JR
2594 };
2595
f7a84cb4
JR
2596
2597/* Initialize state that does not change between invocations. This is only
2598 called when Emacs is dumped. */
20399669 2599void
b56ceb92 2600syms_of_w32font (void)
f7a84cb4 2601{
8eac0c84 2602 DEFSYM (Qgdi, "gdi");
34fd2d28
JR
2603 DEFSYM (Quniscribe, "uniscribe");
2604 DEFSYM (QCformat, ":format");
d205d43b
JR
2605
2606 /* Generic font families. */
2607 DEFSYM (Qmonospace, "monospace");
2608 DEFSYM (Qserif, "serif");
9e1a2995 2609 DEFSYM (Qsansserif, "sansserif");
f7a84cb4 2610 DEFSYM (Qscript, "script");
d205d43b
JR
2611 DEFSYM (Qdecorative, "decorative");
2612 /* Aliases. */
9e1a2995 2613 DEFSYM (Qsans_serif, "sans_serif");
d205d43b
JR
2614 DEFSYM (Qsans, "sans");
2615 DEFSYM (Qmono, "mono");
2616
2617 /* Fake foundries. */
2618 DEFSYM (Qraster, "raster");
2619 DEFSYM (Qoutline, "outline");
f7a84cb4 2620 DEFSYM (Qunknown, "unknown");
d205d43b 2621
91583281
JR
2622 /* Antialiasing. */
2623 DEFSYM (Qstandard, "standard");
2624 DEFSYM (Qsubpixel, "subpixel");
2625 DEFSYM (Qnatural, "natural");
d205d43b 2626
8f112d52 2627 /* Languages */
8f112d52
JR
2628 DEFSYM (Qzh, "zh");
2629
d205d43b
JR
2630 /* Scripts */
2631 DEFSYM (Qlatin, "latin");
2632 DEFSYM (Qgreek, "greek");
2633 DEFSYM (Qcoptic, "coptic");
2634 DEFSYM (Qcyrillic, "cyrillic");
2635 DEFSYM (Qarmenian, "armenian");
2636 DEFSYM (Qhebrew, "hebrew");
2637 DEFSYM (Qarabic, "arabic");
2638 DEFSYM (Qsyriac, "syriac");
2639 DEFSYM (Qnko, "nko");
2640 DEFSYM (Qthaana, "thaana");
2641 DEFSYM (Qdevanagari, "devanagari");
2642 DEFSYM (Qbengali, "bengali");
2643 DEFSYM (Qgurmukhi, "gurmukhi");
2644 DEFSYM (Qgujarati, "gujarati");
2645 DEFSYM (Qoriya, "oriya");
2646 DEFSYM (Qtamil, "tamil");
2647 DEFSYM (Qtelugu, "telugu");
2648 DEFSYM (Qkannada, "kannada");
2649 DEFSYM (Qmalayalam, "malayalam");
2650 DEFSYM (Qsinhala, "sinhala");
2651 DEFSYM (Qthai, "thai");
2652 DEFSYM (Qlao, "lao");
2653 DEFSYM (Qtibetan, "tibetan");
2654 DEFSYM (Qmyanmar, "myanmar");
2655 DEFSYM (Qgeorgian, "georgian");
2656 DEFSYM (Qhangul, "hangul");
2657 DEFSYM (Qethiopic, "ethiopic");
2658 DEFSYM (Qcherokee, "cherokee");
2659 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
2660 DEFSYM (Qogham, "ogham");
2661 DEFSYM (Qrunic, "runic");
2662 DEFSYM (Qkhmer, "khmer");
2663 DEFSYM (Qmongolian, "mongolian");
d205d43b
JR
2664 DEFSYM (Qbraille, "braille");
2665 DEFSYM (Qhan, "han");
2666 DEFSYM (Qideographic_description, "ideographic-description");
2667 DEFSYM (Qcjk_misc, "cjk-misc");
2668 DEFSYM (Qkana, "kana");
2669 DEFSYM (Qbopomofo, "bopomofo");
2670 DEFSYM (Qkanbun, "kanbun");
2671 DEFSYM (Qyi, "yi");
2672 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
2673 DEFSYM (Qmusical_symbol, "musical-symbol");
2674 DEFSYM (Qmathematical, "mathematical");
174f1c74 2675 DEFSYM (Qcham, "cham");
56df6710
JR
2676 DEFSYM (Qphonetic, "phonetic");
2677 DEFSYM (Qbalinese, "balinese");
2678 DEFSYM (Qbuginese, "buginese");
2679 DEFSYM (Qbuhid, "buhid");
2680 DEFSYM (Qcuneiform, "cuneiform");
2681 DEFSYM (Qcypriot, "cypriot");
2682 DEFSYM (Qdeseret, "deseret");
2683 DEFSYM (Qglagolitic, "glagolitic");
2684 DEFSYM (Qgothic, "gothic");
2685 DEFSYM (Qhanunoo, "hanunoo");
2686 DEFSYM (Qkharoshthi, "kharoshthi");
2687 DEFSYM (Qlimbu, "limbu");
2688 DEFSYM (Qlinear_b, "linear_b");
2689 DEFSYM (Qold_italic, "old_italic");
2690 DEFSYM (Qold_persian, "old_persian");
2691 DEFSYM (Qosmanya, "osmanya");
2692 DEFSYM (Qphags_pa, "phags-pa");
2693 DEFSYM (Qphoenician, "phoenician");
2694 DEFSYM (Qshavian, "shavian");
2695 DEFSYM (Qsyloti_nagri, "syloti_nagri");
2696 DEFSYM (Qtagalog, "tagalog");
2697 DEFSYM (Qtagbanwa, "tagbanwa");
2698 DEFSYM (Qtai_le, "tai_le");
2699 DEFSYM (Qtifinagh, "tifinagh");
2700 DEFSYM (Qugaritic, "ugaritic");
d205d43b 2701
57016d37
JR
2702 /* W32 font encodings. */
2703 DEFVAR_LISP ("w32-charset-info-alist",
29208e82 2704 Vw32_charset_info_alist,
57016d37
JR
2705 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
2706Each entry should be of the form:
2707
2708 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
2709
2710where CHARSET_NAME is a string used in font names to identify the charset,
2711WINDOWS_CHARSET is a symbol that can be one of:
2712
2713 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
2714 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
2715 w32-charset-chinesebig5, w32-charset-johab, w32-charset-hebrew,
2716 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
2717 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
2718 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
2719 or w32-charset-oem.
2720
2721CODEPAGE should be an integer specifying the codepage that should be used
2722to display the character set, t to do no translation and output as Unicode,
2723or nil to do no translation and output as 8 bit (or multibyte on far-east
2724versions of Windows) characters. */);
2725 Vw32_charset_info_alist = Qnil;
2726
2727 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
2728 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
2729 DEFSYM (Qw32_charset_default, "w32-charset-default");
2730 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
2731 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
2732 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
2733 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
2734 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
2735 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
2736 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
2737 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
2738 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
2739 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
2740 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
2741 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
2742 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
2743 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
2744 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
2745 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
2746
6fe9826d
JR
2747 defsubr (&Sx_select_font);
2748
8eac0c84 2749 w32font_driver.type = Qgdi;
f7a84cb4
JR
2750 register_font_driver (&w32font_driver, NULL);
2751}
db4f02f2
EZ
2752
2753void
2754globals_of_w32font (void)
2755{
2756 g_b_init_is_w9x = 0;
2757 g_b_init_get_outline_metrics_w = 0;
2758 g_b_init_get_text_metrics_w = 0;
2759 g_b_init_get_glyph_outline_w = 0;
579ca67b 2760 g_b_init_get_char_width_32_w = 0;
db4f02f2 2761}