(Fopen_font): Compute pixel size correctly.
[emacs.git] / src / w32font.c
blobc9494b4f29c2c3962098607a0bafab80b7b77807
1 /* Font backend for the Microsoft W32 API.
2 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
20 #include <windows.h>
21 #include <math.h>
22 #include <ctype.h>
23 #include <commdlg.h>
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"
33 #include "w32font.h"
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.
39 #ifndef CLEARTYPE_QUALITY
40 #define CLEARTYPE_QUALITY 5
41 #endif
42 #ifndef CLEARTYPE_NATURAL_QUALITY
43 #define CLEARTYPE_NATURAL_QUALITY 6
44 #endif
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
55 extern struct font_driver w32font_driver;
57 Lisp_Object Qgdi;
58 Lisp_Object Quniscribe;
59 static Lisp_Object QCformat;
60 static Lisp_Object Qmonospace, Qsansserif, Qmono, Qsans, Qsans_serif;
61 static Lisp_Object Qserif, Qscript, Qdecorative;
62 static Lisp_Object Qraster, Qoutline, Qunknown;
64 /* antialiasing */
65 extern Lisp_Object QCantialias, QCotf, QClang; /* defined in font.c */
66 extern Lisp_Object Qnone; /* reuse from w32fns.c */
67 static Lisp_Object Qstandard, Qsubpixel, Qnatural;
69 /* languages */
70 static Lisp_Object Qja, Qko, Qzh;
72 /* scripts */
73 static Lisp_Object Qlatin, Qgreek, Qcoptic, Qcyrillic, Qarmenian, Qhebrew;
74 static Lisp_Object Qarabic, Qsyriac, Qnko, Qthaana, Qdevanagari, Qbengali;
75 static Lisp_Object Qgurmukhi, Qgujarati, Qoriya, Qtamil, Qtelugu;
76 static Lisp_Object Qkannada, Qmalayalam, Qsinhala, Qthai, Qlao;
77 static Lisp_Object Qtibetan, Qmyanmar, Qgeorgian, Qhangul, Qethiopic;
78 static Lisp_Object Qcherokee, Qcanadian_aboriginal, Qogham, Qrunic;
79 static Lisp_Object Qkhmer, Qmongolian, Qsymbol, Qbraille, Qhan;
80 static Lisp_Object Qideographic_description, Qcjk_misc, Qkana, Qbopomofo;
81 static Lisp_Object Qkanbun, Qyi, Qbyzantine_musical_symbol;
82 static Lisp_Object Qmusical_symbol, Qmathematical;
83 /* Not defined in characters.el, but referenced in fontset.el. */
84 static Lisp_Object Qbalinese, Qbuginese, Qbuhid, Qcuneiform, Qcypriot;
85 static Lisp_Object Qdeseret, Qglagolitic, Qgothic, Qhanunoo, Qkharoshthi;
86 static Lisp_Object Qlimbu, Qlinear_b, Qold_italic, Qold_persian, Qosmanya;
87 static Lisp_Object Qphags_pa, Qphoenician, Qshavian, Qsyloti_nagri;
88 static Lisp_Object Qtagalog, Qtagbanwa, Qtai_le, Qtifinagh, Qugaritic;
89 /* Only defined here, but useful for distinguishing IPA capable fonts. */
90 static Lisp_Object Qphonetic;
92 /* W32 charsets: for use in Vw32_charset_info_alist. */
93 static Lisp_Object Qw32_charset_ansi, Qw32_charset_default;
94 static Lisp_Object Qw32_charset_symbol, Qw32_charset_shiftjis;
95 static Lisp_Object Qw32_charset_hangeul, Qw32_charset_gb2312;
96 static Lisp_Object Qw32_charset_chinesebig5, Qw32_charset_oem;
97 static Lisp_Object Qw32_charset_easteurope, Qw32_charset_turkish;
98 static Lisp_Object Qw32_charset_baltic, Qw32_charset_russian;
99 static Lisp_Object Qw32_charset_arabic, Qw32_charset_greek;
100 static Lisp_Object Qw32_charset_hebrew, Qw32_charset_vietnamese;
101 static Lisp_Object Qw32_charset_thai, Qw32_charset_johab, Qw32_charset_mac;
103 /* Associative list linking character set strings to Windows codepages. */
104 static Lisp_Object Vw32_charset_info_alist;
106 /* Font spacing symbols - defined in font.c. */
107 extern Lisp_Object Qc, Qp, Qm;
109 static void fill_in_logfont P_ ((FRAME_PTR, LOGFONT *, Lisp_Object));
111 static BYTE w32_antialias_type P_ ((Lisp_Object));
112 static Lisp_Object lispy_antialias_type P_ ((BYTE));
114 static Lisp_Object font_supported_scripts P_ ((FONTSIGNATURE *));
115 static int w32font_full_name P_ ((LOGFONT *, Lisp_Object, int, char *, int));
116 static void compute_metrics P_ ((HDC, struct w32font_info *, unsigned int,
117 struct w32_metric_cache *));
118 static void clear_cached_metrics P_ ((struct w32font_info *));
120 static Lisp_Object w32_registry P_ ((LONG, DWORD));
122 /* EnumFontFamiliesEx callbacks. */
123 static int CALLBACK add_font_entity_to_list P_ ((ENUMLOGFONTEX *,
124 NEWTEXTMETRICEX *,
125 DWORD, LPARAM));
126 static int CALLBACK add_one_font_entity_to_list P_ ((ENUMLOGFONTEX *,
127 NEWTEXTMETRICEX *,
128 DWORD, LPARAM));
129 static int CALLBACK add_font_name_to_list P_ ((ENUMLOGFONTEX *,
130 NEWTEXTMETRICEX *,
131 DWORD, LPARAM));
133 /* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
134 of what we really want. */
135 struct font_callback_data
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;
147 /* Whether to match only opentype fonts. */
148 int opentype_only;
151 /* Handles the problem that EnumFontFamiliesEx will not return all
152 style variations if the font name is not specified. */
153 static void list_all_matching_fonts P_ ((struct font_callback_data *));
156 static int
157 memq_no_quit (elt, list)
158 Lisp_Object elt, list;
160 while (CONSP (list) && ! EQ (XCAR (list), elt))
161 list = XCDR (list);
162 return (CONSP (list));
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. */
168 Lisp_Object
169 w32font_get_cache (f)
170 FRAME_PTR f;
172 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
174 return (dpyinfo->name_list_element);
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. */
181 static Lisp_Object
182 w32font_list (frame, font_spec)
183 Lisp_Object frame, font_spec;
185 Lisp_Object fonts = w32font_list_internal (frame, font_spec, 0);
186 font_add_log ("w32font-list", font_spec, fonts);
187 return fonts;
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. */
194 static Lisp_Object
195 w32font_match (frame, font_spec)
196 Lisp_Object frame, font_spec;
198 Lisp_Object entity = w32font_match_internal (frame, font_spec, 0);
199 font_add_log ("w32font-match", font_spec, entity);
200 return entity;
203 /* w32 implementation of list_family for font backend.
204 List available families. The value is a list of family names
205 (symbols). */
206 static Lisp_Object
207 w32font_list_family (frame)
208 Lisp_Object frame;
210 Lisp_Object list = Qnil;
211 LOGFONT font_match_pattern;
212 HDC dc;
213 FRAME_PTR f = XFRAME (frame);
215 bzero (&font_match_pattern, sizeof (font_match_pattern));
216 font_match_pattern.lfCharSet = DEFAULT_CHARSET;
218 dc = get_frame_dc (f);
220 EnumFontFamiliesEx (dc, &font_match_pattern,
221 (FONTENUMPROC) add_font_name_to_list,
222 (LPARAM) &list, 0);
223 release_frame_dc (f, dc);
225 return list;
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. */
231 static Lisp_Object
232 w32font_open (f, font_entity, pixel_size)
233 FRAME_PTR f;
234 Lisp_Object font_entity;
235 int pixel_size;
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);
243 ASET (font_object, FONT_TYPE_INDEX, Qgdi);
245 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
247 return Qnil;
250 /* GDI backend does not use glyph indices. */
251 w32_font->glyph_idx = 0;
253 return font_object;
256 /* w32 implementation of close for font_backend.
257 Close FONT on frame F. */
258 void
259 w32font_close (f, font)
260 FRAME_PTR f;
261 struct font *font;
263 int i;
264 struct w32font_info *w32_font = (struct w32font_info *) font;
266 /* Delete the GDI font object. */
267 DeleteObject (w32_font->hfont);
269 /* Free all the cached metrics. */
270 if (w32_font->cached_metrics)
272 for (i = 0; i < w32_font->n_cache_blocks; i++)
274 if (w32_font->cached_metrics[i])
275 xfree (w32_font->cached_metrics[i]);
277 xfree (w32_font->cached_metrics);
278 w32_font->cached_metrics = NULL;
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. */
288 w32font_has_char (entity, c)
289 Lisp_Object entity;
290 int c;
292 Lisp_Object supported_scripts, extra, script;
293 DWORD mask;
295 extra = AREF (entity, FONT_EXTRA_INDEX);
296 if (!CONSP (extra))
297 return -1;
299 supported_scripts = assq_no_quit (QCscript, extra);
300 /* If font doesn't claim to support any scripts, then we can't be certain
301 until we open it. */
302 if (!CONSP (supported_scripts))
303 return -1;
305 supported_scripts = XCDR (supported_scripts);
307 script = CHAR_TABLE_REF (Vchar_script_table, c);
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;
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;
321 /* w32 implementation of encode_char for font backend.
322 Return a glyph code of FONT for characer C (Unicode code point).
323 If FONT doesn't have such a glyph, return FONT_INVALID_CODE.
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.
329 static unsigned
330 w32font_encode_char (font, c)
331 struct font *font;
332 int c;
334 struct w32font_info * w32_font = (struct w32font_info *)font;
336 if (c < w32_font->metrics.tmFirstChar
337 || c > w32_font->metrics.tmLastChar)
338 return FONT_INVALID_CODE;
339 else
340 return c;
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
346 CODE (length NGLYPHS). Apparently metrics can be NULL, in this
347 case just return the overall width. */
349 w32font_text_extents (font, code, nglyphs, metrics)
350 struct font *font;
351 unsigned *code;
352 int nglyphs;
353 struct font_metrics *metrics;
355 int i;
356 HFONT old_font = NULL;
357 HDC dc = NULL;
358 struct frame * f;
359 int total_width = 0;
360 WORD *wcode;
361 SIZE size;
363 struct w32font_info *w32_font = (struct w32font_info *) font;
365 if (metrics)
367 bzero (metrics, sizeof (struct font_metrics));
368 metrics->ascent = font->ascent;
369 metrics->descent = font->descent;
371 for (i = 0; i < nglyphs; i++)
373 struct w32_metric_cache *char_metric;
374 int block = *(code + i) / CACHE_BLOCKSIZE;
375 int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
377 if (block >= w32_font->n_cache_blocks)
379 if (!w32_font->cached_metrics)
380 w32_font->cached_metrics
381 = xmalloc ((block + 1)
382 * sizeof (struct w32_metric_cache *));
383 else
384 w32_font->cached_metrics
385 = xrealloc (w32_font->cached_metrics,
386 (block + 1)
387 * sizeof (struct w32_metric_cache *));
388 bzero (w32_font->cached_metrics + w32_font->n_cache_blocks,
389 ((block + 1 - w32_font->n_cache_blocks)
390 * sizeof (struct w32_metric_cache *)));
391 w32_font->n_cache_blocks = block + 1;
394 if (!w32_font->cached_metrics[block])
396 w32_font->cached_metrics[block]
397 = xmalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
398 bzero (w32_font->cached_metrics[block],
399 CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
402 char_metric = w32_font->cached_metrics[block] + pos_in_block;
404 if (char_metric->status == W32METRIC_NO_ATTEMPT)
406 if (dc == NULL)
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);
414 dc = get_frame_dc (f);
415 old_font = SelectObject (dc, w32_font->hfont);
417 compute_metrics (dc, w32_font, *(code + i), char_metric);
420 if (char_metric->status == W32METRIC_SUCCESS)
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;
428 else
429 /* If we couldn't get metrics for a char,
430 use alternative method. */
431 break;
433 /* If we got through everything, return. */
434 if (i == nglyphs)
436 if (dc != NULL)
438 /* Restore state and release DC. */
439 SelectObject (dc, old_font);
440 release_frame_dc (f, dc);
443 return metrics->width;
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. */
451 /* Make array big enough to hold surrogates. */
452 wcode = alloca (nglyphs * sizeof (WORD) * 2);
453 for (i = 0; i < nglyphs; i++)
455 if (code[i] < 0x10000)
456 wcode[i] = code[i];
457 else
459 DWORD surrogate = code[i] - 0x10000;
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++;
471 if (dc == NULL)
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);
479 dc = get_frame_dc (f);
480 old_font = SelectObject (dc, w32_font->hfont);
483 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
485 total_width = size.cx;
488 /* On 95/98/ME, only some unicode functions are available, so fallback
489 on doing a dummy draw to find the total width. */
490 if (!total_width)
492 RECT rect;
493 rect.top = 0; rect.bottom = font->height; rect.left = 0; rect.right = 1;
494 DrawTextW (dc, wcode, nglyphs, &rect,
495 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
496 total_width = rect.right;
499 /* Give our best estimate of the metrics, based on what we know. */
500 if (metrics)
502 metrics->width = total_width - w32_font->metrics.tmOverhang;
503 metrics->lbearing = 0;
504 metrics->rbearing = total_width;
507 /* Restore state and release DC. */
508 SelectObject (dc, old_font);
509 release_frame_dc (f, dc);
511 return total_width;
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
519 WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).
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
528 w32font_draw (s, from, to, x, y, with_background)
529 struct glyph_string *s;
530 int from, to, x, y, with_background;
532 UINT options;
533 HRGN orig_clip;
534 struct w32font_info *w32font = (struct w32font_info *) s->font;
536 options = w32font->glyph_idx;
538 /* Save clip region for later restoration. */
539 GetClipRgn(s->hdc, orig_clip);
541 if (s->num_clips > 0)
543 HRGN new_clip = CreateRectRgnIndirect (s->clip);
545 if (s->num_clips > 1)
547 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
549 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
550 DeleteObject (clip2);
553 SelectClipRgn (s->hdc, new_clip);
554 DeleteObject (new_clip);
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);
560 if (with_background)
562 HBRUSH brush;
563 RECT rect;
564 struct font *font = s->font;
566 brush = CreateSolidBrush (s->gc->background);
567 rect.left = x;
568 rect.top = y - font->ascent;
569 rect.right = x + s->width;
570 rect.bottom = y + font->descent;
571 FillRect (s->hdc, &rect, brush);
572 DeleteObject (brush);
575 if (s->padding_p)
577 int len = to - from, i;
579 for (i = 0; i < len; i++)
580 ExtTextOutW (s->hdc, x + i, y, options, NULL,
581 s->char2b + from + i, 1, NULL);
583 else
584 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
586 /* Restore clip region. */
587 if (s->num_clips > 0)
589 SelectClipRgn (s->hdc, orig_clip);
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.
596 static void
597 w32font_free_entity (Lisp_Object entity);
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.
605 static int
606 w32font_prepare_face (FRAME_PTR f, struct face *face);
608 /* w32 implementation of done_face for font backend.
609 Optional.
610 Done FACE for displaying characters by FACE->font on frame F.
611 static void
612 w32font_done_face (FRAME_PTR f, struct face *face); */
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
617 intended that this method is called from the other font-driver
618 for actual drawing.
619 static int
620 w32font_get_bitmap (struct font *font, unsigned code,
621 struct font_bitmap *bitmap, int bits_per_pixel);
623 /* w32 implementation of free_bitmap for font backend.
624 Optional.
625 Free bitmap data in BITMAP.
626 static void
627 w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
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.
633 static void *
634 w32font_get_outline (struct font *font, unsigned code);
636 /* w32 implementation of free_outline for font backend.
637 Optional.
638 Free OUTLINE (that is obtained by the above method).
639 static void
640 w32font_free_outline (struct font *font, void *outline);
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.
647 static int
648 w32font_anchor_point (struct font *font, unsigned code,
649 int index, int *x, int *y);
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.
655 static Lisp_Object
656 w32font_otf_capability (struct font *font);
658 /* w32 implementation of otf_drive for font backend.
659 Optional.
660 Apply FONT's OTF-FEATURES to the glyph string.
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.
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.
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.
674 static int
675 w32font_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);
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. */
684 Lisp_Object
685 w32font_list_internal (frame, font_spec, opentype_only)
686 Lisp_Object frame, font_spec;
687 int opentype_only;
689 struct font_callback_data match_data;
690 HDC dc;
691 FRAME_PTR f = XFRAME (frame);
693 match_data.orig_font_spec = font_spec;
694 match_data.list = Qnil;
695 match_data.frame = frame;
697 bzero (&match_data.pattern, sizeof (LOGFONT));
698 fill_in_logfont (f, &match_data.pattern, font_spec);
700 match_data.opentype_only = opentype_only;
701 if (opentype_only)
702 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
704 if (match_data.pattern.lfFaceName[0] == '\0')
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);
710 else
712 dc = get_frame_dc (f);
714 EnumFontFamiliesEx (dc, &match_data.pattern,
715 (FONTENUMPROC) add_font_entity_to_list,
716 (LPARAM) &match_data, 0);
717 release_frame_dc (f, dc);
720 return NILP (match_data.list) ? Qnil : match_data.list;
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. */
726 Lisp_Object
727 w32font_match_internal (frame, font_spec, opentype_only)
728 Lisp_Object frame, font_spec;
729 int opentype_only;
731 struct font_callback_data match_data;
732 HDC dc;
733 FRAME_PTR f = XFRAME (frame);
735 match_data.orig_font_spec = font_spec;
736 match_data.frame = frame;
737 match_data.list = Qnil;
739 bzero (&match_data.pattern, sizeof (LOGFONT));
740 fill_in_logfont (f, &match_data.pattern, font_spec);
742 match_data.opentype_only = opentype_only;
743 if (opentype_only)
744 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
746 dc = get_frame_dc (f);
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);
753 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
757 w32font_open_internal (f, font_entity, pixel_size, font_object)
758 FRAME_PTR f;
759 Lisp_Object font_entity;
760 int pixel_size;
761 Lisp_Object font_object;
763 int len, size, i;
764 LOGFONT logfont;
765 HDC dc;
766 HFONT hfont, old_font;
767 Lisp_Object val, extra;
768 struct w32font_info *w32_font;
769 struct font * font;
770 OUTLINETEXTMETRICW* metrics = NULL;
772 w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
773 font = (struct font *) w32_font;
775 if (!font)
776 return 0;
778 bzero (&logfont, sizeof (logfont));
779 fill_in_logfont (f, &logfont, font_entity);
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;
787 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
788 if (!size)
789 size = pixel_size;
791 logfont.lfHeight = -size;
792 hfont = CreateFontIndirect (&logfont);
794 if (hfont == NULL)
795 return 0;
797 /* Get the metrics for this font. */
798 dc = get_frame_dc (f);
799 old_font = SelectObject (dc, hfont);
801 /* Try getting the outline metrics (only works for truetype fonts). */
802 len = GetOutlineTextMetricsW (dc, 0, NULL);
803 if (len)
805 metrics = (OUTLINETEXTMETRICW *) alloca (len);
806 if (GetOutlineTextMetricsW (dc, len, metrics))
807 bcopy (&metrics->otmTextMetrics, &w32_font->metrics,
808 sizeof (TEXTMETRICW));
809 else
810 metrics = NULL;
813 if (!metrics)
814 GetTextMetricsW (dc, &w32_font->metrics);
816 w32_font->cached_metrics = NULL;
817 w32_font->n_cache_blocks = 0;
819 SelectObject (dc, old_font);
820 release_frame_dc (f, dc);
822 w32_font->hfont = hfont;
825 char *name;
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;
830 name = alloca (len);
831 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
832 name, len) < 0)
834 len += 32;
835 name = alloca (len);
837 if (name)
838 font->props[FONT_FULLNAME_INDEX]
839 = make_unibyte_string (name, strlen (name));
840 else
841 font->props[FONT_FULLNAME_INDEX] =
842 make_unibyte_string (logfont.lfFaceName, len);
845 font->max_width = w32_font->metrics.tmMaxCharWidth;
846 /* Parts of Emacs display assume that height = ascent + descent...
847 so height is defined later, after ascent and descent.
848 font->height = w32_font->metrics.tmHeight
849 + w32_font->metrics.tmExternalLeading;
852 font->space_width = font->average_width = w32_font->metrics.tmAveCharWidth;
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;
860 font->pixel_size = size;
861 font->driver = &w32font_driver;
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))
867 val = assq_no_quit (QCformat, extra);
868 if (CONSP (val))
869 font->props[FONT_FORMAT_INDEX] = XCDR (val);
870 else
871 font->props[FONT_FORMAT_INDEX] = Qunknown;
873 else
874 font->props[FONT_FORMAT_INDEX] = Qunknown;
876 font->props[FONT_FILE_INDEX] = Qnil;
877 font->encoding_charset = -1;
878 font->repertory_charset = -1;
879 /* TODO: do we really want the minimum width here, which could be negative? */
880 font->min_width = font->space_width;
881 font->ascent = w32_font->metrics.tmAscent;
882 font->descent = w32_font->metrics.tmDescent;
883 font->height = font->ascent + font->descent;
885 if (metrics)
887 font->underline_thickness = metrics->otmsUnderscoreSize;
888 font->underline_position = -metrics->otmsUnderscorePosition;
890 else
892 font->underline_thickness = 0;
893 font->underline_position = -1;
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. */
900 font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
902 return 1;
905 /* Callback function for EnumFontFamiliesEx.
906 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
907 static int CALLBACK
908 add_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;
914 Lisp_Object* list = (Lisp_Object *) list_object;
915 Lisp_Object family;
917 /* Skip vertical fonts (intended only for printing) */
918 if (logical_font->elfLogFont.lfFaceName[0] == '@')
919 return 1;
921 family = font_intern_prop (logical_font->elfLogFont.lfFaceName,
922 strlen (logical_font->elfLogFont.lfFaceName), 1);
923 if (! memq_no_quit (family, *list))
924 *list = Fcons (family, *list);
926 return 1;
929 static int w32_decode_weight P_ ((int));
930 static int w32_encode_weight P_ ((int));
932 /* Convert an enumerated Windows font to an Emacs font entity. */
933 static Lisp_Object
934 w32_enumfont_pattern_entity (frame, logical_font, physical_font,
935 font_type, requested_font, backend)
936 Lisp_Object frame;
937 ENUMLOGFONTEX *logical_font;
938 NEWTEXTMETRICEX *physical_font;
939 DWORD font_type;
940 LOGFONT *requested_font;
941 Lisp_Object backend;
943 Lisp_Object entity, tem;
944 LOGFONT *lf = (LOGFONT*) logical_font;
945 BYTE generic_type;
946 DWORD full_type = physical_font->ntmTm.ntmFlags;
948 entity = font_make_entity ();
950 ASET (entity, FONT_TYPE_INDEX, backend);
951 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
952 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
954 /* Foundry is difficult to get in readable form on Windows.
955 But Emacs crashes if it is not set, so set it to something more
956 generic. These values make xlfds compatible with Emacs 22. */
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;
964 ASET (entity, FONT_FOUNDRY_INDEX, tem);
966 /* Save the generic family in the extra info, as it is likely to be
967 useful to users looking for a close match. */
968 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
969 if (generic_type == FF_DECORATIVE)
970 tem = Qdecorative;
971 else if (generic_type == FF_MODERN)
972 tem = Qmono;
973 else if (generic_type == FF_ROMAN)
974 tem = Qserif;
975 else if (generic_type == FF_SCRIPT)
976 tem = Qscript;
977 else if (generic_type == FF_SWISS)
978 tem = Qsans;
979 else
980 tem = Qnil;
982 ASET (entity, FONT_ADSTYLE_INDEX, tem);
984 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
985 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_PROPORTIONAL));
986 else
987 ASET (entity, FONT_SPACING_INDEX, make_number (FONT_SPACING_CHARCELL));
989 if (requested_font->lfQuality != DEFAULT_QUALITY)
991 font_put_extra (entity, QCantialias,
992 lispy_antialias_type (requested_font->lfQuality));
994 ASET (entity, FONT_FAMILY_INDEX,
995 font_intern_prop (lf->lfFaceName, strlen (lf->lfFaceName), 1));
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));
1001 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
1002 to get it. */
1003 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (100));
1005 if (font_type & RASTER_FONTTYPE)
1006 ASET (entity, FONT_SIZE_INDEX,
1007 make_number (physical_font->ntmTm.tmHeight
1008 + physical_font->ntmTm.tmExternalLeading));
1009 else
1010 ASET (entity, FONT_SIZE_INDEX, make_number (0));
1012 /* Cache unicode codepoints covered by this font, as there is no other way
1013 of getting this information easily. */
1014 if (font_type & TRUETYPE_FONTTYPE)
1016 tem = font_supported_scripts (&physical_font->ntmFontSig);
1017 if (!NILP (tem))
1018 font_put_extra (entity, QCscript, tem);
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. */
1026 if (EQ (backend, Quniscribe) && (full_type & NTMFLAGS_OPENTYPE))
1027 tem = intern ("opentype");
1028 else if (font_type & TRUETYPE_FONTTYPE)
1029 tem = intern ("truetype");
1030 else if (full_type & NTM_PS_OPENTYPE)
1031 tem = intern ("postscript");
1032 else if (full_type & NTM_TYPE1)
1033 tem = intern ("type1");
1034 else if (font_type & RASTER_FONTTYPE)
1035 tem = intern ("w32bitmap");
1036 else
1037 tem = intern ("w32vector");
1039 font_put_extra (entity, QCformat, tem);
1041 return entity;
1045 /* Convert generic families to the family portion of lfPitchAndFamily. */
1046 BYTE
1047 w32_generic_family (Lisp_Object name)
1049 /* Generic families. */
1050 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1051 return FF_MODERN;
1052 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
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;
1064 static int
1065 logfonts_match (font, pattern)
1066 LOGFONT *font, *pattern;
1068 /* Only check height for raster fonts. */
1069 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1070 && font->lfHeight != pattern->lfHeight)
1071 return 0;
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;
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;
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))
1089 static int
1090 font_matches_spec (type, font, spec, backend, logfont)
1091 DWORD type;
1092 NEWTEXTMETRICEX *font;
1093 Lisp_Object spec;
1094 Lisp_Object backend;
1095 LOGFONT *logfont;
1097 Lisp_Object extra, val;
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". */
1102 int slant = FONT_SLANT_NUMERIC (spec);
1104 if (slant >= 0
1105 && ((slant > 150 && !font->ntmTm.tmItalic)
1106 || (slant <= 150 && font->ntmTm.tmItalic)))
1107 return 0;
1110 /* Check adstyle against generic family. */
1111 val = AREF (spec, FONT_ADSTYLE_INDEX);
1112 if (!NILP (val))
1114 BYTE family = w32_generic_family (val);
1115 if (family != FF_DONTCARE
1116 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1117 return 0;
1120 /* Check spacing */
1121 val = AREF (spec, FONT_SPACING_INDEX);
1122 if (INTEGERP (val))
1124 int spacing = XINT (val);
1125 int proportional = (spacing < FONT_SPACING_MONO);
1127 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1128 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1129 return 0;
1132 /* Check extra parameters. */
1133 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1134 CONSP (extra); extra = XCDR (extra))
1136 Lisp_Object extra_entry;
1137 extra_entry = XCAR (extra);
1138 if (CONSP (extra_entry))
1140 Lisp_Object key = XCAR (extra_entry);
1142 val = XCDR (extra_entry);
1143 if (EQ (key, QCscript) && SYMBOLP (val))
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)
1151 Lisp_Object support
1152 = font_supported_scripts (&font->ntmFontSig);
1153 if (! memq_no_quit (val, support))
1154 return 0;
1156 else
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))
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;
1170 else if (EQ (val, Qsymbol))
1172 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1173 return 0;
1175 else if (EQ (val, Qcyrillic))
1177 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1178 return 0;
1180 else if (EQ (val, Qgreek))
1182 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1183 return 0;
1185 else if (EQ (val, Qarabic))
1187 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1188 return 0;
1190 else if (EQ (val, Qhebrew))
1192 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1193 return 0;
1195 else if (EQ (val, Qthai))
1197 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1198 return 0;
1200 else if (EQ (val, Qkana))
1202 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1203 return 0;
1205 else if (EQ (val, Qbopomofo))
1207 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1208 return 0;
1210 else if (EQ (val, Qhangul))
1212 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1213 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1214 return 0;
1216 else if (EQ (val, Qhan))
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;
1225 else
1226 /* Other scripts unlikely to be handled by non-truetype
1227 fonts. */
1228 return 0;
1231 else if (EQ (key, QClang) && SYMBOLP (val))
1233 /* Just handle the CJK languages here, as the lang
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))
1240 if (!(font->ntmFontSig.fsCsb[0] & CSB_JAPANESE))
1241 return 0;
1243 else if (EQ (val, Qko))
1245 if (!(font->ntmFontSig.fsCsb[0] & CSB_KOREAN))
1246 return 0;
1248 else if (EQ (val, Qzh))
1250 if (!(font->ntmFontSig.fsCsb[0] & CSB_CHINESE))
1251 return 0;
1253 else
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
1259 spec should have a fallback, as some backends do
1260 not recognize language at all. */
1261 return 0;
1263 else if (EQ (key, QCotf) && CONSP (val))
1265 /* OTF features only supported by the uniscribe backend. */
1266 if (EQ (backend, Quniscribe))
1268 if (!uniscribe_check_otf (logfont, val))
1269 return 0;
1271 else
1272 return 0;
1276 return 1;
1279 static int
1280 w32font_coverage_ok (coverage, charset)
1281 FONTSIGNATURE * coverage;
1282 BYTE charset;
1284 DWORD subrange1 = coverage->fsUsb[1];
1286 #define SUBRANGE1_HAN_MASK 0x08000000
1287 #define SUBRANGE1_HANGEUL_MASK 0x01000000
1288 #define SUBRANGE1_JAPANESE_MASK (0x00060000 | SUBRANGE1_HAN_MASK)
1290 if (charset == GB2312_CHARSET || charset == CHINESEBIG5_CHARSET)
1292 return (subrange1 & SUBRANGE1_HAN_MASK) == SUBRANGE1_HAN_MASK;
1294 else if (charset == SHIFTJIS_CHARSET)
1296 return (subrange1 & SUBRANGE1_JAPANESE_MASK) == SUBRANGE1_JAPANESE_MASK;
1298 else if (charset == HANGEUL_CHARSET)
1300 return (subrange1 & SUBRANGE1_HANGEUL_MASK) == SUBRANGE1_HANGEUL_MASK;
1303 return 1;
1307 static int
1308 check_face_name (font, full_name)
1309 LOGFONT *font;
1310 char *full_name;
1312 char full_iname[LF_FULLFACESIZE+1];
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) */
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. */
1324 if (!xstrcasecmp (font->lfFaceName, "helvetica"))
1326 strncpy (full_iname, full_name, LF_FULLFACESIZE);
1327 full_iname[LF_FULLFACESIZE] = 0;
1328 _strlwr (full_iname);
1329 return strstr ("helvetica", full_iname) != NULL;
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;
1337 return 1;
1341 /* Callback function for EnumFontFamiliesEx.
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. */
1346 static int CALLBACK
1347 add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1348 ENUMLOGFONTEX *logical_font;
1349 NEWTEXTMETRICEX *physical_font;
1350 DWORD font_type;
1351 LPARAM lParam;
1353 struct font_callback_data *match_data
1354 = (struct font_callback_data *) lParam;
1355 Lisp_Object backend = match_data->opentype_only ? Quniscribe : Qgdi;
1357 if ((!match_data->opentype_only
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))))
1366 && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1367 && font_matches_spec (font_type, physical_font,
1368 match_data->orig_font_spec, backend,
1369 &logical_font->elfLogFont)
1370 && w32font_coverage_ok (&physical_font->ntmFontSig,
1371 match_data->pattern.lfCharSet)
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,
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->elfLogFont,
1386 logical_font->elfFullName)))
1388 Lisp_Object entity
1389 = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1390 physical_font, font_type,
1391 &match_data->pattern,
1392 backend);
1393 if (!NILP (entity))
1395 Lisp_Object spec_charset = AREF (match_data->orig_font_spec,
1396 FONT_REGISTRY_INDEX);
1398 /* If registry was specified as iso10646-1, only report
1399 ANSI and DEFAULT charsets, as most unicode fonts will
1400 contain one of those plus others. */
1401 if ((EQ (spec_charset, Qiso10646_1)
1402 || EQ (spec_charset, Qunicode_bmp)
1403 || EQ (spec_charset, Qunicode_sip))
1404 && logical_font->elfLogFont.lfCharSet != DEFAULT_CHARSET
1405 && logical_font->elfLogFont.lfCharSet != ANSI_CHARSET)
1406 return 1;
1407 /* If registry was specified, but did not map to a windows
1408 charset, only report fonts that have unknown charsets.
1409 This will still report fonts that don't match, but at
1410 least it eliminates known definite mismatches. */
1411 else if (!NILP (spec_charset)
1412 && !EQ (spec_charset, Qiso10646_1)
1413 && !EQ (spec_charset, Qunicode_bmp)
1414 && !EQ (spec_charset, Qunicode_sip)
1415 && match_data->pattern.lfCharSet == DEFAULT_CHARSET
1416 && logical_font->elfLogFont.lfCharSet != DEFAULT_CHARSET)
1417 return 1;
1419 /* If registry was specified, ensure it is reported as the same. */
1420 if (!NILP (spec_charset))
1421 ASET (entity, FONT_REGISTRY_INDEX, spec_charset);
1423 match_data->list = Fcons (entity, match_data->list);
1425 /* If no registry specified, duplicate iso8859-1 truetype fonts
1426 as iso10646-1. */
1427 if (NILP (spec_charset)
1428 && font_type == TRUETYPE_FONTTYPE
1429 && logical_font->elfLogFont.lfCharSet == ANSI_CHARSET)
1431 Lisp_Object tem = Fcopy_font_spec (entity);
1432 ASET (tem, FONT_REGISTRY_INDEX, Qiso10646_1);
1433 match_data->list = Fcons (tem, match_data->list);
1437 return 1;
1440 /* Callback function for EnumFontFamiliesEx.
1441 * Terminates the search once we have a match. */
1442 static int CALLBACK
1443 add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1444 ENUMLOGFONTEX *logical_font;
1445 NEWTEXTMETRICEX *physical_font;
1446 DWORD font_type;
1447 LPARAM lParam;
1449 struct font_callback_data *match_data
1450 = (struct font_callback_data *) lParam;
1451 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1453 /* If we have a font in the list, terminate the search. */
1454 return !NILP (match_data->list);
1457 /* Old function to convert from x to w32 charset, from w32fns.c. */
1458 static LONG
1459 x_to_w32_charset (lpcs)
1460 char * lpcs;
1462 Lisp_Object this_entry, w32_charset;
1463 char *charset;
1464 int len = strlen (lpcs);
1466 /* Support "*-#nnn" format for unknown charsets. */
1467 if (strncmp (lpcs, "*-#", 3) == 0)
1468 return atoi (lpcs + 3);
1470 /* All Windows fonts qualify as unicode. */
1471 if (!strncmp (lpcs, "iso10646", 8))
1472 return DEFAULT_CHARSET;
1474 /* Handle wildcards by ignoring them; eg. treat "big5*-*" as "big5". */
1475 charset = alloca (len + 1);
1476 strcpy (charset, lpcs);
1477 lpcs = strchr (charset, '*');
1478 if (lpcs)
1479 *lpcs = '\0';
1481 /* Look through w32-charset-info-alist for the character set.
1482 Format of each entry is
1483 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1485 this_entry = Fassoc (build_string (charset), Vw32_charset_info_alist);
1487 if (NILP (this_entry))
1489 /* At startup, we want iso8859-1 fonts to come up properly. */
1490 if (xstrcasecmp (charset, "iso8859-1") == 0)
1491 return ANSI_CHARSET;
1492 else
1493 return DEFAULT_CHARSET;
1496 w32_charset = Fcar (Fcdr (this_entry));
1498 /* Translate Lisp symbol to number. */
1499 if (EQ (w32_charset, Qw32_charset_ansi))
1500 return ANSI_CHARSET;
1501 if (EQ (w32_charset, Qw32_charset_symbol))
1502 return SYMBOL_CHARSET;
1503 if (EQ (w32_charset, Qw32_charset_shiftjis))
1504 return SHIFTJIS_CHARSET;
1505 if (EQ (w32_charset, Qw32_charset_hangeul))
1506 return HANGEUL_CHARSET;
1507 if (EQ (w32_charset, Qw32_charset_chinesebig5))
1508 return CHINESEBIG5_CHARSET;
1509 if (EQ (w32_charset, Qw32_charset_gb2312))
1510 return GB2312_CHARSET;
1511 if (EQ (w32_charset, Qw32_charset_oem))
1512 return OEM_CHARSET;
1513 if (EQ (w32_charset, Qw32_charset_johab))
1514 return JOHAB_CHARSET;
1515 if (EQ (w32_charset, Qw32_charset_easteurope))
1516 return EASTEUROPE_CHARSET;
1517 if (EQ (w32_charset, Qw32_charset_turkish))
1518 return TURKISH_CHARSET;
1519 if (EQ (w32_charset, Qw32_charset_baltic))
1520 return BALTIC_CHARSET;
1521 if (EQ (w32_charset, Qw32_charset_russian))
1522 return RUSSIAN_CHARSET;
1523 if (EQ (w32_charset, Qw32_charset_arabic))
1524 return ARABIC_CHARSET;
1525 if (EQ (w32_charset, Qw32_charset_greek))
1526 return GREEK_CHARSET;
1527 if (EQ (w32_charset, Qw32_charset_hebrew))
1528 return HEBREW_CHARSET;
1529 if (EQ (w32_charset, Qw32_charset_vietnamese))
1530 return VIETNAMESE_CHARSET;
1531 if (EQ (w32_charset, Qw32_charset_thai))
1532 return THAI_CHARSET;
1533 if (EQ (w32_charset, Qw32_charset_mac))
1534 return MAC_CHARSET;
1536 return DEFAULT_CHARSET;
1540 /* Convert a Lisp font registry (symbol) to a windows charset. */
1541 static LONG
1542 registry_to_w32_charset (charset)
1543 Lisp_Object charset;
1545 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1546 || EQ (charset, Qunicode_sip))
1547 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1548 else if (EQ (charset, Qiso8859_1))
1549 return ANSI_CHARSET;
1550 else if (SYMBOLP (charset))
1551 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
1552 else
1553 return DEFAULT_CHARSET;
1556 /* Old function to convert from w32 to x charset, from w32fns.c. */
1557 static char *
1558 w32_to_x_charset (fncharset, matching)
1559 int fncharset;
1560 char *matching;
1562 static char buf[32];
1563 Lisp_Object charset_type;
1564 int match_len = 0;
1566 if (matching)
1568 /* If fully specified, accept it as it is. Otherwise use a
1569 substring match. */
1570 char *wildcard = strchr (matching, '*');
1571 if (wildcard)
1572 *wildcard = '\0';
1573 else if (strchr (matching, '-'))
1574 return matching;
1576 match_len = strlen (matching);
1579 switch (fncharset)
1581 case ANSI_CHARSET:
1582 /* Handle startup case of w32-charset-info-alist not
1583 being set up yet. */
1584 if (NILP (Vw32_charset_info_alist))
1585 return "iso8859-1";
1586 charset_type = Qw32_charset_ansi;
1587 break;
1588 case DEFAULT_CHARSET:
1589 charset_type = Qw32_charset_default;
1590 break;
1591 case SYMBOL_CHARSET:
1592 charset_type = Qw32_charset_symbol;
1593 break;
1594 case SHIFTJIS_CHARSET:
1595 charset_type = Qw32_charset_shiftjis;
1596 break;
1597 case HANGEUL_CHARSET:
1598 charset_type = Qw32_charset_hangeul;
1599 break;
1600 case GB2312_CHARSET:
1601 charset_type = Qw32_charset_gb2312;
1602 break;
1603 case CHINESEBIG5_CHARSET:
1604 charset_type = Qw32_charset_chinesebig5;
1605 break;
1606 case OEM_CHARSET:
1607 charset_type = Qw32_charset_oem;
1608 break;
1609 case EASTEUROPE_CHARSET:
1610 charset_type = Qw32_charset_easteurope;
1611 break;
1612 case TURKISH_CHARSET:
1613 charset_type = Qw32_charset_turkish;
1614 break;
1615 case BALTIC_CHARSET:
1616 charset_type = Qw32_charset_baltic;
1617 break;
1618 case RUSSIAN_CHARSET:
1619 charset_type = Qw32_charset_russian;
1620 break;
1621 case ARABIC_CHARSET:
1622 charset_type = Qw32_charset_arabic;
1623 break;
1624 case GREEK_CHARSET:
1625 charset_type = Qw32_charset_greek;
1626 break;
1627 case HEBREW_CHARSET:
1628 charset_type = Qw32_charset_hebrew;
1629 break;
1630 case VIETNAMESE_CHARSET:
1631 charset_type = Qw32_charset_vietnamese;
1632 break;
1633 case THAI_CHARSET:
1634 charset_type = Qw32_charset_thai;
1635 break;
1636 case MAC_CHARSET:
1637 charset_type = Qw32_charset_mac;
1638 break;
1639 case JOHAB_CHARSET:
1640 charset_type = Qw32_charset_johab;
1641 break;
1643 default:
1644 /* Encode numerical value of unknown charset. */
1645 sprintf (buf, "*-#%u", fncharset);
1646 return buf;
1650 Lisp_Object rest;
1651 char * best_match = NULL;
1652 int matching_found = 0;
1654 /* Look through w32-charset-info-alist for the character set.
1655 Prefer ISO codepages, and prefer lower numbers in the ISO
1656 range. Only return charsets for codepages which are installed.
1658 Format of each entry is
1659 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE)).
1661 for (rest = Vw32_charset_info_alist; CONSP (rest); rest = XCDR (rest))
1663 char * x_charset;
1664 Lisp_Object w32_charset;
1665 Lisp_Object codepage;
1667 Lisp_Object this_entry = XCAR (rest);
1669 /* Skip invalid entries in alist. */
1670 if (!CONSP (this_entry) || !STRINGP (XCAR (this_entry))
1671 || !CONSP (XCDR (this_entry))
1672 || !SYMBOLP (XCAR (XCDR (this_entry))))
1673 continue;
1675 x_charset = SDATA (XCAR (this_entry));
1676 w32_charset = XCAR (XCDR (this_entry));
1677 codepage = XCDR (XCDR (this_entry));
1679 /* Look for Same charset and a valid codepage (or non-int
1680 which means ignore). */
1681 if (EQ (w32_charset, charset_type)
1682 && (!INTEGERP (codepage) || XINT (codepage) == CP_DEFAULT
1683 || IsValidCodePage (XINT (codepage))))
1685 /* If we don't have a match already, then this is the
1686 best. */
1687 if (!best_match)
1689 best_match = x_charset;
1690 if (matching && !strnicmp (x_charset, matching, match_len))
1691 matching_found = 1;
1693 /* If we already found a match for MATCHING, then
1694 only consider other matches. */
1695 else if (matching_found
1696 && strnicmp (x_charset, matching, match_len))
1697 continue;
1698 /* If this matches what we want, and the best so far doesn't,
1699 then this is better. */
1700 else if (!matching_found && matching
1701 && !strnicmp (x_charset, matching, match_len))
1703 best_match = x_charset;
1704 matching_found = 1;
1706 /* If this is fully specified, and the best so far isn't,
1707 then this is better. */
1708 else if ((!strchr (best_match, '-') && strchr (x_charset, '-'))
1709 /* If this is an ISO codepage, and the best so far isn't,
1710 then this is better, but only if it fully specifies the
1711 encoding. */
1712 || (strnicmp (best_match, "iso", 3) != 0
1713 && strnicmp (x_charset, "iso", 3) == 0
1714 && strchr (x_charset, '-')))
1715 best_match = x_charset;
1716 /* If both are ISO8859 codepages, choose the one with the
1717 lowest number in the encoding field. */
1718 else if (strnicmp (best_match, "iso8859-", 8) == 0
1719 && strnicmp (x_charset, "iso8859-", 8) == 0)
1721 int best_enc = atoi (best_match + 8);
1722 int this_enc = atoi (x_charset + 8);
1723 if (this_enc > 0 && this_enc < best_enc)
1724 best_match = x_charset;
1729 /* If no match, encode the numeric value. */
1730 if (!best_match)
1732 sprintf (buf, "*-#%u", fncharset);
1733 return buf;
1736 strncpy (buf, best_match, 31);
1737 /* If the charset is not fully specified, put -0 on the end. */
1738 if (!strchr (best_match, '-'))
1740 int pos = strlen (best_match);
1741 /* Charset specifiers shouldn't be very long. If it is a made
1742 up one, truncating it should not do any harm since it isn't
1743 recognized anyway. */
1744 if (pos > 29)
1745 pos = 29;
1746 strcpy (buf + pos, "-0");
1748 buf[31] = '\0';
1749 return buf;
1753 static Lisp_Object
1754 w32_registry (w32_charset, font_type)
1755 LONG w32_charset;
1756 DWORD font_type;
1758 char *charset;
1760 /* If charset is defaulted, charset is unicode or unknown, depending on
1761 font type. */
1762 if (w32_charset == DEFAULT_CHARSET)
1763 return font_type == TRUETYPE_FONTTYPE ? Qiso10646_1 : Qunknown;
1765 charset = w32_to_x_charset (w32_charset, NULL);
1766 return font_intern_prop (charset, strlen(charset), 1);
1769 static int
1770 w32_decode_weight (fnweight)
1771 int fnweight;
1773 if (fnweight >= FW_HEAVY) return 210;
1774 if (fnweight >= FW_EXTRABOLD) return 205;
1775 if (fnweight >= FW_BOLD) return 200;
1776 if (fnweight >= FW_SEMIBOLD) return 180;
1777 if (fnweight >= FW_NORMAL) return 100;
1778 if (fnweight >= FW_LIGHT) return 50;
1779 if (fnweight >= FW_EXTRALIGHT) return 40;
1780 if (fnweight > FW_THIN) return 20;
1781 return 0;
1784 static int
1785 w32_encode_weight (n)
1786 int n;
1788 if (n >= 210) return FW_HEAVY;
1789 if (n >= 205) return FW_EXTRABOLD;
1790 if (n >= 200) return FW_BOLD;
1791 if (n >= 180) return FW_SEMIBOLD;
1792 if (n >= 100) return FW_NORMAL;
1793 if (n >= 50) return FW_LIGHT;
1794 if (n >= 40) return FW_EXTRALIGHT;
1795 if (n >= 20) return FW_THIN;
1796 return 0;
1799 /* Convert a Windows font weight into one of the weights supported
1800 by fontconfig (see font.c:font_parse_fcname). */
1801 static Lisp_Object
1802 w32_to_fc_weight (n)
1803 int n;
1805 if (n >= FW_EXTRABOLD) return intern ("black");
1806 if (n >= FW_BOLD) return intern ("bold");
1807 if (n >= FW_SEMIBOLD) return intern ("demibold");
1808 if (n >= FW_NORMAL) return intern ("medium");
1809 return intern ("light");
1812 /* Fill in all the available details of LOGFONT from FONT_SPEC. */
1813 static void
1814 fill_in_logfont (f, logfont, font_spec)
1815 FRAME_PTR f;
1816 LOGFONT *logfont;
1817 Lisp_Object font_spec;
1819 Lisp_Object tmp, extra;
1820 int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;
1822 tmp = AREF (font_spec, FONT_DPI_INDEX);
1823 if (INTEGERP (tmp))
1825 dpi = XINT (tmp);
1827 else if (FLOATP (tmp))
1829 dpi = (int) (XFLOAT_DATA (tmp) + 0.5);
1832 /* Height */
1833 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1834 if (INTEGERP (tmp))
1835 logfont->lfHeight = -1 * XINT (tmp);
1836 else if (FLOATP (tmp))
1837 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
1839 /* Escapement */
1841 /* Orientation */
1843 /* Weight */
1844 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1845 if (INTEGERP (tmp))
1846 logfont->lfWeight = w32_encode_weight (FONT_WEIGHT_NUMERIC (font_spec));
1848 /* Italic */
1849 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1850 if (INTEGERP (tmp))
1852 int slant = FONT_SLANT_NUMERIC (font_spec);
1853 logfont->lfItalic = slant > 150 ? 1 : 0;
1856 /* Underline */
1858 /* Strikeout */
1860 /* Charset */
1861 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1862 if (! NILP (tmp))
1863 logfont->lfCharSet = registry_to_w32_charset (tmp);
1864 else
1865 logfont->lfCharSet = DEFAULT_CHARSET;
1867 /* Out Precision */
1869 /* Clip Precision */
1871 /* Quality */
1872 logfont->lfQuality = DEFAULT_QUALITY;
1874 /* Generic Family and Face Name */
1875 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1877 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
1878 if (! NILP (tmp))
1880 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1881 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1882 ; /* Font name was generic, don't fill in font name. */
1883 /* Font families are interned, but allow for strings also in case of
1884 user input. */
1885 else if (SYMBOLP (tmp))
1886 strncpy (logfont->lfFaceName, SDATA (SYMBOL_NAME (tmp)), LF_FACESIZE);
1889 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1890 if (!NILP (tmp))
1892 /* Override generic family. */
1893 BYTE family = w32_generic_family (tmp);
1894 if (family != FF_DONTCARE)
1895 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
1899 /* Set pitch based on the spacing property. */
1900 tmp = AREF (font_spec, FONT_SPACING_INDEX);
1901 if (INTEGERP (tmp))
1903 int spacing = XINT (tmp);
1904 if (spacing < FONT_SPACING_MONO)
1905 logfont->lfPitchAndFamily
1906 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1907 else
1908 logfont->lfPitchAndFamily
1909 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1912 /* Process EXTRA info. */
1913 for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
1914 CONSP (extra); extra = XCDR (extra))
1916 tmp = XCAR (extra);
1917 if (CONSP (tmp))
1919 Lisp_Object key, val;
1920 key = XCAR (tmp), val = XCDR (tmp);
1921 /* Only use QCscript if charset is not provided, or is unicode
1922 and a single script is specified. This is rather crude,
1923 and is only used to narrow down the fonts returned where
1924 there is a definite match. Some scripts, such as latin, han,
1925 cjk-misc match multiple lfCharSet values, so we can't pre-filter
1926 them. */
1927 if (EQ (key, QCscript)
1928 && logfont->lfCharSet == DEFAULT_CHARSET
1929 && SYMBOLP (val))
1931 if (EQ (val, Qgreek))
1932 logfont->lfCharSet = GREEK_CHARSET;
1933 else if (EQ (val, Qhangul))
1934 logfont->lfCharSet = HANGUL_CHARSET;
1935 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
1936 logfont->lfCharSet = SHIFTJIS_CHARSET;
1937 else if (EQ (val, Qbopomofo))
1938 logfont->lfCharSet = CHINESEBIG5_CHARSET;
1939 /* GB 18030 supports tibetan, yi, mongolian,
1940 fonts that support it should show up if we ask for
1941 GB2312 fonts. */
1942 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
1943 || EQ (val, Qmongolian))
1944 logfont->lfCharSet = GB2312_CHARSET;
1945 else if (EQ (val, Qhebrew))
1946 logfont->lfCharSet = HEBREW_CHARSET;
1947 else if (EQ (val, Qarabic))
1948 logfont->lfCharSet = ARABIC_CHARSET;
1949 else if (EQ (val, Qthai))
1950 logfont->lfCharSet = THAI_CHARSET;
1951 else if (EQ (val, Qsymbol))
1952 logfont->lfCharSet = SYMBOL_CHARSET;
1954 else if (EQ (key, QCantialias) && SYMBOLP (val))
1956 logfont->lfQuality = w32_antialias_type (val);
1962 static void
1963 list_all_matching_fonts (match_data)
1964 struct font_callback_data *match_data;
1966 HDC dc;
1967 Lisp_Object families = w32font_list_family (match_data->frame);
1968 struct frame *f = XFRAME (match_data->frame);
1970 dc = get_frame_dc (f);
1972 while (!NILP (families))
1974 /* TODO: Use the Unicode versions of the W32 APIs, so we can
1975 handle non-ASCII font names. */
1976 char *name;
1977 Lisp_Object family = CAR (families);
1978 families = CDR (families);
1979 if (NILP (family))
1980 continue;
1981 else if (SYMBOLP (family))
1982 name = SDATA (SYMBOL_NAME (family));
1983 else
1984 continue;
1986 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
1987 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
1989 EnumFontFamiliesEx (dc, &match_data->pattern,
1990 (FONTENUMPROC) add_font_entity_to_list,
1991 (LPARAM) match_data, 0);
1994 release_frame_dc (f, dc);
1997 static Lisp_Object
1998 lispy_antialias_type (type)
1999 BYTE type;
2001 Lisp_Object lispy;
2003 switch (type)
2005 case NONANTIALIASED_QUALITY:
2006 lispy = Qnone;
2007 break;
2008 case ANTIALIASED_QUALITY:
2009 lispy = Qstandard;
2010 break;
2011 case CLEARTYPE_QUALITY:
2012 lispy = Qsubpixel;
2013 break;
2014 case CLEARTYPE_NATURAL_QUALITY:
2015 lispy = Qnatural;
2016 break;
2017 default:
2018 lispy = Qnil;
2019 break;
2021 return lispy;
2024 /* Convert antialiasing symbols to lfQuality */
2025 static BYTE
2026 w32_antialias_type (type)
2027 Lisp_Object type;
2029 if (EQ (type, Qnone))
2030 return NONANTIALIASED_QUALITY;
2031 else if (EQ (type, Qstandard))
2032 return ANTIALIASED_QUALITY;
2033 else if (EQ (type, Qsubpixel))
2034 return CLEARTYPE_QUALITY;
2035 else if (EQ (type, Qnatural))
2036 return CLEARTYPE_NATURAL_QUALITY;
2037 else
2038 return DEFAULT_QUALITY;
2041 /* Return a list of all the scripts that the font supports. */
2042 static Lisp_Object
2043 font_supported_scripts (FONTSIGNATURE * sig)
2045 DWORD * subranges = sig->fsUsb;
2046 Lisp_Object supported = Qnil;
2048 /* Match a single subrange. SYM is set if bit N is set in subranges. */
2049 #define SUBRANGE(n,sym) \
2050 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
2051 supported = Fcons ((sym), supported)
2053 /* Match multiple subranges. SYM is set if any MASK bit is set in
2054 subranges[0 - 3]. */
2055 #define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
2056 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
2057 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
2058 supported = Fcons ((sym), supported)
2060 SUBRANGE (0, Qlatin);
2061 /* The following count as latin too, ASCII should be present in these fonts,
2062 so don't need to mark them separately. */
2063 /* 1: Latin-1 supplement, 2: Latin Extended A, 3: Latin Extended B. */
2064 SUBRANGE (4, Qphonetic);
2065 /* 5: Spacing and tone modifiers, 6: Combining Diacriticals. */
2066 SUBRANGE (7, Qgreek);
2067 SUBRANGE (8, Qcoptic);
2068 SUBRANGE (9, Qcyrillic);
2069 SUBRANGE (10, Qarmenian);
2070 SUBRANGE (11, Qhebrew);
2071 SUBRANGE (13, Qarabic);
2072 SUBRANGE (14, Qnko);
2073 SUBRANGE (15, Qdevanagari);
2074 SUBRANGE (16, Qbengali);
2075 SUBRANGE (17, Qgurmukhi);
2076 SUBRANGE (18, Qgujarati);
2077 SUBRANGE (19, Qoriya);
2078 SUBRANGE (20, Qtamil);
2079 SUBRANGE (21, Qtelugu);
2080 SUBRANGE (22, Qkannada);
2081 SUBRANGE (23, Qmalayalam);
2082 SUBRANGE (24, Qthai);
2083 SUBRANGE (25, Qlao);
2084 SUBRANGE (26, Qgeorgian);
2085 SUBRANGE (27, Qbalinese);
2086 /* 28: Hangul Jamo. */
2087 /* 29: Latin Extended, 30: Greek Extended, 31: Punctuation. */
2088 /* 32-47: Symbols (defined below). */
2089 SUBRANGE (48, Qcjk_misc);
2090 /* Match either 49: katakana or 50: hiragana for kana. */
2091 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
2092 SUBRANGE (51, Qbopomofo);
2093 /* 52: Compatibility Jamo */
2094 SUBRANGE (53, Qphags_pa);
2095 /* 54: Enclosed CJK letters and months, 55: CJK Compatibility. */
2096 SUBRANGE (56, Qhangul);
2097 /* 57: Surrogates. */
2098 SUBRANGE (58, Qphoenician);
2099 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
2100 SUBRANGE (59, Qideographic_description); /* Windows lumps this in. */
2101 SUBRANGE (59, Qkanbun); /* And this. */
2102 /* 60: Private use, 61: CJK strokes and compatibility. */
2103 /* 62: Alphabetic Presentation, 63: Arabic Presentation A. */
2104 /* 64: Combining half marks, 65: Vertical and CJK compatibility. */
2105 /* 66: Small forms, 67: Arabic Presentation B, 68: Half and Full width. */
2106 /* 69: Specials. */
2107 SUBRANGE (70, Qtibetan);
2108 SUBRANGE (71, Qsyriac);
2109 SUBRANGE (72, Qthaana);
2110 SUBRANGE (73, Qsinhala);
2111 SUBRANGE (74, Qmyanmar);
2112 SUBRANGE (75, Qethiopic);
2113 SUBRANGE (76, Qcherokee);
2114 SUBRANGE (77, Qcanadian_aboriginal);
2115 SUBRANGE (78, Qogham);
2116 SUBRANGE (79, Qrunic);
2117 SUBRANGE (80, Qkhmer);
2118 SUBRANGE (81, Qmongolian);
2119 SUBRANGE (82, Qbraille);
2120 SUBRANGE (83, Qyi);
2121 SUBRANGE (84, Qbuhid);
2122 SUBRANGE (84, Qhanunoo);
2123 SUBRANGE (84, Qtagalog);
2124 SUBRANGE (84, Qtagbanwa);
2125 SUBRANGE (85, Qold_italic);
2126 SUBRANGE (86, Qgothic);
2127 SUBRANGE (87, Qdeseret);
2128 SUBRANGE (88, Qbyzantine_musical_symbol);
2129 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
2130 SUBRANGE (89, Qmathematical);
2131 /* 90: Private use, 91: Variation selectors, 92: Tags. */
2132 SUBRANGE (93, Qlimbu);
2133 SUBRANGE (94, Qtai_le);
2134 /* 95: New Tai Le */
2135 SUBRANGE (90, Qbuginese);
2136 SUBRANGE (97, Qglagolitic);
2137 SUBRANGE (98, Qtifinagh);
2138 /* 99: Yijing Hexagrams. */
2139 SUBRANGE (100, Qsyloti_nagri);
2140 SUBRANGE (101, Qlinear_b);
2141 /* 102: Ancient Greek Numbers. */
2142 SUBRANGE (103, Qugaritic);
2143 SUBRANGE (104, Qold_persian);
2144 SUBRANGE (105, Qshavian);
2145 SUBRANGE (106, Qosmanya);
2146 SUBRANGE (107, Qcypriot);
2147 SUBRANGE (108, Qkharoshthi);
2148 /* 109: Tai Xuan Jing. */
2149 SUBRANGE (110, Qcuneiform);
2150 /* 111: Counting Rods. */
2152 /* There isn't really a main symbol range, so include symbol if any
2153 relevant range is set. */
2154 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
2156 /* Missing: Tai Viet (U+AA80) and Cham (U+AA00) . */
2157 #undef SUBRANGE
2158 #undef MASK_ANY
2160 return supported;
2163 /* Generate a full name for a Windows font.
2164 The full name is in fcname format, with weight, slant and antialiasing
2165 specified if they are not "normal". */
2166 static int
2167 w32font_full_name (font, font_obj, pixel_size, name, nbytes)
2168 LOGFONT * font;
2169 Lisp_Object font_obj;
2170 int pixel_size;
2171 char *name;
2172 int nbytes;
2174 int len, height, outline;
2175 char *p;
2176 Lisp_Object antialiasing, weight = Qnil;
2178 len = strlen (font->lfFaceName);
2180 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
2182 /* Represent size of scalable fonts by point size. But use pixelsize for
2183 raster fonts to indicate that they are exactly that size. */
2184 if (outline)
2185 len += 11; /* -SIZE */
2186 else
2187 len += 21;
2189 if (font->lfItalic)
2190 len += 7; /* :italic */
2192 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2194 weight = w32_to_fc_weight (font->lfWeight);
2195 len += 1 + SBYTES (SYMBOL_NAME (weight)); /* :WEIGHT */
2198 antialiasing = lispy_antialias_type (font->lfQuality);
2199 if (! NILP (antialiasing))
2200 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
2202 /* Check that the buffer is big enough */
2203 if (len > nbytes)
2204 return -1;
2206 p = name;
2207 p += sprintf (p, "%s", font->lfFaceName);
2209 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
2211 if (height > 0)
2213 if (outline)
2215 float pointsize = height * 72.0 / one_w32_display_info.resy;
2216 /* Round to nearest half point. floor is used, since round is not
2217 supported in MS library. */
2218 pointsize = floor (pointsize * 2 + 0.5) / 2;
2219 p += sprintf (p, "-%1.1f", pointsize);
2221 else
2222 p += sprintf (p, ":pixelsize=%d", height);
2225 if (SYMBOLP (weight) && ! NILP (weight))
2226 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2228 if (font->lfItalic)
2229 p += sprintf (p, ":italic");
2231 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
2232 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
2234 return (p - name);
2237 /* Convert a logfont and point size into a fontconfig style font name.
2238 POINTSIZE is in tenths of points.
2239 If SIZE indicates the size of buffer FCNAME, into which the font name
2240 is written. If the buffer is not large enough to contain the name,
2241 the function returns -1, otherwise it returns the number of bytes
2242 written to FCNAME. */
2243 static int logfont_to_fcname(font, pointsize, fcname, size)
2244 LOGFONT* font;
2245 int pointsize;
2246 char *fcname;
2247 int size;
2249 int len, height;
2250 char *p = fcname;
2251 Lisp_Object weight = Qnil;
2253 len = strlen (font->lfFaceName) + 2;
2254 height = pointsize / 10;
2255 while (height /= 10)
2256 len++;
2258 if (pointsize % 10)
2259 len += 2;
2261 if (font->lfItalic)
2262 len += 7; /* :italic */
2263 if (font->lfWeight && font->lfWeight != FW_NORMAL)
2265 weight = w32_to_fc_weight (font->lfWeight);
2266 len += SBYTES (SYMBOL_NAME (weight)) + 1;
2269 if (len > size)
2270 return -1;
2272 p += sprintf (p, "%s-%d", font->lfFaceName, pointsize / 10);
2273 if (pointsize % 10)
2274 p += sprintf (p, ".%d", pointsize % 10);
2276 if (SYMBOLP (weight) && !NILP (weight))
2277 p += sprintf (p, ":%s", SDATA (SYMBOL_NAME (weight)));
2279 if (font->lfItalic)
2280 p += sprintf (p, ":italic");
2282 return (p - fcname);
2285 static void
2286 compute_metrics (dc, w32_font, code, metrics)
2287 HDC dc;
2288 struct w32font_info *w32_font;
2289 unsigned int code;
2290 struct w32_metric_cache *metrics;
2292 GLYPHMETRICS gm;
2293 MAT2 transform;
2294 unsigned int options = GGO_METRICS;
2296 if (w32_font->glyph_idx)
2297 options |= GGO_GLYPH_INDEX;
2299 bzero (&transform, sizeof (transform));
2300 transform.eM11.value = 1;
2301 transform.eM22.value = 1;
2303 if (GetGlyphOutlineW (dc, code, options, &gm, 0, NULL, &transform)
2304 != GDI_ERROR)
2306 metrics->lbearing = gm.gmptGlyphOrigin.x;
2307 metrics->rbearing = gm.gmptGlyphOrigin.x + gm.gmBlackBoxX;
2308 metrics->width = gm.gmCellIncX;
2309 metrics->status = W32METRIC_SUCCESS;
2311 else
2312 metrics->status = W32METRIC_FAIL;
2315 static void
2316 clear_cached_metrics (w32_font)
2317 struct w32font_info *w32_font;
2319 int i;
2320 for (i = 0; i < w32_font->n_cache_blocks; i++)
2322 if (w32_font->cached_metrics[i])
2323 bzero (w32_font->cached_metrics[i],
2324 CACHE_BLOCKSIZE * sizeof (struct font_metrics));
2328 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
2329 doc: /* Read a font name using a W32 font selection dialog.
2330 Return fontconfig style font string corresponding to the selection.
2332 If FRAME is omitted or nil, it defaults to the selected frame.
2333 If EXCLUDE-PROPORTIONAL is non-nil, exclude proportional fonts
2334 in the font selection dialog. */)
2335 (frame, exclude_proportional)
2336 Lisp_Object frame, exclude_proportional;
2338 FRAME_PTR f = check_x_frame (frame);
2339 CHOOSEFONT cf;
2340 LOGFONT lf;
2341 TEXTMETRIC tm;
2342 HDC hdc;
2343 HANDLE oldobj;
2344 char buf[100];
2346 bzero (&cf, sizeof (cf));
2347 bzero (&lf, sizeof (lf));
2349 cf.lStructSize = sizeof (cf);
2350 cf.hwndOwner = FRAME_W32_WINDOW (f);
2351 cf.Flags = CF_FORCEFONTEXIST | CF_SCREENFONTS | CF_NOVERTFONTS;
2353 /* If exclude_proportional is non-nil, limit the selection to
2354 monospaced fonts. */
2355 if (!NILP (exclude_proportional))
2356 cf.Flags |= CF_FIXEDPITCHONLY;
2358 cf.lpLogFont = &lf;
2360 /* Initialize as much of the font details as we can from the current
2361 default font. */
2362 hdc = GetDC (FRAME_W32_WINDOW (f));
2363 oldobj = SelectObject (hdc, FONT_HANDLE (FRAME_FONT (f)));
2364 GetTextFace (hdc, LF_FACESIZE, lf.lfFaceName);
2365 if (GetTextMetrics (hdc, &tm))
2367 lf.lfHeight = tm.tmInternalLeading - tm.tmHeight;
2368 lf.lfWeight = tm.tmWeight;
2369 lf.lfItalic = tm.tmItalic;
2370 lf.lfUnderline = tm.tmUnderlined;
2371 lf.lfStrikeOut = tm.tmStruckOut;
2372 lf.lfCharSet = tm.tmCharSet;
2373 cf.Flags |= CF_INITTOLOGFONTSTRUCT;
2375 SelectObject (hdc, oldobj);
2376 ReleaseDC (FRAME_W32_WINDOW (f), hdc);
2378 if (!ChooseFont (&cf)
2379 || logfont_to_fcname (&lf, cf.iPointSize, buf, 100) < 0)
2380 return Qnil;
2382 return build_string (buf);
2385 struct font_driver w32font_driver =
2387 0, /* Qgdi */
2388 0, /* case insensitive */
2389 w32font_get_cache,
2390 w32font_list,
2391 w32font_match,
2392 w32font_list_family,
2393 NULL, /* free_entity */
2394 w32font_open,
2395 w32font_close,
2396 NULL, /* prepare_face */
2397 NULL, /* done_face */
2398 w32font_has_char,
2399 w32font_encode_char,
2400 w32font_text_extents,
2401 w32font_draw,
2402 NULL, /* get_bitmap */
2403 NULL, /* free_bitmap */
2404 NULL, /* get_outline */
2405 NULL, /* free_outline */
2406 NULL, /* anchor_point */
2407 NULL, /* otf_capability */
2408 NULL, /* otf_drive */
2409 NULL, /* start_for_frame */
2410 NULL, /* end_for_frame */
2411 NULL /* shape */
2415 /* Initialize state that does not change between invocations. This is only
2416 called when Emacs is dumped. */
2417 void
2418 syms_of_w32font ()
2420 DEFSYM (Qgdi, "gdi");
2421 DEFSYM (Quniscribe, "uniscribe");
2422 DEFSYM (QCformat, ":format");
2424 /* Generic font families. */
2425 DEFSYM (Qmonospace, "monospace");
2426 DEFSYM (Qserif, "serif");
2427 DEFSYM (Qsansserif, "sansserif");
2428 DEFSYM (Qscript, "script");
2429 DEFSYM (Qdecorative, "decorative");
2430 /* Aliases. */
2431 DEFSYM (Qsans_serif, "sans_serif");
2432 DEFSYM (Qsans, "sans");
2433 DEFSYM (Qmono, "mono");
2435 /* Fake foundries. */
2436 DEFSYM (Qraster, "raster");
2437 DEFSYM (Qoutline, "outline");
2438 DEFSYM (Qunknown, "unknown");
2440 /* Antialiasing. */
2441 DEFSYM (Qstandard, "standard");
2442 DEFSYM (Qsubpixel, "subpixel");
2443 DEFSYM (Qnatural, "natural");
2445 /* Languages */
2446 DEFSYM (Qja, "ja");
2447 DEFSYM (Qko, "ko");
2448 DEFSYM (Qzh, "zh");
2450 /* Scripts */
2451 DEFSYM (Qlatin, "latin");
2452 DEFSYM (Qgreek, "greek");
2453 DEFSYM (Qcoptic, "coptic");
2454 DEFSYM (Qcyrillic, "cyrillic");
2455 DEFSYM (Qarmenian, "armenian");
2456 DEFSYM (Qhebrew, "hebrew");
2457 DEFSYM (Qarabic, "arabic");
2458 DEFSYM (Qsyriac, "syriac");
2459 DEFSYM (Qnko, "nko");
2460 DEFSYM (Qthaana, "thaana");
2461 DEFSYM (Qdevanagari, "devanagari");
2462 DEFSYM (Qbengali, "bengali");
2463 DEFSYM (Qgurmukhi, "gurmukhi");
2464 DEFSYM (Qgujarati, "gujarati");
2465 DEFSYM (Qoriya, "oriya");
2466 DEFSYM (Qtamil, "tamil");
2467 DEFSYM (Qtelugu, "telugu");
2468 DEFSYM (Qkannada, "kannada");
2469 DEFSYM (Qmalayalam, "malayalam");
2470 DEFSYM (Qsinhala, "sinhala");
2471 DEFSYM (Qthai, "thai");
2472 DEFSYM (Qlao, "lao");
2473 DEFSYM (Qtibetan, "tibetan");
2474 DEFSYM (Qmyanmar, "myanmar");
2475 DEFSYM (Qgeorgian, "georgian");
2476 DEFSYM (Qhangul, "hangul");
2477 DEFSYM (Qethiopic, "ethiopic");
2478 DEFSYM (Qcherokee, "cherokee");
2479 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
2480 DEFSYM (Qogham, "ogham");
2481 DEFSYM (Qrunic, "runic");
2482 DEFSYM (Qkhmer, "khmer");
2483 DEFSYM (Qmongolian, "mongolian");
2484 DEFSYM (Qsymbol, "symbol");
2485 DEFSYM (Qbraille, "braille");
2486 DEFSYM (Qhan, "han");
2487 DEFSYM (Qideographic_description, "ideographic-description");
2488 DEFSYM (Qcjk_misc, "cjk-misc");
2489 DEFSYM (Qkana, "kana");
2490 DEFSYM (Qbopomofo, "bopomofo");
2491 DEFSYM (Qkanbun, "kanbun");
2492 DEFSYM (Qyi, "yi");
2493 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
2494 DEFSYM (Qmusical_symbol, "musical-symbol");
2495 DEFSYM (Qmathematical, "mathematical");
2496 DEFSYM (Qphonetic, "phonetic");
2497 DEFSYM (Qbalinese, "balinese");
2498 DEFSYM (Qbuginese, "buginese");
2499 DEFSYM (Qbuhid, "buhid");
2500 DEFSYM (Qcuneiform, "cuneiform");
2501 DEFSYM (Qcypriot, "cypriot");
2502 DEFSYM (Qdeseret, "deseret");
2503 DEFSYM (Qglagolitic, "glagolitic");
2504 DEFSYM (Qgothic, "gothic");
2505 DEFSYM (Qhanunoo, "hanunoo");
2506 DEFSYM (Qkharoshthi, "kharoshthi");
2507 DEFSYM (Qlimbu, "limbu");
2508 DEFSYM (Qlinear_b, "linear_b");
2509 DEFSYM (Qold_italic, "old_italic");
2510 DEFSYM (Qold_persian, "old_persian");
2511 DEFSYM (Qosmanya, "osmanya");
2512 DEFSYM (Qphags_pa, "phags-pa");
2513 DEFSYM (Qphoenician, "phoenician");
2514 DEFSYM (Qshavian, "shavian");
2515 DEFSYM (Qsyloti_nagri, "syloti_nagri");
2516 DEFSYM (Qtagalog, "tagalog");
2517 DEFSYM (Qtagbanwa, "tagbanwa");
2518 DEFSYM (Qtai_le, "tai_le");
2519 DEFSYM (Qtifinagh, "tifinagh");
2520 DEFSYM (Qugaritic, "ugaritic");
2522 /* W32 font encodings. */
2523 DEFVAR_LISP ("w32-charset-info-alist",
2524 &Vw32_charset_info_alist,
2525 doc: /* Alist linking Emacs character sets to Windows fonts and codepages.
2526 Each entry should be of the form:
2528 (CHARSET_NAME . (WINDOWS_CHARSET . CODEPAGE))
2530 where CHARSET_NAME is a string used in font names to identify the charset,
2531 WINDOWS_CHARSET is a symbol that can be one of:
2533 w32-charset-ansi, w32-charset-default, w32-charset-symbol,
2534 w32-charset-shiftjis, w32-charset-hangeul, w32-charset-gb2312,
2535 w32-charset-chinesebig5, w32-charset-johab, w32-charset-hebrew,
2536 w32-charset-arabic, w32-charset-greek, w32-charset-turkish,
2537 w32-charset-vietnamese, w32-charset-thai, w32-charset-easteurope,
2538 w32-charset-russian, w32-charset-mac, w32-charset-baltic,
2539 or w32-charset-oem.
2541 CODEPAGE should be an integer specifying the codepage that should be used
2542 to display the character set, t to do no translation and output as Unicode,
2543 or nil to do no translation and output as 8 bit (or multibyte on far-east
2544 versions of Windows) characters. */);
2545 Vw32_charset_info_alist = Qnil;
2547 DEFSYM (Qw32_charset_ansi, "w32-charset-ansi");
2548 DEFSYM (Qw32_charset_symbol, "w32-charset-symbol");
2549 DEFSYM (Qw32_charset_default, "w32-charset-default");
2550 DEFSYM (Qw32_charset_shiftjis, "w32-charset-shiftjis");
2551 DEFSYM (Qw32_charset_hangeul, "w32-charset-hangeul");
2552 DEFSYM (Qw32_charset_chinesebig5, "w32-charset-chinesebig5");
2553 DEFSYM (Qw32_charset_gb2312, "w32-charset-gb2312");
2554 DEFSYM (Qw32_charset_oem, "w32-charset-oem");
2555 DEFSYM (Qw32_charset_johab, "w32-charset-johab");
2556 DEFSYM (Qw32_charset_easteurope, "w32-charset-easteurope");
2557 DEFSYM (Qw32_charset_turkish, "w32-charset-turkish");
2558 DEFSYM (Qw32_charset_baltic, "w32-charset-baltic");
2559 DEFSYM (Qw32_charset_russian, "w32-charset-russian");
2560 DEFSYM (Qw32_charset_arabic, "w32-charset-arabic");
2561 DEFSYM (Qw32_charset_greek, "w32-charset-greek");
2562 DEFSYM (Qw32_charset_hebrew, "w32-charset-hebrew");
2563 DEFSYM (Qw32_charset_vietnamese, "w32-charset-vietnamese");
2564 DEFSYM (Qw32_charset_thai, "w32-charset-thai");
2565 DEFSYM (Qw32_charset_mac, "w32-charset-mac");
2567 defsubr (&Sx_select_font);
2569 w32font_driver.type = Qgdi;
2570 register_font_driver (&w32font_driver, NULL);
2573 /* arch-tag: 65b8a3cd-46aa-4c0d-a1f3-99e75b9c07ee
2574 (do not change this comment) */