]> code.delx.au - gnu-emacs/blob - src/w32font.c
Wrap in USE_FONT_BACKEND conditional
[gnu-emacs] / src / w32font.c
1 /* Font backend for the Microsoft W32 API.
2 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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. */
20
21 #ifdef USE_FONT_BACKEND
22
23 #include <config.h>
24 #include <windows.h>
25 #include <math.h>
26
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"
36
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.
40 */
41 #ifndef CLEARTYPE_QUALITY
42 #define CLEARTYPE_QUALITY 5
43 #endif
44 #ifndef CLEARTYPE_NATURAL_QUALITY
45 #define CLEARTYPE_NATURAL_QUALITY 6
46 #endif
47
48 extern struct font_driver w32font_driver;
49
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;
56
57 /* antialiasing */
58 extern Lisp_Object QCantialias; /* defined in font.c */
59 extern Lisp_Object Qnone; /* reuse from w32fns.c */
60 static Lisp_Object Qstandard, Qsubpixel, Qnatural;
61
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
74 /* Font spacing symbols - defined in font.c. */
75 extern Lisp_Object Qc, Qp, Qm;
76
77 static void fill_in_logfont P_ ((FRAME_PTR f, LOGFONT *logfont,
78 Lisp_Object font_spec));
79
80 static BYTE w32_antialias_type P_ ((Lisp_Object type));
81 static Lisp_Object lispy_antialias_type P_ ((BYTE type));
82
83 static Lisp_Object font_supported_scripts P_ ((FONTSIGNATURE * sig));
84 static int w32font_full_name P_ ((LOGFONT * font, Lisp_Object font_obj,
85 int pixel_size, char *name, int nbytes));
86 static void recompute_cached_metrics P_ ((HDC dc, struct w32font_info * font));
87
88 static Lisp_Object w32_registry P_ ((LONG w32_charset));
89
90 /* EnumFontFamiliesEx callbacks. */
91 static int CALLBACK add_font_entity_to_list P_ ((ENUMLOGFONTEX *,
92 NEWTEXTMETRICEX *,
93 DWORD, LPARAM));
94 static int CALLBACK add_one_font_entity_to_list P_ ((ENUMLOGFONTEX *,
95 NEWTEXTMETRICEX *,
96 DWORD, LPARAM));
97 static int CALLBACK add_font_name_to_list P_ ((ENUMLOGFONTEX *,
98 NEWTEXTMETRICEX *,
99 DWORD, LPARAM));
100
101 /* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
102 of what we really want. */
103 struct font_callback_data
104 {
105 /* The logfont we are matching against. EnumFontFamiliesEx only matches
106 face name and charset, so we need to manually match everything else
107 in the callback function. */
108 LOGFONT pattern;
109 /* The original font spec or entity. */
110 Lisp_Object orig_font_spec;
111 /* The frame the font is being loaded on. */
112 Lisp_Object frame;
113 /* The list to add matches to. */
114 Lisp_Object list;
115 /* Whether to match only opentype fonts. */
116 int opentype_only;
117 };
118
119 /* Handles the problem that EnumFontFamiliesEx will not return all
120 style variations if the font name is not specified. */
121 static void list_all_matching_fonts P_ ((struct font_callback_data *match));
122
123 /* From old font code in w32fns.c */
124 char * w32_to_x_charset P_ ((int charset, char * matching));
125
126
127 static int
128 memq_no_quit (elt, list)
129 Lisp_Object elt, list;
130 {
131 while (CONSP (list) && ! EQ (XCAR (list), elt))
132 list = XCDR (list);
133 return (CONSP (list));
134 }
135
136 /* w32 implementation of get_cache for font backend.
137 Return a cache of font-entities on FRAME. The cache must be a
138 cons whose cdr part is the actual cache area. */
139 Lisp_Object
140 w32font_get_cache (f)
141 FRAME_PTR f;
142 {
143 struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
144
145 return (dpyinfo->name_list_element);
146 }
147
148 /* w32 implementation of list for font backend.
149 List fonts exactly matching with FONT_SPEC on FRAME. The value
150 is a vector of font-entities. This is the sole API that
151 allocates font-entities. */
152 static Lisp_Object
153 w32font_list (frame, font_spec)
154 Lisp_Object frame, font_spec;
155 {
156 return w32font_list_internal (frame, font_spec, 0);
157 }
158
159 /* w32 implementation of match for font backend.
160 Return a font entity most closely matching with FONT_SPEC on
161 FRAME. The closeness is detemined by the font backend, thus
162 `face-font-selection-order' is ignored here. */
163 static Lisp_Object
164 w32font_match (frame, font_spec)
165 Lisp_Object frame, font_spec;
166 {
167 return w32font_match_internal (frame, font_spec, 0);
168 }
169
170 /* w32 implementation of list_family for font backend.
171 List available families. The value is a list of family names
172 (symbols). */
173 static Lisp_Object
174 w32font_list_family (frame)
175 Lisp_Object frame;
176 {
177 Lisp_Object list = Qnil;
178 LOGFONT font_match_pattern;
179 HDC dc;
180 FRAME_PTR f = XFRAME (frame);
181
182 bzero (&font_match_pattern, sizeof (font_match_pattern));
183
184 dc = get_frame_dc (f);
185
186 EnumFontFamiliesEx (dc, &font_match_pattern,
187 (FONTENUMPROC) add_font_name_to_list,
188 (LPARAM) &list, 0);
189 release_frame_dc (f, dc);
190
191 return list;
192 }
193
194 /* w32 implementation of open for font backend.
195 Open a font specified by FONT_ENTITY on frame F.
196 If the font is scalable, open it with PIXEL_SIZE. */
197 static struct font *
198 w32font_open (f, font_entity, pixel_size)
199 FRAME_PTR f;
200 Lisp_Object font_entity;
201 int pixel_size;
202 {
203 struct w32font_info *w32_font = xmalloc (sizeof (struct w32font_info));
204
205 if (w32_font == NULL)
206 return NULL;
207
208 if (!w32font_open_internal (f, font_entity, pixel_size, w32_font))
209 {
210 xfree (w32_font);
211 return NULL;
212 }
213
214 return (struct font *) w32_font;
215 }
216
217 /* w32 implementation of close for font_backend.
218 Close FONT on frame F. */
219 void
220 w32font_close (f, font)
221 FRAME_PTR f;
222 struct font *font;
223 {
224 if (font->font.font)
225 {
226 W32FontStruct *old_w32_font = (W32FontStruct *)font->font.font;
227 DeleteObject (old_w32_font->hfont);
228 xfree (old_w32_font);
229 font->font.font = 0;
230 }
231
232 if (font->font.full_name && font->font.full_name != font->font.name)
233 xfree (font->font.full_name);
234
235 if (font->font.name)
236 xfree (font->font.name);
237
238 xfree (font);
239 }
240
241 /* w32 implementation of has_char for font backend.
242 Optional.
243 If FONT_ENTITY has a glyph for character C (Unicode code point),
244 return 1. If not, return 0. If a font must be opened to check
245 it, return -1. */
246 int
247 w32font_has_char (entity, c)
248 Lisp_Object entity;
249 int c;
250 {
251 Lisp_Object supported_scripts, extra, script;
252 DWORD mask;
253
254 extra = AREF (entity, FONT_EXTRA_INDEX);
255 if (!CONSP (extra))
256 return -1;
257
258 supported_scripts = assq_no_quit (QCscript, extra);
259 if (!CONSP (supported_scripts))
260 return -1;
261
262 supported_scripts = XCDR (supported_scripts);
263
264 script = CHAR_TABLE_REF (Vchar_script_table, c);
265
266 return (memq_no_quit (script, supported_scripts)) ? -1 : 0;
267 }
268
269 /* w32 implementation of encode_char for font backend.
270 Return a glyph code of FONT for characer C (Unicode code point).
271 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
272 static unsigned
273 w32font_encode_char (font, c)
274 struct font *font;
275 int c;
276 {
277 struct frame *f;
278 HDC dc;
279 HFONT old_font;
280 DWORD retval;
281 GCP_RESULTSW result;
282 wchar_t in[2];
283 wchar_t out[2];
284 int len;
285 struct w32font_info *w32_font = (struct w32font_info *) font;
286
287 /* If glyph indexing is not working for this font, just return the
288 unicode code-point. */
289 if (!w32_font->glyph_idx)
290 return c;
291
292 if (c > 0xFFFF)
293 {
294 /* TODO: Encode as surrogate pair and lookup the glyph. */
295 return FONT_INVALID_CODE;
296 }
297 else
298 {
299 in[0] = (wchar_t) c;
300 len = 1;
301 }
302
303 bzero (&result, sizeof (result));
304 result.lStructSize = sizeof (result);
305 result.lpGlyphs = out;
306 result.nGlyphs = 2;
307
308 f = XFRAME (selected_frame);
309
310 dc = get_frame_dc (f);
311 old_font = SelectObject (dc, ((W32FontStruct *) (font->font.font))->hfont);
312
313 retval = GetCharacterPlacementW (dc, in, len, 0, &result, 0);
314
315 SelectObject (dc, old_font);
316 release_frame_dc (f, dc);
317
318 if (retval)
319 {
320 if (result.nGlyphs != 1 || !result.lpGlyphs[0])
321 return FONT_INVALID_CODE;
322 return result.lpGlyphs[0];
323 }
324 else
325 {
326 int i;
327 /* Mark this font as not supporting glyph indices. This can happen
328 on Windows9x, and maybe with non-Truetype fonts on NT etc. */
329 w32_font->glyph_idx = 0;
330 recompute_cached_metrics (dc, w32_font);
331
332 return c;
333 }
334 }
335
336 /* w32 implementation of text_extents for font backend.
337 Perform the size computation of glyphs of FONT and fillin members
338 of METRICS. The glyphs are specified by their glyph codes in
339 CODE (length NGLYPHS). Apparently metrics can be NULL, in this
340 case just return the overall width. */
341 int
342 w32font_text_extents (font, code, nglyphs, metrics)
343 struct font *font;
344 unsigned *code;
345 int nglyphs;
346 struct font_metrics *metrics;
347 {
348 int i;
349 HFONT old_font = NULL;
350 HDC dc = NULL;
351 struct frame * f;
352 int total_width = 0;
353 WORD *wcode = alloca(nglyphs * sizeof (WORD));
354 SIZE size;
355
356 /* TODO: Frames can come and go, and their fonts outlive them. So we
357 can't cache the frame in the font structure. Use selected_frame
358 until the API is updated to pass in a frame. */
359 f = XFRAME (selected_frame);
360
361 if (metrics)
362 {
363 GLYPHMETRICS gm;
364 MAT2 transform;
365 struct w32font_info *w32_font = (struct w32font_info *) font;
366
367 /* Set transform to the identity matrix. */
368 bzero (&transform, sizeof (transform));
369 transform.eM11.value = 1;
370 transform.eM22.value = 1;
371 metrics->width = 0;
372 metrics->ascent = 0;
373 metrics->descent = 0;
374 metrics->lbearing = 0;
375
376 for (i = 0; i < nglyphs; i++)
377 {
378 if (*(code + i) < 128)
379 {
380 /* Use cached metrics for ASCII. */
381 struct font_metrics *char_metric
382 = &w32_font->ascii_metrics[*(code+i)];
383
384 /* If we couldn't get metrics when caching, use fallback. */
385 if (char_metric->width == 0)
386 break;
387
388 metrics->lbearing = max (metrics->lbearing,
389 char_metric->lbearing - metrics->width);
390 metrics->rbearing = max (metrics->rbearing,
391 metrics->width + char_metric->rbearing);
392 metrics->width += char_metric->width;
393 metrics->ascent = max (metrics->ascent, char_metric->ascent);
394 metrics->descent = max (metrics->descent, char_metric->descent);
395 }
396 else
397 {
398 if (dc == NULL)
399 {
400 dc = get_frame_dc (f);
401 old_font = SelectObject (dc, ((W32FontStruct *)
402 (font->font.font))->hfont);
403 }
404 if (GetGlyphOutlineW (dc, *(code + i),
405 GGO_METRICS
406 | w32_font->glyph_idx
407 ? GGO_GLYPH_INDEX : 0,
408 &gm, 0, NULL, &transform) != GDI_ERROR)
409 {
410 int new_val = metrics->width + gm.gmBlackBoxX
411 + gm.gmptGlyphOrigin.x;
412 metrics->rbearing = max (metrics->rbearing, new_val);
413 new_val = -gm.gmptGlyphOrigin.x - metrics->width;
414 metrics->lbearing = max (metrics->lbearing, new_val);
415 metrics->width += gm.gmCellIncX;
416 new_val = -gm.gmptGlyphOrigin.y;
417 metrics->ascent = max (metrics->ascent, new_val);
418 new_val = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
419 metrics->descent = max (metrics->descent, new_val);
420 }
421 else
422 {
423 if (w32_font->glyph_idx)
424 {
425 /* Disable glyph indexing for this font, as we can't
426 handle the metrics. Abort this run, our recovery
427 strategies rely on having unicode code points here.
428 This will cause a glitch in display, but in practice,
429 any problems should be caught when initialising the
430 metrics cache. */
431 w32_font->glyph_idx = 0;
432 recompute_cached_metrics (dc, w32_font);
433 SelectObject (dc, old_font);
434 release_frame_dc (f, dc);
435 return 0;
436 }
437 /* Rely on an estimate based on the overall font metrics. */
438 break;
439 }
440 }
441 }
442
443 /* If we got through everything, return. */
444 if (i == nglyphs)
445 {
446 if (dc != NULL)
447 {
448 /* Restore state and release DC. */
449 SelectObject (dc, old_font);
450 release_frame_dc (f, dc);
451 }
452
453 return metrics->width;
454 }
455 }
456
457 for (i = 0; i < nglyphs; i++)
458 {
459 if (code[i] < 0x10000)
460 wcode[i] = code[i];
461 else
462 {
463 /* TODO: Convert to surrogate, reallocating array if needed */
464 wcode[i] = 0xffff;
465 }
466 }
467
468 if (dc == NULL)
469 {
470 dc = get_frame_dc (f);
471 old_font = SelectObject (dc, ((W32FontStruct *)
472 (font->font.font))->hfont);
473 }
474
475 if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
476 {
477 total_width = size.cx;
478 }
479
480 if (!total_width)
481 {
482 RECT rect;
483 rect.top = 0; rect.bottom = font->font.height; rect.left = 0; rect.right = 1;
484 DrawTextW (dc, wcode, nglyphs, &rect,
485 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
486 total_width = rect.right;
487 }
488
489 if (metrics)
490 {
491 metrics->width = total_width;
492 metrics->ascent = font->ascent;
493 metrics->descent = font->descent;
494 metrics->lbearing = 0;
495 metrics->rbearing = total_width
496 + ((struct w32font_info *) font)->metrics.tmOverhang;
497 }
498
499 /* Restore state and release DC. */
500 SelectObject (dc, old_font);
501 release_frame_dc (f, dc);
502
503 return total_width;
504 }
505
506 /* w32 implementation of draw for font backend.
507 Optional.
508 Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
509 position of frame F with S->FACE and S->GC. If WITH_BACKGROUND
510 is nonzero, fill the background in advance. It is assured that
511 WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).
512
513 TODO: Currently this assumes that the colors and fonts are already
514 set in the DC. This seems to be true now, but maybe only due to
515 the old font code setting it up. It may be safer to resolve faces
516 and fonts in here and set them explicitly
517 */
518
519 int
520 w32font_draw (s, from, to, x, y, with_background)
521 struct glyph_string *s;
522 int from, to, x, y, with_background;
523 {
524 UINT options;
525 HRGN orig_clip;
526 struct w32font_info *w32font = (struct w32font_info *) s->face->font_info;
527
528 options = w32font->glyph_idx;
529
530 /* Save clip region for later restoration. */
531 GetClipRgn(s->hdc, orig_clip);
532
533 if (s->num_clips > 0)
534 {
535 HRGN new_clip = CreateRectRgnIndirect (s->clip);
536
537 if (s->num_clips > 1)
538 {
539 HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);
540
541 CombineRgn (new_clip, new_clip, clip2, RGN_OR);
542 DeleteObject (clip2);
543 }
544
545 SelectClipRgn (s->hdc, new_clip);
546 DeleteObject (new_clip);
547 }
548
549 /* Using OPAQUE background mode can clear more background than expected
550 when Cleartype is used. Draw the background manually to avoid this. */
551 SetBkMode (s->hdc, TRANSPARENT);
552 if (with_background)
553 {
554 HBRUSH brush;
555 RECT rect;
556 struct font *font = (struct font *) s->face->font_info;
557
558 brush = CreateSolidBrush (s->gc->background);
559 rect.left = x;
560 rect.top = y - font->ascent;
561 rect.right = x + s->width;
562 rect.bottom = y + font->descent;
563 FillRect (s->hdc, &rect, brush);
564 DeleteObject (brush);
565 }
566
567 if (s->padding_p)
568 {
569 int len = to - from, i;
570
571 for (i = 0; i < len; i++)
572 ExtTextOutW (s->hdc, x + i, y, options, NULL,
573 s->char2b + from + i, 1, NULL);
574 }
575 else
576 ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
577
578 /* Restore clip region. */
579 if (s->num_clips > 0)
580 {
581 SelectClipRgn (s->hdc, orig_clip);
582 }
583 }
584
585 /* w32 implementation of free_entity for font backend.
586 Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
587 Free FONT_EXTRA_INDEX field of FONT_ENTITY.
588 static void
589 w32font_free_entity (Lisp_Object entity);
590 */
591
592 /* w32 implementation of prepare_face for font backend.
593 Optional (if FACE->extra is not used).
594 Prepare FACE for displaying characters by FONT on frame F by
595 storing some data in FACE->extra. If successful, return 0.
596 Otherwise, return -1.
597 static int
598 w32font_prepare_face (FRAME_PTR f, struct face *face);
599 */
600 /* w32 implementation of done_face for font backend.
601 Optional.
602 Done FACE for displaying characters by FACE->font on frame F.
603 static void
604 w32font_done_face (FRAME_PTR f, struct face *face); */
605
606 /* w32 implementation of get_bitmap for font backend.
607 Optional.
608 Store bitmap data for glyph-code CODE of FONT in BITMAP. It is
609 intended that this method is called from the other font-driver
610 for actual drawing.
611 static int
612 w32font_get_bitmap (struct font *font, unsigned code,
613 struct font_bitmap *bitmap, int bits_per_pixel);
614 */
615 /* w32 implementation of free_bitmap for font backend.
616 Optional.
617 Free bitmap data in BITMAP.
618 static void
619 w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
620 */
621 /* w32 implementation of get_outline for font backend.
622 Optional.
623 Return an outline data for glyph-code CODE of FONT. The format
624 of the outline data depends on the font-driver.
625 static void *
626 w32font_get_outline (struct font *font, unsigned code);
627 */
628 /* w32 implementation of free_outline for font backend.
629 Optional.
630 Free OUTLINE (that is obtained by the above method).
631 static void
632 w32font_free_outline (struct font *font, void *outline);
633 */
634 /* w32 implementation of anchor_point for font backend.
635 Optional.
636 Get coordinates of the INDEXth anchor point of the glyph whose
637 code is CODE. Store the coordinates in *X and *Y. Return 0 if
638 the operations was successfull. Otherwise return -1.
639 static int
640 w32font_anchor_point (struct font *font, unsigned code,
641 int index, int *x, int *y);
642 */
643 /* w32 implementation of otf_capability for font backend.
644 Optional.
645 Return a list describing which scripts/languages FONT
646 supports by which GSUB/GPOS features of OpenType tables.
647 static Lisp_Object
648 w32font_otf_capability (struct font *font);
649 */
650 /* w32 implementation of otf_drive for font backend.
651 Optional.
652 Apply FONT's OTF-FEATURES to the glyph string.
653
654 FEATURES specifies which OTF features to apply in this format:
655 (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
656 See the documentation of `font-drive-otf' for the detail.
657
658 This method applies the specified features to the codes in the
659 elements of GSTRING-IN (between FROMth and TOth). The output
660 codes are stored in GSTRING-OUT at the IDXth element and the
661 following elements.
662
663 Return the number of output codes. If none of the features are
664 applicable to the input data, return 0. If GSTRING-OUT is too
665 short, return -1.
666 static int
667 w32font_otf_drive (struct font *font, Lisp_Object features,
668 Lisp_Object gstring_in, int from, int to,
669 Lisp_Object gstring_out, int idx,
670 int alternate_subst);
671 */
672
673 /* Internal implementation of w32font_list.
674 Additional parameter opentype_only restricts the returned fonts to
675 opentype fonts, which can be used with the Uniscribe backend. */
676 Lisp_Object
677 w32font_list_internal (frame, font_spec, opentype_only)
678 Lisp_Object frame, font_spec;
679 int opentype_only;
680 {
681 struct font_callback_data match_data;
682 HDC dc;
683 FRAME_PTR f = XFRAME (frame);
684
685 match_data.orig_font_spec = font_spec;
686 match_data.list = Qnil;
687 match_data.frame = frame;
688
689 bzero (&match_data.pattern, sizeof (LOGFONT));
690 fill_in_logfont (f, &match_data.pattern, font_spec);
691
692 match_data.opentype_only = opentype_only;
693 if (opentype_only)
694 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
695
696 if (match_data.pattern.lfFaceName[0] == '\0')
697 {
698 /* EnumFontFamiliesEx does not take other fields into account if
699 font name is blank, so need to use two passes. */
700 list_all_matching_fonts (&match_data);
701 }
702 else
703 {
704 dc = get_frame_dc (f);
705
706 EnumFontFamiliesEx (dc, &match_data.pattern,
707 (FONTENUMPROC) add_font_entity_to_list,
708 (LPARAM) &match_data, 0);
709 release_frame_dc (f, dc);
710 }
711
712 return NILP (match_data.list) ? null_vector : Fvconcat (1, &match_data.list);
713 }
714
715 /* Internal implementation of w32font_match.
716 Additional parameter opentype_only restricts the returned fonts to
717 opentype fonts, which can be used with the Uniscribe backend. */
718 Lisp_Object
719 w32font_match_internal (frame, font_spec, opentype_only)
720 Lisp_Object frame, font_spec;
721 int opentype_only;
722 {
723 struct font_callback_data match_data;
724 HDC dc;
725 FRAME_PTR f = XFRAME (frame);
726
727 match_data.orig_font_spec = font_spec;
728 match_data.frame = frame;
729 match_data.list = Qnil;
730
731 bzero (&match_data.pattern, sizeof (LOGFONT));
732 fill_in_logfont (f, &match_data.pattern, font_spec);
733
734 match_data.opentype_only = opentype_only;
735 if (opentype_only)
736 match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
737
738 dc = get_frame_dc (f);
739
740 EnumFontFamiliesEx (dc, &match_data.pattern,
741 (FONTENUMPROC) add_one_font_entity_to_list,
742 (LPARAM) &match_data, 0);
743 release_frame_dc (f, dc);
744
745 return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
746 }
747
748 int
749 w32font_open_internal (f, font_entity, pixel_size, w32_font)
750 FRAME_PTR f;
751 Lisp_Object font_entity;
752 int pixel_size;
753 struct w32font_info *w32_font;
754 {
755 int len, size;
756 LOGFONT logfont;
757 HDC dc;
758 HFONT hfont, old_font;
759 Lisp_Object val, extra;
760 /* For backwards compatibility. */
761 W32FontStruct *compat_w32_font;
762
763 struct font * font = (struct font *) w32_font;
764 if (!font)
765 return 0;
766
767 bzero (&logfont, sizeof (logfont));
768 fill_in_logfont (f, &logfont, font_entity);
769
770 size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
771 if (!size)
772 size = pixel_size;
773
774 logfont.lfHeight = -size;
775 hfont = CreateFontIndirect (&logfont);
776
777 if (hfont == NULL)
778 return 0;
779
780 /* Get the metrics for this font. */
781 dc = get_frame_dc (f);
782 old_font = SelectObject (dc, hfont);
783
784 GetTextMetrics (dc, &w32_font->metrics);
785
786 /* Cache ASCII metrics. */
787 w32_font->glyph_idx = ETO_GLYPH_INDEX;
788 recompute_cached_metrics (dc, w32_font);
789
790 SelectObject (dc, old_font);
791 release_frame_dc (f, dc);
792
793 /* W32FontStruct - we should get rid of this, and use the w32font_info
794 struct for any W32 specific fields. font->font.font can then be hfont. */
795 font->font.font = xmalloc (sizeof (W32FontStruct));
796 compat_w32_font = (W32FontStruct *) font->font.font;
797 bzero (compat_w32_font, sizeof (W32FontStruct));
798 compat_w32_font->font_type = UNICODE_FONT;
799 /* Duplicate the text metrics. */
800 bcopy (&w32_font->metrics, &compat_w32_font->tm, sizeof (TEXTMETRIC));
801 compat_w32_font->hfont = hfont;
802
803 len = strlen (logfont.lfFaceName);
804 font->font.name = (char *) xmalloc (len + 1);
805 bcopy (logfont.lfFaceName, font->font.name, len);
806 font->font.name[len] = '\0';
807
808 {
809 char *name;
810
811 /* We don't know how much space we need for the full name, so start with
812 96 bytes and go up in steps of 32. */
813 len = 96;
814 name = xmalloc (len);
815 while (name && w32font_full_name (&logfont, font_entity, pixel_size,
816 name, len) < 0)
817 {
818 char *new = xrealloc (name, len += 32);
819
820 if (! new)
821 xfree (name);
822 name = new;
823 }
824 if (name)
825 font->font.full_name = name;
826 else
827 font->font.full_name = font->font.name;
828 }
829 font->font.charset = 0;
830 font->font.codepage = 0;
831 font->font.size = w32_font->metrics.tmMaxCharWidth;
832 font->font.height = w32_font->metrics.tmHeight
833 + w32_font->metrics.tmExternalLeading;
834 font->font.space_width = font->font.average_width
835 = w32_font->metrics.tmAveCharWidth;
836
837 font->font.vertical_centering = 0;
838 font->font.encoding_type = 0;
839 font->font.baseline_offset = 0;
840 font->font.relative_compose = 0;
841 font->font.default_ascent = w32_font->metrics.tmAscent;
842 font->font.font_encoder = NULL;
843 font->entity = font_entity;
844 font->pixel_size = size;
845 font->driver = &w32font_driver;
846 /* Use format cached during list, as the information we have access to
847 here is incomplete. */
848 extra = AREF (font_entity, FONT_EXTRA_INDEX);
849 if (CONSP (extra))
850 {
851 val = assq_no_quit (QCformat, extra);
852 if (CONSP (val))
853 font->format = XCDR (val);
854 else
855 font->format = Qunknown;
856 }
857 else
858 font->format = Qunknown;
859
860 font->file_name = NULL;
861 font->encoding_charset = -1;
862 font->repertory_charset = -1;
863 /* TODO: do we really want the minimum width here, which could be negative? */
864 font->min_width = font->font.space_width;
865 font->ascent = w32_font->metrics.tmAscent;
866 font->descent = w32_font->metrics.tmDescent;
867 font->scalable = w32_font->metrics.tmPitchAndFamily & TMPF_VECTOR;
868
869 /* Set global flag fonts_changed_p to non-zero if the font loaded
870 has a character with a smaller width than any other character
871 before, or if the font loaded has a smaller height than any other
872 font loaded before. If this happens, it will make a glyph matrix
873 reallocation necessary. */
874 {
875 struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
876 dpyinfo->n_fonts++;
877
878 if (dpyinfo->n_fonts == 1)
879 {
880 dpyinfo->smallest_font_height = font->font.height;
881 dpyinfo->smallest_char_width = font->min_width;
882 }
883 else
884 {
885 if (dpyinfo->smallest_font_height > font->font.height)
886 {
887 dpyinfo->smallest_font_height = font->font.height;
888 fonts_changed_p |= 1;
889 }
890 if (dpyinfo->smallest_char_width > font->min_width)
891 {
892 dpyinfo->smallest_char_width = font->min_width;
893 fonts_changed_p |= 1;
894 }
895 }
896 }
897
898 return 1;
899 }
900
901 /* Callback function for EnumFontFamiliesEx.
902 * Adds the name of a font to a Lisp list (passed in as the lParam arg). */
903 static int CALLBACK
904 add_font_name_to_list (logical_font, physical_font, font_type, list_object)
905 ENUMLOGFONTEX *logical_font;
906 NEWTEXTMETRICEX *physical_font;
907 DWORD font_type;
908 LPARAM list_object;
909 {
910 Lisp_Object* list = (Lisp_Object *) list_object;
911 Lisp_Object family;
912
913 /* Skip vertical fonts (intended only for printing) */
914 if (logical_font->elfLogFont.lfFaceName[0] == '@')
915 return 1;
916
917 family = intern_downcase (logical_font->elfLogFont.lfFaceName,
918 strlen (logical_font->elfLogFont.lfFaceName));
919 if (! memq_no_quit (family, *list))
920 *list = Fcons (family, *list);
921
922 return 1;
923 }
924
925 /* Convert an enumerated Windows font to an Emacs font entity. */
926 static Lisp_Object
927 w32_enumfont_pattern_entity (frame, logical_font, physical_font,
928 font_type, requested_font, backend)
929 Lisp_Object frame;
930 ENUMLOGFONTEX *logical_font;
931 NEWTEXTMETRICEX *physical_font;
932 DWORD font_type;
933 LOGFONT *requested_font;
934 Lisp_Object backend;
935 {
936 Lisp_Object entity, tem;
937 LOGFONT *lf = (LOGFONT*) logical_font;
938 BYTE generic_type;
939 BYTE full_type = physical_font->ntmTm.ntmFlags;
940
941 entity = Fmake_vector (make_number (FONT_ENTITY_MAX), Qnil);
942
943 ASET (entity, FONT_TYPE_INDEX, backend);
944 ASET (entity, FONT_FRAME_INDEX, frame);
945 ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet));
946 ASET (entity, FONT_OBJLIST_INDEX, Qnil);
947
948 /* Foundry is difficult to get in readable form on Windows.
949 But Emacs crashes if it is not set, so set it to something more
950 generic. Thes values make xflds compatible with Emacs 22. */
951 if (lf->lfOutPrecision == OUT_STRING_PRECIS)
952 tem = Qraster;
953 else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
954 tem = Qoutline;
955 else
956 tem = Qunknown;
957
958 ASET (entity, FONT_FOUNDRY_INDEX, tem);
959
960 /* Save the generic family in the extra info, as it is likely to be
961 useful to users looking for a close match. */
962 generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
963 if (generic_type == FF_DECORATIVE)
964 tem = Qdecorative;
965 else if (generic_type == FF_MODERN)
966 tem = Qmono;
967 else if (generic_type == FF_ROMAN)
968 tem = Qserif;
969 else if (generic_type == FF_SCRIPT)
970 tem = Qscript;
971 else if (generic_type == FF_SWISS)
972 tem = Qsans;
973 else
974 tem = null_string;
975
976 ASET (entity, FONT_ADSTYLE_INDEX, tem);
977
978 if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
979 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_PROPORTIONAL));
980 else
981 font_put_extra (entity, QCspacing, make_number (FONT_SPACING_MONO));
982
983 if (requested_font->lfQuality != DEFAULT_QUALITY)
984 {
985 font_put_extra (entity, QCantialias,
986 lispy_antialias_type (requested_font->lfQuality));
987 }
988 ASET (entity, FONT_FAMILY_INDEX,
989 intern_downcase (lf->lfFaceName, strlen (lf->lfFaceName)));
990
991 ASET (entity, FONT_WEIGHT_INDEX, make_number (lf->lfWeight));
992 ASET (entity, FONT_SLANT_INDEX, make_number (lf->lfItalic ? 200 : 100));
993 /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
994 to get it. */
995 ASET (entity, FONT_WIDTH_INDEX, make_number (100));
996
997 if (font_type & RASTER_FONTTYPE)
998 ASET (entity, FONT_SIZE_INDEX, make_number (physical_font->ntmTm.tmHeight));
999 else
1000 ASET (entity, FONT_SIZE_INDEX, make_number (0));
1001
1002 /* Cache unicode codepoints covered by this font, as there is no other way
1003 of getting this information easily. */
1004 if (font_type & TRUETYPE_FONTTYPE)
1005 {
1006 font_put_extra (entity, QCscript,
1007 font_supported_scripts (&physical_font->ntmFontSig));
1008 }
1009
1010 /* This information is not fully available when opening fonts, so
1011 save it here. Only Windows 2000 and later return information
1012 about opentype and type1 fonts, so need a fallback for detecting
1013 truetype so that this information is not any worse than we could
1014 have obtained later. */
1015 if (full_type & NTM_TT_OPENTYPE || font_type & TRUETYPE_FONTTYPE)
1016 tem = intern ("truetype");
1017 else if (full_type & NTM_TYPE1)
1018 tem = intern ("type1");
1019 else if (full_type & NTM_PS_OPENTYPE)
1020 tem = intern ("postscript");
1021 else if (font_type & RASTER_FONTTYPE)
1022 tem = intern ("w32bitmap");
1023 else
1024 tem = intern ("w32vector");
1025
1026 font_put_extra (entity, QCformat, tem);
1027
1028 return entity;
1029 }
1030
1031
1032 /* Convert generic families to the family portion of lfPitchAndFamily. */
1033 BYTE
1034 w32_generic_family (Lisp_Object name)
1035 {
1036 /* Generic families. */
1037 if (EQ (name, Qmonospace) || EQ (name, Qmono))
1038 return FF_MODERN;
1039 else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
1040 return FF_SWISS;
1041 else if (EQ (name, Qserif))
1042 return FF_ROMAN;
1043 else if (EQ (name, Qdecorative))
1044 return FF_DECORATIVE;
1045 else if (EQ (name, Qscript))
1046 return FF_SCRIPT;
1047 else
1048 return FF_DONTCARE;
1049 }
1050
1051 static int
1052 logfonts_match (font, pattern)
1053 LOGFONT *font, *pattern;
1054 {
1055 /* Only check height for raster fonts. */
1056 if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
1057 && font->lfHeight != pattern->lfHeight)
1058 return 0;
1059
1060 /* Have some flexibility with weights. */
1061 if (pattern->lfWeight
1062 && ((font->lfWeight < (pattern->lfWeight - 150))
1063 || font->lfWeight > (pattern->lfWeight + 150)))
1064 return 0;
1065
1066 /* Charset and face should be OK. Italic has to be checked
1067 against the original spec, in case we don't have any preference. */
1068 return 1;
1069 }
1070
1071 static int
1072 font_matches_spec (type, font, spec)
1073 DWORD type;
1074 NEWTEXTMETRICEX *font;
1075 Lisp_Object spec;
1076 {
1077 Lisp_Object extra, val;
1078
1079 /* Check italic. Can't check logfonts, since it is a boolean field,
1080 so there is no difference between "non-italic" and "don't care". */
1081 val = AREF (spec, FONT_SLANT_INDEX);
1082 if (INTEGERP (val))
1083 {
1084 int slant = XINT (val);
1085 if ((slant > 150 && !font->ntmTm.tmItalic)
1086 || (slant <= 150 && font->ntmTm.tmItalic))
1087 return 0;
1088 }
1089
1090 /* Check adstyle against generic family. */
1091 val = AREF (spec, FONT_ADSTYLE_INDEX);
1092 if (!NILP (val))
1093 {
1094 BYTE family = w32_generic_family (val);
1095 if (family != FF_DONTCARE
1096 && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
1097 return 0;
1098 }
1099
1100 /* Check extra parameters. */
1101 for (extra = AREF (spec, FONT_EXTRA_INDEX);
1102 CONSP (extra); extra = XCDR (extra))
1103 {
1104 Lisp_Object extra_entry;
1105 extra_entry = XCAR (extra);
1106 if (CONSP (extra_entry))
1107 {
1108 Lisp_Object key = XCAR (extra_entry);
1109 val = XCDR (extra_entry);
1110 if (EQ (key, QCspacing))
1111 {
1112 int proportional;
1113 if (INTEGERP (val))
1114 {
1115 int spacing = XINT (val);
1116 proportional = (spacing < FONT_SPACING_MONO);
1117 }
1118 else if (EQ (val, Qp))
1119 proportional = 1;
1120 else if (EQ (val, Qc) || EQ (val, Qm))
1121 proportional = 0;
1122 else
1123 return 0; /* Bad font spec. */
1124
1125 if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
1126 || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
1127 return 0;
1128 }
1129 else if (EQ (key, QCscript) && SYMBOLP (val))
1130 {
1131 /* Only truetype fonts will have information about what
1132 scripts they support. This probably means the user
1133 will have to force Emacs to use raster, postscript
1134 or atm fonts for non-ASCII text. */
1135 if (type & TRUETYPE_FONTTYPE)
1136 {
1137 Lisp_Object support
1138 = font_supported_scripts (&font->ntmFontSig);
1139 if (! memq_no_quit (val, support))
1140 return 0;
1141 }
1142 else
1143 {
1144 /* Return specific matches, but play it safe. Fonts
1145 that cover more than their charset would suggest
1146 are likely to be truetype or opentype fonts,
1147 covered above. */
1148 if (EQ (val, Qlatin))
1149 {
1150 /* Although every charset but symbol, thai and
1151 arabic contains the basic ASCII set of latin
1152 characters, Emacs expects much more. */
1153 if (font->ntmTm.tmCharSet != ANSI_CHARSET)
1154 return 0;
1155 }
1156 else if (EQ (val, Qsymbol))
1157 {
1158 if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
1159 return 0;
1160 }
1161 else if (EQ (val, Qcyrillic))
1162 {
1163 if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
1164 return 0;
1165 }
1166 else if (EQ (val, Qgreek))
1167 {
1168 if (font->ntmTm.tmCharSet != GREEK_CHARSET)
1169 return 0;
1170 }
1171 else if (EQ (val, Qarabic))
1172 {
1173 if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
1174 return 0;
1175 }
1176 else if (EQ (val, Qhebrew))
1177 {
1178 if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
1179 return 0;
1180 }
1181 else if (EQ (val, Qthai))
1182 {
1183 if (font->ntmTm.tmCharSet != THAI_CHARSET)
1184 return 0;
1185 }
1186 else if (EQ (val, Qkana))
1187 {
1188 if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1189 return 0;
1190 }
1191 else if (EQ (val, Qbopomofo))
1192 {
1193 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
1194 return 0;
1195 }
1196 else if (EQ (val, Qhangul))
1197 {
1198 if (font->ntmTm.tmCharSet != HANGUL_CHARSET
1199 && font->ntmTm.tmCharSet != JOHAB_CHARSET)
1200 return 0;
1201 }
1202 else if (EQ (val, Qhan))
1203 {
1204 if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
1205 && font->ntmTm.tmCharSet != GB2312_CHARSET
1206 && font->ntmTm.tmCharSet != HANGUL_CHARSET
1207 && font->ntmTm.tmCharSet != JOHAB_CHARSET
1208 && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
1209 return 0;
1210 }
1211 else
1212 /* Other scripts unlikely to be handled. */
1213 return 0;
1214 }
1215 }
1216 }
1217 }
1218 return 1;
1219 }
1220
1221 /* Callback function for EnumFontFamiliesEx.
1222 * Checks if a font matches everything we are trying to check agaist,
1223 * and if so, adds it to a list. Both the data we are checking against
1224 * and the list to which the fonts are added are passed in via the
1225 * lparam argument, in the form of a font_callback_data struct. */
1226 static int CALLBACK
1227 add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1228 ENUMLOGFONTEX *logical_font;
1229 NEWTEXTMETRICEX *physical_font;
1230 DWORD font_type;
1231 LPARAM lParam;
1232 {
1233 struct font_callback_data *match_data
1234 = (struct font_callback_data *) lParam;
1235
1236 if ((!match_data->opentype_only
1237 || (physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE))
1238 && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1239 && font_matches_spec (font_type, physical_font,
1240 match_data->orig_font_spec)
1241 /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
1242 We limit this to raster fonts, because the test can catch some
1243 genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
1244 DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
1245 therefore get through this test. Since full names can be prefixed
1246 by a foundry, we accept raster fonts if the font name is found
1247 anywhere within the full name. */
1248 && (logical_font->elfLogFont.lfOutPrecision != OUT_STRING_PRECIS
1249 || strstr (logical_font->elfFullName,
1250 logical_font->elfLogFont.lfFaceName)))
1251 {
1252 Lisp_Object entity
1253 = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1254 physical_font, font_type,
1255 &match_data->pattern,
1256 match_data->opentype_only
1257 ? Quniscribe : Qgdi);
1258 if (!NILP (entity))
1259 match_data->list = Fcons (entity, match_data->list);
1260 }
1261 return 1;
1262 }
1263
1264 /* Callback function for EnumFontFamiliesEx.
1265 * Terminates the search once we have a match. */
1266 static int CALLBACK
1267 add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1268 ENUMLOGFONTEX *logical_font;
1269 NEWTEXTMETRICEX *physical_font;
1270 DWORD font_type;
1271 LPARAM lParam;
1272 {
1273 struct font_callback_data *match_data
1274 = (struct font_callback_data *) lParam;
1275 add_font_entity_to_list (logical_font, physical_font, font_type, lParam);
1276
1277 /* If we have a font in the list, terminate the search. */
1278 return !NILP (match_data->list);
1279 }
1280
1281 /* Convert a Lisp font registry (symbol) to a windows charset. */
1282 static LONG
1283 registry_to_w32_charset (charset)
1284 Lisp_Object charset;
1285 {
1286 if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
1287 || EQ (charset, Qunicode_sip))
1288 return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
1289 else if (EQ (charset, Qiso8859_1))
1290 return ANSI_CHARSET;
1291 else if (SYMBOLP (charset))
1292 return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
1293 else if (STRINGP (charset))
1294 return x_to_w32_charset (SDATA (charset));
1295 else
1296 return DEFAULT_CHARSET;
1297 }
1298
1299 static Lisp_Object
1300 w32_registry (w32_charset)
1301 LONG w32_charset;
1302 {
1303 if (w32_charset == ANSI_CHARSET)
1304 return Qiso10646_1;
1305 else
1306 {
1307 char * charset = w32_to_x_charset (w32_charset, NULL);
1308 return intern_downcase (charset, strlen(charset));
1309 }
1310 }
1311
1312 /* Fill in all the available details of LOGFONT from FONT_SPEC. */
1313 static void
1314 fill_in_logfont (f, logfont, font_spec)
1315 FRAME_PTR f;
1316 LOGFONT *logfont;
1317 Lisp_Object font_spec;
1318 {
1319 Lisp_Object tmp, extra;
1320 int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;
1321
1322 extra = AREF (font_spec, FONT_EXTRA_INDEX);
1323 /* Allow user to override dpi settings. */
1324 if (CONSP (extra))
1325 {
1326 tmp = assq_no_quit (QCdpi, extra);
1327 if (CONSP (tmp) && INTEGERP (XCDR (tmp)))
1328 {
1329 dpi = XINT (XCDR (tmp));
1330 }
1331 else if (CONSP (tmp) && FLOATP (XCDR (tmp)))
1332 {
1333 dpi = (int) (XFLOAT_DATA (XCDR (tmp)) + 0.5);
1334 }
1335 }
1336
1337 /* Height */
1338 tmp = AREF (font_spec, FONT_SIZE_INDEX);
1339 if (INTEGERP (tmp))
1340 logfont->lfHeight = -1 * XINT (tmp);
1341 else if (FLOATP (tmp))
1342 logfont->lfHeight = (int) (-1.0 * dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
1343
1344 /* Escapement */
1345
1346 /* Orientation */
1347
1348 /* Weight */
1349 tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
1350 if (INTEGERP (tmp))
1351 logfont->lfWeight = XINT (tmp);
1352
1353 /* Italic */
1354 tmp = AREF (font_spec, FONT_SLANT_INDEX);
1355 if (INTEGERP (tmp))
1356 {
1357 int slant = XINT (tmp);
1358 logfont->lfItalic = slant > 150 ? 1 : 0;
1359 }
1360
1361 /* Underline */
1362
1363 /* Strikeout */
1364
1365 /* Charset */
1366 tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
1367 if (! NILP (tmp))
1368 logfont->lfCharSet = registry_to_w32_charset (tmp);
1369
1370 /* Out Precision */
1371
1372 /* Clip Precision */
1373
1374 /* Quality */
1375 logfont->lfQuality = DEFAULT_QUALITY;
1376
1377 /* Generic Family and Face Name */
1378 logfont->lfPitchAndFamily = FF_DONTCARE | DEFAULT_PITCH;
1379
1380 tmp = AREF (font_spec, FONT_FAMILY_INDEX);
1381 if (! NILP (tmp))
1382 {
1383 logfont->lfPitchAndFamily = w32_generic_family (tmp) | DEFAULT_PITCH;
1384 if ((logfont->lfPitchAndFamily & 0xF0) != FF_DONTCARE)
1385 ; /* Font name was generic, don't fill in font name. */
1386 /* Font families are interned, but allow for strings also in case of
1387 user input. */
1388 else if (SYMBOLP (tmp))
1389 strncpy (logfont->lfFaceName, SDATA (SYMBOL_NAME (tmp)), LF_FACESIZE);
1390 else if (STRINGP (tmp))
1391 strncpy (logfont->lfFaceName, SDATA (tmp), LF_FACESIZE);
1392 }
1393
1394 tmp = AREF (font_spec, FONT_ADSTYLE_INDEX);
1395 if (!NILP (tmp))
1396 {
1397 /* Override generic family. */
1398 BYTE family = w32_generic_family (tmp);
1399 if (family != FF_DONTCARE)
1400 logfont->lfPitchAndFamily = family | DEFAULT_PITCH;
1401 }
1402
1403 /* Process EXTRA info. */
1404 for ( ; CONSP (extra); extra = XCDR (extra))
1405 {
1406 tmp = XCAR (extra);
1407 if (CONSP (tmp))
1408 {
1409 Lisp_Object key, val;
1410 key = XCAR (tmp), val = XCDR (tmp);
1411 if (EQ (key, QCspacing))
1412 {
1413 /* Set pitch based on the spacing property. */
1414 if (INTEGERP (val))
1415 {
1416 int spacing = XINT (val);
1417 if (spacing < FONT_SPACING_MONO)
1418 logfont->lfPitchAndFamily
1419 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1420 else
1421 logfont->lfPitchAndFamily
1422 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1423 }
1424 else if (EQ (val, Qp))
1425 logfont->lfPitchAndFamily
1426 = logfont->lfPitchAndFamily & 0xF0 | VARIABLE_PITCH;
1427 else if (EQ (val, Qc) || EQ (val, Qm))
1428 logfont->lfPitchAndFamily
1429 = logfont->lfPitchAndFamily & 0xF0 | FIXED_PITCH;
1430 }
1431 /* Only use QCscript if charset is not provided, or is unicode
1432 and a single script is specified. This is rather crude,
1433 and is only used to narrow down the fonts returned where
1434 there is a definite match. Some scripts, such as latin, han,
1435 cjk-misc match multiple lfCharSet values, so we can't pre-filter
1436 them. */
1437 else if (EQ (key, QCscript)
1438 && logfont->lfCharSet == DEFAULT_CHARSET
1439 && SYMBOLP (val))
1440 {
1441 if (EQ (val, Qgreek))
1442 logfont->lfCharSet = GREEK_CHARSET;
1443 else if (EQ (val, Qhangul))
1444 logfont->lfCharSet = HANGUL_CHARSET;
1445 else if (EQ (val, Qkana) || EQ (val, Qkanbun))
1446 logfont->lfCharSet = SHIFTJIS_CHARSET;
1447 else if (EQ (val, Qbopomofo))
1448 logfont->lfCharSet = CHINESEBIG5_CHARSET;
1449 /* GB 18030 supports tibetan, yi, mongolian,
1450 fonts that support it should show up if we ask for
1451 GB2312 fonts. */
1452 else if (EQ (val, Qtibetan) || EQ (val, Qyi)
1453 || EQ (val, Qmongolian))
1454 logfont->lfCharSet = GB2312_CHARSET;
1455 else if (EQ (val, Qhebrew))
1456 logfont->lfCharSet = HEBREW_CHARSET;
1457 else if (EQ (val, Qarabic))
1458 logfont->lfCharSet = ARABIC_CHARSET;
1459 else if (EQ (val, Qthai))
1460 logfont->lfCharSet = THAI_CHARSET;
1461 else if (EQ (val, Qsymbol))
1462 logfont->lfCharSet = SYMBOL_CHARSET;
1463 }
1464 else if (EQ (key, QCantialias) && SYMBOLP (val))
1465 {
1466 logfont->lfQuality = w32_antialias_type (val);
1467 }
1468 }
1469 }
1470 }
1471
1472 static void
1473 list_all_matching_fonts (match_data)
1474 struct font_callback_data *match_data;
1475 {
1476 HDC dc;
1477 Lisp_Object families = w32font_list_family (match_data->frame);
1478 struct frame *f = XFRAME (match_data->frame);
1479
1480 dc = get_frame_dc (f);
1481
1482 while (!NILP (families))
1483 {
1484 /* TODO: Use the Unicode versions of the W32 APIs, so we can
1485 handle non-ASCII font names. */
1486 char *name;
1487 Lisp_Object family = CAR (families);
1488 families = CDR (families);
1489 if (NILP (family))
1490 continue;
1491 else if (STRINGP (family))
1492 name = SDATA (family);
1493 else
1494 name = SDATA (SYMBOL_NAME (family));
1495
1496 strncpy (match_data->pattern.lfFaceName, name, LF_FACESIZE);
1497 match_data->pattern.lfFaceName[LF_FACESIZE - 1] = '\0';
1498
1499 EnumFontFamiliesEx (dc, &match_data->pattern,
1500 (FONTENUMPROC) add_font_entity_to_list,
1501 (LPARAM) match_data, 0);
1502 }
1503
1504 release_frame_dc (f, dc);
1505 }
1506
1507 static Lisp_Object
1508 lispy_antialias_type (type)
1509 BYTE type;
1510 {
1511 Lisp_Object lispy;
1512
1513 switch (type)
1514 {
1515 case NONANTIALIASED_QUALITY:
1516 lispy = Qnone;
1517 break;
1518 case ANTIALIASED_QUALITY:
1519 lispy = Qstandard;
1520 break;
1521 case CLEARTYPE_QUALITY:
1522 lispy = Qsubpixel;
1523 break;
1524 case CLEARTYPE_NATURAL_QUALITY:
1525 lispy = Qnatural;
1526 break;
1527 default:
1528 lispy = Qnil;
1529 break;
1530 }
1531 return lispy;
1532 }
1533
1534 /* Convert antialiasing symbols to lfQuality */
1535 static BYTE
1536 w32_antialias_type (type)
1537 Lisp_Object type;
1538 {
1539 if (EQ (type, Qnone))
1540 return NONANTIALIASED_QUALITY;
1541 else if (EQ (type, Qstandard))
1542 return ANTIALIASED_QUALITY;
1543 else if (EQ (type, Qsubpixel))
1544 return CLEARTYPE_QUALITY;
1545 else if (EQ (type, Qnatural))
1546 return CLEARTYPE_NATURAL_QUALITY;
1547 else
1548 return DEFAULT_QUALITY;
1549 }
1550
1551 /* Return a list of all the scripts that the font supports. */
1552 static Lisp_Object
1553 font_supported_scripts (FONTSIGNATURE * sig)
1554 {
1555 DWORD * subranges = sig->fsUsb;
1556 Lisp_Object supported = Qnil;
1557
1558 /* Match a single subrange. SYM is set if bit N is set in subranges. */
1559 #define SUBRANGE(n,sym) \
1560 if (subranges[(n) / 32] & (1 << ((n) % 32))) \
1561 supported = Fcons ((sym), supported)
1562
1563 /* Match multiple subranges. SYM is set if any MASK bit is set in
1564 subranges[0 - 3]. */
1565 #define MASK_ANY(mask0,mask1,mask2,mask3,sym) \
1566 if ((subranges[0] & (mask0)) || (subranges[1] & (mask1)) \
1567 || (subranges[2] & (mask2)) || (subranges[3] & (mask3))) \
1568 supported = Fcons ((sym), supported)
1569
1570 SUBRANGE (0, Qlatin); /* There are many others... */
1571
1572 SUBRANGE (7, Qgreek);
1573 SUBRANGE (8, Qcoptic);
1574 SUBRANGE (9, Qcyrillic);
1575 SUBRANGE (10, Qarmenian);
1576 SUBRANGE (11, Qhebrew);
1577 SUBRANGE (13, Qarabic);
1578 SUBRANGE (14, Qnko);
1579 SUBRANGE (15, Qdevanagari);
1580 SUBRANGE (16, Qbengali);
1581 SUBRANGE (17, Qgurmukhi);
1582 SUBRANGE (18, Qgujarati);
1583 SUBRANGE (19, Qoriya);
1584 SUBRANGE (20, Qtamil);
1585 SUBRANGE (21, Qtelugu);
1586 SUBRANGE (22, Qkannada);
1587 SUBRANGE (23, Qmalayalam);
1588 SUBRANGE (24, Qthai);
1589 SUBRANGE (25, Qlao);
1590 SUBRANGE (26, Qgeorgian);
1591
1592 SUBRANGE (48, Qcjk_misc);
1593 SUBRANGE (51, Qbopomofo);
1594 SUBRANGE (54, Qkanbun); /* Is this right? */
1595 SUBRANGE (56, Qhangul);
1596
1597 SUBRANGE (59, Qhan); /* There are others, but this is the main one. */
1598 SUBRANGE (59, Qideographic_description); /* Windows lumps this in */
1599
1600 SUBRANGE (70, Qtibetan);
1601 SUBRANGE (71, Qsyriac);
1602 SUBRANGE (72, Qthaana);
1603 SUBRANGE (73, Qsinhala);
1604 SUBRANGE (74, Qmyanmar);
1605 SUBRANGE (75, Qethiopic);
1606 SUBRANGE (76, Qcherokee);
1607 SUBRANGE (77, Qcanadian_aboriginal);
1608 SUBRANGE (78, Qogham);
1609 SUBRANGE (79, Qrunic);
1610 SUBRANGE (80, Qkhmer);
1611 SUBRANGE (81, Qmongolian);
1612 SUBRANGE (82, Qbraille);
1613 SUBRANGE (83, Qyi);
1614
1615 SUBRANGE (88, Qbyzantine_musical_symbol);
1616 SUBRANGE (88, Qmusical_symbol); /* Windows doesn't distinguish these. */
1617
1618 SUBRANGE (89, Qmathematical);
1619
1620 /* Match either katakana or hiragana for kana. */
1621 MASK_ANY (0, 0x00060000, 0, 0, Qkana);
1622
1623 /* There isn't really a main symbol range, so include symbol if any
1624 relevant range is set. */
1625 MASK_ANY (0x8000000, 0x0000FFFF, 0, 0, Qsymbol);
1626
1627 #undef SUBRANGE
1628 #undef MASK_ANY
1629
1630 return supported;
1631 }
1632
1633 /* Generate a full name for a Windows font.
1634 The full name is in fcname format, with weight, slant and antialiasing
1635 specified if they are not "normal". */
1636 static int
1637 w32font_full_name (font, font_obj, pixel_size, name, nbytes)
1638 LOGFONT * font;
1639 Lisp_Object font_obj;
1640 int pixel_size;
1641 char *name;
1642 int nbytes;
1643 {
1644 int len, height, outline;
1645 char *p;
1646 Lisp_Object antialiasing, weight = Qnil;
1647
1648 len = strlen (font->lfFaceName);
1649
1650 outline = EQ (AREF (font_obj, FONT_FOUNDRY_INDEX), Qoutline);
1651
1652 /* Represent size of scalable fonts by point size. But use pixelsize for
1653 raster fonts to indicate that they are exactly that size. */
1654 if (outline)
1655 len += 11; /* -SIZE */
1656 else
1657 len = strlen (font->lfFaceName) + 21;
1658
1659 if (font->lfItalic)
1660 len += 7; /* :italic */
1661
1662 if (font->lfWeight && font->lfWeight != FW_NORMAL)
1663 {
1664 weight = font_symbolic_weight (font_obj);
1665 len += 8 + SBYTES (SYMBOL_NAME (weight)); /* :weight=NAME */
1666 }
1667
1668 antialiasing = lispy_antialias_type (font->lfQuality);
1669 if (! NILP (antialiasing))
1670 len += 11 + SBYTES (SYMBOL_NAME (antialiasing)); /* :antialias=NAME */
1671
1672 /* Check that the buffer is big enough */
1673 if (len > nbytes)
1674 return -1;
1675
1676 p = name;
1677 p += sprintf (p, "%s", font->lfFaceName);
1678
1679 height = font->lfHeight ? eabs (font->lfHeight) : pixel_size;
1680
1681 if (height > 0)
1682 {
1683 if (outline)
1684 {
1685 float pointsize = height * 72.0 / one_w32_display_info.resy;
1686 /* Round to nearest half point. */
1687 pointsize = round (pointsize * 2) / 2;
1688 p += sprintf (p, "-%1.1f", pointsize);
1689 }
1690 else
1691 p += sprintf (p, ":pixelsize=%d", height);
1692 }
1693
1694 if (font->lfItalic)
1695 p += sprintf (p, ":italic");
1696
1697 if (SYMBOLP (weight) && ! NILP (weight))
1698 p += sprintf (p, ":weight=%s", SDATA (SYMBOL_NAME (weight)));
1699
1700 if (SYMBOLP (antialiasing) && ! NILP (antialiasing))
1701 p += sprintf (p, ":antialias=%s", SDATA (SYMBOL_NAME (antialiasing)));
1702
1703 return (p - name);
1704 }
1705
1706
1707 static void
1708 recompute_cached_metrics (dc, w32_font)
1709 HDC dc;
1710 struct w32font_info *w32_font;
1711 {
1712 GLYPHMETRICS gm;
1713 MAT2 transform;
1714 unsigned int i;
1715
1716 bzero (&transform, sizeof (transform));
1717 transform.eM11.value = 1;
1718 transform.eM22.value = 1;
1719
1720 for (i = 0; i < 128; i++)
1721 {
1722 struct font_metrics* char_metric = &w32_font->ascii_metrics[i];
1723 unsigned int options = GGO_METRICS;
1724 if (w32_font->glyph_idx)
1725 options |= GGO_GLYPH_INDEX;
1726
1727 if (GetGlyphOutlineW (dc, i, options, &gm, 0, NULL, &transform)
1728 != GDI_ERROR)
1729 {
1730 char_metric->lbearing = -gm.gmptGlyphOrigin.x;
1731 char_metric->rbearing = gm.gmBlackBoxX + gm.gmptGlyphOrigin.x;
1732 char_metric->width = gm.gmCellIncX;
1733 char_metric->ascent = -gm.gmptGlyphOrigin.y;
1734 char_metric->descent = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
1735 }
1736 else
1737 char_metric->width = 0;
1738 }
1739 }
1740
1741 struct font_driver w32font_driver =
1742 {
1743 0, /* Qgdi */
1744 w32font_get_cache,
1745 w32font_list,
1746 w32font_match,
1747 w32font_list_family,
1748 NULL, /* free_entity */
1749 w32font_open,
1750 w32font_close,
1751 NULL, /* prepare_face */
1752 NULL, /* done_face */
1753 w32font_has_char,
1754 w32font_encode_char,
1755 w32font_text_extents,
1756 w32font_draw,
1757 NULL, /* get_bitmap */
1758 NULL, /* free_bitmap */
1759 NULL, /* get_outline */
1760 NULL, /* free_outline */
1761 NULL, /* anchor_point */
1762 NULL, /* otf_capability */
1763 NULL, /* otf_drive */
1764 NULL, /* start_for_frame */
1765 NULL, /* end_for_frame */
1766 NULL /* shape */
1767 };
1768
1769
1770 /* Initialize state that does not change between invocations. This is only
1771 called when Emacs is dumped. */
1772 void
1773 syms_of_w32font ()
1774 {
1775 DEFSYM (Qgdi, "gdi");
1776 DEFSYM (Quniscribe, "uniscribe");
1777 DEFSYM (QCformat, ":format");
1778
1779 /* Generic font families. */
1780 DEFSYM (Qmonospace, "monospace");
1781 DEFSYM (Qserif, "serif");
1782 DEFSYM (Qsansserif, "sansserif");
1783 DEFSYM (Qscript, "script");
1784 DEFSYM (Qdecorative, "decorative");
1785 /* Aliases. */
1786 DEFSYM (Qsans_serif, "sans_serif");
1787 DEFSYM (Qsans, "sans");
1788 DEFSYM (Qmono, "mono");
1789
1790 /* Fake foundries. */
1791 DEFSYM (Qraster, "raster");
1792 DEFSYM (Qoutline, "outline");
1793 DEFSYM (Qunknown, "unknown");
1794
1795 /* Antialiasing. */
1796 DEFSYM (Qstandard, "standard");
1797 DEFSYM (Qsubpixel, "subpixel");
1798 DEFSYM (Qnatural, "natural");
1799
1800 /* Scripts */
1801 DEFSYM (Qlatin, "latin");
1802 DEFSYM (Qgreek, "greek");
1803 DEFSYM (Qcoptic, "coptic");
1804 DEFSYM (Qcyrillic, "cyrillic");
1805 DEFSYM (Qarmenian, "armenian");
1806 DEFSYM (Qhebrew, "hebrew");
1807 DEFSYM (Qarabic, "arabic");
1808 DEFSYM (Qsyriac, "syriac");
1809 DEFSYM (Qnko, "nko");
1810 DEFSYM (Qthaana, "thaana");
1811 DEFSYM (Qdevanagari, "devanagari");
1812 DEFSYM (Qbengali, "bengali");
1813 DEFSYM (Qgurmukhi, "gurmukhi");
1814 DEFSYM (Qgujarati, "gujarati");
1815 DEFSYM (Qoriya, "oriya");
1816 DEFSYM (Qtamil, "tamil");
1817 DEFSYM (Qtelugu, "telugu");
1818 DEFSYM (Qkannada, "kannada");
1819 DEFSYM (Qmalayalam, "malayalam");
1820 DEFSYM (Qsinhala, "sinhala");
1821 DEFSYM (Qthai, "thai");
1822 DEFSYM (Qlao, "lao");
1823 DEFSYM (Qtibetan, "tibetan");
1824 DEFSYM (Qmyanmar, "myanmar");
1825 DEFSYM (Qgeorgian, "georgian");
1826 DEFSYM (Qhangul, "hangul");
1827 DEFSYM (Qethiopic, "ethiopic");
1828 DEFSYM (Qcherokee, "cherokee");
1829 DEFSYM (Qcanadian_aboriginal, "canadian-aboriginal");
1830 DEFSYM (Qogham, "ogham");
1831 DEFSYM (Qrunic, "runic");
1832 DEFSYM (Qkhmer, "khmer");
1833 DEFSYM (Qmongolian, "mongolian");
1834 DEFSYM (Qsymbol, "symbol");
1835 DEFSYM (Qbraille, "braille");
1836 DEFSYM (Qhan, "han");
1837 DEFSYM (Qideographic_description, "ideographic-description");
1838 DEFSYM (Qcjk_misc, "cjk-misc");
1839 DEFSYM (Qkana, "kana");
1840 DEFSYM (Qbopomofo, "bopomofo");
1841 DEFSYM (Qkanbun, "kanbun");
1842 DEFSYM (Qyi, "yi");
1843 DEFSYM (Qbyzantine_musical_symbol, "byzantine-musical-symbol");
1844 DEFSYM (Qmusical_symbol, "musical-symbol");
1845 DEFSYM (Qmathematical, "mathematical");
1846
1847 w32font_driver.type = Qgdi;
1848 register_font_driver (&w32font_driver, NULL);
1849 }
1850 #endif /* USE_FONT_BACKEND */
1851
1852 /* arch-tag: 65b8a3cd-46aa-4c0d-a1f3-99e75b9c07ee
1853 (do not change this comment) */