Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 3613696 | 2013-08-12 00:33:28 -0400 | [diff] [blame] | 2 | * Copyright © 2011,2012,2013 Google, Inc. |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 3 | * |
| 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 Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 27 | #include "hb.hh" |
Behdad Esfahbod | aa3450c | 2019-06-17 22:41:49 -0700 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_UNISCRIBE |
| 30 | |
Behdad Esfahbod | 8fe1548 | 2019-06-26 13:49:42 -0700 | [diff] [blame] | 31 | #ifdef HB_NO_OT_TAG |
| 32 | #error "Cannot compile 'uniscribe' shaper with HB_NO_OT_TAG." |
| 33 | #endif |
| 34 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 35 | #include "hb-shaper-impl.hh" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 36 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 37 | #include <windows.h> |
| 38 | #include <usp10.h> |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 39 | #include <rpc.h> |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 40 | |
Eli Zaretskii | d5e5f37 | 2019-06-05 22:20:03 +0300 | [diff] [blame] | 41 | #ifndef E_NOT_SUFFICIENT_BUFFER |
| 42 | #define E_NOT_SUFFICIENT_BUFFER HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER) |
| 43 | #endif |
| 44 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 45 | #include "hb-uniscribe.h" |
| 46 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 47 | #include "hb-open-file.hh" |
Behdad Esfahbod | 7a750ac | 2011-08-17 14:19:59 +0200 | [diff] [blame] | 48 | #include "hb-ot-name-table.hh" |
Behdad Esfahbod | 46072b7 | 2018-10-27 04:21:20 -0700 | [diff] [blame] | 49 | #include "hb-ot-layout.h" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 50 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 51 | |
Behdad Esfahbod | 04981ee | 2018-10-27 04:40:43 -0700 | [diff] [blame] | 52 | /** |
| 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 Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 61 | typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 62 | 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 Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 70 | ); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 71 | |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 72 | typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 73 | 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 Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 91 | typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 92 | 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 | |
| 115 | static HRESULT WINAPI |
| 116 | hb_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 | |
| 138 | static HRESULT WINAPI |
| 139 | hb_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 | |
| 171 | static HRESULT WINAPI |
| 172 | hb_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 Esfahbod | 747d256 | 2018-08-12 17:32:10 -0700 | [diff] [blame] | 206 | struct hb_uniscribe_shaper_funcs_t |
| 207 | { |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 208 | SIOT ScriptItemizeOpenType; |
| 209 | SSOT ScriptShapeOpenType; |
| 210 | SPOT ScriptPlaceOpenType; |
| 211 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 212 | void init () |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 213 | { |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 214 | HMODULE hinstLib; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 215 | this->ScriptItemizeOpenType = nullptr; |
| 216 | this->ScriptShapeOpenType = nullptr; |
| 217 | this->ScriptPlaceOpenType = nullptr; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 218 | |
Behdad Esfahbod | 757a7a9 | 2013-08-28 12:35:13 -0400 | [diff] [blame] | 219 | hinstLib = GetModuleHandle (TEXT ("usp10.dll")); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 220 | if (hinstLib) |
| 221 | { |
Behdad Esfahbod | 9a1df82 | 2019-01-28 14:15:18 -0500 | [diff] [blame] | 222 | #pragma GCC diagnostic push |
| 223 | #pragma GCC diagnostic ignored "-Wcast-function-type" |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 224 | this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType"); |
| 225 | this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType"); |
| 226 | this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType"); |
Behdad Esfahbod | 9a1df82 | 2019-01-28 14:15:18 -0500 | [diff] [blame] | 227 | #pragma GCC diagnostic pop |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 228 | } |
| 229 | if (!this->ScriptItemizeOpenType || |
| 230 | !this->ScriptShapeOpenType || |
| 231 | !this->ScriptPlaceOpenType) |
| 232 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 233 | DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back."); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 234 | this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType; |
| 235 | this->ScriptShapeOpenType = hb_ScriptShapeOpenType; |
| 236 | this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType; |
| 237 | } |
| 238 | } |
| 239 | }; |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 240 | |
Ebrahim Byagowi | 1f94388 | 2019-07-16 11:24:29 +0430 | [diff] [blame] | 241 | #if HB_USE_ATEXIT |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 242 | static void free_static_uniscribe_shaper_funcs (); |
Ebrahim Byagowi | 1f94388 | 2019-07-16 11:24:29 +0430 | [diff] [blame] | 243 | #endif |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 244 | |
| 245 | static 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 Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 248 | static hb_uniscribe_shaper_funcs_t *create () |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 249 | { |
| 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 Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 255 | |
Behdad Esfahbod | b89c7fd | 2018-11-21 12:32:48 -0500 | [diff] [blame] | 256 | #if HB_USE_ATEXIT |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 257 | atexit (free_static_uniscribe_shaper_funcs); |
| 258 | #endif |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 259 | |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 260 | return funcs; |
| 261 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 262 | static void destroy (hb_uniscribe_shaper_funcs_t *p) |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 263 | { |
| 264 | free ((void *) p); |
| 265 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 266 | static hb_uniscribe_shaper_funcs_t *get_null () |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 267 | { |
| 268 | return nullptr; |
| 269 | } |
| 270 | } static_uniscribe_shaper_funcs; |
| 271 | |
Behdad Esfahbod | b89c7fd | 2018-11-21 12:32:48 -0500 | [diff] [blame] | 272 | #if HB_USE_ATEXIT |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 273 | static |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 274 | void free_static_uniscribe_shaper_funcs () |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 275 | { |
| 276 | static_uniscribe_shaper_funcs.free_instance (); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 277 | } |
Ebrahim Byagowi | 70d3654 | 2018-03-30 05:00:28 +0430 | [diff] [blame] | 278 | #endif |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 279 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 280 | static hb_uniscribe_shaper_funcs_t * |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 281 | hb_uniscribe_shaper_get_funcs () |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 282 | { |
Behdad Esfahbod | 9bbd1cd | 2018-08-12 18:20:53 -0700 | [diff] [blame] | 283 | return static_uniscribe_shaper_funcs.get_unconst (); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 284 | } |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 285 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 286 | |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 287 | struct active_feature_t { |
| 288 | OPENTYPE_FEATURE_RECORD rec; |
| 289 | unsigned int order; |
| 290 | |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 291 | HB_INTERNAL static int cmp (const void *pa, const void *pb) { |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 292 | const active_feature_t *a = (const active_feature_t *) pa; |
| 293 | const active_feature_t *b = (const active_feature_t *) pb; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 294 | 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 Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 299 | bool operator== (const active_feature_t *f) |
| 300 | { return cmp (this, f) == 0; } |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | struct feature_event_t { |
| 304 | unsigned int index; |
| 305 | bool start; |
| 306 | active_feature_t feature; |
| 307 | |
Behdad Esfahbod | 95df00a | 2019-04-12 17:50:03 -0400 | [diff] [blame] | 308 | HB_INTERNAL static int cmp (const void *pa, const void *pb) |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 309 | { |
Behdad Esfahbod | 98acdde | 2017-10-31 11:17:43 -0600 | [diff] [blame] | 310 | const feature_event_t *a = (const feature_event_t *) pa; |
| 311 | const feature_event_t *b = (const feature_event_t *) pb; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 312 | 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 | |
| 318 | struct range_record_t { |
| 319 | TEXTRANGE_PROPERTIES props; |
| 320 | unsigned int index_first; /* == start */ |
| 321 | unsigned int index_last; /* == end - 1 */ |
| 322 | }; |
| 323 | |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 324 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 325 | /* |
| 326 | * shaper face data |
| 327 | */ |
Behdad Esfahbod | 71388b3 | 2011-08-24 02:09:04 +0200 | [diff] [blame] | 328 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 329 | struct hb_uniscribe_face_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 330 | HANDLE fh; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 331 | hb_uniscribe_shaper_funcs_t *funcs; |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 332 | wchar_t face_name[LF_FACESIZE]; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 333 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 334 | |
Behdad Esfahbod | bdeea60 | 2013-07-21 16:22:10 -0400 | [diff] [blame] | 335 | /* face_name should point to a wchar_t[LF_FACESIZE] object. */ |
| 336 | static void |
| 337 | _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen) |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 338 | { |
| 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 Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 344 | static_assert ((2 + 3 * (16/2) < LF_FACESIZE), ""); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 345 | unsigned int name_str_len = 0; |
Behdad Esfahbod | bdeea60 | 2013-07-21 16:22:10 -0400 | [diff] [blame] | 346 | face_name[name_str_len++] = 'F'; |
| 347 | face_name[name_str_len++] = '_'; |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 348 | 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 Esfahbod | bdeea60 | 2013-07-21 16:22:10 -0400 | [diff] [blame] | 356 | 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 Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 359 | } |
Behdad Esfahbod | bdeea60 | 2013-07-21 16:22:10 -0400 | [diff] [blame] | 360 | face_name[name_str_len] = 0; |
| 361 | if (plen) |
| 362 | *plen = name_str_len; |
| 363 | } |
| 364 | |
| 365 | /* Destroys blob. */ |
| 366 | static hb_blob_t * |
| 367 | _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) |
| 368 | { |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 369 | /* 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 Esfahbod | ed7b2e5 | 2018-08-01 23:59:09 -0700 | [diff] [blame] | 378 | blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (blob); |
Behdad Esfahbod | 8b427c7 | 2013-07-22 10:57:05 -0400 | [diff] [blame] | 379 | |
Behdad Esfahbod | bdeea60 | 2013-07-21 16:22:10 -0400 | [diff] [blame] | 380 | 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 Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 384 | |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 385 | static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 }; |
| 386 | |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 387 | unsigned int name_table_length = OT::name::min_size + |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 388 | ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size + |
| 389 | name_str_len * 2; /* for name data in UTF16BE form */ |
Behdad Esfahbod | 48baf7e | 2018-03-28 13:38:51 -0700 | [diff] [blame] | 390 | unsigned int padded_name_table_length = ((name_table_length + 3) & ~3); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 391 | unsigned int name_table_offset = (length + 3) & ~3; |
| 392 | |
Behdad Esfahbod | 48baf7e | 2018-03-28 13:38:51 -0700 | [diff] [blame] | 393 | new_length = name_table_offset + padded_name_table_length; |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 394 | void *new_sfnt_data = calloc (1, new_length); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 395 | if (!new_sfnt_data) |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 396 | { |
| 397 | hb_blob_destroy (blob); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 398 | return nullptr; |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 399 | } |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 400 | |
| 401 | memcpy(new_sfnt_data, orig_sfnt_data, length); |
| 402 | |
Behdad Esfahbod | 91126aa | 2018-08-02 02:03:13 -0700 | [diff] [blame] | 403 | OT::name &name = StructAtOffset<OT::name> (new_sfnt_data, name_table_offset); |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 404 | name.format = 0; |
| 405 | name.count = ARRAY_LENGTH (name_IDs); |
| 406 | name.stringOffset = name.get_size (); |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 407 | for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++) |
| 408 | { |
Behdad Esfahbod | 96471fe | 2018-09-11 01:39:23 +0200 | [diff] [blame] | 409 | OT::NameRecord &record = name.nameRecordZ[i]; |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 410 | 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 Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 418 | /* Copy string data from new_name, converting wchar_t to UTF16BE. */ |
Behdad Esfahbod | 91126aa | 2018-08-02 02:03:13 -0700 | [diff] [blame] | 419 | unsigned char *p = &StructAfter<unsigned char> (name); |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 420 | for (unsigned int i = 0; i < name_str_len; i++) |
| 421 | { |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 422 | *p++ = new_name[i] >> 8; |
| 423 | *p++ = new_name[i] & 0xff; |
| 424 | } |
| 425 | |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 426 | /* Adjust name table entry to point to new name table */ |
Behdad Esfahbod | 12ff69d | 2013-07-22 10:52:12 -0400 | [diff] [blame] | 427 | 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 Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 430 | { |
Behdad Esfahbod | 8b427c7 | 2013-07-22 10:57:05 -0400 | [diff] [blame] | 431 | /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be |
| 432 | * toe-stepping. But we don't really care. */ |
Behdad Esfahbod | 12ff69d | 2013-07-22 10:52:12 -0400 | [diff] [blame] | 433 | 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 Esfahbod | 48baf7e | 2018-03-28 13:38:51 -0700 | [diff] [blame] | 438 | record.checkSum.set_for_data (&name, padded_name_table_length); |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 439 | record.offset = name_table_offset; |
| 440 | record.length = name_table_length; |
Behdad Esfahbod | 12ff69d | 2013-07-22 10:52:12 -0400 | [diff] [blame] | 441 | } |
| 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 Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 446 | return nullptr; |
Behdad Esfahbod | 12ff69d | 2013-07-22 10:52:12 -0400 | [diff] [blame] | 447 | } |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* The checkSumAdjustment field in the 'head' table is now wrong, |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 451 | * but that doesn't actually seem to cause any problems so we don't |
| 452 | * bother. */ |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 453 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame] | 454 | hb_blob_destroy (blob); |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 455 | return hb_blob_create ((const char *) new_sfnt_data, new_length, |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 456 | HB_MEMORY_MODE_WRITABLE, nullptr, free); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 459 | hb_uniscribe_face_data_t * |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 460 | _hb_uniscribe_shaper_face_data_create (hb_face_t *face) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 461 | { |
Behdad Esfahbod | d4d1bf8 | 2018-08-02 02:04:02 -0700 | [diff] [blame] | 462 | hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t)); |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 463 | if (unlikely (!data)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 464 | return nullptr; |
Behdad Esfahbod | a3bd8a0 | 2011-08-24 03:22:49 +0200 | [diff] [blame] | 465 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 466 | data->funcs = hb_uniscribe_shaper_get_funcs (); |
| 467 | if (unlikely (!data->funcs)) |
| 468 | { |
| 469 | free (data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 470 | return nullptr; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 473 | hb_blob_t *blob = hb_face_reference_blob (face); |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame] | 474 | if (unlikely (!hb_blob_get_length (blob))) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 475 | DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
| 476 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame] | 477 | blob = _hb_rename_font (blob, data->face_name); |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 478 | if (unlikely (!blob)) |
| 479 | { |
| 480 | free (data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 481 | return nullptr; |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 482 | } |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 483 | |
| 484 | DWORD num_fonts_installed; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 485 | data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr), |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame] | 486 | hb_blob_get_length (blob), |
| 487 | 0, &num_fonts_installed); |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 488 | if (unlikely (!data->fh)) |
| 489 | { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 490 | DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 491 | free (data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 492 | return nullptr; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return data; |
| 496 | } |
| 497 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 498 | void |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 499 | _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_face_data_t *data) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 500 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 501 | RemoveFontMemResourceEx (data->fh); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 502 | free (data); |
| 503 | } |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 504 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * shaper font data |
| 508 | */ |
| 509 | |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 510 | struct hb_uniscribe_font_data_t |
| 511 | { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 512 | HDC hdc; |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 513 | mutable LOGFONTW log_font; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 514 | HFONT hfont; |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 515 | mutable SCRIPT_CACHE script_cache; |
Behdad Esfahbod | 7c5bee0 | 2015-08-19 13:20:31 +0100 | [diff] [blame] | 516 | double x_mult, y_mult; /* From LOGFONT space to HB space. */ |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 517 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 518 | |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 519 | static bool |
| 520 | populate_log_font (LOGFONTW *lf, |
Behdad Esfahbod | 7c5bee0 | 2015-08-19 13:20:31 +0100 | [diff] [blame] | 521 | hb_font_t *font, |
| 522 | unsigned int font_size) |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 523 | { |
| 524 | memset (lf, 0, sizeof (*lf)); |
Behdad Esfahbod | 2c43921 | 2017-11-01 18:13:35 -0600 | [diff] [blame] | 525 | lf->lfHeight = - (int) font_size; |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 526 | lf->lfCharSet = DEFAULT_CHARSET; |
| 527 | |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 528 | memcpy (lf->lfFaceName, font->face->data.uniscribe->face_name, sizeof (lf->lfFaceName)); |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 529 | |
| 530 | return true; |
| 531 | } |
| 532 | |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 533 | hb_uniscribe_font_data_t * |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 534 | _hb_uniscribe_shaper_font_data_create (hb_font_t *font) |
| 535 | { |
Behdad Esfahbod | d4d1bf8 | 2018-08-02 02:04:02 -0700 | [diff] [blame] | 536 | hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t)); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 537 | if (unlikely (!data)) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 538 | return nullptr; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 539 | |
Behdad Esfahbod | 7c5bee0 | 2015-08-19 13:20:31 +0100 | [diff] [blame] | 540 | 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 Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 550 | data->hdc = GetDC (nullptr); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 551 | |
Behdad Esfahbod | 7c5bee0 | 2015-08-19 13:20:31 +0100 | [diff] [blame] | 552 | if (unlikely (!populate_log_font (&data->log_font, font, font_size))) { |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 553 | DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
| 554 | _hb_uniscribe_shaper_font_data_destroy (data); |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 555 | return nullptr; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 556 | } |
| 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 Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 562 | return nullptr; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 563 | } |
| 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 Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 568 | return nullptr; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | return data; |
| 572 | } |
| 573 | |
| 574 | void |
Behdad Esfahbod | 3d22aef | 2018-08-01 18:03:32 -0700 | [diff] [blame] | 575 | _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_font_data_t *data) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 576 | { |
| 577 | if (data->hdc) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 578 | ReleaseDC (nullptr, data->hdc); |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 579 | if (data->hfont) |
| 580 | DeleteObject (data->hfont); |
| 581 | if (data->script_cache) |
| 582 | ScriptFreeCache (&data->script_cache); |
| 583 | free (data); |
| 584 | } |
| 585 | |
Behdad Esfahbod | 0ae6dbf | 2012-12-09 18:37:38 -0500 | [diff] [blame] | 586 | LOGFONTW * |
| 587 | hb_uniscribe_font_get_logfontw (hb_font_t *font) |
| 588 | { |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 589 | const hb_uniscribe_font_data_t *data = font->data.uniscribe; |
| 590 | return data ? &data->log_font : nullptr; |
Behdad Esfahbod | 0ae6dbf | 2012-12-09 18:37:38 -0500 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | HFONT |
| 594 | hb_uniscribe_font_get_hfont (hb_font_t *font) |
| 595 | { |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 596 | const hb_uniscribe_font_data_t *data = font->data.uniscribe; |
| 597 | return data ? data->hfont : nullptr; |
Behdad Esfahbod | 0ae6dbf | 2012-12-09 18:37:38 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 600 | |
| 601 | /* |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 602 | * shaper |
| 603 | */ |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 604 | |
| 605 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 606 | hb_bool_t |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 607 | _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, |
| 608 | hb_font_t *font, |
Behdad Esfahbod | 6bd9b47 | 2012-04-12 14:53:53 -0400 | [diff] [blame] | 609 | hb_buffer_t *buffer, |
| 610 | const hb_feature_t *features, |
| 611 | unsigned int num_features) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 612 | { |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 613 | hb_face_t *face = font->face; |
Behdad Esfahbod | ce5da0f | 2018-11-16 02:29:13 -0500 | [diff] [blame] | 614 | const hb_uniscribe_face_data_t *face_data = face->data.uniscribe; |
| 615 | const hb_uniscribe_font_data_t *font_data = font->data.uniscribe; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 616 | hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs; |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 617 | |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 618 | /* |
| 619 | * Set up features. |
| 620 | */ |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 621 | hb_vector_t<OPENTYPE_FEATURE_RECORD> feature_records; |
| 622 | hb_vector_t<range_record_t> range_records; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 623 | if (num_features) |
| 624 | { |
| 625 | /* Sort features by start/end events. */ |
Behdad Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 626 | hb_vector_t<feature_event_t> feature_events; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 627 | 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 Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 637 | event->index = features[i].start; |
| 638 | event->start = true; |
| 639 | event->feature = feature; |
| 640 | |
| 641 | event = feature_events.push (); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 642 | event->index = features[i].end; |
| 643 | event->start = false; |
| 644 | event->feature = feature; |
| 645 | } |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 646 | feature_events.qsort (); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 647 | /* 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 Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 655 | 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 Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 661 | hb_vector_t<active_feature_t> active_features; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 662 | unsigned int last_index = 0; |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 663 | for (unsigned int i = 0; i < feature_events.length; i++) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 664 | { |
| 665 | feature_event_t *event = &feature_events[i]; |
| 666 | |
| 667 | if (event->index != last_index) |
| 668 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 669 | /* Save a snapshot of active features and the range. */ |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 670 | range_record_t *range = range_records.push (); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 671 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 672 | unsigned int offset = feature_records.length; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 673 | |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 674 | active_features.qsort (); |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 675 | for (unsigned int j = 0; j < active_features.length; j++) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 676 | { |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 677 | if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.length - 1].tagFeature) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 678 | { |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 679 | feature_records.push (active_features[j].rec); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 680 | } |
| 681 | else |
| 682 | { |
| 683 | /* Overrides value for existing feature. */ |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 684 | feature_records[feature_records.length - 1].lParameter = active_features[j].rec.lParameter; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 685 | } |
| 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 Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 691 | range->props.cotfRecords = feature_records.length - offset; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 692 | range->index_first = last_index; |
| 693 | range->index_last = event->index - 1; |
| 694 | |
| 695 | last_index = event->index; |
| 696 | } |
| 697 | |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 698 | if (event->start) |
| 699 | { |
| 700 | active_features.push (event->feature); |
| 701 | } |
| 702 | else |
| 703 | { |
Ebrahim Byagowi | a0b4ac4 | 2019-08-24 17:57:14 +0430 | [diff] [blame] | 704 | active_feature_t *feature = active_features.find (&event->feature); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 705 | if (feature) |
Behdad Esfahbod | 2fb3a83 | 2019-05-10 18:40:29 -0700 | [diff] [blame] | 706 | active_features.remove (feature - active_features.arrayZ); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 710 | if (!range_records.length) /* No active feature found. */ |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 711 | num_features = 0; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 712 | |
| 713 | /* Fixup the pointers. */ |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 714 | for (unsigned int i = 0; i < range_records.length; i++) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 715 | { |
| 716 | range_record_t *range = &range_records[i]; |
Behdad Esfahbod | 69d232e | 2018-12-21 15:08:06 -0500 | [diff] [blame] | 717 | range->props.potfRecords = (OPENTYPE_FEATURE_RECORD *) feature_records + reinterpret_cast<uintptr_t> (range->props.potfRecords); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 718 | } |
| 719 | } |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 720 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 721 | #define FAIL(...) \ |
| 722 | HB_STMT_START { \ |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 723 | DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 724 | return false; \ |
Behdad Esfahbod | 10bac21 | 2019-06-18 15:15:06 -0700 | [diff] [blame] | 725 | } HB_STMT_END |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 726 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 727 | HRESULT hr; |
| 728 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 729 | retry: |
| 730 | |
| 731 | unsigned int scratch_size; |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 732 | hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 733 | |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 734 | #define ALLOCATE_ARRAY(Type, name, len) \ |
| 735 | Type *name = (Type *) scratch; \ |
Behdad Esfahbod | 68e12e6 | 2019-05-13 17:28:59 -0700 | [diff] [blame] | 736 | do { \ |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 737 | unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ |
| 738 | assert (_consumed <= scratch_size); \ |
| 739 | scratch += _consumed; \ |
| 740 | scratch_size -= _consumed; \ |
Behdad Esfahbod | 68e12e6 | 2019-05-13 17:28:59 -0700 | [diff] [blame] | 741 | } while (0) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 742 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 743 | #define utf16_index() var1.u32 |
| 744 | |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 745 | ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2); |
| 746 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 747 | unsigned int chars_len = 0; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 748 | for (unsigned int i = 0; i < buffer->len; i++) |
| 749 | { |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 750 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 751 | buffer->info[i].utf16_index() = chars_len; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 752 | if (likely (c <= 0xFFFFu)) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 753 | pchars[chars_len++] = c; |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 754 | else if (unlikely (c > 0x10FFFFu)) |
| 755 | pchars[chars_len++] = 0xFFFDu; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 756 | else { |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 757 | pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10); |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 758 | pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1)); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 762 | ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
| 763 | ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
| 764 | |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 765 | 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 Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 774 | if (hb_in_range (c, 0x10000u, 0x10FFFFu)) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 775 | log_clusters[chars_len++] = cluster; /* Surrogates. */ |
| 776 | } |
| 777 | } |
| 778 | |
Behdad Esfahbod | 3216e44 | 2013-12-23 14:39:23 -0500 | [diff] [blame] | 779 | /* 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 Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 787 | |
| 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 Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 794 | /* 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 Esfahbod | 91e721e | 2012-07-25 19:20:34 -0400 | [diff] [blame] | 802 | #undef ALLOCATE_ARRAY |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 803 | |
Behdad Esfahbod | 8729691 | 2012-06-08 14:18:30 -0400 | [diff] [blame] | 804 | #define MAX_ITEMS 256 |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 805 | |
| 806 | SCRIPT_ITEM items[MAX_ITEMS + 1]; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 807 | SCRIPT_CONTROL bidi_control = {0}; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 808 | SCRIPT_STATE bidi_state = {0}; |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 809 | ULONG script_tags[MAX_ITEMS]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 810 | int item_count; |
| 811 | |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 812 | /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 813 | //bidi_control.fMergeNeutralItems = true; |
Behdad Esfahbod | 3331731 | 2016-08-08 17:24:04 -0700 | [diff] [blame] | 814 | *(uint32_t*)&bidi_control |= 1u<<24; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 815 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 816 | bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
Behdad Esfahbod | 29eac8f | 2012-06-08 09:26:17 -0400 | [diff] [blame] | 817 | bidi_state.fOverrideDirection = 1; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 818 | |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 819 | hr = funcs->ScriptItemizeOpenType (pchars, |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 820 | chars_len, |
| 821 | MAX_ITEMS, |
| 822 | &bidi_control, |
| 823 | &bidi_state, |
| 824 | items, |
| 825 | script_tags, |
| 826 | &item_count); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 827 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 6a5d2b2 | 2019-01-17 19:57:17 -0500 | [diff] [blame] | 828 | FAIL ("ScriptItemizeOpenType() failed: 0x%08lx", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 829 | |
| 830 | #undef MAX_ITEMS |
| 831 | |
Behdad Esfahbod | 252632c | 2018-11-05 14:33:41 -0500 | [diff] [blame] | 832 | 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 Esfahbod | 3a4e5dd | 2018-10-29 18:05:25 -0700 | [diff] [blame] | 839 | hb_vector_t<TEXTRANGE_PROPERTIES*> range_properties; |
| 840 | hb_vector_t<int> range_char_counts; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 841 | |
| 842 | unsigned int glyphs_offset = 0; |
| 843 | unsigned int glyphs_len; |
Behdad Esfahbod | b069c3c | 2012-06-08 10:10:29 -0400 | [diff] [blame] | 844 | bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
Behdad Esfahbod | 2c43921 | 2017-11-01 18:13:35 -0600 | [diff] [blame] | 845 | for (int i = 0; i < item_count; i++) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 846 | { |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 847 | unsigned int chars_offset = items[i].iCharPos; |
| 848 | unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
Behdad Esfahbod | 1c1233e | 2012-06-08 09:20:53 -0400 | [diff] [blame] | 849 | |
Behdad Esfahbod | 38d5c58 | 2013-08-06 14:45:36 -0400 | [diff] [blame] | 850 | if (num_features) |
| 851 | { |
Behdad Esfahbod | 38d5c58 | 2013-08-06 14:45:36 -0400 | [diff] [blame] | 852 | range_properties.shrink (0); |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 853 | range_char_counts.shrink (0); |
Behdad Esfahbod | 38d5c58 | 2013-08-06 14:45:36 -0400 | [diff] [blame] | 854 | |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 855 | range_record_t *last_range = &range_records[0]; |
Behdad Esfahbod | 38d5c58 | 2013-08-06 14:45:36 -0400 | [diff] [blame] | 856 | |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 857 | 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 Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 864 | if (!range_properties.length || |
| 865 | &range->props != range_properties[range_properties.length - 1]) |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 866 | { |
| 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 Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 880 | range_char_counts[range_char_counts.length - 1]++; |
Behdad Esfahbod | 9a17591 | 2013-08-07 17:26:13 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | last_range = range; |
| 884 | } |
Behdad Esfahbod | 38d5c58 | 2013-08-06 14:45:36 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Behdad Esfahbod | 3409fb1 | 2013-08-21 17:22:21 -0400 | [diff] [blame] | 887 | /* Asking for glyphs in logical order circumvents at least |
| 888 | * one bug in Uniscribe. */ |
| 889 | items[i].a.fLogicalOrder = true; |
| 890 | |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 891 | retry_shape: |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 892 | hr = funcs->ScriptShapeOpenType (font_data->hdc, |
| 893 | &font_data->script_cache, |
| 894 | &items[i].a, |
| 895 | script_tags[i], |
| 896 | language_tag, |
Behdad Esfahbod | 2fb3a83 | 2019-05-10 18:40:29 -0700 | [diff] [blame] | 897 | range_char_counts.arrayZ, |
| 898 | range_properties.arrayZ, |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 899 | range_properties.length, |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 900 | pchars + chars_offset, |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 901 | 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 Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 909 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 910 | if (unlikely (items[i].a.fNoGlyphIndex)) |
| 911 | FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
Behdad Esfahbod | eaf1e93 | 2015-12-17 16:57:47 +0000 | [diff] [blame] | 912 | if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER)) |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 913 | { |
Behdad Esfahbod | 15c633d | 2014-08-11 13:42:42 -0400 | [diff] [blame] | 914 | if (unlikely (!buffer->ensure (buffer->allocated * 2))) |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 915 | FAIL ("Buffer resize failed"); |
| 916 | goto retry; |
| 917 | } |
| 918 | if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 919 | { |
| 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 Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 925 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 926 | { |
Behdad Esfahbod | 6a5d2b2 | 2019-01-17 19:57:17 -0500 | [diff] [blame] | 927 | FAIL ("ScriptShapeOpenType() failed: 0x%08lx", hr); |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++) |
| 931 | log_clusters[j] += glyphs_offset; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 932 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 933 | hr = funcs->ScriptPlaceOpenType (font_data->hdc, |
| 934 | &font_data->script_cache, |
| 935 | &items[i].a, |
| 936 | script_tags[i], |
| 937 | language_tag, |
Behdad Esfahbod | 2fb3a83 | 2019-05-10 18:40:29 -0700 | [diff] [blame] | 938 | range_char_counts.arrayZ, |
| 939 | range_properties.arrayZ, |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 940 | range_properties.length, |
Behdad Esfahbod | 68c372e | 2013-11-13 14:44:01 -0500 | [diff] [blame] | 941 | pchars + chars_offset, |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 942 | 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 Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 951 | nullptr); |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 952 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 6a5d2b2 | 2019-01-17 19:57:17 -0500 | [diff] [blame] | 953 | FAIL ("ScriptPlaceOpenType() failed: 0x%08lx", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 954 | |
Behdad Esfahbod | 1d05fdd | 2013-08-20 13:22:21 -0400 | [diff] [blame] | 955 | 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 Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 963 | glyphs_offset += glyphs_len; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 964 | } |
| 965 | glyphs_len = glyphs_offset; |
| 966 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 967 | /* 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 Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 971 | for (unsigned int i = 0; i < glyphs_len; i++) |
Behdad Esfahbod | 4d3c8f9 | 2019-01-17 19:58:54 -0500 | [diff] [blame] | 972 | vis_clusters[i] = (uint32_t) -1; |
Behdad Esfahbod | 577326b | 2011-08-07 01:04:40 -0400 | [diff] [blame] | 973 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 974 | uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
Behdad Esfahbod | 41248cc | 2019-05-07 20:54:31 -0700 | [diff] [blame] | 975 | *p = hb_min (*p, buffer->info[i].cluster); |
Behdad Esfahbod | 577326b | 2011-08-07 01:04:40 -0400 | [diff] [blame] | 976 | } |
Behdad Esfahbod | 3409fb1 | 2013-08-21 17:22:21 -0400 | [diff] [blame] | 977 | for (unsigned int i = 1; i < glyphs_len; i++) |
Behdad Esfahbod | 4d3c8f9 | 2019-01-17 19:58:54 -0500 | [diff] [blame] | 978 | if (vis_clusters[i] == (uint32_t) -1) |
Behdad Esfahbod | 3409fb1 | 2013-08-21 17:22:21 -0400 | [diff] [blame] | 979 | vis_clusters[i] = vis_clusters[i - 1]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 980 | |
| 981 | #undef utf16_index |
| 982 | |
Behdad Esfahbod | 15c633d | 2014-08-11 13:42:42 -0400 | [diff] [blame] | 983 | if (unlikely (!buffer->ensure (glyphs_len))) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 984 | FAIL ("Buffer in error"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 985 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 986 | #undef FAIL |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 987 | |
| 988 | /* Set glyph infos */ |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 989 | buffer->len = 0; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 990 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 991 | { |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 992 | hb_glyph_info_t *info = &buffer->info[buffer->len++]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 993 | |
| 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 Esfahbod | 163c435 | 2015-08-20 15:39:06 +0100 | [diff] [blame] | 999 | info->var1.i32 = offsets[i].du; |
| 1000 | info->var2.i32 = offsets[i].dv; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /* Set glyph positions */ |
| 1004 | buffer->clear_positions (); |
Behdad Esfahbod | 7c5bee0 | 2015-08-19 13:20:31 +0100 | [diff] [blame] | 1005 | double x_mult = font_data->x_mult, y_mult = font_data->y_mult; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1006 | 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 Esfahbod | e6d5e4c | 2016-01-06 12:29:50 +0000 | [diff] [blame] | 1012 | pos->x_advance = x_mult * (int32_t) info->mask; |
Behdad Esfahbod | 163c435 | 2015-08-20 15:39:06 +0100 | [diff] [blame] | 1013 | pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32); |
| 1014 | pos->y_offset = y_mult * info->var2.i32; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Behdad Esfahbod | 3409fb1 | 2013-08-21 17:22:21 -0400 | [diff] [blame] | 1017 | if (backward) |
| 1018 | hb_buffer_reverse (buffer); |
| 1019 | |
Behdad Esfahbod | e4da380 | 2017-11-10 17:14:27 -0800 | [diff] [blame] | 1020 | buffer->unsafe_to_break_all (); |
| 1021 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1022 | /* Wow, done! */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 1023 | return true; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | |
Behdad Esfahbod | aa3450c | 2019-06-17 22:41:49 -0700 | [diff] [blame] | 1027 | #endif |