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