Undo port to hypothetical nonzero Qnil case
[emacs.git] / src / w32uniscribe.c
blob9cd97e28616c0fab21e1399f07ab39febd682568
1 /* Font backend for the Microsoft W32 Uniscribe API.
2 Copyright (C) 2008-2015 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
9 (at 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 <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 /* Override API version - Uniscribe is only available as standard since
22 Windows 2000, though most users of older systems will have it
23 since it installs with Internet Explorer 5.0 and other software.
24 We only enable the feature if it is available, so there is no chance
25 of calling non-existent functions. */
26 #undef _WIN32_WINNT
27 #define _WIN32_WINNT 0x500
28 #include <windows.h>
29 #include <usp10.h>
31 #include "lisp.h"
32 #include "w32term.h"
33 #include "frame.h"
34 #include "dispextern.h"
35 #include "character.h"
36 #include "charset.h"
37 #include "composite.h"
38 #include "fontset.h"
39 #include "font.h"
40 #include "w32font.h"
42 struct uniscribe_font_info
44 struct w32font_info w32_font;
45 SCRIPT_CACHE cache;
48 int uniscribe_available = 0;
50 /* EnumFontFamiliesEx callback. */
51 static int CALLBACK ALIGN_STACK add_opentype_font_name_to_list (ENUMLOGFONTEX *,
52 NEWTEXTMETRICEX *,
53 DWORD, LPARAM);
54 /* Used by uniscribe_otf_capability. */
55 static Lisp_Object otf_features (HDC context, char *table);
57 static int
58 memq_no_quit (Lisp_Object elt, Lisp_Object list)
60 while (CONSP (list) && ! EQ (XCAR (list), elt))
61 list = XCDR (list);
62 return (CONSP (list));
66 /* Font backend interface implementation. */
67 static Lisp_Object
68 uniscribe_list (struct frame *f, Lisp_Object font_spec)
70 Lisp_Object fonts = w32font_list_internal (f, font_spec, 1);
71 FONT_ADD_LOG ("uniscribe-list", font_spec, fonts);
72 return fonts;
75 static Lisp_Object
76 uniscribe_match (struct frame *f, Lisp_Object font_spec)
78 Lisp_Object entity = w32font_match_internal (f, font_spec, 1);
79 FONT_ADD_LOG ("uniscribe-match", font_spec, entity);
80 return entity;
83 static Lisp_Object
84 uniscribe_list_family (struct frame *f)
86 Lisp_Object list = Qnil;
87 LOGFONT font_match_pattern;
88 HDC dc;
90 memset (&font_match_pattern, 0, sizeof (font_match_pattern));
91 /* Limit enumerated fonts to outline fonts to save time. */
92 font_match_pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
94 dc = get_frame_dc (f);
96 EnumFontFamiliesEx (dc, &font_match_pattern,
97 (FONTENUMPROC) add_opentype_font_name_to_list,
98 (LPARAM) &list, 0);
99 release_frame_dc (f, dc);
101 return list;
104 static Lisp_Object
105 uniscribe_open (struct frame *f, Lisp_Object font_entity, int pixel_size)
107 Lisp_Object font_object
108 = font_make_object (VECSIZE (struct uniscribe_font_info),
109 font_entity, pixel_size);
110 struct uniscribe_font_info *uniscribe_font
111 = (struct uniscribe_font_info *) XFONT_OBJECT (font_object);
113 ASET (font_object, FONT_TYPE_INDEX, Quniscribe);
115 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
117 return Qnil;
120 /* Initialize the cache for this font. */
121 uniscribe_font->cache = NULL;
123 /* Uniscribe backend uses glyph indices. */
124 uniscribe_font->w32_font.glyph_idx = ETO_GLYPH_INDEX;
126 uniscribe_font->w32_font.font.driver = &uniscribe_font_driver;
128 return font_object;
131 static void
132 uniscribe_close (struct font *font)
134 struct uniscribe_font_info *uniscribe_font
135 = (struct uniscribe_font_info *) font;
137 if (uniscribe_font->cache)
138 ScriptFreeCache (&(uniscribe_font->cache));
140 w32font_close (font);
143 /* Return a list describing which scripts/languages FONT supports by
144 which GSUB/GPOS features of OpenType tables. */
145 static Lisp_Object
146 uniscribe_otf_capability (struct font *font)
148 HDC context;
149 HFONT old_font;
150 struct frame *f;
151 Lisp_Object capability = Fcons (Qnil, Qnil);
152 Lisp_Object features;
154 f = XFRAME (selected_frame);
155 context = get_frame_dc (f);
156 old_font = SelectObject (context, FONT_HANDLE (font));
158 features = otf_features (context, "GSUB");
159 XSETCAR (capability, features);
160 features = otf_features (context, "GPOS");
161 XSETCDR (capability, features);
163 SelectObject (context, old_font);
164 release_frame_dc (f, context);
166 return capability;
169 /* Uniscribe implementation of shape for font backend.
171 Shape text in LGSTRING. See the docstring of
172 `composition-get-gstring' for the format of LGSTRING. If the
173 (N+1)th element of LGSTRING is nil, input of shaping is from the
174 1st to (N)th elements. In each input glyph, FROM, TO, CHAR, and
175 CODE are already set.
177 This function updates all fields of the input glyphs. If the
178 output glyphs (M) are more than the input glyphs (N), (N+1)th
179 through (M)th elements of LGSTRING are updated possibly by making
180 a new glyph object and storing it in LGSTRING. If (M) is greater
181 than the length of LGSTRING, nil should be returned. In that case,
182 this function is called again with a larger LGSTRING. */
183 static Lisp_Object
184 uniscribe_shape (Lisp_Object lgstring)
186 struct font *font = CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring));
187 struct uniscribe_font_info *uniscribe_font
188 = (struct uniscribe_font_info *) font;
189 EMACS_UINT nchars;
190 int nitems, max_items, i, max_glyphs, done_glyphs;
191 wchar_t *chars;
192 WORD *glyphs, *clusters;
193 SCRIPT_ITEM *items;
194 SCRIPT_VISATTR *attributes;
195 int *advances;
196 GOFFSET *offsets;
197 ABC overall_metrics;
198 HRESULT result;
199 struct frame * f = NULL;
200 HDC context = NULL;
201 HFONT old_font = NULL;
203 /* Get the chars from lgstring in a form we can use with uniscribe. */
204 max_glyphs = nchars = LGSTRING_GLYPH_LEN (lgstring);
205 done_glyphs = 0;
206 chars = (wchar_t *) alloca (nchars * sizeof (wchar_t));
207 /* FIXME: This loop assumes that characters in the input LGSTRING
208 are all inside the BMP. Need to encode characters beyond the BMP
209 as UTF-16. */
210 for (i = 0; i < nchars; i++)
212 /* lgstring can be bigger than the number of characters in it, in
213 the case where more glyphs are required to display those characters.
214 If that is the case, note the real number of characters. */
215 if (NILP (LGSTRING_GLYPH (lgstring, i)))
216 nchars = i;
217 else
218 chars[i] = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
221 /* First we need to break up the glyph string into runs of glyphs that
222 can be treated together. First try a single run. */
223 max_items = 2;
224 items = xmalloc (sizeof (SCRIPT_ITEM) * max_items + 1);
226 while ((result = ScriptItemize (chars, nchars, max_items, NULL, NULL,
227 items, &nitems)) == E_OUTOFMEMORY)
229 /* If that wasn't enough, keep trying with one more run. */
230 max_items++;
231 items = (SCRIPT_ITEM *) xrealloc (items,
232 sizeof (SCRIPT_ITEM) * max_items + 1);
235 if (FAILED (result))
237 xfree (items);
238 return Qnil;
241 glyphs = alloca (max_glyphs * sizeof (WORD));
242 clusters = alloca (nchars * sizeof (WORD));
243 attributes = alloca (max_glyphs * sizeof (SCRIPT_VISATTR));
244 advances = alloca (max_glyphs * sizeof (int));
245 offsets = alloca (max_glyphs * sizeof (GOFFSET));
247 for (i = 0; i < nitems; i++)
249 int nglyphs, nchars_in_run;
250 nchars_in_run = items[i+1].iCharPos - items[i].iCharPos;
251 /* Force ScriptShape to generate glyphs in the same order as
252 they are in the input LGSTRING, which is in the logical
253 order. */
254 items[i].a.fLogicalOrder = 1;
256 /* Context may be NULL here, in which case the cache should be
257 used without needing to select the font. */
258 result = ScriptShape (context, &(uniscribe_font->cache),
259 chars + items[i].iCharPos, nchars_in_run,
260 max_glyphs - done_glyphs, &(items[i].a),
261 glyphs, clusters, attributes, &nglyphs);
263 if (result == E_PENDING && !context)
265 /* This assumes the selected frame is on the same display as the
266 one we are drawing. It would be better for the frame to be
267 passed in. */
268 f = XFRAME (selected_frame);
269 context = get_frame_dc (f);
270 old_font = SelectObject (context, FONT_HANDLE (font));
272 result = ScriptShape (context, &(uniscribe_font->cache),
273 chars + items[i].iCharPos, nchars_in_run,
274 max_glyphs - done_glyphs, &(items[i].a),
275 glyphs, clusters, attributes, &nglyphs);
278 if (result == E_OUTOFMEMORY)
280 /* Need a bigger lgstring. */
281 lgstring = Qnil;
282 break;
284 else if (FAILED (result))
286 /* Can't shape this run - return results so far if any. */
287 break;
289 else if (items[i].a.fNoGlyphIndex)
291 /* Glyph indices not supported by this font (or OS), means we
292 can't really do any meaningful shaping. */
293 break;
295 else
297 result = ScriptPlace (context, &(uniscribe_font->cache),
298 glyphs, nglyphs, attributes, &(items[i].a),
299 advances, offsets, &overall_metrics);
300 if (result == E_PENDING && !context)
302 /* Cache not complete... */
303 f = XFRAME (selected_frame);
304 context = get_frame_dc (f);
305 old_font = SelectObject (context, FONT_HANDLE (font));
307 result = ScriptPlace (context, &(uniscribe_font->cache),
308 glyphs, nglyphs, attributes, &(items[i].a),
309 advances, offsets, &overall_metrics);
311 if (SUCCEEDED (result))
313 int j, from, to, adj_offset = 0;
315 from = 0;
316 to = from;
318 for (j = 0; j < nglyphs; j++)
320 int lglyph_index = j + done_glyphs;
321 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, lglyph_index);
322 ABC char_metric;
323 unsigned gl;
325 if (NILP (lglyph))
327 lglyph = LGLYPH_NEW ();
328 LGSTRING_SET_GLYPH (lgstring, lglyph_index, lglyph);
330 /* Copy to a 32-bit data type to shut up the
331 compiler warning in LGLYPH_SET_CODE about
332 comparison being always false. */
333 gl = glyphs[j];
334 LGLYPH_SET_CODE (lglyph, gl);
336 /* Detect clusters, for linking codes back to
337 characters. */
338 if (attributes[j].fClusterStart)
340 while (from < nchars_in_run && clusters[from] < j)
341 from++;
342 if (from >= nchars_in_run)
343 from = to = nchars_in_run - 1;
344 else
346 int k;
347 to = nchars_in_run - 1;
348 for (k = from + 1; k < nchars_in_run; k++)
350 if (clusters[k] > j)
352 to = k - 1;
353 break;
358 /* For RTL text, the Uniscribe shaper prepares
359 the values in ADVANCES array for layout in
360 reverse order, whereby "advance width" is
361 applied to move the pen in reverse direction
362 and _before_ drawing the glyph. Since we
363 draw glyphs in their normal left-to-right
364 order, we need to adjust the coordinates of
365 each non-base glyph in a grapheme cluster via
366 X-OFF component of the gstring's ADJUSTMENT
367 sub-vector. This loop computes, for each
368 grapheme cluster, the initial value of the
369 adjustment for the base character, which is
370 then updated for each successive glyph in the
371 grapheme cluster. */
372 if (items[i].a.fRTL)
374 int j1 = j;
376 adj_offset = 0;
377 while (j1 < nglyphs && !attributes[j1].fClusterStart)
379 adj_offset += advances[j1];
380 j1++;
385 LGLYPH_SET_CHAR (lglyph, chars[items[i].iCharPos
386 + from]);
387 LGLYPH_SET_FROM (lglyph, items[i].iCharPos + from);
388 LGLYPH_SET_TO (lglyph, items[i].iCharPos + to);
390 /* Metrics. */
391 LGLYPH_SET_WIDTH (lglyph, advances[j]);
392 LGLYPH_SET_ASCENT (lglyph, font->ascent);
393 LGLYPH_SET_DESCENT (lglyph, font->descent);
395 result = ScriptGetGlyphABCWidth (context,
396 &(uniscribe_font->cache),
397 glyphs[j], &char_metric);
398 if (result == E_PENDING && !context)
400 /* Cache incomplete... */
401 f = XFRAME (selected_frame);
402 context = get_frame_dc (f);
403 old_font = SelectObject (context, FONT_HANDLE (font));
404 result = ScriptGetGlyphABCWidth (context,
405 &(uniscribe_font->cache),
406 glyphs[j], &char_metric);
409 if (SUCCEEDED (result))
411 int lbearing = char_metric.abcA;
412 int rbearing = char_metric.abcA + char_metric.abcB;
414 LGLYPH_SET_LBEARING (lglyph, lbearing);
415 LGLYPH_SET_RBEARING (lglyph, rbearing);
417 else
419 LGLYPH_SET_LBEARING (lglyph, 0);
420 LGLYPH_SET_RBEARING (lglyph, advances[j]);
423 if (offsets[j].du || offsets[j].dv
424 /* For non-base glyphs of RTL grapheme clusters,
425 adjust the X offset even if both DU and DV
426 are zero. */
427 || (!attributes[j].fClusterStart && items[i].a.fRTL))
429 Lisp_Object vec = make_uninit_vector (3);
431 if (items[i].a.fRTL)
433 /* Empirically, it looks like Uniscribe
434 interprets DU in reverse direction for
435 RTL clusters. E.g., if we don't reverse
436 the direction, the Hebrew point HOLAM is
437 drawn above the right edge of the base
438 consonant, instead of above the left edge. */
439 ASET (vec, 0, make_number (-offsets[j].du
440 + adj_offset));
441 /* Update the adjustment value for the width
442 advance of the glyph we just emitted. */
443 adj_offset -= 2 * advances[j];
445 else
446 ASET (vec, 0, make_number (offsets[j].du + adj_offset));
447 /* In the font definition coordinate system, the
448 Y coordinate points up, while in our screen
449 coordinates Y grows downwards. So we need to
450 reverse the sign of Y-OFFSET here. */
451 ASET (vec, 1, make_number (-offsets[j].dv));
452 /* Based on what ftfont.c does... */
453 ASET (vec, 2, make_number (advances[j]));
454 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
456 else
458 LGLYPH_SET_ADJUSTMENT (lglyph, Qnil);
459 /* Update the adjustment value to compensate for
460 the width of the base character. */
461 if (items[i].a.fRTL)
462 adj_offset -= advances[j];
467 done_glyphs += nglyphs;
470 xfree (items);
472 if (context)
474 SelectObject (context, old_font);
475 release_frame_dc (f, context);
478 if (NILP (lgstring))
479 return Qnil;
480 else
481 return make_number (done_glyphs);
484 /* Uniscribe implementation of encode_char for font backend.
485 Return a glyph code of FONT for character C (Unicode code point).
486 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
487 static unsigned
488 uniscribe_encode_char (struct font *font, int c)
490 HDC context = NULL;
491 struct frame *f = NULL;
492 HFONT old_font = NULL;
493 unsigned code = FONT_INVALID_CODE;
494 wchar_t ch[2];
495 int len;
496 SCRIPT_ITEM* items;
497 int nitems;
498 struct uniscribe_font_info *uniscribe_font
499 = (struct uniscribe_font_info *)font;
501 if (c < 0x10000)
503 ch[0] = (wchar_t) c;
504 len = 1;
506 else
508 DWORD surrogate = c - 0x10000;
510 /* High surrogate: U+D800 - U+DBFF. */
511 ch[0] = 0xD800 + ((surrogate >> 10) & 0x03FF);
512 /* Low surrogate: U+DC00 - U+DFFF. */
513 ch[1] = 0xDC00 + (surrogate & 0x03FF);
514 len = 2;
517 /* Non BMP characters must be handled by the uniscribe shaping
518 engine as GDI functions (except blindly displaying lines of
519 Unicode text) and the promising looking ScriptGetCMap do not
520 convert surrogate pairs to glyph indexes correctly. */
522 items = (SCRIPT_ITEM *) alloca (sizeof (SCRIPT_ITEM) * 2 + 1);
523 if (SUCCEEDED (ScriptItemize (ch, len, 2, NULL, NULL, items, &nitems)))
525 HRESULT result;
526 /* Surrogates seem to need 2 here, even though only one glyph is
527 returned. Indic characters can also produce 2 or more glyphs for
528 a single code point, but they need to use uniscribe_shape
529 above for correct display. */
530 WORD glyphs[2], clusters[2];
531 SCRIPT_VISATTR attrs[2];
532 int nglyphs;
534 /* Force ScriptShape to generate glyphs in the logical
535 order. */
536 items[0].a.fLogicalOrder = 1;
538 result = ScriptShape (context, &(uniscribe_font->cache),
539 ch, len, 2, &(items[0].a),
540 glyphs, clusters, attrs, &nglyphs);
542 if (result == E_PENDING)
544 /* Use selected frame until API is updated to pass
545 the frame. */
546 f = XFRAME (selected_frame);
547 context = get_frame_dc (f);
548 old_font = SelectObject (context, FONT_HANDLE (font));
549 result = ScriptShape (context, &(uniscribe_font->cache),
550 ch, len, 2, &(items[0].a),
551 glyphs, clusters, attrs, &nglyphs);
554 if (SUCCEEDED (result) && nglyphs == 1)
556 /* Some fonts return .notdef glyphs instead of failing.
557 (TrueType spec reserves glyph code 0 for .notdef) */
558 if (glyphs[0])
559 code = glyphs[0];
561 else if (SUCCEEDED (result) || result == E_OUTOFMEMORY)
563 /* This character produces zero or more than one glyph
564 when shaped. But we still need the return from here
565 to be valid for the shaping engine to be invoked
566 later. */
567 result = ScriptGetCMap (context, &(uniscribe_font->cache),
568 ch, len, 0, glyphs);
569 if (SUCCEEDED (result) && glyphs[0])
570 code = glyphs[0];
574 if (context)
576 SelectObject (context, old_font);
577 release_frame_dc (f, context);
580 return code;
584 Shared with w32font:
585 Lisp_Object uniscribe_get_cache (Lisp_Object frame);
586 void uniscribe_free_entity (Lisp_Object font_entity);
587 int uniscribe_has_char (Lisp_Object entity, int c);
588 void uniscribe_text_extents (struct font *font, unsigned *code,
589 int nglyphs, struct font_metrics *metrics);
590 int uniscribe_draw (struct glyph_string *s, int from, int to,
591 int x, int y, int with_background);
593 Unused:
594 int uniscribe_prepare_face (struct frame *f, struct face *face);
595 void uniscribe_done_face (struct frame *f, struct face *face);
596 int uniscribe_get_bitmap (struct font *font, unsigned code,
597 struct font_bitmap *bitmap, int bits_per_pixel);
598 void uniscribe_free_bitmap (struct font *font, struct font_bitmap *bitmap);
599 int uniscribe_anchor_point (struct font *font, unsigned code,
600 int index, int *x, int *y);
601 int uniscribe_start_for_frame (struct frame *f);
602 int uniscribe_end_for_frame (struct frame *f);
607 /* Callback function for EnumFontFamiliesEx.
608 Adds the name of opentype fonts to a Lisp list (passed in as the
609 lParam arg). */
610 static int CALLBACK ALIGN_STACK
611 add_opentype_font_name_to_list (ENUMLOGFONTEX *logical_font,
612 NEWTEXTMETRICEX *physical_font,
613 DWORD font_type, LPARAM list_object)
615 Lisp_Object* list = (Lisp_Object *) list_object;
616 Lisp_Object family;
618 /* Skip vertical fonts (intended only for printing) */
619 if (logical_font->elfLogFont.lfFaceName[0] == '@')
620 return 1;
622 /* Skip non opentype fonts. Count old truetype fonts as opentype,
623 as some of them do contain GPOS and GSUB data that Uniscribe
624 can make use of. */
625 if (!(physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
626 && font_type != TRUETYPE_FONTTYPE)
627 return 1;
629 /* Skip fonts that have no Unicode coverage. */
630 if (!physical_font->ntmFontSig.fsUsb[3]
631 && !physical_font->ntmFontSig.fsUsb[2]
632 && !physical_font->ntmFontSig.fsUsb[1]
633 && !(physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff))
634 return 1;
636 family = intern_font_name (logical_font->elfLogFont.lfFaceName);
637 if (! memq_no_quit (family, *list))
638 *list = Fcons (family, *list);
640 return 1;
644 /* :otf property handling.
645 Since the necessary Uniscribe APIs for getting font tag information
646 are only available in Vista, we need to parse the font data directly
647 according to the OpenType Specification. */
649 /* Push into DWORD backwards to cope with endianness. */
650 #define OTF_TAG(STR) \
651 ((STR[3] << 24) | (STR[2] << 16) | (STR[1] << 8) | STR[0])
653 #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \
654 do { \
655 BYTE temp, data[2]; \
656 if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \
657 goto font_table_error; \
658 temp = data[0], data[0] = data[1], data[1] = temp; \
659 memcpy (PTR, data, 2); \
660 } while (0)
662 /* Do not reverse the bytes, because we will compare with a OTF_TAG value
663 that has them reversed already. */
664 #define OTF_DWORDTAG_VAL(TABLE, OFFSET, PTR) \
665 do { \
666 if (GetFontData (context, TABLE, OFFSET, PTR, 4) != 4) \
667 goto font_table_error; \
668 } while (0)
670 #define OTF_TAG_VAL(TABLE, OFFSET, STR) \
671 do { \
672 if (GetFontData (context, TABLE, OFFSET, STR, 4) != 4) \
673 goto font_table_error; \
674 STR[4] = '\0'; \
675 } while (0)
677 #define SNAME(VAL) SDATA (SYMBOL_NAME (VAL))
679 /* Check if font supports the otf script/language/features specified.
680 OTF_SPEC is in the format
681 (script lang [(gsub_feature ...)|nil] [(gpos_feature ...)]?) */
683 uniscribe_check_otf (LOGFONT *font, Lisp_Object otf_spec)
685 Lisp_Object script, lang, rest;
686 Lisp_Object features[2];
687 DWORD feature_tables[2];
688 DWORD script_tag, default_script, lang_tag = 0;
689 struct frame * f;
690 HDC context;
691 HFONT check_font, old_font;
692 int i, retval = 0;
693 struct gcpro gcpro1;
695 /* Check the spec is in the right format. */
696 if (!CONSP (otf_spec) || XINT (Flength (otf_spec)) < 3)
697 return 0;
699 /* Break otf_spec into its components. */
700 script = XCAR (otf_spec);
701 rest = XCDR (otf_spec);
703 lang = XCAR (rest);
704 rest = XCDR (rest);
706 features[0] = XCAR (rest);
707 rest = XCDR (rest);
708 if (NILP (rest))
709 features[1] = Qnil;
710 else
711 features[1] = XCAR (rest);
713 /* Set up tags we will use in the search. */
714 feature_tables[0] = OTF_TAG ("GSUB");
715 feature_tables[1] = OTF_TAG ("GPOS");
716 default_script = OTF_TAG ("DFLT");
717 if (NILP (script))
718 script_tag = default_script;
719 else
720 script_tag = OTF_TAG (SNAME (script));
721 if (!NILP (lang))
722 lang_tag = OTF_TAG (SNAME (lang));
724 /* Set up graphics context so we can use the font. */
725 f = XFRAME (selected_frame);
726 context = get_frame_dc (f);
727 check_font = CreateFontIndirect (font);
728 old_font = SelectObject (context, check_font);
730 /* Everything else is contained within otf_spec so should get
731 marked along with it. */
732 GCPRO1 (otf_spec);
734 /* Scan GSUB and GPOS tables. */
735 for (i = 0; i < 2; i++)
737 int j, n_match_features;
738 unsigned short scriptlist_table, feature_table, n_scripts;
739 unsigned short script_table, langsys_table, n_langs;
740 unsigned short feature_index, n_features;
741 DWORD tbl = feature_tables[i];
743 /* Skip if no features requested from this table. */
744 if (NILP (features[i]))
745 continue;
747 /* If features is not a cons, this font spec is messed up. */
748 if (!CONSP (features[i]))
749 goto no_support;
751 /* Read GPOS/GSUB header. */
752 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
753 OTF_INT16_VAL (tbl, 6, &feature_table);
754 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
756 /* Find the appropriate script table. */
757 script_table = 0;
758 for (j = 0; j < n_scripts; j++)
760 DWORD script_id;
761 OTF_DWORDTAG_VAL (tbl, scriptlist_table + 2 + j * 6, &script_id);
762 if (script_id == script_tag)
764 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
765 break;
767 #if 0 /* Causes false positives. */
768 /* If there is a DFLT script defined in the font, use it
769 if the specified script is not found. */
770 else if (script_id == default_script)
771 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
772 #endif
774 /* If no specific or default script table was found, then this font
775 does not support the script. */
776 if (!script_table)
777 goto no_support;
779 /* Offset is from beginning of scriptlist_table. */
780 script_table += scriptlist_table;
782 /* Get default langsys table. */
783 OTF_INT16_VAL (tbl, script_table, &langsys_table);
785 /* If lang was specified, see if font contains a specific entry. */
786 if (!NILP (lang))
788 OTF_INT16_VAL (tbl, script_table + 2, &n_langs);
790 for (j = 0; j < n_langs; j++)
792 DWORD lang_id;
793 OTF_DWORDTAG_VAL (tbl, script_table + 4 + j * 6, &lang_id);
794 if (lang_id == lang_tag)
796 OTF_INT16_VAL (tbl, script_table + 8 + j * 6, &langsys_table);
797 break;
802 if (!langsys_table)
803 goto no_support;
805 /* Offset is from beginning of script table. */
806 langsys_table += script_table;
808 /* Check the features. Features may contain nil according to
809 documentation in font_prop_validate_otf, so count them. */
810 n_match_features = 0;
811 for (rest = features[i]; CONSP (rest); rest = XCDR (rest))
813 Lisp_Object feature = XCAR (rest);
814 if (!NILP (feature))
815 n_match_features++;
818 /* If there are no features to check, skip checking. */
819 if (!n_match_features)
820 continue;
822 /* First check required feature (if any). */
823 OTF_INT16_VAL (tbl, langsys_table + 2, &feature_index);
824 if (feature_index != 0xFFFF)
826 char feature_id[5];
827 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
828 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
829 /* Assume no duplicates in the font table. This allows us to mark
830 the features off by simply decrementing a counter. */
831 if (!NILP (Fmemq (intern (feature_id), features[i])))
832 n_match_features--;
834 /* Now check all the other features. */
835 OTF_INT16_VAL (tbl, langsys_table + 4, &n_features);
836 for (j = 0; j < n_features; j++)
838 char feature_id[5];
839 OTF_INT16_VAL (tbl, langsys_table + 6 + j * 2, &feature_index);
840 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
841 /* Assume no duplicates in the font table. This allows us to mark
842 the features off by simply decrementing a counter. */
843 if (!NILP (Fmemq (intern (feature_id), features[i])))
844 n_match_features--;
847 if (n_match_features > 0)
848 goto no_support;
851 retval = 1;
853 no_support:
854 font_table_error:
855 /* restore graphics context. */
856 SelectObject (context, old_font);
857 DeleteObject (check_font);
858 release_frame_dc (f, context);
860 return retval;
863 static Lisp_Object
864 otf_features (HDC context, char *table)
866 Lisp_Object script_list = Qnil;
867 unsigned short scriptlist_table, n_scripts, feature_table;
868 DWORD tbl = OTF_TAG (table);
869 int i, j, k;
871 /* Look for scripts in the table. */
872 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
873 OTF_INT16_VAL (tbl, 6, &feature_table);
874 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
876 for (i = 0; i < n_scripts; i++)
878 char script[5], lang[5];
879 unsigned short script_table, lang_count, langsys_table, feature_count;
880 Lisp_Object script_tag, langsys_list, langsys_tag, feature_list;
881 unsigned short record_offset = scriptlist_table + 2 + i * 6;
882 OTF_TAG_VAL (tbl, record_offset, script);
883 OTF_INT16_VAL (tbl, record_offset + 4, &script_table);
885 /* Offset is from beginning of script table. */
886 script_table += scriptlist_table;
888 script_tag = intern (script);
889 langsys_list = Qnil;
891 /* Optional default lang. */
892 OTF_INT16_VAL (tbl, script_table, &langsys_table);
893 if (langsys_table)
895 /* Offset is from beginning of script table. */
896 langsys_table += script_table;
898 langsys_tag = Qnil;
899 feature_list = Qnil;
900 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
901 for (k = 0; k < feature_count; k++)
903 char feature[5];
904 unsigned short index;
905 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
906 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
907 feature_list = Fcons (intern (feature), feature_list);
909 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
910 langsys_list);
913 /* List of supported languages. */
914 OTF_INT16_VAL (tbl, script_table + 2, &lang_count);
916 for (j = 0; j < lang_count; j++)
918 record_offset = script_table + 4 + j * 6;
919 OTF_TAG_VAL (tbl, record_offset, lang);
920 OTF_INT16_VAL (tbl, record_offset + 4, &langsys_table);
922 /* Offset is from beginning of script table. */
923 langsys_table += script_table;
925 langsys_tag = intern (lang);
926 feature_list = Qnil;
927 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
928 for (k = 0; k < feature_count; k++)
930 char feature[5];
931 unsigned short index;
932 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
933 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
934 feature_list = Fcons (intern (feature), feature_list);
936 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
937 langsys_list);
941 script_list = Fcons (Fcons (script_tag, langsys_list), script_list);
944 return script_list;
946 font_table_error:
947 return Qnil;
950 #undef OTF_INT16_VAL
951 #undef OTF_TAG_VAL
952 #undef OTF_TAG
955 struct font_driver uniscribe_font_driver =
957 LISP_INITIALLY_ZERO, /* Quniscribe */
958 0, /* case insensitive */
959 w32font_get_cache,
960 uniscribe_list,
961 uniscribe_match,
962 uniscribe_list_family,
963 NULL, /* free_entity */
964 uniscribe_open,
965 uniscribe_close,
966 NULL, /* prepare_face */
967 NULL, /* done_face */
968 w32font_has_char,
969 uniscribe_encode_char,
970 w32font_text_extents,
971 w32font_draw,
972 NULL, /* get_bitmap */
973 NULL, /* free_bitmap */
974 NULL, /* anchor_point */
975 uniscribe_otf_capability, /* Defined so (font-get FONTOBJ :otf) works. */
976 NULL, /* otf_drive - use shape instead. */
977 NULL, /* start_for_frame */
978 NULL, /* end_for_frame */
979 uniscribe_shape,
980 NULL, /* check */
981 NULL, /* get_variation_glyphs */
982 NULL, /* filter_properties */
983 NULL, /* cached_font_ok */
986 /* Note that this should be called at every startup, not just when dumping,
987 as it needs to test for the existence of the Uniscribe library. */
988 void
989 syms_of_w32uniscribe (void)
991 HMODULE uniscribe;
993 /* Don't init uniscribe when dumping */
994 if (!initialized)
995 return;
997 /* Don't register if uniscribe is not available. */
998 uniscribe = GetModuleHandle ("usp10");
999 if (!uniscribe)
1000 return;
1002 uniscribe_font_driver.type = Quniscribe;
1003 uniscribe_available = 1;
1005 register_font_driver (&uniscribe_font_driver, NULL);