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