Update for rename islamic-diary-entry-symbol to diary-islamic-entry-symbol.
[emacs.git] / src / w32font.c
blob866b86e228704a17f47bdba1b3ff1010a8866275
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 = font->ascent;
382 metrics->descent = font->descent;
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;
403 else
405 if (dc == NULL)
407 dc = get_frame_dc (f);
408 old_font = SelectObject (dc, ((W32FontStruct *)
409 (font->font.font))->hfont);
411 if (GetGlyphOutlineW (dc, *(code + i),
412 GGO_METRICS
413 | w32_font->glyph_idx
414 ? GGO_GLYPH_INDEX : 0,
415 &gm, 0, NULL, &transform) != GDI_ERROR)
417 int new_val = metrics->width + gm.gmBlackBoxX
418 + gm.gmptGlyphOrigin.x;
419 metrics->rbearing = max (metrics->rbearing, new_val);
420 new_val = metrics->width + gm.gmptGlyphOrigin.x;
421 metrics->lbearing = min (metrics->lbearing, new_val);
422 metrics->width += gm.gmCellIncX;
424 else
426 if (w32_font->glyph_idx)
428 /* Disable glyph indexing for this font, as we can't
429 handle the metrics. Abort this run, our recovery
430 strategies rely on having unicode code points here.
431 This will cause a glitch in display, but in practice,
432 any problems should be caught when initialising the
433 metrics cache. */
434 w32_font->glyph_idx = 0;
435 recompute_cached_metrics (dc, w32_font);
436 SelectObject (dc, old_font);
437 release_frame_dc (f, dc);
438 return 0;
440 /* Rely on an estimate based on the overall font metrics. */
441 break;
446 /* If we got through everything, return. */
447 if (i == nglyphs)
449 if (dc != NULL)
451 /* Restore state and release DC. */
452 SelectObject (dc, old_font);
453 release_frame_dc (f, dc);
456 return metrics->width;
460 /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
461 fallback on other methods that will at least give some of the metric
462 information. */
463 for (i = 0; i < nglyphs; i++)
465 if (code[i] < 0x10000)
466 wcode[i] = code[i];
467 else
469 /* TODO: Convert to surrogate, reallocating array if needed */
470 wcode[i] = 0xffff;
474 if (dc == NULL)
476 dc = get_frame_dc (f);
477 old_font = SelectObject (dc, ((W32FontStruct *)
478 (font->font.font))->hfont);
481 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
483 total_width = size.cx;
486 /* On 95/98/ME, only some unicode functions are available, so fallback
487 on doing a dummy draw to find the total width. */
488 if (!total_width)
490 RECT rect;
491 rect.top = 0; rect.bottom = font->font.height; rect.left = 0; rect.right = 1;
492 DrawTextW (dc, wcode, nglyphs, &rect,
493 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
494 total_width = rect.right;
497 /* Give our best estimate of the metrics, based on what we know. */
498 if (metrics)
500 metrics->width = total_width;
501 metrics->lbearing = 0;
502 metrics->rbearing = total_width
503 + ((struct w32font_info *) font)->metrics.tmOverhang;
506 /* Restore state and release DC. */
507 SelectObject (dc, old_font);
508 release_frame_dc (f, dc);
510 return total_width;
513 /* w32 implementation of draw for font backend.
514 Optional.
515 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
516 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND
517 is nonzero, fill the background in advance. It is assured that
518 WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).
520 TODO: Currently this assumes that the colors and fonts are already
521 set in the DC. This seems to be true now, but maybe only due to
522 the old font code setting it up. It may be safer to resolve faces
523 and fonts in here and set them explicitly
527 w32font_draw (s, from, to, x, y, with_background)
528 struct glyph_string *s;
529 int from, to, x, y, with_background;
531 UINT options;
532 HRGN orig_clip;
533 struct w32font_info *w32font = (struct w32font_info *) s->face->font_info;
535 options = w32font->glyph_idx;
537 /* Save clip region for later restoration. */
538 GetClipRgn(s->hdc, orig_clip);
540 if (s->num_clips > 0)
542 HRGN new_clip = CreateRectRgnIndirect (s->clip);
544 if (s->num_clips > 1)
546 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
548 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
549 DeleteObject (clip2);
552 SelectClipRgn (s->hdc, new_clip);
553 DeleteObject (new_clip);
556 /* Using OPAQUE background mode can clear more background than expected
557 when Cleartype is used. Draw the background manually to avoid this. */
558 SetBkMode (s->hdc, TRANSPARENT);
559 if (with_background)
561 HBRUSH brush;
562 RECT rect;
563 struct font *font = (struct font *) s->face->font_info;
565 brush = CreateSolidBrush (s->gc->background);
566 rect.left = x;
567 rect.top = y - font->ascent;
568 rect.right = x + s->width;
569 rect.bottom = y + font->descent;
570 FillRect (s->hdc, &rect, brush);
571 DeleteObject (brush);
574 if (s->padding_p)
576 int len = to - from, i;
578 for (i = 0; i < len; i++)
579 ExtTextOutW (s->hdc, x + i, y, options, NULL,
580 s->char2b + from + i, 1, NULL);
582 else
583 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
585 /* Restore clip region. */
586 if (s->num_clips > 0)
588 SelectClipRgn (s->hdc, orig_clip);
592 /* w32 implementation of free_entity for font backend.
593 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
594 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
595 static void
596 w32font_free_entity (Lisp_Object entity);
599 /* w32 implementation of prepare_face for font backend.
600 Optional (if FACE->extra is not used).
601 Prepare FACE for displaying characters by FONT on frame F by
602 storing some data in FACE->extra. If successful, return 0.
603 Otherwise, return -1.
604 static int
605 w32font_prepare_face (FRAME_PTR f, struct face *face);
607 /* w32 implementation of done_face for font backend.
608 Optional.
609 Done FACE for displaying characters by FACE->font on frame F.
610 static void
611 w32font_done_face (FRAME_PTR f, struct face *face); */
613 /* w32 implementation of get_bitmap for font backend.
614 Optional.
615 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
616 intended that this method is called from the other font-driver
617 for actual drawing.
618 static int
619 w32font_get_bitmap (struct font *font, unsigned code,
620 struct font_bitmap *bitmap, int bits_per_pixel);
622 /* w32 implementation of free_bitmap for font backend.
623 Optional.
624 Free bitmap data in BITMAP.
625 static void
626 w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
628 /* w32 implementation of get_outline for font backend.
629 Optional.
630 Return an outline data for glyph-code CODE of FONT. The format
631 of the outline data depends on the font-driver.
632 static void *
633 w32font_get_outline (struct font *font, unsigned code);
635 /* w32 implementation of free_outline for font backend.
636 Optional.
637 Free OUTLINE (that is obtained by the above method).
638 static void
639 w32font_free_outline (struct font *font, void *outline);
641 /* w32 implementation of anchor_point for font backend.
642 Optional.
643 Get coordinates of the INDEXth anchor point of the glyph whose
644 code is CODE. Store the coordinates in *X and *Y. Return 0 if
645 the operations was successfull. Otherwise return -1.
646 static int
647 w32font_anchor_point (struct font *font, unsigned code,
648 int index, int *x, int *y);
650 /* w32 implementation of otf_capability for font backend.
651 Optional.
652 Return a list describing which scripts/languages FONT
653 supports by which GSUB/GPOS features of OpenType tables.
654 static Lisp_Object
655 w32font_otf_capability (struct font *font);
657 /* w32 implementation of otf_drive for font backend.
658 Optional.
659 Apply FONT's OTF-FEATURES to the glyph string.
661 FEATURES specifies which OTF features to apply in this format:
662 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
663 See the documentation of `font-drive-otf' for the detail.
665 This method applies the specified features to the codes in the
666 elements of GSTRING-IN (between FROMth and TOth). The output
667 codes are stored in GSTRING-OUT at the IDXth element and the
668 following elements.
670 Return the number of output codes. If none of the features are
671 applicable to the input data, return 0. If GSTRING-OUT is too
672 short, return -1.
673 static int
674 w32font_otf_drive (struct font *font, Lisp_Object features,
675 Lisp_Object gstring_in, int from, int to,
676 Lisp_Object gstring_out, int idx,
677 int alternate_subst);
680 /* Internal implementation of w32font_list.
681 Additional parameter opentype_only restricts the returned fonts to
682 opentype fonts, which can be used with the Uniscribe backend. */
683 Lisp_Object
684 w32font_list_internal (frame, font_spec, opentype_only)
685 Lisp_Object frame, font_spec;
686 int opentype_only;
688 struct font_callback_data match_data;
689 HDC dc;
690 FRAME_PTR f = XFRAME (frame);
692 match_data.orig_font_spec = font_spec;
693 match_data.list = Qnil;
694 match_data.frame = frame;
696 bzero (&match_data.pattern, sizeof (LOGFONT));
697 fill_in_logfont (f, &match_data.pattern, font_spec);
699 match_data.opentype_only = opentype_only;
700 if (opentype_only)
701 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
703 if (match_data.pattern.lfFaceName[0] == '\0')
705 /* EnumFontFamiliesEx does not take other fields into account if
706 font name is blank, so need to use two passes. */
707 list_all_matching_fonts (&match_data);
709 else
711 dc = get_frame_dc (f);
713 EnumFontFamiliesEx (dc, &match_data.pattern,
714 (FONTENUMPROC) add_font_entity_to_list,
715 (LPARAM) &match_data, 0);
716 release_frame_dc (f, dc);
719 return NILP (match_data.list) ? null_vector : Fvconcat (1, &match_data.list);
722 /* Internal implementation of w32font_match.
723 Additional parameter opentype_only restricts the returned fonts to
724 opentype fonts, which can be used with the Uniscribe backend. */
725 Lisp_Object
726 w32font_match_internal (frame, font_spec, opentype_only)
727 Lisp_Object frame, font_spec;
728 int opentype_only;
730 struct font_callback_data match_data;
731 HDC dc;
732 FRAME_PTR f = XFRAME (frame);
734 match_data.orig_font_spec = font_spec;
735 match_data.frame = frame;
736 match_data.list = Qnil;
738 bzero (&match_data.pattern, sizeof (LOGFONT));
739 fill_in_logfont (f, &match_data.pattern, font_spec);
741 match_data.opentype_only = opentype_only;
742 if (opentype_only)
743 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
745 dc = get_frame_dc (f);
747 EnumFontFamiliesEx (dc, &match_data.pattern,
748 (FONTENUMPROC) add_one_font_entity_to_list,
749 (LPARAM) &match_data, 0);
750 release_frame_dc (f, dc);
752 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
756 w32font_open_internal (f, font_entity, pixel_size, w32_font)
757 FRAME_PTR f;
758 Lisp_Object font_entity;
759 int pixel_size;
760 struct w32font_info *w32_font;
762 int len, size;
763 LOGFONT logfont;
764 HDC dc;
765 HFONT hfont, old_font;
766 Lisp_Object val, extra;
767 /* For backwards compatibility. */
768 W32FontStruct *compat_w32_font;
770 struct font * font = (struct font *) w32_font;
771 if (!font)
772 return 0;
774 bzero (&logfont, sizeof (logfont));
775 fill_in_logfont (f, &logfont, font_entity);
777 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
778 if (!size)
779 size = pixel_size;
781 logfont.lfHeight = -size;
782 hfont = CreateFontIndirect (&logfont);
784 if (hfont == NULL)
785 return 0;
787 /* Get the metrics for this font. */
788 dc = get_frame_dc (f);
789 old_font = SelectObject (dc, hfont);
791 GetTextMetrics (dc, &w32_font->metrics);
793 w32_font->glyph_idx = ETO_GLYPH_INDEX;
795 /* Cache ASCII metrics. */
796 recompute_cached_metrics (dc, w32_font);
798 SelectObject (dc, old_font);
799 release_frame_dc (f, dc);
801 /* W32FontStruct - we should get rid of this, and use the w32font_info
802 struct for any W32 specific fields. font->font.font can then be hfont. */
803 font->font.font = xmalloc (sizeof (W32FontStruct));
804 compat_w32_font = (W32FontStruct *) font->font.font;
805 bzero (compat_w32_font, sizeof (W32FontStruct));
806 compat_w32_font->font_type = UNICODE_FONT;
807 /* Duplicate the text metrics. */
808 bcopy (&w32_font->metrics, &compat_w32_font->tm, sizeof (TEXTMETRIC));
809 compat_w32_font->hfont = hfont;
811 len = strlen (logfont.lfFaceName);
812 font->font.name = (char *) xmalloc (len + 1);
813 bcopy (logfont.lfFaceName, font->font.name, len);
814 font->font.name[len] = '\0';
817 char *name;
819 /* We don't know how much space we need for the full name, so start with
820 96 bytes and go up in steps of 32. */
821 len = 96;
822 name = xmalloc (len);
823 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
824 name, len) < 0)
826 char *new = xrealloc (name, len += 32);
828 if (! new)
829 xfree (name);
830 name = new;
832 if (name)
833 font->font.full_name = name;
834 else
835 font->font.full_name = font->font.name;
837 font->font.charset = 0;
838 font->font.codepage = 0;
839 font->font.size = w32_font->metrics.tmMaxCharWidth;
840 font->font.height = w32_font->metrics.tmHeight
841 + w32_font->metrics.tmExternalLeading;
842 font->font.space_width = font->font.average_width
843 = w32_font->metrics.tmAveCharWidth;
845 font->font.vertical_centering = 0;
846 font->font.encoding_type = 0;
847 font->font.baseline_offset = 0;
848 font->font.relative_compose = 0;
849 font->font.default_ascent = w32_font->metrics.tmAscent;
850 font->font.font_encoder = NULL;
851 font->entity = font_entity;
852 font->pixel_size = size;
853 font->driver = &w32font_driver;
854 /* Use format cached during list, as the information we have access to
855 here is incomplete. */
856 extra = AREF (font_entity, FONT_EXTRA_INDEX);
857 if (CONSP (extra))
859 val = assq_no_quit (QCformat, extra);
860 if (CONSP (val))
861 font->format = XCDR (val);
862 else
863 font->format = Qunknown;
865 else
866 font->format = Qunknown;
868 font->file_name = NULL;
869 font->encoding_charset = -1;
870 font->repertory_charset = -1;
871 /* TODO: do we really want the minimum width here, which could be negative? */
872 font->min_width = font->font.space_width;
873 font->ascent = w32_font->metrics.tmAscent;
874 font->descent = w32_font->metrics.tmDescent;
875 font->scalable = w32_font->metrics.tmPitchAndFamily & TMPF_VECTOR;
877 /* Set global flag fonts_changed_p to non-zero if the font loaded
878 has a character with a smaller width than any other character
879 before, or if the font loaded has a smaller height than any other
880 font loaded before. If this happens, it will make a glyph matrix
881 reallocation necessary. */
883 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
884 dpyinfo->n_fonts++;
886 if (dpyinfo->n_fonts == 1)
888 dpyinfo->smallest_font_height = font->font.height;
889 dpyinfo->smallest_char_width = font->min_width;
891 else
893 if (dpyinfo->smallest_font_height > font->font.height)
895 dpyinfo->smallest_font_height = font->font.height;
896 fonts_changed_p |= 1;
898 if (dpyinfo->smallest_char_width > font->min_width)
900 dpyinfo->smallest_char_width = font->min_width;
901 fonts_changed_p |= 1;
906 return 1;
909 /* Callback function for EnumFontFamiliesEx.
910 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
911 static int CALLBACK
912 add_font_name_to_list (logical_font, physical_font, font_type, list_object)
913 ENUMLOGFONTEX *logical_font;
914 NEWTEXTMETRICEX *physical_font;
915 DWORD font_type;
916 LPARAM list_object;
918 Lisp_Object* list = (Lisp_Object *) list_object;
919 Lisp_Object family;
921 /* Skip vertical fonts (intended only for printing) */
922 if (logical_font->elfLogFont.lfFaceName[0] == '@')
923 return 1;
925 family = intern_downcase (logical_font->elfLogFont.lfFaceName,
926 strlen (logical_font->elfLogFont.lfFaceName));
927 if (! memq_no_quit (family, *list))
928 *list = Fcons (family, *list);
930 return 1;
933 /* Convert an enumerated Windows font to an Emacs font entity. */
934 static Lisp_Object
935 w32_enumfont_pattern_entity (frame, logical_font, physical_font,
936 font_type, requested_font, backend)
937 Lisp_Object frame;
938 ENUMLOGFONTEX *logical_font;
939 NEWTEXTMETRICEX *physical_font;
940 DWORD font_type;
941 LOGFONT *requested_font;
942 Lisp_Object backend;
944 Lisp_Object entity, tem;
945 LOGFONT *lf = (LOGFONT*) logical_font;
946 BYTE generic_type;
947 DWORD full_type = physical_font->ntmTm.ntmFlags;
949 entity = Fmake_vector (make_number (FONT_ENTITY_MAX), Qnil);
951 ASET (entity, FONT_TYPE_INDEX, backend);
952 ASET (entity, FONT_FRAME_INDEX, frame);
953 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
954 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
956 /* Foundry is difficult to get in readable form on Windows.
957 But Emacs crashes if it is not set, so set it to something more
958 generic. Thes values make xflds compatible with Emacs 22. */
959 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
960 tem = Qraster;
961 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
962 tem = Qoutline;
963 else
964 tem = Qunknown;
966 ASET (entity, FONT_FOUNDRY_INDEX, tem);
968 /* Save the generic family in the extra info, as it is likely to be
969 useful to users looking for a close match. */
970 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
971 if (generic_type == FF_DECORATIVE)
972 tem = Qdecorative;
973 else if (generic_type == FF_MODERN)
974 tem = Qmono;
975 else if (generic_type == FF_ROMAN)
976 tem = Qserif;
977 else if (generic_type == FF_SCRIPT)
978 tem = Qscript;
979 else if (generic_type == FF_SWISS)
980 tem = Qsans;
981 else
982 tem = null_string;
984 ASET (entity, FONT_ADSTYLE_INDEX, tem);
986 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
987 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_PROPORTIONAL));
988 else
989 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_MONO));
991 if (requested_font->lfQuality != DEFAULT_QUALITY)
993 font_put_extra (entity, QCantialias,
994 lispy_antialias_type (requested_font->lfQuality));
996 ASET (entity, FONT_FAMILY_INDEX,
997 intern_downcase (lf->lfFaceName, strlen (lf->lfFaceName)));
999 ASET (entity, FONT_WEIGHT_INDEX, make_number (lf->lfWeight));
1000 ASET (entity, FONT_SLANT_INDEX, make_number (lf->lfItalic ? 200 : 100));
1001 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
1002 to get it. */
1003 ASET (entity, FONT_WIDTH_INDEX, make_number (100));
1005 if (font_type & RASTER_FONTTYPE)
1006 ASET (entity, FONT_SIZE_INDEX, make_number (physical_font->ntmTm.tmHeight));
1007 else
1008 ASET (entity, FONT_SIZE_INDEX, make_number (0));
1010 /* Cache unicode codepoints covered by this font, as there is no other way
1011 of getting this information easily. */
1012 if (font_type & TRUETYPE_FONTTYPE)
1014 font_put_extra (entity, QCscript,
1015 font_supported_scripts (&physical_font->ntmFontSig));
1018 /* This information is not fully available when opening fonts, so
1019 save it here. Only Windows 2000 and later return information
1020 about opentype and type1 fonts, so need a fallback for detecting
1021 truetype so that this information is not any worse than we could
1022 have obtained later. */
1023 if (EQ (backend, Quniscribe) && (full_type & NTMFLAGS_OPENTYPE))
1024 tem = intern ("opentype");
1025 else if (font_type & TRUETYPE_FONTTYPE)
1026 tem = intern ("truetype");
1027 else if (full_type & NTM_PS_OPENTYPE)
1028 tem = intern ("postscript");
1029 else if (full_type & NTM_TYPE1)
1030 tem = intern ("type1");
1031 else if (font_type & RASTER_FONTTYPE)
1032 tem = intern ("w32bitmap");
1033 else
1034 tem = intern ("w32vector");
1036 font_put_extra (entity, QCformat, tem);
1038 return entity;
1042 /* Convert generic families to the family portion of lfPitchAndFamily. */
1043 BYTE
1044 w32_generic_family (Lisp_Object name)
1046 /* Generic families. */
1047 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1048 return FF_MODERN;
1049 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
1050 return FF_SWISS;
1051 else if (EQ (name, Qserif))
1052 return FF_ROMAN;
1053 else if (EQ (name, Qdecorative))
1054 return FF_DECORATIVE;
1055 else if (EQ (name, Qscript))
1056 return FF_SCRIPT;
1057 else
1058 return FF_DONTCARE;
1061 static int
1062 logfonts_match (font, pattern)
1063 LOGFONT *font, *pattern;
1065 /* Only check height for raster fonts. */
1066 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1067 && font->lfHeight != pattern->lfHeight)
1068 return 0;
1070 /* Have some flexibility with weights. */
1071 if (pattern->lfWeight
1072 && ((font->lfWeight < (pattern->lfWeight - 150))
1073 || font->lfWeight > (pattern->lfWeight + 150)))
1074 return 0;
1076 /* Charset and face should be OK. Italic has to be checked
1077 against the original spec, in case we don't have any preference. */
1078 return 1;
1081 static int
1082 font_matches_spec (type, font, spec, backend, logfont)
1083 DWORD type;
1084 NEWTEXTMETRICEX *font;
1085 Lisp_Object spec;
1086 Lisp_Object backend;
1087 LOGFONT *logfont;
1089 Lisp_Object extra, val;
1091 /* Check italic. Can't check logfonts, since it is a boolean field,
1092 so there is no difference between "non-italic" and "don't care". */
1093 val = AREF (spec, FONT_SLANT_INDEX);
1094 if (INTEGERP (val))
1096 int slant = XINT (val);
1097 if ((slant > 150 && !font->ntmTm.tmItalic)
1098 || (slant <= 150 && font->ntmTm.tmItalic))
1099 return 0;
1102 /* Check adstyle against generic family. */
1103 val = AREF (spec, FONT_ADSTYLE_INDEX);
1104 if (!NILP (val))
1106 BYTE family = w32_generic_family (val);
1107 if (family != FF_DONTCARE
1108 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1109 return 0;
1112 /* Check extra parameters. */
1113 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1114 CONSP (extra); extra = XCDR (extra))
1116 Lisp_Object extra_entry;
1117 extra_entry = XCAR (extra);
1118 if (CONSP (extra_entry))
1120 Lisp_Object key = XCAR (extra_entry);
1121 val = XCDR (extra_entry);
1122 if (EQ (key, QCspacing))
1124 int proportional;
1125 if (INTEGERP (val))
1127 int spacing = XINT (val);
1128 proportional = (spacing < FONT_SPACING_MONO);
1130 else if (EQ (val, Qp))
1131 proportional = 1;
1132 else if (EQ (val, Qc) || EQ (val, Qm))
1133 proportional = 0;
1134 else
1135 return 0; /* Bad font spec. */
1137 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1138 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1139 return 0;
1141 else if (EQ (key, QCscript) && SYMBOLP (val))
1143 /* Only truetype fonts will have information about what
1144 scripts they support. This probably means the user
1145 will have to force Emacs to use raster, postscript
1146 or atm fonts for non-ASCII text. */
1147 if (type & TRUETYPE_FONTTYPE)
1149 Lisp_Object support
1150 = font_supported_scripts (&font->ntmFontSig);
1151 if (! memq_no_quit (val, support))
1152 return 0;
1154 else
1156 /* Return specific matches, but play it safe. Fonts
1157 that cover more than their charset would suggest
1158 are likely to be truetype or opentype fonts,
1159 covered above. */
1160 if (EQ (val, Qlatin))
1162 /* Although every charset but symbol, thai and
1163 arabic contains the basic ASCII set of latin
1164 characters, Emacs expects much more. */
1165 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1166 return 0;
1168 else if (EQ (val, Qsymbol))
1170 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1171 return 0;
1173 else if (EQ (val, Qcyrillic))
1175 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1176 return 0;
1178 else if (EQ (val, Qgreek))
1180 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1181 return 0;
1183 else if (EQ (val, Qarabic))
1185 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1186 return 0;
1188 else if (EQ (val, Qhebrew))
1190 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1191 return 0;
1193 else if (EQ (val, Qthai))
1195 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1196 return 0;
1198 else if (EQ (val, Qkana))
1200 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1201 return 0;
1203 else if (EQ (val, Qbopomofo))
1205 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1206 return 0;
1208 else if (EQ (val, Qhangul))
1210 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1211 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1212 return 0;
1214 else if (EQ (val, Qhan))
1216 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1217 && font->ntmTm.tmCharSet != GB2312_CHARSET
1218 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1219 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1220 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1221 return 0;
1223 else
1224 /* Other scripts unlikely to be handled by non-truetype
1225 fonts. */
1226 return 0;
1229 else if (EQ (key, QCotf) && CONSP (val))
1231 /* OTF features only supported by the uniscribe backend. */
1232 if (EQ (backend, Quniscribe))
1234 if (!uniscribe_check_otf (logfont, val))
1235 return 0;
1237 else
1238 return 0;
1242 return 1;
1245 static int
1246 w32font_coverage_ok (coverage, charset)
1247 FONTSIGNATURE * coverage;
1248 BYTE charset;
1250 DWORD subrange1 = coverage->fsUsb[1];
1252 #define SUBRANGE1_HAN_MASK 0x08000000
1253 #define SUBRANGE1_HANGEUL_MASK 0x01000000
1254 #define SUBRANGE1_JAPANESE_MASK (0x00060000 | SUBRANGE1_HAN_MASK)
1256 if (charset == GB2312_CHARSET || charset == CHINESEBIG5_CHARSET)
1258 return (subrange1 & SUBRANGE1_HAN_MASK) == SUBRANGE1_HAN_MASK;
1260 else if (charset == SHIFTJIS_CHARSET)
1262 return (subrange1 & SUBRANGE1_JAPANESE_MASK) == SUBRANGE1_JAPANESE_MASK;
1264 else if (charset == HANGEUL_CHARSET)
1266 return (subrange1 & SUBRANGE1_HANGEUL_MASK) == SUBRANGE1_HANGEUL_MASK;
1269 return 1;
1272 /* Callback function for EnumFontFamiliesEx.
1273 * Checks if a font matches everything we are trying to check agaist,
1274 * and if so, adds it to a list. Both the data we are checking against
1275 * and the list to which the fonts are added are passed in via the
1276 * lparam argument, in the form of a font_callback_data struct. */
1277 static int CALLBACK
1278 add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1279 ENUMLOGFONTEX *logical_font;
1280 NEWTEXTMETRICEX *physical_font;
1281 DWORD font_type;
1282 LPARAM lParam;
1284 struct font_callback_data *match_data
1285 = (struct font_callback_data *) lParam;
1286 Lisp_Object backend = match_data->opentype_only ? Quniscribe : Qgdi;
1288 if ((!match_data->opentype_only
1289 || (physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
1290 || (font_type & TRUETYPE_FONTTYPE))
1291 && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1292 && font_matches_spec (font_type, physical_font,
1293 match_data->orig_font_spec, backend,
1294 &logical_font->elfLogFont)
1295 && w32font_coverage_ok (&physical_font->ntmFontSig,
1296 match_data->pattern.lfCharSet)
1297 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1298 We limit this to raster fonts, because the test can catch some
1299 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1300 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1301 therefore get through this test. Since full names can be prefixed
1302 by a foundry, we accept raster fonts if the font name is found
1303 anywhere within the full name. */
1304 && (logical_font->elfLogFont.lfOutPrecision != OUT_STRING_PRECIS
1305 || strstr (logical_font->elfFullName,
1306 logical_font->elfLogFont.lfFaceName)))
1308 Lisp_Object entity
1309 = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1310 physical_font, font_type,
1311 &match_data->pattern,
1312 backend);
1313 if (!NILP (entity))
1314 match_data->list = Fcons (entity, match_data->list);
1316 return 1;
1319 /* Callback function for EnumFontFamiliesEx.
1320 * Terminates the search once we have a match. */
1321 static int CALLBACK
1322 add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1323 ENUMLOGFONTEX *logical_font;
1324 NEWTEXTMETRICEX *physical_font;
1325 DWORD font_type;
1326 LPARAM lParam;
1328 struct font_callback_data *match_data
1329 = (struct font_callback_data *) lParam;
1330 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1332 /* If we have a font in the list, terminate the search. */
1333 return !NILP (match_data->list);
1336 /* Convert a Lisp font registry (symbol) to a windows charset. */
1337 static LONG
1338 registry_to_w32_charset (charset)
1339 Lisp_Object charset;
1341 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1342 || EQ (charset, Qunicode_sip))
1343 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1344 else if (EQ (charset, Qiso8859_1))
1345 return ANSI_CHARSET;
1346 else if (SYMBOLP (charset))
1347 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
1348 else if (STRINGP (charset))
1349 return x_to_w32_charset (SDATA (charset));
1350 else
1351 return DEFAULT_CHARSET;
1354 static Lisp_Object
1355 w32_registry (w32_charset, font_type)
1356 LONG w32_charset;
1357 DWORD font_type;
1359 /* If charset is defaulted, use ANSI (unicode for truetype fonts). */
1360 if (w32_charset == DEFAULT_CHARSET)
1361 w32_charset = ANSI_CHARSET;
1363 if (font_type == TRUETYPE_FONTTYPE && w32_charset == ANSI_CHARSET)
1364 return Qiso10646_1;
1365 else
1367 char * charset = w32_to_x_charset (w32_charset, NULL);
1368 return intern_downcase (charset, strlen(charset));
1372 /* Fill in all the available details of LOGFONT from FONT_SPEC. */
1373 static void
1374 fill_in_logfont (f, logfont, font_spec)
1375 FRAME_PTR f;
1376 LOGFONT *logfont;
1377 Lisp_Object font_spec;
1379 Lisp_Object tmp, extra;
1380 int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;
1382 extra = AREF (font_spec, FONT_EXTRA_INDEX);
1383 /* Allow user to override dpi settings. */
1384 if (CONSP (extra))
1386 tmp = assq_no_quit (QCdpi, extra);
1387 if (CONSP (tmp) && INTEGERP (XCDR (tmp)))
1389 dpi = XINT (XCDR (tmp));
1391 else if (CONSP (tmp) && FLOATP (XCDR (tmp)))
1393 dpi = (int) (XFLOAT_DATA (XCDR (tmp)) + 0.5);
1397 /* Height */
1398 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1399 if (INTEGERP (tmp))
1400 logfont->lfHeight = -1 * XINT (tmp);
1401 else if (FLOATP (tmp))
1402 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
1404 /* Escapement */
1406 /* Orientation */
1408 /* Weight */
1409 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1410 if (INTEGERP (tmp))
1411 logfont->lfWeight = XINT (tmp);
1413 /* Italic */
1414 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1415 if (INTEGERP (tmp))
1417 int slant = XINT (tmp);
1418 logfont->lfItalic = slant > 150 ? 1 : 0;
1421 /* Underline */
1423 /* Strikeout */
1425 /* Charset */
1426 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1427 if (! NILP (tmp))
1428 logfont->lfCharSet = registry_to_w32_charset (tmp);
1429 else
1430 logfont->lfCharSet = DEFAULT_CHARSET;
1432 /* Out Precision */
1434 /* Clip Precision */
1436 /* Quality */
1437 logfont->lfQuality = DEFAULT_QUALITY;
1439 /* Generic Family and Face Name */
1440 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1442 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
1443 if (! NILP (tmp))
1445 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1446 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1447 ; /* Font name was generic, don't fill in font name. */
1448 /* Font families are interned, but allow for strings also in case of
1449 user input. */
1450 else if (SYMBOLP (tmp))
1451 strncpy (logfont->lfFaceName, SDATA (SYMBOL_NAME (tmp)), LF_FACESIZE);
1452 else if (STRINGP (tmp))
1453 strncpy (logfont->lfFaceName, SDATA (tmp), LF_FACESIZE);
1456 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1457 if (!NILP (tmp))
1459 /* Override generic family. */
1460 BYTE family = w32_generic_family (tmp);
1461 if (family != FF_DONTCARE)
1462 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
1465 /* Process EXTRA info. */
1466 for ( ; CONSP (extra); extra = XCDR (extra))
1468 tmp = XCAR (extra);
1469 if (CONSP (tmp))
1471 Lisp_Object key, val;
1472 key = XCAR (tmp), val = XCDR (tmp);
1473 if (EQ (key, QCspacing))
1475 /* Set pitch based on the spacing property. */
1476 if (INTEGERP (val))
1478 int spacing = XINT (val);
1479 if (spacing < FONT_SPACING_MONO)
1480 logfont->lfPitchAndFamily
1481 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1482 else
1483 logfont->lfPitchAndFamily
1484 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1486 else if (EQ (val, Qp))
1487 logfont->lfPitchAndFamily
1488 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1489 else if (EQ (val, Qc) || EQ (val, Qm))
1490 logfont->lfPitchAndFamily
1491 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1493 /* Only use QCscript if charset is not provided, or is unicode
1494 and a single script is specified. This is rather crude,
1495 and is only used to narrow down the fonts returned where
1496 there is a definite match. Some scripts, such as latin, han,
1497 cjk-misc match multiple lfCharSet values, so we can't pre-filter
1498 them. */
1499 else if (EQ (key, QCscript)
1500 && logfont->lfCharSet == DEFAULT_CHARSET
1501 && SYMBOLP (val))
1503 if (EQ (val, Qgreek))
1504 logfont->lfCharSet = GREEK_CHARSET;
1505 else if (EQ (val, Qhangul))
1506 logfont->lfCharSet = HANGUL_CHARSET;
1507 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
1508 logfont->lfCharSet = SHIFTJIS_CHARSET;
1509 else if (EQ (val, Qbopomofo))
1510 logfont->lfCharSet = CHINESEBIG5_CHARSET;
1511 /* GB 18030 supports tibetan, yi, mongolian,
1512 fonts that support it should show up if we ask for
1513 GB2312 fonts. */
1514 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
1515 || EQ (val, Qmongolian))
1516 logfont->lfCharSet = GB2312_CHARSET;
1517 else if (EQ (val, Qhebrew))
1518 logfont->lfCharSet = HEBREW_CHARSET;
1519 else if (EQ (val, Qarabic))
1520 logfont->lfCharSet = ARABIC_CHARSET;
1521 else if (EQ (val, Qthai))
1522 logfont->lfCharSet = THAI_CHARSET;
1523 else if (EQ (val, Qsymbol))
1524 logfont->lfCharSet = SYMBOL_CHARSET;
1526 else if (EQ (key, QCantialias) && SYMBOLP (val))
1528 logfont->lfQuality = w32_antialias_type (val);
1534 static void
1535 list_all_matching_fonts (match_data)
1536 struct font_callback_data *match_data;
1538 HDC dc;
1539 Lisp_Object families = w32font_list_family (match_data->frame);
1540 struct frame *f = XFRAME (match_data->frame);
1542 dc = get_frame_dc (f);
1544 while (!NILP (families))
1546 /* TODO: Use the Unicode versions of the W32 APIs, so we can
1547 handle non-ASCII font names. */
1548 char *name;
1549 Lisp_Object family = CAR (families);
1550 families = CDR (families);
1551 if (NILP (family))
1552 continue;
1553 else if (STRINGP (family))
1554 name = SDATA (family);
1555 else
1556 name = SDATA (SYMBOL_NAME (family));
1558 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
1559 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
1561 EnumFontFamiliesEx (dc, &match_data->pattern,
1562 (FONTENUMPROC) add_font_entity_to_list,
1563 (LPARAM) match_data, 0);
1566 release_frame_dc (f, dc);
1569 static Lisp_Object
1570 lispy_antialias_type (type)
1571 BYTE type;
1573 Lisp_Object lispy;
1575 switch (type)
1577 case NONANTIALIASED_QUALITY:
1578 lispy = Qnone;
1579 break;
1580 case ANTIALIASED_QUALITY:
1581 lispy = Qstandard;
1582 break;
1583 case CLEARTYPE_QUALITY:
1584 lispy = Qsubpixel;
1585 break;
1586 case CLEARTYPE_NATURAL_QUALITY:
1587 lispy = Qnatural;
1588 break;
1589 default:
1590 lispy = Qnil;
1591 break;
1593 return lispy;
1596 /* Convert antialiasing symbols to lfQuality */
1597 static BYTE
1598 w32_antialias_type (type)
1599 Lisp_Object type;
1601 if (EQ (type, Qnone))
1602 return NONANTIALIASED_QUALITY;
1603 else if (EQ (type, Qstandard))
1604 return ANTIALIASED_QUALITY;
1605 else if (EQ (type, Qsubpixel))
1606 return CLEARTYPE_QUALITY;
1607 else if (EQ (type, Qnatural))
1608 return CLEARTYPE_NATURAL_QUALITY;
1609 else
1610 return DEFAULT_QUALITY;
1613 /* Return a list of all the scripts that the font supports. */
1614 static Lisp_Object
1615 font_supported_scripts (FONTSIGNATURE * sig)
1617 DWORD * subranges = sig->fsUsb;
1618 Lisp_Object supported = Qnil;
1620 /* Match a single subrange. SYM is set if bit N is set in subranges. */
1621 #define SUBRANGE(n,sym) \
1622 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
1623 supported = Fcons ((sym), supported)
1625 /* Match multiple subranges. SYM is set if any MASK bit is set in
1626 subranges[0 - 3]. */
1627 #define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
1628 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
1629 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
1630 supported = Fcons ((sym), supported)
1632 SUBRANGE (0, Qlatin);
1633 /* The following count as latin too, ASCII should be present in these fonts,
1634 so don't need to mark them separately. */
1635 /* 1: Latin-1 supplement, 2: Latin Extended A, 3: Latin Extended B. */
1636 SUBRANGE (4, Qphonetic);
1637 /* 5: Spacing and tone modifiers, 6: Combining Diacriticals. */
1638 SUBRANGE (7, Qgreek);
1639 SUBRANGE (8, Qcoptic);
1640 SUBRANGE (9, Qcyrillic);
1641 SUBRANGE (10, Qarmenian);
1642 SUBRANGE (11, Qhebrew);
1643 SUBRANGE (13, Qarabic);
1644 SUBRANGE (14, Qnko);
1645 SUBRANGE (15, Qdevanagari);
1646 SUBRANGE (16, Qbengali);
1647 SUBRANGE (17, Qgurmukhi);
1648 SUBRANGE (18, Qgujarati);
1649 SUBRANGE (19, Qoriya);
1650 SUBRANGE (20, Qtamil);
1651 SUBRANGE (21, Qtelugu);
1652 SUBRANGE (22, Qkannada);
1653 SUBRANGE (23, Qmalayalam);
1654 SUBRANGE (24, Qthai);
1655 SUBRANGE (25, Qlao);
1656 SUBRANGE (26, Qgeorgian);
1657 SUBRANGE (27, Qbalinese);
1658 /* 28: Hangul Jamo. */
1659 /* 29: Latin Extended, 30: Greek Extended, 31: Punctuation. */
1660 /* 32-47: Symbols (defined below). */
1661 SUBRANGE (48, Qcjk_misc);
1662 /* Match either 49: katakana or 50: hiragana for kana. */
1663 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
1664 SUBRANGE (51, Qbopomofo);
1665 /* 52: Compatibility Jamo */
1666 SUBRANGE (53, Qphags_pa);
1667 /* 54: Enclosed CJK letters and months, 55: CJK Compatibility. */
1668 SUBRANGE (56, Qhangul);
1669 /* 57: Surrogates. */
1670 SUBRANGE (58, Qphoenician);
1671 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
1672 SUBRANGE (59, Qideographic_description); /* Windows lumps this in. */
1673 SUBRANGE (59, Qkanbun); /* And this. */
1674 /* 60: Private use, 61: CJK strokes and compatibility. */
1675 /* 62: Alphabetic Presentation, 63: Arabic Presentation A. */
1676 /* 64: Combining half marks, 65: Vertical and CJK compatibility. */
1677 /* 66: Small forms, 67: Arabic Presentation B, 68: Half and Full width. */
1678 /* 69: Specials. */
1679 SUBRANGE (70, Qtibetan);
1680 SUBRANGE (71, Qsyriac);
1681 SUBRANGE (72, Qthaana);
1682 SUBRANGE (73, Qsinhala);
1683 SUBRANGE (74, Qmyanmar);
1684 SUBRANGE (75, Qethiopic);
1685 SUBRANGE (76, Qcherokee);
1686 SUBRANGE (77, Qcanadian_aboriginal);
1687 SUBRANGE (78, Qogham);
1688 SUBRANGE (79, Qrunic);
1689 SUBRANGE (80, Qkhmer);
1690 SUBRANGE (81, Qmongolian);
1691 SUBRANGE (82, Qbraille);
1692 SUBRANGE (83, Qyi);
1693 SUBRANGE (84, Qbuhid);
1694 SUBRANGE (84, Qhanunoo);
1695 SUBRANGE (84, Qtagalog);
1696 SUBRANGE (84, Qtagbanwa);
1697 SUBRANGE (85, Qold_italic);
1698 SUBRANGE (86, Qgothic);
1699 SUBRANGE (87, Qdeseret);
1700 SUBRANGE (88, Qbyzantine_musical_symbol);
1701 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
1702 SUBRANGE (89, Qmathematical);
1703 /* 90: Private use, 91: Variation selectors, 92: Tags. */
1704 SUBRANGE (93, Qlimbu);
1705 SUBRANGE (94, Qtai_le);
1706 /* 95: New Tai Le */
1707 SUBRANGE (90, Qbuginese);
1708 SUBRANGE (97, Qglagolitic);
1709 SUBRANGE (98, Qtifinagh);
1710 /* 99: Yijing Hexagrams. */
1711 SUBRANGE (100, Qsyloti_nagri);
1712 SUBRANGE (101, Qlinear_b);
1713 /* 102: Ancient Greek Numbers. */
1714 SUBRANGE (103, Qugaritic);
1715 SUBRANGE (104, Qold_persian);
1716 SUBRANGE (105, Qshavian);
1717 SUBRANGE (106, Qosmanya);
1718 SUBRANGE (107, Qcypriot);
1719 SUBRANGE (108, Qkharoshthi);
1720 /* 109: Tai Xuan Jing. */
1721 SUBRANGE (110, Qcuneiform);
1722 /* 111: Counting Rods. */
1724 /* There isn't really a main symbol range, so include symbol if any
1725 relevant range is set. */
1726 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
1728 /* Missing: Tai Viet (U+AA80) and Cham (U+AA00) . */
1729 #undef SUBRANGE
1730 #undef MASK_ANY
1732 return supported;
1735 /* Generate a full name for a Windows font.
1736 The full name is in fcname format, with weight, slant and antialiasing
1737 specified if they are not "normal". */
1738 static int
1739 w32font_full_name (font, font_obj, pixel_size, name, nbytes)
1740 LOGFONT * font;
1741 Lisp_Object font_obj;
1742 int pixel_size;
1743 char *name;
1744 int nbytes;
1746 int len, height, outline;
1747 char *p;
1748 Lisp_Object antialiasing, weight = Qnil;
1750 len = strlen (font->lfFaceName);
1752 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
1754 /* Represent size of scalable fonts by point size. But use pixelsize for
1755 raster fonts to indicate that they are exactly that size. */
1756 if (outline)
1757 len += 11; /* -SIZE */
1758 else
1759 len = strlen (font->lfFaceName) + 21;
1761 if (font->lfItalic)
1762 len += 7; /* :italic */
1764 if (font->lfWeight && font->lfWeight != FW_NORMAL)
1766 weight = font_symbolic_weight (font_obj);
1767 len += 8 + SBYTES (SYMBOL_NAME (weight)); /* :weight=NAME */
1770 antialiasing = lispy_antialias_type (font->lfQuality);
1771 if (! NILP (antialiasing))
1772 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
1774 /* Check that the buffer is big enough */
1775 if (len > nbytes)
1776 return -1;
1778 p = name;
1779 p += sprintf (p, "%s", font->lfFaceName);
1781 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
1783 if (height > 0)
1785 if (outline)
1787 float pointsize = height * 72.0 / one_w32_display_info.resy;
1788 /* Round to nearest half point. floor is used, since round is not
1789 supported in MS library. */
1790 pointsize = floor (pointsize * 2 + 0.5) / 2;
1791 p += sprintf (p, "-%1.1f", pointsize);
1793 else
1794 p += sprintf (p, ":pixelsize=%d", height);
1797 if (font->lfItalic)
1798 p += sprintf (p, ":italic");
1800 if (SYMBOLP (weight) && ! NILP (weight))
1801 p += sprintf (p, ":weight=%s", SDATA (SYMBOL_NAME (weight)));
1803 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
1804 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
1806 return (p - name);
1810 static void
1811 recompute_cached_metrics (dc, w32_font)
1812 HDC dc;
1813 struct w32font_info *w32_font;
1815 GLYPHMETRICS gm;
1816 MAT2 transform;
1817 unsigned int i;
1819 bzero (&transform, sizeof (transform));
1820 transform.eM11.value = 1;
1821 transform.eM22.value = 1;
1823 for (i = 0; i < 128; i++)
1825 struct font_metrics* char_metric = &w32_font->ascii_metrics[i];
1826 unsigned int options = GGO_METRICS;
1827 if (w32_font->glyph_idx)
1828 options |= GGO_GLYPH_INDEX;
1830 if (GetGlyphOutlineW (dc, i, options, &gm, 0, NULL, &transform)
1831 != GDI_ERROR)
1833 char_metric->lbearing = gm.gmptGlyphOrigin.x;
1834 char_metric->rbearing = gm.gmBlackBoxX + gm.gmptGlyphOrigin.x;
1835 char_metric->width = gm.gmCellIncX;
1837 else
1838 char_metric->width = 0;
1842 struct font_driver w32font_driver =
1844 0, /* Qgdi */
1845 w32font_get_cache,
1846 w32font_list,
1847 w32font_match,
1848 w32font_list_family,
1849 NULL, /* free_entity */
1850 w32font_open,
1851 w32font_close,
1852 NULL, /* prepare_face */
1853 NULL, /* done_face */
1854 w32font_has_char,
1855 w32font_encode_char,
1856 w32font_text_extents,
1857 w32font_draw,
1858 NULL, /* get_bitmap */
1859 NULL, /* free_bitmap */
1860 NULL, /* get_outline */
1861 NULL, /* free_outline */
1862 NULL, /* anchor_point */
1863 NULL, /* otf_capability */
1864 NULL, /* otf_drive */
1865 NULL, /* start_for_frame */
1866 NULL, /* end_for_frame */
1867 NULL /* shape */
1871 /* Initialize state that does not change between invocations. This is only
1872 called when Emacs is dumped. */
1873 void
1874 syms_of_w32font ()
1876 DEFSYM (Qgdi, "gdi");
1877 DEFSYM (Quniscribe, "uniscribe");
1878 DEFSYM (QCformat, ":format");
1880 /* Generic font families. */
1881 DEFSYM (Qmonospace, "monospace");
1882 DEFSYM (Qserif, "serif");
1883 DEFSYM (Qsansserif, "sansserif");
1884 DEFSYM (Qscript, "script");
1885 DEFSYM (Qdecorative, "decorative");
1886 /* Aliases. */
1887 DEFSYM (Qsans_serif, "sans_serif");
1888 DEFSYM (Qsans, "sans");
1889 DEFSYM (Qmono, "mono");
1891 /* Fake foundries. */
1892 DEFSYM (Qraster, "raster");
1893 DEFSYM (Qoutline, "outline");
1894 DEFSYM (Qunknown, "unknown");
1896 /* Antialiasing. */
1897 DEFSYM (Qstandard, "standard");
1898 DEFSYM (Qsubpixel, "subpixel");
1899 DEFSYM (Qnatural, "natural");
1901 /* Scripts */
1902 DEFSYM (Qlatin, "latin");
1903 DEFSYM (Qgreek, "greek");
1904 DEFSYM (Qcoptic, "coptic");
1905 DEFSYM (Qcyrillic, "cyrillic");
1906 DEFSYM (Qarmenian, "armenian");
1907 DEFSYM (Qhebrew, "hebrew");
1908 DEFSYM (Qarabic, "arabic");
1909 DEFSYM (Qsyriac, "syriac");
1910 DEFSYM (Qnko, "nko");
1911 DEFSYM (Qthaana, "thaana");
1912 DEFSYM (Qdevanagari, "devanagari");
1913 DEFSYM (Qbengali, "bengali");
1914 DEFSYM (Qgurmukhi, "gurmukhi");
1915 DEFSYM (Qgujarati, "gujarati");
1916 DEFSYM (Qoriya, "oriya");
1917 DEFSYM (Qtamil, "tamil");
1918 DEFSYM (Qtelugu, "telugu");
1919 DEFSYM (Qkannada, "kannada");
1920 DEFSYM (Qmalayalam, "malayalam");
1921 DEFSYM (Qsinhala, "sinhala");
1922 DEFSYM (Qthai, "thai");
1923 DEFSYM (Qlao, "lao");
1924 DEFSYM (Qtibetan, "tibetan");
1925 DEFSYM (Qmyanmar, "myanmar");
1926 DEFSYM (Qgeorgian, "georgian");
1927 DEFSYM (Qhangul, "hangul");
1928 DEFSYM (Qethiopic, "ethiopic");
1929 DEFSYM (Qcherokee, "cherokee");
1930 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
1931 DEFSYM (Qogham, "ogham");
1932 DEFSYM (Qrunic, "runic");
1933 DEFSYM (Qkhmer, "khmer");
1934 DEFSYM (Qmongolian, "mongolian");
1935 DEFSYM (Qsymbol, "symbol");
1936 DEFSYM (Qbraille, "braille");
1937 DEFSYM (Qhan, "han");
1938 DEFSYM (Qideographic_description, "ideographic-description");
1939 DEFSYM (Qcjk_misc, "cjk-misc");
1940 DEFSYM (Qkana, "kana");
1941 DEFSYM (Qbopomofo, "bopomofo");
1942 DEFSYM (Qkanbun, "kanbun");
1943 DEFSYM (Qyi, "yi");
1944 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
1945 DEFSYM (Qmusical_symbol, "musical-symbol");
1946 DEFSYM (Qmathematical, "mathematical");
1947 DEFSYM (Qphonetic, "phonetic");
1948 DEFSYM (Qbalinese, "balinese");
1949 DEFSYM (Qbuginese, "buginese");
1950 DEFSYM (Qbuhid, "buhid");
1951 DEFSYM (Qcuneiform, "cuneiform");
1952 DEFSYM (Qcypriot, "cypriot");
1953 DEFSYM (Qdeseret, "deseret");
1954 DEFSYM (Qglagolitic, "glagolitic");
1955 DEFSYM (Qgothic, "gothic");
1956 DEFSYM (Qhanunoo, "hanunoo");
1957 DEFSYM (Qkharoshthi, "kharoshthi");
1958 DEFSYM (Qlimbu, "limbu");
1959 DEFSYM (Qlinear_b, "linear_b");
1960 DEFSYM (Qold_italic, "old_italic");
1961 DEFSYM (Qold_persian, "old_persian");
1962 DEFSYM (Qosmanya, "osmanya");
1963 DEFSYM (Qphags_pa, "phags-pa");
1964 DEFSYM (Qphoenician, "phoenician");
1965 DEFSYM (Qshavian, "shavian");
1966 DEFSYM (Qsyloti_nagri, "syloti_nagri");
1967 DEFSYM (Qtagalog, "tagalog");
1968 DEFSYM (Qtagbanwa, "tagbanwa");
1969 DEFSYM (Qtai_le, "tai_le");
1970 DEFSYM (Qtifinagh, "tifinagh");
1971 DEFSYM (Qugaritic, "ugaritic");
1973 w32font_driver.type = Qgdi;
1974 register_font_driver (&w32font_driver, NULL);
1976 #endif /* USE_FONT_BACKEND */
1978 /* arch-tag: 65b8a3cd-46aa-4c0d-a1f3-99e75b9c07ee
1979 (do not change this comment) */