blob: 6c186bd1079751a8bcbc696548ec4d6e8175ab1f [file] [log] [blame]
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001/*
Behdad Esfahbod1c1233e2012-06-08 09:20:53 -04002 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04003 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod552bf3a2012-07-11 16:44:51 -040027#define _WIN32_WINNT 0x0600
Behdad Esfahbodeb56f6a2012-08-07 21:44:25 -040028#define WIN32_LEAN_AND_MEAN
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040029
Behdad Esfahbod027857d2012-07-26 17:34:25 -040030#define HB_SHAPER uniscribe
31#include "hb-shaper-impl-private.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040032
Behdad Esfahbodb4922992011-08-05 20:34:50 -040033#include <windows.h>
34#include <usp10.h>
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -040035#include <rpc.h>
Behdad Esfahbodb4922992011-08-05 20:34:50 -040036
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040037#include "hb-uniscribe.h"
38
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -040039#include "hb-open-file-private.hh"
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020040#include "hb-ot-name-table.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040041#include "hb-ot-tag.h"
42
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040043
44#ifndef HB_DEBUG_UNISCRIBE
45#define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
46#endif
47
48
Chun-wei Fan93a04b82013-06-03 17:49:37 +080049typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040050 const WCHAR *pwcInChars,
51 int cInChars,
52 int cMaxItems,
53 const SCRIPT_CONTROL *psControl,
54 const SCRIPT_STATE *psState,
55 SCRIPT_ITEM *pItems,
56 OPENTYPE_TAG *pScriptTags,
57 int *pcItems
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040058);
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040059
Chun-wei Fan93a04b82013-06-03 17:49:37 +080060typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040061 HDC hdc,
62 SCRIPT_CACHE *psc,
63 SCRIPT_ANALYSIS *psa,
64 OPENTYPE_TAG tagScript,
65 OPENTYPE_TAG tagLangSys,
66 int *rcRangeChars,
67 TEXTRANGE_PROPERTIES **rpRangeProperties,
68 int cRanges,
69 const WCHAR *pwcChars,
70 int cChars,
71 int cMaxGlyphs,
72 WORD *pwLogClust,
73 SCRIPT_CHARPROP *pCharProps,
74 WORD *pwOutGlyphs,
75 SCRIPT_GLYPHPROP *pOutGlyphProps,
76 int *pcGlyphs
77);
78
Chun-wei Fan93a04b82013-06-03 17:49:37 +080079typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040080 HDC hdc,
81 SCRIPT_CACHE *psc,
82 SCRIPT_ANALYSIS *psa,
83 OPENTYPE_TAG tagScript,
84 OPENTYPE_TAG tagLangSys,
85 int *rcRangeChars,
86 TEXTRANGE_PROPERTIES **rpRangeProperties,
87 int cRanges,
88 const WCHAR *pwcChars,
89 WORD *pwLogClust,
90 SCRIPT_CHARPROP *pCharProps,
91 int cChars,
92 const WORD *pwGlyphs,
93 const SCRIPT_GLYPHPROP *pGlyphProps,
94 int cGlyphs,
95 int *piAdvance,
96 GOFFSET *pGoffset,
97 ABC *pABC
98);
99
100
101/* Fallback implementations. */
102
103static HRESULT WINAPI
104hb_ScriptItemizeOpenType(
105 const WCHAR *pwcInChars,
106 int cInChars,
107 int cMaxItems,
108 const SCRIPT_CONTROL *psControl,
109 const SCRIPT_STATE *psState,
110 SCRIPT_ITEM *pItems,
111 OPENTYPE_TAG *pScriptTags,
112 int *pcItems
113)
114{
115{
116 return ScriptItemize (pwcInChars,
117 cInChars,
118 cMaxItems,
119 psControl,
120 psState,
121 pItems,
122 pcItems);
123}
124}
125
126static HRESULT WINAPI
127hb_ScriptShapeOpenType(
128 HDC hdc,
129 SCRIPT_CACHE *psc,
130 SCRIPT_ANALYSIS *psa,
131 OPENTYPE_TAG tagScript,
132 OPENTYPE_TAG tagLangSys,
133 int *rcRangeChars,
134 TEXTRANGE_PROPERTIES **rpRangeProperties,
135 int cRanges,
136 const WCHAR *pwcChars,
137 int cChars,
138 int cMaxGlyphs,
139 WORD *pwLogClust,
140 SCRIPT_CHARPROP *pCharProps,
141 WORD *pwOutGlyphs,
142 SCRIPT_GLYPHPROP *pOutGlyphProps,
143 int *pcGlyphs
144)
145{
146 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
147 return ScriptShape (hdc,
148 psc,
149 pwcChars,
150 cChars,
151 cMaxGlyphs,
152 psa,
153 pwOutGlyphs,
154 pwLogClust,
155 psva,
156 pcGlyphs);
157}
158
159static HRESULT WINAPI
160hb_ScriptPlaceOpenType(
161 HDC hdc,
162 SCRIPT_CACHE *psc,
163 SCRIPT_ANALYSIS *psa,
164 OPENTYPE_TAG tagScript,
165 OPENTYPE_TAG tagLangSys,
166 int *rcRangeChars,
167 TEXTRANGE_PROPERTIES **rpRangeProperties,
168 int cRanges,
169 const WCHAR *pwcChars,
170 WORD *pwLogClust,
171 SCRIPT_CHARPROP *pCharProps,
172 int cChars,
173 const WORD *pwGlyphs,
174 const SCRIPT_GLYPHPROP *pGlyphProps,
175 int cGlyphs,
176 int *piAdvance,
177 GOFFSET *pGoffset,
178 ABC *pABC
179)
180{
181 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
182 return ScriptPlace (hdc,
183 psc,
184 pwGlyphs,
185 cGlyphs,
186 psva,
187 psa,
188 piAdvance,
189 pGoffset,
190 pABC);
191}
192
193
194struct hb_uniscribe_shaper_funcs_t {
195 SIOT ScriptItemizeOpenType;
196 SSOT ScriptShapeOpenType;
197 SPOT ScriptPlaceOpenType;
198
199 inline void init (void)
200 {
Chun-wei Fan93a04b82013-06-03 17:49:37 +0800201 HMODULE hinstLib;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400202 this->ScriptItemizeOpenType = NULL;
203 this->ScriptShapeOpenType = NULL;
204 this->ScriptPlaceOpenType = NULL;
205
Chun-wei Fan93a04b82013-06-03 17:49:37 +0800206 hinstLib = GetModuleHandle("usp10.dll");
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400207 if (hinstLib)
208 {
209 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
210 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
211 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
212 }
213 if (!this->ScriptItemizeOpenType ||
214 !this->ScriptShapeOpenType ||
215 !this->ScriptPlaceOpenType)
216 {
217 DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back.");
218 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
219 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
220 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
221 }
222 }
223};
224static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
225
226static inline void
227free_uniscribe_funcs (void)
228{
229 free (uniscribe_funcs);
230}
231
232
233static hb_uniscribe_shaper_funcs_t *
234hb_uniscribe_shaper_get_funcs (void)
235{
236retry:
237 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
238
239 if (unlikely (!funcs))
240 {
241 funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
242 if (unlikely (!funcs))
243 return NULL;
244
245 funcs->init ();
246
247 if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) {
248 free (funcs);
249 goto retry;
250 }
251
252#ifdef HAVE_ATEXIT
253 atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */
254#endif
255 }
256
257 return funcs;
258}
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400259
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200260
Behdad Esfahbodcfe98822012-07-27 03:06:30 -0400261HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face)
262HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font)
Behdad Esfahbodcfe98822012-07-27 03:06:30 -0400263
264
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400265/*
266 * shaper face data
267 */
Behdad Esfahbod71388b32011-08-24 02:09:04 +0200268
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400269struct hb_uniscribe_shaper_face_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200270 HANDLE fh;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400271 hb_uniscribe_shaper_funcs_t *funcs;
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400272 wchar_t face_name[LF_FACESIZE];
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400273};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200274
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400275/* Destroys blob. */
276static hb_blob_t *
277_hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400278{
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400279 unsigned int length, new_length = 0;
280 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400281 /* We'll create a private name for the font from a UUID using a simple,
282 * somewhat base64-like encoding scheme */
283 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
284 UUID id;
285 UuidCreate ((UUID*) &id);
286 unsigned int name_str_len = 0;
287 new_name[name_str_len++] = 'F';
288 new_name[name_str_len++] = '_';
289 unsigned char *p = (unsigned char *) &id;
290 for (unsigned int i = 0; i < 16; i += 2)
291 {
292 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
293 * using the bits in groups of 5,5,6 to select chars from enc.
294 * This will generate 24 characters; with the 'F_' prefix we already provided,
295 * the name will be 26 chars (plus the NUL terminator), so will always fit within
296 * face_name (LF_FACESIZE = 32). */
297 new_name[name_str_len++] = enc[p[i] >> 3];
298 new_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
299 new_name[name_str_len++] = enc[p[i + 1] & 0x3f];
300 }
301 new_name[name_str_len] = 0;
302
303 /* Create a copy of the font data, with the 'name' table replaced by a table
304 * that names the font with our private F_* name created above.
305 * For simplicity, we just append a new 'name' table and update the sfnt directory;
306 * the original table is left in place, but unused. */
307
308 /* The new table will contain just 5 name IDs: family, style, unique, full, PS.
309 * All of them point to the same name data with our unique F_* name. */
310
311 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
312
313 const unsigned int name_header_size = 3 * 2; /* NameTableHeader: USHORT x 3 */
314 const unsigned int name_record_size = 6 * 2; /* NameRecord: USHORT x 6 */
315 const unsigned int name_count = sizeof (name_IDs) / sizeof (name_IDs[0]);
316 unsigned int name_table_length = name_header_size +
317 name_count * name_record_size +
318 name_str_len * 2; /* for name data in UTF16BE form */
319
320 unsigned int name_table_offset = (length + 3) & ~3;
321
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400322 new_length = name_table_offset + (name_table_length + 3) & ~3;
323 char *new_sfnt_data = (char *) calloc (1, new_length);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400324 if (!new_sfnt_data)
325 return NULL;
326
327 memcpy(new_sfnt_data, orig_sfnt_data, length);
328
329 unsigned char *name_table_data = (unsigned char *) (new_sfnt_data + name_table_offset);
330
331 p = name_table_data;
332 *p++ = 0x00; *p++ = 0x00; /* format = 0 */
333 *p++ = 0x00; *p++ = name_count; /* number of name records */
334 *p++ = 0x00; *p++ = name_header_size + name_count * name_record_size; /* offset to string data */
335
336 for (unsigned int i = 0; i < name_count; i++) {
337 *p++ = 0x00; *p++ = 0x03; /* platformID */
338 *p++ = 0x00; *p++ = 0x01; /* encodingID */
339 *p++ = 0x04; *p++ = 0x09; /* languageID = 0x0409 */
340 *p++ = 0x00; *p++ = name_IDs[i]; /* nameID */
341 *p++ = 0x00; *p++ = name_str_len * 2; /* string length (bytes) */
342 *p++ = 0x00; *p++ = 0x00; /* string offset */
343 }
344
345 for (unsigned int i = 0; i < name_str_len; i++) {
346 /* copy string data from face_name, converting wchar_t to UTF16BE */
347 *p++ = new_name[i] >> 8;
348 *p++ = new_name[i] & 0xff;
349 }
350
351 /* calculate new name table checksum */
352 uint32_t checksum = 0;
353 while (name_table_data < p) {
354 checksum += (name_table_data[0] << 24) +
355 (name_table_data[1] << 16) +
356 (name_table_data[2] << 8) +
357 name_table_data[3];
358 name_table_data += 4;
359 }
360
361 /* adjust name table entry to point to new name table */
362 OT::OpenTypeFontFace *face = (OT::OpenTypeFontFace *) (new_sfnt_data);
363 unsigned int index;
364 if (face->find_table_index (HB_OT_TAG_name, &index))
365 {
366 const OT::TableRecord& record = face->get_table (index);
367 OT::TableRecord *rp = const_cast<OT::TableRecord *> (&record);
368 rp->checkSum.set(checksum);
369 rp->offset.set(name_table_offset);
370 rp->length.set(name_table_length);
371 }
372
373 /* The checkSumAdjustment field in the 'head' table is now wrong,
374 but AFAIK that doesn't actually cause any problems so I haven't
375 bothered to fix it. */
376
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400377 hb_blob_destroy (blob);
378 return hb_blob_create (new_sfnt_data, new_length, HB_MEMORY_MODE_WRITABLE, NULL, free);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400379}
380
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400381hb_uniscribe_shaper_face_data_t *
382_hb_uniscribe_shaper_face_data_create (hb_face_t *face)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200383{
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400384 hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200385 if (unlikely (!data))
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400386 return NULL;
Behdad Esfahboda3bd8a02011-08-24 03:22:49 +0200387
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400388 data->funcs = hb_uniscribe_shaper_get_funcs ();
389 if (unlikely (!data->funcs))
390 {
391 free (data);
392 return NULL;
393 }
394
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200395 hb_blob_t *blob = hb_face_reference_blob (face);
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400396 if (unlikely (!hb_blob_get_length (blob)))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200397 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
398
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400399 blob = _hb_rename_font (blob, data->face_name);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400400
401 DWORD num_fonts_installed;
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400402 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL),
403 hb_blob_get_length (blob),
404 0, &num_fonts_installed);
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400405 if (unlikely (!data->fh)) {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200406 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400407 free (data);
408 return NULL;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200409 }
410
411 return data;
412}
413
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400414void
415_hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
416{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400417 RemoveFontMemResourceEx (data->fh);
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400418 free (data);
419}
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200420
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400421
422/*
423 * shaper font data
424 */
425
426struct hb_uniscribe_shaper_font_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200427 HDC hdc;
Behdad Esfahbodd6660352011-08-10 22:08:36 +0200428 LOGFONTW log_font;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200429 HFONT hfont;
430 SCRIPT_CACHE script_cache;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400431};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200432
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400433static bool
434populate_log_font (LOGFONTW *lf,
435 hb_font_t *font)
436{
437 memset (lf, 0, sizeof (*lf));
438 lf->lfHeight = -font->y_scale;
439 lf->lfCharSet = DEFAULT_CHARSET;
440
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400441 hb_face_t *face = font->face;
442 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400443
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400444 memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName));
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400445
446 return true;
447}
448
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400449hb_uniscribe_shaper_font_data_t *
450_hb_uniscribe_shaper_font_data_create (hb_font_t *font)
451{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400452 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL;
453
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400454 hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400455 if (unlikely (!data))
456 return NULL;
457
458 data->hdc = GetDC (NULL);
459
460 if (unlikely (!populate_log_font (&data->log_font, font))) {
461 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
462 _hb_uniscribe_shaper_font_data_destroy (data);
463 return NULL;
464 }
465
466 data->hfont = CreateFontIndirectW (&data->log_font);
467 if (unlikely (!data->hfont)) {
468 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
469 _hb_uniscribe_shaper_font_data_destroy (data);
470 return NULL;
471 }
472
473 if (!SelectObject (data->hdc, data->hfont)) {
474 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
475 _hb_uniscribe_shaper_font_data_destroy (data);
476 return NULL;
477 }
478
479 return data;
480}
481
482void
483_hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200484{
485 if (data->hdc)
486 ReleaseDC (NULL, data->hdc);
487 if (data->hfont)
488 DeleteObject (data->hfont);
489 if (data->script_cache)
490 ScriptFreeCache (&data->script_cache);
491 free (data);
492}
493
Behdad Esfahbod0ae6dbf2012-12-09 18:37:38 -0500494LOGFONTW *
495hb_uniscribe_font_get_logfontw (hb_font_t *font)
496{
497 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
498 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
499 return &font_data->log_font;
500}
501
502HFONT
503hb_uniscribe_font_get_hfont (hb_font_t *font)
504{
505 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
506 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
507 return font_data->hfont;
508}
509
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400510
511/*
512 * shaper shape_plan data
513 */
514
515struct hb_uniscribe_shaper_shape_plan_data_t {};
516
517hb_uniscribe_shaper_shape_plan_data_t *
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400518_hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
519 const hb_feature_t *user_features HB_UNUSED,
520 unsigned int num_user_features HB_UNUSED)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200521{
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400522 return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
523}
524
525void
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400526_hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400527{
528}
529
530
531/*
532 * shaper
533 */
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200534
535
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400536hb_bool_t
Behdad Esfahbodbd26b4d2012-07-26 22:05:39 -0400537_hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
538 hb_font_t *font,
Behdad Esfahbod6bd9b472012-04-12 14:53:53 -0400539 hb_buffer_t *buffer,
540 const hb_feature_t *features,
541 unsigned int num_features)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400542{
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400543 hb_face_t *face = font->face;
544 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
545 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400546 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400547
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200548#define FAIL(...) \
549 HB_STMT_START { \
550 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400551 return false; \
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200552 } HB_STMT_END;
553
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200554 HRESULT hr;
555
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400556retry:
557
558 unsigned int scratch_size;
559 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
560
561 /* Allocate char buffers; they all fit */
562
563#define ALLOCATE_ARRAY(Type, name, len) \
564 Type *name = (Type *) scratch; \
Behdad Esfahbod91e721e2012-07-25 19:20:34 -0400565 scratch += (len) * sizeof ((name)[0]); \
566 scratch_size -= (len) * sizeof ((name)[0]);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400567
568#define utf16_index() var1.u32
569
570 WCHAR *pchars = (WCHAR *) scratch;
571 unsigned int chars_len = 0;
572 for (unsigned int i = 0; i < buffer->len; i++) {
573 hb_codepoint_t c = buffer->info[i].codepoint;
574 buffer->info[i].utf16_index() = chars_len;
575 if (likely (c < 0x10000))
576 pchars[chars_len++] = c;
577 else if (unlikely (c >= 0x110000))
578 pchars[chars_len++] = 0xFFFD;
579 else {
580 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10);
581 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1));
582 }
583 }
584
585 ALLOCATE_ARRAY (WCHAR, wchars, chars_len);
586 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
587 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
588
589 /* On Windows, we don't care about alignment...*/
590 unsigned int glyphs_size = scratch_size / (sizeof (WORD) +
591 sizeof (SCRIPT_GLYPHPROP) +
592 sizeof (int) +
593 sizeof (GOFFSET) +
594 sizeof (uint32_t));
595
596 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
597 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
598 ALLOCATE_ARRAY (int, advances, glyphs_size);
599 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
600 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
601
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400602 /* Note:
603 * We can't touch the contents of glyph_props. Our fallback
604 * implementations of Shape and Place functions use that buffer
605 * by casting it to a different type. It works because they
606 * both agree about it, but if we want to access it here we
607 * need address that issue first.
608 */
609
Behdad Esfahbod91e721e2012-07-25 19:20:34 -0400610#undef ALLOCATE_ARRAY
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400611
Behdad Esfahbod87296912012-06-08 14:18:30 -0400612#define MAX_ITEMS 256
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400613
614 SCRIPT_ITEM items[MAX_ITEMS + 1];
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400615 SCRIPT_CONTROL bidi_control = {0};
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400616 SCRIPT_STATE bidi_state = {0};
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400617 ULONG script_tags[MAX_ITEMS];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400618 int item_count;
619
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400620 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400621 //bidi_control.fMergeNeutralItems = true;
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400622 *(uint32_t*)&bidi_control |= 1<<24;
623
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400624 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
Behdad Esfahbod29eac8f2012-06-08 09:26:17 -0400625 bidi_state.fOverrideDirection = 1;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400626
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400627 hr = funcs->ScriptItemizeOpenType (wchars,
628 chars_len,
629 MAX_ITEMS,
630 &bidi_control,
631 &bidi_state,
632 items,
633 script_tags,
634 &item_count);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400635 if (unlikely (FAILED (hr)))
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400636 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400637
638#undef MAX_ITEMS
639
640 int *range_char_counts = NULL;
641 TEXTRANGE_PROPERTIES **range_properties = NULL;
642 int range_count = 0;
643 if (num_features) {
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400644 /* TODO setup ranges */
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400645 }
646
Behdad Esfahbodfcd6f532012-06-08 09:59:43 -0400647 OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400648
649 unsigned int glyphs_offset = 0;
650 unsigned int glyphs_len;
Behdad Esfahbodb069c3c2012-06-08 10:10:29 -0400651 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
652 for (unsigned int j = 0; j < item_count; j++)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400653 {
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400654 unsigned int i = backward ? item_count - 1 - j : j;
655 unsigned int chars_offset = items[i].iCharPos;
656 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
Behdad Esfahbod1c1233e2012-06-08 09:20:53 -0400657
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400658 retry_shape:
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400659 hr = funcs->ScriptShapeOpenType (font_data->hdc,
660 &font_data->script_cache,
661 &items[i].a,
662 script_tags[i],
663 language_tag,
664 range_char_counts,
665 range_properties,
666 range_count,
667 wchars + chars_offset,
668 item_chars_len,
669 glyphs_size - glyphs_offset,
670 /* out */
671 log_clusters + chars_offset,
672 char_props + chars_offset,
673 glyphs + glyphs_offset,
674 glyph_props + glyphs_offset,
675 (int *) &glyphs_len);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400676
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400677 if (unlikely (items[i].a.fNoGlyphIndex))
678 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
679 if (unlikely (hr == E_OUTOFMEMORY))
680 {
681 buffer->ensure (buffer->allocated * 2);
682 if (buffer->in_error)
683 FAIL ("Buffer resize failed");
684 goto retry;
685 }
686 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400687 {
688 if (items[i].a.eScript == SCRIPT_UNDEFINED)
689 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
690 items[i].a.eScript = SCRIPT_UNDEFINED;
691 goto retry_shape;
692 }
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400693 if (unlikely (FAILED (hr)))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400694 {
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400695 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400696 }
697
698 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
699 log_clusters[j] += glyphs_offset;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400700
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400701 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
702 &font_data->script_cache,
703 &items[i].a,
704 script_tags[i],
705 language_tag,
706 range_char_counts,
707 range_properties,
708 range_count,
709 wchars + chars_offset,
710 log_clusters + chars_offset,
711 char_props + chars_offset,
712 item_chars_len,
713 glyphs + glyphs_offset,
714 glyph_props + glyphs_offset,
715 glyphs_len,
716 /* out */
717 advances + glyphs_offset,
718 offsets + glyphs_offset,
719 NULL);
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400720 if (unlikely (FAILED (hr)))
721 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400722
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400723 glyphs_offset += glyphs_len;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400724 }
725 glyphs_len = glyphs_offset;
726
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400727 /* Ok, we've got everything we need, now compose output buffer,
728 * very, *very*, carefully! */
729
730 /* Calculate visual-clusters. That's what we ship. */
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400731 for (unsigned int i = 0; i < glyphs_len; i++)
732 vis_clusters[i] = -1;
Behdad Esfahbod577326b2011-08-07 01:04:40 -0400733 for (unsigned int i = 0; i < buffer->len; i++) {
734 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
735 *p = MIN (*p, buffer->info[i].cluster);
736 }
Behdad Esfahbodb069c3c2012-06-08 10:10:29 -0400737 if (!backward) {
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400738 for (unsigned int i = 1; i < glyphs_len; i++)
Behdad Esfahbod8e7beba2012-06-08 10:22:06 -0400739 if (vis_clusters[i] == -1)
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400740 vis_clusters[i] = vis_clusters[i - 1];
741 } else {
742 for (int i = glyphs_len - 2; i >= 0; i--)
Behdad Esfahbod8e7beba2012-06-08 10:22:06 -0400743 if (vis_clusters[i] == -1)
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400744 vis_clusters[i] = vis_clusters[i + 1];
745 }
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400746
747#undef utf16_index
748
749 buffer->ensure (glyphs_len);
750 if (buffer->in_error)
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400751 FAIL ("Buffer in error");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400752
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400753#undef FAIL
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400754
755 /* Set glyph infos */
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200756 buffer->len = 0;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400757 for (unsigned int i = 0; i < glyphs_len; i++)
758 {
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200759 hb_glyph_info_t *info = &buffer->info[buffer->len++];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400760
761 info->codepoint = glyphs[i];
762 info->cluster = vis_clusters[i];
763
764 /* The rest is crap. Let's store position info there for now. */
765 info->mask = advances[i];
766 info->var1.u32 = offsets[i].du;
767 info->var2.u32 = offsets[i].dv;
768 }
769
770 /* Set glyph positions */
771 buffer->clear_positions ();
772 for (unsigned int i = 0; i < glyphs_len; i++)
773 {
774 hb_glyph_info_t *info = &buffer->info[i];
775 hb_glyph_position_t *pos = &buffer->pos[i];
776
777 /* TODO vertical */
778 pos->x_advance = info->mask;
779 pos->x_offset = info->var1.u32;
780 pos->y_offset = info->var2.u32;
781 }
782
783 /* Wow, done! */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400784 return true;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400785}
786
787