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