Remove an incorrect assertion on move_it_by_lines.
[emacs.git] / src / w32uniscribe.c
blob5e5c8e88b2beadcda24c1c8f367a1789193954a4
1 /* Font backend for the Microsoft W32 Uniscribe API.
2 Copyright (C) 2008-2012 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>
30 #include <setjmp.h>
32 #include "lisp.h"
33 #include "w32term.h"
34 #include "frame.h"
35 #include "dispextern.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "composite.h"
39 #include "fontset.h"
40 #include "font.h"
41 #include "w32font.h"
43 struct uniscribe_font_info
45 struct w32font_info w32_font;
46 SCRIPT_CACHE cache;
49 int uniscribe_available = 0;
51 /* Defined in w32font.c, since it is required there as well. */
52 extern Lisp_Object Quniscribe;
53 extern Lisp_Object Qopentype;
55 /* EnumFontFamiliesEx callback. */
56 static int CALLBACK add_opentype_font_name_to_list (ENUMLOGFONTEX *,
57 NEWTEXTMETRICEX *,
58 DWORD, LPARAM);
59 /* Used by uniscribe_otf_capability. */
60 static Lisp_Object otf_features (HDC context, char *table);
62 static int
63 memq_no_quit (Lisp_Object elt, Lisp_Object list)
65 while (CONSP (list) && ! EQ (XCAR (list), elt))
66 list = XCDR (list);
67 return (CONSP (list));
71 /* Font backend interface implementation. */
72 static Lisp_Object
73 uniscribe_list (Lisp_Object frame, Lisp_Object font_spec)
75 Lisp_Object fonts = w32font_list_internal (frame, font_spec, 1);
76 FONT_ADD_LOG ("uniscribe-list", font_spec, fonts);
77 return fonts;
80 static Lisp_Object
81 uniscribe_match (Lisp_Object frame, Lisp_Object font_spec)
83 Lisp_Object entity = w32font_match_internal (frame, font_spec, 1);
84 FONT_ADD_LOG ("uniscribe-match", font_spec, entity);
85 return entity;
88 static Lisp_Object
89 uniscribe_list_family (Lisp_Object frame)
91 Lisp_Object list = Qnil;
92 LOGFONT font_match_pattern;
93 HDC dc;
94 FRAME_PTR f = XFRAME (frame);
96 memset (&font_match_pattern, 0, sizeof (font_match_pattern));
97 /* Limit enumerated fonts to outline fonts to save time. */
98 font_match_pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
100 dc = get_frame_dc (f);
102 EnumFontFamiliesEx (dc, &font_match_pattern,
103 (FONTENUMPROC) add_opentype_font_name_to_list,
104 (LPARAM) &list, 0);
105 release_frame_dc (f, dc);
107 return list;
110 static Lisp_Object
111 uniscribe_open (FRAME_PTR 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))
123 return Qnil;
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 /* Mark the format as opentype */
133 uniscribe_font->w32_font.font.props[FONT_FORMAT_INDEX] = Qopentype;
134 uniscribe_font->w32_font.font.driver = &uniscribe_font_driver;
136 return font_object;
139 static void
140 uniscribe_close (FRAME_PTR f, struct font *font)
142 struct uniscribe_font_info *uniscribe_font
143 = (struct uniscribe_font_info *) font;
145 if (uniscribe_font->cache)
146 ScriptFreeCache (&(uniscribe_font->cache));
148 w32font_close (f, font);
151 /* Return a list describing which scripts/languages FONT supports by
152 which GSUB/GPOS features of OpenType tables. */
153 static Lisp_Object
154 uniscribe_otf_capability (struct font *font)
156 HDC context;
157 HFONT old_font;
158 struct frame *f;
159 Lisp_Object capability = Fcons (Qnil, Qnil);
160 Lisp_Object features;
162 f = XFRAME (selected_frame);
163 context = get_frame_dc (f);
164 old_font = SelectObject (context, FONT_HANDLE (font));
166 features = otf_features (context, "GSUB");
167 XSETCAR (capability, features);
168 features = otf_features (context, "GPOS");
169 XSETCDR (capability, features);
171 SelectObject (context, old_font);
172 release_frame_dc (f, context);
174 return capability;
177 /* Uniscribe implementation of shape for font backend.
179 Shape text in LGSTRING. See the docstring of
180 `composition-get-gstring' for the format of LGSTRING. If the
181 (N+1)th element of LGSTRING is nil, input of shaping is from the
182 1st to (N)th elements. In each input glyph, FROM, TO, CHAR, and
183 CODE are already set.
185 This function updates all fields of the input glyphs. If the
186 output glyphs (M) are more than the input glyphs (N), (N+1)th
187 through (M)th elements of LGSTRING are updated possibly by making
188 a new glyph object and storing it in LGSTRING. If (M) is greater
189 than the length of LGSTRING, nil should be returned. In that case,
190 this function is called again with a larger LGSTRING. */
191 static Lisp_Object
192 uniscribe_shape (Lisp_Object lgstring)
194 struct font * font;
195 struct uniscribe_font_info * uniscribe_font;
196 EMACS_UINT nchars;
197 int nitems, max_items, i, max_glyphs, done_glyphs;
198 wchar_t *chars;
199 WORD *glyphs, *clusters;
200 SCRIPT_ITEM *items;
201 SCRIPT_VISATTR *attributes;
202 int *advances;
203 GOFFSET *offsets;
204 ABC overall_metrics;
205 HRESULT result;
206 struct frame * f = NULL;
207 HDC context = NULL;
208 HFONT old_font = NULL;
210 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
211 uniscribe_font = (struct uniscribe_font_info *) font;
213 /* Get the chars from lgstring in a form we can use with uniscribe. */
214 max_glyphs = nchars = LGSTRING_GLYPH_LEN (lgstring);
215 done_glyphs = 0;
216 chars = (wchar_t *) alloca (nchars * sizeof (wchar_t));
217 /* FIXME: This loop assumes that characters in the input LGSTRING
218 are all inside the BMP. Need to encode characters beyond the BMP
219 as UTF-16. */
220 for (i = 0; i < nchars; i++)
222 /* lgstring can be bigger than the number of characters in it, in
223 the case where more glyphs are required to display those characters.
224 If that is the case, note the real number of characters. */
225 if (NILP (LGSTRING_GLYPH (lgstring, i)))
226 nchars = i;
227 else
228 chars[i] = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
231 /* First we need to break up the glyph string into runs of glyphs that
232 can be treated together. First try a single run. */
233 max_items = 2;
234 items = (SCRIPT_ITEM *) xmalloc (sizeof (SCRIPT_ITEM) * max_items + 1);
236 while ((result = ScriptItemize (chars, nchars, max_items, NULL, NULL,
237 items, &nitems)) == E_OUTOFMEMORY)
239 /* If that wasn't enough, keep trying with one more run. */
240 max_items++;
241 items = (SCRIPT_ITEM *) xrealloc (items,
242 sizeof (SCRIPT_ITEM) * max_items + 1);
245 if (FAILED (result))
247 xfree (items);
248 return Qnil;
251 glyphs = alloca (max_glyphs * sizeof (WORD));
252 clusters = alloca (nchars * sizeof (WORD));
253 attributes = alloca (max_glyphs * sizeof (SCRIPT_VISATTR));
254 advances = alloca (max_glyphs * sizeof (int));
255 offsets = alloca (max_glyphs * sizeof (GOFFSET));
257 for (i = 0; i < nitems; i++)
259 int nglyphs, nchars_in_run;
260 nchars_in_run = items[i+1].iCharPos - items[i].iCharPos;
261 /* Force ScriptShape to generate glyphs in the same order as
262 they are in the input LGSTRING, which is in the logical
263 order. */
264 items[i].a.fLogicalOrder = 1;
266 /* Context may be NULL here, in which case the cache should be
267 used without needing to select the font. */
268 result = ScriptShape (context, &(uniscribe_font->cache),
269 chars + items[i].iCharPos, nchars_in_run,
270 max_glyphs - done_glyphs, &(items[i].a),
271 glyphs, clusters, attributes, &nglyphs);
273 if (result == E_PENDING && !context)
275 /* This assumes the selected frame is on the same display as the
276 one we are drawing. It would be better for the frame to be
277 passed in. */
278 f = XFRAME (selected_frame);
279 context = get_frame_dc (f);
280 old_font = SelectObject (context, FONT_HANDLE (font));
282 result = ScriptShape (context, &(uniscribe_font->cache),
283 chars + items[i].iCharPos, nchars_in_run,
284 max_glyphs - done_glyphs, &(items[i].a),
285 glyphs, clusters, attributes, &nglyphs);
288 if (result == E_OUTOFMEMORY)
290 /* Need a bigger lgstring. */
291 lgstring = Qnil;
292 break;
294 else if (FAILED (result))
296 /* Can't shape this run - return results so far if any. */
297 break;
299 else if (items[i].a.fNoGlyphIndex)
301 /* Glyph indices not supported by this font (or OS), means we
302 can't really do any meaningful shaping. */
303 break;
305 else
307 result = ScriptPlace (context, &(uniscribe_font->cache),
308 glyphs, nglyphs, attributes, &(items[i].a),
309 advances, offsets, &overall_metrics);
310 if (result == E_PENDING && !context)
312 /* Cache not complete... */
313 f = XFRAME (selected_frame);
314 context = get_frame_dc (f);
315 old_font = SelectObject (context, FONT_HANDLE (font));
317 result = ScriptPlace (context, &(uniscribe_font->cache),
318 glyphs, nglyphs, attributes, &(items[i].a),
319 advances, offsets, &overall_metrics);
321 if (SUCCEEDED (result))
323 int j, from, to;
325 from = 0;
326 to = from;
328 for (j = 0; j < nglyphs; j++)
330 int lglyph_index = j + done_glyphs;
331 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, lglyph_index);
332 ABC char_metric;
333 unsigned gl;
335 if (NILP (lglyph))
337 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
338 LGSTRING_SET_GLYPH (lgstring, lglyph_index, lglyph);
340 /* Copy to a 32-bit data type to shut up the
341 compiler warning in LGLYPH_SET_CODE about
342 comparison being always false. */
343 gl = glyphs[j];
344 LGLYPH_SET_CODE (lglyph, gl);
346 /* Detect clusters, for linking codes back to
347 characters. */
348 if (attributes[j].fClusterStart)
350 while (from < nchars_in_run && clusters[from] < j)
351 from++;
352 if (from >= nchars_in_run)
353 from = to = nchars_in_run - 1;
354 else
356 int k;
357 to = nchars_in_run - 1;
358 for (k = from + 1; k < nchars_in_run; k++)
360 if (clusters[k] > j)
362 to = k - 1;
363 break;
369 LGLYPH_SET_CHAR (lglyph, chars[items[i].iCharPos
370 + from]);
371 LGLYPH_SET_FROM (lglyph, items[i].iCharPos + from);
372 LGLYPH_SET_TO (lglyph, items[i].iCharPos + to);
374 /* Metrics. */
375 LGLYPH_SET_WIDTH (lglyph, advances[j]);
376 LGLYPH_SET_ASCENT (lglyph, font->ascent);
377 LGLYPH_SET_DESCENT (lglyph, font->descent);
379 result = ScriptGetGlyphABCWidth (context,
380 &(uniscribe_font->cache),
381 glyphs[j], &char_metric);
382 if (result == E_PENDING && !context)
384 /* Cache incomplete... */
385 f = XFRAME (selected_frame);
386 context = get_frame_dc (f);
387 old_font = SelectObject (context, FONT_HANDLE (font));
388 result = ScriptGetGlyphABCWidth (context,
389 &(uniscribe_font->cache),
390 glyphs[j], &char_metric);
393 if (SUCCEEDED (result))
395 LGLYPH_SET_LBEARING (lglyph, char_metric.abcA);
396 LGLYPH_SET_RBEARING (lglyph, (char_metric.abcA
397 + char_metric.abcB));
399 else
401 LGLYPH_SET_LBEARING (lglyph, 0);
402 LGLYPH_SET_RBEARING (lglyph, advances[j]);
405 if (offsets[j].du || offsets[j].dv)
407 Lisp_Object vec;
408 vec = Fmake_vector (make_number (3), Qnil);
409 ASET (vec, 0, make_number (offsets[j].du));
410 ASET (vec, 1, make_number (offsets[j].dv));
411 /* Based on what ftfont.c does... */
412 ASET (vec, 2, make_number (advances[j]));
413 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
415 else
416 LGLYPH_SET_ADJUSTMENT (lglyph, Qnil);
420 done_glyphs += nglyphs;
423 xfree (items);
425 if (context)
427 SelectObject (context, old_font);
428 release_frame_dc (f, context);
431 if (NILP (lgstring))
432 return Qnil;
433 else
434 return make_number (done_glyphs);
437 /* Uniscribe implementation of encode_char for font backend.
438 Return a glyph code of FONT for character C (Unicode code point).
439 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
440 static unsigned
441 uniscribe_encode_char (struct font *font, int c)
443 HDC context = NULL;
444 struct frame *f = NULL;
445 HFONT old_font = NULL;
446 unsigned code = FONT_INVALID_CODE;
447 wchar_t ch[2];
448 int len;
449 SCRIPT_ITEM* items;
450 int nitems;
451 struct uniscribe_font_info *uniscribe_font
452 = (struct uniscribe_font_info *)font;
454 if (c < 0x10000)
456 ch[0] = (wchar_t) c;
457 len = 1;
459 else
461 DWORD surrogate = c - 0x10000;
463 /* High surrogate: U+D800 - U+DBFF. */
464 ch[0] = 0xD800 + ((surrogate >> 10) & 0x03FF);
465 /* Low surrogate: U+DC00 - U+DFFF. */
466 ch[1] = 0xDC00 + (surrogate & 0x03FF);
467 len = 2;
470 /* Non BMP characters must be handled by the uniscribe shaping
471 engine as GDI functions (except blindly displaying lines of
472 Unicode text) and the promising looking ScriptGetCMap do not
473 convert surrogate pairs to glyph indexes correctly. */
475 items = (SCRIPT_ITEM *) alloca (sizeof (SCRIPT_ITEM) * 2 + 1);
476 if (SUCCEEDED (ScriptItemize (ch, len, 2, NULL, NULL, items, &nitems)))
478 HRESULT result;
479 /* Surrogates seem to need 2 here, even though only one glyph is
480 returned. Indic characters can also produce 2 or more glyphs for
481 a single code point, but they need to use uniscribe_shape
482 above for correct display. */
483 WORD glyphs[2], clusters[2];
484 SCRIPT_VISATTR attrs[2];
485 int nglyphs;
487 /* Force ScriptShape to generate glyphs in the logical
488 order. */
489 items[0].a.fLogicalOrder = 1;
491 result = ScriptShape (context, &(uniscribe_font->cache),
492 ch, len, 2, &(items[0].a),
493 glyphs, clusters, attrs, &nglyphs);
495 if (result == E_PENDING)
497 /* Use selected frame until API is updated to pass
498 the frame. */
499 f = XFRAME (selected_frame);
500 context = get_frame_dc (f);
501 old_font = SelectObject (context, FONT_HANDLE (font));
502 result = ScriptShape (context, &(uniscribe_font->cache),
503 ch, len, 2, &(items[0].a),
504 glyphs, clusters, attrs, &nglyphs);
507 if (SUCCEEDED (result) && nglyphs == 1)
509 /* Some fonts return .notdef glyphs instead of failing.
510 (TrueType spec reserves glyph code 0 for .notdef) */
511 if (glyphs[0])
512 code = glyphs[0];
514 else if (SUCCEEDED (result) || result == E_OUTOFMEMORY)
516 /* This character produces zero or more than one glyph
517 when shaped. But we still need the return from here
518 to be valid for the shaping engine to be invoked
519 later. */
520 result = ScriptGetCMap (context, &(uniscribe_font->cache),
521 ch, len, 0, glyphs);
522 if (SUCCEEDED (result) && glyphs[0])
523 code = glyphs[0];
527 if (context)
529 SelectObject (context, old_font);
530 release_frame_dc (f, context);
533 return code;
537 Shared with w32font:
538 Lisp_Object uniscribe_get_cache (Lisp_Object frame);
539 void uniscribe_free_entity (Lisp_Object font_entity);
540 int uniscribe_has_char (Lisp_Object entity, int c);
541 int uniscribe_text_extents (struct font *font, unsigned *code,
542 int nglyphs, struct font_metrics *metrics);
543 int uniscribe_draw (struct glyph_string *s, int from, int to,
544 int x, int y, int with_background);
546 Unused:
547 int uniscribe_prepare_face (FRAME_PTR f, struct face *face);
548 void uniscribe_done_face (FRAME_PTR f, struct face *face);
549 int uniscribe_get_bitmap (struct font *font, unsigned code,
550 struct font_bitmap *bitmap, int bits_per_pixel);
551 void uniscribe_free_bitmap (struct font *font, struct font_bitmap *bitmap);
552 void * uniscribe_get_outline (struct font *font, unsigned code);
553 void uniscribe_free_outline (struct font *font, void *outline);
554 int uniscribe_anchor_point (struct font *font, unsigned code,
555 int index, int *x, int *y);
556 int uniscribe_start_for_frame (FRAME_PTR f);
557 int uniscribe_end_for_frame (FRAME_PTR f);
562 /* Callback function for EnumFontFamiliesEx.
563 Adds the name of opentype fonts to a Lisp list (passed in as the
564 lParam arg). */
565 static int CALLBACK
566 add_opentype_font_name_to_list (ENUMLOGFONTEX *logical_font,
567 NEWTEXTMETRICEX *physical_font,
568 DWORD font_type, LPARAM list_object)
570 Lisp_Object* list = (Lisp_Object *) list_object;
571 Lisp_Object family;
573 /* Skip vertical fonts (intended only for printing) */
574 if (logical_font->elfLogFont.lfFaceName[0] == '@')
575 return 1;
577 /* Skip non opentype fonts. Count old truetype fonts as opentype,
578 as some of them do contain GPOS and GSUB data that Uniscribe
579 can make use of. */
580 if (!(physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
581 && font_type != TRUETYPE_FONTTYPE)
582 return 1;
584 /* Skip fonts that have no Unicode coverage. */
585 if (!physical_font->ntmFontSig.fsUsb[3]
586 && !physical_font->ntmFontSig.fsUsb[2]
587 && !physical_font->ntmFontSig.fsUsb[1]
588 && !(physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff))
589 return 1;
591 family = intern_font_name (logical_font->elfLogFont.lfFaceName);
592 if (! memq_no_quit (family, *list))
593 *list = Fcons (family, *list);
595 return 1;
599 /* :otf property handling.
600 Since the necessary Uniscribe APIs for getting font tag information
601 are only available in Vista, we need to parse the font data directly
602 according to the OpenType Specification. */
604 /* Push into DWORD backwards to cope with endianness. */
605 #define OTF_TAG(STR) \
606 ((STR[3] << 24) | (STR[2] << 16) | (STR[1] << 8) | STR[0])
608 #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \
609 do { \
610 BYTE temp, data[2]; \
611 if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \
612 goto font_table_error; \
613 temp = data[0], data[0] = data[1], data[1] = temp; \
614 memcpy (PTR, data, 2); \
615 } while (0)
617 /* Do not reverse the bytes, because we will compare with a OTF_TAG value
618 that has them reversed already. */
619 #define OTF_DWORDTAG_VAL(TABLE, OFFSET, PTR) \
620 do { \
621 if (GetFontData (context, TABLE, OFFSET, PTR, 4) != 4) \
622 goto font_table_error; \
623 } while (0)
625 #define OTF_TAG_VAL(TABLE, OFFSET, STR) \
626 do { \
627 if (GetFontData (context, TABLE, OFFSET, STR, 4) != 4) \
628 goto font_table_error; \
629 STR[4] = '\0'; \
630 } while (0)
632 #define SNAME(VAL) SDATA (SYMBOL_NAME (VAL))
634 /* Check if font supports the otf script/language/features specified.
635 OTF_SPEC is in the format
636 (script lang [(gsub_feature ...)|nil] [(gpos_feature ...)]?) */
638 uniscribe_check_otf (LOGFONT *font, Lisp_Object otf_spec)
640 Lisp_Object script, lang, rest;
641 Lisp_Object features[2];
642 DWORD feature_tables[2];
643 DWORD script_tag, default_script, lang_tag = 0;
644 struct frame * f;
645 HDC context;
646 HFONT check_font, old_font;
647 int i, retval = 0;
648 struct gcpro gcpro1;
650 /* Check the spec is in the right format. */
651 if (!CONSP (otf_spec) || XINT (Flength (otf_spec)) < 3)
652 return 0;
654 /* Break otf_spec into its components. */
655 script = XCAR (otf_spec);
656 rest = XCDR (otf_spec);
658 lang = XCAR (rest);
659 rest = XCDR (rest);
661 features[0] = XCAR (rest);
662 rest = XCDR (rest);
663 if (NILP (rest))
664 features[1] = Qnil;
665 else
666 features[1] = XCAR (rest);
668 /* Set up tags we will use in the search. */
669 feature_tables[0] = OTF_TAG ("GSUB");
670 feature_tables[1] = OTF_TAG ("GPOS");
671 default_script = OTF_TAG ("DFLT");
672 if (NILP (script))
673 script_tag = default_script;
674 else
675 script_tag = OTF_TAG (SNAME (script));
676 if (!NILP (lang))
677 lang_tag = OTF_TAG (SNAME (lang));
679 /* Set up graphics context so we can use the font. */
680 f = XFRAME (selected_frame);
681 context = get_frame_dc (f);
682 check_font = CreateFontIndirect (font);
683 old_font = SelectObject (context, check_font);
685 /* Everything else is contained within otf_spec so should get
686 marked along with it. */
687 GCPRO1 (otf_spec);
689 /* Scan GSUB and GPOS tables. */
690 for (i = 0; i < 2; i++)
692 int j, n_match_features;
693 unsigned short scriptlist_table, feature_table, n_scripts;
694 unsigned short script_table, langsys_table, n_langs;
695 unsigned short feature_index, n_features;
696 DWORD tbl = feature_tables[i];
698 /* Skip if no features requested from this table. */
699 if (NILP (features[i]))
700 continue;
702 /* If features is not a cons, this font spec is messed up. */
703 if (!CONSP (features[i]))
704 goto no_support;
706 /* Read GPOS/GSUB header. */
707 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
708 OTF_INT16_VAL (tbl, 6, &feature_table);
709 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
711 /* Find the appropriate script table. */
712 script_table = 0;
713 for (j = 0; j < n_scripts; j++)
715 DWORD script_id;
716 OTF_DWORDTAG_VAL (tbl, scriptlist_table + 2 + j * 6, &script_id);
717 if (script_id == script_tag)
719 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
720 break;
722 #if 0 /* Causes false positives. */
723 /* If there is a DFLT script defined in the font, use it
724 if the specified script is not found. */
725 else if (script_id == default_script)
726 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
727 #endif
729 /* If no specific or default script table was found, then this font
730 does not support the script. */
731 if (!script_table)
732 goto no_support;
734 /* Offset is from beginning of scriptlist_table. */
735 script_table += scriptlist_table;
737 /* Get default langsys table. */
738 OTF_INT16_VAL (tbl, script_table, &langsys_table);
740 /* If lang was specified, see if font contains a specific entry. */
741 if (!NILP (lang))
743 OTF_INT16_VAL (tbl, script_table + 2, &n_langs);
745 for (j = 0; j < n_langs; j++)
747 DWORD lang_id;
748 OTF_DWORDTAG_VAL (tbl, script_table + 4 + j * 6, &lang_id);
749 if (lang_id == lang_tag)
751 OTF_INT16_VAL (tbl, script_table + 8 + j * 6, &langsys_table);
752 break;
757 if (!langsys_table)
758 goto no_support;
760 /* Offset is from beginning of script table. */
761 langsys_table += script_table;
763 /* Check the features. Features may contain nil according to
764 documentation in font_prop_validate_otf, so count them. */
765 n_match_features = 0;
766 for (rest = features[i]; CONSP (rest); rest = XCDR (rest))
768 Lisp_Object feature = XCAR (rest);
769 if (!NILP (feature))
770 n_match_features++;
773 /* If there are no features to check, skip checking. */
774 if (!n_match_features)
775 continue;
777 /* First check required feature (if any). */
778 OTF_INT16_VAL (tbl, langsys_table + 2, &feature_index);
779 if (feature_index != 0xFFFF)
781 char feature_id[5];
782 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
783 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
784 /* Assume no duplicates in the font table. This allows us to mark
785 the features off by simply decrementing a counter. */
786 if (!NILP (Fmemq (intern (feature_id), features[i])))
787 n_match_features--;
789 /* Now check all the other features. */
790 OTF_INT16_VAL (tbl, langsys_table + 4, &n_features);
791 for (j = 0; j < n_features; j++)
793 char feature_id[5];
794 OTF_INT16_VAL (tbl, langsys_table + 6 + j * 2, &feature_index);
795 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
796 /* Assume no duplicates in the font table. This allows us to mark
797 the features off by simply decrementing a counter. */
798 if (!NILP (Fmemq (intern (feature_id), features[i])))
799 n_match_features--;
802 if (n_match_features > 0)
803 goto no_support;
806 retval = 1;
808 no_support:
809 font_table_error:
810 /* restore graphics context. */
811 SelectObject (context, old_font);
812 DeleteObject (check_font);
813 release_frame_dc (f, context);
815 return retval;
818 static Lisp_Object
819 otf_features (HDC context, char *table)
821 Lisp_Object script_list = Qnil;
822 unsigned short scriptlist_table, n_scripts, feature_table;
823 DWORD tbl = OTF_TAG (table);
824 int i, j, k;
826 /* Look for scripts in the table. */
827 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
828 OTF_INT16_VAL (tbl, 6, &feature_table);
829 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
831 for (i = 0; i < n_scripts; i++)
833 char script[5], lang[5];
834 unsigned short script_table, lang_count, langsys_table, feature_count;
835 Lisp_Object script_tag, langsys_list, langsys_tag, feature_list;
836 unsigned short record_offset = scriptlist_table + 2 + i * 6;
837 OTF_TAG_VAL (tbl, record_offset, script);
838 OTF_INT16_VAL (tbl, record_offset + 4, &script_table);
840 /* Offset is from beginning of script table. */
841 script_table += scriptlist_table;
843 script_tag = intern (script);
844 langsys_list = Qnil;
846 /* Optional default lang. */
847 OTF_INT16_VAL (tbl, script_table, &langsys_table);
848 if (langsys_table)
850 /* Offset is from beginning of script table. */
851 langsys_table += script_table;
853 langsys_tag = Qnil;
854 feature_list = Qnil;
855 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
856 for (k = 0; k < feature_count; k++)
858 char feature[5];
859 unsigned short index;
860 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
861 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
862 feature_list = Fcons (intern (feature), feature_list);
864 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
865 langsys_list);
868 /* List of supported languages. */
869 OTF_INT16_VAL (tbl, script_table + 2, &lang_count);
871 for (j = 0; j < lang_count; j++)
873 record_offset = script_table + 4 + j * 6;
874 OTF_TAG_VAL (tbl, record_offset, lang);
875 OTF_INT16_VAL (tbl, record_offset + 4, &langsys_table);
877 /* Offset is from beginning of script table. */
878 langsys_table += script_table;
880 langsys_tag = intern (lang);
881 feature_list = Qnil;
882 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
883 for (k = 0; k < feature_count; k++)
885 char feature[5];
886 unsigned short index;
887 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
888 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
889 feature_list = Fcons (intern (feature), feature_list);
891 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
892 langsys_list);
896 script_list = Fcons (Fcons (script_tag, langsys_list), script_list);
899 return script_list;
901 font_table_error:
902 return Qnil;
905 #undef OTF_INT16_VAL
906 #undef OTF_TAG_VAL
907 #undef OTF_TAG
910 struct font_driver uniscribe_font_driver =
912 0, /* Quniscribe */
913 0, /* case insensitive */
914 w32font_get_cache,
915 uniscribe_list,
916 uniscribe_match,
917 uniscribe_list_family,
918 NULL, /* free_entity */
919 uniscribe_open,
920 uniscribe_close,
921 NULL, /* prepare_face */
922 NULL, /* done_face */
923 w32font_has_char,
924 uniscribe_encode_char,
925 w32font_text_extents,
926 w32font_draw,
927 NULL, /* get_bitmap */
928 NULL, /* free_bitmap */
929 NULL, /* get_outline */
930 NULL, /* free_outline */
931 NULL, /* anchor_point */
932 uniscribe_otf_capability, /* Defined so (font-get FONTOBJ :otf) works. */
933 NULL, /* otf_drive - use shape instead. */
934 NULL, /* start_for_frame */
935 NULL, /* end_for_frame */
936 uniscribe_shape,
937 NULL, /* check */
938 NULL, /* get_variation_glyphs */
939 NULL, /* filter_properties */
940 NULL, /* cached_font_ok */
943 /* Note that this should be called at every startup, not just when dumping,
944 as it needs to test for the existence of the Uniscribe library. */
945 void
946 syms_of_w32uniscribe (void)
948 HMODULE uniscribe;
950 /* Don't init uniscribe when dumping */
951 if (!initialized)
952 return;
954 /* Don't register if uniscribe is not available. */
955 uniscribe = GetModuleHandle ("usp10");
956 if (!uniscribe)
957 return;
959 uniscribe_font_driver.type = Quniscribe;
960 uniscribe_available = 1;
962 register_font_driver (&uniscribe_font_driver, NULL);