Make sure all backends support vc-BACKEND-root.
[emacs.git] / src / w32font.c
blob5776c95d0a7fcf5ce29dea55383ccf7c041a41ae
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 #include <config.h>
22 #include <windows.h>
24 #include "lisp.h"
25 #include "w32term.h"
26 #include "frame.h"
27 #include "dispextern.h"
28 #include "character.h"
29 #include "charset.h"
30 #include "fontset.h"
31 #include "font.h"
32 #include "w32font.h"
34 /* Cleartype available on Windows XP, cleartype_natural from XP SP1.
35 The latter does not try to fit cleartype smoothed fonts into the
36 same bounding box as the non-antialiased version of the font.
38 #ifndef CLEARTYPE_QUALITY
39 #define CLEARTYPE_QUALITY 5
40 #endif
41 #ifndef CLEARTYPE_NATURAL_QUALITY
42 #define CLEARTYPE_NATURAL_QUALITY 6
43 #endif
45 extern struct font_driver w32font_driver;
47 Lisp_Object Qgdi;
48 static Lisp_Object Qmonospace, Qsansserif, Qmono, Qsans, Qsans_serif;
49 static Lisp_Object Qserif, Qscript, Qdecorative;
50 static Lisp_Object Qraster, Qoutline, Qunknown;
52 /* antialiasing */
53 extern Lisp_Object QCantialias; /* defined in font.c */
54 extern Lisp_Object Qnone; /* reuse from w32fns.c */
55 static Lisp_Object Qstandard, Qsubpixel, Qnatural;
57 /* scripts */
58 static Lisp_Object Qlatin, Qgreek, Qcoptic, Qcyrillic, Qarmenian, Qhebrew;
59 static Lisp_Object Qarabic, Qsyriac, Qnko, Qthaana, Qdevanagari, Qbengali;
60 static Lisp_Object Qgurmukhi, Qgujarati, Qoriya, Qtamil, Qtelugu;
61 static Lisp_Object Qkannada, Qmalayalam, Qsinhala, Qthai, Qlao;
62 static Lisp_Object Qtibetan, Qmyanmar, Qgeorgian, Qhangul, Qethiopic;
63 static Lisp_Object Qcherokee, Qcanadian_aboriginal, Qogham, Qrunic;
64 static Lisp_Object Qkhmer, Qmongolian, Qsymbol, Qbraille, Qhan;
65 static Lisp_Object Qideographic_description, Qcjk_misc, Qkana, Qbopomofo;
66 static Lisp_Object Qkanbun, Qyi, Qbyzantine_musical_symbol;
67 static Lisp_Object Qmusical_symbol, Qmathematical;
69 /* Font spacing symbols - defined in font.c. */
70 extern Lisp_Object Qc, Qp, Qm;
72 static void fill_in_logfont P_ ((FRAME_PTR f, LOGFONT *logfont,
73 Lisp_Object font_spec));
75 static BYTE w32_antialias_type P_ ((Lisp_Object type));
76 static Lisp_Object lispy_antialias_type P_ ((BYTE type));
78 static Lisp_Object font_supported_scripts P_ ((FONTSIGNATURE * sig));
80 /* From old font code in w32fns.c */
81 char * w32_to_x_charset P_ ((int charset, char * matching));
83 static Lisp_Object w32_registry P_ ((LONG w32_charset));
85 /* EnumFontFamiliesEx callbacks. */
86 static int CALLBACK add_font_entity_to_list P_ ((ENUMLOGFONTEX *,
87 NEWTEXTMETRICEX *,
88 DWORD, LPARAM));
89 static int CALLBACK add_one_font_entity_to_list P_ ((ENUMLOGFONTEX *,
90 NEWTEXTMETRICEX *,
91 DWORD, LPARAM));
92 static int CALLBACK add_font_name_to_list P_ ((ENUMLOGFONTEX *,
93 NEWTEXTMETRICEX *,
94 DWORD, LPARAM));
96 /* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
97 of what we really want. */
98 struct font_callback_data
100 /* The logfont we are matching against. EnumFontFamiliesEx only matches
101 face name and charset, so we need to manually match everything else
102 in the callback function. */
103 LOGFONT pattern;
104 /* The original font spec or entity. */
105 Lisp_Object orig_font_spec;
106 /* The frame the font is being loaded on. */
107 Lisp_Object frame;
108 /* The list to add matches to. */
109 Lisp_Object list;
110 /* Whether to match only opentype fonts. */
111 int opentype_only;
114 /* Handles the problem that EnumFontFamiliesEx will not return all
115 style variations if the font name is not specified. */
116 static void list_all_matching_fonts P_ ((struct font_callback_data *match));
119 /* MingW headers only define this when _WIN32_WINNT >= 0x0500, but we
120 target older versions. */
121 #ifndef GGI_MARK_NONEXISTING_GLYPHS
122 #define GGI_MARK_NONEXISTING_GLYPHS 1
123 #endif
125 static int
126 memq_no_quit (elt, list)
127 Lisp_Object elt, list;
129 while (CONSP (list) && ! EQ (XCAR (list), elt))
130 list = XCDR (list);
131 return (CONSP (list));
134 /* w32 implementation of get_cache for font backend.
135 Return a cache of font-entities on FRAME. The cache must be a
136 cons whose cdr part is the actual cache area. */
137 Lisp_Object
138 w32font_get_cache (f)
139 FRAME_PTR f;
141 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
143 return (dpyinfo->name_list_element);
146 /* w32 implementation of list for font backend.
147 List fonts exactly matching with FONT_SPEC on FRAME. The value
148 is a vector of font-entities. This is the sole API that
149 allocates font-entities. */
150 static Lisp_Object
151 w32font_list (frame, font_spec)
152 Lisp_Object frame, font_spec;
154 return w32font_list_internal (frame, font_spec, 0);
157 /* w32 implementation of match for font backend.
158 Return a font entity most closely matching with FONT_SPEC on
159 FRAME. The closeness is detemined by the font backend, thus
160 `face-font-selection-order' is ignored here. */
161 static Lisp_Object
162 w32font_match (frame, font_spec)
163 Lisp_Object frame, font_spec;
165 return w32font_match_internal (frame, font_spec, 0);
168 /* w32 implementation of list_family for font backend.
169 List available families. The value is a list of family names
170 (symbols). */
171 static Lisp_Object
172 w32font_list_family (frame)
173 Lisp_Object frame;
175 Lisp_Object list = Qnil;
176 LOGFONT font_match_pattern;
177 HDC dc;
178 FRAME_PTR f = XFRAME (frame);
180 bzero (&font_match_pattern, sizeof (font_match_pattern));
182 dc = get_frame_dc (f);
184 EnumFontFamiliesEx (dc, &font_match_pattern,
185 (FONTENUMPROC) add_font_name_to_list,
186 (LPARAM) &list, 0);
187 release_frame_dc (f, dc);
189 return list;
192 /* w32 implementation of open for font backend.
193 Open a font specified by FONT_ENTITY on frame F.
194 If the font is scalable, open it with PIXEL_SIZE. */
195 static struct font *
196 w32font_open (f, font_entity, pixel_size)
197 FRAME_PTR f;
198 Lisp_Object font_entity;
199 int pixel_size;
201 struct w32font_info *w32_font = xmalloc (sizeof (struct w32font_info));
203 if (w32_font == NULL)
204 return NULL;
206 if (!w32font_open_internal (f, font_entity, pixel_size, w32_font))
208 xfree (w32_font);
209 return NULL;
212 return (struct font *) w32_font;
215 /* w32 implementation of close for font_backend.
216 Close FONT on frame F. */
217 void
218 w32font_close (f, font)
219 FRAME_PTR f;
220 struct font *font;
222 if (font->font.font)
224 W32FontStruct *old_w32_font = (W32FontStruct *)font->font.font;
225 DeleteObject (old_w32_font->hfont);
226 xfree (old_w32_font);
227 font->font.font = 0;
230 if (font->font.full_name && font->font.full_name != font->font.name)
231 xfree (font->font.full_name);
233 if (font->font.name)
234 xfree (font->font.name);
236 xfree (font);
239 /* w32 implementation of has_char for font backend.
240 Optional.
241 If FONT_ENTITY has a glyph for character C (Unicode code point),
242 return 1. If not, return 0. If a font must be opened to check
243 it, return -1. */
245 w32font_has_char (entity, c)
246 Lisp_Object entity;
247 int c;
249 Lisp_Object supported_scripts, extra, script;
250 DWORD mask;
252 extra = AREF (entity, FONT_EXTRA_INDEX);
253 if (!CONSP (extra))
254 return -1;
256 supported_scripts = assq_no_quit (QCscript, extra);
257 if (!CONSP (supported_scripts))
258 return -1;
260 supported_scripts = XCDR (supported_scripts);
262 script = CHAR_TABLE_REF (Vchar_script_table, c);
264 return (memq_no_quit (script, supported_scripts)) ? 1 : 0;
267 /* w32 implementation of encode_char for font backend.
268 Return a glyph code of FONT for characer C (Unicode code point).
269 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
270 unsigned
271 w32font_encode_char (font, c)
272 struct font *font;
273 int c;
275 /* Avoid unneccesary conversion - all the Win32 APIs will take a unicode
276 character. */
277 return c;
280 /* w32 implementation of text_extents for font backend.
281 Perform the size computation of glyphs of FONT and fillin members
282 of METRICS. The glyphs are specified by their glyph codes in
283 CODE (length NGLYPHS). Apparently metrics can be NULL, in this
284 case just return the overall width. */
286 w32font_text_extents (font, code, nglyphs, metrics)
287 struct font *font;
288 unsigned *code;
289 int nglyphs;
290 struct font_metrics *metrics;
292 int i;
293 HFONT old_font;
294 HDC dc;
295 struct frame * f;
296 int total_width = 0;
297 WORD *wcode = alloca(nglyphs * sizeof (WORD));
298 SIZE size;
300 /* TODO: Frames can come and go, and their fonts outlive them. So we
301 can't cache the frame in the font structure. Use selected_frame
302 until the API is updated to pass in a frame. */
303 f = XFRAME (selected_frame);
305 dc = get_frame_dc (f);
306 old_font = SelectObject (dc, ((W32FontStruct *)(font->font.font))->hfont);
308 if (metrics)
310 GLYPHMETRICS gm;
311 MAT2 transform;
313 /* Set transform to the identity matrix. */
314 bzero (&transform, sizeof (transform));
315 transform.eM11.value = 1;
316 transform.eM22.value = 1;
317 metrics->width = 0;
318 metrics->ascent = 0;
319 metrics->descent = 0;
320 metrics->lbearing = 0;
322 for (i = 0; i < nglyphs; i++)
324 if (*(code + i) < 128 && *(code + i) > 32)
326 /* Use cached metrics for ASCII. */
327 struct font_metrics *char_metric
328 = &((struct w32font_info *)font)->ascii_metrics[*(code+i)-32];
330 /* If we couldn't get metrics when caching, use fallback. */
331 if (char_metric->width == 0)
332 break;
334 metrics->lbearing = max (metrics->lbearing,
335 char_metric->lbearing - metrics->width);
336 metrics->rbearing = max (metrics->rbearing,
337 metrics->width + char_metric->rbearing);
338 metrics->width += char_metric->width;
339 metrics->ascent = max (metrics->ascent, char_metric->ascent);
340 metrics->descent = max (metrics->descent, char_metric->descent);
342 else if (GetGlyphOutlineW (dc, *(code + i), GGO_METRICS, &gm, 0,
343 NULL, &transform) != GDI_ERROR)
345 int new_val = metrics->width + gm.gmBlackBoxX
346 + gm.gmptGlyphOrigin.x;
347 metrics->rbearing = max (metrics->rbearing, new_val);
348 new_val = -gm.gmptGlyphOrigin.x - metrics->width;
349 metrics->lbearing = max (metrics->lbearing, new_val);
350 metrics->width += gm.gmCellIncX;
351 new_val = -gm.gmptGlyphOrigin.y;
352 metrics->ascent = max (metrics->ascent, new_val);
353 new_val = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
354 metrics->descent = max (metrics->descent, new_val);
356 else
358 /* Rely on an estimate based on the overall font metrics. */
359 break;
363 /* If we got through everything, return. */
364 if (i == nglyphs)
366 /* Restore state and release DC. */
367 SelectObject (dc, old_font);
368 release_frame_dc (f, dc);
370 return metrics->width;
374 for (i = 0; i < nglyphs; i++)
376 if (code[i] < 0x10000)
377 wcode[i] = code[i];
378 else
380 /* TODO: Convert to surrogate, reallocating array if needed */
381 wcode[i] = 0xffff;
385 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
387 total_width = size.cx;
390 if (!total_width)
392 RECT rect;
393 rect.top = 0; rect.bottom = font->font.height; rect.left = 0; rect.right = 1;
394 DrawTextW (dc, wcode, nglyphs, &rect,
395 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
396 total_width = rect.right;
399 if (metrics)
401 metrics->width = total_width;
402 metrics->ascent = font->ascent;
403 metrics->descent = font->descent;
404 metrics->lbearing = 0;
405 metrics->rbearing = total_width
406 + ((struct w32font_info *) font)->metrics.tmOverhang;
409 /* Restore state and release DC. */
410 SelectObject (dc, old_font);
411 release_frame_dc (f, dc);
413 return total_width;
416 /* w32 implementation of draw for font backend.
417 Optional.
418 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
419 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND
420 is nonzero, fill the background in advance. It is assured that
421 WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).
423 TODO: Currently this assumes that the colors and fonts are already
424 set in the DC. This seems to be true now, but maybe only due to
425 the old font code setting it up. It may be safer to resolve faces
426 and fonts in here and set them explicitly
430 w32font_draw (s, from, to, x, y, with_background)
431 struct glyph_string *s;
432 int from, to, x, y, with_background;
434 UINT options = 0;
435 HRGN orig_clip;
437 /* Save clip region for later restoration. */
438 GetClipRgn(s->hdc, orig_clip);
440 if (s->num_clips > 0)
442 HRGN new_clip = CreateRectRgnIndirect (s->clip);
444 if (s->num_clips > 1)
446 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
448 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
449 DeleteObject (clip2);
452 SelectClipRgn (s->hdc, new_clip);
453 DeleteObject (new_clip);
456 /* Using OPAQUE background mode can clear more background than expected
457 when Cleartype is used. Draw the background manually to avoid this. */
458 SetBkMode (s->hdc, TRANSPARENT);
459 if (with_background)
461 HBRUSH brush;
462 RECT rect;
463 struct font *font = (struct font *) s->face->font_info;
465 brush = CreateSolidBrush (s->gc->background);
466 rect.left = x;
467 rect.top = y - font->ascent;
468 rect.right = x + s->width;
469 rect.bottom = y + font->descent;
470 FillRect (s->hdc, &rect, brush);
471 DeleteObject (brush);
474 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
476 /* Restore clip region. */
477 if (s->num_clips > 0)
479 SelectClipRgn (s->hdc, orig_clip);
483 /* w32 implementation of free_entity for font backend.
484 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
485 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
486 static void
487 w32font_free_entity (Lisp_Object entity);
490 /* w32 implementation of prepare_face for font backend.
491 Optional (if FACE->extra is not used).
492 Prepare FACE for displaying characters by FONT on frame F by
493 storing some data in FACE->extra. If successful, return 0.
494 Otherwise, return -1.
495 static int
496 w32font_prepare_face (FRAME_PTR f, struct face *face);
498 /* w32 implementation of done_face for font backend.
499 Optional.
500 Done FACE for displaying characters by FACE->font on frame F.
501 static void
502 w32font_done_face (FRAME_PTR f, struct face *face); */
504 /* w32 implementation of get_bitmap for font backend.
505 Optional.
506 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
507 intended that this method is called from the other font-driver
508 for actual drawing.
509 static int
510 w32font_get_bitmap (struct font *font, unsigned code,
511 struct font_bitmap *bitmap, int bits_per_pixel);
513 /* w32 implementation of free_bitmap for font backend.
514 Optional.
515 Free bitmap data in BITMAP.
516 static void
517 w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
519 /* w32 implementation of get_outline for font backend.
520 Optional.
521 Return an outline data for glyph-code CODE of FONT. The format
522 of the outline data depends on the font-driver.
523 static void *
524 w32font_get_outline (struct font *font, unsigned code);
526 /* w32 implementation of free_outline for font backend.
527 Optional.
528 Free OUTLINE (that is obtained by the above method).
529 static void
530 w32font_free_outline (struct font *font, void *outline);
532 /* w32 implementation of anchor_point for font backend.
533 Optional.
534 Get coordinates of the INDEXth anchor point of the glyph whose
535 code is CODE. Store the coordinates in *X and *Y. Return 0 if
536 the operations was successfull. Otherwise return -1.
537 static int
538 w32font_anchor_point (struct font *font, unsigned code,
539 int index, int *x, int *y);
541 /* w32 implementation of otf_capability for font backend.
542 Optional.
543 Return a list describing which scripts/languages FONT
544 supports by which GSUB/GPOS features of OpenType tables.
545 static Lisp_Object
546 w32font_otf_capability (struct font *font);
548 /* w32 implementation of otf_drive for font backend.
549 Optional.
550 Apply FONT's OTF-FEATURES to the glyph string.
552 FEATURES specifies which OTF features to apply in this format:
553 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
554 See the documentation of `font-drive-otf' for the detail.
556 This method applies the specified features to the codes in the
557 elements of GSTRING-IN (between FROMth and TOth). The output
558 codes are stored in GSTRING-OUT at the IDXth element and the
559 following elements.
561 Return the number of output codes. If none of the features are
562 applicable to the input data, return 0. If GSTRING-OUT is too
563 short, return -1.
564 static int
565 w32font_otf_drive (struct font *font, Lisp_Object features,
566 Lisp_Object gstring_in, int from, int to,
567 Lisp_Object gstring_out, int idx,
568 int alternate_subst);
571 /* Internal implementation of w32font_list.
572 Additional parameter opentype_only restricts the returned fonts to
573 opentype fonts, which can be used with the Uniscribe backend. */
574 Lisp_Object
575 w32font_list_internal (frame, font_spec, opentype_only)
576 Lisp_Object frame, font_spec;
577 int opentype_only;
579 struct font_callback_data match_data;
580 HDC dc;
581 FRAME_PTR f = XFRAME (frame);
583 match_data.orig_font_spec = font_spec;
584 match_data.list = Qnil;
585 match_data.frame = frame;
587 bzero (&match_data.pattern, sizeof (LOGFONT));
588 fill_in_logfont (f, &match_data.pattern, font_spec);
590 match_data.opentype_only = opentype_only;
591 if (opentype_only)
592 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
594 if (match_data.pattern.lfFaceName[0] == '\0')
596 /* EnumFontFamiliesEx does not take other fields into account if
597 font name is blank, so need to use two passes. */
598 list_all_matching_fonts (&match_data);
600 else
602 dc = get_frame_dc (f);
604 EnumFontFamiliesEx (dc, &match_data.pattern,
605 (FONTENUMPROC) add_font_entity_to_list,
606 (LPARAM) &match_data, 0);
607 release_frame_dc (f, dc);
610 return NILP (match_data.list) ? null_vector : Fvconcat (1, &match_data.list);
613 /* Internal implementation of w32font_match.
614 Additional parameter opentype_only restricts the returned fonts to
615 opentype fonts, which can be used with the Uniscribe backend. */
616 Lisp_Object
617 w32font_match_internal (frame, font_spec, opentype_only)
618 Lisp_Object frame, font_spec;
619 int opentype_only;
621 struct font_callback_data match_data;
622 HDC dc;
623 FRAME_PTR f = XFRAME (frame);
625 match_data.orig_font_spec = font_spec;
626 match_data.frame = frame;
627 match_data.list = Qnil;
629 bzero (&match_data.pattern, sizeof (LOGFONT));
630 fill_in_logfont (f, &match_data.pattern, font_spec);
632 match_data.opentype_only = opentype_only;
633 if (opentype_only)
634 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
636 dc = get_frame_dc (f);
638 EnumFontFamiliesEx (dc, &match_data.pattern,
639 (FONTENUMPROC) add_one_font_entity_to_list,
640 (LPARAM) &match_data, 0);
641 release_frame_dc (f, dc);
643 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
647 w32font_open_internal (f, font_entity, pixel_size, w32_font)
648 FRAME_PTR f;
649 Lisp_Object font_entity;
650 int pixel_size;
651 struct w32font_info *w32_font;
653 int len, size;
654 LOGFONT logfont;
655 HDC dc;
656 HFONT hfont, old_font;
657 Lisp_Object val, extra;
658 /* For backwards compatibility. */
659 W32FontStruct *compat_w32_font;
661 struct font * font = (struct font *) w32_font;
662 if (!font)
663 return 0;
665 bzero (&logfont, sizeof (logfont));
666 fill_in_logfont (f, &logfont, font_entity);
668 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
669 if (!size)
670 size = pixel_size;
672 logfont.lfHeight = -size;
673 hfont = CreateFontIndirect (&logfont);
675 if (hfont == NULL)
676 return 0;
678 /* Get the metrics for this font. */
679 dc = get_frame_dc (f);
680 old_font = SelectObject (dc, hfont);
682 GetTextMetrics (dc, &w32_font->metrics);
684 /* Cache ASCII metrics. */
686 GLYPHMETRICS gm;
687 MAT2 transform;
688 int i;
690 bzero (&transform, sizeof (transform));
691 transform.eM11.value = 1;
692 transform.eM22.value = 1;
694 for (i = 0; i < 96; i++)
696 struct font_metrics* char_metric = &w32_font->ascii_metrics[i];
698 if (GetGlyphOutlineW (dc, i + 32, GGO_METRICS, &gm, 0,
699 NULL, &transform) != GDI_ERROR)
701 char_metric->lbearing = -gm.gmptGlyphOrigin.x;
702 char_metric->rbearing = gm.gmBlackBoxX + gm.gmptGlyphOrigin.x;
703 char_metric->width = gm.gmCellIncX;
704 char_metric->ascent = -gm.gmptGlyphOrigin.y;
705 char_metric->descent = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
707 else
708 char_metric->width = 0;
711 SelectObject (dc, old_font);
712 release_frame_dc (f, dc);
713 /* W32FontStruct - we should get rid of this, and use the w32font_info
714 struct for any W32 specific fields. font->font.font can then be hfont. */
715 font->font.font = xmalloc (sizeof (W32FontStruct));
716 compat_w32_font = (W32FontStruct *) font->font.font;
717 bzero (compat_w32_font, sizeof (W32FontStruct));
718 compat_w32_font->font_type = UNICODE_FONT;
719 /* Duplicate the text metrics. */
720 bcopy (&w32_font->metrics, &compat_w32_font->tm, sizeof (TEXTMETRIC));
721 compat_w32_font->hfont = hfont;
723 len = strlen (logfont.lfFaceName);
724 font->font.name = (char *) xmalloc (len + 1);
725 bcopy (logfont.lfFaceName, font->font.name, len);
726 font->font.name[len] = '\0';
729 char *name;
731 /* We don't know how much space we need for the full name, so start with
732 96 bytes and go up in steps of 32. */
733 len = 96;
734 name = xmalloc (len);
735 while (name && font_unparse_fcname (font_entity, pixel_size, name, len) < 0)
737 char *new = xrealloc (name, len += 32);
739 if (! new)
740 xfree (name);
741 name = new;
743 if (name)
744 font->font.full_name = name;
745 else
746 font->font.full_name = font->font.name;
748 font->font.charset = 0;
749 font->font.codepage = 0;
750 font->font.size = w32_font->metrics.tmMaxCharWidth;
751 font->font.height = w32_font->metrics.tmHeight
752 + w32_font->metrics.tmExternalLeading;
753 font->font.space_width = font->font.average_width
754 = w32_font->metrics.tmAveCharWidth;
756 font->font.vertical_centering = 0;
757 font->font.encoding_type = 0;
758 font->font.baseline_offset = 0;
759 font->font.relative_compose = 0;
760 font->font.default_ascent = w32_font->metrics.tmAscent;
761 font->font.font_encoder = NULL;
762 font->entity = font_entity;
763 font->pixel_size = size;
764 font->driver = &w32font_driver;
765 font->format = Qgdi;
766 font->file_name = NULL;
767 font->encoding_charset = -1;
768 font->repertory_charset = -1;
769 /* TODO: do we really want the minimum width here, which could be negative? */
770 font->min_width = font->font.space_width;
771 font->ascent = w32_font->metrics.tmAscent;
772 font->descent = w32_font->metrics.tmDescent;
773 font->scalable = w32_font->metrics.tmPitchAndFamily & TMPF_VECTOR;
775 /* Set global flag fonts_changed_p to non-zero if the font loaded
776 has a character with a smaller width than any other character
777 before, or if the font loaded has a smaller height than any other
778 font loaded before. If this happens, it will make a glyph matrix
779 reallocation necessary. */
781 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
782 dpyinfo->n_fonts++;
784 if (dpyinfo->n_fonts == 1)
786 dpyinfo->smallest_font_height = font->font.height;
787 dpyinfo->smallest_char_width = font->min_width;
789 else
791 if (dpyinfo->smallest_font_height > font->font.height)
793 dpyinfo->smallest_font_height = font->font.height;
794 fonts_changed_p |= 1;
796 if (dpyinfo->smallest_char_width > font->min_width)
798 dpyinfo->smallest_char_width = font->min_width;
799 fonts_changed_p |= 1;
804 return 1;
807 /* Callback function for EnumFontFamiliesEx.
808 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
809 static int CALLBACK
810 add_font_name_to_list (logical_font, physical_font, font_type, list_object)
811 ENUMLOGFONTEX *logical_font;
812 NEWTEXTMETRICEX *physical_font;
813 DWORD font_type;
814 LPARAM list_object;
816 Lisp_Object* list = (Lisp_Object *) list_object;
817 Lisp_Object family;
819 /* Skip vertical fonts (intended only for printing) */
820 if (logical_font->elfLogFont.lfFaceName[0] == '@')
821 return 1;
823 family = intern_downcase (logical_font->elfLogFont.lfFaceName,
824 strlen (logical_font->elfLogFont.lfFaceName));
825 if (! memq_no_quit (family, *list))
826 *list = Fcons (family, *list);
828 return 1;
831 /* Convert an enumerated Windows font to an Emacs font entity. */
832 static Lisp_Object
833 w32_enumfont_pattern_entity (frame, logical_font, physical_font,
834 font_type, requested_font)
835 Lisp_Object frame;
836 ENUMLOGFONTEX *logical_font;
837 NEWTEXTMETRICEX *physical_font;
838 DWORD font_type;
839 LOGFONT *requested_font;
841 Lisp_Object entity, tem;
842 LOGFONT *lf = (LOGFONT*) logical_font;
843 BYTE generic_type;
845 entity = Fmake_vector (make_number (FONT_ENTITY_MAX), Qnil);
847 ASET (entity, FONT_TYPE_INDEX, Qgdi);
848 ASET (entity, FONT_FRAME_INDEX, frame);
849 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet));
850 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
852 /* Foundry is difficult to get in readable form on Windows.
853 But Emacs crashes if it is not set, so set it to something more
854 generic. Thes values make xflds compatible with Emacs 22. */
855 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
856 tem = Qraster;
857 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
858 tem = Qoutline;
859 else
860 tem = Qunknown;
862 ASET (entity, FONT_FOUNDRY_INDEX, tem);
864 /* Save the generic family in the extra info, as it is likely to be
865 useful to users looking for a close match. */
866 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
867 if (generic_type == FF_DECORATIVE)
868 tem = Qdecorative;
869 else if (generic_type == FF_MODERN)
870 tem = Qmono;
871 else if (generic_type == FF_ROMAN)
872 tem = Qserif;
873 else if (generic_type == FF_SCRIPT)
874 tem = Qscript;
875 else if (generic_type == FF_SWISS)
876 tem = Qsans;
877 else
878 tem = null_string;
880 ASET (entity, FONT_ADSTYLE_INDEX, tem);
882 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
883 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_PROPORTIONAL));
884 else
885 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_MONO));
887 if (requested_font->lfQuality != DEFAULT_QUALITY)
889 font_put_extra (entity, QCantialias,
890 lispy_antialias_type (requested_font->lfQuality));
892 ASET (entity, FONT_FAMILY_INDEX,
893 intern_downcase (lf->lfFaceName, strlen (lf->lfFaceName)));
895 ASET (entity, FONT_WEIGHT_INDEX, make_number (lf->lfWeight));
896 ASET (entity, FONT_SLANT_INDEX, make_number (lf->lfItalic ? 200 : 100));
897 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
898 to get it. */
899 ASET (entity, FONT_WIDTH_INDEX, make_number (100));
901 if (font_type & RASTER_FONTTYPE)
902 ASET (entity, FONT_SIZE_INDEX, make_number (physical_font->ntmTm.tmHeight));
903 else
904 ASET (entity, FONT_SIZE_INDEX, make_number (0));
906 /* Cache unicode codepoints covered by this font, as there is no other way
907 of getting this information easily. */
908 if (font_type & TRUETYPE_FONTTYPE)
910 font_put_extra (entity, QCscript,
911 font_supported_scripts (&physical_font->ntmFontSig));
914 return entity;
918 /* Convert generic families to the family portion of lfPitchAndFamily. */
919 BYTE
920 w32_generic_family (Lisp_Object name)
922 /* Generic families. */
923 if (EQ (name, Qmonospace) || EQ (name, Qmono))
924 return FF_MODERN;
925 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
926 return FF_SWISS;
927 else if (EQ (name, Qserif))
928 return FF_ROMAN;
929 else if (EQ (name, Qdecorative))
930 return FF_DECORATIVE;
931 else if (EQ (name, Qscript))
932 return FF_SCRIPT;
933 else
934 return FF_DONTCARE;
937 static int
938 logfonts_match (font, pattern)
939 LOGFONT *font, *pattern;
941 /* Only check height for raster fonts. */
942 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
943 && font->lfHeight != pattern->lfHeight)
944 return 0;
946 /* Have some flexibility with weights. */
947 if (pattern->lfWeight
948 && ((font->lfWeight < (pattern->lfWeight - 150))
949 || font->lfWeight > (pattern->lfWeight + 150)))
950 return 0;
952 /* Charset and face should be OK. Italic has to be checked
953 against the original spec, in case we don't have any preference. */
954 return 1;
957 static int
958 font_matches_spec (type, font, spec)
959 DWORD type;
960 NEWTEXTMETRICEX *font;
961 Lisp_Object spec;
963 Lisp_Object extra, val;
965 /* Check italic. Can't check logfonts, since it is a boolean field,
966 so there is no difference between "non-italic" and "don't care". */
967 val = AREF (spec, FONT_SLANT_INDEX);
968 if (INTEGERP (val))
970 int slant = XINT (val);
971 if ((slant > 150 && !font->ntmTm.tmItalic)
972 || (slant <= 150 && font->ntmTm.tmItalic))
973 return 0;
976 /* Check adstyle against generic family. */
977 val = AREF (spec, FONT_ADSTYLE_INDEX);
978 if (!NILP (val))
980 BYTE family = w32_generic_family (val);
981 if (family != FF_DONTCARE
982 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
983 return 0;
986 /* Check extra parameters. */
987 for (extra = AREF (spec, FONT_EXTRA_INDEX);
988 CONSP (extra); extra = XCDR (extra))
990 Lisp_Object extra_entry;
991 extra_entry = XCAR (extra);
992 if (CONSP (extra_entry))
994 Lisp_Object key = XCAR (extra_entry);
995 val = XCDR (extra_entry);
996 if (EQ (key, QCspacing))
998 int proportional;
999 if (INTEGERP (val))
1001 int spacing = XINT (val);
1002 proportional = (spacing < FONT_SPACING_MONO);
1004 else if (EQ (val, Qp))
1005 proportional = 1;
1006 else if (EQ (val, Qc) || EQ (val, Qm))
1007 proportional = 0;
1008 else
1009 return 0; /* Bad font spec. */
1011 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1012 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1013 return 0;
1015 else if (EQ (key, QCscript) && SYMBOLP (val))
1017 /* Only truetype fonts will have information about what
1018 scripts they support. This probably means the user
1019 will have to force Emacs to use raster, postscript
1020 or atm fonts for non-ASCII text. */
1021 if (type & TRUETYPE_FONTTYPE)
1023 Lisp_Object support
1024 = font_supported_scripts (&font->ntmFontSig);
1025 if (! memq_no_quit (val, support))
1026 return 0;
1028 else
1030 /* Return specific matches, but play it safe. Fonts
1031 that cover more than their charset would suggest
1032 are likely to be truetype or opentype fonts,
1033 covered above. */
1034 if (EQ (val, Qlatin))
1036 /* Although every charset but symbol, thai and
1037 arabic contains the basic ASCII set of latin
1038 characters, Emacs expects much more. */
1039 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1040 return 0;
1042 else if (EQ (val, Qsymbol))
1044 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1045 return 0;
1047 else if (EQ (val, Qcyrillic))
1049 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1050 return 0;
1052 else if (EQ (val, Qgreek))
1054 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1055 return 0;
1057 else if (EQ (val, Qarabic))
1059 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1060 return 0;
1062 else if (EQ (val, Qhebrew))
1064 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1065 return 0;
1067 else if (EQ (val, Qthai))
1069 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1070 return 0;
1072 else if (EQ (val, Qkana))
1074 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1075 return 0;
1077 else if (EQ (val, Qbopomofo))
1079 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1080 return 0;
1082 else if (EQ (val, Qhangul))
1084 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1085 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1086 return 0;
1088 else if (EQ (val, Qhan))
1090 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1091 && font->ntmTm.tmCharSet != GB2312_CHARSET
1092 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1093 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1094 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1095 return 0;
1097 else
1098 /* Other scripts unlikely to be handled. */
1099 return 0;
1104 return 1;
1107 /* Callback function for EnumFontFamiliesEx.
1108 * Checks if a font matches everything we are trying to check agaist,
1109 * and if so, adds it to a list. Both the data we are checking against
1110 * and the list to which the fonts are added are passed in via the
1111 * lparam argument, in the form of a font_callback_data struct. */
1112 static int CALLBACK
1113 add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1114 ENUMLOGFONTEX *logical_font;
1115 NEWTEXTMETRICEX *physical_font;
1116 DWORD font_type;
1117 LPARAM lParam;
1119 struct font_callback_data *match_data
1120 = (struct font_callback_data *) lParam;
1122 if ((!match_data->opentype_only
1123 || (physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE))
1124 && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1125 && font_matches_spec (font_type, physical_font,
1126 match_data->orig_font_spec)
1127 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1128 We limit this to raster fonts, because the test can catch some
1129 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1130 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1131 therefore get through this test. Since full names can be prefixed
1132 by a foundry, we accept raster fonts if the font name is found
1133 anywhere within the full name. */
1134 && (logical_font->elfLogFont.lfOutPrecision != OUT_STRING_PRECIS
1135 || strstr (logical_font->elfFullName,
1136 logical_font->elfLogFont.lfFaceName)))
1138 Lisp_Object entity
1139 = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1140 physical_font, font_type,
1141 &match_data->pattern);
1142 if (!NILP (entity))
1143 match_data->list = Fcons (entity, match_data->list);
1145 return 1;
1148 /* Callback function for EnumFontFamiliesEx.
1149 * Terminates the search once we have a match. */
1150 static int CALLBACK
1151 add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1152 ENUMLOGFONTEX *logical_font;
1153 NEWTEXTMETRICEX *physical_font;
1154 DWORD font_type;
1155 LPARAM lParam;
1157 struct font_callback_data *match_data
1158 = (struct font_callback_data *) lParam;
1159 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1161 /* If we have a font in the list, terminate the search. */
1162 return !NILP (match_data->list);
1165 /* Convert a Lisp font registry (symbol) to a windows charset. */
1166 static LONG
1167 registry_to_w32_charset (charset)
1168 Lisp_Object charset;
1170 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1171 || EQ (charset, Qunicode_sip))
1172 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1173 else if (EQ (charset, Qiso8859_1))
1174 return ANSI_CHARSET;
1175 else if (SYMBOLP (charset))
1176 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
1177 else if (STRINGP (charset))
1178 return x_to_w32_charset (SDATA (charset));
1179 else
1180 return DEFAULT_CHARSET;
1183 static Lisp_Object
1184 w32_registry (w32_charset)
1185 LONG w32_charset;
1187 if (w32_charset == ANSI_CHARSET)
1188 return Qiso10646_1;
1189 else
1191 char * charset = w32_to_x_charset (w32_charset, NULL);
1192 return intern_downcase (charset, strlen(charset));
1196 /* Fill in all the available details of LOGFONT from FONT_SPEC. */
1197 static void
1198 fill_in_logfont (f, logfont, font_spec)
1199 FRAME_PTR f;
1200 LOGFONT *logfont;
1201 Lisp_Object font_spec;
1203 Lisp_Object tmp, extra;
1204 int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;
1206 extra = AREF (font_spec, FONT_EXTRA_INDEX);
1207 /* Allow user to override dpi settings. */
1208 if (CONSP (extra))
1210 tmp = assq_no_quit (QCdpi, extra);
1211 if (CONSP (tmp) && INTEGERP (XCDR (tmp)))
1213 dpi = XINT (XCDR (tmp));
1215 else if (CONSP (tmp) && FLOATP (XCDR (tmp)))
1217 dpi = (int) (XFLOAT_DATA (XCDR (tmp)) + 0.5);
1221 /* Height */
1222 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1223 if (INTEGERP (tmp))
1224 logfont->lfHeight = -1 * XINT (tmp);
1225 else if (FLOATP (tmp))
1226 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
1228 /* Escapement */
1230 /* Orientation */
1232 /* Weight */
1233 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1234 if (INTEGERP (tmp))
1235 logfont->lfWeight = XINT (tmp);
1237 /* Italic */
1238 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1239 if (INTEGERP (tmp))
1241 int slant = XINT (tmp);
1242 logfont->lfItalic = slant > 150 ? 1 : 0;
1245 /* Underline */
1247 /* Strikeout */
1249 /* Charset */
1250 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1251 if (! NILP (tmp))
1252 logfont->lfCharSet = registry_to_w32_charset (tmp);
1254 /* Out Precision */
1256 /* Clip Precision */
1258 /* Quality */
1259 logfont->lfQuality = DEFAULT_QUALITY;
1261 /* Generic Family and Face Name */
1262 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1264 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
1265 if (! NILP (tmp))
1267 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1268 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1269 ; /* Font name was generic, don't fill in font name. */
1270 /* Font families are interned, but allow for strings also in case of
1271 user input. */
1272 else if (SYMBOLP (tmp))
1273 strncpy (logfont->lfFaceName, SDATA (SYMBOL_NAME (tmp)), LF_FACESIZE);
1274 else if (STRINGP (tmp))
1275 strncpy (logfont->lfFaceName, SDATA (tmp), LF_FACESIZE);
1278 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1279 if (!NILP (tmp))
1281 /* Override generic family. */
1282 BYTE family = w32_generic_family (tmp);
1283 if (family != FF_DONTCARE)
1284 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
1287 /* Process EXTRA info. */
1288 for ( ; CONSP (extra); extra = XCDR (extra))
1290 tmp = XCAR (extra);
1291 if (CONSP (tmp))
1293 Lisp_Object key, val;
1294 key = XCAR (tmp), val = XCDR (tmp);
1295 if (EQ (key, QCspacing))
1297 /* Set pitch based on the spacing property. */
1298 if (INTEGERP (val))
1300 int spacing = XINT (val);
1301 if (spacing < FONT_SPACING_MONO)
1302 logfont->lfPitchAndFamily
1303 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1304 else
1305 logfont->lfPitchAndFamily
1306 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1308 else if (EQ (val, Qp))
1309 logfont->lfPitchAndFamily
1310 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1311 else if (EQ (val, Qc) || EQ (val, Qm))
1312 logfont->lfPitchAndFamily
1313 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1315 /* Only use QCscript if charset is not provided, or is unicode
1316 and a single script is specified. This is rather crude,
1317 and is only used to narrow down the fonts returned where
1318 there is a definite match. Some scripts, such as latin, han,
1319 cjk-misc match multiple lfCharSet values, so we can't pre-filter
1320 them. */
1321 else if (EQ (key, QCscript)
1322 && logfont->lfCharSet == DEFAULT_CHARSET
1323 && SYMBOLP (val))
1325 if (EQ (val, Qgreek))
1326 logfont->lfCharSet = GREEK_CHARSET;
1327 else if (EQ (val, Qhangul))
1328 logfont->lfCharSet = HANGUL_CHARSET;
1329 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
1330 logfont->lfCharSet = SHIFTJIS_CHARSET;
1331 else if (EQ (val, Qbopomofo))
1332 logfont->lfCharSet = CHINESEBIG5_CHARSET;
1333 /* GB 18030 supports tibetan, yi, mongolian,
1334 fonts that support it should show up if we ask for
1335 GB2312 fonts. */
1336 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
1337 || EQ (val, Qmongolian))
1338 logfont->lfCharSet = GB2312_CHARSET;
1339 else if (EQ (val, Qhebrew))
1340 logfont->lfCharSet = HEBREW_CHARSET;
1341 else if (EQ (val, Qarabic))
1342 logfont->lfCharSet = ARABIC_CHARSET;
1343 else if (EQ (val, Qthai))
1344 logfont->lfCharSet = THAI_CHARSET;
1345 else if (EQ (val, Qsymbol))
1346 logfont->lfCharSet = SYMBOL_CHARSET;
1348 else if (EQ (key, QCantialias) && SYMBOLP (val))
1350 logfont->lfQuality = w32_antialias_type (val);
1356 static void
1357 list_all_matching_fonts (match_data)
1358 struct font_callback_data *match_data;
1360 HDC dc;
1361 Lisp_Object families = w32font_list_family (match_data->frame);
1362 struct frame *f = XFRAME (match_data->frame);
1364 dc = get_frame_dc (f);
1366 while (!NILP (families))
1368 /* TODO: Use the Unicode versions of the W32 APIs, so we can
1369 handle non-ASCII font names. */
1370 char *name;
1371 Lisp_Object family = CAR (families);
1372 families = CDR (families);
1373 if (NILP (family))
1374 continue;
1375 else if (STRINGP (family))
1376 name = SDATA (family);
1377 else
1378 name = SDATA (SYMBOL_NAME (family));
1380 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
1381 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
1383 EnumFontFamiliesEx (dc, &match_data->pattern,
1384 (FONTENUMPROC) add_font_entity_to_list,
1385 (LPARAM) match_data, 0);
1388 release_frame_dc (f, dc);
1391 static Lisp_Object
1392 lispy_antialias_type (type)
1393 BYTE type;
1395 Lisp_Object lispy;
1397 switch (type)
1399 case NONANTIALIASED_QUALITY:
1400 lispy = Qnone;
1401 break;
1402 case ANTIALIASED_QUALITY:
1403 lispy = Qstandard;
1404 break;
1405 case CLEARTYPE_QUALITY:
1406 lispy = Qsubpixel;
1407 break;
1408 case CLEARTYPE_NATURAL_QUALITY:
1409 lispy = Qnatural;
1410 break;
1411 default:
1412 lispy = Qnil;
1413 break;
1415 return lispy;
1418 /* Convert antialiasing symbols to lfQuality */
1419 static BYTE
1420 w32_antialias_type (type)
1421 Lisp_Object type;
1423 if (EQ (type, Qnone))
1424 return NONANTIALIASED_QUALITY;
1425 else if (EQ (type, Qstandard))
1426 return ANTIALIASED_QUALITY;
1427 else if (EQ (type, Qsubpixel))
1428 return CLEARTYPE_QUALITY;
1429 else if (EQ (type, Qnatural))
1430 return CLEARTYPE_NATURAL_QUALITY;
1431 else
1432 return DEFAULT_QUALITY;
1435 /* Return a list of all the scripts that the font supports. */
1436 static Lisp_Object
1437 font_supported_scripts (FONTSIGNATURE * sig)
1439 DWORD * subranges = sig->fsUsb;
1440 Lisp_Object supported = Qnil;
1442 /* Match a single subrange. SYM is set if bit N is set in subranges. */
1443 #define SUBRANGE(n,sym) \
1444 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
1445 supported = Fcons ((sym), supported)
1447 /* Match multiple subranges. SYM is set if any MASK bit is set in
1448 subranges[0 - 3]. */
1449 #define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
1450 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
1451 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
1452 supported = Fcons ((sym), supported)
1454 SUBRANGE (0, Qlatin); /* There are many others... */
1456 SUBRANGE (7, Qgreek);
1457 SUBRANGE (8, Qcoptic);
1458 SUBRANGE (9, Qcyrillic);
1459 SUBRANGE (10, Qarmenian);
1460 SUBRANGE (11, Qhebrew);
1461 SUBRANGE (13, Qarabic);
1462 SUBRANGE (14, Qnko);
1463 SUBRANGE (15, Qdevanagari);
1464 SUBRANGE (16, Qbengali);
1465 SUBRANGE (17, Qgurmukhi);
1466 SUBRANGE (18, Qgujarati);
1467 SUBRANGE (19, Qoriya);
1468 SUBRANGE (20, Qtamil);
1469 SUBRANGE (21, Qtelugu);
1470 SUBRANGE (22, Qkannada);
1471 SUBRANGE (23, Qmalayalam);
1472 SUBRANGE (24, Qthai);
1473 SUBRANGE (25, Qlao);
1474 SUBRANGE (26, Qgeorgian);
1476 SUBRANGE (48, Qcjk_misc);
1477 SUBRANGE (51, Qbopomofo);
1478 SUBRANGE (54, Qkanbun); /* Is this right? */
1479 SUBRANGE (56, Qhangul);
1481 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
1482 SUBRANGE (59, Qideographic_description); /* Windows lumps this in */
1484 SUBRANGE (70, Qtibetan);
1485 SUBRANGE (71, Qsyriac);
1486 SUBRANGE (72, Qthaana);
1487 SUBRANGE (73, Qsinhala);
1488 SUBRANGE (74, Qmyanmar);
1489 SUBRANGE (75, Qethiopic);
1490 SUBRANGE (76, Qcherokee);
1491 SUBRANGE (77, Qcanadian_aboriginal);
1492 SUBRANGE (78, Qogham);
1493 SUBRANGE (79, Qrunic);
1494 SUBRANGE (80, Qkhmer);
1495 SUBRANGE (81, Qmongolian);
1496 SUBRANGE (82, Qbraille);
1497 SUBRANGE (83, Qyi);
1499 SUBRANGE (88, Qbyzantine_musical_symbol);
1500 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
1502 SUBRANGE (89, Qmathematical);
1504 /* Match either katakana or hiragana for kana. */
1505 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
1507 /* There isn't really a main symbol range, so include symbol if any
1508 relevant range is set. */
1509 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
1511 #undef SUBRANGE
1512 #undef MASK_ANY
1514 return supported;
1518 struct font_driver w32font_driver =
1520 0, /* Qgdi */
1521 w32font_get_cache,
1522 w32font_list,
1523 w32font_match,
1524 w32font_list_family,
1525 NULL, /* free_entity */
1526 w32font_open,
1527 w32font_close,
1528 NULL, /* prepare_face */
1529 NULL, /* done_face */
1530 w32font_has_char,
1531 w32font_encode_char,
1532 w32font_text_extents,
1533 w32font_draw,
1534 NULL, /* get_bitmap */
1535 NULL, /* free_bitmap */
1536 NULL, /* get_outline */
1537 NULL, /* free_outline */
1538 NULL, /* anchor_point */
1539 NULL, /* otf_capability */
1540 NULL, /* otf_drive */
1541 NULL, /* start_for_frame */
1542 NULL, /* end_for_frame */
1543 NULL /* shape */
1547 /* Initialize state that does not change between invocations. This is only
1548 called when Emacs is dumped. */
1549 void
1550 syms_of_w32font ()
1552 DEFSYM (Qgdi, "gdi");
1554 /* Generic font families. */
1555 DEFSYM (Qmonospace, "monospace");
1556 DEFSYM (Qserif, "serif");
1557 DEFSYM (Qsansserif, "sansserif");
1558 DEFSYM (Qscript, "script");
1559 DEFSYM (Qdecorative, "decorative");
1560 /* Aliases. */
1561 DEFSYM (Qsans_serif, "sans_serif");
1562 DEFSYM (Qsans, "sans");
1563 DEFSYM (Qmono, "mono");
1565 /* Fake foundries. */
1566 DEFSYM (Qraster, "raster");
1567 DEFSYM (Qoutline, "outline");
1568 DEFSYM (Qunknown, "unknown");
1570 /* Antialiasing. */
1571 DEFSYM (Qstandard, "standard");
1572 DEFSYM (Qsubpixel, "subpixel");
1573 DEFSYM (Qnatural, "natural");
1575 /* Scripts */
1576 DEFSYM (Qlatin, "latin");
1577 DEFSYM (Qgreek, "greek");
1578 DEFSYM (Qcoptic, "coptic");
1579 DEFSYM (Qcyrillic, "cyrillic");
1580 DEFSYM (Qarmenian, "armenian");
1581 DEFSYM (Qhebrew, "hebrew");
1582 DEFSYM (Qarabic, "arabic");
1583 DEFSYM (Qsyriac, "syriac");
1584 DEFSYM (Qnko, "nko");
1585 DEFSYM (Qthaana, "thaana");
1586 DEFSYM (Qdevanagari, "devanagari");
1587 DEFSYM (Qbengali, "bengali");
1588 DEFSYM (Qgurmukhi, "gurmukhi");
1589 DEFSYM (Qgujarati, "gujarati");
1590 DEFSYM (Qoriya, "oriya");
1591 DEFSYM (Qtamil, "tamil");
1592 DEFSYM (Qtelugu, "telugu");
1593 DEFSYM (Qkannada, "kannada");
1594 DEFSYM (Qmalayalam, "malayalam");
1595 DEFSYM (Qsinhala, "sinhala");
1596 DEFSYM (Qthai, "thai");
1597 DEFSYM (Qlao, "lao");
1598 DEFSYM (Qtibetan, "tibetan");
1599 DEFSYM (Qmyanmar, "myanmar");
1600 DEFSYM (Qgeorgian, "georgian");
1601 DEFSYM (Qhangul, "hangul");
1602 DEFSYM (Qethiopic, "ethiopic");
1603 DEFSYM (Qcherokee, "cherokee");
1604 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
1605 DEFSYM (Qogham, "ogham");
1606 DEFSYM (Qrunic, "runic");
1607 DEFSYM (Qkhmer, "khmer");
1608 DEFSYM (Qmongolian, "mongolian");
1609 DEFSYM (Qsymbol, "symbol");
1610 DEFSYM (Qbraille, "braille");
1611 DEFSYM (Qhan, "han");
1612 DEFSYM (Qideographic_description, "ideographic-description");
1613 DEFSYM (Qcjk_misc, "cjk-misc");
1614 DEFSYM (Qkana, "kana");
1615 DEFSYM (Qbopomofo, "bopomofo");
1616 DEFSYM (Qkanbun, "kanbun");
1617 DEFSYM (Qyi, "yi");
1618 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
1619 DEFSYM (Qmusical_symbol, "musical-symbol");
1620 DEFSYM (Qmathematical, "mathematical");
1622 w32font_driver.type = Qgdi;
1623 register_font_driver (&w32font_driver, NULL);
1626 /* arch-tag: 65b8a3cd-46aa-4c0d-a1f3-99e75b9c07ee
1627 (do not change this comment) */