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