Fix memory leak in fontset handling
[emacs.git] / src / w32font.c
blob1c2f9665037849322907108d02a4199e057a5455
1 /* Font backend for the Microsoft Windows API.
2 Copyright (C) 2007-2015 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
19 #include <config.h>
20 #include <windows.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <ctype.h>
24 #include <commdlg.h>
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
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.
44 #ifndef CLEARTYPE_QUALITY
45 #define CLEARTYPE_QUALITY 5
46 #endif
47 #ifndef CLEARTYPE_NATURAL_QUALITY
48 #define CLEARTYPE_NATURAL_QUALITY 6
49 #endif
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
60 static void fill_in_logfont (struct frame *, LOGFONT *, Lisp_Object);
62 static BYTE w32_antialias_type (Lisp_Object);
63 static Lisp_Object lispy_antialias_type (BYTE);
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 *);
70 static Lisp_Object w32_registry (LONG, DWORD);
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);
83 /* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
84 of what we really want. */
85 struct font_callback_data
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 bool opentype_only;
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 *);
105 #ifdef WINDOWSNT
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;
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);
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)
145 return maybe_load_unicows_dll ();
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)
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)
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");
164 eassert (s_pfn_Get_Outline_Text_MetricsW != NULL);
165 return s_pfn_Get_Outline_Text_MetricsW (hdc, cbData, lpotmw);
168 static BOOL WINAPI
169 get_text_metrics_w(HDC hdc, LPTEXTMETRICW lptmw)
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)
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");
181 eassert (s_pfn_Get_Text_MetricsW != NULL);
182 return s_pfn_Get_Text_MetricsW (hdc, lptmw);
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)
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)
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");
199 eassert (s_pfn_Get_Glyph_OutlineW != NULL);
200 return s_pfn_Get_Glyph_OutlineW (hdc, uChar, uFormat, lpgm, cbBuffer,
201 lpvBuffer, lpmat2);
204 static DWORD WINAPI
205 get_char_width_32_w (HDC hdc, UINT uFirstChar, UINT uLastChar, LPINT lpBuffer)
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)
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");
217 eassert (s_pfn_Get_Char_Width_32W != NULL);
218 return s_pfn_Get_Char_Width_32W (hdc, uFirstChar, uLastChar, lpBuffer);
221 #else /* Cygwin */
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)
231 #endif /* Cygwin */
233 static int
234 memq_no_quit (Lisp_Object elt, Lisp_Object list)
236 while (CONSP (list) && ! EQ (XCAR (list), elt))
237 list = XCDR (list);
238 return (CONSP (list));
241 Lisp_Object
242 intern_font_name (char * string)
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);
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)
258 struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
260 return (dpyinfo->name_list_element);
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)
270 Lisp_Object fonts = w32font_list_internal (f, font_spec, 0);
271 FONT_ADD_LOG ("w32font-list", font_spec, fonts);
272 return fonts;
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)
282 Lisp_Object entity = w32font_match_internal (f, font_spec, 0);
283 FONT_ADD_LOG ("w32font-match", font_spec, entity);
284 return entity;
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)
293 Lisp_Object list = Qnil;
294 LOGFONT font_match_pattern;
295 HDC dc;
297 memset (&font_match_pattern, 0, sizeof (font_match_pattern));
298 font_match_pattern.lfCharSet = DEFAULT_CHARSET;
300 dc = get_frame_dc (f);
302 EnumFontFamiliesEx (dc, &font_match_pattern,
303 (FONTENUMPROC) add_font_name_to_list,
304 (LPARAM) &list, 0);
305 release_frame_dc (f, dc);
307 return list;
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)
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);
322 ASET (font_object, FONT_TYPE_INDEX, Qgdi);
324 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
326 return Qnil;
329 /* GDI backend does not use glyph indices. */
330 w32_font->glyph_idx = 0;
332 return font_object;
335 /* w32 implementation of close for font_backend. */
336 void
337 w32font_close (struct font *font)
339 struct w32font_info *w32_font = (struct w32font_info *) font;
341 if (w32_font->hfont)
343 /* Delete the GDI font object. */
344 DeleteObject (w32_font->hfont);
345 w32_font->hfont = NULL;
347 /* Free all the cached metrics. */
348 if (w32_font->cached_metrics)
350 int i;
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;
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. */
366 w32font_has_char (Lisp_Object entity, int c)
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;
373 #if 0
374 Lisp_Object supported_scripts, extra, script;
375 DWORD mask;
377 extra = AREF (entity, FONT_EXTRA_INDEX);
378 if (!CONSP (extra))
379 return -1;
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;
387 supported_scripts = XCDR (supported_scripts);
389 script = CHAR_TABLE_REF (Vchar_script_table, c);
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;
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
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.
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.
414 static unsigned
415 w32font_encode_char (struct font *font, int c)
417 struct w32font_info * w32_font = (struct w32font_info *)font;
419 if (c < w32_font->metrics.tmFirstChar
420 || c > w32_font->metrics.tmLastChar)
421 return FONT_INVALID_CODE;
422 else
423 return c;
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)
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 bool first;
444 struct w32font_info *w32_font = (struct w32font_info *) font;
446 memset (metrics, 0, sizeof (struct font_metrics));
448 for (i = 0, first = true; i < nglyphs; i++)
450 struct w32_metric_cache *char_metric;
451 int block = *(code + i) / CACHE_BLOCKSIZE;
452 int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
454 if (block >= w32_font->n_cache_blocks)
456 if (!w32_font->cached_metrics)
457 w32_font->cached_metrics
458 = xmalloc ((block + 1)
459 * sizeof (struct w32_metric_cache *));
460 else
461 w32_font->cached_metrics
462 = xrealloc (w32_font->cached_metrics,
463 (block + 1)
464 * sizeof (struct w32_metric_cache *));
465 memset (w32_font->cached_metrics + w32_font->n_cache_blocks, 0,
466 ((block + 1 - w32_font->n_cache_blocks)
467 * sizeof (struct w32_metric_cache *)));
468 w32_font->n_cache_blocks = block + 1;
471 if (!w32_font->cached_metrics[block])
473 w32_font->cached_metrics[block]
474 = xzalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
477 char_metric = w32_font->cached_metrics[block] + pos_in_block;
479 if (char_metric->status == W32METRIC_NO_ATTEMPT)
481 if (dc == NULL)
483 /* TODO: Frames can come and go, and their fonts
484 outlive them. So we can't cache the frame in the
485 font structure. Use selected_frame until the API
486 is updated to pass in a frame. */
487 f = XFRAME (selected_frame);
489 dc = get_frame_dc (f);
490 old_font = SelectObject (dc, w32_font->hfont);
492 compute_metrics (dc, w32_font, *(code + i), char_metric);
495 if (char_metric->status == W32METRIC_SUCCESS)
497 if (first)
499 metrics->lbearing = char_metric->lbearing;
500 metrics->rbearing = char_metric->rbearing;
501 metrics->width = 0;
502 metrics->ascent = char_metric->ascent;
503 metrics->descent = char_metric->descent;
504 first = false;
506 if (metrics->lbearing > char_metric->lbearing)
507 metrics->lbearing = char_metric->lbearing;
508 if (metrics->rbearing < char_metric->rbearing)
509 metrics->rbearing = char_metric->rbearing;
510 metrics->width += char_metric->width;
511 if (metrics->ascent < char_metric->ascent)
512 metrics->ascent = char_metric->ascent;
513 if (metrics->descent < char_metric->descent)
514 metrics->descent = char_metric->descent;
516 else
517 /* If we couldn't get metrics for a char,
518 use alternative method. */
519 break;
521 /* If we got through everything, return. */
522 if (i == nglyphs)
524 if (dc != NULL)
526 /* Restore state and release DC. */
527 SelectObject (dc, old_font);
528 release_frame_dc (f, dc);
530 return;
533 /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
534 fallback on other methods that will at least give some of the metric
535 information. */
537 /* Make array big enough to hold surrogates. */
538 wcode = alloca (nglyphs * sizeof (WORD) * 2);
539 for (i = 0; i < nglyphs; i++)
541 if (code[i] < 0x10000)
542 wcode[i] = code[i];
543 else
545 DWORD surrogate = code[i] - 0x10000;
547 /* High surrogate: U+D800 - U+DBFF. */
548 wcode[i++] = 0xD800 + ((surrogate >> 10) & 0x03FF);
549 /* Low surrogate: U+DC00 - U+DFFF. */
550 wcode[i] = 0xDC00 + (surrogate & 0x03FF);
551 /* An extra glyph. wcode is already double the size of code to
552 cope with this. */
553 nglyphs++;
557 if (dc == NULL)
559 /* TODO: Frames can come and go, and their fonts outlive
560 them. So we can't cache the frame in the font structure. Use
561 selected_frame until the API is updated to pass in a
562 frame. */
563 f = XFRAME (selected_frame);
565 dc = get_frame_dc (f);
566 old_font = SelectObject (dc, w32_font->hfont);
569 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
571 total_width = size.cx;
574 /* On 95/98/ME, only some Unicode functions are available, so fallback
575 on doing a dummy draw to find the total width. */
576 if (!total_width)
578 RECT rect;
579 rect.top = 0; rect.bottom = font->height; rect.left = 0; rect.right = 1;
580 DrawTextW (dc, wcode, nglyphs, &rect,
581 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
582 total_width = rect.right;
585 /* Give our best estimate of the metrics, based on what we know. */
586 metrics->width = total_width - w32_font->metrics.tmOverhang;
587 metrics->lbearing = 0;
588 metrics->rbearing = total_width;
589 metrics->ascent = font->ascent;
590 metrics->descent = font->descent;
592 /* Restore state and release DC. */
593 SelectObject (dc, old_font);
594 release_frame_dc (f, dc);
597 /* w32 implementation of draw for font backend.
598 Optional.
599 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
600 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND,
601 fill the background in advance. It is assured that WITH_BACKGROUND
602 is false when (FROM > 0 || TO < S->nchars).
604 TODO: Currently this assumes that the colors and fonts are already
605 set in the DC. This seems to be true now, but maybe only due to
606 the old font code setting it up. It may be safer to resolve faces
607 and fonts in here and set them explicitly
611 w32font_draw (struct glyph_string *s, int from, int to,
612 int x, int y, bool with_background)
614 UINT options;
615 HRGN orig_clip = NULL;
616 int len = to - from;
617 struct w32font_info *w32font = (struct w32font_info *) s->font;
619 options = w32font->glyph_idx;
621 if (s->num_clips > 0)
623 HRGN new_clip = CreateRectRgnIndirect (s->clip);
625 /* Save clip region for later restoration. */
626 orig_clip = CreateRectRgn (0, 0, 0, 0);
627 if (!GetClipRgn (s->hdc, orig_clip))
629 DeleteObject (orig_clip);
630 orig_clip = NULL;
633 if (s->num_clips > 1)
635 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
637 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
638 DeleteObject (clip2);
641 SelectClipRgn (s->hdc, new_clip);
642 DeleteObject (new_clip);
645 /* Using OPAQUE background mode can clear more background than expected
646 when Cleartype is used. Draw the background manually to avoid this. */
647 SetBkMode (s->hdc, TRANSPARENT);
648 if (with_background)
650 HBRUSH brush;
651 RECT rect;
652 struct font *font = s->font;
653 int ascent = font->ascent, descent = font->descent;
655 /* Font's global ascent and descent values might be
656 preposterously large for some fonts. We fix here the case
657 when those fonts are used for display of glyphless
658 characters, because drawing background with font dimensions
659 in those cases makes the display illegible. There's only one
660 more call to the draw method with with_background set to
661 true, and that's in x_draw_glyph_string_foreground, when
662 drawing the cursor, where we have no such heuristics
663 available. FIXME. */
664 if (s->first_glyph->type == GLYPHLESS_GLYPH
665 && (s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE
666 || s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM))
668 ascent =
669 s->first_glyph->slice.glyphless.lower_yoff
670 - s->first_glyph->slice.glyphless.upper_yoff;
671 descent = 0;
673 brush = CreateSolidBrush (s->gc->background);
674 rect.left = x;
675 rect.top = y - ascent;
676 rect.right = x + s->width;
677 rect.bottom = y + descent;
678 FillRect (s->hdc, &rect, brush);
679 DeleteObject (brush);
682 if (s->padding_p)
684 int i;
686 for (i = 0; i < len; i++)
687 ExtTextOutW (s->hdc, x + i, y, options, NULL,
688 s->char2b + from + i, 1, NULL);
690 else
691 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, len, NULL);
693 /* Restore clip region. */
694 if (s->num_clips > 0)
695 SelectClipRgn (s->hdc, orig_clip);
697 if (orig_clip)
698 DeleteObject (orig_clip);
700 return len;
703 /* w32 implementation of free_entity for font backend.
704 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
705 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
706 static void
707 w32font_free_entity (Lisp_Object entity);
710 /* w32 implementation of prepare_face for font backend.
711 Optional (if FACE->extra is not used).
712 Prepare FACE for displaying characters by FONT on frame F by
713 storing some data in FACE->extra. If successful, return 0.
714 Otherwise, return -1.
715 static int
716 w32font_prepare_face (struct frame *f, struct face *face);
718 /* w32 implementation of done_face for font backend.
719 Optional.
720 Done FACE for displaying characters by FACE->font on frame F.
721 static void
722 w32font_done_face (struct frame *f, struct face *face); */
724 /* w32 implementation of get_bitmap for font backend.
725 Optional.
726 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
727 intended that this method is called from the other font-driver
728 for actual drawing.
729 static int
730 w32font_get_bitmap (struct font *font, unsigned code,
731 struct font_bitmap *bitmap, int bits_per_pixel);
733 /* w32 implementation of free_bitmap for font backend.
734 Optional.
735 Free bitmap data in BITMAP.
736 static void
737 w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
739 /* w32 implementation of anchor_point for font backend.
740 Optional.
741 Get coordinates of the INDEXth anchor point of the glyph whose
742 code is CODE. Store the coordinates in *X and *Y. Return 0 if
743 the operations was successful. Otherwise return -1.
744 static int
745 w32font_anchor_point (struct font *font, unsigned code,
746 int index, int *x, int *y);
748 /* w32 implementation of otf_capability for font backend.
749 Optional.
750 Return a list describing which scripts/languages FONT
751 supports by which GSUB/GPOS features of OpenType tables.
752 static Lisp_Object
753 w32font_otf_capability (struct font *font);
755 /* w32 implementation of otf_drive for font backend.
756 Optional.
757 Apply FONT's OTF-FEATURES to the glyph string.
759 FEATURES specifies which OTF features to apply in this format:
760 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
761 See the documentation of `font-drive-otf' for the detail.
763 This method applies the specified features to the codes in the
764 elements of GSTRING-IN (between FROMth and TOth). The output
765 codes are stored in GSTRING-OUT at the IDXth element and the
766 following elements.
768 Return the number of output codes. If none of the features are
769 applicable to the input data, return 0. If GSTRING-OUT is too
770 short, return -1.
771 static int
772 w32font_otf_drive (struct font *font, Lisp_Object features,
773 Lisp_Object gstring_in, int from, int to,
774 Lisp_Object gstring_out, int idx,
775 bool alternate_subst);
778 /* Internal implementation of w32font_list.
779 Additional parameter opentype_only restricts the returned fonts to
780 opentype fonts, which can be used with the Uniscribe backend. */
781 Lisp_Object
782 w32font_list_internal (struct frame *f, Lisp_Object font_spec,
783 bool opentype_only)
785 struct font_callback_data match_data;
786 HDC dc;
788 match_data.orig_font_spec = font_spec;
789 match_data.list = Qnil;
790 XSETFRAME (match_data.frame, f);
792 memset (&match_data.pattern, 0, sizeof (LOGFONT));
793 fill_in_logfont (f, &match_data.pattern, font_spec);
795 /* If the charset is unrecognized, then we won't find a font, so don't
796 waste time looking for one. */
797 if (match_data.pattern.lfCharSet == DEFAULT_CHARSET)
799 Lisp_Object spec_charset = AREF (font_spec, FONT_REGISTRY_INDEX);
800 if (!NILP (spec_charset)
801 && !EQ (spec_charset, Qiso10646_1)
802 && !EQ (spec_charset, Qunicode_bmp)
803 && !EQ (spec_charset, Qunicode_sip)
804 && !EQ (spec_charset, Qunknown))
805 return Qnil;
808 match_data.opentype_only = opentype_only;
809 if (opentype_only)
810 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
812 if (match_data.pattern.lfFaceName[0] == '\0')
814 /* EnumFontFamiliesEx does not take other fields into account if
815 font name is blank, so need to use two passes. */
816 list_all_matching_fonts (&match_data);
818 else
820 dc = get_frame_dc (f);
822 EnumFontFamiliesEx (dc, &match_data.pattern,
823 (FONTENUMPROC) add_font_entity_to_list,
824 (LPARAM) &match_data, 0);
825 release_frame_dc (f, dc);
828 return match_data.list;
831 /* Internal implementation of w32font_match.
832 Additional parameter opentype_only restricts the returned fonts to
833 opentype fonts, which can be used with the Uniscribe backend. */
834 Lisp_Object
835 w32font_match_internal (struct frame *f, Lisp_Object font_spec,
836 bool opentype_only)
838 struct font_callback_data match_data;
839 HDC dc;
841 match_data.orig_font_spec = font_spec;
842 XSETFRAME (match_data.frame, f);
843 match_data.list = Qnil;
845 memset (&match_data.pattern, 0, sizeof (LOGFONT));
846 fill_in_logfont (f, &match_data.pattern, font_spec);
848 match_data.opentype_only = opentype_only;
849 if (opentype_only)
850 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
852 dc = get_frame_dc (f);
854 EnumFontFamiliesEx (dc, &match_data.pattern,
855 (FONTENUMPROC) add_one_font_entity_to_list,
856 (LPARAM) &match_data, 0);
857 release_frame_dc (f, dc);
859 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
863 w32font_open_internal (struct frame *f, Lisp_Object font_entity,
864 int pixel_size, Lisp_Object font_object)
866 int len, size;
867 LOGFONT logfont;
868 HDC dc;
869 HFONT hfont, old_font;
870 Lisp_Object val;
871 struct w32font_info *w32_font;
872 struct font * font;
873 OUTLINETEXTMETRICW* metrics = NULL;
875 w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
876 font = (struct font *) w32_font;
878 if (!font)
879 return 0;
881 memset (&logfont, 0, sizeof (logfont));
882 fill_in_logfont (f, &logfont, font_entity);
884 /* Prefer truetype fonts, to avoid known problems with type1 fonts, and
885 limitations in bitmap fonts. */
886 val = AREF (font_entity, FONT_FOUNDRY_INDEX);
887 if (!EQ (val, Qraster))
888 logfont.lfOutPrecision = OUT_TT_PRECIS;
890 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
891 if (!size)
892 size = pixel_size;
894 logfont.lfHeight = -size;
895 hfont = CreateFontIndirect (&logfont);
897 if (hfont == NULL)
898 return 0;
900 /* Get the metrics for this font. */
901 dc = get_frame_dc (f);
902 old_font = SelectObject (dc, hfont);
904 /* Try getting the outline metrics (only works for truetype fonts). */
905 len = get_outline_metrics_w (dc, 0, NULL);
906 if (len)
908 metrics = (OUTLINETEXTMETRICW *) alloca (len);
909 if (get_outline_metrics_w (dc, len, metrics))
910 memcpy (&w32_font->metrics, &metrics->otmTextMetrics,
911 sizeof (TEXTMETRICW));
912 else
913 metrics = NULL;
916 if (!metrics)
917 get_text_metrics_w (dc, &w32_font->metrics);
919 w32_font->cached_metrics = NULL;
920 w32_font->n_cache_blocks = 0;
922 SelectObject (dc, old_font);
923 release_frame_dc (f, dc);
925 w32_font->hfont = hfont;
928 char *name;
930 /* We don't know how much space we need for the full name, so start with
931 96 bytes and go up in steps of 32. */
932 len = 96;
933 name = alloca (len);
934 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
935 name, len) < 0)
937 len += 32;
938 name = alloca (len);
940 if (name)
941 font->props[FONT_FULLNAME_INDEX]
942 = DECODE_SYSTEM (build_string (name));
943 else
944 font->props[FONT_FULLNAME_INDEX]
945 = DECODE_SYSTEM (build_string (logfont.lfFaceName));
948 font->max_width = w32_font->metrics.tmMaxCharWidth;
949 /* Parts of Emacs display assume that height = ascent + descent...
950 so height is defined later, after ascent and descent.
951 font->height = w32_font->metrics.tmHeight
952 + w32_font->metrics.tmExternalLeading;
955 font->space_width = font->average_width = w32_font->metrics.tmAveCharWidth;
957 font->vertical_centering = 0;
958 font->baseline_offset = 0;
959 font->relative_compose = 0;
960 font->default_ascent = w32_font->metrics.tmAscent;
961 font->pixel_size = size;
962 font->driver = &w32font_driver;
963 font->encoding_charset = -1;
964 font->repertory_charset = -1;
965 /* TODO: do we really want the minimum width here, which could be negative? */
966 font->min_width = font->space_width;
967 font->ascent = w32_font->metrics.tmAscent;
968 font->descent = w32_font->metrics.tmDescent;
969 font->height = font->ascent + font->descent;
971 if (metrics)
973 font->underline_thickness = metrics->otmsUnderscoreSize;
974 font->underline_position = -metrics->otmsUnderscorePosition;
976 else
978 font->underline_thickness = 0;
979 font->underline_position = -1;
982 /* For temporary compatibility with legacy code that expects the
983 name to be usable in x-list-fonts. Eventually we expect to change
984 x-list-fonts and other places that use fonts so that this can be
985 an fcname or similar. */
986 font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
988 return 1;
991 /* Callback function for EnumFontFamiliesEx.
992 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
993 static int CALLBACK ALIGN_STACK
994 add_font_name_to_list (ENUMLOGFONTEX *logical_font,
995 NEWTEXTMETRICEX *physical_font,
996 DWORD font_type, LPARAM list_object)
998 Lisp_Object* list = (Lisp_Object *) list_object;
999 Lisp_Object family;
1001 /* Skip vertical fonts (intended only for printing) */
1002 if (logical_font->elfLogFont.lfFaceName[0] == '@')
1003 return 1;
1005 family = intern_font_name (logical_font->elfLogFont.lfFaceName);
1006 if (! memq_no_quit (family, *list))
1007 *list = Fcons (family, *list);
1009 return 1;
1012 static int w32_decode_weight (int);
1013 static int w32_encode_weight (int);
1015 /* Convert an enumerated Windows font to an Emacs font entity. */
1016 static Lisp_Object
1017 w32_enumfont_pattern_entity (Lisp_Object frame,
1018 ENUMLOGFONTEX *logical_font,
1019 NEWTEXTMETRICEX *physical_font,
1020 DWORD font_type,
1021 LOGFONT *requested_font,
1022 Lisp_Object backend)
1024 Lisp_Object entity, tem;
1025 LOGFONT *lf = (LOGFONT*) logical_font;
1026 BYTE generic_type;
1027 DWORD full_type = physical_font->ntmTm.ntmFlags;
1029 entity = font_make_entity ();
1031 ASET (entity, FONT_TYPE_INDEX, backend);
1032 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
1033 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
1035 /* Foundry is difficult to get in readable form on Windows.
1036 But Emacs crashes if it is not set, so set it to something more
1037 generic. These values make xlfds compatible with Emacs 22. */
1038 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
1039 tem = Qraster;
1040 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
1041 tem = Qoutline;
1042 else
1043 tem = Qunknown;
1045 ASET (entity, FONT_FOUNDRY_INDEX, tem);
1047 /* Save the generic family in the extra info, as it is likely to be
1048 useful to users looking for a close match. */
1049 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
1050 if (generic_type == FF_DECORATIVE)
1051 tem = Qdecorative;
1052 else if (generic_type == FF_MODERN)
1053 tem = Qmono;
1054 else if (generic_type == FF_ROMAN)
1055 tem = Qserif;
1056 else if (generic_type == FF_SCRIPT)
1057 tem = Qscript;
1058 else if (generic_type == FF_SWISS)
1059 tem = Qsans;
1060 else
1061 tem = Qnil;
1063 ASET (entity, FONT_ADSTYLE_INDEX, tem);
1065 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
1066 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_PROPORTIONAL));
1067 else
1068 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_CHARCELL));
1070 if (requested_font->lfQuality != DEFAULT_QUALITY)
1072 font_put_extra (entity, QCantialias,
1073 lispy_antialias_type (requested_font->lfQuality));
1075 ASET (entity, FONT_FAMILY_INDEX,
1076 intern_font_name (lf->lfFaceName));
1078 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX,
1079 make_number (w32_decode_weight (lf->lfWeight)));
1080 FONT_SET_STYLE (entity, FONT_SLANT_INDEX,
1081 make_number (lf->lfItalic ? 200 : 100));
1082 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
1083 to get it. */
1084 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (100));
1086 if (font_type & RASTER_FONTTYPE)
1087 ASET (entity, FONT_SIZE_INDEX,
1088 make_number (physical_font->ntmTm.tmHeight
1089 + physical_font->ntmTm.tmExternalLeading));
1090 else
1091 ASET (entity, FONT_SIZE_INDEX, make_number (0));
1093 /* Cache Unicode codepoints covered by this font, as there is no other way
1094 of getting this information easily. */
1095 if (font_type & TRUETYPE_FONTTYPE)
1097 tem = font_supported_scripts (&physical_font->ntmFontSig);
1098 if (!NILP (tem))
1099 font_put_extra (entity, QCscript, tem);
1102 /* This information is not fully available when opening fonts, so
1103 save it here. Only Windows 2000 and later return information
1104 about opentype and type1 fonts, so need a fallback for detecting
1105 truetype so that this information is not any worse than we could
1106 have obtained later. */
1107 if (EQ (backend, Quniscribe) && (full_type & NTMFLAGS_OPENTYPE))
1108 tem = Qopentype;
1109 else if (font_type & TRUETYPE_FONTTYPE)
1110 tem = intern ("truetype");
1111 else if (full_type & NTM_PS_OPENTYPE)
1112 tem = Qpostscript;
1113 else if (full_type & NTM_TYPE1)
1114 tem = intern ("type1");
1115 else if (font_type & RASTER_FONTTYPE)
1116 tem = intern ("w32bitmap");
1117 else
1118 tem = intern ("w32vector");
1120 font_put_extra (entity, QCformat, tem);
1122 return entity;
1126 /* Convert generic families to the family portion of lfPitchAndFamily. */
1127 static BYTE
1128 w32_generic_family (Lisp_Object name)
1130 /* Generic families. */
1131 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1132 return FF_MODERN;
1133 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
1134 return FF_SWISS;
1135 else if (EQ (name, Qserif))
1136 return FF_ROMAN;
1137 else if (EQ (name, Qdecorative))
1138 return FF_DECORATIVE;
1139 else if (EQ (name, Qscript))
1140 return FF_SCRIPT;
1141 else
1142 return FF_DONTCARE;
1145 static int
1146 logfonts_match (LOGFONT *font, LOGFONT *pattern)
1148 /* Only check height for raster fonts. */
1149 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1150 && font->lfHeight != pattern->lfHeight)
1151 return 0;
1153 /* Have some flexibility with weights. */
1154 if (pattern->lfWeight
1155 && ((font->lfWeight < (pattern->lfWeight - 150))
1156 || font->lfWeight > (pattern->lfWeight + 150)))
1157 return 0;
1159 /* Charset and face should be OK. Italic has to be checked
1160 against the original spec, in case we don't have any preference. */
1161 return 1;
1164 /* Codepage Bitfields in FONTSIGNATURE struct. */
1165 #define CSB_JAPANESE (1 << 17)
1166 #define CSB_KOREAN ((1 << 19) | (1 << 21))
1167 #define CSB_CHINESE ((1 << 18) | (1 << 20))
1169 static int
1170 font_matches_spec (DWORD type, NEWTEXTMETRICEX *font,
1171 Lisp_Object spec, Lisp_Object backend,
1172 LOGFONT *logfont)
1174 Lisp_Object extra, val;
1176 /* Check italic. Can't check logfonts, since it is a boolean field,
1177 so there is no difference between "non-italic" and "don't care". */
1179 int slant = FONT_SLANT_NUMERIC (spec);
1181 if (slant >= 0
1182 && ((slant > 150 && !font->ntmTm.tmItalic)
1183 || (slant <= 150 && font->ntmTm.tmItalic)))
1184 return 0;
1187 /* Check adstyle against generic family. */
1188 val = AREF (spec, FONT_ADSTYLE_INDEX);
1189 if (!NILP (val))
1191 BYTE family = w32_generic_family (val);
1192 if (family != FF_DONTCARE
1193 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1194 return 0;
1197 /* Check spacing */
1198 val = AREF (spec, FONT_SPACING_INDEX);
1199 if (INTEGERP (val))
1201 int spacing = XINT (val);
1202 int proportional = (spacing < FONT_SPACING_MONO);
1204 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1205 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1206 return 0;
1209 /* Check extra parameters. */
1210 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1211 CONSP (extra); extra = XCDR (extra))
1213 Lisp_Object extra_entry;
1214 extra_entry = XCAR (extra);
1215 if (CONSP (extra_entry))
1217 Lisp_Object key = XCAR (extra_entry);
1219 val = XCDR (extra_entry);
1220 if (EQ (key, QCscript) && SYMBOLP (val))
1222 /* Only truetype fonts will have information about what
1223 scripts they support. This probably means the user
1224 will have to force Emacs to use raster, PostScript
1225 or ATM fonts for non-ASCII text. */
1226 if (type & TRUETYPE_FONTTYPE)
1228 Lisp_Object support
1229 = font_supported_scripts (&font->ntmFontSig);
1230 if (! memq_no_quit (val, support))
1231 return 0;
1233 /* Avoid using non-Japanese fonts for Japanese, even
1234 if they claim they are capable, due to known
1235 breakage in Vista and Windows 7 fonts
1236 (bug#6029). */
1237 if (EQ (val, Qkana)
1238 && (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET
1239 || !(font->ntmFontSig.fsCsb[0] & CSB_JAPANESE)))
1240 return 0;
1242 else
1244 /* Return specific matches, but play it safe. Fonts
1245 that cover more than their charset would suggest
1246 are likely to be truetype or opentype fonts,
1247 covered above. */
1248 if (EQ (val, Qlatin))
1250 /* Although every charset but symbol, thai and
1251 arabic contains the basic ASCII set of latin
1252 characters, Emacs expects much more. */
1253 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1254 return 0;
1256 else if (EQ (val, Qsymbol))
1258 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1259 return 0;
1261 else if (EQ (val, Qcyrillic))
1263 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1264 return 0;
1266 else if (EQ (val, Qgreek))
1268 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1269 return 0;
1271 else if (EQ (val, Qarabic))
1273 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1274 return 0;
1276 else if (EQ (val, Qhebrew))
1278 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1279 return 0;
1281 else if (EQ (val, Qthai))
1283 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1284 return 0;
1286 else if (EQ (val, Qkana))
1288 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1289 return 0;
1291 else if (EQ (val, Qbopomofo))
1293 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1294 return 0;
1296 else if (EQ (val, Qhangul))
1298 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1299 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1300 return 0;
1302 else if (EQ (val, Qhan))
1304 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1305 && font->ntmTm.tmCharSet != GB2312_CHARSET
1306 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1307 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1308 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1309 return 0;
1311 else
1312 /* Other scripts unlikely to be handled by non-truetype
1313 fonts. */
1314 return 0;
1317 else if (EQ (key, QClang) && SYMBOLP (val))
1319 /* Just handle the CJK languages here, as the lang
1320 parameter is used to select a font with appropriate
1321 glyphs in the cjk unified ideographs block. Other fonts
1322 support for a language can be solely determined by
1323 its character coverage. */
1324 if (EQ (val, Qja))
1326 if (!(font->ntmFontSig.fsCsb[0] & CSB_JAPANESE))
1327 return 0;
1329 else if (EQ (val, Qko))
1331 if (!(font->ntmFontSig.fsCsb[0] & CSB_KOREAN))
1332 return 0;
1334 else if (EQ (val, Qzh))
1336 if (!(font->ntmFontSig.fsCsb[0] & CSB_CHINESE))
1337 return 0;
1339 else
1340 /* Any other language, we don't recognize it. Only the above
1341 currently appear in fontset.el, so it isn't worth
1342 creating a mapping table of codepages/scripts to languages
1343 or opening the font to see if there are any language tags
1344 in it that the Windows API does not expose. Fontset
1345 spec should have a fallback, as some backends do
1346 not recognize language at all. */
1347 return 0;
1349 else if (EQ (key, QCotf) && CONSP (val))
1351 /* OTF features only supported by the uniscribe backend. */
1352 if (EQ (backend, Quniscribe))
1354 if (!uniscribe_check_otf (logfont, val))
1355 return 0;
1357 else
1358 return 0;
1362 return 1;
1365 static int
1366 w32font_coverage_ok (FONTSIGNATURE * coverage, BYTE charset)
1368 DWORD subrange1 = coverage->fsUsb[1];
1370 #define SUBRANGE1_HAN_MASK 0x08000000
1371 #define SUBRANGE1_HANGEUL_MASK 0x01000000
1372 #define SUBRANGE1_JAPANESE_MASK (0x00060000 | SUBRANGE1_HAN_MASK)
1374 if (charset == GB2312_CHARSET || charset == CHINESEBIG5_CHARSET)
1376 return (subrange1 & SUBRANGE1_HAN_MASK) == SUBRANGE1_HAN_MASK;
1378 else if (charset == SHIFTJIS_CHARSET)
1380 return (subrange1 & SUBRANGE1_JAPANESE_MASK) == SUBRANGE1_JAPANESE_MASK;
1382 else if (charset == HANGEUL_CHARSET)
1384 return (subrange1 & SUBRANGE1_HANGEUL_MASK) == SUBRANGE1_HANGEUL_MASK;
1387 return 1;
1390 #ifndef WINDOWSNT
1391 #define _strlwr strlwr
1392 #endif /* !WINDOWSNT */
1394 static int
1395 check_face_name (LOGFONT *font, char *full_name)
1397 char full_iname[LF_FULLFACESIZE+1];
1399 /* Just check for names known to cause problems, since the full name
1400 can contain expanded abbreviations, prefixed foundry, postfixed
1401 style, the latter of which sometimes differs from the style indicated
1402 in the shorter name (eg Lt becomes Light or even Extra Light) */
1404 /* Helvetica is mapped to Arial in Windows, but if a Type-1 Helvetica is
1405 installed, we run into problems with the Uniscribe backend which tries
1406 to avoid non-truetype fonts, and ends up mixing the Type-1 Helvetica
1407 with Arial's characteristics, since that attempt to use TrueType works
1408 some places, but not others. */
1409 if (!xstrcasecmp (font->lfFaceName, "helvetica"))
1411 strncpy (full_iname, full_name, LF_FULLFACESIZE);
1412 full_iname[LF_FULLFACESIZE] = 0;
1413 _strlwr (full_iname);
1414 return strstr ("helvetica", full_iname) != NULL;
1416 /* Same for Helv. */
1417 if (!xstrcasecmp (font->lfFaceName, "helv"))
1419 strncpy (full_iname, full_name, LF_FULLFACESIZE);
1420 full_iname[LF_FULLFACESIZE] = 0;
1421 _strlwr (full_iname);
1422 return strstr ("helv", full_iname) != NULL;
1425 /* Since Times is mapped to Times New Roman, a substring
1426 match is not sufficient to filter out the bogus match. */
1427 else if (!xstrcasecmp (font->lfFaceName, "times"))
1428 return xstrcasecmp (full_name, "times") == 0;
1430 return 1;
1434 /* Callback function for EnumFontFamiliesEx.
1435 * Checks if a font matches everything we are trying to check against,
1436 * and if so, adds it to a list. Both the data we are checking against
1437 * and the list to which the fonts are added are passed in via the
1438 * lparam argument, in the form of a font_callback_data struct. */
1439 static int CALLBACK ALIGN_STACK
1440 add_font_entity_to_list (ENUMLOGFONTEX *logical_font,
1441 NEWTEXTMETRICEX *physical_font,
1442 DWORD font_type, LPARAM lParam)
1444 struct font_callback_data *match_data
1445 = (struct font_callback_data *) lParam;
1446 Lisp_Object backend = match_data->opentype_only ? Quniscribe : Qgdi;
1447 Lisp_Object entity;
1449 int is_unicode = physical_font->ntmFontSig.fsUsb[3]
1450 || physical_font->ntmFontSig.fsUsb[2]
1451 || physical_font->ntmFontSig.fsUsb[1]
1452 || physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff;
1454 /* Skip non matching fonts. */
1456 /* For uniscribe backend, consider only truetype or opentype fonts
1457 that have some Unicode coverage. */
1458 if (match_data->opentype_only
1459 && ((!(physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
1460 && !(font_type & TRUETYPE_FONTTYPE))
1461 || !is_unicode))
1462 return 1;
1464 /* Ensure a match. */
1465 if (!logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1466 || !font_matches_spec (font_type, physical_font,
1467 match_data->orig_font_spec, backend,
1468 &logical_font->elfLogFont)
1469 || !w32font_coverage_ok (&physical_font->ntmFontSig,
1470 match_data->pattern.lfCharSet))
1471 return 1;
1473 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1474 We limit this to raster fonts, because the test can catch some
1475 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1476 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1477 therefore get through this test. Since full names can be prefixed
1478 by a foundry, we accept raster fonts if the font name is found
1479 anywhere within the full name. */
1480 if ((logical_font->elfLogFont.lfOutPrecision == OUT_STRING_PRECIS
1481 && !strstr (logical_font->elfFullName,
1482 logical_font->elfLogFont.lfFaceName))
1483 /* Check for well known substitutions that mess things up in the
1484 presence of Type-1 fonts of the same name. */
1485 || (!check_face_name (&logical_font->elfLogFont,
1486 logical_font->elfFullName)))
1487 return 1;
1489 /* Make a font entity for the font. */
1490 entity = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1491 physical_font, font_type,
1492 &match_data->pattern,
1493 backend);
1495 if (!NILP (entity))
1497 Lisp_Object spec_charset = AREF (match_data->orig_font_spec,
1498 FONT_REGISTRY_INDEX);
1500 /* iso10646-1 fonts must contain Unicode mapping tables. */
1501 if (EQ (spec_charset, Qiso10646_1))
1503 if (!is_unicode)
1504 return 1;
1506 /* unicode-bmp fonts must contain characters from the BMP. */
1507 else if (EQ (spec_charset, Qunicode_bmp))
1509 if (!physical_font->ntmFontSig.fsUsb[3]
1510 && !(physical_font->ntmFontSig.fsUsb[2] & 0xFFFFFF9E)
1511 && !(physical_font->ntmFontSig.fsUsb[1] & 0xE81FFFFF)
1512 && !(physical_font->ntmFontSig.fsUsb[0] & 0x007F001F))
1513 return 1;
1515 /* unicode-sip fonts must contain characters in Unicode plane 2.
1516 so look for bit 57 (surrogates) in the Unicode subranges, plus
1517 the bits for CJK ranges that include those characters. */
1518 else if (EQ (spec_charset, Qunicode_sip))
1520 if (!(physical_font->ntmFontSig.fsUsb[1] & 0x02000000)
1521 || !(physical_font->ntmFontSig.fsUsb[1] & 0x28000000))
1522 return 1;
1525 /* This font matches. */
1527 /* If registry was specified, ensure it is reported as the same. */
1528 if (!NILP (spec_charset))
1530 /* Avoid using non-Japanese fonts for Japanese, even if they
1531 claim they are capable, due to known breakage in Vista
1532 and Windows 7 fonts (bug#6029). */
1533 if (logical_font->elfLogFont.lfCharSet == SHIFTJIS_CHARSET
1534 && !(physical_font->ntmFontSig.fsCsb[0] & CSB_JAPANESE))
1535 return 1;
1536 else
1537 ASET (entity, FONT_REGISTRY_INDEX, spec_charset);
1539 /* Otherwise if using the uniscribe backend, report ANSI and DEFAULT
1540 fonts as Unicode and skip other charsets. */
1541 else if (match_data->opentype_only)
1543 if (logical_font->elfLogFont.lfCharSet == ANSI_CHARSET
1544 || logical_font->elfLogFont.lfCharSet == DEFAULT_CHARSET)
1545 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
1546 else
1547 return 1;
1550 /* Add this font to the list. */
1551 match_data->list = Fcons (entity, match_data->list);
1553 return 1;
1556 /* Callback function for EnumFontFamiliesEx.
1557 * Terminates the search once we have a match. */
1558 static int CALLBACK ALIGN_STACK
1559 add_one_font_entity_to_list (ENUMLOGFONTEX *logical_font,
1560 NEWTEXTMETRICEX *physical_font,
1561 DWORD font_type, LPARAM lParam)
1563 struct font_callback_data *match_data
1564 = (struct font_callback_data *) lParam;
1565 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1567 /* If we have a font in the list, terminate the search. */
1568 return NILP (match_data->list);
1571 /* Old function to convert from x to w32 charset, from w32fns.c. */
1572 static LONG
1573 x_to_w32_charset (char * lpcs)
1575 Lisp_Object this_entry, w32_charset;
1576 char *charset;
1577 int len = strlen (lpcs);
1579 /* Support "*-#nnn" format for unknown charsets. */
1580 if (strncmp (lpcs, "*-#", 3) == 0)
1581 return atoi (lpcs + 3);
1583 /* All Windows fonts qualify as Unicode. */
1584 if (!strncmp (lpcs, "iso10646", 8))
1585 return DEFAULT_CHARSET;
1587 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
1588 charset = alloca (len + 1);
1589 strcpy (charset, lpcs);
1590 lpcs = strchr (charset, '*');
1591 if (lpcs)
1592 *lpcs = '\0';
1594 /* Look through w32-charset-info-alist for the character set.
1595 Format of each entry is
1596 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1598 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
1600 if (NILP (this_entry))
1602 /* At startup, we want iso8859-1 fonts to come up properly. */
1603 if (xstrcasecmp (charset, "iso8859-1") == 0)
1604 return ANSI_CHARSET;
1605 else
1606 return DEFAULT_CHARSET;
1609 w32_charset = Fcar (Fcdr (this_entry));
1611 /* Translate Lisp symbol to number. */
1612 if (EQ (w32_charset, Qw32_charset_ansi))
1613 return ANSI_CHARSET;
1614 if (EQ (w32_charset, Qw32_charset_symbol))
1615 return SYMBOL_CHARSET;
1616 if (EQ (w32_charset, Qw32_charset_shiftjis))
1617 return SHIFTJIS_CHARSET;
1618 if (EQ (w32_charset, Qw32_charset_hangeul))
1619 return HANGEUL_CHARSET;
1620 if (EQ (w32_charset, Qw32_charset_chinesebig5))
1621 return CHINESEBIG5_CHARSET;
1622 if (EQ (w32_charset, Qw32_charset_gb2312))
1623 return GB2312_CHARSET;
1624 if (EQ (w32_charset, Qw32_charset_oem))
1625 return OEM_CHARSET;
1626 if (EQ (w32_charset, Qw32_charset_johab))
1627 return JOHAB_CHARSET;
1628 if (EQ (w32_charset, Qw32_charset_easteurope))
1629 return EASTEUROPE_CHARSET;
1630 if (EQ (w32_charset, Qw32_charset_turkish))
1631 return TURKISH_CHARSET;
1632 if (EQ (w32_charset, Qw32_charset_baltic))
1633 return BALTIC_CHARSET;
1634 if (EQ (w32_charset, Qw32_charset_russian))
1635 return RUSSIAN_CHARSET;
1636 if (EQ (w32_charset, Qw32_charset_arabic))
1637 return ARABIC_CHARSET;
1638 if (EQ (w32_charset, Qw32_charset_greek))
1639 return GREEK_CHARSET;
1640 if (EQ (w32_charset, Qw32_charset_hebrew))
1641 return HEBREW_CHARSET;
1642 if (EQ (w32_charset, Qw32_charset_vietnamese))
1643 return VIETNAMESE_CHARSET;
1644 if (EQ (w32_charset, Qw32_charset_thai))
1645 return THAI_CHARSET;
1646 if (EQ (w32_charset, Qw32_charset_mac))
1647 return MAC_CHARSET;
1649 return DEFAULT_CHARSET;
1653 /* Convert a Lisp font registry (symbol) to a windows charset. */
1654 static LONG
1655 registry_to_w32_charset (Lisp_Object charset)
1657 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1658 || EQ (charset, Qunicode_sip))
1659 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1660 else if (EQ (charset, Qiso8859_1))
1661 return ANSI_CHARSET;
1662 else if (SYMBOLP (charset))
1663 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
1664 else
1665 return DEFAULT_CHARSET;
1668 /* Old function to convert from w32 to x charset, from w32fns.c. */
1669 static char *
1670 w32_to_x_charset (int fncharset, char *matching)
1672 static char buf[32];
1673 Lisp_Object charset_type;
1674 int match_len = 0;
1676 if (matching)
1678 /* If fully specified, accept it as it is. Otherwise use a
1679 substring match. */
1680 char *wildcard = strchr (matching, '*');
1681 if (wildcard)
1682 *wildcard = '\0';
1683 else if (strchr (matching, '-'))
1684 return matching;
1686 match_len = strlen (matching);
1689 switch (fncharset)
1691 case ANSI_CHARSET:
1692 /* Handle startup case of w32-charset-info-alist not
1693 being set up yet. */
1694 if (NILP (Vw32_charset_info_alist))
1695 return "iso8859-1";
1696 charset_type = Qw32_charset_ansi;
1697 break;
1698 case DEFAULT_CHARSET:
1699 charset_type = Qw32_charset_default;
1700 break;
1701 case SYMBOL_CHARSET:
1702 charset_type = Qw32_charset_symbol;
1703 break;
1704 case SHIFTJIS_CHARSET:
1705 charset_type = Qw32_charset_shiftjis;
1706 break;
1707 case HANGEUL_CHARSET:
1708 charset_type = Qw32_charset_hangeul;
1709 break;
1710 case GB2312_CHARSET:
1711 charset_type = Qw32_charset_gb2312;
1712 break;
1713 case CHINESEBIG5_CHARSET:
1714 charset_type = Qw32_charset_chinesebig5;
1715 break;
1716 case OEM_CHARSET:
1717 charset_type = Qw32_charset_oem;
1718 break;
1719 case EASTEUROPE_CHARSET:
1720 charset_type = Qw32_charset_easteurope;
1721 break;
1722 case TURKISH_CHARSET:
1723 charset_type = Qw32_charset_turkish;
1724 break;
1725 case BALTIC_CHARSET:
1726 charset_type = Qw32_charset_baltic;
1727 break;
1728 case RUSSIAN_CHARSET:
1729 charset_type = Qw32_charset_russian;
1730 break;
1731 case ARABIC_CHARSET:
1732 charset_type = Qw32_charset_arabic;
1733 break;
1734 case GREEK_CHARSET:
1735 charset_type = Qw32_charset_greek;
1736 break;
1737 case HEBREW_CHARSET:
1738 charset_type = Qw32_charset_hebrew;
1739 break;
1740 case VIETNAMESE_CHARSET:
1741 charset_type = Qw32_charset_vietnamese;
1742 break;
1743 case THAI_CHARSET:
1744 charset_type = Qw32_charset_thai;
1745 break;
1746 case MAC_CHARSET:
1747 charset_type = Qw32_charset_mac;
1748 break;
1749 case JOHAB_CHARSET:
1750 charset_type = Qw32_charset_johab;
1751 break;
1753 default:
1754 /* Encode numerical value of unknown charset. */
1755 sprintf (buf, "*-#%u", fncharset);
1756 return buf;
1760 Lisp_Object rest;
1761 char * best_match = NULL;
1762 int matching_found = 0;
1764 /* Look through w32-charset-info-alist for the character set.
1765 Prefer ISO codepages, and prefer lower numbers in the ISO
1766 range. Only return charsets for codepages which are installed.
1768 Format of each entry is
1769 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1771 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
1773 char * x_charset;
1774 Lisp_Object w32_charset;
1775 Lisp_Object codepage;
1777 Lisp_Object this_entry = XCAR (rest);
1779 /* Skip invalid entries in alist. */
1780 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
1781 || !CONSP (XCDR (this_entry))
1782 || !SYMBOLP (XCAR (XCDR (this_entry))))
1783 continue;
1785 x_charset = SDATA (XCAR (this_entry));
1786 w32_charset = XCAR (XCDR (this_entry));
1787 codepage = XCDR (XCDR (this_entry));
1789 /* Look for Same charset and a valid codepage (or non-int
1790 which means ignore). */
1791 if (EQ (w32_charset, charset_type)
1792 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
1793 || IsValidCodePage (XINT (codepage))))
1795 /* If we don't have a match already, then this is the
1796 best. */
1797 if (!best_match)
1799 best_match = x_charset;
1800 if (matching && !strnicmp (x_charset, matching, match_len))
1801 matching_found = 1;
1803 /* If we already found a match for MATCHING, then
1804 only consider other matches. */
1805 else if (matching_found
1806 && strnicmp (x_charset, matching, match_len))
1807 continue;
1808 /* If this matches what we want, and the best so far doesn't,
1809 then this is better. */
1810 else if (!matching_found && matching
1811 && !strnicmp (x_charset, matching, match_len))
1813 best_match = x_charset;
1814 matching_found = 1;
1816 /* If this is fully specified, and the best so far isn't,
1817 then this is better. */
1818 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
1819 /* If this is an ISO codepage, and the best so far isn't,
1820 then this is better, but only if it fully specifies the
1821 encoding. */
1822 || (strnicmp (best_match, "iso", 3) != 0
1823 && strnicmp (x_charset, "iso", 3) == 0
1824 && strchr (x_charset, '-')))
1825 best_match = x_charset;
1826 /* If both are ISO8859 codepages, choose the one with the
1827 lowest number in the encoding field. */
1828 else if (strnicmp (best_match, "iso8859-", 8) == 0
1829 && strnicmp (x_charset, "iso8859-", 8) == 0)
1831 int best_enc = atoi (best_match + 8);
1832 int this_enc = atoi (x_charset + 8);
1833 if (this_enc > 0 && this_enc < best_enc)
1834 best_match = x_charset;
1839 /* If no match, encode the numeric value. */
1840 if (!best_match)
1842 sprintf (buf, "*-#%u", fncharset);
1843 return buf;
1846 strncpy (buf, best_match, 31);
1847 /* If the charset is not fully specified, put -0 on the end. */
1848 if (!strchr (best_match, '-'))
1850 int pos = strlen (best_match);
1851 /* Charset specifiers shouldn't be very long. If it is a made
1852 up one, truncating it should not do any harm since it isn't
1853 recognized anyway. */
1854 if (pos > 29)
1855 pos = 29;
1856 strcpy (buf + pos, "-0");
1858 buf[31] = '\0';
1859 return buf;
1863 static Lisp_Object
1864 w32_registry (LONG w32_charset, DWORD font_type)
1866 char *charset;
1868 /* If charset is defaulted, charset is Unicode or unknown, depending on
1869 font type. */
1870 if (w32_charset == DEFAULT_CHARSET)
1871 return font_type == TRUETYPE_FONTTYPE ? Qiso10646_1 : Qunknown;
1873 charset = w32_to_x_charset (w32_charset, NULL);
1874 return font_intern_prop (charset, strlen (charset), 1);
1877 static int
1878 w32_decode_weight (int fnweight)
1880 if (fnweight >= FW_HEAVY) return 210;
1881 if (fnweight >= FW_EXTRABOLD) return 205;
1882 if (fnweight >= FW_BOLD) return 200;
1883 if (fnweight >= FW_SEMIBOLD) return 180;
1884 if (fnweight >= FW_NORMAL) return 100;
1885 if (fnweight >= FW_LIGHT) return 50;
1886 if (fnweight >= FW_EXTRALIGHT) return 40;
1887 if (fnweight > FW_THIN) return 20;
1888 return 0;
1891 static int
1892 w32_encode_weight (int n)
1894 if (n >= 210) return FW_HEAVY;
1895 if (n >= 205) return FW_EXTRABOLD;
1896 if (n >= 200) return FW_BOLD;
1897 if (n >= 180) return FW_SEMIBOLD;
1898 if (n >= 100) return FW_NORMAL;
1899 if (n >= 50) return FW_LIGHT;
1900 if (n >= 40) return FW_EXTRALIGHT;
1901 if (n >= 20) return FW_THIN;
1902 return 0;
1905 /* Convert a Windows font weight into one of the weights supported
1906 by fontconfig (see font.c:font_parse_fcname). */
1907 static Lisp_Object
1908 w32_to_fc_weight (int n)
1910 if (n >= FW_EXTRABOLD) return intern ("black");
1911 if (n >= FW_BOLD) return Qbold;
1912 if (n >= FW_SEMIBOLD) return intern ("demibold");
1913 if (n >= FW_NORMAL) return intern ("medium");
1914 return Qlight;
1917 /* Fill in all the available details of LOGFONT from FONT_SPEC. */
1918 static void
1919 fill_in_logfont (struct frame *f, LOGFONT *logfont, Lisp_Object font_spec)
1921 Lisp_Object tmp, extra;
1922 int dpi = FRAME_RES_Y (f);
1924 tmp = AREF (font_spec, FONT_DPI_INDEX);
1925 if (INTEGERP (tmp))
1927 dpi = XINT (tmp);
1929 else if (FLOATP (tmp))
1931 dpi = (int) (XFLOAT_DATA (tmp) + 0.5);
1934 /* Height */
1935 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1936 if (INTEGERP (tmp))
1937 logfont->lfHeight = -1 * XINT (tmp);
1938 else if (FLOATP (tmp))
1939 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
1941 /* Escapement */
1943 /* Orientation */
1945 /* Weight */
1946 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1947 if (INTEGERP (tmp))
1948 logfont->lfWeight = w32_encode_weight (FONT_WEIGHT_NUMERIC (font_spec));
1950 /* Italic */
1951 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1952 if (INTEGERP (tmp))
1954 int slant = FONT_SLANT_NUMERIC (font_spec);
1955 logfont->lfItalic = slant > 150 ? 1 : 0;
1958 /* Underline */
1960 /* Strikeout */
1962 /* Charset */
1963 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1964 if (! NILP (tmp))
1965 logfont->lfCharSet = registry_to_w32_charset (tmp);
1966 else
1967 logfont->lfCharSet = DEFAULT_CHARSET;
1969 /* Out Precision */
1971 /* Clip Precision */
1973 /* Quality */
1974 logfont->lfQuality = DEFAULT_QUALITY;
1976 /* Generic Family and Face Name */
1977 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1979 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
1980 if (! NILP (tmp))
1982 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1983 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1984 ; /* Font name was generic, don't fill in font name. */
1985 /* Font families are interned, but allow for strings also in case of
1986 user input. */
1987 else if (SYMBOLP (tmp))
1989 strncpy (logfont->lfFaceName,
1990 SDATA (ENCODE_SYSTEM (SYMBOL_NAME (tmp))), LF_FACESIZE);
1991 logfont->lfFaceName[LF_FACESIZE-1] = '\0';
1995 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1996 if (!NILP (tmp))
1998 /* Override generic family. */
1999 BYTE family = w32_generic_family (tmp);
2000 if (family != FF_DONTCARE)
2001 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
2004 /* Set pitch based on the spacing property. */
2005 tmp = AREF (font_spec, FONT_SPACING_INDEX);
2006 if (INTEGERP (tmp))
2008 int spacing = XINT (tmp);
2009 if (spacing < FONT_SPACING_MONO)
2010 logfont->lfPitchAndFamily
2011 = (logfont->lfPitchAndFamily & 0xF0) | VARIABLE_PITCH;
2012 else
2013 logfont->lfPitchAndFamily
2014 = (logfont->lfPitchAndFamily & 0xF0) | FIXED_PITCH;
2017 /* Process EXTRA info. */
2018 for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
2019 CONSP (extra); extra = XCDR (extra))
2021 tmp = XCAR (extra);
2022 if (CONSP (tmp))
2024 Lisp_Object key, val;
2025 key = XCAR (tmp), val = XCDR (tmp);
2026 /* Only use QCscript if charset is not provided, or is Unicode
2027 and a single script is specified. This is rather crude,
2028 and is only used to narrow down the fonts returned where
2029 there is a definite match. Some scripts, such as latin, han,
2030 cjk-misc match multiple lfCharSet values, so we can't pre-filter
2031 them. */
2032 if (EQ (key, QCscript)
2033 && logfont->lfCharSet == DEFAULT_CHARSET
2034 && SYMBOLP (val))
2036 if (EQ (val, Qgreek))
2037 logfont->lfCharSet = GREEK_CHARSET;
2038 else if (EQ (val, Qhangul))
2039 logfont->lfCharSet = HANGUL_CHARSET;
2040 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
2041 logfont->lfCharSet = SHIFTJIS_CHARSET;
2042 else if (EQ (val, Qbopomofo))
2043 logfont->lfCharSet = CHINESEBIG5_CHARSET;
2044 /* GB 18030 supports tibetan, yi, mongolian,
2045 fonts that support it should show up if we ask for
2046 GB2312 fonts. */
2047 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
2048 || EQ (val, Qmongolian))
2049 logfont->lfCharSet = GB2312_CHARSET;
2050 else if (EQ (val, Qhebrew))
2051 logfont->lfCharSet = HEBREW_CHARSET;
2052 else if (EQ (val, Qarabic))
2053 logfont->lfCharSet = ARABIC_CHARSET;
2054 else if (EQ (val, Qthai))
2055 logfont->lfCharSet = THAI_CHARSET;
2057 else if (EQ (key, QCantialias) && SYMBOLP (val))
2059 logfont->lfQuality = w32_antialias_type (val);
2065 static void
2066 list_all_matching_fonts (struct font_callback_data *match_data)
2068 HDC dc;
2069 Lisp_Object families = w32font_list_family (XFRAME (match_data->frame));
2070 struct frame *f = XFRAME (match_data->frame);
2072 dc = get_frame_dc (f);
2074 while (!NILP (families))
2076 /* Only fonts from the current locale are given localized names
2077 on Windows, so we can keep backwards compatibility with
2078 Windows 9x/ME by using non-Unicode font enumeration without
2079 sacrificing internationalization here. */
2080 char *name;
2081 Lisp_Object family = CAR (families);
2082 families = CDR (families);
2083 if (NILP (family))
2084 continue;
2085 else if (SYMBOLP (family))
2086 name = SDATA (ENCODE_SYSTEM (SYMBOL_NAME (family)));
2087 else
2088 continue;
2090 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
2091 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
2093 EnumFontFamiliesEx (dc, &match_data->pattern,
2094 (FONTENUMPROC) add_font_entity_to_list,
2095 (LPARAM) match_data, 0);
2098 release_frame_dc (f, dc);
2101 static Lisp_Object
2102 lispy_antialias_type (BYTE type)
2104 Lisp_Object lispy;
2106 switch (type)
2108 case NONANTIALIASED_QUALITY:
2109 lispy = Qnone;
2110 break;
2111 case ANTIALIASED_QUALITY:
2112 lispy = Qstandard;
2113 break;
2114 case CLEARTYPE_QUALITY:
2115 lispy = Qsubpixel;
2116 break;
2117 case CLEARTYPE_NATURAL_QUALITY:
2118 lispy = Qnatural;
2119 break;
2120 default:
2121 lispy = Qnil;
2122 break;
2124 return lispy;
2127 /* Convert antialiasing symbols to lfQuality */
2128 static BYTE
2129 w32_antialias_type (Lisp_Object type)
2131 if (EQ (type, Qnone))
2132 return NONANTIALIASED_QUALITY;
2133 else if (EQ (type, Qstandard))
2134 return ANTIALIASED_QUALITY;
2135 else if (EQ (type, Qsubpixel))
2136 return CLEARTYPE_QUALITY;
2137 else if (EQ (type, Qnatural))
2138 return CLEARTYPE_NATURAL_QUALITY;
2139 else
2140 return DEFAULT_QUALITY;
2143 /* Return a list of all the scripts that the font supports. */
2144 static Lisp_Object
2145 font_supported_scripts (FONTSIGNATURE * sig)
2147 DWORD * subranges = sig->fsUsb;
2148 Lisp_Object supported = Qnil;
2150 /* Match a single subrange. SYM is set if bit N is set in subranges. */
2151 #define SUBRANGE(n,sym) \
2152 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
2153 supported = Fcons ((sym), supported)
2155 /* Match multiple subranges. SYM is set if any MASK bit is set in
2156 subranges[0 - 3]. */
2157 #define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
2158 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
2159 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
2160 supported = Fcons ((sym), supported)
2162 SUBRANGE (0, Qlatin);
2163 /* 1: Latin-1 supplement, 2: Latin Extended A, 3: Latin Extended B. */
2164 /* Most fonts that support Latin will have good coverage of the
2165 Extended blocks, so in practice marking them below is not really
2166 needed, or useful: if a font claims support for, say, Latin
2167 Extended-B, but does not contain glyphs for some of the
2168 characters in the range, the user will have to augment her
2169 fontset to display those few characters. But we mark these
2170 subranges here anyway, for the marginal use cases where they
2171 might make a difference. */
2172 SUBRANGE (1, Qlatin);
2173 SUBRANGE (2, Qlatin);
2174 SUBRANGE (3, Qlatin);
2175 SUBRANGE (4, Qphonetic);
2176 /* 5: Spacing and tone modifiers, 6: Combining Diacritical Marks. */
2177 SUBRANGE (7, Qgreek);
2178 SUBRANGE (8, Qcoptic);
2179 SUBRANGE (9, Qcyrillic);
2180 SUBRANGE (10, Qarmenian);
2181 SUBRANGE (11, Qhebrew);
2182 /* Bit 12 is rather useless if the user has Hebrew fonts installed,
2183 because apparently at some point in the past bit 12 was "Hebrew
2184 Extended", and many Hebrew fonts still have this bit set. The
2185 only workaround is to customize fontsets to use fonts like Ebrima
2186 or Quivira. */
2187 SUBRANGE (12, Qvai);
2188 SUBRANGE (13, Qarabic);
2189 SUBRANGE (14, Qnko);
2190 SUBRANGE (15, Qdevanagari);
2191 SUBRANGE (16, Qbengali);
2192 SUBRANGE (17, Qgurmukhi);
2193 SUBRANGE (18, Qgujarati);
2194 SUBRANGE (19, Qoriya);
2195 SUBRANGE (20, Qtamil);
2196 SUBRANGE (21, Qtelugu);
2197 SUBRANGE (22, Qkannada);
2198 SUBRANGE (23, Qmalayalam);
2199 SUBRANGE (24, Qthai);
2200 SUBRANGE (25, Qlao);
2201 SUBRANGE (26, Qgeorgian);
2202 SUBRANGE (27, Qbalinese);
2203 /* 28: Hangul Jamo -- covered by the default fontset. */
2204 /* 29: Latin Extended, 30: Greek Extended -- covered above. */
2205 /* 31: Supplemental Punctuation -- most probably be masked by
2206 Courier New, so fontset customization is needed. */
2207 SUBRANGE (31, Qsymbol);
2208 /* 32-47: Symbols (defined below). */
2209 SUBRANGE (48, Qcjk_misc);
2210 /* Match either 49: katakana or 50: hiragana for kana. */
2211 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
2212 SUBRANGE (51, Qbopomofo);
2213 /* 52: Compatibility Jamo */
2214 SUBRANGE (53, Qphags_pa);
2215 /* 54: Enclosed CJK letters and months, 55: CJK Compatibility. */
2216 SUBRANGE (56, Qhangul);
2217 /* 57: Surrogates. */
2218 SUBRANGE (58, Qphoenician);
2219 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
2220 SUBRANGE (59, Qideographic_description); /* Windows lumps this in. */
2221 SUBRANGE (59, Qkanbun); /* And this. */
2222 /* These are covered well either by the default Courier New or by
2223 CJK fonts that are set up specially in the default fontset. So
2224 marking them here wouldn't be useful. */
2225 /* 60: Private use, 61: CJK strokes and compatibility. */
2226 /* 62: Alphabetic Presentation, 63: Arabic Presentation A. */
2227 /* 64: Combining half marks, 65: Vertical and CJK compatibility. */
2228 /* 66: Small forms, 67: Arabic Presentation B, 68: Half and Full width. */
2229 /* 69: Specials. */
2230 SUBRANGE (70, Qtibetan);
2231 SUBRANGE (71, Qsyriac);
2232 SUBRANGE (72, Qthaana);
2233 SUBRANGE (73, Qsinhala);
2234 SUBRANGE (74, Qmyanmar);
2235 SUBRANGE (75, Qethiopic);
2236 SUBRANGE (76, Qcherokee);
2237 SUBRANGE (77, Qcanadian_aboriginal);
2238 SUBRANGE (78, Qogham);
2239 SUBRANGE (79, Qrunic);
2240 SUBRANGE (80, Qkhmer);
2241 SUBRANGE (81, Qmongolian);
2242 SUBRANGE (82, Qbraille);
2243 SUBRANGE (83, Qyi);
2244 SUBRANGE (84, Qbuhid);
2245 SUBRANGE (84, Qhanunoo);
2246 SUBRANGE (84, Qtagalog);
2247 SUBRANGE (84, Qtagbanwa);
2248 SUBRANGE (85, Qold_italic);
2249 SUBRANGE (86, Qgothic);
2250 SUBRANGE (87, Qdeseret);
2251 SUBRANGE (88, Qbyzantine_musical_symbol);
2252 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
2253 SUBRANGE (89, Qmathematical_bold); /* See fontset.el:setup-default-fontset. */
2254 SUBRANGE (89, Qmathematical_italic);
2255 SUBRANGE (89, Qmathematical_bold_italic);
2256 SUBRANGE (89, Qmathematical_script);
2257 SUBRANGE (89, Qmathematical_bold_script);
2258 SUBRANGE (89, Qmathematical_fraktur);
2259 SUBRANGE (89, Qmathematical_double_struck);
2260 SUBRANGE (89, Qmathematical_bold_fraktur);
2261 SUBRANGE (89, Qmathematical_sans_serif);
2262 SUBRANGE (89, Qmathematical_sans_serif_bold);
2263 SUBRANGE (89, Qmathematical_sans_serif_italic);
2264 SUBRANGE (89, Qmathematical_sans_serif_bold_italic);
2265 SUBRANGE (89, Qmathematical_monospace);
2266 /* 90: Private use, 91: Variation selectors, 92: Tags. */
2267 SUBRANGE (93, Qlimbu);
2268 SUBRANGE (94, Qtai_le);
2269 SUBRANGE (95, Qtai_le);
2270 SUBRANGE (96, Qbuginese);
2271 SUBRANGE (97, Qglagolitic);
2272 SUBRANGE (98, Qtifinagh);
2273 /* 99: Yijing Hexagrams. */
2274 SUBRANGE (99, Qhan);
2275 SUBRANGE (100, Qsyloti_nagri);
2276 SUBRANGE (101, Qlinear_b);
2277 SUBRANGE (102, Qancient_greek_number);
2278 SUBRANGE (103, Qugaritic);
2279 SUBRANGE (104, Qold_persian);
2280 SUBRANGE (105, Qshavian);
2281 SUBRANGE (106, Qosmanya);
2282 SUBRANGE (107, Qcypriot);
2283 SUBRANGE (108, Qkharoshthi);
2284 SUBRANGE (109, Qtai_xuan_jing_symbol);
2285 SUBRANGE (110, Qcuneiform);
2286 SUBRANGE (111, Qcounting_rod_numeral);
2287 SUBRANGE (112, Qsundanese);
2288 SUBRANGE (113, Qlepcha);
2289 SUBRANGE (114, Qol_chiki);
2290 SUBRANGE (115, Qsaurashtra);
2291 SUBRANGE (116, Qkayah_li);
2292 SUBRANGE (117, Qrejang);
2293 SUBRANGE (118, Qcham);
2294 SUBRANGE (119, Qancient_symbol);
2295 SUBRANGE (120, Qphaistos_disc);
2296 SUBRANGE (121, Qlycian);
2297 SUBRANGE (121, Qcarian);
2298 SUBRANGE (121, Qlydian);
2299 SUBRANGE (122, Qdomino_tile);
2300 SUBRANGE (122, Qmahjong_tile);
2301 /* 123-127: Reserved. */
2303 /* There isn't really a main symbol range, so include symbol if any
2304 relevant range is set. */
2305 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
2307 /* Missing: Tai Viet (U+AA80-U+AADF). */
2308 #undef SUBRANGE
2309 #undef MASK_ANY
2311 return supported;
2314 /* Generate a full name for a Windows font.
2315 The full name is in fcname format, with weight, slant and antialiasing
2316 specified if they are not "normal". */
2317 static int
2318 w32font_full_name (LOGFONT * font, Lisp_Object font_obj,
2319 int pixel_size, char *name, int nbytes)
2321 int len, height, outline;
2322 char *p;
2323 Lisp_Object antialiasing, weight = Qnil;
2325 len = strlen (font->lfFaceName);
2327 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
2329 /* Represent size of scalable fonts by point size. But use pixelsize for
2330 raster fonts to indicate that they are exactly that size. */
2331 if (outline)
2332 len += 11; /* -SIZE */
2333 else
2334 len += 21;
2336 if (font->lfItalic)
2337 len += 7; /* :italic */
2339 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2341 weight = w32_to_fc_weight (font->lfWeight);
2342 len += 1 + SBYTES (SYMBOL_NAME (weight)); /* :WEIGHT */
2345 antialiasing = lispy_antialias_type (font->lfQuality);
2346 if (! NILP (antialiasing))
2347 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
2349 /* Check that the buffer is big enough */
2350 if (len > nbytes)
2351 return -1;
2353 p = name;
2354 p += sprintf (p, "%s", font->lfFaceName);
2356 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
2358 if (height > 0)
2360 if (outline)
2362 float pointsize = height * 72.0 / one_w32_display_info.resy;
2363 /* Round to nearest half point. floor is used, since round is not
2364 supported in MS library. */
2365 pointsize = floor (pointsize * 2 + 0.5) / 2;
2366 p += sprintf (p, "-%1.1f", pointsize);
2368 else
2369 p += sprintf (p, ":pixelsize=%d", height);
2372 if (SYMBOLP (weight) && ! NILP (weight))
2373 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2375 if (font->lfItalic)
2376 p += sprintf (p, ":italic");
2378 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
2379 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
2381 return (p - name);
2384 /* Convert a logfont and point size into a fontconfig style font name.
2385 POINTSIZE is in tenths of points.
2386 If SIZE indicates the size of buffer FCNAME, into which the font name
2387 is written. If the buffer is not large enough to contain the name,
2388 the function returns -1, otherwise it returns the number of bytes
2389 written to FCNAME. */
2390 static int
2391 logfont_to_fcname (LOGFONT* font, int pointsize, char *fcname, int size)
2393 int len, height;
2394 char *p = fcname;
2395 Lisp_Object weight = Qnil;
2397 len = strlen (font->lfFaceName) + 2;
2398 height = pointsize / 10;
2399 while (height /= 10)
2400 len++;
2402 if (pointsize % 10)
2403 len += 2;
2405 if (font->lfItalic)
2406 len += 7; /* :italic */
2407 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2409 weight = w32_to_fc_weight (font->lfWeight);
2410 len += SBYTES (SYMBOL_NAME (weight)) + 1;
2413 if (len > size)
2414 return -1;
2416 p += sprintf (p, "%s-%d", font->lfFaceName, pointsize / 10);
2417 if (pointsize % 10)
2418 p += sprintf (p, ".%d", pointsize % 10);
2420 if (SYMBOLP (weight) && !NILP (weight))
2421 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2423 if (font->lfItalic)
2424 p += sprintf (p, ":italic");
2426 return (p - fcname);
2429 static void
2430 compute_metrics (HDC dc, struct w32font_info *w32_font, unsigned int code,
2431 struct w32_metric_cache *metrics)
2433 GLYPHMETRICS gm;
2434 MAT2 transform;
2435 unsigned int options = GGO_METRICS;
2436 INT width;
2438 if (w32_font->glyph_idx)
2439 options |= GGO_GLYPH_INDEX;
2441 memset (&transform, 0, sizeof (transform));
2442 transform.eM11.value = 1;
2443 transform.eM22.value = 1;
2445 if (get_glyph_outline_w (dc, code, options, &gm, 0, NULL, &transform)
2446 != GDI_ERROR)
2448 metrics->lbearing = gm.gmptGlyphOrigin.x;
2449 metrics->rbearing = gm.gmptGlyphOrigin.x + gm.gmBlackBoxX;
2450 metrics->width = gm.gmCellIncX;
2451 metrics->ascent = gm.gmptGlyphOrigin.y;
2452 metrics->descent = gm.gmBlackBoxY - gm.gmptGlyphOrigin.y;
2453 metrics->status = W32METRIC_SUCCESS;
2455 else if (get_char_width_32_w (dc, code, code, &width) != 0)
2457 metrics->lbearing = 0;
2458 metrics->rbearing = width;
2459 metrics->width = width;
2460 metrics->ascent = w32_font->font.ascent;
2461 metrics->descent = w32_font->font.descent;
2462 metrics->status = W32METRIC_SUCCESS;
2464 else
2465 metrics->status = W32METRIC_FAIL;
2468 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
2469 doc: /* Read a font name using a W32 font selection dialog.
2470 Return fontconfig style font string corresponding to the selection.
2472 If FRAME is omitted or nil, it defaults to the selected frame.
2473 If EXCLUDE-PROPORTIONAL is non-nil, exclude proportional fonts
2474 in the font selection dialog. */)
2475 (Lisp_Object frame, Lisp_Object exclude_proportional)
2477 struct frame *f = decode_window_system_frame (frame);
2478 CHOOSEFONT cf;
2479 LOGFONT lf;
2480 TEXTMETRIC tm;
2481 HDC hdc;
2482 HANDLE oldobj;
2483 char buf[100];
2485 memset (&cf, 0, sizeof (cf));
2486 memset (&lf, 0, sizeof (lf));
2488 cf.lStructSize = sizeof (cf);
2489 cf.hwndOwner = FRAME_W32_WINDOW (f);
2490 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
2492 /* If exclude_proportional is non-nil, limit the selection to
2493 monospaced fonts. */
2494 if (!NILP (exclude_proportional))
2495 cf.Flags |= CF_FIXEDPITCHONLY;
2497 cf.lpLogFont = &lf;
2499 /* Initialize as much of the font details as we can from the current
2500 default font. */
2501 hdc = GetDC (FRAME_W32_WINDOW (f));
2502 oldobj = SelectObject (hdc, FONT_HANDLE (FRAME_FONT (f)));
2503 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
2504 if (GetTextMetrics (hdc, &tm))
2506 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
2507 lf.lfWeight = tm.tmWeight;
2508 lf.lfItalic = tm.tmItalic;
2509 lf.lfUnderline = tm.tmUnderlined;
2510 lf.lfStrikeOut = tm.tmStruckOut;
2511 lf.lfCharSet = tm.tmCharSet;
2512 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
2514 SelectObject (hdc, oldobj);
2515 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
2517 if (!ChooseFont (&cf)
2518 || logfont_to_fcname (&lf, cf.iPointSize, buf, 100) < 0)
2519 return Qnil;
2521 return DECODE_SYSTEM (build_string (buf));
2524 static const char *const w32font_booleans [] = {
2525 NULL,
2528 static const char *const w32font_non_booleans [] = {
2529 ":script",
2530 ":antialias",
2531 ":style",
2532 NULL,
2535 static void
2536 w32font_filter_properties (Lisp_Object font, Lisp_Object alist)
2538 font_filter_properties (font, alist, w32font_booleans, w32font_non_booleans);
2541 struct font_driver w32font_driver =
2543 LISP_INITIALLY_ZERO, /* Qgdi */
2544 false, /* case insensitive */
2545 w32font_get_cache,
2546 w32font_list,
2547 w32font_match,
2548 w32font_list_family,
2549 NULL, /* free_entity */
2550 w32font_open,
2551 w32font_close,
2552 NULL, /* prepare_face */
2553 NULL, /* done_face */
2554 w32font_has_char,
2555 w32font_encode_char,
2556 w32font_text_extents,
2557 w32font_draw,
2558 NULL, /* get_bitmap */
2559 NULL, /* free_bitmap */
2560 NULL, /* anchor_point */
2561 NULL, /* otf_capability */
2562 NULL, /* otf_drive */
2563 NULL, /* start_for_frame */
2564 NULL, /* end_for_frame */
2565 NULL, /* shape */
2566 NULL, /* check */
2567 NULL, /* get_variation_glyphs */
2568 w32font_filter_properties,
2569 NULL, /* cached_font_ok */
2573 /* Initialize state that does not change between invocations. This is only
2574 called when Emacs is dumped. */
2575 void
2576 syms_of_w32font (void)
2578 DEFSYM (Qgdi, "gdi");
2579 DEFSYM (Quniscribe, "uniscribe");
2580 DEFSYM (QCformat, ":format");
2582 /* Generic font families. */
2583 DEFSYM (Qmonospace, "monospace");
2584 DEFSYM (Qserif, "serif");
2585 DEFSYM (Qsansserif, "sansserif");
2586 DEFSYM (Qscript, "script");
2587 DEFSYM (Qdecorative, "decorative");
2588 /* Aliases. */
2589 DEFSYM (Qsans_serif, "sans_serif");
2590 DEFSYM (Qsans, "sans");
2591 DEFSYM (Qmono, "mono");
2593 /* Fake foundries. */
2594 DEFSYM (Qraster, "raster");
2595 DEFSYM (Qoutline, "outline");
2596 DEFSYM (Qunknown, "unknown");
2598 /* Antialiasing. */
2599 DEFSYM (Qstandard, "standard");
2600 DEFSYM (Qsubpixel, "subpixel");
2601 DEFSYM (Qnatural, "natural");
2603 /* Languages */
2604 DEFSYM (Qzh, "zh");
2606 /* Scripts */
2607 DEFSYM (Qlatin, "latin");
2608 DEFSYM (Qgreek, "greek");
2609 DEFSYM (Qcoptic, "coptic");
2610 DEFSYM (Qcyrillic, "cyrillic");
2611 DEFSYM (Qarmenian, "armenian");
2612 DEFSYM (Qhebrew, "hebrew");
2613 DEFSYM (Qvai, "vai");
2614 DEFSYM (Qarabic, "arabic");
2615 DEFSYM (Qsyriac, "syriac");
2616 DEFSYM (Qnko, "nko");
2617 DEFSYM (Qthaana, "thaana");
2618 DEFSYM (Qdevanagari, "devanagari");
2619 DEFSYM (Qbengali, "bengali");
2620 DEFSYM (Qgurmukhi, "gurmukhi");
2621 DEFSYM (Qgujarati, "gujarati");
2622 DEFSYM (Qoriya, "oriya");
2623 DEFSYM (Qtamil, "tamil");
2624 DEFSYM (Qtelugu, "telugu");
2625 DEFSYM (Qkannada, "kannada");
2626 DEFSYM (Qmalayalam, "malayalam");
2627 DEFSYM (Qsinhala, "sinhala");
2628 DEFSYM (Qthai, "thai");
2629 DEFSYM (Qlao, "lao");
2630 DEFSYM (Qtibetan, "tibetan");
2631 DEFSYM (Qmyanmar, "myanmar");
2632 DEFSYM (Qgeorgian, "georgian");
2633 DEFSYM (Qhangul, "hangul");
2634 DEFSYM (Qethiopic, "ethiopic");
2635 DEFSYM (Qcherokee, "cherokee");
2636 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
2637 DEFSYM (Qogham, "ogham");
2638 DEFSYM (Qrunic, "runic");
2639 DEFSYM (Qkhmer, "khmer");
2640 DEFSYM (Qmongolian, "mongolian");
2641 DEFSYM (Qbraille, "braille");
2642 DEFSYM (Qhan, "han");
2643 DEFSYM (Qideographic_description, "ideographic-description");
2644 DEFSYM (Qcjk_misc, "cjk-misc");
2645 DEFSYM (Qkana, "kana");
2646 DEFSYM (Qbopomofo, "bopomofo");
2647 DEFSYM (Qkanbun, "kanbun");
2648 DEFSYM (Qyi, "yi");
2649 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
2650 DEFSYM (Qmusical_symbol, "musical-symbol");
2651 DEFSYM (Qmathematical_bold, "mathematical-bold");
2652 DEFSYM (Qmathematical_italic, "mathematical-italic");
2653 DEFSYM (Qmathematical_bold_italic, "mathematical-bold-italic");
2654 DEFSYM (Qmathematical_script, "mathematical-script");
2655 DEFSYM (Qmathematical_bold_script, "mathematical-bold-script");
2656 DEFSYM (Qmathematical_fraktur, "mathematical-fraktur");
2657 DEFSYM (Qmathematical_double_struck, "mathematical-double-struck");
2658 DEFSYM (Qmathematical_bold_fraktur, "mathematical-bold-fraktur");
2659 DEFSYM (Qmathematical_sans_serif, "mathematical-sans-serif");
2660 DEFSYM (Qmathematical_sans_serif_bold, "mathematical-sans-serif-bold");
2661 DEFSYM (Qmathematical_sans_serif_italic, "mathematical-sans-serif-italic");
2662 DEFSYM (Qmathematical_sans_serif_bold_italic, "mathematical-sans-serif-bold-italic");
2663 DEFSYM (Qmathematical_monospace, "mathematical-monospace");
2664 DEFSYM (Qcham, "cham");
2665 DEFSYM (Qphonetic, "phonetic");
2666 DEFSYM (Qbalinese, "balinese");
2667 DEFSYM (Qbuginese, "buginese");
2668 DEFSYM (Qbuhid, "buhid");
2669 DEFSYM (Qcuneiform, "cuneiform");
2670 DEFSYM (Qcypriot, "cypriot");
2671 DEFSYM (Qdeseret, "deseret");
2672 DEFSYM (Qglagolitic, "glagolitic");
2673 DEFSYM (Qgothic, "gothic");
2674 DEFSYM (Qhanunoo, "hanunoo");
2675 DEFSYM (Qkharoshthi, "kharoshthi");
2676 DEFSYM (Qlimbu, "limbu");
2677 DEFSYM (Qlinear_b, "linear_b");
2678 DEFSYM (Qold_italic, "old_italic");
2679 DEFSYM (Qold_persian, "old_persian");
2680 DEFSYM (Qosmanya, "osmanya");
2681 DEFSYM (Qphags_pa, "phags-pa");
2682 DEFSYM (Qphoenician, "phoenician");
2683 DEFSYM (Qshavian, "shavian");
2684 DEFSYM (Qsyloti_nagri, "syloti_nagri");
2685 DEFSYM (Qtagalog, "tagalog");
2686 DEFSYM (Qtagbanwa, "tagbanwa");
2687 DEFSYM (Qtai_le, "tai_le");
2688 DEFSYM (Qtifinagh, "tifinagh");
2689 DEFSYM (Qugaritic, "ugaritic");
2690 DEFSYM (Qlycian, "lycian");
2691 DEFSYM (Qcarian, "carian");
2692 DEFSYM (Qlydian, "lydian");
2693 DEFSYM (Qdomino_tile, "domino-tile");
2694 DEFSYM (Qmahjong_tile, "mahjong-tile");
2695 DEFSYM (Qtai_xuan_jing_symbol, "tai-xuan-jing-symbol");
2696 DEFSYM (Qcounting_rod_numeral, "counting-rod-numeral");
2697 DEFSYM (Qancient_symbol, "ancient-symbol");
2698 DEFSYM (Qphaistos_disc, "phaistos-disc");
2699 DEFSYM (Qancient_greek_number, "ancient-greek-number");
2700 DEFSYM (Qsundanese, "sundanese");
2701 DEFSYM (Qlepcha, "lepcha");
2702 DEFSYM (Qol_chiki, "ol-chiki");
2703 DEFSYM (Qsaurashtra, "saurashtra");
2704 DEFSYM (Qkayah_li, "kayah-li");
2705 DEFSYM (Qrejang, "rejang");
2707 /* W32 font encodings. */
2708 DEFVAR_LISP ("w32-charset-info-alist",
2709 Vw32_charset_info_alist,
2710 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
2711 Each entry should be of the form:
2713 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
2715 where CHARSET_NAME is a string used in font names to identify the charset,
2716 WINDOWS_CHARSET is a symbol that can be one of:
2718 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
2719 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
2720 w32-charset-chinesebig5, w32-charset-johab, w32-charset-hebrew,
2721 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
2722 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
2723 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
2724 or w32-charset-oem.
2726 CODEPAGE should be an integer specifying the codepage that should be used
2727 to display the character set, t to do no translation and output as Unicode,
2728 or nil to do no translation and output as 8 bit (or multibyte on far-east
2729 versions of Windows) characters. */);
2730 Vw32_charset_info_alist = Qnil;
2732 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
2733 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
2734 DEFSYM (Qw32_charset_default, "w32-charset-default");
2735 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
2736 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
2737 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
2738 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
2739 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
2740 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
2741 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
2742 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
2743 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
2744 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
2745 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
2746 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
2747 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
2748 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
2749 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
2750 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
2752 defsubr (&Sx_select_font);
2754 w32font_driver.type = Qgdi;
2755 register_font_driver (&w32font_driver, NULL);
2758 void
2759 globals_of_w32font (void)
2761 #ifdef WINDOWSNT
2762 g_b_init_get_outline_metrics_w = 0;
2763 g_b_init_get_text_metrics_w = 0;
2764 g_b_init_get_glyph_outline_w = 0;
2765 g_b_init_get_char_width_32_w = 0;
2766 #endif