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