]> code.delx.au - gnu-emacs/blob - src/w32uniscribe.c
MS-Windows followup to previous commit
[gnu-emacs] / src / w32uniscribe.c
1 /* Font backend for the Microsoft W32 Uniscribe API.
2 Copyright (C) 2008-2015 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 of the License, or
9 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
18
19
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
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"
41
42 struct uniscribe_font_info
43 {
44 struct w32font_info w32_font;
45 SCRIPT_CACHE cache;
46 };
47
48 int uniscribe_available = 0;
49
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);
56
57 static int
58 memq_no_quit (Lisp_Object elt, Lisp_Object list)
59 {
60 while (CONSP (list) && ! EQ (XCAR (list), elt))
61 list = XCDR (list);
62 return (CONSP (list));
63 }
64
65 \f
66 /* Font backend interface implementation. */
67 static Lisp_Object
68 uniscribe_list (struct frame *f, Lisp_Object font_spec)
69 {
70 Lisp_Object fonts = w32font_list_internal (f, font_spec, 1);
71 FONT_ADD_LOG ("uniscribe-list", font_spec, fonts);
72 return fonts;
73 }
74
75 static Lisp_Object
76 uniscribe_match (struct frame *f, Lisp_Object font_spec)
77 {
78 Lisp_Object entity = w32font_match_internal (f, font_spec, 1);
79 FONT_ADD_LOG ("uniscribe-match", font_spec, entity);
80 return entity;
81 }
82
83 static Lisp_Object
84 uniscribe_list_family (struct frame *f)
85 {
86 Lisp_Object list = Qnil;
87 LOGFONT font_match_pattern;
88 HDC dc;
89
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;
93
94 dc = get_frame_dc (f);
95
96 EnumFontFamiliesEx (dc, &font_match_pattern,
97 (FONTENUMPROC) add_opentype_font_name_to_list,
98 (LPARAM) &list, 0);
99 release_frame_dc (f, dc);
100
101 return list;
102 }
103
104 static Lisp_Object
105 uniscribe_open (struct frame *f, Lisp_Object font_entity, int pixel_size)
106 {
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);
112
113 ASET (font_object, FONT_TYPE_INDEX, Quniscribe);
114
115 if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
116 {
117 return Qnil;
118 }
119
120 /* Initialize the cache for this font. */
121 uniscribe_font->cache = NULL;
122
123 /* Uniscribe backend uses glyph indices. */
124 uniscribe_font->w32_font.glyph_idx = ETO_GLYPH_INDEX;
125
126 uniscribe_font->w32_font.font.driver = &uniscribe_font_driver;
127
128 return font_object;
129 }
130
131 static void
132 uniscribe_close (struct font *font)
133 {
134 struct uniscribe_font_info *uniscribe_font
135 = (struct uniscribe_font_info *) font;
136
137 if (uniscribe_font->cache)
138 ScriptFreeCache (&(uniscribe_font->cache));
139
140 w32font_close (font);
141 }
142
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)
147 {
148 HDC context;
149 HFONT old_font;
150 struct frame *f;
151 Lisp_Object capability = Fcons (Qnil, Qnil);
152 Lisp_Object features;
153
154 f = XFRAME (selected_frame);
155 context = get_frame_dc (f);
156 old_font = SelectObject (context, FONT_HANDLE (font));
157
158 features = otf_features (context, "GSUB");
159 XSETCAR (capability, features);
160 features = otf_features (context, "GPOS");
161 XSETCDR (capability, features);
162
163 SelectObject (context, old_font);
164 release_frame_dc (f, context);
165
166 return capability;
167 }
168
169 /* Uniscribe implementation of shape for font backend.
170
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.
176
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)
185 {
186 struct font * font;
187 struct uniscribe_font_info * uniscribe_font;
188 EMACS_UINT nchars;
189 int nitems, max_items, i, max_glyphs, done_glyphs;
190 wchar_t *chars;
191 WORD *glyphs, *clusters;
192 SCRIPT_ITEM *items;
193 SCRIPT_VISATTR *attributes;
194 int *advances;
195 GOFFSET *offsets;
196 ABC overall_metrics;
197 HRESULT result;
198 struct frame * f = NULL;
199 HDC context = NULL;
200 HFONT old_font = NULL;
201
202 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
203 uniscribe_font = (struct uniscribe_font_info *) font;
204
205 /* Get the chars from lgstring in a form we can use with uniscribe. */
206 max_glyphs = nchars = LGSTRING_GLYPH_LEN (lgstring);
207 done_glyphs = 0;
208 chars = (wchar_t *) alloca (nchars * sizeof (wchar_t));
209 /* FIXME: This loop assumes that characters in the input LGSTRING
210 are all inside the BMP. Need to encode characters beyond the BMP
211 as UTF-16. */
212 for (i = 0; i < nchars; i++)
213 {
214 /* lgstring can be bigger than the number of characters in it, in
215 the case where more glyphs are required to display those characters.
216 If that is the case, note the real number of characters. */
217 if (NILP (LGSTRING_GLYPH (lgstring, i)))
218 nchars = i;
219 else
220 chars[i] = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
221 }
222
223 /* First we need to break up the glyph string into runs of glyphs that
224 can be treated together. First try a single run. */
225 max_items = 2;
226 items = xmalloc (sizeof (SCRIPT_ITEM) * max_items + 1);
227
228 while ((result = ScriptItemize (chars, nchars, max_items, NULL, NULL,
229 items, &nitems)) == E_OUTOFMEMORY)
230 {
231 /* If that wasn't enough, keep trying with one more run. */
232 max_items++;
233 items = (SCRIPT_ITEM *) xrealloc (items,
234 sizeof (SCRIPT_ITEM) * max_items + 1);
235 }
236
237 if (FAILED (result))
238 {
239 xfree (items);
240 return Qnil;
241 }
242
243 glyphs = alloca (max_glyphs * sizeof (WORD));
244 clusters = alloca (nchars * sizeof (WORD));
245 attributes = alloca (max_glyphs * sizeof (SCRIPT_VISATTR));
246 advances = alloca (max_glyphs * sizeof (int));
247 offsets = alloca (max_glyphs * sizeof (GOFFSET));
248
249 for (i = 0; i < nitems; i++)
250 {
251 int nglyphs, nchars_in_run;
252 nchars_in_run = items[i+1].iCharPos - items[i].iCharPos;
253 /* Force ScriptShape to generate glyphs in the same order as
254 they are in the input LGSTRING, which is in the logical
255 order. */
256 items[i].a.fLogicalOrder = 1;
257
258 /* Context may be NULL here, in which case the cache should be
259 used without needing to select the font. */
260 result = ScriptShape (context, &(uniscribe_font->cache),
261 chars + items[i].iCharPos, nchars_in_run,
262 max_glyphs - done_glyphs, &(items[i].a),
263 glyphs, clusters, attributes, &nglyphs);
264
265 if (result == E_PENDING && !context)
266 {
267 /* This assumes the selected frame is on the same display as the
268 one we are drawing. It would be better for the frame to be
269 passed in. */
270 f = XFRAME (selected_frame);
271 context = get_frame_dc (f);
272 old_font = SelectObject (context, FONT_HANDLE (font));
273
274 result = ScriptShape (context, &(uniscribe_font->cache),
275 chars + items[i].iCharPos, nchars_in_run,
276 max_glyphs - done_glyphs, &(items[i].a),
277 glyphs, clusters, attributes, &nglyphs);
278 }
279
280 if (result == E_OUTOFMEMORY)
281 {
282 /* Need a bigger lgstring. */
283 lgstring = Qnil;
284 break;
285 }
286 else if (FAILED (result))
287 {
288 /* Can't shape this run - return results so far if any. */
289 break;
290 }
291 else if (items[i].a.fNoGlyphIndex)
292 {
293 /* Glyph indices not supported by this font (or OS), means we
294 can't really do any meaningful shaping. */
295 break;
296 }
297 else
298 {
299 result = ScriptPlace (context, &(uniscribe_font->cache),
300 glyphs, nglyphs, attributes, &(items[i].a),
301 advances, offsets, &overall_metrics);
302 if (result == E_PENDING && !context)
303 {
304 /* Cache not complete... */
305 f = XFRAME (selected_frame);
306 context = get_frame_dc (f);
307 old_font = SelectObject (context, FONT_HANDLE (font));
308
309 result = ScriptPlace (context, &(uniscribe_font->cache),
310 glyphs, nglyphs, attributes, &(items[i].a),
311 advances, offsets, &overall_metrics);
312 }
313 if (SUCCEEDED (result))
314 {
315 int j, from, to, adj_offset = 0;
316
317 from = 0;
318 to = from;
319
320 for (j = 0; j < nglyphs; j++)
321 {
322 int lglyph_index = j + done_glyphs;
323 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, lglyph_index);
324 ABC char_metric;
325 unsigned gl;
326
327 if (NILP (lglyph))
328 {
329 lglyph = LGLYPH_NEW ();
330 LGSTRING_SET_GLYPH (lgstring, lglyph_index, lglyph);
331 }
332 /* Copy to a 32-bit data type to shut up the
333 compiler warning in LGLYPH_SET_CODE about
334 comparison being always false. */
335 gl = glyphs[j];
336 LGLYPH_SET_CODE (lglyph, gl);
337
338 /* Detect clusters, for linking codes back to
339 characters. */
340 if (attributes[j].fClusterStart)
341 {
342 while (from < nchars_in_run && clusters[from] < j)
343 from++;
344 if (from >= nchars_in_run)
345 from = to = nchars_in_run - 1;
346 else
347 {
348 int k;
349 to = nchars_in_run - 1;
350 for (k = from + 1; k < nchars_in_run; k++)
351 {
352 if (clusters[k] > j)
353 {
354 to = k - 1;
355 break;
356 }
357 }
358 }
359
360 /* For RTL text, the Uniscribe shaper prepares
361 the values in ADVANCES array for layout in
362 reverse order, whereby "advance width" is
363 applied to move the pen in reverse direction
364 and _before_ drawing the glyph. Since we
365 draw glyphs in their normal left-to-right
366 order, we need to adjust the coordinates of
367 each non-base glyph in a grapheme cluster via
368 X-OFF component of the gstring's ADJUSTMENT
369 sub-vector. This loop computes, for each
370 grapheme cluster, the initial value of the
371 adjustment for the base character, which is
372 then updated for each successive glyph in the
373 grapheme cluster. */
374 if (items[i].a.fRTL)
375 {
376 int j1 = j;
377
378 adj_offset = 0;
379 while (j1 < nglyphs && !attributes[j1].fClusterStart)
380 {
381 adj_offset += advances[j1];
382 j1++;
383 }
384 }
385 }
386
387 LGLYPH_SET_CHAR (lglyph, chars[items[i].iCharPos
388 + from]);
389 LGLYPH_SET_FROM (lglyph, items[i].iCharPos + from);
390 LGLYPH_SET_TO (lglyph, items[i].iCharPos + to);
391
392 /* Metrics. */
393 LGLYPH_SET_WIDTH (lglyph, advances[j]);
394 LGLYPH_SET_ASCENT (lglyph, font->ascent);
395 LGLYPH_SET_DESCENT (lglyph, font->descent);
396
397 result = ScriptGetGlyphABCWidth (context,
398 &(uniscribe_font->cache),
399 glyphs[j], &char_metric);
400 if (result == E_PENDING && !context)
401 {
402 /* Cache incomplete... */
403 f = XFRAME (selected_frame);
404 context = get_frame_dc (f);
405 old_font = SelectObject (context, FONT_HANDLE (font));
406 result = ScriptGetGlyphABCWidth (context,
407 &(uniscribe_font->cache),
408 glyphs[j], &char_metric);
409 }
410
411 if (SUCCEEDED (result))
412 {
413 int lbearing = char_metric.abcA;
414 int rbearing = char_metric.abcA + char_metric.abcB;
415
416 LGLYPH_SET_LBEARING (lglyph, lbearing);
417 LGLYPH_SET_RBEARING (lglyph, rbearing);
418 }
419 else
420 {
421 LGLYPH_SET_LBEARING (lglyph, 0);
422 LGLYPH_SET_RBEARING (lglyph, advances[j]);
423 }
424
425 if (offsets[j].du || offsets[j].dv
426 /* For non-base glyphs of RTL grapheme clusters,
427 adjust the X offset even if both DU and DV
428 are zero. */
429 || (!attributes[j].fClusterStart && items[i].a.fRTL))
430 {
431 Lisp_Object vec = make_uninit_vector (3);
432
433 if (items[i].a.fRTL)
434 {
435 /* Empirically, it looks like Uniscribe
436 interprets DU in reverse direction for
437 RTL clusters. E.g., if we don't reverse
438 the direction, the Hebrew point HOLAM is
439 drawn above the right edge of the base
440 consonant, instead of above the left edge. */
441 ASET (vec, 0, make_number (-offsets[j].du
442 + adj_offset));
443 /* Update the adjustment value for the width
444 advance of the glyph we just emitted. */
445 adj_offset -= 2 * advances[j];
446 }
447 else
448 ASET (vec, 0, make_number (offsets[j].du + adj_offset));
449 /* In the font definition coordinate system, the
450 Y coordinate points up, while in our screen
451 coordinates Y grows downwards. So we need to
452 reverse the sign of Y-OFFSET here. */
453 ASET (vec, 1, make_number (-offsets[j].dv));
454 /* Based on what ftfont.c does... */
455 ASET (vec, 2, make_number (advances[j]));
456 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
457 }
458 else
459 {
460 LGLYPH_SET_ADJUSTMENT (lglyph, Qnil);
461 /* Update the adjustment value to compensate for
462 the width of the base character. */
463 if (items[i].a.fRTL)
464 adj_offset -= advances[j];
465 }
466 }
467 }
468 }
469 done_glyphs += nglyphs;
470 }
471
472 xfree (items);
473
474 if (context)
475 {
476 SelectObject (context, old_font);
477 release_frame_dc (f, context);
478 }
479
480 if (NILP (lgstring))
481 return Qnil;
482 else
483 return make_number (done_glyphs);
484 }
485
486 /* Uniscribe implementation of encode_char for font backend.
487 Return a glyph code of FONT for character C (Unicode code point).
488 If FONT doesn't have such a glyph, return FONT_INVALID_CODE. */
489 static unsigned
490 uniscribe_encode_char (struct font *font, int c)
491 {
492 HDC context = NULL;
493 struct frame *f = NULL;
494 HFONT old_font = NULL;
495 unsigned code = FONT_INVALID_CODE;
496 wchar_t ch[2];
497 int len;
498 SCRIPT_ITEM* items;
499 int nitems;
500 struct uniscribe_font_info *uniscribe_font
501 = (struct uniscribe_font_info *)font;
502
503 if (c < 0x10000)
504 {
505 ch[0] = (wchar_t) c;
506 len = 1;
507 }
508 else
509 {
510 DWORD surrogate = c - 0x10000;
511
512 /* High surrogate: U+D800 - U+DBFF. */
513 ch[0] = 0xD800 + ((surrogate >> 10) & 0x03FF);
514 /* Low surrogate: U+DC00 - U+DFFF. */
515 ch[1] = 0xDC00 + (surrogate & 0x03FF);
516 len = 2;
517 }
518
519 /* Non BMP characters must be handled by the uniscribe shaping
520 engine as GDI functions (except blindly displaying lines of
521 Unicode text) and the promising looking ScriptGetCMap do not
522 convert surrogate pairs to glyph indexes correctly. */
523 {
524 items = (SCRIPT_ITEM *) alloca (sizeof (SCRIPT_ITEM) * 2 + 1);
525 if (SUCCEEDED (ScriptItemize (ch, len, 2, NULL, NULL, items, &nitems)))
526 {
527 HRESULT result;
528 /* Surrogates seem to need 2 here, even though only one glyph is
529 returned. Indic characters can also produce 2 or more glyphs for
530 a single code point, but they need to use uniscribe_shape
531 above for correct display. */
532 WORD glyphs[2], clusters[2];
533 SCRIPT_VISATTR attrs[2];
534 int nglyphs;
535
536 /* Force ScriptShape to generate glyphs in the logical
537 order. */
538 items[0].a.fLogicalOrder = 1;
539
540 result = ScriptShape (context, &(uniscribe_font->cache),
541 ch, len, 2, &(items[0].a),
542 glyphs, clusters, attrs, &nglyphs);
543
544 if (result == E_PENDING)
545 {
546 /* Use selected frame until API is updated to pass
547 the frame. */
548 f = XFRAME (selected_frame);
549 context = get_frame_dc (f);
550 old_font = SelectObject (context, FONT_HANDLE (font));
551 result = ScriptShape (context, &(uniscribe_font->cache),
552 ch, len, 2, &(items[0].a),
553 glyphs, clusters, attrs, &nglyphs);
554 }
555
556 if (SUCCEEDED (result) && nglyphs == 1)
557 {
558 /* Some fonts return .notdef glyphs instead of failing.
559 (TrueType spec reserves glyph code 0 for .notdef) */
560 if (glyphs[0])
561 code = glyphs[0];
562 }
563 else if (SUCCEEDED (result) || result == E_OUTOFMEMORY)
564 {
565 /* This character produces zero or more than one glyph
566 when shaped. But we still need the return from here
567 to be valid for the shaping engine to be invoked
568 later. */
569 result = ScriptGetCMap (context, &(uniscribe_font->cache),
570 ch, len, 0, glyphs);
571 if (SUCCEEDED (result) && glyphs[0])
572 code = glyphs[0];
573 }
574 }
575 }
576 if (context)
577 {
578 SelectObject (context, old_font);
579 release_frame_dc (f, context);
580 }
581
582 return code;
583 }
584
585 /*
586 Shared with w32font:
587 Lisp_Object uniscribe_get_cache (Lisp_Object frame);
588 void uniscribe_free_entity (Lisp_Object font_entity);
589 int uniscribe_has_char (Lisp_Object entity, int c);
590 void uniscribe_text_extents (struct font *font, unsigned *code,
591 int nglyphs, struct font_metrics *metrics);
592 int uniscribe_draw (struct glyph_string *s, int from, int to,
593 int x, int y, int with_background);
594
595 Unused:
596 int uniscribe_prepare_face (struct frame *f, struct face *face);
597 void uniscribe_done_face (struct frame *f, struct face *face);
598 int uniscribe_get_bitmap (struct font *font, unsigned code,
599 struct font_bitmap *bitmap, int bits_per_pixel);
600 void uniscribe_free_bitmap (struct font *font, struct font_bitmap *bitmap);
601 int uniscribe_anchor_point (struct font *font, unsigned code,
602 int index, int *x, int *y);
603 int uniscribe_start_for_frame (struct frame *f);
604 int uniscribe_end_for_frame (struct frame *f);
605
606 */
607
608 \f
609 /* Callback function for EnumFontFamiliesEx.
610 Adds the name of opentype fonts to a Lisp list (passed in as the
611 lParam arg). */
612 static int CALLBACK ALIGN_STACK
613 add_opentype_font_name_to_list (ENUMLOGFONTEX *logical_font,
614 NEWTEXTMETRICEX *physical_font,
615 DWORD font_type, LPARAM list_object)
616 {
617 Lisp_Object* list = (Lisp_Object *) list_object;
618 Lisp_Object family;
619
620 /* Skip vertical fonts (intended only for printing) */
621 if (logical_font->elfLogFont.lfFaceName[0] == '@')
622 return 1;
623
624 /* Skip non opentype fonts. Count old truetype fonts as opentype,
625 as some of them do contain GPOS and GSUB data that Uniscribe
626 can make use of. */
627 if (!(physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE)
628 && font_type != TRUETYPE_FONTTYPE)
629 return 1;
630
631 /* Skip fonts that have no Unicode coverage. */
632 if (!physical_font->ntmFontSig.fsUsb[3]
633 && !physical_font->ntmFontSig.fsUsb[2]
634 && !physical_font->ntmFontSig.fsUsb[1]
635 && !(physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff))
636 return 1;
637
638 family = intern_font_name (logical_font->elfLogFont.lfFaceName);
639 if (! memq_no_quit (family, *list))
640 *list = Fcons (family, *list);
641
642 return 1;
643 }
644
645 \f
646 /* :otf property handling.
647 Since the necessary Uniscribe APIs for getting font tag information
648 are only available in Vista, we need to parse the font data directly
649 according to the OpenType Specification. */
650
651 /* Push into DWORD backwards to cope with endianness. */
652 #define OTF_TAG(STR) \
653 ((STR[3] << 24) | (STR[2] << 16) | (STR[1] << 8) | STR[0])
654
655 #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \
656 do { \
657 BYTE temp, data[2]; \
658 if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \
659 goto font_table_error; \
660 temp = data[0], data[0] = data[1], data[1] = temp; \
661 memcpy (PTR, data, 2); \
662 } while (0)
663
664 /* Do not reverse the bytes, because we will compare with a OTF_TAG value
665 that has them reversed already. */
666 #define OTF_DWORDTAG_VAL(TABLE, OFFSET, PTR) \
667 do { \
668 if (GetFontData (context, TABLE, OFFSET, PTR, 4) != 4) \
669 goto font_table_error; \
670 } while (0)
671
672 #define OTF_TAG_VAL(TABLE, OFFSET, STR) \
673 do { \
674 if (GetFontData (context, TABLE, OFFSET, STR, 4) != 4) \
675 goto font_table_error; \
676 STR[4] = '\0'; \
677 } while (0)
678
679 #define SNAME(VAL) SDATA (SYMBOL_NAME (VAL))
680
681 /* Check if font supports the otf script/language/features specified.
682 OTF_SPEC is in the format
683 (script lang [(gsub_feature ...)|nil] [(gpos_feature ...)]?) */
684 int
685 uniscribe_check_otf (LOGFONT *font, Lisp_Object otf_spec)
686 {
687 Lisp_Object script, lang, rest;
688 Lisp_Object features[2];
689 DWORD feature_tables[2];
690 DWORD script_tag, default_script, lang_tag = 0;
691 struct frame * f;
692 HDC context;
693 HFONT check_font, old_font;
694 int i, retval = 0;
695 struct gcpro gcpro1;
696
697 /* Check the spec is in the right format. */
698 if (!CONSP (otf_spec) || XINT (Flength (otf_spec)) < 3)
699 return 0;
700
701 /* Break otf_spec into its components. */
702 script = XCAR (otf_spec);
703 rest = XCDR (otf_spec);
704
705 lang = XCAR (rest);
706 rest = XCDR (rest);
707
708 features[0] = XCAR (rest);
709 rest = XCDR (rest);
710 if (NILP (rest))
711 features[1] = Qnil;
712 else
713 features[1] = XCAR (rest);
714
715 /* Set up tags we will use in the search. */
716 feature_tables[0] = OTF_TAG ("GSUB");
717 feature_tables[1] = OTF_TAG ("GPOS");
718 default_script = OTF_TAG ("DFLT");
719 if (NILP (script))
720 script_tag = default_script;
721 else
722 script_tag = OTF_TAG (SNAME (script));
723 if (!NILP (lang))
724 lang_tag = OTF_TAG (SNAME (lang));
725
726 /* Set up graphics context so we can use the font. */
727 f = XFRAME (selected_frame);
728 context = get_frame_dc (f);
729 check_font = CreateFontIndirect (font);
730 old_font = SelectObject (context, check_font);
731
732 /* Everything else is contained within otf_spec so should get
733 marked along with it. */
734 GCPRO1 (otf_spec);
735
736 /* Scan GSUB and GPOS tables. */
737 for (i = 0; i < 2; i++)
738 {
739 int j, n_match_features;
740 unsigned short scriptlist_table, feature_table, n_scripts;
741 unsigned short script_table, langsys_table, n_langs;
742 unsigned short feature_index, n_features;
743 DWORD tbl = feature_tables[i];
744
745 /* Skip if no features requested from this table. */
746 if (NILP (features[i]))
747 continue;
748
749 /* If features is not a cons, this font spec is messed up. */
750 if (!CONSP (features[i]))
751 goto no_support;
752
753 /* Read GPOS/GSUB header. */
754 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
755 OTF_INT16_VAL (tbl, 6, &feature_table);
756 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
757
758 /* Find the appropriate script table. */
759 script_table = 0;
760 for (j = 0; j < n_scripts; j++)
761 {
762 DWORD script_id;
763 OTF_DWORDTAG_VAL (tbl, scriptlist_table + 2 + j * 6, &script_id);
764 if (script_id == script_tag)
765 {
766 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
767 break;
768 }
769 #if 0 /* Causes false positives. */
770 /* If there is a DFLT script defined in the font, use it
771 if the specified script is not found. */
772 else if (script_id == default_script)
773 OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
774 #endif
775 }
776 /* If no specific or default script table was found, then this font
777 does not support the script. */
778 if (!script_table)
779 goto no_support;
780
781 /* Offset is from beginning of scriptlist_table. */
782 script_table += scriptlist_table;
783
784 /* Get default langsys table. */
785 OTF_INT16_VAL (tbl, script_table, &langsys_table);
786
787 /* If lang was specified, see if font contains a specific entry. */
788 if (!NILP (lang))
789 {
790 OTF_INT16_VAL (tbl, script_table + 2, &n_langs);
791
792 for (j = 0; j < n_langs; j++)
793 {
794 DWORD lang_id;
795 OTF_DWORDTAG_VAL (tbl, script_table + 4 + j * 6, &lang_id);
796 if (lang_id == lang_tag)
797 {
798 OTF_INT16_VAL (tbl, script_table + 8 + j * 6, &langsys_table);
799 break;
800 }
801 }
802 }
803
804 if (!langsys_table)
805 goto no_support;
806
807 /* Offset is from beginning of script table. */
808 langsys_table += script_table;
809
810 /* Check the features. Features may contain nil according to
811 documentation in font_prop_validate_otf, so count them. */
812 n_match_features = 0;
813 for (rest = features[i]; CONSP (rest); rest = XCDR (rest))
814 {
815 Lisp_Object feature = XCAR (rest);
816 if (!NILP (feature))
817 n_match_features++;
818 }
819
820 /* If there are no features to check, skip checking. */
821 if (!n_match_features)
822 continue;
823
824 /* First check required feature (if any). */
825 OTF_INT16_VAL (tbl, langsys_table + 2, &feature_index);
826 if (feature_index != 0xFFFF)
827 {
828 char feature_id[5];
829 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
830 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
831 /* Assume no duplicates in the font table. This allows us to mark
832 the features off by simply decrementing a counter. */
833 if (!NILP (Fmemq (intern (feature_id), features[i])))
834 n_match_features--;
835 }
836 /* Now check all the other features. */
837 OTF_INT16_VAL (tbl, langsys_table + 4, &n_features);
838 for (j = 0; j < n_features; j++)
839 {
840 char feature_id[5];
841 OTF_INT16_VAL (tbl, langsys_table + 6 + j * 2, &feature_index);
842 OTF_TAG_VAL (tbl, feature_table + 2 + feature_index * 6, feature_id);
843 /* Assume no duplicates in the font table. This allows us to mark
844 the features off by simply decrementing a counter. */
845 if (!NILP (Fmemq (intern (feature_id), features[i])))
846 n_match_features--;
847 }
848
849 if (n_match_features > 0)
850 goto no_support;
851 }
852
853 retval = 1;
854
855 no_support:
856 font_table_error:
857 /* restore graphics context. */
858 SelectObject (context, old_font);
859 DeleteObject (check_font);
860 release_frame_dc (f, context);
861
862 return retval;
863 }
864
865 static Lisp_Object
866 otf_features (HDC context, char *table)
867 {
868 Lisp_Object script_list = Qnil;
869 unsigned short scriptlist_table, n_scripts, feature_table;
870 DWORD tbl = OTF_TAG (table);
871 int i, j, k;
872
873 /* Look for scripts in the table. */
874 OTF_INT16_VAL (tbl, 4, &scriptlist_table);
875 OTF_INT16_VAL (tbl, 6, &feature_table);
876 OTF_INT16_VAL (tbl, scriptlist_table, &n_scripts);
877
878 for (i = 0; i < n_scripts; i++)
879 {
880 char script[5], lang[5];
881 unsigned short script_table, lang_count, langsys_table, feature_count;
882 Lisp_Object script_tag, langsys_list, langsys_tag, feature_list;
883 unsigned short record_offset = scriptlist_table + 2 + i * 6;
884 OTF_TAG_VAL (tbl, record_offset, script);
885 OTF_INT16_VAL (tbl, record_offset + 4, &script_table);
886
887 /* Offset is from beginning of script table. */
888 script_table += scriptlist_table;
889
890 script_tag = intern (script);
891 langsys_list = Qnil;
892
893 /* Optional default lang. */
894 OTF_INT16_VAL (tbl, script_table, &langsys_table);
895 if (langsys_table)
896 {
897 /* Offset is from beginning of script table. */
898 langsys_table += script_table;
899
900 langsys_tag = Qnil;
901 feature_list = Qnil;
902 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
903 for (k = 0; k < feature_count; k++)
904 {
905 char feature[5];
906 unsigned short index;
907 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
908 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
909 feature_list = Fcons (intern (feature), feature_list);
910 }
911 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
912 langsys_list);
913 }
914
915 /* List of supported languages. */
916 OTF_INT16_VAL (tbl, script_table + 2, &lang_count);
917
918 for (j = 0; j < lang_count; j++)
919 {
920 record_offset = script_table + 4 + j * 6;
921 OTF_TAG_VAL (tbl, record_offset, lang);
922 OTF_INT16_VAL (tbl, record_offset + 4, &langsys_table);
923
924 /* Offset is from beginning of script table. */
925 langsys_table += script_table;
926
927 langsys_tag = intern (lang);
928 feature_list = Qnil;
929 OTF_INT16_VAL (tbl, langsys_table + 4, &feature_count);
930 for (k = 0; k < feature_count; k++)
931 {
932 char feature[5];
933 unsigned short index;
934 OTF_INT16_VAL (tbl, langsys_table + 6 + k * 2, &index);
935 OTF_TAG_VAL (tbl, feature_table + 2 + index * 6, feature);
936 feature_list = Fcons (intern (feature), feature_list);
937 }
938 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
939 langsys_list);
940
941 }
942
943 script_list = Fcons (Fcons (script_tag, langsys_list), script_list);
944 }
945
946 return script_list;
947
948 font_table_error:
949 return Qnil;
950 }
951
952 #undef OTF_INT16_VAL
953 #undef OTF_TAG_VAL
954 #undef OTF_TAG
955
956 \f
957 struct font_driver uniscribe_font_driver =
958 {
959 LISP_INITIALLY_ZERO, /* Quniscribe */
960 0, /* case insensitive */
961 w32font_get_cache,
962 uniscribe_list,
963 uniscribe_match,
964 uniscribe_list_family,
965 NULL, /* free_entity */
966 uniscribe_open,
967 uniscribe_close,
968 NULL, /* prepare_face */
969 NULL, /* done_face */
970 w32font_has_char,
971 uniscribe_encode_char,
972 w32font_text_extents,
973 w32font_draw,
974 NULL, /* get_bitmap */
975 NULL, /* free_bitmap */
976 NULL, /* anchor_point */
977 uniscribe_otf_capability, /* Defined so (font-get FONTOBJ :otf) works. */
978 NULL, /* otf_drive - use shape instead. */
979 NULL, /* start_for_frame */
980 NULL, /* end_for_frame */
981 uniscribe_shape,
982 NULL, /* check */
983 NULL, /* get_variation_glyphs */
984 NULL, /* filter_properties */
985 NULL, /* cached_font_ok */
986 };
987
988 /* Note that this should be called at every startup, not just when dumping,
989 as it needs to test for the existence of the Uniscribe library. */
990 void
991 syms_of_w32uniscribe (void)
992 {
993 HMODULE uniscribe;
994
995 /* Don't init uniscribe when dumping */
996 if (!initialized)
997 return;
998
999 /* Don't register if uniscribe is not available. */
1000 uniscribe = GetModuleHandle ("usp10");
1001 if (!uniscribe)
1002 return;
1003
1004 uniscribe_font_driver.type = Quniscribe;
1005 uniscribe_available = 1;
1006
1007 register_font_driver (&uniscribe_font_driver, NULL);
1008 }