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