1 /* Font backend for the Microsoft W32 Uniscribe API.
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 /* Override API version - Uniscribe is only available as standard
22 since Windows 2000, though most users of older systems will have it
23 since it installs with Internet Explorer 5.0 and other software.
24 Also, MinGW64 w32api headers by default define OPENTYPE_TAG typedef
25 only if _WIN32_WINNT >= 0x0600. We only use the affected APIs if
26 they are available, so there is no chance of calling non-existent
29 #define _WIN32_WINNT 0x0600
36 #include "composite.h"
39 #include "w32common.h"
41 struct uniscribe_font_info
43 struct w32font_info w32_font
;
47 int uniscribe_available
= 0;
49 /* EnumFontFamiliesEx callback. */
50 static int CALLBACK ALIGN_STACK
add_opentype_font_name_to_list (ENUMLOGFONTEX
*,
53 /* Used by uniscribe_otf_capability. */
54 static Lisp_Object
otf_features (HDC context
, const char *table
);
57 memq_no_quit (Lisp_Object elt
, Lisp_Object list
)
59 while (CONSP (list
) && ! EQ (XCAR (list
), elt
))
61 return (CONSP (list
));
65 /* Font backend interface implementation. */
67 uniscribe_list (struct frame
*f
, Lisp_Object font_spec
)
69 Lisp_Object fonts
= w32font_list_internal (f
, font_spec
, true);
70 FONT_ADD_LOG ("uniscribe-list", font_spec
, fonts
);
75 uniscribe_match (struct frame
*f
, Lisp_Object font_spec
)
77 Lisp_Object entity
= w32font_match_internal (f
, font_spec
, true);
78 FONT_ADD_LOG ("uniscribe-match", font_spec
, entity
);
83 uniscribe_list_family (struct frame
*f
)
85 Lisp_Object list
= Qnil
;
86 LOGFONT font_match_pattern
;
89 memset (&font_match_pattern
, 0, sizeof (font_match_pattern
));
90 /* Limit enumerated fonts to outline fonts to save time. */
91 font_match_pattern
.lfOutPrecision
= OUT_OUTLINE_PRECIS
;
93 /* Prevent quitting while EnumFontFamiliesEx runs and conses the
94 list it will return. That's because get_frame_dc acquires the
95 critical section, so we cannot quit before we release it in
97 Lisp_Object prev_quit
= Vinhibit_quit
;
99 dc
= get_frame_dc (f
);
101 EnumFontFamiliesEx (dc
, &font_match_pattern
,
102 (FONTENUMPROC
) add_opentype_font_name_to_list
,
104 release_frame_dc (f
, dc
);
105 Vinhibit_quit
= prev_quit
;
111 uniscribe_open (struct frame
*f
, Lisp_Object font_entity
, int pixel_size
)
113 Lisp_Object font_object
114 = font_make_object (VECSIZE (struct uniscribe_font_info
),
115 font_entity
, pixel_size
);
116 struct uniscribe_font_info
*uniscribe_font
117 = (struct uniscribe_font_info
*) XFONT_OBJECT (font_object
);
119 ASET (font_object
, FONT_TYPE_INDEX
, Quniscribe
);
121 if (!w32font_open_internal (f
, font_entity
, pixel_size
, font_object
))
126 /* Initialize the cache for this font. */
127 uniscribe_font
->cache
= NULL
;
129 /* Uniscribe backend uses glyph indices. */
130 uniscribe_font
->w32_font
.glyph_idx
= ETO_GLYPH_INDEX
;
132 uniscribe_font
->w32_font
.font
.driver
= &uniscribe_font_driver
;
138 uniscribe_close (struct font
*font
)
140 struct uniscribe_font_info
*uniscribe_font
141 = (struct uniscribe_font_info
*) font
;
143 if (uniscribe_font
->cache
)
144 ScriptFreeCache (&(uniscribe_font
->cache
));
146 w32font_close (font
);
149 /* Return a list describing which scripts/languages FONT supports by
150 which GSUB/GPOS features of OpenType tables.
152 Implementation note: otf_features called by this function uses
153 GetFontData to access the font tables directly, instead of using
154 ScriptGetFontScriptTags etc. APIs even if those are available. The
155 reason is that font-get, which uses the result of this function,
156 expects a cons cell (GSUB . GPOS) where the features are reported
157 separately for these 2 OTF tables, while the Uniscribe APIs report
158 the features as a single list. There doesn't seem to be a reason
159 for returning the features in 2 separate parts, except for
160 compatibility with libotf; the features are disjoint (each can
161 appear only in one of the 2 slots), and no client of this data
162 discerns between the two slots: the few that request this data all
163 look in both slots. If use of the Uniscribe APIs ever becomes
164 necessary here, and the 2 separate slots are still required, it
165 should be possible to split the feature list the APIs return into 2
166 because each sub-list is alphabetically sorted, so the place where
167 the sorting order breaks is where the GSUB features end and GPOS
168 features begin. But for now, this is not necessary, so we leave
169 the original code in place. */
171 uniscribe_otf_capability (struct font
*font
)
176 Lisp_Object capability
= Fcons (Qnil
, Qnil
);
177 Lisp_Object features
;
179 f
= XFRAME (selected_frame
);
180 context
= get_frame_dc (f
);
181 old_font
= SelectObject (context
, FONT_HANDLE (font
));
183 features
= otf_features (context
, "GSUB");
184 XSETCAR (capability
, features
);
185 features
= otf_features (context
, "GPOS");
186 XSETCDR (capability
, features
);
188 SelectObject (context
, old_font
);
189 release_frame_dc (f
, context
);
194 /* Uniscribe implementation of shape for font backend.
196 Shape text in LGSTRING. See the docstring of
197 `composition-get-gstring' for the format of LGSTRING. If the
198 (N+1)th element of LGSTRING is nil, input of shaping is from the
199 1st to (N)th elements. In each input glyph, FROM, TO, CHAR, and
200 CODE are already set.
202 This function updates all fields of the input glyphs. If the
203 output glyphs (M) are more than the input glyphs (N), (N+1)th
204 through (M)th elements of LGSTRING are updated possibly by making
205 a new glyph object and storing it in LGSTRING. If (M) is greater
206 than the length of LGSTRING, nil should be returned. In that case,
207 this function is called again with a larger LGSTRING. */
209 uniscribe_shape (Lisp_Object lgstring
)
211 struct font
*font
= CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
));
212 struct uniscribe_font_info
*uniscribe_font
213 = (struct uniscribe_font_info
*) font
;
215 int nitems
, max_items
, i
, max_glyphs
, done_glyphs
;
217 WORD
*glyphs
, *clusters
;
219 SCRIPT_VISATTR
*attributes
;
224 struct frame
* f
= NULL
;
226 HFONT old_font
= NULL
;
228 /* Get the chars from lgstring in a form we can use with uniscribe. */
229 max_glyphs
= nchars
= LGSTRING_GLYPH_LEN (lgstring
);
231 chars
= (wchar_t *) alloca (nchars
* sizeof (wchar_t));
232 /* FIXME: This loop assumes that characters in the input LGSTRING
233 are all inside the BMP. Need to encode characters beyond the BMP
235 for (i
= 0; i
< nchars
; i
++)
237 /* lgstring can be bigger than the number of characters in it, in
238 the case where more glyphs are required to display those characters.
239 If that is the case, note the real number of characters. */
240 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
243 chars
[i
] = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
246 /* First we need to break up the glyph string into runs of glyphs that
247 can be treated together. First try a single run. */
249 items
= xmalloc (sizeof (SCRIPT_ITEM
) * max_items
+ 1);
251 while ((result
= ScriptItemize (chars
, nchars
, max_items
, NULL
, NULL
,
252 items
, &nitems
)) == E_OUTOFMEMORY
)
254 /* If that wasn't enough, keep trying with one more run. */
256 items
= (SCRIPT_ITEM
*) xrealloc (items
,
257 sizeof (SCRIPT_ITEM
) * max_items
+ 1);
266 glyphs
= alloca (max_glyphs
* sizeof (WORD
));
267 clusters
= alloca (nchars
* sizeof (WORD
));
268 attributes
= alloca (max_glyphs
* sizeof (SCRIPT_VISATTR
));
269 advances
= alloca (max_glyphs
* sizeof (int));
270 offsets
= alloca (max_glyphs
* sizeof (GOFFSET
));
272 for (i
= 0; i
< nitems
; i
++)
274 int nglyphs
, nchars_in_run
;
275 nchars_in_run
= items
[i
+1].iCharPos
- items
[i
].iCharPos
;
276 /* Force ScriptShape to generate glyphs in the same order as
277 they are in the input LGSTRING, which is in the logical
279 items
[i
].a
.fLogicalOrder
= 1;
281 /* Context may be NULL here, in which case the cache should be
282 used without needing to select the font. */
283 result
= ScriptShape (context
, &(uniscribe_font
->cache
),
284 chars
+ items
[i
].iCharPos
, nchars_in_run
,
285 max_glyphs
- done_glyphs
, &(items
[i
].a
),
286 glyphs
, clusters
, attributes
, &nglyphs
);
288 if (result
== E_PENDING
&& !context
)
290 /* This assumes the selected frame is on the same display as the
291 one we are drawing. It would be better for the frame to be
293 f
= XFRAME (selected_frame
);
294 context
= get_frame_dc (f
);
295 old_font
= SelectObject (context
, FONT_HANDLE (font
));
297 result
= ScriptShape (context
, &(uniscribe_font
->cache
),
298 chars
+ items
[i
].iCharPos
, nchars_in_run
,
299 max_glyphs
- done_glyphs
, &(items
[i
].a
),
300 glyphs
, clusters
, attributes
, &nglyphs
);
303 if (result
== E_OUTOFMEMORY
)
305 /* Need a bigger lgstring. */
309 else if (FAILED (result
))
311 /* Can't shape this run - return results so far if any. */
314 else if (items
[i
].a
.fNoGlyphIndex
)
316 /* Glyph indices not supported by this font (or OS), means we
317 can't really do any meaningful shaping. */
322 result
= ScriptPlace (context
, &(uniscribe_font
->cache
),
323 glyphs
, nglyphs
, attributes
, &(items
[i
].a
),
324 advances
, offsets
, &overall_metrics
);
325 if (result
== E_PENDING
&& !context
)
327 /* Cache not complete... */
328 f
= XFRAME (selected_frame
);
329 context
= get_frame_dc (f
);
330 old_font
= SelectObject (context
, FONT_HANDLE (font
));
332 result
= ScriptPlace (context
, &(uniscribe_font
->cache
),
333 glyphs
, nglyphs
, attributes
, &(items
[i
].a
),
334 advances
, offsets
, &overall_metrics
);
336 if (SUCCEEDED (result
))
338 int j
, from
, to
, adj_offset
= 0;
343 for (j
= 0; j
< nglyphs
; j
++)
345 int lglyph_index
= j
+ done_glyphs
;
346 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, lglyph_index
);
352 lglyph
= LGLYPH_NEW ();
353 LGSTRING_SET_GLYPH (lgstring
, lglyph_index
, lglyph
);
355 /* Copy to a 32-bit data type to shut up the
356 compiler warning in LGLYPH_SET_CODE about
357 comparison being always false. */
359 LGLYPH_SET_CODE (lglyph
, gl
);
361 /* Detect clusters, for linking codes back to
363 if (attributes
[j
].fClusterStart
)
365 while (from
< nchars_in_run
&& clusters
[from
] < j
)
367 if (from
>= nchars_in_run
)
368 from
= to
= nchars_in_run
- 1;
372 to
= nchars_in_run
- 1;
373 for (k
= from
+ 1; k
< nchars_in_run
; k
++)
383 /* For RTL text, the Uniscribe shaper prepares
384 the values in ADVANCES array for layout in
385 reverse order, whereby "advance width" is
386 applied to move the pen in reverse direction
387 and _before_ drawing the glyph. Since we
388 draw glyphs in their normal left-to-right
389 order, we need to adjust the coordinates of
390 each non-base glyph in a grapheme cluster via
391 X-OFF component of the gstring's ADJUSTMENT
392 sub-vector. This loop computes, for each
393 grapheme cluster, the initial value of the
394 adjustment for the base character, which is
395 then updated for each successive glyph in the
402 while (j1
< nglyphs
&& !attributes
[j1
].fClusterStart
)
404 adj_offset
+= advances
[j1
];
410 LGLYPH_SET_CHAR (lglyph
, chars
[items
[i
].iCharPos
412 LGLYPH_SET_FROM (lglyph
, items
[i
].iCharPos
+ from
);
413 LGLYPH_SET_TO (lglyph
, items
[i
].iCharPos
+ to
);
416 LGLYPH_SET_WIDTH (lglyph
, advances
[j
]);
417 LGLYPH_SET_ASCENT (lglyph
, font
->ascent
);
418 LGLYPH_SET_DESCENT (lglyph
, font
->descent
);
420 result
= ScriptGetGlyphABCWidth (context
,
421 &(uniscribe_font
->cache
),
422 glyphs
[j
], &char_metric
);
423 if (result
== E_PENDING
&& !context
)
425 /* Cache incomplete... */
426 f
= XFRAME (selected_frame
);
427 context
= get_frame_dc (f
);
428 old_font
= SelectObject (context
, FONT_HANDLE (font
));
429 result
= ScriptGetGlyphABCWidth (context
,
430 &(uniscribe_font
->cache
),
431 glyphs
[j
], &char_metric
);
434 if (SUCCEEDED (result
))
436 int lbearing
= char_metric
.abcA
;
437 int rbearing
= char_metric
.abcA
+ char_metric
.abcB
;
439 LGLYPH_SET_LBEARING (lglyph
, lbearing
);
440 LGLYPH_SET_RBEARING (lglyph
, rbearing
);
444 LGLYPH_SET_LBEARING (lglyph
, 0);
445 LGLYPH_SET_RBEARING (lglyph
, advances
[j
]);
448 if (offsets
[j
].du
|| offsets
[j
].dv
449 /* For non-base glyphs of RTL grapheme clusters,
450 adjust the X offset even if both DU and DV
452 || (!attributes
[j
].fClusterStart
&& items
[i
].a
.fRTL
))
454 Lisp_Object vec
= make_uninit_vector (3);
458 /* Empirically, it looks like Uniscribe
459 interprets DU in reverse direction for
460 RTL clusters. E.g., if we don't reverse
461 the direction, the Hebrew point HOLAM is
462 drawn above the right edge of the base
463 consonant, instead of above the left edge. */
464 ASET (vec
, 0, make_fixnum (-offsets
[j
].du
466 /* Update the adjustment value for the width
467 advance of the glyph we just emitted. */
468 adj_offset
-= 2 * advances
[j
];
471 ASET (vec
, 0, make_fixnum (offsets
[j
].du
+ adj_offset
));
472 /* In the font definition coordinate system, the
473 Y coordinate points up, while in our screen
474 coordinates Y grows downwards. So we need to
475 reverse the sign of Y-OFFSET here. */
476 ASET (vec
, 1, make_fixnum (-offsets
[j
].dv
));
477 /* Based on what ftfont.c does... */
478 ASET (vec
, 2, make_fixnum (advances
[j
]));
479 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
483 LGLYPH_SET_ADJUSTMENT (lglyph
, Qnil
);
484 /* Update the adjustment value to compensate for
485 the width of the base character. */
487 adj_offset
-= advances
[j
];
492 done_glyphs
+= nglyphs
;
499 SelectObject (context
, old_font
);
500 release_frame_dc (f
, context
);
506 return make_fixnum (done_glyphs
);
509 /* Uniscribe implementation of encode_char for font backend.
510 Return a glyph code of FONT for character C (Unicode code point).
511 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
513 uniscribe_encode_char (struct font
*font
, int c
)
516 struct frame
*f
= NULL
;
517 HFONT old_font
= NULL
;
518 unsigned code
= FONT_INVALID_CODE
;
523 struct uniscribe_font_info
*uniscribe_font
524 = (struct uniscribe_font_info
*)font
;
533 DWORD surrogate
= c
- 0x10000;
535 /* High surrogate: U+D800 - U+DBFF. */
536 ch
[0] = 0xD800 + ((surrogate
>> 10) & 0x03FF);
537 /* Low surrogate: U+DC00 - U+DFFF. */
538 ch
[1] = 0xDC00 + (surrogate
& 0x03FF);
542 /* Non BMP characters must be handled by the uniscribe shaping
543 engine as GDI functions (except blindly displaying lines of
544 Unicode text) and the promising looking ScriptGetCMap do not
545 convert surrogate pairs to glyph indexes correctly. */
547 items
= (SCRIPT_ITEM
*) alloca (sizeof (SCRIPT_ITEM
) * 2 + 1);
548 if (SUCCEEDED (ScriptItemize (ch
, len
, 2, NULL
, NULL
, items
, &nitems
)))
551 /* Surrogates seem to need 2 here, even though only one glyph is
552 returned. Indic characters can also produce 2 or more glyphs for
553 a single code point, but they need to use uniscribe_shape
554 above for correct display. */
555 WORD glyphs
[2], clusters
[2];
556 SCRIPT_VISATTR attrs
[2];
559 /* Force ScriptShape to generate glyphs in the logical
561 items
[0].a
.fLogicalOrder
= 1;
563 result
= ScriptShape (context
, &(uniscribe_font
->cache
),
564 ch
, len
, 2, &(items
[0].a
),
565 glyphs
, clusters
, attrs
, &nglyphs
);
567 if (result
== E_PENDING
)
569 /* Use selected frame until API is updated to pass
571 f
= XFRAME (selected_frame
);
572 context
= get_frame_dc (f
);
573 old_font
= SelectObject (context
, FONT_HANDLE (font
));
574 result
= ScriptShape (context
, &(uniscribe_font
->cache
),
575 ch
, len
, 2, &(items
[0].a
),
576 glyphs
, clusters
, attrs
, &nglyphs
);
579 if (SUCCEEDED (result
) && nglyphs
== 1)
581 /* Some fonts return .notdef glyphs instead of failing.
582 (TrueType spec reserves glyph code 0 for .notdef) */
586 else if (SUCCEEDED (result
) || result
== E_OUTOFMEMORY
)
588 /* This character produces zero or more than one glyph
589 when shaped. But we still need the return from here
590 to be valid for the shaping engine to be invoked
592 result
= ScriptGetCMap (context
, &(uniscribe_font
->cache
),
594 if (SUCCEEDED (result
) && glyphs
[0])
601 SelectObject (context
, old_font
);
602 release_frame_dc (f
, context
);
610 Lisp_Object uniscribe_get_cache (Lisp_Object frame);
611 void uniscribe_free_entity (Lisp_Object font_entity);
612 int uniscribe_has_char (Lisp_Object entity, int c);
613 void uniscribe_text_extents (struct font *font, unsigned *code,
614 int nglyphs, struct font_metrics *metrics);
615 int uniscribe_draw (struct glyph_string *s, int from, int to,
616 int x, int y, int with_background);
619 int uniscribe_prepare_face (struct frame *f, struct face *face);
620 void uniscribe_done_face (struct frame *f, struct face *face);
621 int uniscribe_get_bitmap (struct font *font, unsigned code,
622 struct font_bitmap *bitmap, int bits_per_pixel);
623 void uniscribe_free_bitmap (struct font *font, struct font_bitmap *bitmap);
624 int uniscribe_anchor_point (struct font *font, unsigned code,
625 int index, int *x, int *y);
626 int uniscribe_start_for_frame (struct frame *f);
627 int uniscribe_end_for_frame (struct frame *f);
632 /* Callback function for EnumFontFamiliesEx.
633 Adds the name of opentype fonts to a Lisp list (passed in as the
635 static int CALLBACK ALIGN_STACK
636 add_opentype_font_name_to_list (ENUMLOGFONTEX
*logical_font
,
637 NEWTEXTMETRICEX
*physical_font
,
638 DWORD font_type
, LPARAM list_object
)
640 Lisp_Object
* list
= (Lisp_Object
*) list_object
;
643 /* Skip vertical fonts (intended only for printing) */
644 if (logical_font
->elfLogFont
.lfFaceName
[0] == '@')
647 /* Skip non opentype fonts. Count old truetype fonts as opentype,
648 as some of them do contain GPOS and GSUB data that Uniscribe
650 if (!(physical_font
->ntmTm
.ntmFlags
& NTMFLAGS_OPENTYPE
)
651 && font_type
!= TRUETYPE_FONTTYPE
)
654 /* Skip fonts that have no Unicode coverage. */
655 if (!physical_font
->ntmFontSig
.fsUsb
[3]
656 && !physical_font
->ntmFontSig
.fsUsb
[2]
657 && !physical_font
->ntmFontSig
.fsUsb
[1]
658 && !(physical_font
->ntmFontSig
.fsUsb
[0] & 0x3fffffff))
661 family
= intern_font_name (logical_font
->elfLogFont
.lfFaceName
);
662 if (! memq_no_quit (family
, *list
))
663 *list
= Fcons (family
, *list
);
669 /* :otf property handling.
670 Since the necessary Uniscribe APIs for getting font tag information
671 are only available in Vista, we may need to parse the font data directly
672 according to the OpenType Specification. */
674 /* Push into DWORD backwards to cope with endianness. */
675 #define OTF_TAG(STR) \
676 ((STR[3] << 24) | (STR[2] << 16) | (STR[1] << 8) | STR[0])
678 #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \
680 BYTE temp, data[2]; \
681 if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \
682 goto font_table_error; \
683 temp = data[0], data[0] = data[1], data[1] = temp; \
684 memcpy (PTR, data, 2); \
687 /* Do not reverse the bytes, because we will compare with a OTF_TAG value
688 that has them reversed already. */
689 #define OTF_DWORDTAG_VAL(TABLE, OFFSET, PTR) \
691 if (GetFontData (context, TABLE, OFFSET, PTR, 4) != 4) \
692 goto font_table_error; \
695 #define OTF_TAG_VAL(TABLE, OFFSET, STR) \
697 if (GetFontData (context, TABLE, OFFSET, STR, 4) != 4) \
698 goto font_table_error; \
702 #define SNAME(VAL) SSDATA (SYMBOL_NAME (VAL))
704 /* Uniscribe APIs available only since Windows Vista. */
705 typedef HRESULT (WINAPI
*ScriptGetFontScriptTags_Proc
)
706 (HDC
, SCRIPT_CACHE
*, SCRIPT_ANALYSIS
*, int, OPENTYPE_TAG
*, int *);
708 typedef HRESULT (WINAPI
*ScriptGetFontLanguageTags_Proc
)
709 (HDC
, SCRIPT_CACHE
*, SCRIPT_ANALYSIS
*, OPENTYPE_TAG
, int, OPENTYPE_TAG
*, int *);
711 typedef HRESULT (WINAPI
*ScriptGetFontFeatureTags_Proc
)
712 (HDC
, SCRIPT_CACHE
*, SCRIPT_ANALYSIS
*, OPENTYPE_TAG
, OPENTYPE_TAG
, int, OPENTYPE_TAG
*, int *);
714 ScriptGetFontScriptTags_Proc script_get_font_scripts_fn
;
715 ScriptGetFontLanguageTags_Proc script_get_font_languages_fn
;
716 ScriptGetFontFeatureTags_Proc script_get_font_features_fn
;
718 static bool uniscribe_new_apis
;
720 /* Verify that all the required features in FEATURES, each of whose
721 elements is a list or nil, can be found among the N feature tags in
722 FTAGS. Return 'true' if the required features are supported,
723 'false' if not. Each list in FEATURES can include an element of
724 nil, which means all the elements after it must not be in FTAGS. */
726 uniscribe_check_features (Lisp_Object features
[2], OPENTYPE_TAG
*ftags
, int n
)
730 for (j
= 0; j
< 2; j
++)
732 bool negative
= false;
735 for (rest
= features
[j
]; CONSP (rest
); rest
= XCDR (rest
))
737 Lisp_Object feature
= XCAR (rest
);
739 /* The font must NOT have any of the features after nil.
740 See the doc string of 'font-spec', under ':otf'. */
745 OPENTYPE_TAG feature_tag
= OTF_TAG (SNAME (feature
));
748 for (i
= 0; i
< n
; i
++)
750 if (ftags
[i
] == feature_tag
)
752 /* Test fails if we find a feature that the font
760 /* Test fails if we do NOT find a feature that the font
762 if (i
>= n
&& !negative
)
771 /* Check if font supports the required OTF script/language/features
772 using the Unsicribe APIs available since Windows Vista. We prefer
773 these APIs as a kind of future-proofing Emacs: they seem to
774 retrieve script tags that the old code (and also libotf) doesn't
775 seem to be able to get, e.g., some fonts that claim support for
776 "dev2" script don't show "deva", but the new APIs do report it. */
778 uniscribe_check_otf_1 (HDC context
, Lisp_Object script
, Lisp_Object lang
,
779 Lisp_Object features
[2], int *retval
)
781 SCRIPT_CACHE cache
= NULL
;
782 OPENTYPE_TAG tags
[32], script_tag
, lang_tag
;
783 int max_tags
= ARRAYELTS (tags
);
784 int ntags
, i
, ret
= 0;
789 rslt
= script_get_font_scripts_fn (context
, &cache
, NULL
, max_tags
,
793 DebPrint (("ScriptGetFontScriptTags failed with 0x%x\n", rslt
));
798 script_tag
= OTF_TAG ("DFLT");
800 script_tag
= OTF_TAG (SNAME (script
));
801 for (i
= 0; i
< ntags
; i
++)
802 if (tags
[i
] == script_tag
)
809 lang_tag
= OTF_TAG ("dflt");
812 rslt
= script_get_font_languages_fn (context
, &cache
, NULL
, script_tag
,
813 max_tags
, tags
, &ntags
);
816 DebPrint (("ScriptGetFontLanguageTags failed with 0x%x\n", rslt
));
821 lang_tag
= OTF_TAG ("dflt");
824 lang_tag
= OTF_TAG (SNAME (lang
));
825 for (i
= 0; i
< ntags
; i
++)
826 if (tags
[i
] == lang_tag
)
834 if (!NILP (features
[0]))
836 /* Are the 2 feature lists valid? */
837 if (!CONSP (features
[0])
838 || (!NILP (features
[1]) && !CONSP (features
[1])))
840 rslt
= script_get_font_features_fn (context
, &cache
, NULL
,
841 script_tag
, lang_tag
,
842 max_tags
, tags
, &ntags
);
845 DebPrint (("ScriptGetFontFeatureTags failed with 0x%x\n", rslt
));
850 /* ScriptGetFontFeatureTags doesn't let us query features
851 separately for GSUB and GPOS, so we check them all together.
852 It doesn't really matter, since the features in GSUB and GPOS
853 are disjoint, i.e. no feature can appear in both tables. */
854 if (!uniscribe_check_features (features
, tags
, ntags
))
863 ScriptFreeCache (&cache
);
867 /* Check if font supports the otf script/language/features specified.
868 OTF_SPEC is in the format
869 (script lang [(gsub_feature ...)|nil] [(gpos_feature ...)]?) */
871 uniscribe_check_otf (LOGFONT
*font
, Lisp_Object otf_spec
)
873 Lisp_Object script
, lang
, rest
;
874 Lisp_Object features
[2];
875 DWORD feature_tables
[2];
876 DWORD script_tag
, default_script
, lang_tag
= 0;
879 HFONT check_font
, old_font
;
882 /* Check the spec is in the right format. */
883 if (!CONSP (otf_spec
) || XFIXNUM (Flength (otf_spec
)) < 3)
886 /* Break otf_spec into its components. */
887 script
= XCAR (otf_spec
);
888 rest
= XCDR (otf_spec
);
893 features
[0] = XCAR (rest
);
898 features
[1] = XCAR (rest
);
900 /* Set up graphics context so we can use the font. */
901 f
= XFRAME (selected_frame
);
902 context
= get_frame_dc (f
);
903 check_font
= CreateFontIndirect (font
);
904 old_font
= SelectObject (context
, check_font
);
906 /* If we are on Vista or later, use the new APIs. */
907 if (uniscribe_new_apis
908 && !w32_disable_new_uniscribe_apis
909 && uniscribe_check_otf_1 (context
, script
, lang
, features
, &retval
) != -1)
912 /* Set up tags we will use in the search. */
913 feature_tables
[0] = OTF_TAG ("GSUB");
914 feature_tables
[1] = OTF_TAG ("GPOS");
915 default_script
= OTF_TAG ("DFLT");
917 script_tag
= default_script
;
919 script_tag
= OTF_TAG (SNAME (script
));
921 lang_tag
= OTF_TAG (SNAME (lang
));
923 /* Scan GSUB and GPOS tables. */
924 for (i
= 0; i
< 2; i
++)
926 int j
, n_match_features
;
927 unsigned short scriptlist_table
, feature_table
, n_scripts
;
928 unsigned short script_table
, langsys_table
, n_langs
;
929 unsigned short feature_index
, n_features
;
930 DWORD tbl
= feature_tables
[i
];
931 DWORD feature_id
, *ftags
;
932 Lisp_Object farray
[2];
934 /* Skip if no features requested from this table. */
935 if (NILP (features
[i
]))
938 /* If features is not a cons, this font spec is messed up. */
939 if (!CONSP (features
[i
]))
942 /* Read GPOS/GSUB header. */
943 OTF_INT16_VAL (tbl
, 4, &scriptlist_table
);
944 OTF_INT16_VAL (tbl
, 6, &feature_table
);
945 OTF_INT16_VAL (tbl
, scriptlist_table
, &n_scripts
);
947 /* Find the appropriate script table. */
949 for (j
= 0; j
< n_scripts
; j
++)
952 OTF_DWORDTAG_VAL (tbl
, scriptlist_table
+ 2 + j
* 6, &script_id
);
953 if (script_id
== script_tag
)
955 OTF_INT16_VAL (tbl
, scriptlist_table
+ 6 + j
* 6, &script_table
);
958 #if 0 /* Causes false positives. */
959 /* If there is a DFLT script defined in the font, use it
960 if the specified script is not found. */
961 else if (script_id
== default_script
)
962 OTF_INT16_VAL (tbl
, scriptlist_table
+ 6 + j
* 6, &script_table
);
965 /* If no specific or default script table was found, then this font
966 does not support the script. */
970 /* Offset is from beginning of scriptlist_table. */
971 script_table
+= scriptlist_table
;
973 /* Get default langsys table. */
974 OTF_INT16_VAL (tbl
, script_table
, &langsys_table
);
976 /* If lang was specified, see if font contains a specific entry. */
979 OTF_INT16_VAL (tbl
, script_table
+ 2, &n_langs
);
981 for (j
= 0; j
< n_langs
; j
++)
984 OTF_DWORDTAG_VAL (tbl
, script_table
+ 4 + j
* 6, &lang_id
);
985 if (lang_id
== lang_tag
)
987 OTF_INT16_VAL (tbl
, script_table
+ 8 + j
* 6, &langsys_table
);
996 /* Offset is from beginning of script table. */
997 langsys_table
+= script_table
;
999 /* If there are no features to check, skip checking. */
1000 if (NILP (features
[i
]))
1002 if (!CONSP (features
[i
]))
1005 n_match_features
= 0;
1007 /* First get required feature (if any). */
1008 OTF_INT16_VAL (tbl
, langsys_table
+ 2, &feature_index
);
1009 if (feature_index
!= 0xFFFF)
1010 n_match_features
= 1;
1011 OTF_INT16_VAL (tbl
, langsys_table
+ 4, &n_features
);
1012 n_match_features
+= n_features
;
1014 SAFE_NALLOCA (ftags
, 1, n_match_features
);
1016 if (feature_index
!= 0xFFFF)
1018 OTF_DWORDTAG_VAL (tbl
, feature_table
+ 2 + feature_index
* 6,
1020 ftags
[k
++] = feature_id
;
1022 /* Now get all the other features. */
1023 for (j
= 0; j
< n_features
; j
++)
1025 OTF_INT16_VAL (tbl
, langsys_table
+ 6 + j
* 2, &feature_index
);
1026 OTF_DWORDTAG_VAL (tbl
, feature_table
+ 2 + feature_index
* 6,
1028 ftags
[k
++] = feature_id
;
1031 /* Check the features for this table. */
1032 farray
[0] = features
[i
];
1034 if (!uniscribe_check_features (farray
, ftags
, n_match_features
))
1044 /* restore graphics context. */
1045 SelectObject (context
, old_font
);
1046 DeleteObject (check_font
);
1047 release_frame_dc (f
, context
);
1053 otf_features (HDC context
, const char *table
)
1055 Lisp_Object script_list
= Qnil
;
1056 unsigned short scriptlist_table
, n_scripts
, feature_table
;
1057 DWORD tbl
= OTF_TAG (table
);
1060 /* Look for scripts in the table. */
1061 OTF_INT16_VAL (tbl
, 4, &scriptlist_table
);
1062 OTF_INT16_VAL (tbl
, 6, &feature_table
);
1063 OTF_INT16_VAL (tbl
, scriptlist_table
, &n_scripts
);
1065 for (i
= n_scripts
- 1; i
>= 0; i
--)
1067 char script
[5], lang
[5];
1068 unsigned short script_table
, lang_count
, langsys_table
, feature_count
;
1069 Lisp_Object script_tag
, langsys_list
, langsys_tag
, feature_list
;
1070 unsigned short record_offset
= scriptlist_table
+ 2 + i
* 6;
1071 OTF_TAG_VAL (tbl
, record_offset
, script
);
1072 OTF_INT16_VAL (tbl
, record_offset
+ 4, &script_table
);
1074 /* Offset is from beginning of script table. */
1075 script_table
+= scriptlist_table
;
1077 script_tag
= intern (script
);
1078 langsys_list
= Qnil
;
1080 /* Optional default lang. */
1081 OTF_INT16_VAL (tbl
, script_table
, &langsys_table
);
1084 /* Offset is from beginning of script table. */
1085 langsys_table
+= script_table
;
1088 feature_list
= Qnil
;
1089 OTF_INT16_VAL (tbl
, langsys_table
+ 4, &feature_count
);
1090 for (k
= feature_count
- 1; k
>= 0; k
--)
1093 unsigned short index
;
1094 OTF_INT16_VAL (tbl
, langsys_table
+ 6 + k
* 2, &index
);
1095 OTF_TAG_VAL (tbl
, feature_table
+ 2 + index
* 6, feature
);
1096 feature_list
= Fcons (intern (feature
), feature_list
);
1098 langsys_list
= Fcons (Fcons (langsys_tag
, feature_list
),
1102 /* List of supported languages. */
1103 OTF_INT16_VAL (tbl
, script_table
+ 2, &lang_count
);
1105 for (j
= lang_count
- 1; j
>= 0; j
--)
1107 record_offset
= script_table
+ 4 + j
* 6;
1108 OTF_TAG_VAL (tbl
, record_offset
, lang
);
1109 OTF_INT16_VAL (tbl
, record_offset
+ 4, &langsys_table
);
1111 /* Offset is from beginning of script table. */
1112 langsys_table
+= script_table
;
1114 langsys_tag
= intern (lang
);
1115 feature_list
= Qnil
;
1116 OTF_INT16_VAL (tbl
, langsys_table
+ 4, &feature_count
);
1117 for (k
= feature_count
- 1; k
>= 0; k
--)
1120 unsigned short index
;
1121 OTF_INT16_VAL (tbl
, langsys_table
+ 6 + k
* 2, &index
);
1122 OTF_TAG_VAL (tbl
, feature_table
+ 2 + index
* 6, feature
);
1123 feature_list
= Fcons (intern (feature
), feature_list
);
1125 langsys_list
= Fcons (Fcons (langsys_tag
, feature_list
),
1130 script_list
= Fcons (Fcons (script_tag
, langsys_list
), script_list
);
1139 #undef OTF_INT16_VAL
1144 struct font_driver uniscribe_font_driver
=
1146 LISPSYM_INITIALLY (Quniscribe
),
1147 0, /* case insensitive */
1151 uniscribe_list_family
,
1152 NULL
, /* free_entity */
1155 NULL
, /* prepare_face */
1156 NULL
, /* done_face */
1158 uniscribe_encode_char
,
1159 w32font_text_extents
,
1161 NULL
, /* get_bitmap */
1162 NULL
, /* free_bitmap */
1163 NULL
, /* anchor_point */
1164 uniscribe_otf_capability
, /* Defined so (font-get FONTOBJ :otf) works. */
1165 NULL
, /* otf_drive - use shape instead. */
1166 NULL
, /* start_for_frame */
1167 NULL
, /* end_for_frame */
1170 NULL
, /* get_variation_glyphs */
1171 NULL
, /* filter_properties */
1172 NULL
, /* cached_font_ok */
1175 /* Note that this should be called at every startup, not just when dumping,
1176 as it needs to test for the existence of the Uniscribe library. */
1177 void syms_of_w32uniscribe (void);
1180 syms_of_w32uniscribe (void)
1184 /* Don't init uniscribe when dumping */
1188 /* Don't register if uniscribe is not available. */
1189 uniscribe
= GetModuleHandle ("usp10");
1193 uniscribe_available
= 1;
1195 register_font_driver (&uniscribe_font_driver
, NULL
);
1197 script_get_font_scripts_fn
= (ScriptGetFontScriptTags_Proc
)
1198 get_proc_addr (uniscribe
, "ScriptGetFontScriptTags");
1199 script_get_font_languages_fn
= (ScriptGetFontLanguageTags_Proc
)
1200 get_proc_addr (uniscribe
, "ScriptGetFontLanguageTags");
1201 script_get_font_features_fn
= (ScriptGetFontFeatureTags_Proc
)
1202 get_proc_addr (uniscribe
, "ScriptGetFontFeatureTags");
1203 if (script_get_font_scripts_fn
1204 && script_get_font_languages_fn
1205 && script_get_font_features_fn
)
1206 uniscribe_new_apis
= true;
1208 uniscribe_new_apis
= false;