Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2011,2012,2013 Google, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 5 | * |
michael@0 | 6 | * Permission is hereby granted, without written agreement and without |
michael@0 | 7 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 8 | * software and its documentation for any purpose, provided that the |
michael@0 | 9 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 10 | * all copies of this software. |
michael@0 | 11 | * |
michael@0 | 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 16 | * DAMAGE. |
michael@0 | 17 | * |
michael@0 | 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 23 | * |
michael@0 | 24 | * Google Author(s): Behdad Esfahbod |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | #define _WIN32_WINNT 0x0600 |
michael@0 | 28 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 29 | |
michael@0 | 30 | #define HB_SHAPER uniscribe |
michael@0 | 31 | #include "hb-shaper-impl-private.hh" |
michael@0 | 32 | |
michael@0 | 33 | #include <windows.h> |
michael@0 | 34 | #include <usp10.h> |
michael@0 | 35 | #include <rpc.h> |
michael@0 | 36 | |
michael@0 | 37 | #include "hb-uniscribe.h" |
michael@0 | 38 | |
michael@0 | 39 | #include "hb-open-file-private.hh" |
michael@0 | 40 | #include "hb-ot-name-table.hh" |
michael@0 | 41 | #include "hb-ot-tag.h" |
michael@0 | 42 | |
michael@0 | 43 | |
michael@0 | 44 | #ifndef HB_DEBUG_UNISCRIBE |
michael@0 | 45 | #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0) |
michael@0 | 46 | #endif |
michael@0 | 47 | |
michael@0 | 48 | |
michael@0 | 49 | typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/( |
michael@0 | 50 | const WCHAR *pwcInChars, |
michael@0 | 51 | int cInChars, |
michael@0 | 52 | int cMaxItems, |
michael@0 | 53 | const SCRIPT_CONTROL *psControl, |
michael@0 | 54 | const SCRIPT_STATE *psState, |
michael@0 | 55 | SCRIPT_ITEM *pItems, |
michael@0 | 56 | OPENTYPE_TAG *pScriptTags, |
michael@0 | 57 | int *pcItems |
michael@0 | 58 | ); |
michael@0 | 59 | |
michael@0 | 60 | typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/( |
michael@0 | 61 | HDC hdc, |
michael@0 | 62 | SCRIPT_CACHE *psc, |
michael@0 | 63 | SCRIPT_ANALYSIS *psa, |
michael@0 | 64 | OPENTYPE_TAG tagScript, |
michael@0 | 65 | OPENTYPE_TAG tagLangSys, |
michael@0 | 66 | int *rcRangeChars, |
michael@0 | 67 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
michael@0 | 68 | int cRanges, |
michael@0 | 69 | const WCHAR *pwcChars, |
michael@0 | 70 | int cChars, |
michael@0 | 71 | int cMaxGlyphs, |
michael@0 | 72 | WORD *pwLogClust, |
michael@0 | 73 | SCRIPT_CHARPROP *pCharProps, |
michael@0 | 74 | WORD *pwOutGlyphs, |
michael@0 | 75 | SCRIPT_GLYPHPROP *pOutGlyphProps, |
michael@0 | 76 | int *pcGlyphs |
michael@0 | 77 | ); |
michael@0 | 78 | |
michael@0 | 79 | typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/( |
michael@0 | 80 | HDC hdc, |
michael@0 | 81 | SCRIPT_CACHE *psc, |
michael@0 | 82 | SCRIPT_ANALYSIS *psa, |
michael@0 | 83 | OPENTYPE_TAG tagScript, |
michael@0 | 84 | OPENTYPE_TAG tagLangSys, |
michael@0 | 85 | int *rcRangeChars, |
michael@0 | 86 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
michael@0 | 87 | int cRanges, |
michael@0 | 88 | const WCHAR *pwcChars, |
michael@0 | 89 | WORD *pwLogClust, |
michael@0 | 90 | SCRIPT_CHARPROP *pCharProps, |
michael@0 | 91 | int cChars, |
michael@0 | 92 | const WORD *pwGlyphs, |
michael@0 | 93 | const SCRIPT_GLYPHPROP *pGlyphProps, |
michael@0 | 94 | int cGlyphs, |
michael@0 | 95 | int *piAdvance, |
michael@0 | 96 | GOFFSET *pGoffset, |
michael@0 | 97 | ABC *pABC |
michael@0 | 98 | ); |
michael@0 | 99 | |
michael@0 | 100 | |
michael@0 | 101 | /* Fallback implementations. */ |
michael@0 | 102 | |
michael@0 | 103 | static HRESULT WINAPI |
michael@0 | 104 | hb_ScriptItemizeOpenType( |
michael@0 | 105 | const WCHAR *pwcInChars, |
michael@0 | 106 | int cInChars, |
michael@0 | 107 | int cMaxItems, |
michael@0 | 108 | const SCRIPT_CONTROL *psControl, |
michael@0 | 109 | const SCRIPT_STATE *psState, |
michael@0 | 110 | SCRIPT_ITEM *pItems, |
michael@0 | 111 | OPENTYPE_TAG *pScriptTags, |
michael@0 | 112 | int *pcItems |
michael@0 | 113 | ) |
michael@0 | 114 | { |
michael@0 | 115 | { |
michael@0 | 116 | return ScriptItemize (pwcInChars, |
michael@0 | 117 | cInChars, |
michael@0 | 118 | cMaxItems, |
michael@0 | 119 | psControl, |
michael@0 | 120 | psState, |
michael@0 | 121 | pItems, |
michael@0 | 122 | pcItems); |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | static HRESULT WINAPI |
michael@0 | 127 | hb_ScriptShapeOpenType( |
michael@0 | 128 | HDC hdc, |
michael@0 | 129 | SCRIPT_CACHE *psc, |
michael@0 | 130 | SCRIPT_ANALYSIS *psa, |
michael@0 | 131 | OPENTYPE_TAG tagScript, |
michael@0 | 132 | OPENTYPE_TAG tagLangSys, |
michael@0 | 133 | int *rcRangeChars, |
michael@0 | 134 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
michael@0 | 135 | int cRanges, |
michael@0 | 136 | const WCHAR *pwcChars, |
michael@0 | 137 | int cChars, |
michael@0 | 138 | int cMaxGlyphs, |
michael@0 | 139 | WORD *pwLogClust, |
michael@0 | 140 | SCRIPT_CHARPROP *pCharProps, |
michael@0 | 141 | WORD *pwOutGlyphs, |
michael@0 | 142 | SCRIPT_GLYPHPROP *pOutGlyphProps, |
michael@0 | 143 | int *pcGlyphs |
michael@0 | 144 | ) |
michael@0 | 145 | { |
michael@0 | 146 | SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps; |
michael@0 | 147 | return ScriptShape (hdc, |
michael@0 | 148 | psc, |
michael@0 | 149 | pwcChars, |
michael@0 | 150 | cChars, |
michael@0 | 151 | cMaxGlyphs, |
michael@0 | 152 | psa, |
michael@0 | 153 | pwOutGlyphs, |
michael@0 | 154 | pwLogClust, |
michael@0 | 155 | psva, |
michael@0 | 156 | pcGlyphs); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | static HRESULT WINAPI |
michael@0 | 160 | hb_ScriptPlaceOpenType( |
michael@0 | 161 | HDC hdc, |
michael@0 | 162 | SCRIPT_CACHE *psc, |
michael@0 | 163 | SCRIPT_ANALYSIS *psa, |
michael@0 | 164 | OPENTYPE_TAG tagScript, |
michael@0 | 165 | OPENTYPE_TAG tagLangSys, |
michael@0 | 166 | int *rcRangeChars, |
michael@0 | 167 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
michael@0 | 168 | int cRanges, |
michael@0 | 169 | const WCHAR *pwcChars, |
michael@0 | 170 | WORD *pwLogClust, |
michael@0 | 171 | SCRIPT_CHARPROP *pCharProps, |
michael@0 | 172 | int cChars, |
michael@0 | 173 | const WORD *pwGlyphs, |
michael@0 | 174 | const SCRIPT_GLYPHPROP *pGlyphProps, |
michael@0 | 175 | int cGlyphs, |
michael@0 | 176 | int *piAdvance, |
michael@0 | 177 | GOFFSET *pGoffset, |
michael@0 | 178 | ABC *pABC |
michael@0 | 179 | ) |
michael@0 | 180 | { |
michael@0 | 181 | SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps; |
michael@0 | 182 | return ScriptPlace (hdc, |
michael@0 | 183 | psc, |
michael@0 | 184 | pwGlyphs, |
michael@0 | 185 | cGlyphs, |
michael@0 | 186 | psva, |
michael@0 | 187 | psa, |
michael@0 | 188 | piAdvance, |
michael@0 | 189 | pGoffset, |
michael@0 | 190 | pABC); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | |
michael@0 | 194 | struct hb_uniscribe_shaper_funcs_t { |
michael@0 | 195 | SIOT ScriptItemizeOpenType; |
michael@0 | 196 | SSOT ScriptShapeOpenType; |
michael@0 | 197 | SPOT ScriptPlaceOpenType; |
michael@0 | 198 | |
michael@0 | 199 | inline void init (void) |
michael@0 | 200 | { |
michael@0 | 201 | HMODULE hinstLib; |
michael@0 | 202 | this->ScriptItemizeOpenType = NULL; |
michael@0 | 203 | this->ScriptShapeOpenType = NULL; |
michael@0 | 204 | this->ScriptPlaceOpenType = NULL; |
michael@0 | 205 | |
michael@0 | 206 | hinstLib = GetModuleHandle (TEXT ("usp10.dll")); |
michael@0 | 207 | if (hinstLib) |
michael@0 | 208 | { |
michael@0 | 209 | this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType"); |
michael@0 | 210 | this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType"); |
michael@0 | 211 | this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType"); |
michael@0 | 212 | } |
michael@0 | 213 | if (!this->ScriptItemizeOpenType || |
michael@0 | 214 | !this->ScriptShapeOpenType || |
michael@0 | 215 | !this->ScriptPlaceOpenType) |
michael@0 | 216 | { |
michael@0 | 217 | DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back."); |
michael@0 | 218 | this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType; |
michael@0 | 219 | this->ScriptShapeOpenType = hb_ScriptShapeOpenType; |
michael@0 | 220 | this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType; |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | }; |
michael@0 | 224 | static hb_uniscribe_shaper_funcs_t *uniscribe_funcs; |
michael@0 | 225 | |
michael@0 | 226 | static inline void |
michael@0 | 227 | free_uniscribe_funcs (void) |
michael@0 | 228 | { |
michael@0 | 229 | free (uniscribe_funcs); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | static hb_uniscribe_shaper_funcs_t * |
michael@0 | 233 | hb_uniscribe_shaper_get_funcs (void) |
michael@0 | 234 | { |
michael@0 | 235 | retry: |
michael@0 | 236 | hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs); |
michael@0 | 237 | |
michael@0 | 238 | if (unlikely (!funcs)) |
michael@0 | 239 | { |
michael@0 | 240 | funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t)); |
michael@0 | 241 | if (unlikely (!funcs)) |
michael@0 | 242 | return NULL; |
michael@0 | 243 | |
michael@0 | 244 | funcs->init (); |
michael@0 | 245 | |
michael@0 | 246 | if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) { |
michael@0 | 247 | free (funcs); |
michael@0 | 248 | goto retry; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | #ifdef HAVE_ATEXIT |
michael@0 | 252 | atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */ |
michael@0 | 253 | #endif |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | return funcs; |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | |
michael@0 | 260 | struct active_feature_t { |
michael@0 | 261 | OPENTYPE_FEATURE_RECORD rec; |
michael@0 | 262 | unsigned int order; |
michael@0 | 263 | |
michael@0 | 264 | static int cmp (const active_feature_t *a, const active_feature_t *b) { |
michael@0 | 265 | return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 : |
michael@0 | 266 | a->order < b->order ? -1 : a->order > b->order ? 1 : |
michael@0 | 267 | a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 : |
michael@0 | 268 | 0; |
michael@0 | 269 | } |
michael@0 | 270 | bool operator== (const active_feature_t *f) { |
michael@0 | 271 | return cmp (this, f) == 0; |
michael@0 | 272 | } |
michael@0 | 273 | }; |
michael@0 | 274 | |
michael@0 | 275 | struct feature_event_t { |
michael@0 | 276 | unsigned int index; |
michael@0 | 277 | bool start; |
michael@0 | 278 | active_feature_t feature; |
michael@0 | 279 | |
michael@0 | 280 | static int cmp (const feature_event_t *a, const feature_event_t *b) { |
michael@0 | 281 | return a->index < b->index ? -1 : a->index > b->index ? 1 : |
michael@0 | 282 | a->start < b->start ? -1 : a->start > b->start ? 1 : |
michael@0 | 283 | active_feature_t::cmp (&a->feature, &b->feature); |
michael@0 | 284 | } |
michael@0 | 285 | }; |
michael@0 | 286 | |
michael@0 | 287 | struct range_record_t { |
michael@0 | 288 | TEXTRANGE_PROPERTIES props; |
michael@0 | 289 | unsigned int index_first; /* == start */ |
michael@0 | 290 | unsigned int index_last; /* == end - 1 */ |
michael@0 | 291 | }; |
michael@0 | 292 | |
michael@0 | 293 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face) |
michael@0 | 294 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font) |
michael@0 | 295 | |
michael@0 | 296 | |
michael@0 | 297 | /* |
michael@0 | 298 | * shaper face data |
michael@0 | 299 | */ |
michael@0 | 300 | |
michael@0 | 301 | struct hb_uniscribe_shaper_face_data_t { |
michael@0 | 302 | HANDLE fh; |
michael@0 | 303 | hb_uniscribe_shaper_funcs_t *funcs; |
michael@0 | 304 | wchar_t face_name[LF_FACESIZE]; |
michael@0 | 305 | }; |
michael@0 | 306 | |
michael@0 | 307 | /* face_name should point to a wchar_t[LF_FACESIZE] object. */ |
michael@0 | 308 | static void |
michael@0 | 309 | _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen) |
michael@0 | 310 | { |
michael@0 | 311 | /* We'll create a private name for the font from a UUID using a simple, |
michael@0 | 312 | * somewhat base64-like encoding scheme */ |
michael@0 | 313 | const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"; |
michael@0 | 314 | UUID id; |
michael@0 | 315 | UuidCreate ((UUID*) &id); |
michael@0 | 316 | unsigned int name_str_len = 0; |
michael@0 | 317 | face_name[name_str_len++] = 'F'; |
michael@0 | 318 | face_name[name_str_len++] = '_'; |
michael@0 | 319 | unsigned char *p = (unsigned char *) &id; |
michael@0 | 320 | for (unsigned int i = 0; i < 16; i += 2) |
michael@0 | 321 | { |
michael@0 | 322 | /* Spread the 16 bits from two bytes of the UUID across three chars of face_name, |
michael@0 | 323 | * using the bits in groups of 5,5,6 to select chars from enc. |
michael@0 | 324 | * This will generate 24 characters; with the 'F_' prefix we already provided, |
michael@0 | 325 | * the name will be 26 chars (plus the NUL terminator), so will always fit within |
michael@0 | 326 | * face_name (LF_FACESIZE = 32). */ |
michael@0 | 327 | face_name[name_str_len++] = enc[p[i] >> 3]; |
michael@0 | 328 | face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f]; |
michael@0 | 329 | face_name[name_str_len++] = enc[p[i + 1] & 0x3f]; |
michael@0 | 330 | } |
michael@0 | 331 | face_name[name_str_len] = 0; |
michael@0 | 332 | if (plen) |
michael@0 | 333 | *plen = name_str_len; |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | /* Destroys blob. */ |
michael@0 | 337 | static hb_blob_t * |
michael@0 | 338 | _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) |
michael@0 | 339 | { |
michael@0 | 340 | /* Create a copy of the font data, with the 'name' table replaced by a |
michael@0 | 341 | * table that names the font with our private F_* name created above. |
michael@0 | 342 | * For simplicity, we just append a new 'name' table and update the |
michael@0 | 343 | * sfnt directory; the original table is left in place, but unused. |
michael@0 | 344 | * |
michael@0 | 345 | * The new table will contain just 5 name IDs: family, style, unique, |
michael@0 | 346 | * full, PS. All of them point to the same name data with our unique name. |
michael@0 | 347 | */ |
michael@0 | 348 | |
michael@0 | 349 | blob = OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (blob); |
michael@0 | 350 | |
michael@0 | 351 | unsigned int length, new_length, name_str_len; |
michael@0 | 352 | const char *orig_sfnt_data = hb_blob_get_data (blob, &length); |
michael@0 | 353 | |
michael@0 | 354 | _hb_generate_unique_face_name (new_name, &name_str_len); |
michael@0 | 355 | |
michael@0 | 356 | static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 }; |
michael@0 | 357 | |
michael@0 | 358 | unsigned int name_table_length = OT::name::min_size + |
michael@0 | 359 | ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size + |
michael@0 | 360 | name_str_len * 2; /* for name data in UTF16BE form */ |
michael@0 | 361 | unsigned int name_table_offset = (length + 3) & ~3; |
michael@0 | 362 | |
michael@0 | 363 | new_length = name_table_offset + ((name_table_length + 3) & ~3); |
michael@0 | 364 | void *new_sfnt_data = calloc (1, new_length); |
michael@0 | 365 | if (!new_sfnt_data) |
michael@0 | 366 | { |
michael@0 | 367 | hb_blob_destroy (blob); |
michael@0 | 368 | return NULL; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | memcpy(new_sfnt_data, orig_sfnt_data, length); |
michael@0 | 372 | |
michael@0 | 373 | OT::name &name = OT::StructAtOffset<OT::name> (new_sfnt_data, name_table_offset); |
michael@0 | 374 | name.format.set (0); |
michael@0 | 375 | name.count.set (ARRAY_LENGTH (name_IDs)); |
michael@0 | 376 | name.stringOffset.set (name.get_size ()); |
michael@0 | 377 | for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++) |
michael@0 | 378 | { |
michael@0 | 379 | OT::NameRecord &record = name.nameRecord[i]; |
michael@0 | 380 | record.platformID.set (3); |
michael@0 | 381 | record.encodingID.set (1); |
michael@0 | 382 | record.languageID.set (0x0409); /* English */ |
michael@0 | 383 | record.nameID.set (name_IDs[i]); |
michael@0 | 384 | record.length.set (name_str_len * 2); |
michael@0 | 385 | record.offset.set (0); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | /* Copy string data from new_name, converting wchar_t to UTF16BE. */ |
michael@0 | 389 | unsigned char *p = &OT::StructAfter<unsigned char> (name); |
michael@0 | 390 | for (unsigned int i = 0; i < name_str_len; i++) |
michael@0 | 391 | { |
michael@0 | 392 | *p++ = new_name[i] >> 8; |
michael@0 | 393 | *p++ = new_name[i] & 0xff; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | /* Adjust name table entry to point to new name table */ |
michael@0 | 397 | const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data); |
michael@0 | 398 | unsigned int face_count = file.get_face_count (); |
michael@0 | 399 | for (unsigned int face_index = 0; face_index < face_count; face_index++) |
michael@0 | 400 | { |
michael@0 | 401 | /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be |
michael@0 | 402 | * toe-stepping. But we don't really care. */ |
michael@0 | 403 | const OT::OpenTypeFontFace &face = file.get_face (face_index); |
michael@0 | 404 | unsigned int index; |
michael@0 | 405 | if (face.find_table_index (HB_OT_TAG_name, &index)) |
michael@0 | 406 | { |
michael@0 | 407 | OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index)); |
michael@0 | 408 | record.checkSum.set_for_data (&name, name_table_length); |
michael@0 | 409 | record.offset.set (name_table_offset); |
michael@0 | 410 | record.length.set (name_table_length); |
michael@0 | 411 | } |
michael@0 | 412 | else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */ |
michael@0 | 413 | { |
michael@0 | 414 | free (new_sfnt_data); |
michael@0 | 415 | hb_blob_destroy (blob); |
michael@0 | 416 | return NULL; |
michael@0 | 417 | } |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | /* The checkSumAdjustment field in the 'head' table is now wrong, |
michael@0 | 421 | * but that doesn't actually seem to cause any problems so we don't |
michael@0 | 422 | * bother. */ |
michael@0 | 423 | |
michael@0 | 424 | hb_blob_destroy (blob); |
michael@0 | 425 | return hb_blob_create ((const char *) new_sfnt_data, new_length, |
michael@0 | 426 | HB_MEMORY_MODE_WRITABLE, NULL, free); |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | hb_uniscribe_shaper_face_data_t * |
michael@0 | 430 | _hb_uniscribe_shaper_face_data_create (hb_face_t *face) |
michael@0 | 431 | { |
michael@0 | 432 | hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t)); |
michael@0 | 433 | if (unlikely (!data)) |
michael@0 | 434 | return NULL; |
michael@0 | 435 | |
michael@0 | 436 | data->funcs = hb_uniscribe_shaper_get_funcs (); |
michael@0 | 437 | if (unlikely (!data->funcs)) |
michael@0 | 438 | { |
michael@0 | 439 | free (data); |
michael@0 | 440 | return NULL; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | hb_blob_t *blob = hb_face_reference_blob (face); |
michael@0 | 444 | if (unlikely (!hb_blob_get_length (blob))) |
michael@0 | 445 | DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
michael@0 | 446 | |
michael@0 | 447 | blob = _hb_rename_font (blob, data->face_name); |
michael@0 | 448 | if (unlikely (!blob)) |
michael@0 | 449 | { |
michael@0 | 450 | free (data); |
michael@0 | 451 | return NULL; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | DWORD num_fonts_installed; |
michael@0 | 455 | data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL), |
michael@0 | 456 | hb_blob_get_length (blob), |
michael@0 | 457 | 0, &num_fonts_installed); |
michael@0 | 458 | if (unlikely (!data->fh)) |
michael@0 | 459 | { |
michael@0 | 460 | DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
michael@0 | 461 | free (data); |
michael@0 | 462 | return NULL; |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | return data; |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | void |
michael@0 | 469 | _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data) |
michael@0 | 470 | { |
michael@0 | 471 | RemoveFontMemResourceEx (data->fh); |
michael@0 | 472 | free (data); |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | |
michael@0 | 476 | /* |
michael@0 | 477 | * shaper font data |
michael@0 | 478 | */ |
michael@0 | 479 | |
michael@0 | 480 | struct hb_uniscribe_shaper_font_data_t { |
michael@0 | 481 | HDC hdc; |
michael@0 | 482 | LOGFONTW log_font; |
michael@0 | 483 | HFONT hfont; |
michael@0 | 484 | SCRIPT_CACHE script_cache; |
michael@0 | 485 | }; |
michael@0 | 486 | |
michael@0 | 487 | static bool |
michael@0 | 488 | populate_log_font (LOGFONTW *lf, |
michael@0 | 489 | hb_font_t *font) |
michael@0 | 490 | { |
michael@0 | 491 | memset (lf, 0, sizeof (*lf)); |
michael@0 | 492 | lf->lfHeight = -font->y_scale; |
michael@0 | 493 | lf->lfCharSet = DEFAULT_CHARSET; |
michael@0 | 494 | |
michael@0 | 495 | hb_face_t *face = font->face; |
michael@0 | 496 | hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); |
michael@0 | 497 | |
michael@0 | 498 | memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName)); |
michael@0 | 499 | |
michael@0 | 500 | return true; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | hb_uniscribe_shaper_font_data_t * |
michael@0 | 504 | _hb_uniscribe_shaper_font_data_create (hb_font_t *font) |
michael@0 | 505 | { |
michael@0 | 506 | if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL; |
michael@0 | 507 | |
michael@0 | 508 | hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t)); |
michael@0 | 509 | if (unlikely (!data)) |
michael@0 | 510 | return NULL; |
michael@0 | 511 | |
michael@0 | 512 | data->hdc = GetDC (NULL); |
michael@0 | 513 | |
michael@0 | 514 | if (unlikely (!populate_log_font (&data->log_font, font))) { |
michael@0 | 515 | DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
michael@0 | 516 | _hb_uniscribe_shaper_font_data_destroy (data); |
michael@0 | 517 | return NULL; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | data->hfont = CreateFontIndirectW (&data->log_font); |
michael@0 | 521 | if (unlikely (!data->hfont)) { |
michael@0 | 522 | DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); |
michael@0 | 523 | _hb_uniscribe_shaper_font_data_destroy (data); |
michael@0 | 524 | return NULL; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | if (!SelectObject (data->hdc, data->hfont)) { |
michael@0 | 528 | DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); |
michael@0 | 529 | _hb_uniscribe_shaper_font_data_destroy (data); |
michael@0 | 530 | return NULL; |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | return data; |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | void |
michael@0 | 537 | _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data) |
michael@0 | 538 | { |
michael@0 | 539 | if (data->hdc) |
michael@0 | 540 | ReleaseDC (NULL, data->hdc); |
michael@0 | 541 | if (data->hfont) |
michael@0 | 542 | DeleteObject (data->hfont); |
michael@0 | 543 | if (data->script_cache) |
michael@0 | 544 | ScriptFreeCache (&data->script_cache); |
michael@0 | 545 | free (data); |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | LOGFONTW * |
michael@0 | 549 | hb_uniscribe_font_get_logfontw (hb_font_t *font) |
michael@0 | 550 | { |
michael@0 | 551 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
michael@0 | 552 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
michael@0 | 553 | return &font_data->log_font; |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | HFONT |
michael@0 | 557 | hb_uniscribe_font_get_hfont (hb_font_t *font) |
michael@0 | 558 | { |
michael@0 | 559 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
michael@0 | 560 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
michael@0 | 561 | return font_data->hfont; |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | |
michael@0 | 565 | /* |
michael@0 | 566 | * shaper shape_plan data |
michael@0 | 567 | */ |
michael@0 | 568 | |
michael@0 | 569 | struct hb_uniscribe_shaper_shape_plan_data_t {}; |
michael@0 | 570 | |
michael@0 | 571 | hb_uniscribe_shaper_shape_plan_data_t * |
michael@0 | 572 | _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, |
michael@0 | 573 | const hb_feature_t *user_features HB_UNUSED, |
michael@0 | 574 | unsigned int num_user_features HB_UNUSED) |
michael@0 | 575 | { |
michael@0 | 576 | return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | void |
michael@0 | 580 | _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED) |
michael@0 | 581 | { |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | |
michael@0 | 585 | /* |
michael@0 | 586 | * shaper |
michael@0 | 587 | */ |
michael@0 | 588 | |
michael@0 | 589 | |
michael@0 | 590 | hb_bool_t |
michael@0 | 591 | _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, |
michael@0 | 592 | hb_font_t *font, |
michael@0 | 593 | hb_buffer_t *buffer, |
michael@0 | 594 | const hb_feature_t *features, |
michael@0 | 595 | unsigned int num_features) |
michael@0 | 596 | { |
michael@0 | 597 | hb_face_t *face = font->face; |
michael@0 | 598 | hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); |
michael@0 | 599 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
michael@0 | 600 | hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs; |
michael@0 | 601 | |
michael@0 | 602 | /* |
michael@0 | 603 | * Set up features. |
michael@0 | 604 | */ |
michael@0 | 605 | hb_auto_array_t<OPENTYPE_FEATURE_RECORD> feature_records; |
michael@0 | 606 | hb_auto_array_t<range_record_t> range_records; |
michael@0 | 607 | if (num_features) |
michael@0 | 608 | { |
michael@0 | 609 | /* Sort features by start/end events. */ |
michael@0 | 610 | hb_auto_array_t<feature_event_t> feature_events; |
michael@0 | 611 | for (unsigned int i = 0; i < num_features; i++) |
michael@0 | 612 | { |
michael@0 | 613 | active_feature_t feature; |
michael@0 | 614 | feature.rec.tagFeature = hb_uint32_swap (features[i].tag); |
michael@0 | 615 | feature.rec.lParameter = features[i].value; |
michael@0 | 616 | feature.order = i; |
michael@0 | 617 | |
michael@0 | 618 | feature_event_t *event; |
michael@0 | 619 | |
michael@0 | 620 | event = feature_events.push (); |
michael@0 | 621 | if (unlikely (!event)) |
michael@0 | 622 | goto fail_features; |
michael@0 | 623 | event->index = features[i].start; |
michael@0 | 624 | event->start = true; |
michael@0 | 625 | event->feature = feature; |
michael@0 | 626 | |
michael@0 | 627 | event = feature_events.push (); |
michael@0 | 628 | if (unlikely (!event)) |
michael@0 | 629 | goto fail_features; |
michael@0 | 630 | event->index = features[i].end; |
michael@0 | 631 | event->start = false; |
michael@0 | 632 | event->feature = feature; |
michael@0 | 633 | } |
michael@0 | 634 | feature_events.sort (); |
michael@0 | 635 | /* Add a strategic final event. */ |
michael@0 | 636 | { |
michael@0 | 637 | active_feature_t feature; |
michael@0 | 638 | feature.rec.tagFeature = 0; |
michael@0 | 639 | feature.rec.lParameter = 0; |
michael@0 | 640 | feature.order = num_features + 1; |
michael@0 | 641 | |
michael@0 | 642 | feature_event_t *event = feature_events.push (); |
michael@0 | 643 | if (unlikely (!event)) |
michael@0 | 644 | goto fail_features; |
michael@0 | 645 | event->index = 0; /* This value does magic. */ |
michael@0 | 646 | event->start = false; |
michael@0 | 647 | event->feature = feature; |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | /* Scan events and save features for each range. */ |
michael@0 | 651 | hb_auto_array_t<active_feature_t> active_features; |
michael@0 | 652 | unsigned int last_index = 0; |
michael@0 | 653 | for (unsigned int i = 0; i < feature_events.len; i++) |
michael@0 | 654 | { |
michael@0 | 655 | feature_event_t *event = &feature_events[i]; |
michael@0 | 656 | |
michael@0 | 657 | if (event->index != last_index) |
michael@0 | 658 | { |
michael@0 | 659 | /* Save a snapshot of active features and the range. */ |
michael@0 | 660 | range_record_t *range = range_records.push (); |
michael@0 | 661 | if (unlikely (!range)) |
michael@0 | 662 | goto fail_features; |
michael@0 | 663 | |
michael@0 | 664 | unsigned int offset = feature_records.len; |
michael@0 | 665 | |
michael@0 | 666 | active_features.sort (); |
michael@0 | 667 | for (unsigned int j = 0; j < active_features.len; j++) |
michael@0 | 668 | { |
michael@0 | 669 | if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature) |
michael@0 | 670 | { |
michael@0 | 671 | OPENTYPE_FEATURE_RECORD *feature = feature_records.push (); |
michael@0 | 672 | if (unlikely (!feature)) |
michael@0 | 673 | goto fail_features; |
michael@0 | 674 | *feature = active_features[j].rec; |
michael@0 | 675 | } |
michael@0 | 676 | else |
michael@0 | 677 | { |
michael@0 | 678 | /* Overrides value for existing feature. */ |
michael@0 | 679 | feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter; |
michael@0 | 680 | } |
michael@0 | 681 | } |
michael@0 | 682 | |
michael@0 | 683 | /* Will convert to pointer after all is ready, since feature_records.array |
michael@0 | 684 | * may move as we grow it. */ |
michael@0 | 685 | range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset); |
michael@0 | 686 | range->props.cotfRecords = feature_records.len - offset; |
michael@0 | 687 | range->index_first = last_index; |
michael@0 | 688 | range->index_last = event->index - 1; |
michael@0 | 689 | |
michael@0 | 690 | last_index = event->index; |
michael@0 | 691 | } |
michael@0 | 692 | |
michael@0 | 693 | if (event->start) { |
michael@0 | 694 | active_feature_t *feature = active_features.push (); |
michael@0 | 695 | if (unlikely (!feature)) |
michael@0 | 696 | goto fail_features; |
michael@0 | 697 | *feature = event->feature; |
michael@0 | 698 | } else { |
michael@0 | 699 | active_feature_t *feature = active_features.find (&event->feature); |
michael@0 | 700 | if (feature) |
michael@0 | 701 | active_features.remove (feature - active_features.array); |
michael@0 | 702 | } |
michael@0 | 703 | } |
michael@0 | 704 | |
michael@0 | 705 | if (!range_records.len) /* No active feature found. */ |
michael@0 | 706 | goto fail_features; |
michael@0 | 707 | |
michael@0 | 708 | /* Fixup the pointers. */ |
michael@0 | 709 | for (unsigned int i = 0; i < range_records.len; i++) |
michael@0 | 710 | { |
michael@0 | 711 | range_record_t *range = &range_records[i]; |
michael@0 | 712 | range->props.potfRecords = feature_records.array + reinterpret_cast<uintptr_t> (range->props.potfRecords); |
michael@0 | 713 | } |
michael@0 | 714 | } |
michael@0 | 715 | else |
michael@0 | 716 | { |
michael@0 | 717 | fail_features: |
michael@0 | 718 | num_features = 0; |
michael@0 | 719 | } |
michael@0 | 720 | |
michael@0 | 721 | #define FAIL(...) \ |
michael@0 | 722 | HB_STMT_START { \ |
michael@0 | 723 | DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ |
michael@0 | 724 | return false; \ |
michael@0 | 725 | } HB_STMT_END; |
michael@0 | 726 | |
michael@0 | 727 | HRESULT hr; |
michael@0 | 728 | |
michael@0 | 729 | retry: |
michael@0 | 730 | |
michael@0 | 731 | unsigned int scratch_size; |
michael@0 | 732 | hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); |
michael@0 | 733 | |
michael@0 | 734 | #define ALLOCATE_ARRAY(Type, name, len) \ |
michael@0 | 735 | Type *name = (Type *) scratch; \ |
michael@0 | 736 | { \ |
michael@0 | 737 | unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ |
michael@0 | 738 | assert (_consumed <= scratch_size); \ |
michael@0 | 739 | scratch += _consumed; \ |
michael@0 | 740 | scratch_size -= _consumed; \ |
michael@0 | 741 | } |
michael@0 | 742 | |
michael@0 | 743 | #define utf16_index() var1.u32 |
michael@0 | 744 | |
michael@0 | 745 | ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2); |
michael@0 | 746 | |
michael@0 | 747 | unsigned int chars_len = 0; |
michael@0 | 748 | for (unsigned int i = 0; i < buffer->len; i++) |
michael@0 | 749 | { |
michael@0 | 750 | hb_codepoint_t c = buffer->info[i].codepoint; |
michael@0 | 751 | buffer->info[i].utf16_index() = chars_len; |
michael@0 | 752 | if (likely (c < 0x10000)) |
michael@0 | 753 | pchars[chars_len++] = c; |
michael@0 | 754 | else if (unlikely (c >= 0x110000)) |
michael@0 | 755 | pchars[chars_len++] = 0xFFFD; |
michael@0 | 756 | else { |
michael@0 | 757 | pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); |
michael@0 | 758 | pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); |
michael@0 | 759 | } |
michael@0 | 760 | } |
michael@0 | 761 | |
michael@0 | 762 | ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
michael@0 | 763 | ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
michael@0 | 764 | |
michael@0 | 765 | if (num_features) |
michael@0 | 766 | { |
michael@0 | 767 | /* Need log_clusters to assign features. */ |
michael@0 | 768 | chars_len = 0; |
michael@0 | 769 | for (unsigned int i = 0; i < buffer->len; i++) |
michael@0 | 770 | { |
michael@0 | 771 | hb_codepoint_t c = buffer->info[i].codepoint; |
michael@0 | 772 | unsigned int cluster = buffer->info[i].cluster; |
michael@0 | 773 | log_clusters[chars_len++] = cluster; |
michael@0 | 774 | if (c >= 0x10000 && c < 0x110000) |
michael@0 | 775 | log_clusters[chars_len++] = cluster; /* Surrogates. */ |
michael@0 | 776 | } |
michael@0 | 777 | } |
michael@0 | 778 | |
michael@0 | 779 | /* The -2 in the following is to compensate for possible |
michael@0 | 780 | * alignment needed after the WORD array. sizeof(WORD) == 2. */ |
michael@0 | 781 | unsigned int glyphs_size = (scratch_size * sizeof (int) - 2) |
michael@0 | 782 | / (sizeof (WORD) + |
michael@0 | 783 | sizeof (SCRIPT_GLYPHPROP) + |
michael@0 | 784 | sizeof (int) + |
michael@0 | 785 | sizeof (GOFFSET) + |
michael@0 | 786 | sizeof (uint32_t)); |
michael@0 | 787 | |
michael@0 | 788 | ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); |
michael@0 | 789 | ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); |
michael@0 | 790 | ALLOCATE_ARRAY (int, advances, glyphs_size); |
michael@0 | 791 | ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); |
michael@0 | 792 | ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); |
michael@0 | 793 | |
michael@0 | 794 | /* Note: |
michael@0 | 795 | * We can't touch the contents of glyph_props. Our fallback |
michael@0 | 796 | * implementations of Shape and Place functions use that buffer |
michael@0 | 797 | * by casting it to a different type. It works because they |
michael@0 | 798 | * both agree about it, but if we want to access it here we |
michael@0 | 799 | * need address that issue first. |
michael@0 | 800 | */ |
michael@0 | 801 | |
michael@0 | 802 | #undef ALLOCATE_ARRAY |
michael@0 | 803 | |
michael@0 | 804 | #define MAX_ITEMS 256 |
michael@0 | 805 | |
michael@0 | 806 | SCRIPT_ITEM items[MAX_ITEMS + 1]; |
michael@0 | 807 | SCRIPT_CONTROL bidi_control = {0}; |
michael@0 | 808 | SCRIPT_STATE bidi_state = {0}; |
michael@0 | 809 | ULONG script_tags[MAX_ITEMS]; |
michael@0 | 810 | int item_count; |
michael@0 | 811 | |
michael@0 | 812 | /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ |
michael@0 | 813 | //bidi_control.fMergeNeutralItems = true; |
michael@0 | 814 | *(uint32_t*)&bidi_control |= 1<<24; |
michael@0 | 815 | |
michael@0 | 816 | bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
michael@0 | 817 | bidi_state.fOverrideDirection = 1; |
michael@0 | 818 | |
michael@0 | 819 | hr = funcs->ScriptItemizeOpenType (pchars, |
michael@0 | 820 | chars_len, |
michael@0 | 821 | MAX_ITEMS, |
michael@0 | 822 | &bidi_control, |
michael@0 | 823 | &bidi_state, |
michael@0 | 824 | items, |
michael@0 | 825 | script_tags, |
michael@0 | 826 | &item_count); |
michael@0 | 827 | if (unlikely (FAILED (hr))) |
michael@0 | 828 | FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); |
michael@0 | 829 | |
michael@0 | 830 | #undef MAX_ITEMS |
michael@0 | 831 | |
michael@0 | 832 | OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language)); |
michael@0 | 833 | hb_auto_array_t<TEXTRANGE_PROPERTIES*> range_properties; |
michael@0 | 834 | hb_auto_array_t<int> range_char_counts; |
michael@0 | 835 | |
michael@0 | 836 | unsigned int glyphs_offset = 0; |
michael@0 | 837 | unsigned int glyphs_len; |
michael@0 | 838 | bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
michael@0 | 839 | for (unsigned int i = 0; i < item_count; i++) |
michael@0 | 840 | { |
michael@0 | 841 | unsigned int chars_offset = items[i].iCharPos; |
michael@0 | 842 | unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
michael@0 | 843 | |
michael@0 | 844 | if (num_features) |
michael@0 | 845 | { |
michael@0 | 846 | range_properties.shrink (0); |
michael@0 | 847 | range_char_counts.shrink (0); |
michael@0 | 848 | |
michael@0 | 849 | range_record_t *last_range = &range_records[0]; |
michael@0 | 850 | |
michael@0 | 851 | for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++) |
michael@0 | 852 | { |
michael@0 | 853 | range_record_t *range = last_range; |
michael@0 | 854 | while (log_clusters[k] < range->index_first) |
michael@0 | 855 | range--; |
michael@0 | 856 | while (log_clusters[k] > range->index_last) |
michael@0 | 857 | range++; |
michael@0 | 858 | if (!range_properties.len || |
michael@0 | 859 | &range->props != range_properties[range_properties.len - 1]) |
michael@0 | 860 | { |
michael@0 | 861 | TEXTRANGE_PROPERTIES **props = range_properties.push (); |
michael@0 | 862 | int *c = range_char_counts.push (); |
michael@0 | 863 | if (unlikely (!props || !c)) |
michael@0 | 864 | { |
michael@0 | 865 | range_properties.shrink (0); |
michael@0 | 866 | range_char_counts.shrink (0); |
michael@0 | 867 | break; |
michael@0 | 868 | } |
michael@0 | 869 | *props = &range->props; |
michael@0 | 870 | *c = 1; |
michael@0 | 871 | } |
michael@0 | 872 | else |
michael@0 | 873 | { |
michael@0 | 874 | range_char_counts[range_char_counts.len - 1]++; |
michael@0 | 875 | } |
michael@0 | 876 | |
michael@0 | 877 | last_range = range; |
michael@0 | 878 | } |
michael@0 | 879 | } |
michael@0 | 880 | |
michael@0 | 881 | /* Asking for glyphs in logical order circumvents at least |
michael@0 | 882 | * one bug in Uniscribe. */ |
michael@0 | 883 | items[i].a.fLogicalOrder = true; |
michael@0 | 884 | |
michael@0 | 885 | retry_shape: |
michael@0 | 886 | hr = funcs->ScriptShapeOpenType (font_data->hdc, |
michael@0 | 887 | &font_data->script_cache, |
michael@0 | 888 | &items[i].a, |
michael@0 | 889 | script_tags[i], |
michael@0 | 890 | language_tag, |
michael@0 | 891 | range_char_counts.array, |
michael@0 | 892 | range_properties.array, |
michael@0 | 893 | range_properties.len, |
michael@0 | 894 | pchars + chars_offset, |
michael@0 | 895 | item_chars_len, |
michael@0 | 896 | glyphs_size - glyphs_offset, |
michael@0 | 897 | /* out */ |
michael@0 | 898 | log_clusters + chars_offset, |
michael@0 | 899 | char_props + chars_offset, |
michael@0 | 900 | glyphs + glyphs_offset, |
michael@0 | 901 | glyph_props + glyphs_offset, |
michael@0 | 902 | (int *) &glyphs_len); |
michael@0 | 903 | |
michael@0 | 904 | if (unlikely (items[i].a.fNoGlyphIndex)) |
michael@0 | 905 | FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
michael@0 | 906 | if (unlikely (hr == E_OUTOFMEMORY)) |
michael@0 | 907 | { |
michael@0 | 908 | buffer->ensure (buffer->allocated * 2); |
michael@0 | 909 | if (buffer->in_error) |
michael@0 | 910 | FAIL ("Buffer resize failed"); |
michael@0 | 911 | goto retry; |
michael@0 | 912 | } |
michael@0 | 913 | if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
michael@0 | 914 | { |
michael@0 | 915 | if (items[i].a.eScript == SCRIPT_UNDEFINED) |
michael@0 | 916 | FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); |
michael@0 | 917 | items[i].a.eScript = SCRIPT_UNDEFINED; |
michael@0 | 918 | goto retry_shape; |
michael@0 | 919 | } |
michael@0 | 920 | if (unlikely (FAILED (hr))) |
michael@0 | 921 | { |
michael@0 | 922 | FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); |
michael@0 | 923 | } |
michael@0 | 924 | |
michael@0 | 925 | for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++) |
michael@0 | 926 | log_clusters[j] += glyphs_offset; |
michael@0 | 927 | |
michael@0 | 928 | hr = funcs->ScriptPlaceOpenType (font_data->hdc, |
michael@0 | 929 | &font_data->script_cache, |
michael@0 | 930 | &items[i].a, |
michael@0 | 931 | script_tags[i], |
michael@0 | 932 | language_tag, |
michael@0 | 933 | range_char_counts.array, |
michael@0 | 934 | range_properties.array, |
michael@0 | 935 | range_properties.len, |
michael@0 | 936 | pchars + chars_offset, |
michael@0 | 937 | log_clusters + chars_offset, |
michael@0 | 938 | char_props + chars_offset, |
michael@0 | 939 | item_chars_len, |
michael@0 | 940 | glyphs + glyphs_offset, |
michael@0 | 941 | glyph_props + glyphs_offset, |
michael@0 | 942 | glyphs_len, |
michael@0 | 943 | /* out */ |
michael@0 | 944 | advances + glyphs_offset, |
michael@0 | 945 | offsets + glyphs_offset, |
michael@0 | 946 | NULL); |
michael@0 | 947 | if (unlikely (FAILED (hr))) |
michael@0 | 948 | FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); |
michael@0 | 949 | |
michael@0 | 950 | if (DEBUG_ENABLED (UNISCRIBE)) |
michael@0 | 951 | fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n", |
michael@0 | 952 | i, |
michael@0 | 953 | items[i].a.fRTL, |
michael@0 | 954 | items[i].a.fLayoutRTL, |
michael@0 | 955 | items[i].a.fLogicalOrder, |
michael@0 | 956 | HB_UNTAG (hb_uint32_swap (script_tags[i]))); |
michael@0 | 957 | |
michael@0 | 958 | glyphs_offset += glyphs_len; |
michael@0 | 959 | } |
michael@0 | 960 | glyphs_len = glyphs_offset; |
michael@0 | 961 | |
michael@0 | 962 | /* Ok, we've got everything we need, now compose output buffer, |
michael@0 | 963 | * very, *very*, carefully! */ |
michael@0 | 964 | |
michael@0 | 965 | /* Calculate visual-clusters. That's what we ship. */ |
michael@0 | 966 | for (unsigned int i = 0; i < glyphs_len; i++) |
michael@0 | 967 | vis_clusters[i] = -1; |
michael@0 | 968 | for (unsigned int i = 0; i < buffer->len; i++) { |
michael@0 | 969 | uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
michael@0 | 970 | *p = MIN (*p, buffer->info[i].cluster); |
michael@0 | 971 | } |
michael@0 | 972 | for (unsigned int i = 1; i < glyphs_len; i++) |
michael@0 | 973 | if (vis_clusters[i] == -1) |
michael@0 | 974 | vis_clusters[i] = vis_clusters[i - 1]; |
michael@0 | 975 | |
michael@0 | 976 | #undef utf16_index |
michael@0 | 977 | |
michael@0 | 978 | buffer->ensure (glyphs_len); |
michael@0 | 979 | if (buffer->in_error) |
michael@0 | 980 | FAIL ("Buffer in error"); |
michael@0 | 981 | |
michael@0 | 982 | #undef FAIL |
michael@0 | 983 | |
michael@0 | 984 | /* Set glyph infos */ |
michael@0 | 985 | buffer->len = 0; |
michael@0 | 986 | for (unsigned int i = 0; i < glyphs_len; i++) |
michael@0 | 987 | { |
michael@0 | 988 | hb_glyph_info_t *info = &buffer->info[buffer->len++]; |
michael@0 | 989 | |
michael@0 | 990 | info->codepoint = glyphs[i]; |
michael@0 | 991 | info->cluster = vis_clusters[i]; |
michael@0 | 992 | |
michael@0 | 993 | /* The rest is crap. Let's store position info there for now. */ |
michael@0 | 994 | info->mask = advances[i]; |
michael@0 | 995 | info->var1.u32 = offsets[i].du; |
michael@0 | 996 | info->var2.u32 = offsets[i].dv; |
michael@0 | 997 | } |
michael@0 | 998 | |
michael@0 | 999 | /* Set glyph positions */ |
michael@0 | 1000 | buffer->clear_positions (); |
michael@0 | 1001 | for (unsigned int i = 0; i < glyphs_len; i++) |
michael@0 | 1002 | { |
michael@0 | 1003 | hb_glyph_info_t *info = &buffer->info[i]; |
michael@0 | 1004 | hb_glyph_position_t *pos = &buffer->pos[i]; |
michael@0 | 1005 | |
michael@0 | 1006 | /* TODO vertical */ |
michael@0 | 1007 | pos->x_advance = info->mask; |
michael@0 | 1008 | pos->x_offset = backward ? -info->var1.u32 : info->var1.u32; |
michael@0 | 1009 | pos->y_offset = info->var2.u32; |
michael@0 | 1010 | } |
michael@0 | 1011 | |
michael@0 | 1012 | if (backward) |
michael@0 | 1013 | hb_buffer_reverse (buffer); |
michael@0 | 1014 | |
michael@0 | 1015 | /* Wow, done! */ |
michael@0 | 1016 | return true; |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 |