blob: e93cf7f419a2c3161d40796b6940ab388d15a4ce [file] [log] [blame]
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001/*
Behdad Esfahbod36136962013-08-12 00:33:28 -04002 * Copyright © 2011,2012,2013 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 Esfahbodc77ae402018-08-25 22:36:36 -070027#include "hb.hh"
Behdad Esfahbodaa3450c2019-06-17 22:41:49 -070028
29#ifdef HAVE_UNISCRIBE
30
Behdad Esfahbod8fe15482019-06-26 13:49:42 -070031#ifdef HB_NO_OT_TAG
32#error "Cannot compile 'uniscribe' shaper with HB_NO_OT_TAG."
33#endif
34
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070035#include "hb-shaper-impl.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040036
Behdad Esfahbodb4922992011-08-05 20:34:50 -040037#include <windows.h>
38#include <usp10.h>
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -040039#include <rpc.h>
Behdad Esfahbodb4922992011-08-05 20:34:50 -040040
Eli Zaretskiid5e5f372019-06-05 22:20:03 +030041#ifndef E_NOT_SUFFICIENT_BUFFER
42#define E_NOT_SUFFICIENT_BUFFER HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)
43#endif
44
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040045#include "hb-uniscribe.h"
46
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070047#include "hb-open-file.hh"
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020048#include "hb-ot-name-table.hh"
Behdad Esfahbod46072b72018-10-27 04:21:20 -070049#include "hb-ot-layout.h"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040050
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040051
Behdad Esfahbod04981ee2018-10-27 04:40:43 -070052/**
53 * SECTION:hb-uniscribe
54 * @title: hb-uniscribe
55 * @short_description: Windows integration
56 * @include: hb-uniscribe.h
57 *
58 * Functions for using HarfBuzz with the Windows fonts.
59 **/
60
Chun-wei Fan93a04b82013-06-03 17:49:37 +080061typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040062 const WCHAR *pwcInChars,
63 int cInChars,
64 int cMaxItems,
65 const SCRIPT_CONTROL *psControl,
66 const SCRIPT_STATE *psState,
67 SCRIPT_ITEM *pItems,
68 OPENTYPE_TAG *pScriptTags,
69 int *pcItems
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040070);
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040071
Chun-wei Fan93a04b82013-06-03 17:49:37 +080072typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040073 HDC hdc,
74 SCRIPT_CACHE *psc,
75 SCRIPT_ANALYSIS *psa,
76 OPENTYPE_TAG tagScript,
77 OPENTYPE_TAG tagLangSys,
78 int *rcRangeChars,
79 TEXTRANGE_PROPERTIES **rpRangeProperties,
80 int cRanges,
81 const WCHAR *pwcChars,
82 int cChars,
83 int cMaxGlyphs,
84 WORD *pwLogClust,
85 SCRIPT_CHARPROP *pCharProps,
86 WORD *pwOutGlyphs,
87 SCRIPT_GLYPHPROP *pOutGlyphProps,
88 int *pcGlyphs
89);
90
Chun-wei Fan93a04b82013-06-03 17:49:37 +080091typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
Behdad Esfahbod2a17f952013-05-28 13:10:51 -040092 HDC hdc,
93 SCRIPT_CACHE *psc,
94 SCRIPT_ANALYSIS *psa,
95 OPENTYPE_TAG tagScript,
96 OPENTYPE_TAG tagLangSys,
97 int *rcRangeChars,
98 TEXTRANGE_PROPERTIES **rpRangeProperties,
99 int cRanges,
100 const WCHAR *pwcChars,
101 WORD *pwLogClust,
102 SCRIPT_CHARPROP *pCharProps,
103 int cChars,
104 const WORD *pwGlyphs,
105 const SCRIPT_GLYPHPROP *pGlyphProps,
106 int cGlyphs,
107 int *piAdvance,
108 GOFFSET *pGoffset,
109 ABC *pABC
110);
111
112
113/* Fallback implementations. */
114
115static HRESULT WINAPI
116hb_ScriptItemizeOpenType(
117 const WCHAR *pwcInChars,
118 int cInChars,
119 int cMaxItems,
120 const SCRIPT_CONTROL *psControl,
121 const SCRIPT_STATE *psState,
122 SCRIPT_ITEM *pItems,
123 OPENTYPE_TAG *pScriptTags,
124 int *pcItems
125)
126{
127{
128 return ScriptItemize (pwcInChars,
129 cInChars,
130 cMaxItems,
131 psControl,
132 psState,
133 pItems,
134 pcItems);
135}
136}
137
138static HRESULT WINAPI
139hb_ScriptShapeOpenType(
140 HDC hdc,
141 SCRIPT_CACHE *psc,
142 SCRIPT_ANALYSIS *psa,
143 OPENTYPE_TAG tagScript,
144 OPENTYPE_TAG tagLangSys,
145 int *rcRangeChars,
146 TEXTRANGE_PROPERTIES **rpRangeProperties,
147 int cRanges,
148 const WCHAR *pwcChars,
149 int cChars,
150 int cMaxGlyphs,
151 WORD *pwLogClust,
152 SCRIPT_CHARPROP *pCharProps,
153 WORD *pwOutGlyphs,
154 SCRIPT_GLYPHPROP *pOutGlyphProps,
155 int *pcGlyphs
156)
157{
158 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
159 return ScriptShape (hdc,
160 psc,
161 pwcChars,
162 cChars,
163 cMaxGlyphs,
164 psa,
165 pwOutGlyphs,
166 pwLogClust,
167 psva,
168 pcGlyphs);
169}
170
171static HRESULT WINAPI
172hb_ScriptPlaceOpenType(
173 HDC hdc,
174 SCRIPT_CACHE *psc,
175 SCRIPT_ANALYSIS *psa,
176 OPENTYPE_TAG tagScript,
177 OPENTYPE_TAG tagLangSys,
178 int *rcRangeChars,
179 TEXTRANGE_PROPERTIES **rpRangeProperties,
180 int cRanges,
181 const WCHAR *pwcChars,
182 WORD *pwLogClust,
183 SCRIPT_CHARPROP *pCharProps,
184 int cChars,
185 const WORD *pwGlyphs,
186 const SCRIPT_GLYPHPROP *pGlyphProps,
187 int cGlyphs,
188 int *piAdvance,
189 GOFFSET *pGoffset,
190 ABC *pABC
191)
192{
193 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
194 return ScriptPlace (hdc,
195 psc,
196 pwGlyphs,
197 cGlyphs,
198 psva,
199 psa,
200 piAdvance,
201 pGoffset,
202 pABC);
203}
204
205
Behdad Esfahbod747d2562018-08-12 17:32:10 -0700206struct hb_uniscribe_shaper_funcs_t
207{
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400208 SIOT ScriptItemizeOpenType;
209 SSOT ScriptShapeOpenType;
210 SPOT ScriptPlaceOpenType;
211
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330212 void init ()
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400213 {
Chun-wei Fan93a04b82013-06-03 17:49:37 +0800214 HMODULE hinstLib;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200215 this->ScriptItemizeOpenType = nullptr;
216 this->ScriptShapeOpenType = nullptr;
217 this->ScriptPlaceOpenType = nullptr;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400218
Behdad Esfahbod757a7a92013-08-28 12:35:13 -0400219 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400220 if (hinstLib)
221 {
Behdad Esfahbod9a1df822019-01-28 14:15:18 -0500222#pragma GCC diagnostic push
223#pragma GCC diagnostic ignored "-Wcast-function-type"
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400224 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
225 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
226 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
Behdad Esfahbod9a1df822019-01-28 14:15:18 -0500227#pragma GCC diagnostic pop
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400228 }
229 if (!this->ScriptItemizeOpenType ||
230 !this->ScriptShapeOpenType ||
231 !this->ScriptPlaceOpenType)
232 {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200233 DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back.");
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400234 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
235 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
236 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
237 }
238 }
239};
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700240
Ebrahim Byagowi1f943882019-07-16 11:24:29 +0430241#if HB_USE_ATEXIT
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330242static void free_static_uniscribe_shaper_funcs ();
Ebrahim Byagowi1f943882019-07-16 11:24:29 +0430243#endif
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700244
245static struct hb_uniscribe_shaper_funcs_lazy_loader_t : hb_lazy_loader_t<hb_uniscribe_shaper_funcs_t,
246 hb_uniscribe_shaper_funcs_lazy_loader_t>
247{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330248 static hb_uniscribe_shaper_funcs_t *create ()
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700249 {
250 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
251 if (unlikely (!funcs))
252 return nullptr;
253
254 funcs->init ();
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400255
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500256#if HB_USE_ATEXIT
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700257 atexit (free_static_uniscribe_shaper_funcs);
258#endif
Ebrahim Byagowi70d36542018-03-30 05:00:28 +0430259
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700260 return funcs;
261 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330262 static void destroy (hb_uniscribe_shaper_funcs_t *p)
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700263 {
264 free ((void *) p);
265 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330266 static hb_uniscribe_shaper_funcs_t *get_null ()
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700267 {
268 return nullptr;
269 }
270} static_uniscribe_shaper_funcs;
271
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500272#if HB_USE_ATEXIT
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700273static
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330274void free_static_uniscribe_shaper_funcs ()
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700275{
276 static_uniscribe_shaper_funcs.free_instance ();
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400277}
Ebrahim Byagowi70d36542018-03-30 05:00:28 +0430278#endif
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400279
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400280static hb_uniscribe_shaper_funcs_t *
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330281hb_uniscribe_shaper_get_funcs ()
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400282{
Behdad Esfahbod9bbd1cd2018-08-12 18:20:53 -0700283 return static_uniscribe_shaper_funcs.get_unconst ();
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400284}
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400285
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200286
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400287struct active_feature_t {
288 OPENTYPE_FEATURE_RECORD rec;
289 unsigned int order;
290
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400291 HB_INTERNAL static int cmp (const void *pa, const void *pb) {
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600292 const active_feature_t *a = (const active_feature_t *) pa;
293 const active_feature_t *b = (const active_feature_t *) pb;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400294 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
295 a->order < b->order ? -1 : a->order > b->order ? 1 :
296 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
297 0;
298 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330299 bool operator== (const active_feature_t *f)
300 { return cmp (this, f) == 0; }
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400301};
302
303struct feature_event_t {
304 unsigned int index;
305 bool start;
306 active_feature_t feature;
307
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400308 HB_INTERNAL static int cmp (const void *pa, const void *pb)
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330309 {
Behdad Esfahbod98acdde2017-10-31 11:17:43 -0600310 const feature_event_t *a = (const feature_event_t *) pa;
311 const feature_event_t *b = (const feature_event_t *) pb;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400312 return a->index < b->index ? -1 : a->index > b->index ? 1 :
313 a->start < b->start ? -1 : a->start > b->start ? 1 :
314 active_feature_t::cmp (&a->feature, &b->feature);
315 }
316};
317
318struct range_record_t {
319 TEXTRANGE_PROPERTIES props;
320 unsigned int index_first; /* == start */
321 unsigned int index_last; /* == end - 1 */
322};
323
Behdad Esfahbodcfe98822012-07-27 03:06:30 -0400324
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400325/*
326 * shaper face data
327 */
Behdad Esfahbod71388b32011-08-24 02:09:04 +0200328
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700329struct hb_uniscribe_face_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200330 HANDLE fh;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400331 hb_uniscribe_shaper_funcs_t *funcs;
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400332 wchar_t face_name[LF_FACESIZE];
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400333};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200334
Behdad Esfahbodbdeea602013-07-21 16:22:10 -0400335/* face_name should point to a wchar_t[LF_FACESIZE] object. */
336static void
337_hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400338{
339 /* We'll create a private name for the font from a UUID using a simple,
340 * somewhat base64-like encoding scheme */
341 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
342 UUID id;
343 UuidCreate ((UUID*) &id);
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200344 static_assert ((2 + 3 * (16/2) < LF_FACESIZE), "");
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400345 unsigned int name_str_len = 0;
Behdad Esfahbodbdeea602013-07-21 16:22:10 -0400346 face_name[name_str_len++] = 'F';
347 face_name[name_str_len++] = '_';
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400348 unsigned char *p = (unsigned char *) &id;
349 for (unsigned int i = 0; i < 16; i += 2)
350 {
351 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
352 * using the bits in groups of 5,5,6 to select chars from enc.
353 * This will generate 24 characters; with the 'F_' prefix we already provided,
354 * the name will be 26 chars (plus the NUL terminator), so will always fit within
355 * face_name (LF_FACESIZE = 32). */
Behdad Esfahbodbdeea602013-07-21 16:22:10 -0400356 face_name[name_str_len++] = enc[p[i] >> 3];
357 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
358 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400359 }
Behdad Esfahbodbdeea602013-07-21 16:22:10 -0400360 face_name[name_str_len] = 0;
361 if (plen)
362 *plen = name_str_len;
363}
364
365/* Destroys blob. */
366static hb_blob_t *
367_hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
368{
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400369 /* Create a copy of the font data, with the 'name' table replaced by a
370 * table that names the font with our private F_* name created above.
371 * For simplicity, we just append a new 'name' table and update the
372 * sfnt directory; the original table is left in place, but unused.
373 *
374 * The new table will contain just 5 name IDs: family, style, unique,
375 * full, PS. All of them point to the same name data with our unique name.
376 */
377
Behdad Esfahboded7b2e52018-08-01 23:59:09 -0700378 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (blob);
Behdad Esfahbod8b427c72013-07-22 10:57:05 -0400379
Behdad Esfahbodbdeea602013-07-21 16:22:10 -0400380 unsigned int length, new_length, name_str_len;
381 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
382
383 _hb_generate_unique_face_name (new_name, &name_str_len);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400384
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400385 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
386
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400387 unsigned int name_table_length = OT::name::min_size +
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430388 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
389 name_str_len * 2; /* for name data in UTF16BE form */
Behdad Esfahbod48baf7e2018-03-28 13:38:51 -0700390 unsigned int padded_name_table_length = ((name_table_length + 3) & ~3);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400391 unsigned int name_table_offset = (length + 3) & ~3;
392
Behdad Esfahbod48baf7e2018-03-28 13:38:51 -0700393 new_length = name_table_offset + padded_name_table_length;
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400394 void *new_sfnt_data = calloc (1, new_length);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400395 if (!new_sfnt_data)
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400396 {
397 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200398 return nullptr;
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400399 }
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400400
401 memcpy(new_sfnt_data, orig_sfnt_data, length);
402
Behdad Esfahbod91126aa2018-08-02 02:03:13 -0700403 OT::name &name = StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700404 name.format = 0;
405 name.count = ARRAY_LENGTH (name_IDs);
406 name.stringOffset = name.get_size ();
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400407 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
408 {
Behdad Esfahbod96471fe2018-09-11 01:39:23 +0200409 OT::NameRecord &record = name.nameRecordZ[i];
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700410 record.platformID = 3;
411 record.encodingID = 1;
412 record.languageID = 0x0409u; /* English */
413 record.nameID = name_IDs[i];
414 record.length = name_str_len * 2;
415 record.offset = 0;
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400416 }
417
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400418 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
Behdad Esfahbod91126aa2018-08-02 02:03:13 -0700419 unsigned char *p = &StructAfter<unsigned char> (name);
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400420 for (unsigned int i = 0; i < name_str_len; i++)
421 {
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400422 *p++ = new_name[i] >> 8;
423 *p++ = new_name[i] & 0xff;
424 }
425
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400426 /* Adjust name table entry to point to new name table */
Behdad Esfahbod12ff69d2013-07-22 10:52:12 -0400427 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
428 unsigned int face_count = file.get_face_count ();
429 for (unsigned int face_index = 0; face_index < face_count; face_index++)
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400430 {
Behdad Esfahbod8b427c72013-07-22 10:57:05 -0400431 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
432 * toe-stepping. But we don't really care. */
Behdad Esfahbod12ff69d2013-07-22 10:52:12 -0400433 const OT::OpenTypeFontFace &face = file.get_face (face_index);
434 unsigned int index;
435 if (face.find_table_index (HB_OT_TAG_name, &index))
436 {
437 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
Behdad Esfahbod48baf7e2018-03-28 13:38:51 -0700438 record.checkSum.set_for_data (&name, padded_name_table_length);
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700439 record.offset = name_table_offset;
440 record.length = name_table_length;
Behdad Esfahbod12ff69d2013-07-22 10:52:12 -0400441 }
442 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
443 {
444 free (new_sfnt_data);
445 hb_blob_destroy (blob);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200446 return nullptr;
Behdad Esfahbod12ff69d2013-07-22 10:52:12 -0400447 }
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400448 }
449
450 /* The checkSumAdjustment field in the 'head' table is now wrong,
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400451 * but that doesn't actually seem to cause any problems so we don't
452 * bother. */
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400453
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400454 hb_blob_destroy (blob);
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400455 return hb_blob_create ((const char *) new_sfnt_data, new_length,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200456 HB_MEMORY_MODE_WRITABLE, nullptr, free);
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400457}
458
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700459hb_uniscribe_face_data_t *
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400460_hb_uniscribe_shaper_face_data_create (hb_face_t *face)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200461{
Behdad Esfahbodd4d1bf82018-08-02 02:04:02 -0700462 hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200463 if (unlikely (!data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200464 return nullptr;
Behdad Esfahboda3bd8a02011-08-24 03:22:49 +0200465
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400466 data->funcs = hb_uniscribe_shaper_get_funcs ();
467 if (unlikely (!data->funcs))
468 {
469 free (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200470 return nullptr;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400471 }
472
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200473 hb_blob_t *blob = hb_face_reference_blob (face);
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400474 if (unlikely (!hb_blob_get_length (blob)))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200475 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
476
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400477 blob = _hb_rename_font (blob, data->face_name);
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400478 if (unlikely (!blob))
479 {
480 free (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200481 return nullptr;
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400482 }
Behdad Esfahbod8ac2e882013-07-21 16:06:49 -0400483
484 DWORD num_fonts_installed;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200485 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr),
Behdad Esfahbod73f947e2013-07-21 16:16:35 -0400486 hb_blob_get_length (blob),
487 0, &num_fonts_installed);
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400488 if (unlikely (!data->fh))
489 {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200490 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400491 free (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200492 return nullptr;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200493 }
494
495 return data;
496}
497
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400498void
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700499_hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_face_data_t *data)
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400500{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400501 RemoveFontMemResourceEx (data->fh);
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400502 free (data);
503}
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200504
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400505
506/*
507 * shaper font data
508 */
509
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500510struct hb_uniscribe_font_data_t
511{
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200512 HDC hdc;
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500513 mutable LOGFONTW log_font;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200514 HFONT hfont;
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500515 mutable SCRIPT_CACHE script_cache;
Behdad Esfahbod7c5bee02015-08-19 13:20:31 +0100516 double x_mult, y_mult; /* From LOGFONT space to HB space. */
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400517};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200518
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400519static bool
520populate_log_font (LOGFONTW *lf,
Behdad Esfahbod7c5bee02015-08-19 13:20:31 +0100521 hb_font_t *font,
522 unsigned int font_size)
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400523{
524 memset (lf, 0, sizeof (*lf));
Behdad Esfahbod2c439212017-11-01 18:13:35 -0600525 lf->lfHeight = - (int) font_size;
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400526 lf->lfCharSet = DEFAULT_CHARSET;
527
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500528 memcpy (lf->lfFaceName, font->face->data.uniscribe->face_name, sizeof (lf->lfFaceName));
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400529
530 return true;
531}
532
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700533hb_uniscribe_font_data_t *
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400534_hb_uniscribe_shaper_font_data_create (hb_font_t *font)
535{
Behdad Esfahbodd4d1bf82018-08-02 02:04:02 -0700536 hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400537 if (unlikely (!data))
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200538 return nullptr;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400539
Behdad Esfahbod7c5bee02015-08-19 13:20:31 +0100540 int font_size = font->face->get_upem (); /* Default... */
541 /* No idea if the following is even a good idea. */
542 if (font->y_ppem)
543 font_size = font->y_ppem;
544
545 if (font_size < 0)
546 font_size = -font_size;
547 data->x_mult = (double) font->x_scale / font_size;
548 data->y_mult = (double) font->y_scale / font_size;
549
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200550 data->hdc = GetDC (nullptr);
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400551
Behdad Esfahbod7c5bee02015-08-19 13:20:31 +0100552 if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400553 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
554 _hb_uniscribe_shaper_font_data_destroy (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200555 return nullptr;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400556 }
557
558 data->hfont = CreateFontIndirectW (&data->log_font);
559 if (unlikely (!data->hfont)) {
560 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
561 _hb_uniscribe_shaper_font_data_destroy (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200562 return nullptr;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400563 }
564
565 if (!SelectObject (data->hdc, data->hfont)) {
566 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
567 _hb_uniscribe_shaper_font_data_destroy (data);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200568 return nullptr;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400569 }
570
571 return data;
572}
573
574void
Behdad Esfahbod3d22aef2018-08-01 18:03:32 -0700575_hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_font_data_t *data)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200576{
577 if (data->hdc)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200578 ReleaseDC (nullptr, data->hdc);
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200579 if (data->hfont)
580 DeleteObject (data->hfont);
581 if (data->script_cache)
582 ScriptFreeCache (&data->script_cache);
583 free (data);
584}
585
Behdad Esfahbod0ae6dbf2012-12-09 18:37:38 -0500586LOGFONTW *
587hb_uniscribe_font_get_logfontw (hb_font_t *font)
588{
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500589 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
590 return data ? &data->log_font : nullptr;
Behdad Esfahbod0ae6dbf2012-12-09 18:37:38 -0500591}
592
593HFONT
594hb_uniscribe_font_get_hfont (hb_font_t *font)
595{
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500596 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
597 return data ? data->hfont : nullptr;
Behdad Esfahbod0ae6dbf2012-12-09 18:37:38 -0500598}
599
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400600
601/*
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400602 * shaper
603 */
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200604
605
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400606hb_bool_t
Behdad Esfahbodbd26b4d2012-07-26 22:05:39 -0400607_hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
608 hb_font_t *font,
Behdad Esfahbod6bd9b472012-04-12 14:53:53 -0400609 hb_buffer_t *buffer,
610 const hb_feature_t *features,
611 unsigned int num_features)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400612{
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400613 hb_face_t *face = font->face;
Behdad Esfahbodce5da0f2018-11-16 02:29:13 -0500614 const hb_uniscribe_face_data_t *face_data = face->data.uniscribe;
615 const hb_uniscribe_font_data_t *font_data = font->data.uniscribe;
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400616 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400617
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400618 /*
619 * Set up features.
620 */
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700621 hb_vector_t<OPENTYPE_FEATURE_RECORD> feature_records;
622 hb_vector_t<range_record_t> range_records;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400623 if (num_features)
624 {
625 /* Sort features by start/end events. */
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700626 hb_vector_t<feature_event_t> feature_events;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400627 for (unsigned int i = 0; i < num_features; i++)
628 {
629 active_feature_t feature;
630 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
631 feature.rec.lParameter = features[i].value;
632 feature.order = i;
633
634 feature_event_t *event;
635
636 event = feature_events.push ();
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400637 event->index = features[i].start;
638 event->start = true;
639 event->feature = feature;
640
641 event = feature_events.push ();
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400642 event->index = features[i].end;
643 event->start = false;
644 event->feature = feature;
645 }
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400646 feature_events.qsort ();
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400647 /* Add a strategic final event. */
648 {
649 active_feature_t feature;
650 feature.rec.tagFeature = 0;
651 feature.rec.lParameter = 0;
652 feature.order = num_features + 1;
653
654 feature_event_t *event = feature_events.push ();
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400655 event->index = 0; /* This value does magic. */
656 event->start = false;
657 event->feature = feature;
658 }
659
660 /* Scan events and save features for each range. */
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700661 hb_vector_t<active_feature_t> active_features;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400662 unsigned int last_index = 0;
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500663 for (unsigned int i = 0; i < feature_events.length; i++)
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400664 {
665 feature_event_t *event = &feature_events[i];
666
667 if (event->index != last_index)
668 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430669 /* Save a snapshot of active features and the range. */
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400670 range_record_t *range = range_records.push ();
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400671
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500672 unsigned int offset = feature_records.length;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400673
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400674 active_features.qsort ();
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500675 for (unsigned int j = 0; j < active_features.length; j++)
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400676 {
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500677 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.length - 1].tagFeature)
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400678 {
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700679 feature_records.push (active_features[j].rec);
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400680 }
681 else
682 {
683 /* Overrides value for existing feature. */
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500684 feature_records[feature_records.length - 1].lParameter = active_features[j].rec.lParameter;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400685 }
686 }
687
688 /* Will convert to pointer after all is ready, since feature_records.array
689 * may move as we grow it. */
690 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500691 range->props.cotfRecords = feature_records.length - offset;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400692 range->index_first = last_index;
693 range->index_last = event->index - 1;
694
695 last_index = event->index;
696 }
697
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700698 if (event->start)
699 {
700 active_features.push (event->feature);
701 }
702 else
703 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430704 active_feature_t *feature = active_features.find (&event->feature);
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400705 if (feature)
Behdad Esfahbod2fb3a832019-05-10 18:40:29 -0700706 active_features.remove (feature - active_features.arrayZ);
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400707 }
708 }
709
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500710 if (!range_records.length) /* No active feature found. */
Behdad Esfahbodf7515762018-06-01 17:48:37 -0700711 num_features = 0;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400712
713 /* Fixup the pointers. */
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500714 for (unsigned int i = 0; i < range_records.length; i++)
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400715 {
716 range_record_t *range = &range_records[i];
Behdad Esfahbod69d232e2018-12-21 15:08:06 -0500717 range->props.potfRecords = (OPENTYPE_FEATURE_RECORD *) feature_records + reinterpret_cast<uintptr_t> (range->props.potfRecords);
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400718 }
719 }
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400720
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200721#define FAIL(...) \
722 HB_STMT_START { \
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200723 DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400724 return false; \
Behdad Esfahbod10bac212019-06-18 15:15:06 -0700725 } HB_STMT_END
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200726
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200727 HRESULT hr;
728
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400729retry:
730
731 unsigned int scratch_size;
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500732 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400733
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500734#define ALLOCATE_ARRAY(Type, name, len) \
735 Type *name = (Type *) scratch; \
Behdad Esfahbod68e12e62019-05-13 17:28:59 -0700736 do { \
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500737 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
738 assert (_consumed <= scratch_size); \
739 scratch += _consumed; \
740 scratch_size -= _consumed; \
Behdad Esfahbod68e12e62019-05-13 17:28:59 -0700741 } while (0)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400742
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400743#define utf16_index() var1.u32
744
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500745 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
746
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400747 unsigned int chars_len = 0;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400748 for (unsigned int i = 0; i < buffer->len; i++)
749 {
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400750 hb_codepoint_t c = buffer->info[i].codepoint;
751 buffer->info[i].utf16_index() = chars_len;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400752 if (likely (c <= 0xFFFFu))
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400753 pchars[chars_len++] = c;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400754 else if (unlikely (c > 0x10FFFFu))
755 pchars[chars_len++] = 0xFFFDu;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400756 else {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400757 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Behdad Esfahbod33317312016-08-08 17:24:04 -0700758 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400759 }
760 }
761
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400762 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
763 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
764
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400765 if (num_features)
766 {
767 /* Need log_clusters to assign features. */
768 chars_len = 0;
769 for (unsigned int i = 0; i < buffer->len; i++)
770 {
771 hb_codepoint_t c = buffer->info[i].codepoint;
772 unsigned int cluster = buffer->info[i].cluster;
773 log_clusters[chars_len++] = cluster;
Behdad Esfahbod76271002014-07-11 14:54:42 -0400774 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400775 log_clusters[chars_len++] = cluster; /* Surrogates. */
776 }
777 }
778
Behdad Esfahbod3216e442013-12-23 14:39:23 -0500779 /* The -2 in the following is to compensate for possible
780 * alignment needed after the WORD array. sizeof(WORD) == 2. */
781 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
782 / (sizeof (WORD) +
783 sizeof (SCRIPT_GLYPHPROP) +
784 sizeof (int) +
785 sizeof (GOFFSET) +
786 sizeof (uint32_t));
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400787
788 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
789 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
790 ALLOCATE_ARRAY (int, advances, glyphs_size);
791 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
792 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
793
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400794 /* Note:
795 * We can't touch the contents of glyph_props. Our fallback
796 * implementations of Shape and Place functions use that buffer
797 * by casting it to a different type. It works because they
798 * both agree about it, but if we want to access it here we
799 * need address that issue first.
800 */
801
Behdad Esfahbod91e721e2012-07-25 19:20:34 -0400802#undef ALLOCATE_ARRAY
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400803
Behdad Esfahbod87296912012-06-08 14:18:30 -0400804#define MAX_ITEMS 256
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400805
806 SCRIPT_ITEM items[MAX_ITEMS + 1];
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400807 SCRIPT_CONTROL bidi_control = {0};
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400808 SCRIPT_STATE bidi_state = {0};
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400809 ULONG script_tags[MAX_ITEMS];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400810 int item_count;
811
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400812 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400813 //bidi_control.fMergeNeutralItems = true;
Behdad Esfahbod33317312016-08-08 17:24:04 -0700814 *(uint32_t*)&bidi_control |= 1u<<24;
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400815
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400816 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
Behdad Esfahbod29eac8f2012-06-08 09:26:17 -0400817 bidi_state.fOverrideDirection = 1;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400818
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500819 hr = funcs->ScriptItemizeOpenType (pchars,
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400820 chars_len,
821 MAX_ITEMS,
822 &bidi_control,
823 &bidi_state,
824 items,
825 script_tags,
826 &item_count);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400827 if (unlikely (FAILED (hr)))
Behdad Esfahbod6a5d2b22019-01-17 19:57:17 -0500828 FAIL ("ScriptItemizeOpenType() failed: 0x%08lx", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400829
830#undef MAX_ITEMS
831
Behdad Esfahbod252632c2018-11-05 14:33:41 -0500832 hb_tag_t lang_tag;
833 unsigned int lang_count = 1;
834 hb_ot_tags_from_script_and_language (buffer->props.script,
835 buffer->props.language,
836 nullptr, nullptr,
837 &lang_count, &lang_tag);
838 OPENTYPE_TAG language_tag = hb_uint32_swap (lang_count ? lang_tag : HB_TAG_NONE);
Behdad Esfahbod3a4e5dd2018-10-29 18:05:25 -0700839 hb_vector_t<TEXTRANGE_PROPERTIES*> range_properties;
840 hb_vector_t<int> range_char_counts;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400841
842 unsigned int glyphs_offset = 0;
843 unsigned int glyphs_len;
Behdad Esfahbodb069c3c2012-06-08 10:10:29 -0400844 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
Behdad Esfahbod2c439212017-11-01 18:13:35 -0600845 for (int i = 0; i < item_count; i++)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400846 {
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400847 unsigned int chars_offset = items[i].iCharPos;
848 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
Behdad Esfahbod1c1233e2012-06-08 09:20:53 -0400849
Behdad Esfahbod38d5c582013-08-06 14:45:36 -0400850 if (num_features)
851 {
Behdad Esfahbod38d5c582013-08-06 14:45:36 -0400852 range_properties.shrink (0);
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400853 range_char_counts.shrink (0);
Behdad Esfahbod38d5c582013-08-06 14:45:36 -0400854
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400855 range_record_t *last_range = &range_records[0];
Behdad Esfahbod38d5c582013-08-06 14:45:36 -0400856
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400857 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
858 {
859 range_record_t *range = last_range;
860 while (log_clusters[k] < range->index_first)
861 range--;
862 while (log_clusters[k] > range->index_last)
863 range++;
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500864 if (!range_properties.length ||
865 &range->props != range_properties[range_properties.length - 1])
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400866 {
867 TEXTRANGE_PROPERTIES **props = range_properties.push ();
868 int *c = range_char_counts.push ();
869 if (unlikely (!props || !c))
870 {
871 range_properties.shrink (0);
872 range_char_counts.shrink (0);
873 break;
874 }
875 *props = &range->props;
876 *c = 1;
877 }
878 else
879 {
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500880 range_char_counts[range_char_counts.length - 1]++;
Behdad Esfahbod9a175912013-08-07 17:26:13 -0400881 }
882
883 last_range = range;
884 }
Behdad Esfahbod38d5c582013-08-06 14:45:36 -0400885 }
886
Behdad Esfahbod3409fb12013-08-21 17:22:21 -0400887 /* Asking for glyphs in logical order circumvents at least
888 * one bug in Uniscribe. */
889 items[i].a.fLogicalOrder = true;
890
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400891 retry_shape:
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400892 hr = funcs->ScriptShapeOpenType (font_data->hdc,
893 &font_data->script_cache,
894 &items[i].a,
895 script_tags[i],
896 language_tag,
Behdad Esfahbod2fb3a832019-05-10 18:40:29 -0700897 range_char_counts.arrayZ,
898 range_properties.arrayZ,
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500899 range_properties.length,
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500900 pchars + chars_offset,
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400901 item_chars_len,
902 glyphs_size - glyphs_offset,
903 /* out */
904 log_clusters + chars_offset,
905 char_props + chars_offset,
906 glyphs + glyphs_offset,
907 glyph_props + glyphs_offset,
908 (int *) &glyphs_len);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400909
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400910 if (unlikely (items[i].a.fNoGlyphIndex))
911 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
Behdad Esfahbodeaf1e932015-12-17 16:57:47 +0000912 if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400913 {
Behdad Esfahbod15c633d2014-08-11 13:42:42 -0400914 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400915 FAIL ("Buffer resize failed");
916 goto retry;
917 }
918 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400919 {
920 if (items[i].a.eScript == SCRIPT_UNDEFINED)
921 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
922 items[i].a.eScript = SCRIPT_UNDEFINED;
923 goto retry_shape;
924 }
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400925 if (unlikely (FAILED (hr)))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400926 {
Behdad Esfahbod6a5d2b22019-01-17 19:57:17 -0500927 FAIL ("ScriptShapeOpenType() failed: 0x%08lx", hr);
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400928 }
929
930 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
931 log_clusters[j] += glyphs_offset;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400932
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400933 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
934 &font_data->script_cache,
935 &items[i].a,
936 script_tags[i],
937 language_tag,
Behdad Esfahbod2fb3a832019-05-10 18:40:29 -0700938 range_char_counts.arrayZ,
939 range_properties.arrayZ,
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500940 range_properties.length,
Behdad Esfahbod68c372e2013-11-13 14:44:01 -0500941 pchars + chars_offset,
Behdad Esfahbod2a17f952013-05-28 13:10:51 -0400942 log_clusters + chars_offset,
943 char_props + chars_offset,
944 item_chars_len,
945 glyphs + glyphs_offset,
946 glyph_props + glyphs_offset,
947 glyphs_len,
948 /* out */
949 advances + glyphs_offset,
950 offsets + glyphs_offset,
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200951 nullptr);
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400952 if (unlikely (FAILED (hr)))
Behdad Esfahbod6a5d2b22019-01-17 19:57:17 -0500953 FAIL ("ScriptPlaceOpenType() failed: 0x%08lx", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400954
Behdad Esfahbod1d05fdd2013-08-20 13:22:21 -0400955 if (DEBUG_ENABLED (UNISCRIBE))
956 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
957 i,
958 items[i].a.fRTL,
959 items[i].a.fLayoutRTL,
960 items[i].a.fLogicalOrder,
961 HB_UNTAG (hb_uint32_swap (script_tags[i])));
962
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400963 glyphs_offset += glyphs_len;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400964 }
965 glyphs_len = glyphs_offset;
966
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400967 /* Ok, we've got everything we need, now compose output buffer,
968 * very, *very*, carefully! */
969
970 /* Calculate visual-clusters. That's what we ship. */
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400971 for (unsigned int i = 0; i < glyphs_len; i++)
Behdad Esfahbod4d3c8f92019-01-17 19:58:54 -0500972 vis_clusters[i] = (uint32_t) -1;
Behdad Esfahbod577326b2011-08-07 01:04:40 -0400973 for (unsigned int i = 0; i < buffer->len; i++) {
974 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700975 *p = hb_min (*p, buffer->info[i].cluster);
Behdad Esfahbod577326b2011-08-07 01:04:40 -0400976 }
Behdad Esfahbod3409fb12013-08-21 17:22:21 -0400977 for (unsigned int i = 1; i < glyphs_len; i++)
Behdad Esfahbod4d3c8f92019-01-17 19:58:54 -0500978 if (vis_clusters[i] == (uint32_t) -1)
Behdad Esfahbod3409fb12013-08-21 17:22:21 -0400979 vis_clusters[i] = vis_clusters[i - 1];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400980
981#undef utf16_index
982
Behdad Esfahbod15c633d2014-08-11 13:42:42 -0400983 if (unlikely (!buffer->ensure (glyphs_len)))
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400984 FAIL ("Buffer in error");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400985
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400986#undef FAIL
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400987
988 /* Set glyph infos */
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200989 buffer->len = 0;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400990 for (unsigned int i = 0; i < glyphs_len; i++)
991 {
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200992 hb_glyph_info_t *info = &buffer->info[buffer->len++];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400993
994 info->codepoint = glyphs[i];
995 info->cluster = vis_clusters[i];
996
997 /* The rest is crap. Let's store position info there for now. */
998 info->mask = advances[i];
Behdad Esfahbod163c4352015-08-20 15:39:06 +0100999 info->var1.i32 = offsets[i].du;
1000 info->var2.i32 = offsets[i].dv;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001001 }
1002
1003 /* Set glyph positions */
1004 buffer->clear_positions ();
Behdad Esfahbod7c5bee02015-08-19 13:20:31 +01001005 double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001006 for (unsigned int i = 0; i < glyphs_len; i++)
1007 {
1008 hb_glyph_info_t *info = &buffer->info[i];
1009 hb_glyph_position_t *pos = &buffer->pos[i];
1010
1011 /* TODO vertical */
Behdad Esfahbode6d5e4c2016-01-06 12:29:50 +00001012 pos->x_advance = x_mult * (int32_t) info->mask;
Behdad Esfahbod163c4352015-08-20 15:39:06 +01001013 pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1014 pos->y_offset = y_mult * info->var2.i32;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001015 }
1016
Behdad Esfahbod3409fb12013-08-21 17:22:21 -04001017 if (backward)
1018 hb_buffer_reverse (buffer);
1019
Behdad Esfahbode4da3802017-11-10 17:14:27 -08001020 buffer->unsafe_to_break_all ();
1021
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001022 /* Wow, done! */
Behdad Esfahbod0594a242012-06-05 20:35:40 -04001023 return true;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001024}
1025
1026
Behdad Esfahbodaa3450c2019-06-17 22:41:49 -07001027#endif