(w32_enumfont_pattern_entity): Use requested registry.
[bpt/emacs.git] / src / w32font.c
CommitLineData
f7a84cb4 1/* Font backend for the Microsoft W32 API.
7e14d905 2 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
f7a84cb4
JR
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
f7a84cb4 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
f7a84cb4
JR
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f7a84cb4
JR
18
19#include <config.h>
20#include <windows.h>
09fe06b7 21#include <math.h>
f7a84cb4
JR
22
23#include "lisp.h"
24#include "w32term.h"
25#include "frame.h"
26#include "dispextern.h"
27#include "character.h"
28#include "charset.h"
29#include "fontset.h"
30#include "font.h"
46fd1ded 31#include "w32font.h"
f7a84cb4 32
91583281
JR
33/* Cleartype available on Windows XP, cleartype_natural from XP SP1.
34 The latter does not try to fit cleartype smoothed fonts into the
35 same bounding box as the non-antialiased version of the font.
36 */
37#ifndef CLEARTYPE_QUALITY
38#define CLEARTYPE_QUALITY 5
39#endif
40#ifndef CLEARTYPE_NATURAL_QUALITY
41#define CLEARTYPE_NATURAL_QUALITY 6
42#endif
43
f7a84cb4
JR
44extern struct font_driver w32font_driver;
45
91583281 46Lisp_Object Qgdi;
34fd2d28
JR
47Lisp_Object Quniscribe;
48static Lisp_Object QCformat;
9e1a2995
JR
49static Lisp_Object Qmonospace, Qsansserif, Qmono, Qsans, Qsans_serif;
50static Lisp_Object Qserif, Qscript, Qdecorative;
51static Lisp_Object Qraster, Qoutline, Qunknown;
d205d43b 52
91583281 53/* antialiasing */
5f18d119 54extern Lisp_Object QCantialias, QCotf, QClang; /* defined in font.c */
91583281
JR
55extern Lisp_Object Qnone; /* reuse from w32fns.c */
56static Lisp_Object Qstandard, Qsubpixel, Qnatural;
57
8f112d52
JR
58/* languages */
59static Lisp_Object Qja, Qko, Qzh;
60
d205d43b
JR
61/* scripts */
62static Lisp_Object Qlatin, Qgreek, Qcoptic, Qcyrillic, Qarmenian, Qhebrew;
63static Lisp_Object Qarabic, Qsyriac, Qnko, Qthaana, Qdevanagari, Qbengali;
64static Lisp_Object Qgurmukhi, Qgujarati, Qoriya, Qtamil, Qtelugu;
65static Lisp_Object Qkannada, Qmalayalam, Qsinhala, Qthai, Qlao;
66static Lisp_Object Qtibetan, Qmyanmar, Qgeorgian, Qhangul, Qethiopic;
67static Lisp_Object Qcherokee, Qcanadian_aboriginal, Qogham, Qrunic;
68static Lisp_Object Qkhmer, Qmongolian, Qsymbol, Qbraille, Qhan;
69static Lisp_Object Qideographic_description, Qcjk_misc, Qkana, Qbopomofo;
70static Lisp_Object Qkanbun, Qyi, Qbyzantine_musical_symbol;
71static Lisp_Object Qmusical_symbol, Qmathematical;
56df6710
JR
72/* Not defined in characters.el, but referenced in fontset.el. */
73static Lisp_Object Qbalinese, Qbuginese, Qbuhid, Qcuneiform, Qcypriot;
74static Lisp_Object Qdeseret, Qglagolitic, Qgothic, Qhanunoo, Qkharoshthi;
75static Lisp_Object Qlimbu, Qlinear_b, Qold_italic, Qold_persian, Qosmanya;
76static Lisp_Object Qphags_pa, Qphoenician, Qshavian, Qsyloti_nagri;
77static Lisp_Object Qtagalog, Qtagbanwa, Qtai_le, Qtifinagh, Qugaritic;
78/* Only defined here, but useful for distinguishing IPA capable fonts. */
79static Lisp_Object Qphonetic;
d205d43b
JR
80
81/* Font spacing symbols - defined in font.c. */
82extern Lisp_Object Qc, Qp, Qm;
f7a84cb4 83
8f112d52 84static void fill_in_logfont P_ ((FRAME_PTR, LOGFONT *, Lisp_Object));
f7a84cb4 85
8f112d52
JR
86static BYTE w32_antialias_type P_ ((Lisp_Object));
87static Lisp_Object lispy_antialias_type P_ ((BYTE));
91583281 88
8f112d52
JR
89static Lisp_Object font_supported_scripts P_ ((FONTSIGNATURE *));
90static int w32font_full_name P_ ((LOGFONT *, Lisp_Object, int, char *, int));
91static void compute_metrics P_ ((HDC, struct w32font_info *, unsigned int,
92 struct w32_metric_cache *));
93static void clear_cached_metrics P_ ((struct w32font_info *));
f7a84cb4 94
8f112d52 95static Lisp_Object w32_registry P_ ((LONG, DWORD));
f7a84cb4
JR
96
97/* EnumFontFamiliesEx callbacks. */
98static int CALLBACK add_font_entity_to_list P_ ((ENUMLOGFONTEX *,
99 NEWTEXTMETRICEX *,
100 DWORD, LPARAM));
101static int CALLBACK add_one_font_entity_to_list P_ ((ENUMLOGFONTEX *,
102 NEWTEXTMETRICEX *,
103 DWORD, LPARAM));
104static int CALLBACK add_font_name_to_list P_ ((ENUMLOGFONTEX *,
105 NEWTEXTMETRICEX *,
106 DWORD, LPARAM));
107
d205d43b
JR
108/* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
109 of what we really want. */
110struct font_callback_data
111{
112 /* The logfont we are matching against. EnumFontFamiliesEx only matches
113 face name and charset, so we need to manually match everything else
114 in the callback function. */
115 LOGFONT pattern;
116 /* The original font spec or entity. */
117 Lisp_Object orig_font_spec;
118 /* The frame the font is being loaded on. */
119 Lisp_Object frame;
120 /* The list to add matches to. */
121 Lisp_Object list;
46fd1ded
JR
122 /* Whether to match only opentype fonts. */
123 int opentype_only;
d205d43b
JR
124};
125
126/* Handles the problem that EnumFontFamiliesEx will not return all
127 style variations if the font name is not specified. */
8f112d52 128static void list_all_matching_fonts P_ ((struct font_callback_data *));
d205d43b 129
34fd2d28 130/* From old font code in w32fns.c */
8f112d52 131char * w32_to_x_charset P_ ((int, char *));
d205d43b 132
f7a84cb4
JR
133
134static int
135memq_no_quit (elt, list)
136 Lisp_Object elt, list;
137{
138 while (CONSP (list) && ! EQ (XCAR (list), elt))
139 list = XCDR (list);
140 return (CONSP (list));
141}
142
143/* w32 implementation of get_cache for font backend.
144 Return a cache of font-entities on FRAME. The cache must be a
145 cons whose cdr part is the actual cache area. */
46fd1ded 146Lisp_Object
a4c71909
KH
147w32font_get_cache (f)
148 FRAME_PTR f;
f7a84cb4 149{
a4c71909 150 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f7a84cb4
JR
151
152 return (dpyinfo->name_list_element);
153}
154
155/* w32 implementation of list for font backend.
156 List fonts exactly matching with FONT_SPEC on FRAME. The value
157 is a vector of font-entities. This is the sole API that
158 allocates font-entities. */
20399669
JR
159static Lisp_Object
160w32font_list (frame, font_spec)
161 Lisp_Object frame, font_spec;
f7a84cb4 162{
46fd1ded 163 return w32font_list_internal (frame, font_spec, 0);
f7a84cb4
JR
164}
165
166/* w32 implementation of match for font backend.
167 Return a font entity most closely matching with FONT_SPEC on
168 FRAME. The closeness is detemined by the font backend, thus
169 `face-font-selection-order' is ignored here. */
20399669
JR
170static Lisp_Object
171w32font_match (frame, font_spec)
172 Lisp_Object frame, font_spec;
f7a84cb4 173{
46fd1ded 174 return w32font_match_internal (frame, font_spec, 0);
f7a84cb4
JR
175}
176
f7a84cb4
JR
177/* w32 implementation of list_family for font backend.
178 List available families. The value is a list of family names
179 (symbols). */
20399669
JR
180static Lisp_Object
181w32font_list_family (frame)
182 Lisp_Object frame;
f7a84cb4
JR
183{
184 Lisp_Object list = Qnil;
185 LOGFONT font_match_pattern;
186 HDC dc;
187 FRAME_PTR f = XFRAME (frame);
188
189 bzero (&font_match_pattern, sizeof (font_match_pattern));
56df6710 190 font_match_pattern.lfCharSet = DEFAULT_CHARSET;
f7a84cb4
JR
191
192 dc = get_frame_dc (f);
193
194 EnumFontFamiliesEx (dc, &font_match_pattern,
195 (FONTENUMPROC) add_font_name_to_list,
196 (LPARAM) &list, 0);
197 release_frame_dc (f, dc);
198
199 return list;
200}
201
202/* w32 implementation of open for font backend.
203 Open a font specified by FONT_ENTITY on frame F.
204 If the font is scalable, open it with PIXEL_SIZE. */
5f18d119 205static Lisp_Object
20399669
JR
206w32font_open (f, font_entity, pixel_size)
207 FRAME_PTR f;
208 Lisp_Object font_entity;
209 int pixel_size;
f7a84cb4 210{
5f18d119 211 Lisp_Object font_object;
f7a84cb4 212
5f18d119 213 font_object = font_make_object (VECSIZE (struct w32font_info));
f7a84cb4 214
5f18d119 215 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
f7a84cb4 216 {
5f18d119 217 return Qnil;
f7a84cb4
JR
218 }
219
5f18d119 220 return font_object;
f7a84cb4
JR
221}
222
223/* w32 implementation of close for font_backend.
224 Close FONT on frame F. */
46fd1ded 225void
20399669
JR
226w32font_close (f, font)
227 FRAME_PTR f;
228 struct font *font;
f7a84cb4 229{
5f18d119
KH
230 struct w32font_info *w32_font = (struct w32font_info *) font;
231
232 if (w32_font->compat_w32_font)
f7a84cb4 233 {
5f18d119 234 W32FontStruct *old_w32_font = w32_font->compat_w32_font;
e9a15283 235 DeleteObject (old_w32_font->hfont);
f7a84cb4 236 xfree (old_w32_font);
5f18d119 237 w32_font->compat_w32_font = 0;
f7a84cb4 238 }
f7a84cb4
JR
239}
240
241/* w32 implementation of has_char for font backend.
242 Optional.
243 If FONT_ENTITY has a glyph for character C (Unicode code point),
244 return 1. If not, return 0. If a font must be opened to check
245 it, return -1. */
46fd1ded 246int
20399669
JR
247w32font_has_char (entity, c)
248 Lisp_Object entity;
249 int c;
f7a84cb4 250{
d205d43b 251 Lisp_Object supported_scripts, extra, script;
f7a84cb4
JR
252 DWORD mask;
253
01dbeb0b
JR
254 extra = AREF (entity, FONT_EXTRA_INDEX);
255 if (!CONSP (extra))
256 return -1;
257
d205d43b
JR
258 supported_scripts = assq_no_quit (QCscript, extra);
259 if (!CONSP (supported_scripts))
f7a84cb4
JR
260 return -1;
261
d205d43b 262 supported_scripts = XCDR (supported_scripts);
f7a84cb4 263
d205d43b 264 script = CHAR_TABLE_REF (Vchar_script_table, c);
f7a84cb4 265
34fd2d28 266 return (memq_no_quit (script, supported_scripts)) ? -1 : 0;
f7a84cb4
JR
267}
268
269/* w32 implementation of encode_char for font backend.
270 Return a glyph code of FONT for characer C (Unicode code point).
271 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
34fd2d28 272static unsigned
20399669
JR
273w32font_encode_char (font, c)
274 struct font *font;
275 int c;
f7a84cb4 276{
34fd2d28
JR
277 struct frame *f;
278 HDC dc;
279 HFONT old_font;
280 DWORD retval;
281 GCP_RESULTSW result;
282 wchar_t in[2];
283 wchar_t out[2];
284 int len;
285 struct w32font_info *w32_font = (struct w32font_info *) font;
286
287 /* If glyph indexing is not working for this font, just return the
288 unicode code-point. */
289 if (!w32_font->glyph_idx)
290 return c;
291
292 if (c > 0xFFFF)
293 {
294 /* TODO: Encode as surrogate pair and lookup the glyph. */
295 return FONT_INVALID_CODE;
296 }
297 else
298 {
299 in[0] = (wchar_t) c;
300 len = 1;
301 }
302
303 bzero (&result, sizeof (result));
304 result.lStructSize = sizeof (result);
305 result.lpGlyphs = out;
306 result.nGlyphs = 2;
307
308 f = XFRAME (selected_frame);
309
310 dc = get_frame_dc (f);
5f18d119 311 old_font = SelectObject (dc, w32_font->compat_w32_font->hfont);
34fd2d28
JR
312
313 retval = GetCharacterPlacementW (dc, in, len, 0, &result, 0);
314
315 SelectObject (dc, old_font);
316 release_frame_dc (f, dc);
317
318 if (retval)
319 {
320 if (result.nGlyphs != 1 || !result.lpGlyphs[0])
321 return FONT_INVALID_CODE;
322 return result.lpGlyphs[0];
323 }
324 else
325 {
326 int i;
327 /* Mark this font as not supporting glyph indices. This can happen
328 on Windows9x, and maybe with non-Truetype fonts on NT etc. */
329 w32_font->glyph_idx = 0;
8f112d52
JR
330 /* Clear metrics cache. */
331 clear_cached_metrics (w32_font);
34fd2d28
JR
332
333 return c;
334 }
f7a84cb4
JR
335}
336
337/* w32 implementation of text_extents for font backend.
338 Perform the size computation of glyphs of FONT and fillin members
339 of METRICS. The glyphs are specified by their glyph codes in
78573c57 340 CODE (length NGLYPHS). Apparently metrics can be NULL, in this
f7a84cb4 341 case just return the overall width. */
46fd1ded 342int
20399669
JR
343w32font_text_extents (font, code, nglyphs, metrics)
344 struct font *font;
345 unsigned *code;
346 int nglyphs;
347 struct font_metrics *metrics;
f7a84cb4
JR
348{
349 int i;
2a36efcf
JR
350 HFONT old_font = NULL;
351 HDC dc = NULL;
46fd1ded 352 struct frame * f;
f7a84cb4 353 int total_width = 0;
8f112d52 354 WORD *wcode = NULL;
d205d43b 355 SIZE size;
f7a84cb4 356
f7a84cb4
JR
357 if (metrics)
358 {
34fd2d28 359 struct w32font_info *w32_font = (struct w32font_info *) font;
d205d43b 360
ba93d684 361 bzero (metrics, sizeof (struct font_metrics));
388c38f9
JR
362 metrics->ascent = font->ascent;
363 metrics->descent = font->descent;
f7a84cb4
JR
364
365 for (i = 0; i < nglyphs; i++)
366 {
8f112d52
JR
367 struct w32_metric_cache *char_metric;
368 int block = *(code + i) / CACHE_BLOCKSIZE;
369 int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
370
371 if (block >= w32_font->n_cache_blocks)
372 {
373 if (!w32_font->cached_metrics)
374 w32_font->cached_metrics
375 = xmalloc ((block + 1)
376 * sizeof (struct w32_cached_metric *));
377 else
378 w32_font->cached_metrics
379 = xrealloc (w32_font->cached_metrics,
380 (block + 1)
381 * sizeof (struct w32_cached_metric *));
382 bzero (w32_font->cached_metrics + w32_font->n_cache_blocks,
383 ((block + 1 - w32_font->n_cache_blocks)
384 * sizeof (struct w32_cached_metric *)));
385 w32_font->n_cache_blocks = block + 1;
386 }
387
388 if (!w32_font->cached_metrics[block])
389 {
390 w32_font->cached_metrics[block]
391 = xmalloc (CACHE_BLOCKSIZE * sizeof (struct font_metrics));
392 bzero (w32_font->cached_metrics[block],
393 CACHE_BLOCKSIZE * sizeof (struct font_metrics));
394 }
395
396 char_metric = w32_font->cached_metrics[block] + pos_in_block;
397
398 if (char_metric->status == W32METRIC_NO_ATTEMPT)
399 {
400 if (dc == NULL)
401 {
402 /* TODO: Frames can come and go, and their fonts
403 outlive them. So we can't cache the frame in the
404 font structure. Use selected_frame until the API
405 is updated to pass in a frame. */
406 f = XFRAME (selected_frame);
9c623c85 407
2a36efcf 408 dc = get_frame_dc (f);
5f18d119 409 old_font = SelectObject (dc, FONT_COMPAT (font)->hfont);
8f112d52
JR
410 }
411 compute_metrics (dc, w32_font, *(code + i), char_metric);
412 }
78573c57 413
8f112d52
JR
414 if (char_metric->status == W32METRIC_SUCCESS)
415 {
416 metrics->lbearing = min (metrics->lbearing,
417 metrics->width + char_metric->lbearing);
418 metrics->rbearing = max (metrics->rbearing,
419 metrics->width + char_metric->rbearing);
420 metrics->width += char_metric->width;
421 }
422 else
423 /* If we couldn't get metrics for a char,
424 use alternative method. */
425 break;
426 }
78573c57
JR
427 /* If we got through everything, return. */
428 if (i == nglyphs)
429 {
2a36efcf
JR
430 if (dc != NULL)
431 {
432 /* Restore state and release DC. */
433 SelectObject (dc, old_font);
434 release_frame_dc (f, dc);
435 }
78573c57
JR
436
437 return metrics->width;
438 }
f7a84cb4 439 }
78573c57 440
56df6710
JR
441 /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
442 fallback on other methods that will at least give some of the metric
443 information. */
8f112d52
JR
444 if (!wcode) {
445 wcode = alloca (nglyphs * sizeof (WORD));
446 for (i = 0; i < nglyphs; i++)
447 {
448 if (code[i] < 0x10000)
449 wcode[i] = code[i];
450 else
451 {
452 /* TODO: Convert to surrogate, reallocating array if needed */
453 wcode[i] = 0xffff;
454 }
455 }
456 }
2a36efcf
JR
457 if (dc == NULL)
458 {
8f112d52
JR
459 /* TODO: Frames can come and go, and their fonts outlive
460 them. So we can't cache the frame in the font structure. Use
461 selected_frame until the API is updated to pass in a
462 frame. */
463 f = XFRAME (selected_frame);
464
2a36efcf 465 dc = get_frame_dc (f);
5f18d119 466 old_font = SelectObject (dc, FONT_COMPAT (font)->hfont);
2a36efcf
JR
467 }
468
d205d43b 469 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
f7a84cb4 470 {
d205d43b 471 total_width = size.cx;
f7a84cb4
JR
472 }
473
56df6710
JR
474 /* On 95/98/ME, only some unicode functions are available, so fallback
475 on doing a dummy draw to find the total width. */
d205d43b 476 if (!total_width)
f7a84cb4
JR
477 {
478 RECT rect;
5f18d119 479 rect.top = 0; rect.bottom = font->height; rect.left = 0; rect.right = 1;
f7a84cb4
JR
480 DrawTextW (dc, wcode, nglyphs, &rect,
481 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
482 total_width = rect.right;
483 }
d205d43b 484
56df6710 485 /* Give our best estimate of the metrics, based on what we know. */
78573c57
JR
486 if (metrics)
487 {
488 metrics->width = total_width;
78573c57
JR
489 metrics->lbearing = 0;
490 metrics->rbearing = total_width
491 + ((struct w32font_info *) font)->metrics.tmOverhang;
492 }
493
f7a84cb4
JR
494 /* Restore state and release DC. */
495 SelectObject (dc, old_font);
46fd1ded 496 release_frame_dc (f, dc);
f7a84cb4
JR
497
498 return total_width;
499}
500
501/* w32 implementation of draw for font backend.
502 Optional.
503 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
504 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND
505 is nonzero, fill the background in advance. It is assured that
a74ddbda
JR
506 WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).
507
508 TODO: Currently this assumes that the colors and fonts are already
509 set in the DC. This seems to be true now, but maybe only due to
510 the old font code setting it up. It may be safer to resolve faces
511 and fonts in here and set them explicitly
512*/
513
46fd1ded 514int
20399669
JR
515w32font_draw (s, from, to, x, y, with_background)
516 struct glyph_string *s;
517 int from, to, x, y, with_background;
f7a84cb4 518{
34fd2d28 519 UINT options;
5c2c9c79 520 HRGN orig_clip;
5f18d119 521 struct w32font_info *w32font = (struct w32font_info *) s->font;
34fd2d28
JR
522
523 options = w32font->glyph_idx;
5c2c9c79
JR
524
525 /* Save clip region for later restoration. */
526 GetClipRgn(s->hdc, orig_clip);
527
528 if (s->num_clips > 0)
529 {
530 HRGN new_clip = CreateRectRgnIndirect (s->clip);
531
532 if (s->num_clips > 1)
533 {
534 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
535
536 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
537 DeleteObject (clip2);
538 }
539
540 SelectClipRgn (s->hdc, new_clip);
541 DeleteObject (new_clip);
542 }
f7a84cb4 543
fb3b8017
JR
544 /* Using OPAQUE background mode can clear more background than expected
545 when Cleartype is used. Draw the background manually to avoid this. */
546 SetBkMode (s->hdc, TRANSPARENT);
f7a84cb4
JR
547 if (with_background)
548 {
1065a502
JR
549 HBRUSH brush;
550 RECT rect;
5f18d119 551 struct font *font = s->font;
1065a502
JR
552
553 brush = CreateSolidBrush (s->gc->background);
554 rect.left = x;
fb3b8017 555 rect.top = y - font->ascent;
1065a502 556 rect.right = x + s->width;
fb3b8017 557 rect.bottom = y + font->descent;
1065a502 558 FillRect (s->hdc, &rect, brush);
f2b25c0e 559 DeleteObject (brush);
f7a84cb4 560 }
040fe918 561
de63f07f
KH
562 if (s->padding_p)
563 {
564 int len = to - from, i;
565
566 for (i = 0; i < len; i++)
567 ExtTextOutW (s->hdc, x + i, y, options, NULL,
8501c48b 568 s->char2b + from + i, 1, NULL);
de63f07f
KH
569 }
570 else
571 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
5c2c9c79
JR
572
573 /* Restore clip region. */
574 if (s->num_clips > 0)
575 {
576 SelectClipRgn (s->hdc, orig_clip);
577 }
f7a84cb4
JR
578}
579
580/* w32 implementation of free_entity for font backend.
581 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
582 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
20399669
JR
583static void
584w32font_free_entity (Lisp_Object entity);
f7a84cb4
JR
585 */
586
587/* w32 implementation of prepare_face for font backend.
588 Optional (if FACE->extra is not used).
589 Prepare FACE for displaying characters by FONT on frame F by
590 storing some data in FACE->extra. If successful, return 0.
591 Otherwise, return -1.
20399669
JR
592static int
593w32font_prepare_face (FRAME_PTR f, struct face *face);
f7a84cb4
JR
594 */
595/* w32 implementation of done_face for font backend.
596 Optional.
597 Done FACE for displaying characters by FACE->font on frame F.
20399669
JR
598static void
599w32font_done_face (FRAME_PTR f, struct face *face); */
f7a84cb4
JR
600
601/* w32 implementation of get_bitmap for font backend.
602 Optional.
603 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
f2b25c0e 604 intended that this method is called from the other font-driver
f7a84cb4 605 for actual drawing.
20399669
JR
606static int
607w32font_get_bitmap (struct font *font, unsigned code,
608 struct font_bitmap *bitmap, int bits_per_pixel);
f7a84cb4
JR
609 */
610/* w32 implementation of free_bitmap for font backend.
611 Optional.
612 Free bitmap data in BITMAP.
20399669
JR
613static void
614w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
f7a84cb4
JR
615 */
616/* w32 implementation of get_outline for font backend.
617 Optional.
618 Return an outline data for glyph-code CODE of FONT. The format
619 of the outline data depends on the font-driver.
20399669
JR
620static void *
621w32font_get_outline (struct font *font, unsigned code);
f7a84cb4
JR
622 */
623/* w32 implementation of free_outline for font backend.
624 Optional.
625 Free OUTLINE (that is obtained by the above method).
20399669
JR
626static void
627w32font_free_outline (struct font *font, void *outline);
f7a84cb4
JR
628 */
629/* w32 implementation of anchor_point for font backend.
630 Optional.
631 Get coordinates of the INDEXth anchor point of the glyph whose
632 code is CODE. Store the coordinates in *X and *Y. Return 0 if
633 the operations was successfull. Otherwise return -1.
20399669
JR
634static int
635w32font_anchor_point (struct font *font, unsigned code,
f7a84cb4
JR
636 int index, int *x, int *y);
637 */
638/* w32 implementation of otf_capability for font backend.
639 Optional.
640 Return a list describing which scripts/languages FONT
641 supports by which GSUB/GPOS features of OpenType tables.
20399669
JR
642static Lisp_Object
643w32font_otf_capability (struct font *font);
f7a84cb4
JR
644 */
645/* w32 implementation of otf_drive for font backend.
646 Optional.
647 Apply FONT's OTF-FEATURES to the glyph string.
648
649 FEATURES specifies which OTF features to apply in this format:
650 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
651 See the documentation of `font-drive-otf' for the detail.
652
653 This method applies the specified features to the codes in the
654 elements of GSTRING-IN (between FROMth and TOth). The output
655 codes are stored in GSTRING-OUT at the IDXth element and the
656 following elements.
657
658 Return the number of output codes. If none of the features are
659 applicable to the input data, return 0. If GSTRING-OUT is too
660 short, return -1.
20399669
JR
661static int
662w32font_otf_drive (struct font *font, Lisp_Object features,
663 Lisp_Object gstring_in, int from, int to,
664 Lisp_Object gstring_out, int idx,
665 int alternate_subst);
f7a84cb4
JR
666 */
667
46fd1ded
JR
668/* Internal implementation of w32font_list.
669 Additional parameter opentype_only restricts the returned fonts to
670 opentype fonts, which can be used with the Uniscribe backend. */
671Lisp_Object
672w32font_list_internal (frame, font_spec, opentype_only)
673 Lisp_Object frame, font_spec;
674 int opentype_only;
675{
676 struct font_callback_data match_data;
677 HDC dc;
678 FRAME_PTR f = XFRAME (frame);
679
680 match_data.orig_font_spec = font_spec;
681 match_data.list = Qnil;
682 match_data.frame = frame;
683
684 bzero (&match_data.pattern, sizeof (LOGFONT));
685 fill_in_logfont (f, &match_data.pattern, font_spec);
686
687 match_data.opentype_only = opentype_only;
688 if (opentype_only)
689 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
690
691 if (match_data.pattern.lfFaceName[0] == '\0')
692 {
693 /* EnumFontFamiliesEx does not take other fields into account if
694 font name is blank, so need to use two passes. */
695 list_all_matching_fonts (&match_data);
696 }
697 else
698 {
699 dc = get_frame_dc (f);
700
701 EnumFontFamiliesEx (dc, &match_data.pattern,
702 (FONTENUMPROC) add_font_entity_to_list,
703 (LPARAM) &match_data, 0);
704 release_frame_dc (f, dc);
705 }
706
5f18d119 707 return NILP (match_data.list) ? Qnil : match_data.list;
46fd1ded
JR
708}
709
710/* Internal implementation of w32font_match.
711 Additional parameter opentype_only restricts the returned fonts to
712 opentype fonts, which can be used with the Uniscribe backend. */
713Lisp_Object
714w32font_match_internal (frame, font_spec, opentype_only)
715 Lisp_Object frame, font_spec;
716 int opentype_only;
717{
718 struct font_callback_data match_data;
719 HDC dc;
720 FRAME_PTR f = XFRAME (frame);
721
722 match_data.orig_font_spec = font_spec;
723 match_data.frame = frame;
724 match_data.list = Qnil;
725
726 bzero (&match_data.pattern, sizeof (LOGFONT));
727 fill_in_logfont (f, &match_data.pattern, font_spec);
728
729 match_data.opentype_only = opentype_only;
730 if (opentype_only)
731 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
732
733 dc = get_frame_dc (f);
734
735 EnumFontFamiliesEx (dc, &match_data.pattern,
736 (FONTENUMPROC) add_one_font_entity_to_list,
737 (LPARAM) &match_data, 0);
738 release_frame_dc (f, dc);
739
740 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
741}
742
f0121ad2 743int
5f18d119 744w32font_open_internal (f, font_entity, pixel_size, font_object)
f0121ad2
JR
745 FRAME_PTR f;
746 Lisp_Object font_entity;
747 int pixel_size;
5f18d119 748 Lisp_Object font_object;
f0121ad2 749{
5f18d119 750 int len, size, i;
f0121ad2
JR
751 LOGFONT logfont;
752 HDC dc;
753 HFONT hfont, old_font;
754 Lisp_Object val, extra;
755 /* For backwards compatibility. */
756 W32FontStruct *compat_w32_font;
5f18d119
KH
757 struct w32font_info *w32_font;
758 struct font * font;
759 OUTLINETEXTMETRIC* metrics = NULL;
760
761 w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
762 font = (struct font *) w32_font;
f0121ad2 763
f0121ad2
JR
764 if (!font)
765 return 0;
766
5f18d119
KH
767 /* Copy from font entity. */
768 for (i = 0; i < FONT_ENTITY_MAX; i++)
769 ASET (font_object, i, AREF (font_entity, i));
770 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
771
f0121ad2
JR
772 bzero (&logfont, sizeof (logfont));
773 fill_in_logfont (f, &logfont, font_entity);
774
1519d52e
JR
775 /* Prefer truetype fonts, to avoid known problems with type1 fonts, and
776 limitations in bitmap fonts. */
777 val = AREF (font_entity, FONT_FOUNDRY_INDEX);
778 if (!EQ (val, Qraster))
779 logfont.lfOutPrecision = OUT_TT_PRECIS;
780
f0121ad2
JR
781 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
782 if (!size)
783 size = pixel_size;
784
785 logfont.lfHeight = -size;
786 hfont = CreateFontIndirect (&logfont);
787
788 if (hfont == NULL)
789 return 0;
790
f0121ad2
JR
791 /* Get the metrics for this font. */
792 dc = get_frame_dc (f);
793 old_font = SelectObject (dc, hfont);
794
5f18d119
KH
795 /* Try getting the outline metrics (only works for truetype fonts). */
796 len = GetOutlineTextMetrics (dc, 0, NULL);
797 if (len)
798 {
799 metrics = (OUTLINETEXTMETRIC *) alloca (len);
800 if (GetOutlineTextMetrics (dc, len, metrics))
801 bcopy (&metrics->otmTextMetrics, &w32_font->metrics,
802 sizeof (TEXTMETRIC));
803 else
804 metrics = NULL;
943f2093
JR
805
806 /* If it supports outline metrics, it should support Glyph Indices. */
807 w32_font->glyph_idx = ETO_GLYPH_INDEX;
5f18d119 808 }
f0121ad2 809
943f2093
JR
810 if (!metrics)
811 {
812 GetTextMetrics (dc, &w32_font->metrics);
813 w32_font->glyph_idx = 0;
814 }
7145be81 815
8f112d52
JR
816 w32_font->cached_metrics = NULL;
817 w32_font->n_cache_blocks = 0;
54efdcd1 818
f0121ad2
JR
819 SelectObject (dc, old_font);
820 release_frame_dc (f, dc);
34fd2d28 821
f0121ad2
JR
822 /* W32FontStruct - we should get rid of this, and use the w32font_info
823 struct for any W32 specific fields. font->font.font can then be hfont. */
5f18d119
KH
824 w32_font->compat_w32_font = xmalloc (sizeof (W32FontStruct));
825 compat_w32_font = w32_font->compat_w32_font;
f0121ad2
JR
826 bzero (compat_w32_font, sizeof (W32FontStruct));
827 compat_w32_font->font_type = UNICODE_FONT;
828 /* Duplicate the text metrics. */
829 bcopy (&w32_font->metrics, &compat_w32_font->tm, sizeof (TEXTMETRIC));
830 compat_w32_font->hfont = hfont;
831
5ace1ec1
JR
832 {
833 char *name;
834
835 /* We don't know how much space we need for the full name, so start with
836 96 bytes and go up in steps of 32. */
837 len = 96;
e3a77b22 838 name = xmalloc (len);
67997c79
JR
839 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
840 name, len) < 0)
5ace1ec1 841 {
e3a77b22 842 char *new = xrealloc (name, len += 32);
5ace1ec1
JR
843
844 if (! new)
e3a77b22 845 xfree (name);
5ace1ec1
JR
846 name = new;
847 }
848 if (name)
5f18d119
KH
849 font->props[FONT_FULLNAME_INDEX]
850 = make_unibyte_string (name, strlen (name));
5ace1ec1 851 else
5f18d119
KH
852 font->props[FONT_FULLNAME_INDEX] =
853 make_unibyte_string (logfont.lfFaceName, len);
5ace1ec1 854 }
5f18d119
KH
855
856 font->max_width = w32_font->metrics.tmMaxCharWidth;
857 font->height = w32_font->metrics.tmHeight
f0121ad2 858 + w32_font->metrics.tmExternalLeading;
5f18d119
KH
859 font->space_width = font->average_width = w32_font->metrics.tmAveCharWidth;
860
861 font->vertical_centering = 0;
862 font->encoding_type = 0;
863 font->baseline_offset = 0;
864 font->relative_compose = 0;
865 font->default_ascent = w32_font->metrics.tmAscent;
866 font->font_encoder = NULL;
f0121ad2
JR
867 font->pixel_size = size;
868 font->driver = &w32font_driver;
34fd2d28
JR
869 /* Use format cached during list, as the information we have access to
870 here is incomplete. */
871 extra = AREF (font_entity, FONT_EXTRA_INDEX);
872 if (CONSP (extra))
873 {
874 val = assq_no_quit (QCformat, extra);
875 if (CONSP (val))
5f18d119 876 font->props[FONT_FORMAT_INDEX] = XCDR (val);
34fd2d28 877 else
5f18d119 878 font->props[FONT_FORMAT_INDEX] = Qunknown;
34fd2d28
JR
879 }
880 else
5f18d119 881 font->props[FONT_FORMAT_INDEX] = Qunknown;
34fd2d28 882
5f18d119 883 font->props[FONT_FILE_INDEX] = Qnil;
f0121ad2
JR
884 font->encoding_charset = -1;
885 font->repertory_charset = -1;
04b65d2b 886 /* TODO: do we really want the minimum width here, which could be negative? */
5f18d119 887 font->min_width = font->space_width;
f0121ad2
JR
888 font->ascent = w32_font->metrics.tmAscent;
889 font->descent = w32_font->metrics.tmDescent;
5f18d119
KH
890
891 if (metrics)
892 {
893 font->underline_thickness = metrics->otmsUnderscoreSize;
894 font->underline_position = -metrics->otmsUnderscorePosition;
895 }
896 else
897 {
898 font->underline_thickness = 0;
899 font->underline_position = -1;
900 }
f0121ad2 901
e157d7ba
JR
902 /* max_descent is used for underlining in w32term.c. Hopefully this
903 is temporary, as we'll want to get rid of the old compatibility
904 stuff later. */
905 compat_w32_font->max_bounds.descent = font->descent;
906
5f18d119
KH
907 /* For temporary compatibility with legacy code that expects the
908 name to be usable in x-list-fonts. Eventually we expect to change
909 x-list-fonts and other places that use fonts so that this can be
910 an fcname or similar. */
a30e1957 911 font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
04b65d2b 912
f0121ad2
JR
913 return 1;
914}
915
f7a84cb4
JR
916/* Callback function for EnumFontFamiliesEx.
917 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
20399669
JR
918static int CALLBACK
919add_font_name_to_list (logical_font, physical_font, font_type, list_object)
920 ENUMLOGFONTEX *logical_font;
921 NEWTEXTMETRICEX *physical_font;
922 DWORD font_type;
923 LPARAM list_object;
f7a84cb4
JR
924{
925 Lisp_Object* list = (Lisp_Object *) list_object;
20a2b756
JR
926 Lisp_Object family;
927
928 /* Skip vertical fonts (intended only for printing) */
929 if (logical_font->elfLogFont.lfFaceName[0] == '@')
930 return 1;
931
5f18d119
KH
932 family = font_intern_prop (logical_font->elfLogFont.lfFaceName,
933 strlen (logical_font->elfLogFont.lfFaceName));
f7a84cb4
JR
934 if (! memq_no_quit (family, *list))
935 *list = Fcons (family, *list);
936
937 return 1;
938}
939
5f18d119
KH
940static int w32_decode_weight P_ ((int));
941static int w32_encode_weight P_ ((int));
942
f7a84cb4 943/* Convert an enumerated Windows font to an Emacs font entity. */
20399669 944static Lisp_Object
91583281 945w32_enumfont_pattern_entity (frame, logical_font, physical_font,
34fd2d28 946 font_type, requested_font, backend)
d205d43b 947 Lisp_Object frame;
20399669
JR
948 ENUMLOGFONTEX *logical_font;
949 NEWTEXTMETRICEX *physical_font;
950 DWORD font_type;
91583281 951 LOGFONT *requested_font;
34fd2d28 952 Lisp_Object backend;
f7a84cb4
JR
953{
954 Lisp_Object entity, tem;
955 LOGFONT *lf = (LOGFONT*) logical_font;
956 BYTE generic_type;
56df6710 957 DWORD full_type = physical_font->ntmTm.ntmFlags;
f7a84cb4 958
5f18d119 959 entity = font_make_entity ();
f7a84cb4 960
34fd2d28 961 ASET (entity, FONT_TYPE_INDEX, backend);
c960bff8 962 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
f7a84cb4
JR
963 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
964
965 /* Foundry is difficult to get in readable form on Windows.
d205d43b 966 But Emacs crashes if it is not set, so set it to something more
9c623c85 967 generic. These values make xlfds compatible with Emacs 22. */
d205d43b
JR
968 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
969 tem = Qraster;
970 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
971 tem = Qoutline;
972 else
973 tem = Qunknown;
974
975 ASET (entity, FONT_FOUNDRY_INDEX, tem);
976
977 /* Save the generic family in the extra info, as it is likely to be
978 useful to users looking for a close match. */
f7a84cb4
JR
979 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
980 if (generic_type == FF_DECORATIVE)
981 tem = Qdecorative;
982 else if (generic_type == FF_MODERN)
9e1a2995 983 tem = Qmono;
f7a84cb4 984 else if (generic_type == FF_ROMAN)
d205d43b 985 tem = Qserif;
f7a84cb4
JR
986 else if (generic_type == FF_SCRIPT)
987 tem = Qscript;
988 else if (generic_type == FF_SWISS)
9e1a2995 989 tem = Qsans;
f7a84cb4 990 else
5f18d119 991 tem = Qnil;
9e1a2995
JR
992
993 ASET (entity, FONT_ADSTYLE_INDEX, tem);
f7a84cb4 994
d205d43b 995 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
5f18d119 996 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_PROPORTIONAL));
d205d43b 997 else
ed18e612 998 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_CHARCELL));
f7a84cb4 999
91583281
JR
1000 if (requested_font->lfQuality != DEFAULT_QUALITY)
1001 {
1002 font_put_extra (entity, QCantialias,
1003 lispy_antialias_type (requested_font->lfQuality));
1004 }
f7a84cb4 1005 ASET (entity, FONT_FAMILY_INDEX,
5f18d119 1006 font_intern_prop (lf->lfFaceName, strlen (lf->lfFaceName)));
f7a84cb4 1007
5f18d119
KH
1008 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX,
1009 make_number (w32_decode_weight (lf->lfWeight)));
1010 FONT_SET_STYLE (entity, FONT_SLANT_INDEX,
1011 make_number (lf->lfItalic ? 200 : 100));
d205d43b
JR
1012 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
1013 to get it. */
5f18d119 1014 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (100));
f7a84cb4 1015
040fe918
JR
1016 if (font_type & RASTER_FONTTYPE)
1017 ASET (entity, FONT_SIZE_INDEX, make_number (physical_font->ntmTm.tmHeight));
1018 else
1019 ASET (entity, FONT_SIZE_INDEX, make_number (0));
f7a84cb4
JR
1020
1021 /* Cache unicode codepoints covered by this font, as there is no other way
1022 of getting this information easily. */
040fe918 1023 if (font_type & TRUETYPE_FONTTYPE)
f7a84cb4 1024 {
aee986fd
JR
1025 tem = font_supported_scripts (&physical_font->ntmFontSig);
1026 if (!NILP (tem))
1027 font_put_extra (entity, QCscript, tem);
f7a84cb4 1028 }
d205d43b 1029
34fd2d28
JR
1030 /* This information is not fully available when opening fonts, so
1031 save it here. Only Windows 2000 and later return information
1032 about opentype and type1 fonts, so need a fallback for detecting
1033 truetype so that this information is not any worse than we could
1034 have obtained later. */
56df6710
JR
1035 if (EQ (backend, Quniscribe) && (full_type & NTMFLAGS_OPENTYPE))
1036 tem = intern ("opentype");
1037 else if (font_type & TRUETYPE_FONTTYPE)
34fd2d28 1038 tem = intern ("truetype");
34fd2d28
JR
1039 else if (full_type & NTM_PS_OPENTYPE)
1040 tem = intern ("postscript");
56df6710
JR
1041 else if (full_type & NTM_TYPE1)
1042 tem = intern ("type1");
34fd2d28
JR
1043 else if (font_type & RASTER_FONTTYPE)
1044 tem = intern ("w32bitmap");
1045 else
1046 tem = intern ("w32vector");
1047
1048 font_put_extra (entity, QCformat, tem);
1049
f7a84cb4
JR
1050 return entity;
1051}
1052
d205d43b
JR
1053
1054/* Convert generic families to the family portion of lfPitchAndFamily. */
1055BYTE
1056w32_generic_family (Lisp_Object name)
1057{
1058 /* Generic families. */
1059 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1060 return FF_MODERN;
9e1a2995 1061 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
d205d43b
JR
1062 return FF_SWISS;
1063 else if (EQ (name, Qserif))
1064 return FF_ROMAN;
1065 else if (EQ (name, Qdecorative))
1066 return FF_DECORATIVE;
1067 else if (EQ (name, Qscript))
1068 return FF_SCRIPT;
1069 else
1070 return FF_DONTCARE;
1071}
1072
1073static int
1074logfonts_match (font, pattern)
1075 LOGFONT *font, *pattern;
1076{
1077 /* Only check height for raster fonts. */
1078 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1079 && font->lfHeight != pattern->lfHeight)
1080 return 0;
1081
1082 /* Have some flexibility with weights. */
1083 if (pattern->lfWeight
1084 && ((font->lfWeight < (pattern->lfWeight - 150))
1085 || font->lfWeight > (pattern->lfWeight + 150)))
1086 return 0;
1087
1088 /* Charset and face should be OK. Italic has to be checked
1089 against the original spec, in case we don't have any preference. */
1090 return 1;
1091}
1092
1093static int
56df6710 1094font_matches_spec (type, font, spec, backend, logfont)
d205d43b
JR
1095 DWORD type;
1096 NEWTEXTMETRICEX *font;
1097 Lisp_Object spec;
56df6710
JR
1098 Lisp_Object backend;
1099 LOGFONT *logfont;
d205d43b
JR
1100{
1101 Lisp_Object extra, val;
1102
1103 /* Check italic. Can't check logfonts, since it is a boolean field,
1104 so there is no difference between "non-italic" and "don't care". */
5f18d119
KH
1105 {
1106 int slant = FONT_SLANT_NUMERIC (spec);
1107
1108 if (slant >= 0
1109 && ((slant > 150 && !font->ntmTm.tmItalic)
1110 || (slant <= 150 && font->ntmTm.tmItalic)))
1111 return 0;
1112 }
d205d43b 1113
4f2a2ee2
JR
1114 /* Check adstyle against generic family. */
1115 val = AREF (spec, FONT_ADSTYLE_INDEX);
1116 if (!NILP (val))
1117 {
1118 BYTE family = w32_generic_family (val);
1119 if (family != FF_DONTCARE
1120 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1121 return 0;
1122 }
1123
5f18d119
KH
1124 /* Check spacing */
1125 val = AREF (spec, FONT_SPACING_INDEX);
1126 if (INTEGERP (val))
1127 {
1128 int spacing = XINT (val);
1129 int proportional = (spacing < FONT_SPACING_MONO);
1130
1131 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1132 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1133 return 0;
1134 }
1135
d205d43b
JR
1136 /* Check extra parameters. */
1137 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1138 CONSP (extra); extra = XCDR (extra))
1139 {
1140 Lisp_Object extra_entry;
1141 extra_entry = XCAR (extra);
1142 if (CONSP (extra_entry))
1143 {
1144 Lisp_Object key = XCAR (extra_entry);
d205d43b 1145
5f18d119
KH
1146 val = XCDR (extra_entry);
1147 if (EQ (key, QCscript) && SYMBOLP (val))
d205d43b
JR
1148 {
1149 /* Only truetype fonts will have information about what
1150 scripts they support. This probably means the user
1151 will have to force Emacs to use raster, postscript
1152 or atm fonts for non-ASCII text. */
1153 if (type & TRUETYPE_FONTTYPE)
1154 {
1155 Lisp_Object support
1156 = font_supported_scripts (&font->ntmFontSig);
1157 if (! memq_no_quit (val, support))
1158 return 0;
1159 }
1160 else
1161 {
1162 /* Return specific matches, but play it safe. Fonts
1163 that cover more than their charset would suggest
1164 are likely to be truetype or opentype fonts,
1165 covered above. */
1166 if (EQ (val, Qlatin))
1167 {
1168 /* Although every charset but symbol, thai and
1169 arabic contains the basic ASCII set of latin
1170 characters, Emacs expects much more. */
1171 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1172 return 0;
1173 }
1174 else if (EQ (val, Qsymbol))
1175 {
1176 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1177 return 0;
1178 }
1179 else if (EQ (val, Qcyrillic))
1180 {
1181 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1182 return 0;
1183 }
1184 else if (EQ (val, Qgreek))
1185 {
1186 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1187 return 0;
1188 }
1189 else if (EQ (val, Qarabic))
1190 {
1191 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1192 return 0;
1193 }
1194 else if (EQ (val, Qhebrew))
1195 {
1196 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1197 return 0;
1198 }
1199 else if (EQ (val, Qthai))
1200 {
1201 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1202 return 0;
1203 }
1204 else if (EQ (val, Qkana))
1205 {
1206 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1207 return 0;
1208 }
1209 else if (EQ (val, Qbopomofo))
1210 {
1211 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1212 return 0;
1213 }
1214 else if (EQ (val, Qhangul))
1215 {
1216 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1217 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1218 return 0;
1219 }
1220 else if (EQ (val, Qhan))
1221 {
1222 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1223 && font->ntmTm.tmCharSet != GB2312_CHARSET
1224 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1225 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1226 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1227 return 0;
1228 }
1229 else
56df6710
JR
1230 /* Other scripts unlikely to be handled by non-truetype
1231 fonts. */
d205d43b
JR
1232 return 0;
1233 }
1234 }
5f18d119 1235 else if (EQ (key, QClang) && SYMBOLP (val))
8f112d52
JR
1236 {
1237 /* Just handle the CJK languages here, as the language
1238 parameter is used to select a font with appropriate
1239 glyphs in the cjk unified ideographs block. Other fonts
1240 support for a language can be solely determined by
1241 its character coverage. */
1242 if (EQ (val, Qja))
1243 {
1244 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1245 return 0;
1246 }
1247 else if (EQ (val, Qko))
1248 {
1249 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1250 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1251 return 0;
1252 }
1253 else if (EQ (val, Qzh))
1254 {
1255 if (font->ntmTm.tmCharSet != GB2312_CHARSET
1256 && font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1257 return 0;
1258 }
1259 else
1260 /* Any other language, we don't recognize it. Fontset
1261 spec should have a fallback, as some backends do
1262 not recognize language at all. */
1263 return 0;
1264 }
56df6710
JR
1265 else if (EQ (key, QCotf) && CONSP (val))
1266 {
1267 /* OTF features only supported by the uniscribe backend. */
1268 if (EQ (backend, Quniscribe))
1269 {
1270 if (!uniscribe_check_otf (logfont, val))
1271 return 0;
1272 }
1273 else
1274 return 0;
1275 }
d205d43b
JR
1276 }
1277 }
1278 return 1;
1279}
1280
c960bff8
JR
1281static int
1282w32font_coverage_ok (coverage, charset)
1283 FONTSIGNATURE * coverage;
1284 BYTE charset;
1285{
1286 DWORD subrange1 = coverage->fsUsb[1];
1287
1288#define SUBRANGE1_HAN_MASK 0x08000000
1289#define SUBRANGE1_HANGEUL_MASK 0x01000000
1290#define SUBRANGE1_JAPANESE_MASK (0x00060000 | SUBRANGE1_HAN_MASK)
1291
1292 if (charset == GB2312_CHARSET || charset == CHINESEBIG5_CHARSET)
1293 {
1294 return (subrange1 & SUBRANGE1_HAN_MASK) == SUBRANGE1_HAN_MASK;
1295 }
1296 else if (charset == SHIFTJIS_CHARSET)
1297 {
1298 return (subrange1 & SUBRANGE1_JAPANESE_MASK) == SUBRANGE1_JAPANESE_MASK;
1299 }
1300 else if (charset == HANGEUL_CHARSET)
1301 {
1302 return (subrange1 & SUBRANGE1_HANGEUL_MASK) == SUBRANGE1_HANGEUL_MASK;
1303 }
1304
1305 return 1;
1306}
1307
f7a84cb4 1308/* Callback function for EnumFontFamiliesEx.
d205d43b
JR
1309 * Checks if a font matches everything we are trying to check agaist,
1310 * and if so, adds it to a list. Both the data we are checking against
1311 * and the list to which the fonts are added are passed in via the
1312 * lparam argument, in the form of a font_callback_data struct. */
20399669 1313static int CALLBACK
d205d43b 1314add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
20399669
JR
1315 ENUMLOGFONTEX *logical_font;
1316 NEWTEXTMETRICEX *physical_font;
1317 DWORD font_type;
d205d43b 1318 LPARAM lParam;
f7a84cb4 1319{
d205d43b
JR
1320 struct font_callback_data *match_data
1321 = (struct font_callback_data *) lParam;
56df6710 1322 Lisp_Object backend = match_data->opentype_only ? Quniscribe : Qgdi;
f7a84cb4 1323
46fd1ded 1324 if ((!match_data->opentype_only
ef8e283d
JR
1325 || (((physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
1326 || (font_type & TRUETYPE_FONTTYPE))
1327 /* For the uniscribe backend, only consider fonts that claim
1328 to cover at least some part of Unicode. */
1329 && (physical_font->ntmFontSig.fsUsb[3]
1330 || physical_font->ntmFontSig.fsUsb[2]
1331 || physical_font->ntmFontSig.fsUsb[1]
1332 || (physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff))))
46fd1ded 1333 && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
d205d43b 1334 && font_matches_spec (font_type, physical_font,
56df6710
JR
1335 match_data->orig_font_spec, backend,
1336 &logical_font->elfLogFont)
c960bff8
JR
1337 && w32font_coverage_ok (&physical_font->ntmFontSig,
1338 match_data->pattern.lfCharSet)
4f2a2ee2
JR
1339 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1340 We limit this to raster fonts, because the test can catch some
1341 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1342 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1343 therefore get through this test. Since full names can be prefixed
1344 by a foundry, we accept raster fonts if the font name is found
1345 anywhere within the full name. */
1346 && (logical_font->elfLogFont.lfOutPrecision != OUT_STRING_PRECIS
1347 || strstr (logical_font->elfFullName,
1348 logical_font->elfLogFont.lfFaceName)))
d205d43b
JR
1349 {
1350 Lisp_Object entity
1351 = w32_enumfont_pattern_entity (match_data->frame, logical_font,
91583281 1352 physical_font, font_type,
34fd2d28 1353 &match_data->pattern,
56df6710 1354 backend);
d205d43b 1355 if (!NILP (entity))
aee986fd
JR
1356 {
1357 Lisp_Object spec_charset = AREF (match_data->orig_font_spec,
1358 FONT_REGISTRY_INDEX);
1359
1360 /* If registry was specified as iso10646-1, only report
1361 ANSI and DEFAULT charsets, as most unicode fonts will
1362 contain one of those plus others. */
1363 if (EQ (spec_charset, Qiso10646_1)
1364 && logical_font->elfLogFont.lfCharSet != DEFAULT_CHARSET
1365 && logical_font->elfLogFont.lfCharSet != ANSI_CHARSET)
1366 return 1;
1367 /* If registry was specified, but did not map to a windows
1368 charset, only report fonts that have unknown charsets.
1369 This will still report fonts that don't match, but at
1370 least it eliminates known definite mismatches. */
1371 else if (!NILP (spec_charset)
1372 && !EQ (spec_charset, Qiso10646_1)
1373 && match_data->pattern.lfCharSet == DEFAULT_CHARSET
1374 && logical_font->elfLogFont.lfCharSet != DEFAULT_CHARSET)
1375 return 1;
1376
1377 /* If registry was specified, ensure it is reported as the same. */
1378 if (!NILP (spec_charset))
1379 ASET (entity, FONT_REGISTRY_INDEX, spec_charset);
1380
1381 match_data->list = Fcons (entity, match_data->list);
1382
1383 /* If no registry specified, duplicate iso8859-1 truetype fonts
1384 as iso10646-1. */
1385 if (NILP (spec_charset)
1386 && font_type == TRUETYPE_FONTTYPE
1387 && logical_font->elfLogFont.lfCharSet == ANSI_CHARSET)
1388 {
1389 Lisp_Object tem = Fcopy_font_spec (entity);
1390 ASET (tem, FONT_REGISTRY_INDEX, Qiso10646_1);
1391 match_data->list = Fcons (tem, match_data->list);
1392 }
1393 }
d205d43b 1394 }
f7a84cb4
JR
1395 return 1;
1396}
1397
1398/* Callback function for EnumFontFamiliesEx.
d205d43b 1399 * Terminates the search once we have a match. */
20399669 1400static int CALLBACK
d205d43b 1401add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
20399669
JR
1402 ENUMLOGFONTEX *logical_font;
1403 NEWTEXTMETRICEX *physical_font;
1404 DWORD font_type;
d205d43b 1405 LPARAM lParam;
f7a84cb4 1406{
d205d43b
JR
1407 struct font_callback_data *match_data
1408 = (struct font_callback_data *) lParam;
1409 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1410
1411 /* If we have a font in the list, terminate the search. */
1412 return !NILP (match_data->list);
f7a84cb4
JR
1413}
1414
1415/* Convert a Lisp font registry (symbol) to a windows charset. */
20399669
JR
1416static LONG
1417registry_to_w32_charset (charset)
1418 Lisp_Object charset;
f7a84cb4
JR
1419{
1420 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1421 || EQ (charset, Qunicode_sip))
1422 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1423 else if (EQ (charset, Qiso8859_1))
1424 return ANSI_CHARSET;
040fe918
JR
1425 else if (SYMBOLP (charset))
1426 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
f7a84cb4
JR
1427 else
1428 return DEFAULT_CHARSET;
1429}
1430
20399669 1431static Lisp_Object
c960bff8 1432w32_registry (w32_charset, font_type)
20399669 1433 LONG w32_charset;
c960bff8 1434 DWORD font_type;
f7a84cb4 1435{
aee986fd
JR
1436 char *charset;
1437
1438 /* If charset is defaulted, charset is unicode or unknown, depending on
1439 font type. */
c960bff8 1440 if (w32_charset == DEFAULT_CHARSET)
aee986fd 1441 return font_type == TRUETYPE_FONTTYPE ? Qiso10646_1 : Qunknown;
c960bff8 1442
aee986fd
JR
1443 charset = w32_to_x_charset (w32_charset, NULL);
1444 return font_intern_prop (charset, strlen(charset));
f7a84cb4
JR
1445}
1446
5f18d119
KH
1447static int
1448w32_decode_weight (fnweight)
1449 int fnweight;
1450{
1451 if (fnweight >= FW_HEAVY) return 210;
1452 if (fnweight >= FW_EXTRABOLD) return 205;
1453 if (fnweight >= FW_BOLD) return 200;
1454 if (fnweight >= FW_SEMIBOLD) return 180;
1455 if (fnweight >= FW_NORMAL) return 100;
1456 if (fnweight >= FW_LIGHT) return 50;
1457 if (fnweight >= FW_EXTRALIGHT) return 40;
1458 if (fnweight > FW_THIN) return 20;
1459 return 0;
1460}
1461
1462static int
1463w32_encode_weight (n)
1464 int n;
1465{
1466 if (n >= 210) return FW_HEAVY;
1467 if (n >= 205) return FW_EXTRABOLD;
1468 if (n >= 200) return FW_BOLD;
1469 if (n >= 180) return FW_SEMIBOLD;
1470 if (n >= 100) return FW_NORMAL;
1471 if (n >= 50) return FW_LIGHT;
1472 if (n >= 40) return FW_EXTRALIGHT;
1473 if (n >= 20) return FW_THIN;
1474 return 0;
1475}
1476
f7a84cb4 1477/* Fill in all the available details of LOGFONT from FONT_SPEC. */
20399669
JR
1478static void
1479fill_in_logfont (f, logfont, font_spec)
1480 FRAME_PTR f;
1481 LOGFONT *logfont;
1482 Lisp_Object font_spec;
f7a84cb4 1483{
d205d43b 1484 Lisp_Object tmp, extra;
f7a84cb4
JR
1485 int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;
1486
5f18d119
KH
1487 tmp = AREF (font_spec, FONT_DPI_INDEX);
1488 if (INTEGERP (tmp))
d205d43b 1489 {
5f18d119
KH
1490 dpi = XINT (tmp);
1491 }
1492 else if (FLOATP (tmp))
1493 {
1494 dpi = (int) (XFLOAT_DATA (tmp) + 0.5);
d205d43b 1495 }
f7a84cb4
JR
1496
1497 /* Height */
1498 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1499 if (INTEGERP (tmp))
040fe918 1500 logfont->lfHeight = -1 * XINT (tmp);
f7a84cb4 1501 else if (FLOATP (tmp))
d205d43b 1502 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
f7a84cb4
JR
1503
1504 /* Escapement */
1505
1506 /* Orientation */
1507
1508 /* Weight */
1509 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1510 if (INTEGERP (tmp))
5f18d119 1511 logfont->lfWeight = w32_encode_weight (FONT_WEIGHT_NUMERIC (font_spec));
f7a84cb4
JR
1512
1513 /* Italic */
1514 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1515 if (INTEGERP (tmp))
1516 {
5f18d119 1517 int slant = FONT_SLANT_NUMERIC (font_spec);
f7a84cb4
JR
1518 logfont->lfItalic = slant > 150 ? 1 : 0;
1519 }
1520
1521 /* Underline */
1522
1523 /* Strikeout */
1524
1525 /* Charset */
1526 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1527 if (! NILP (tmp))
d205d43b 1528 logfont->lfCharSet = registry_to_w32_charset (tmp);
56df6710
JR
1529 else
1530 logfont->lfCharSet = DEFAULT_CHARSET;
f7a84cb4
JR
1531
1532 /* Out Precision */
91583281 1533
f7a84cb4 1534 /* Clip Precision */
91583281
JR
1535
1536 /* Quality */
040fe918
JR
1537 logfont->lfQuality = DEFAULT_QUALITY;
1538
d205d43b
JR
1539 /* Generic Family and Face Name */
1540 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1541
f7a84cb4 1542 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
d205d43b
JR
1543 if (! NILP (tmp))
1544 {
1545 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1546 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1547 ; /* Font name was generic, don't fill in font name. */
1548 /* Font families are interned, but allow for strings also in case of
1549 user input. */
1550 else if (SYMBOLP (tmp))
1551 strncpy (logfont->lfFaceName, SDATA (SYMBOL_NAME (tmp)), LF_FACESIZE);
d205d43b 1552 }
f7a84cb4 1553
9e1a2995
JR
1554 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1555 if (!NILP (tmp))
1556 {
1557 /* Override generic family. */
1558 BYTE family = w32_generic_family (tmp);
1559 if (family != FF_DONTCARE)
1560 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
1561 }
1562
9c623c85 1563
5f18d119
KH
1564 /* Set pitch based on the spacing property. */
1565 tmp = AREF (font_spec, FONT_SPACING_INDEX);
1566 if (INTEGERP (tmp))
1567 {
1568 int spacing = XINT (tmp);
1569 if (spacing < FONT_SPACING_MONO)
1570 logfont->lfPitchAndFamily
1571 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1572 else
1573 logfont->lfPitchAndFamily
1574 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1575 }
1576
d205d43b 1577 /* Process EXTRA info. */
5f18d119
KH
1578 for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
1579 CONSP (extra); extra = XCDR (extra))
d205d43b
JR
1580 {
1581 tmp = XCAR (extra);
1582 if (CONSP (tmp))
1583 {
1584 Lisp_Object key, val;
1585 key = XCAR (tmp), val = XCDR (tmp);
d205d43b
JR
1586 /* Only use QCscript if charset is not provided, or is unicode
1587 and a single script is specified. This is rather crude,
1588 and is only used to narrow down the fonts returned where
1589 there is a definite match. Some scripts, such as latin, han,
1590 cjk-misc match multiple lfCharSet values, so we can't pre-filter
1591 them. */
5f18d119 1592 if (EQ (key, QCscript)
d205d43b
JR
1593 && logfont->lfCharSet == DEFAULT_CHARSET
1594 && SYMBOLP (val))
1595 {
1596 if (EQ (val, Qgreek))
1597 logfont->lfCharSet = GREEK_CHARSET;
1598 else if (EQ (val, Qhangul))
1599 logfont->lfCharSet = HANGUL_CHARSET;
1600 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
1601 logfont->lfCharSet = SHIFTJIS_CHARSET;
1602 else if (EQ (val, Qbopomofo))
1603 logfont->lfCharSet = CHINESEBIG5_CHARSET;
1604 /* GB 18030 supports tibetan, yi, mongolian,
1605 fonts that support it should show up if we ask for
1606 GB2312 fonts. */
1607 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
1608 || EQ (val, Qmongolian))
1609 logfont->lfCharSet = GB2312_CHARSET;
1610 else if (EQ (val, Qhebrew))
1611 logfont->lfCharSet = HEBREW_CHARSET;
1612 else if (EQ (val, Qarabic))
1613 logfont->lfCharSet = ARABIC_CHARSET;
1614 else if (EQ (val, Qthai))
1615 logfont->lfCharSet = THAI_CHARSET;
1616 else if (EQ (val, Qsymbol))
1617 logfont->lfCharSet = SYMBOL_CHARSET;
1618 }
91583281
JR
1619 else if (EQ (key, QCantialias) && SYMBOLP (val))
1620 {
1621 logfont->lfQuality = w32_antialias_type (val);
1622 }
d205d43b
JR
1623 }
1624 }
f7a84cb4
JR
1625}
1626
20399669 1627static void
d205d43b
JR
1628list_all_matching_fonts (match_data)
1629 struct font_callback_data *match_data;
f7a84cb4
JR
1630{
1631 HDC dc;
d205d43b
JR
1632 Lisp_Object families = w32font_list_family (match_data->frame);
1633 struct frame *f = XFRAME (match_data->frame);
f7a84cb4
JR
1634
1635 dc = get_frame_dc (f);
1636
1637 while (!NILP (families))
1638 {
d205d43b
JR
1639 /* TODO: Use the Unicode versions of the W32 APIs, so we can
1640 handle non-ASCII font names. */
1641 char *name;
f7a84cb4
JR
1642 Lisp_Object family = CAR (families);
1643 families = CDR (families);
d205d43b
JR
1644 if (NILP (family))
1645 continue;
8f112d52 1646 else if (SYMBOLP (family))
9c623c85 1647 name = SDATA (SYMBOL_NAME (family));
8f112d52
JR
1648 else
1649 continue;
d205d43b
JR
1650
1651 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
1652 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
1653
1654 EnumFontFamiliesEx (dc, &match_data->pattern,
1655 (FONTENUMPROC) add_font_entity_to_list,
1656 (LPARAM) match_data, 0);
f7a84cb4
JR
1657 }
1658
1659 release_frame_dc (f, dc);
1660}
1661
91583281
JR
1662static Lisp_Object
1663lispy_antialias_type (type)
1664 BYTE type;
1665{
1666 Lisp_Object lispy;
1667
1668 switch (type)
1669 {
1670 case NONANTIALIASED_QUALITY:
1671 lispy = Qnone;
1672 break;
1673 case ANTIALIASED_QUALITY:
1674 lispy = Qstandard;
1675 break;
1676 case CLEARTYPE_QUALITY:
1677 lispy = Qsubpixel;
1678 break;
1679 case CLEARTYPE_NATURAL_QUALITY:
1680 lispy = Qnatural;
1681 break;
1682 default:
1683 lispy = Qnil;
1684 break;
1685 }
1686 return lispy;
1687}
1688
1689/* Convert antialiasing symbols to lfQuality */
1690static BYTE
1691w32_antialias_type (type)
1692 Lisp_Object type;
1693{
1694 if (EQ (type, Qnone))
1695 return NONANTIALIASED_QUALITY;
1696 else if (EQ (type, Qstandard))
1697 return ANTIALIASED_QUALITY;
1698 else if (EQ (type, Qsubpixel))
1699 return CLEARTYPE_QUALITY;
1700 else if (EQ (type, Qnatural))
1701 return CLEARTYPE_NATURAL_QUALITY;
1702 else
1703 return DEFAULT_QUALITY;
1704}
1705
d205d43b
JR
1706/* Return a list of all the scripts that the font supports. */
1707static Lisp_Object
1708font_supported_scripts (FONTSIGNATURE * sig)
f7a84cb4 1709{
d205d43b
JR
1710 DWORD * subranges = sig->fsUsb;
1711 Lisp_Object supported = Qnil;
1712
1713 /* Match a single subrange. SYM is set if bit N is set in subranges. */
1714#define SUBRANGE(n,sym) \
1715 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
1716 supported = Fcons ((sym), supported)
1717
1718 /* Match multiple subranges. SYM is set if any MASK bit is set in
1719 subranges[0 - 3]. */
1720#define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
1721 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
1722 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
1723 supported = Fcons ((sym), supported)
1724
56df6710
JR
1725 SUBRANGE (0, Qlatin);
1726 /* The following count as latin too, ASCII should be present in these fonts,
1727 so don't need to mark them separately. */
1728 /* 1: Latin-1 supplement, 2: Latin Extended A, 3: Latin Extended B. */
1729 SUBRANGE (4, Qphonetic);
1730 /* 5: Spacing and tone modifiers, 6: Combining Diacriticals. */
d205d43b
JR
1731 SUBRANGE (7, Qgreek);
1732 SUBRANGE (8, Qcoptic);
1733 SUBRANGE (9, Qcyrillic);
1734 SUBRANGE (10, Qarmenian);
1735 SUBRANGE (11, Qhebrew);
1736 SUBRANGE (13, Qarabic);
1737 SUBRANGE (14, Qnko);
1738 SUBRANGE (15, Qdevanagari);
1739 SUBRANGE (16, Qbengali);
1740 SUBRANGE (17, Qgurmukhi);
1741 SUBRANGE (18, Qgujarati);
1742 SUBRANGE (19, Qoriya);
1743 SUBRANGE (20, Qtamil);
1744 SUBRANGE (21, Qtelugu);
1745 SUBRANGE (22, Qkannada);
1746 SUBRANGE (23, Qmalayalam);
1747 SUBRANGE (24, Qthai);
1748 SUBRANGE (25, Qlao);
1749 SUBRANGE (26, Qgeorgian);
56df6710
JR
1750 SUBRANGE (27, Qbalinese);
1751 /* 28: Hangul Jamo. */
1752 /* 29: Latin Extended, 30: Greek Extended, 31: Punctuation. */
1753 /* 32-47: Symbols (defined below). */
d205d43b 1754 SUBRANGE (48, Qcjk_misc);
56df6710
JR
1755 /* Match either 49: katakana or 50: hiragana for kana. */
1756 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
d205d43b 1757 SUBRANGE (51, Qbopomofo);
56df6710
JR
1758 /* 52: Compatibility Jamo */
1759 SUBRANGE (53, Qphags_pa);
1760 /* 54: Enclosed CJK letters and months, 55: CJK Compatibility. */
d205d43b 1761 SUBRANGE (56, Qhangul);
56df6710
JR
1762 /* 57: Surrogates. */
1763 SUBRANGE (58, Qphoenician);
d205d43b 1764 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
56df6710
JR
1765 SUBRANGE (59, Qideographic_description); /* Windows lumps this in. */
1766 SUBRANGE (59, Qkanbun); /* And this. */
1767 /* 60: Private use, 61: CJK strokes and compatibility. */
1768 /* 62: Alphabetic Presentation, 63: Arabic Presentation A. */
1769 /* 64: Combining half marks, 65: Vertical and CJK compatibility. */
1770 /* 66: Small forms, 67: Arabic Presentation B, 68: Half and Full width. */
1771 /* 69: Specials. */
d205d43b
JR
1772 SUBRANGE (70, Qtibetan);
1773 SUBRANGE (71, Qsyriac);
1774 SUBRANGE (72, Qthaana);
1775 SUBRANGE (73, Qsinhala);
1776 SUBRANGE (74, Qmyanmar);
1777 SUBRANGE (75, Qethiopic);
1778 SUBRANGE (76, Qcherokee);
1779 SUBRANGE (77, Qcanadian_aboriginal);
1780 SUBRANGE (78, Qogham);
1781 SUBRANGE (79, Qrunic);
1782 SUBRANGE (80, Qkhmer);
1783 SUBRANGE (81, Qmongolian);
1784 SUBRANGE (82, Qbraille);
1785 SUBRANGE (83, Qyi);
56df6710
JR
1786 SUBRANGE (84, Qbuhid);
1787 SUBRANGE (84, Qhanunoo);
1788 SUBRANGE (84, Qtagalog);
1789 SUBRANGE (84, Qtagbanwa);
1790 SUBRANGE (85, Qold_italic);
1791 SUBRANGE (86, Qgothic);
1792 SUBRANGE (87, Qdeseret);
d205d43b
JR
1793 SUBRANGE (88, Qbyzantine_musical_symbol);
1794 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
d205d43b 1795 SUBRANGE (89, Qmathematical);
56df6710
JR
1796 /* 90: Private use, 91: Variation selectors, 92: Tags. */
1797 SUBRANGE (93, Qlimbu);
1798 SUBRANGE (94, Qtai_le);
1799 /* 95: New Tai Le */
1800 SUBRANGE (90, Qbuginese);
1801 SUBRANGE (97, Qglagolitic);
1802 SUBRANGE (98, Qtifinagh);
1803 /* 99: Yijing Hexagrams. */
1804 SUBRANGE (100, Qsyloti_nagri);
1805 SUBRANGE (101, Qlinear_b);
1806 /* 102: Ancient Greek Numbers. */
1807 SUBRANGE (103, Qugaritic);
1808 SUBRANGE (104, Qold_persian);
1809 SUBRANGE (105, Qshavian);
1810 SUBRANGE (106, Qosmanya);
1811 SUBRANGE (107, Qcypriot);
1812 SUBRANGE (108, Qkharoshthi);
1813 /* 109: Tai Xuan Jing. */
1814 SUBRANGE (110, Qcuneiform);
1815 /* 111: Counting Rods. */
d205d43b
JR
1816
1817 /* There isn't really a main symbol range, so include symbol if any
1818 relevant range is set. */
1819 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
1820
56df6710 1821 /* Missing: Tai Viet (U+AA80) and Cham (U+AA00) . */
d205d43b
JR
1822#undef SUBRANGE
1823#undef MASK_ANY
1824
1825 return supported;
f7a84cb4
JR
1826}
1827
67997c79
JR
1828/* Generate a full name for a Windows font.
1829 The full name is in fcname format, with weight, slant and antialiasing
1830 specified if they are not "normal". */
1831static int
1832w32font_full_name (font, font_obj, pixel_size, name, nbytes)
1833 LOGFONT * font;
1834 Lisp_Object font_obj;
1835 int pixel_size;
1836 char *name;
1837 int nbytes;
1838{
a823468b 1839 int len, height, outline;
67997c79
JR
1840 char *p;
1841 Lisp_Object antialiasing, weight = Qnil;
1842
a823468b
JR
1843 len = strlen (font->lfFaceName);
1844
1845 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
1846
1847 /* Represent size of scalable fonts by point size. But use pixelsize for
1848 raster fonts to indicate that they are exactly that size. */
1849 if (outline)
1850 len += 11; /* -SIZE */
1851 else
1852 len = strlen (font->lfFaceName) + 21;
67997c79
JR
1853
1854 if (font->lfItalic)
1855 len += 7; /* :italic */
1856
1857 if (font->lfWeight && font->lfWeight != FW_NORMAL)
1858 {
5f18d119 1859 weight = FONT_WEIGHT_SYMBOLIC (font_obj);
67997c79
JR
1860 len += 8 + SBYTES (SYMBOL_NAME (weight)); /* :weight=NAME */
1861 }
1862
1863 antialiasing = lispy_antialias_type (font->lfQuality);
1864 if (! NILP (antialiasing))
1865 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
1866
1867 /* Check that the buffer is big enough */
1868 if (len > nbytes)
1869 return -1;
1870
1871 p = name;
1872 p += sprintf (p, "%s", font->lfFaceName);
1873
a823468b
JR
1874 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
1875
1876 if (height > 0)
1877 {
1878 if (outline)
1879 {
1880 float pointsize = height * 72.0 / one_w32_display_info.resy;
7145be81
JR
1881 /* Round to nearest half point. floor is used, since round is not
1882 supported in MS library. */
1883 pointsize = floor (pointsize * 2 + 0.5) / 2;
a823468b
JR
1884 p += sprintf (p, "-%1.1f", pointsize);
1885 }
1886 else
1887 p += sprintf (p, ":pixelsize=%d", height);
1888 }
67997c79
JR
1889
1890 if (font->lfItalic)
1891 p += sprintf (p, ":italic");
1892
1893 if (SYMBOLP (weight) && ! NILP (weight))
1894 p += sprintf (p, ":weight=%s", SDATA (SYMBOL_NAME (weight)));
1895
1896 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
1897 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
1898
1899 return (p - name);
1900}
f7a84cb4 1901
34fd2d28 1902
9c623c85
JB
1903static void
1904compute_metrics (dc, w32_font, code, metrics)
34fd2d28 1905 HDC dc;
54efdcd1 1906 struct w32font_info *w32_font;
8f112d52
JR
1907 unsigned int code;
1908 struct w32_metric_cache *metrics;
34fd2d28
JR
1909{
1910 GLYPHMETRICS gm;
1911 MAT2 transform;
8f112d52
JR
1912 unsigned int options = GGO_METRICS;
1913
1914 if (w32_font->glyph_idx)
1915 options |= GGO_GLYPH_INDEX;
34fd2d28
JR
1916
1917 bzero (&transform, sizeof (transform));
1918 transform.eM11.value = 1;
1919 transform.eM22.value = 1;
8f112d52
JR
1920
1921 if (GetGlyphOutlineW (dc, code, options, &gm, 0, NULL, &transform)
1922 != GDI_ERROR)
1923 {
1924 metrics->lbearing = gm.gmptGlyphOrigin.x;
1925 metrics->rbearing = gm.gmptGlyphOrigin.x + gm.gmBlackBoxX;
1926 metrics->width = gm.gmCellIncX;
1927 metrics->status = W32METRIC_SUCCESS;
1928 }
9c623c85 1929 else if (w32_font->glyph_idx)
34fd2d28 1930 {
9c623c85
JB
1931 /* Can't use glyph indexes after all.
1932 Avoid it in future, and clear any metrics that were based on
1933 glyph indexes. */
1934 w32_font->glyph_idx = 0;
1935 clear_cached_metrics (w32_font);
34fd2d28 1936 }
9c623c85
JB
1937 else
1938 metrics->status = W32METRIC_FAIL;
34fd2d28
JR
1939}
1940
8f112d52
JR
1941static void
1942clear_cached_metrics (w32_font)
1943 struct w32font_info *w32_font;
1944{
1945 int i;
1946 for (i = 0; i < w32_font->n_cache_blocks; i++)
5f18d119
KH
1947 {
1948 if (w32_font->cached_metrics[i])
1949 bzero (w32_font->cached_metrics[i],
1950 CACHE_BLOCKSIZE * sizeof (struct font_metrics));
1951 }
8f112d52
JR
1952}
1953
f7a84cb4
JR
1954struct font_driver w32font_driver =
1955 {
8eac0c84 1956 0, /* Qgdi */
5f18d119 1957 0, /* case insensitive */
f7a84cb4
JR
1958 w32font_get_cache,
1959 w32font_list,
1960 w32font_match,
1961 w32font_list_family,
1962 NULL, /* free_entity */
1963 w32font_open,
1964 w32font_close,
1965 NULL, /* prepare_face */
1966 NULL, /* done_face */
1967 w32font_has_char,
1968 w32font_encode_char,
1969 w32font_text_extents,
1970 w32font_draw,
1971 NULL, /* get_bitmap */
1972 NULL, /* free_bitmap */
1973 NULL, /* get_outline */
1974 NULL, /* free_outline */
1975 NULL, /* anchor_point */
1976 NULL, /* otf_capability */
5b0c3446
JR
1977 NULL, /* otf_drive */
1978 NULL, /* start_for_frame */
1979 NULL, /* end_for_frame */
1980 NULL /* shape */
f7a84cb4
JR
1981 };
1982
f7a84cb4
JR
1983
1984/* Initialize state that does not change between invocations. This is only
1985 called when Emacs is dumped. */
20399669
JR
1986void
1987syms_of_w32font ()
f7a84cb4 1988{
8eac0c84 1989 DEFSYM (Qgdi, "gdi");
34fd2d28
JR
1990 DEFSYM (Quniscribe, "uniscribe");
1991 DEFSYM (QCformat, ":format");
d205d43b
JR
1992
1993 /* Generic font families. */
1994 DEFSYM (Qmonospace, "monospace");
1995 DEFSYM (Qserif, "serif");
9e1a2995 1996 DEFSYM (Qsansserif, "sansserif");
f7a84cb4 1997 DEFSYM (Qscript, "script");
d205d43b
JR
1998 DEFSYM (Qdecorative, "decorative");
1999 /* Aliases. */
9e1a2995 2000 DEFSYM (Qsans_serif, "sans_serif");
d205d43b
JR
2001 DEFSYM (Qsans, "sans");
2002 DEFSYM (Qmono, "mono");
2003
2004 /* Fake foundries. */
2005 DEFSYM (Qraster, "raster");
2006 DEFSYM (Qoutline, "outline");
f7a84cb4 2007 DEFSYM (Qunknown, "unknown");
d205d43b 2008
91583281
JR
2009 /* Antialiasing. */
2010 DEFSYM (Qstandard, "standard");
2011 DEFSYM (Qsubpixel, "subpixel");
2012 DEFSYM (Qnatural, "natural");
d205d43b 2013
8f112d52
JR
2014 /* Languages */
2015 DEFSYM (Qja, "ja");
2016 DEFSYM (Qko, "ko");
2017 DEFSYM (Qzh, "zh");
2018
d205d43b
JR
2019 /* Scripts */
2020 DEFSYM (Qlatin, "latin");
2021 DEFSYM (Qgreek, "greek");
2022 DEFSYM (Qcoptic, "coptic");
2023 DEFSYM (Qcyrillic, "cyrillic");
2024 DEFSYM (Qarmenian, "armenian");
2025 DEFSYM (Qhebrew, "hebrew");
2026 DEFSYM (Qarabic, "arabic");
2027 DEFSYM (Qsyriac, "syriac");
2028 DEFSYM (Qnko, "nko");
2029 DEFSYM (Qthaana, "thaana");
2030 DEFSYM (Qdevanagari, "devanagari");
2031 DEFSYM (Qbengali, "bengali");
2032 DEFSYM (Qgurmukhi, "gurmukhi");
2033 DEFSYM (Qgujarati, "gujarati");
2034 DEFSYM (Qoriya, "oriya");
2035 DEFSYM (Qtamil, "tamil");
2036 DEFSYM (Qtelugu, "telugu");
2037 DEFSYM (Qkannada, "kannada");
2038 DEFSYM (Qmalayalam, "malayalam");
2039 DEFSYM (Qsinhala, "sinhala");
2040 DEFSYM (Qthai, "thai");
2041 DEFSYM (Qlao, "lao");
2042 DEFSYM (Qtibetan, "tibetan");
2043 DEFSYM (Qmyanmar, "myanmar");
2044 DEFSYM (Qgeorgian, "georgian");
2045 DEFSYM (Qhangul, "hangul");
2046 DEFSYM (Qethiopic, "ethiopic");
2047 DEFSYM (Qcherokee, "cherokee");
2048 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
2049 DEFSYM (Qogham, "ogham");
2050 DEFSYM (Qrunic, "runic");
2051 DEFSYM (Qkhmer, "khmer");
2052 DEFSYM (Qmongolian, "mongolian");
2053 DEFSYM (Qsymbol, "symbol");
2054 DEFSYM (Qbraille, "braille");
2055 DEFSYM (Qhan, "han");
2056 DEFSYM (Qideographic_description, "ideographic-description");
2057 DEFSYM (Qcjk_misc, "cjk-misc");
2058 DEFSYM (Qkana, "kana");
2059 DEFSYM (Qbopomofo, "bopomofo");
2060 DEFSYM (Qkanbun, "kanbun");
2061 DEFSYM (Qyi, "yi");
2062 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
2063 DEFSYM (Qmusical_symbol, "musical-symbol");
2064 DEFSYM (Qmathematical, "mathematical");
56df6710
JR
2065 DEFSYM (Qphonetic, "phonetic");
2066 DEFSYM (Qbalinese, "balinese");
2067 DEFSYM (Qbuginese, "buginese");
2068 DEFSYM (Qbuhid, "buhid");
2069 DEFSYM (Qcuneiform, "cuneiform");
2070 DEFSYM (Qcypriot, "cypriot");
2071 DEFSYM (Qdeseret, "deseret");
2072 DEFSYM (Qglagolitic, "glagolitic");
2073 DEFSYM (Qgothic, "gothic");
2074 DEFSYM (Qhanunoo, "hanunoo");
2075 DEFSYM (Qkharoshthi, "kharoshthi");
2076 DEFSYM (Qlimbu, "limbu");
2077 DEFSYM (Qlinear_b, "linear_b");
2078 DEFSYM (Qold_italic, "old_italic");
2079 DEFSYM (Qold_persian, "old_persian");
2080 DEFSYM (Qosmanya, "osmanya");
2081 DEFSYM (Qphags_pa, "phags-pa");
2082 DEFSYM (Qphoenician, "phoenician");
2083 DEFSYM (Qshavian, "shavian");
2084 DEFSYM (Qsyloti_nagri, "syloti_nagri");
2085 DEFSYM (Qtagalog, "tagalog");
2086 DEFSYM (Qtagbanwa, "tagbanwa");
2087 DEFSYM (Qtai_le, "tai_le");
2088 DEFSYM (Qtifinagh, "tifinagh");
2089 DEFSYM (Qugaritic, "ugaritic");
d205d43b 2090
8eac0c84 2091 w32font_driver.type = Qgdi;
f7a84cb4
JR
2092 register_font_driver (&w32font_driver, NULL);
2093}
6d8c85b5
MB
2094
2095/* arch-tag: 65b8a3cd-46aa-4c0d-a1f3-99e75b9c07ee
2096 (do not change this comment) */