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