Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 1c1233e | 2012-06-08 09:20:53 -0400 | [diff] [blame] | 2 | * Copyright © 2011,2012 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 | 552bf3a | 2012-07-11 16:44:51 -0400 | [diff] [blame] | 27 | #define _WIN32_WINNT 0x0600 |
Behdad Esfahbod | eb56f6a | 2012-08-07 21:44:25 -0400 | [diff] [blame] | 28 | #define WIN32_LEAN_AND_MEAN |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 29 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 30 | #define HB_SHAPER uniscribe |
| 31 | #include "hb-shaper-impl-private.hh" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 33 | #include <windows.h> |
| 34 | #include <usp10.h> |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 35 | #include <rpc.h> |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 36 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 37 | #include "hb-uniscribe.h" |
| 38 | |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 39 | #include "hb-open-file-private.hh" |
Behdad Esfahbod | 7a750ac | 2011-08-17 14:19:59 +0200 | [diff] [blame] | 40 | #include "hb-ot-name-table.hh" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 41 | #include "hb-ot-tag.h" |
| 42 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 43 | |
| 44 | #ifndef HB_DEBUG_UNISCRIBE |
| 45 | #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0) |
| 46 | #endif |
| 47 | |
| 48 | |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 49 | typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 50 | const WCHAR *pwcInChars, |
| 51 | int cInChars, |
| 52 | int cMaxItems, |
| 53 | const SCRIPT_CONTROL *psControl, |
| 54 | const SCRIPT_STATE *psState, |
| 55 | SCRIPT_ITEM *pItems, |
| 56 | OPENTYPE_TAG *pScriptTags, |
| 57 | int *pcItems |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 58 | ); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 59 | |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 60 | typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 61 | HDC hdc, |
| 62 | SCRIPT_CACHE *psc, |
| 63 | SCRIPT_ANALYSIS *psa, |
| 64 | OPENTYPE_TAG tagScript, |
| 65 | OPENTYPE_TAG tagLangSys, |
| 66 | int *rcRangeChars, |
| 67 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
| 68 | int cRanges, |
| 69 | const WCHAR *pwcChars, |
| 70 | int cChars, |
| 71 | int cMaxGlyphs, |
| 72 | WORD *pwLogClust, |
| 73 | SCRIPT_CHARPROP *pCharProps, |
| 74 | WORD *pwOutGlyphs, |
| 75 | SCRIPT_GLYPHPROP *pOutGlyphProps, |
| 76 | int *pcGlyphs |
| 77 | ); |
| 78 | |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 79 | typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/( |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 80 | HDC hdc, |
| 81 | SCRIPT_CACHE *psc, |
| 82 | SCRIPT_ANALYSIS *psa, |
| 83 | OPENTYPE_TAG tagScript, |
| 84 | OPENTYPE_TAG tagLangSys, |
| 85 | int *rcRangeChars, |
| 86 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
| 87 | int cRanges, |
| 88 | const WCHAR *pwcChars, |
| 89 | WORD *pwLogClust, |
| 90 | SCRIPT_CHARPROP *pCharProps, |
| 91 | int cChars, |
| 92 | const WORD *pwGlyphs, |
| 93 | const SCRIPT_GLYPHPROP *pGlyphProps, |
| 94 | int cGlyphs, |
| 95 | int *piAdvance, |
| 96 | GOFFSET *pGoffset, |
| 97 | ABC *pABC |
| 98 | ); |
| 99 | |
| 100 | |
| 101 | /* Fallback implementations. */ |
| 102 | |
| 103 | static HRESULT WINAPI |
| 104 | hb_ScriptItemizeOpenType( |
| 105 | const WCHAR *pwcInChars, |
| 106 | int cInChars, |
| 107 | int cMaxItems, |
| 108 | const SCRIPT_CONTROL *psControl, |
| 109 | const SCRIPT_STATE *psState, |
| 110 | SCRIPT_ITEM *pItems, |
| 111 | OPENTYPE_TAG *pScriptTags, |
| 112 | int *pcItems |
| 113 | ) |
| 114 | { |
| 115 | { |
| 116 | return ScriptItemize (pwcInChars, |
| 117 | cInChars, |
| 118 | cMaxItems, |
| 119 | psControl, |
| 120 | psState, |
| 121 | pItems, |
| 122 | pcItems); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static HRESULT WINAPI |
| 127 | hb_ScriptShapeOpenType( |
| 128 | HDC hdc, |
| 129 | SCRIPT_CACHE *psc, |
| 130 | SCRIPT_ANALYSIS *psa, |
| 131 | OPENTYPE_TAG tagScript, |
| 132 | OPENTYPE_TAG tagLangSys, |
| 133 | int *rcRangeChars, |
| 134 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
| 135 | int cRanges, |
| 136 | const WCHAR *pwcChars, |
| 137 | int cChars, |
| 138 | int cMaxGlyphs, |
| 139 | WORD *pwLogClust, |
| 140 | SCRIPT_CHARPROP *pCharProps, |
| 141 | WORD *pwOutGlyphs, |
| 142 | SCRIPT_GLYPHPROP *pOutGlyphProps, |
| 143 | int *pcGlyphs |
| 144 | ) |
| 145 | { |
| 146 | SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps; |
| 147 | return ScriptShape (hdc, |
| 148 | psc, |
| 149 | pwcChars, |
| 150 | cChars, |
| 151 | cMaxGlyphs, |
| 152 | psa, |
| 153 | pwOutGlyphs, |
| 154 | pwLogClust, |
| 155 | psva, |
| 156 | pcGlyphs); |
| 157 | } |
| 158 | |
| 159 | static HRESULT WINAPI |
| 160 | hb_ScriptPlaceOpenType( |
| 161 | HDC hdc, |
| 162 | SCRIPT_CACHE *psc, |
| 163 | SCRIPT_ANALYSIS *psa, |
| 164 | OPENTYPE_TAG tagScript, |
| 165 | OPENTYPE_TAG tagLangSys, |
| 166 | int *rcRangeChars, |
| 167 | TEXTRANGE_PROPERTIES **rpRangeProperties, |
| 168 | int cRanges, |
| 169 | const WCHAR *pwcChars, |
| 170 | WORD *pwLogClust, |
| 171 | SCRIPT_CHARPROP *pCharProps, |
| 172 | int cChars, |
| 173 | const WORD *pwGlyphs, |
| 174 | const SCRIPT_GLYPHPROP *pGlyphProps, |
| 175 | int cGlyphs, |
| 176 | int *piAdvance, |
| 177 | GOFFSET *pGoffset, |
| 178 | ABC *pABC |
| 179 | ) |
| 180 | { |
| 181 | SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps; |
| 182 | return ScriptPlace (hdc, |
| 183 | psc, |
| 184 | pwGlyphs, |
| 185 | cGlyphs, |
| 186 | psva, |
| 187 | psa, |
| 188 | piAdvance, |
| 189 | pGoffset, |
| 190 | pABC); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | struct hb_uniscribe_shaper_funcs_t { |
| 195 | SIOT ScriptItemizeOpenType; |
| 196 | SSOT ScriptShapeOpenType; |
| 197 | SPOT ScriptPlaceOpenType; |
| 198 | |
| 199 | inline void init (void) |
| 200 | { |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 201 | HMODULE hinstLib; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 202 | this->ScriptItemizeOpenType = NULL; |
| 203 | this->ScriptShapeOpenType = NULL; |
| 204 | this->ScriptPlaceOpenType = NULL; |
| 205 | |
Chun-wei Fan | 93a04b8 | 2013-06-03 17:49:37 +0800 | [diff] [blame] | 206 | hinstLib = GetModuleHandle("usp10.dll"); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 207 | if (hinstLib) |
| 208 | { |
| 209 | this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType"); |
| 210 | this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType"); |
| 211 | this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType"); |
| 212 | } |
| 213 | if (!this->ScriptItemizeOpenType || |
| 214 | !this->ScriptShapeOpenType || |
| 215 | !this->ScriptPlaceOpenType) |
| 216 | { |
| 217 | DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back."); |
| 218 | this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType; |
| 219 | this->ScriptShapeOpenType = hb_ScriptShapeOpenType; |
| 220 | this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType; |
| 221 | } |
| 222 | } |
| 223 | }; |
| 224 | static hb_uniscribe_shaper_funcs_t *uniscribe_funcs; |
| 225 | |
| 226 | static inline void |
| 227 | free_uniscribe_funcs (void) |
| 228 | { |
| 229 | free (uniscribe_funcs); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | static hb_uniscribe_shaper_funcs_t * |
| 234 | hb_uniscribe_shaper_get_funcs (void) |
| 235 | { |
| 236 | retry: |
| 237 | hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs); |
| 238 | |
| 239 | if (unlikely (!funcs)) |
| 240 | { |
| 241 | funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t)); |
| 242 | if (unlikely (!funcs)) |
| 243 | return NULL; |
| 244 | |
| 245 | funcs->init (); |
| 246 | |
| 247 | if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) { |
| 248 | free (funcs); |
| 249 | goto retry; |
| 250 | } |
| 251 | |
| 252 | #ifdef HAVE_ATEXIT |
| 253 | atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */ |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | return funcs; |
| 258 | } |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 259 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 260 | |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 261 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face) |
| 262 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font) |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 263 | |
| 264 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 265 | /* |
| 266 | * shaper face data |
| 267 | */ |
Behdad Esfahbod | 71388b3 | 2011-08-24 02:09:04 +0200 | [diff] [blame] | 268 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 269 | struct hb_uniscribe_shaper_face_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 270 | HANDLE fh; |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 271 | hb_uniscribe_shaper_funcs_t *funcs; |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 272 | wchar_t face_name[LF_FACESIZE]; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 273 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 274 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 275 | /* Destroys blob. */ |
| 276 | static hb_blob_t * |
| 277 | _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 278 | { |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 279 | unsigned int length, new_length = 0; |
| 280 | const char *orig_sfnt_data = hb_blob_get_data (blob, &length); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 281 | /* We'll create a private name for the font from a UUID using a simple, |
| 282 | * somewhat base64-like encoding scheme */ |
| 283 | const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"; |
| 284 | UUID id; |
| 285 | UuidCreate ((UUID*) &id); |
| 286 | unsigned int name_str_len = 0; |
| 287 | new_name[name_str_len++] = 'F'; |
| 288 | new_name[name_str_len++] = '_'; |
| 289 | unsigned char *p = (unsigned char *) &id; |
| 290 | for (unsigned int i = 0; i < 16; i += 2) |
| 291 | { |
| 292 | /* Spread the 16 bits from two bytes of the UUID across three chars of face_name, |
| 293 | * using the bits in groups of 5,5,6 to select chars from enc. |
| 294 | * This will generate 24 characters; with the 'F_' prefix we already provided, |
| 295 | * the name will be 26 chars (plus the NUL terminator), so will always fit within |
| 296 | * face_name (LF_FACESIZE = 32). */ |
| 297 | new_name[name_str_len++] = enc[p[i] >> 3]; |
| 298 | new_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f]; |
| 299 | new_name[name_str_len++] = enc[p[i + 1] & 0x3f]; |
| 300 | } |
| 301 | new_name[name_str_len] = 0; |
| 302 | |
| 303 | /* Create a copy of the font data, with the 'name' table replaced by a table |
| 304 | * that names the font with our private F_* name created above. |
| 305 | * For simplicity, we just append a new 'name' table and update the sfnt directory; |
| 306 | * the original table is left in place, but unused. */ |
| 307 | |
| 308 | /* The new table will contain just 5 name IDs: family, style, unique, full, PS. |
| 309 | * All of them point to the same name data with our unique F_* name. */ |
| 310 | |
| 311 | static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 }; |
| 312 | |
| 313 | const unsigned int name_header_size = 3 * 2; /* NameTableHeader: USHORT x 3 */ |
| 314 | const unsigned int name_record_size = 6 * 2; /* NameRecord: USHORT x 6 */ |
| 315 | const unsigned int name_count = sizeof (name_IDs) / sizeof (name_IDs[0]); |
| 316 | unsigned int name_table_length = name_header_size + |
| 317 | name_count * name_record_size + |
| 318 | name_str_len * 2; /* for name data in UTF16BE form */ |
| 319 | |
| 320 | unsigned int name_table_offset = (length + 3) & ~3; |
| 321 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 322 | new_length = name_table_offset + (name_table_length + 3) & ~3; |
| 323 | char *new_sfnt_data = (char *) calloc (1, new_length); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 324 | if (!new_sfnt_data) |
| 325 | return NULL; |
| 326 | |
| 327 | memcpy(new_sfnt_data, orig_sfnt_data, length); |
| 328 | |
| 329 | unsigned char *name_table_data = (unsigned char *) (new_sfnt_data + name_table_offset); |
| 330 | |
| 331 | p = name_table_data; |
| 332 | *p++ = 0x00; *p++ = 0x00; /* format = 0 */ |
| 333 | *p++ = 0x00; *p++ = name_count; /* number of name records */ |
| 334 | *p++ = 0x00; *p++ = name_header_size + name_count * name_record_size; /* offset to string data */ |
| 335 | |
| 336 | for (unsigned int i = 0; i < name_count; i++) { |
| 337 | *p++ = 0x00; *p++ = 0x03; /* platformID */ |
| 338 | *p++ = 0x00; *p++ = 0x01; /* encodingID */ |
| 339 | *p++ = 0x04; *p++ = 0x09; /* languageID = 0x0409 */ |
| 340 | *p++ = 0x00; *p++ = name_IDs[i]; /* nameID */ |
| 341 | *p++ = 0x00; *p++ = name_str_len * 2; /* string length (bytes) */ |
| 342 | *p++ = 0x00; *p++ = 0x00; /* string offset */ |
| 343 | } |
| 344 | |
| 345 | for (unsigned int i = 0; i < name_str_len; i++) { |
| 346 | /* copy string data from face_name, converting wchar_t to UTF16BE */ |
| 347 | *p++ = new_name[i] >> 8; |
| 348 | *p++ = new_name[i] & 0xff; |
| 349 | } |
| 350 | |
| 351 | /* calculate new name table checksum */ |
| 352 | uint32_t checksum = 0; |
| 353 | while (name_table_data < p) { |
| 354 | checksum += (name_table_data[0] << 24) + |
| 355 | (name_table_data[1] << 16) + |
| 356 | (name_table_data[2] << 8) + |
| 357 | name_table_data[3]; |
| 358 | name_table_data += 4; |
| 359 | } |
| 360 | |
| 361 | /* adjust name table entry to point to new name table */ |
| 362 | OT::OpenTypeFontFace *face = (OT::OpenTypeFontFace *) (new_sfnt_data); |
| 363 | unsigned int index; |
| 364 | if (face->find_table_index (HB_OT_TAG_name, &index)) |
| 365 | { |
| 366 | const OT::TableRecord& record = face->get_table (index); |
| 367 | OT::TableRecord *rp = const_cast<OT::TableRecord *> (&record); |
| 368 | rp->checkSum.set(checksum); |
| 369 | rp->offset.set(name_table_offset); |
| 370 | rp->length.set(name_table_length); |
| 371 | } |
| 372 | |
| 373 | /* The checkSumAdjustment field in the 'head' table is now wrong, |
| 374 | but AFAIK that doesn't actually cause any problems so I haven't |
| 375 | bothered to fix it. */ |
| 376 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 377 | hb_blob_destroy (blob); |
| 378 | return hb_blob_create (new_sfnt_data, new_length, HB_MEMORY_MODE_WRITABLE, NULL, free); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 379 | } |
| 380 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 381 | hb_uniscribe_shaper_face_data_t * |
| 382 | _hb_uniscribe_shaper_face_data_create (hb_face_t *face) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 383 | { |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 384 | hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t)); |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 385 | if (unlikely (!data)) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 386 | return NULL; |
Behdad Esfahbod | a3bd8a0 | 2011-08-24 03:22:49 +0200 | [diff] [blame] | 387 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 388 | data->funcs = hb_uniscribe_shaper_get_funcs (); |
| 389 | if (unlikely (!data->funcs)) |
| 390 | { |
| 391 | free (data); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 395 | hb_blob_t *blob = hb_face_reference_blob (face); |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 396 | if (unlikely (!hb_blob_get_length (blob))) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 397 | DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
| 398 | |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 399 | blob = _hb_rename_font (blob, data->face_name); |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 400 | |
| 401 | DWORD num_fonts_installed; |
Behdad Esfahbod | 73f947e | 2013-07-21 16:16:35 -0400 | [diff] [blame^] | 402 | data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL), |
| 403 | hb_blob_get_length (blob), |
| 404 | 0, &num_fonts_installed); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 405 | if (unlikely (!data->fh)) { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 406 | DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 407 | free (data); |
| 408 | return NULL; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | return data; |
| 412 | } |
| 413 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 414 | void |
| 415 | _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data) |
| 416 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 417 | RemoveFontMemResourceEx (data->fh); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 418 | free (data); |
| 419 | } |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 420 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * shaper font data |
| 424 | */ |
| 425 | |
| 426 | struct hb_uniscribe_shaper_font_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 427 | HDC hdc; |
Behdad Esfahbod | d666035 | 2011-08-10 22:08:36 +0200 | [diff] [blame] | 428 | LOGFONTW log_font; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 429 | HFONT hfont; |
| 430 | SCRIPT_CACHE script_cache; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 431 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 432 | |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 433 | static bool |
| 434 | populate_log_font (LOGFONTW *lf, |
| 435 | hb_font_t *font) |
| 436 | { |
| 437 | memset (lf, 0, sizeof (*lf)); |
| 438 | lf->lfHeight = -font->y_scale; |
| 439 | lf->lfCharSet = DEFAULT_CHARSET; |
| 440 | |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 441 | hb_face_t *face = font->face; |
| 442 | hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 443 | |
Behdad Esfahbod | 8ac2e88 | 2013-07-21 16:06:49 -0400 | [diff] [blame] | 444 | memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName)); |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 445 | |
| 446 | return true; |
| 447 | } |
| 448 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 449 | hb_uniscribe_shaper_font_data_t * |
| 450 | _hb_uniscribe_shaper_font_data_create (hb_font_t *font) |
| 451 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 452 | if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL; |
| 453 | |
Behdad Esfahbod | e82061e | 2012-07-27 02:29:32 -0400 | [diff] [blame] | 454 | hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t)); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 455 | if (unlikely (!data)) |
| 456 | return NULL; |
| 457 | |
| 458 | data->hdc = GetDC (NULL); |
| 459 | |
| 460 | if (unlikely (!populate_log_font (&data->log_font, font))) { |
| 461 | DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
| 462 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | data->hfont = CreateFontIndirectW (&data->log_font); |
| 467 | if (unlikely (!data->hfont)) { |
| 468 | DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); |
| 469 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | if (!SelectObject (data->hdc, data->hfont)) { |
| 474 | DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); |
| 475 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | return data; |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 484 | { |
| 485 | if (data->hdc) |
| 486 | ReleaseDC (NULL, data->hdc); |
| 487 | if (data->hfont) |
| 488 | DeleteObject (data->hfont); |
| 489 | if (data->script_cache) |
| 490 | ScriptFreeCache (&data->script_cache); |
| 491 | free (data); |
| 492 | } |
| 493 | |
Behdad Esfahbod | 0ae6dbf | 2012-12-09 18:37:38 -0500 | [diff] [blame] | 494 | LOGFONTW * |
| 495 | hb_uniscribe_font_get_logfontw (hb_font_t *font) |
| 496 | { |
| 497 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
| 498 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
| 499 | return &font_data->log_font; |
| 500 | } |
| 501 | |
| 502 | HFONT |
| 503 | hb_uniscribe_font_get_hfont (hb_font_t *font) |
| 504 | { |
| 505 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
| 506 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
| 507 | return font_data->hfont; |
| 508 | } |
| 509 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * shaper shape_plan data |
| 513 | */ |
| 514 | |
| 515 | struct hb_uniscribe_shaper_shape_plan_data_t {}; |
| 516 | |
| 517 | hb_uniscribe_shaper_shape_plan_data_t * |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame] | 518 | _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, |
| 519 | const hb_feature_t *user_features HB_UNUSED, |
| 520 | unsigned int num_user_features HB_UNUSED) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 521 | { |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 522 | return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; |
| 523 | } |
| 524 | |
| 525 | void |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame] | 526 | _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 527 | { |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* |
| 532 | * shaper |
| 533 | */ |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 534 | |
| 535 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 536 | hb_bool_t |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 537 | _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, |
| 538 | hb_font_t *font, |
Behdad Esfahbod | 6bd9b47 | 2012-04-12 14:53:53 -0400 | [diff] [blame] | 539 | hb_buffer_t *buffer, |
| 540 | const hb_feature_t *features, |
| 541 | unsigned int num_features) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 542 | { |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 543 | hb_face_t *face = font->face; |
| 544 | hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); |
| 545 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 546 | hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs; |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 547 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 548 | #define FAIL(...) \ |
| 549 | HB_STMT_START { \ |
| 550 | DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 551 | return false; \ |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 552 | } HB_STMT_END; |
| 553 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 554 | HRESULT hr; |
| 555 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 556 | retry: |
| 557 | |
| 558 | unsigned int scratch_size; |
| 559 | char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); |
| 560 | |
| 561 | /* Allocate char buffers; they all fit */ |
| 562 | |
| 563 | #define ALLOCATE_ARRAY(Type, name, len) \ |
| 564 | Type *name = (Type *) scratch; \ |
Behdad Esfahbod | 91e721e | 2012-07-25 19:20:34 -0400 | [diff] [blame] | 565 | scratch += (len) * sizeof ((name)[0]); \ |
| 566 | scratch_size -= (len) * sizeof ((name)[0]); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 567 | |
| 568 | #define utf16_index() var1.u32 |
| 569 | |
| 570 | WCHAR *pchars = (WCHAR *) scratch; |
| 571 | unsigned int chars_len = 0; |
| 572 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 573 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 574 | buffer->info[i].utf16_index() = chars_len; |
| 575 | if (likely (c < 0x10000)) |
| 576 | pchars[chars_len++] = c; |
| 577 | else if (unlikely (c >= 0x110000)) |
| 578 | pchars[chars_len++] = 0xFFFD; |
| 579 | else { |
| 580 | pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); |
| 581 | pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | ALLOCATE_ARRAY (WCHAR, wchars, chars_len); |
| 586 | ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
| 587 | ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
| 588 | |
| 589 | /* On Windows, we don't care about alignment...*/ |
| 590 | unsigned int glyphs_size = scratch_size / (sizeof (WORD) + |
| 591 | sizeof (SCRIPT_GLYPHPROP) + |
| 592 | sizeof (int) + |
| 593 | sizeof (GOFFSET) + |
| 594 | sizeof (uint32_t)); |
| 595 | |
| 596 | ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); |
| 597 | ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); |
| 598 | ALLOCATE_ARRAY (int, advances, glyphs_size); |
| 599 | ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); |
| 600 | ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); |
| 601 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 602 | /* Note: |
| 603 | * We can't touch the contents of glyph_props. Our fallback |
| 604 | * implementations of Shape and Place functions use that buffer |
| 605 | * by casting it to a different type. It works because they |
| 606 | * both agree about it, but if we want to access it here we |
| 607 | * need address that issue first. |
| 608 | */ |
| 609 | |
Behdad Esfahbod | 91e721e | 2012-07-25 19:20:34 -0400 | [diff] [blame] | 610 | #undef ALLOCATE_ARRAY |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 611 | |
Behdad Esfahbod | 8729691 | 2012-06-08 14:18:30 -0400 | [diff] [blame] | 612 | #define MAX_ITEMS 256 |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 613 | |
| 614 | SCRIPT_ITEM items[MAX_ITEMS + 1]; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 615 | SCRIPT_CONTROL bidi_control = {0}; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 616 | SCRIPT_STATE bidi_state = {0}; |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 617 | ULONG script_tags[MAX_ITEMS]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 618 | int item_count; |
| 619 | |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 620 | /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 621 | //bidi_control.fMergeNeutralItems = true; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 622 | *(uint32_t*)&bidi_control |= 1<<24; |
| 623 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 624 | bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
Behdad Esfahbod | 29eac8f | 2012-06-08 09:26:17 -0400 | [diff] [blame] | 625 | bidi_state.fOverrideDirection = 1; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 626 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 627 | hr = funcs->ScriptItemizeOpenType (wchars, |
| 628 | chars_len, |
| 629 | MAX_ITEMS, |
| 630 | &bidi_control, |
| 631 | &bidi_state, |
| 632 | items, |
| 633 | script_tags, |
| 634 | &item_count); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 635 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 636 | FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 637 | |
| 638 | #undef MAX_ITEMS |
| 639 | |
| 640 | int *range_char_counts = NULL; |
| 641 | TEXTRANGE_PROPERTIES **range_properties = NULL; |
| 642 | int range_count = 0; |
| 643 | if (num_features) { |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 644 | /* TODO setup ranges */ |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Behdad Esfahbod | fcd6f53 | 2012-06-08 09:59:43 -0400 | [diff] [blame] | 647 | OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language)); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 648 | |
| 649 | unsigned int glyphs_offset = 0; |
| 650 | unsigned int glyphs_len; |
Behdad Esfahbod | b069c3c | 2012-06-08 10:10:29 -0400 | [diff] [blame] | 651 | bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
| 652 | for (unsigned int j = 0; j < item_count; j++) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 653 | { |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 654 | unsigned int i = backward ? item_count - 1 - j : j; |
| 655 | unsigned int chars_offset = items[i].iCharPos; |
| 656 | unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
Behdad Esfahbod | 1c1233e | 2012-06-08 09:20:53 -0400 | [diff] [blame] | 657 | |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 658 | retry_shape: |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 659 | hr = funcs->ScriptShapeOpenType (font_data->hdc, |
| 660 | &font_data->script_cache, |
| 661 | &items[i].a, |
| 662 | script_tags[i], |
| 663 | language_tag, |
| 664 | range_char_counts, |
| 665 | range_properties, |
| 666 | range_count, |
| 667 | wchars + chars_offset, |
| 668 | item_chars_len, |
| 669 | glyphs_size - glyphs_offset, |
| 670 | /* out */ |
| 671 | log_clusters + chars_offset, |
| 672 | char_props + chars_offset, |
| 673 | glyphs + glyphs_offset, |
| 674 | glyph_props + glyphs_offset, |
| 675 | (int *) &glyphs_len); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 676 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 677 | if (unlikely (items[i].a.fNoGlyphIndex)) |
| 678 | FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
| 679 | if (unlikely (hr == E_OUTOFMEMORY)) |
| 680 | { |
| 681 | buffer->ensure (buffer->allocated * 2); |
| 682 | if (buffer->in_error) |
| 683 | FAIL ("Buffer resize failed"); |
| 684 | goto retry; |
| 685 | } |
| 686 | if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 687 | { |
| 688 | if (items[i].a.eScript == SCRIPT_UNDEFINED) |
| 689 | FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); |
| 690 | items[i].a.eScript = SCRIPT_UNDEFINED; |
| 691 | goto retry_shape; |
| 692 | } |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 693 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 694 | { |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 695 | FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++) |
| 699 | log_clusters[j] += glyphs_offset; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 700 | |
Behdad Esfahbod | 2a17f95 | 2013-05-28 13:10:51 -0400 | [diff] [blame] | 701 | hr = funcs->ScriptPlaceOpenType (font_data->hdc, |
| 702 | &font_data->script_cache, |
| 703 | &items[i].a, |
| 704 | script_tags[i], |
| 705 | language_tag, |
| 706 | range_char_counts, |
| 707 | range_properties, |
| 708 | range_count, |
| 709 | wchars + chars_offset, |
| 710 | log_clusters + chars_offset, |
| 711 | char_props + chars_offset, |
| 712 | item_chars_len, |
| 713 | glyphs + glyphs_offset, |
| 714 | glyph_props + glyphs_offset, |
| 715 | glyphs_len, |
| 716 | /* out */ |
| 717 | advances + glyphs_offset, |
| 718 | offsets + glyphs_offset, |
| 719 | NULL); |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 720 | if (unlikely (FAILED (hr))) |
| 721 | FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 722 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 723 | glyphs_offset += glyphs_len; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 724 | } |
| 725 | glyphs_len = glyphs_offset; |
| 726 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 727 | /* Ok, we've got everything we need, now compose output buffer, |
| 728 | * very, *very*, carefully! */ |
| 729 | |
| 730 | /* Calculate visual-clusters. That's what we ship. */ |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 731 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 732 | vis_clusters[i] = -1; |
Behdad Esfahbod | 577326b | 2011-08-07 01:04:40 -0400 | [diff] [blame] | 733 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 734 | uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
| 735 | *p = MIN (*p, buffer->info[i].cluster); |
| 736 | } |
Behdad Esfahbod | b069c3c | 2012-06-08 10:10:29 -0400 | [diff] [blame] | 737 | if (!backward) { |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 738 | for (unsigned int i = 1; i < glyphs_len; i++) |
Behdad Esfahbod | 8e7beba | 2012-06-08 10:22:06 -0400 | [diff] [blame] | 739 | if (vis_clusters[i] == -1) |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 740 | vis_clusters[i] = vis_clusters[i - 1]; |
| 741 | } else { |
| 742 | for (int i = glyphs_len - 2; i >= 0; i--) |
Behdad Esfahbod | 8e7beba | 2012-06-08 10:22:06 -0400 | [diff] [blame] | 743 | if (vis_clusters[i] == -1) |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 744 | vis_clusters[i] = vis_clusters[i + 1]; |
| 745 | } |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 746 | |
| 747 | #undef utf16_index |
| 748 | |
| 749 | buffer->ensure (glyphs_len); |
| 750 | if (buffer->in_error) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 751 | FAIL ("Buffer in error"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 752 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 753 | #undef FAIL |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 754 | |
| 755 | /* Set glyph infos */ |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 756 | buffer->len = 0; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 757 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 758 | { |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 759 | hb_glyph_info_t *info = &buffer->info[buffer->len++]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 760 | |
| 761 | info->codepoint = glyphs[i]; |
| 762 | info->cluster = vis_clusters[i]; |
| 763 | |
| 764 | /* The rest is crap. Let's store position info there for now. */ |
| 765 | info->mask = advances[i]; |
| 766 | info->var1.u32 = offsets[i].du; |
| 767 | info->var2.u32 = offsets[i].dv; |
| 768 | } |
| 769 | |
| 770 | /* Set glyph positions */ |
| 771 | buffer->clear_positions (); |
| 772 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 773 | { |
| 774 | hb_glyph_info_t *info = &buffer->info[i]; |
| 775 | hb_glyph_position_t *pos = &buffer->pos[i]; |
| 776 | |
| 777 | /* TODO vertical */ |
| 778 | pos->x_advance = info->mask; |
| 779 | pos->x_offset = info->var1.u32; |
| 780 | pos->y_offset = info->var2.u32; |
| 781 | } |
| 782 | |
| 783 | /* Wow, done! */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 784 | return true; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |