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> |
| 35 | |
| 36 | typedef ULONG WIN_ULONG; |
| 37 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 38 | #include "hb-uniscribe.h" |
| 39 | |
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 | |
| 49 | /* |
| 50 | DWORD GetFontData( |
| 51 | __in HDC hdc, |
| 52 | __in DWORD dwTable, |
| 53 | __in DWORD dwOffset, |
| 54 | __out LPVOID lpvBuffer, |
| 55 | __in DWORD cbData |
| 56 | ); |
| 57 | */ |
| 58 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 59 | |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 60 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face) |
| 61 | HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font) |
Behdad Esfahbod | cfe9882 | 2012-07-27 03:06:30 -0400 | [diff] [blame] | 62 | |
| 63 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 64 | /* |
| 65 | * shaper face data |
| 66 | */ |
Behdad Esfahbod | 71388b3 | 2011-08-24 02:09:04 +0200 | [diff] [blame] | 67 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 68 | struct hb_uniscribe_shaper_face_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 69 | HANDLE fh; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 70 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 71 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 72 | hb_uniscribe_shaper_face_data_t * |
| 73 | _hb_uniscribe_shaper_face_data_create (hb_face_t *face) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 74 | { |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 75 | 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] | 76 | if (unlikely (!data)) |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 77 | return NULL; |
Behdad Esfahbod | a3bd8a0 | 2011-08-24 03:22:49 +0200 | [diff] [blame] | 78 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 79 | hb_blob_t *blob = hb_face_reference_blob (face); |
| 80 | unsigned int blob_length; |
| 81 | const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 82 | if (unlikely (!blob_length)) |
| 83 | DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
| 84 | |
| 85 | DWORD num_fonts_installed; |
| 86 | data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_fonts_installed); |
| 87 | hb_blob_destroy (blob); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 88 | if (unlikely (!data->fh)) { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 89 | DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 90 | free (data); |
| 91 | return NULL; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return data; |
| 95 | } |
| 96 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 97 | void |
| 98 | _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data) |
| 99 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 100 | RemoveFontMemResourceEx (data->fh); |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 101 | free (data); |
| 102 | } |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 103 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * shaper font data |
| 107 | */ |
| 108 | |
| 109 | struct hb_uniscribe_shaper_font_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 110 | HDC hdc; |
Behdad Esfahbod | d666035 | 2011-08-10 22:08:36 +0200 | [diff] [blame] | 111 | LOGFONTW log_font; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 112 | HFONT hfont; |
| 113 | SCRIPT_CACHE script_cache; |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 114 | }; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 115 | |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 116 | static bool |
| 117 | populate_log_font (LOGFONTW *lf, |
| 118 | hb_font_t *font) |
| 119 | { |
| 120 | memset (lf, 0, sizeof (*lf)); |
| 121 | lf->lfHeight = -font->y_scale; |
| 122 | lf->lfCharSet = DEFAULT_CHARSET; |
| 123 | |
| 124 | hb_blob_t *blob = Sanitizer<name>::sanitize (hb_face_reference_table (font->face, HB_TAG ('n','a','m','e'))); |
| 125 | const name *name_table = Sanitizer<name>::lock_instance (blob); |
| 126 | unsigned int len = name_table->get_name (3, 1, 0x409, 4, |
| 127 | lf->lfFaceName, |
| 128 | sizeof (lf->lfFaceName[0]) * LF_FACESIZE) |
| 129 | / sizeof (lf->lfFaceName[0]); |
| 130 | hb_blob_destroy (blob); |
| 131 | |
| 132 | if (unlikely (!len)) { |
| 133 | DEBUG_MSG (UNISCRIBE, NULL, "Didn't find English name table entry"); |
| 134 | return false; |
| 135 | } |
| 136 | if (unlikely (len >= LF_FACESIZE)) { |
| 137 | DEBUG_MSG (UNISCRIBE, NULL, "Font name too long"); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | for (unsigned int i = 0; i < len; i++) |
| 142 | lf->lfFaceName[i] = hb_be_uint16 (lf->lfFaceName[i]); |
| 143 | lf->lfFaceName[len] = 0; |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 148 | hb_uniscribe_shaper_font_data_t * |
| 149 | _hb_uniscribe_shaper_font_data_create (hb_font_t *font) |
| 150 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 151 | if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL; |
| 152 | |
Behdad Esfahbod | e82061e | 2012-07-27 02:29:32 -0400 | [diff] [blame] | 153 | 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] | 154 | if (unlikely (!data)) |
| 155 | return NULL; |
| 156 | |
| 157 | data->hdc = GetDC (NULL); |
| 158 | |
| 159 | if (unlikely (!populate_log_font (&data->log_font, font))) { |
| 160 | DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
| 161 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | data->hfont = CreateFontIndirectW (&data->log_font); |
| 166 | if (unlikely (!data->hfont)) { |
| 167 | DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); |
| 168 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | if (!SelectObject (data->hdc, data->hfont)) { |
| 173 | DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); |
| 174 | _hb_uniscribe_shaper_font_data_destroy (data); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | return data; |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | _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] | 183 | { |
| 184 | if (data->hdc) |
| 185 | ReleaseDC (NULL, data->hdc); |
| 186 | if (data->hfont) |
| 187 | DeleteObject (data->hfont); |
| 188 | if (data->script_cache) |
| 189 | ScriptFreeCache (&data->script_cache); |
| 190 | free (data); |
| 191 | } |
| 192 | |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * shaper shape_plan data |
| 196 | */ |
| 197 | |
| 198 | struct hb_uniscribe_shaper_shape_plan_data_t {}; |
| 199 | |
| 200 | hb_uniscribe_shaper_shape_plan_data_t * |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame^] | 201 | _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, |
| 202 | const hb_feature_t *user_features HB_UNUSED, |
| 203 | unsigned int num_user_features HB_UNUSED) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 204 | { |
Behdad Esfahbod | 027857d | 2012-07-26 17:34:25 -0400 | [diff] [blame] | 205 | return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; |
| 206 | } |
| 207 | |
| 208 | void |
Behdad Esfahbod | 45c1383 | 2012-08-14 09:33:18 -0400 | [diff] [blame^] | 209 | _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] | 210 | { |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /* |
| 215 | * shaper |
| 216 | */ |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 217 | |
Behdad Esfahbod | d666035 | 2011-08-10 22:08:36 +0200 | [diff] [blame] | 218 | LOGFONTW * |
| 219 | hb_uniscribe_font_get_logfontw (hb_font_t *font) |
| 220 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 221 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
Behdad Esfahbod | a00ad60 | 2012-07-28 21:16:08 -0400 | [diff] [blame] | 222 | return NULL; |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 223 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
Behdad Esfahbod | d666035 | 2011-08-10 22:08:36 +0200 | [diff] [blame] | 224 | return &font_data->log_font; |
| 225 | } |
| 226 | |
| 227 | HFONT |
| 228 | hb_uniscribe_font_get_hfont (hb_font_t *font) |
| 229 | { |
Behdad Esfahbod | 713914d | 2012-07-30 17:54:38 -0400 | [diff] [blame] | 230 | if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 231 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
Behdad Esfahbod | d666035 | 2011-08-10 22:08:36 +0200 | [diff] [blame] | 232 | return font_data->hfont; |
| 233 | } |
| 234 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 235 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 236 | hb_bool_t |
Behdad Esfahbod | bd26b4d | 2012-07-26 22:05:39 -0400 | [diff] [blame] | 237 | _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, |
| 238 | hb_font_t *font, |
Behdad Esfahbod | 6bd9b47 | 2012-04-12 14:53:53 -0400 | [diff] [blame] | 239 | hb_buffer_t *buffer, |
| 240 | const hb_feature_t *features, |
| 241 | unsigned int num_features) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 242 | { |
Behdad Esfahbod | b6b7ba1 | 2012-07-27 01:26:11 -0400 | [diff] [blame] | 243 | hb_face_t *face = font->face; |
| 244 | hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); |
| 245 | hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 246 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 247 | #define FAIL(...) \ |
| 248 | HB_STMT_START { \ |
| 249 | DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 250 | return false; \ |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 251 | } HB_STMT_END; |
| 252 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 253 | HRESULT hr; |
| 254 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 255 | retry: |
| 256 | |
| 257 | unsigned int scratch_size; |
| 258 | char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); |
| 259 | |
| 260 | /* Allocate char buffers; they all fit */ |
| 261 | |
| 262 | #define ALLOCATE_ARRAY(Type, name, len) \ |
| 263 | Type *name = (Type *) scratch; \ |
Behdad Esfahbod | 91e721e | 2012-07-25 19:20:34 -0400 | [diff] [blame] | 264 | scratch += (len) * sizeof ((name)[0]); \ |
| 265 | scratch_size -= (len) * sizeof ((name)[0]); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 266 | |
| 267 | #define utf16_index() var1.u32 |
| 268 | |
| 269 | WCHAR *pchars = (WCHAR *) scratch; |
| 270 | unsigned int chars_len = 0; |
| 271 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 272 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 273 | buffer->info[i].utf16_index() = chars_len; |
| 274 | if (likely (c < 0x10000)) |
| 275 | pchars[chars_len++] = c; |
| 276 | else if (unlikely (c >= 0x110000)) |
| 277 | pchars[chars_len++] = 0xFFFD; |
| 278 | else { |
| 279 | pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); |
| 280 | pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | ALLOCATE_ARRAY (WCHAR, wchars, chars_len); |
| 285 | ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
| 286 | ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
| 287 | |
| 288 | /* On Windows, we don't care about alignment...*/ |
| 289 | unsigned int glyphs_size = scratch_size / (sizeof (WORD) + |
| 290 | sizeof (SCRIPT_GLYPHPROP) + |
| 291 | sizeof (int) + |
| 292 | sizeof (GOFFSET) + |
| 293 | sizeof (uint32_t)); |
| 294 | |
| 295 | ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); |
| 296 | ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); |
| 297 | ALLOCATE_ARRAY (int, advances, glyphs_size); |
| 298 | ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); |
| 299 | ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); |
| 300 | |
Behdad Esfahbod | 91e721e | 2012-07-25 19:20:34 -0400 | [diff] [blame] | 301 | #undef ALLOCATE_ARRAY |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 302 | |
Behdad Esfahbod | 8729691 | 2012-06-08 14:18:30 -0400 | [diff] [blame] | 303 | #define MAX_ITEMS 256 |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 304 | |
| 305 | SCRIPT_ITEM items[MAX_ITEMS + 1]; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 306 | SCRIPT_CONTROL bidi_control = {0}; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 307 | SCRIPT_STATE bidi_state = {0}; |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 308 | WIN_ULONG script_tags[MAX_ITEMS]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 309 | int item_count; |
| 310 | |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 311 | /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 312 | //bidi_control.fMergeNeutralItems = true; |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 313 | *(uint32_t*)&bidi_control |= 1<<24; |
| 314 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 315 | bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
Behdad Esfahbod | 29eac8f | 2012-06-08 09:26:17 -0400 | [diff] [blame] | 316 | bidi_state.fOverrideDirection = 1; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 317 | |
| 318 | hr = ScriptItemizeOpenType (wchars, |
| 319 | chars_len, |
| 320 | MAX_ITEMS, |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 321 | &bidi_control, |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 322 | &bidi_state, |
| 323 | items, |
| 324 | script_tags, |
| 325 | &item_count); |
| 326 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 327 | FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 328 | |
| 329 | #undef MAX_ITEMS |
| 330 | |
| 331 | int *range_char_counts = NULL; |
| 332 | TEXTRANGE_PROPERTIES **range_properties = NULL; |
| 333 | int range_count = 0; |
| 334 | if (num_features) { |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 335 | /* TODO setup ranges */ |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Behdad Esfahbod | fcd6f53 | 2012-06-08 09:59:43 -0400 | [diff] [blame] | 338 | 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] | 339 | |
| 340 | unsigned int glyphs_offset = 0; |
| 341 | unsigned int glyphs_len; |
Behdad Esfahbod | b069c3c | 2012-06-08 10:10:29 -0400 | [diff] [blame] | 342 | bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
| 343 | for (unsigned int j = 0; j < item_count; j++) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 344 | { |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 345 | unsigned int i = backward ? item_count - 1 - j : j; |
| 346 | unsigned int chars_offset = items[i].iCharPos; |
| 347 | unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
Behdad Esfahbod | 1c1233e | 2012-06-08 09:20:53 -0400 | [diff] [blame] | 348 | |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 349 | retry_shape: |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 350 | hr = ScriptShapeOpenType (font_data->hdc, |
| 351 | &font_data->script_cache, |
| 352 | &items[i].a, |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 353 | script_tags[i], |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 354 | language_tag, |
| 355 | range_char_counts, |
| 356 | range_properties, |
| 357 | range_count, |
| 358 | wchars + chars_offset, |
| 359 | item_chars_len, |
| 360 | glyphs_size - glyphs_offset, |
| 361 | /* out */ |
| 362 | log_clusters + chars_offset, |
| 363 | char_props + chars_offset, |
| 364 | glyphs + glyphs_offset, |
| 365 | glyph_props + glyphs_offset, |
| 366 | (int *) &glyphs_len); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 367 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 368 | if (unlikely (items[i].a.fNoGlyphIndex)) |
| 369 | FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
| 370 | if (unlikely (hr == E_OUTOFMEMORY)) |
| 371 | { |
| 372 | buffer->ensure (buffer->allocated * 2); |
| 373 | if (buffer->in_error) |
| 374 | FAIL ("Buffer resize failed"); |
| 375 | goto retry; |
| 376 | } |
| 377 | if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 378 | { |
| 379 | if (items[i].a.eScript == SCRIPT_UNDEFINED) |
| 380 | FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); |
| 381 | items[i].a.eScript = SCRIPT_UNDEFINED; |
| 382 | goto retry_shape; |
| 383 | } |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 384 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 385 | { |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 386 | FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++) |
| 390 | log_clusters[j] += glyphs_offset; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 391 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 392 | hr = ScriptPlaceOpenType (font_data->hdc, |
| 393 | &font_data->script_cache, |
| 394 | &items[i].a, |
Behdad Esfahbod | e9c0f15 | 2012-07-20 17:05:46 -0400 | [diff] [blame] | 395 | script_tags[i], |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 396 | language_tag, |
| 397 | range_char_counts, |
| 398 | range_properties, |
| 399 | range_count, |
| 400 | wchars + chars_offset, |
| 401 | log_clusters + chars_offset, |
| 402 | char_props + chars_offset, |
| 403 | item_chars_len, |
| 404 | glyphs + glyphs_offset, |
| 405 | glyph_props + glyphs_offset, |
| 406 | glyphs_len, |
| 407 | /* out */ |
| 408 | advances + glyphs_offset, |
| 409 | offsets + glyphs_offset, |
| 410 | NULL); |
| 411 | if (unlikely (FAILED (hr))) |
| 412 | FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 413 | |
Behdad Esfahbod | 0dd86f9 | 2012-06-08 10:23:03 -0400 | [diff] [blame] | 414 | glyphs_offset += glyphs_len; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 415 | } |
| 416 | glyphs_len = glyphs_offset; |
| 417 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 418 | /* Ok, we've got everything we need, now compose output buffer, |
| 419 | * very, *very*, carefully! */ |
| 420 | |
| 421 | /* Calculate visual-clusters. That's what we ship. */ |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 422 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 423 | vis_clusters[i] = -1; |
Behdad Esfahbod | 577326b | 2011-08-07 01:04:40 -0400 | [diff] [blame] | 424 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 425 | uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
| 426 | *p = MIN (*p, buffer->info[i].cluster); |
| 427 | } |
Behdad Esfahbod | b069c3c | 2012-06-08 10:10:29 -0400 | [diff] [blame] | 428 | if (!backward) { |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 429 | for (unsigned int i = 1; i < glyphs_len; i++) |
Behdad Esfahbod | 8e7beba | 2012-06-08 10:22:06 -0400 | [diff] [blame] | 430 | if (vis_clusters[i] == -1) |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 431 | vis_clusters[i] = vis_clusters[i - 1]; |
| 432 | } else { |
| 433 | for (int i = glyphs_len - 2; i >= 0; i--) |
Behdad Esfahbod | 8e7beba | 2012-06-08 10:22:06 -0400 | [diff] [blame] | 434 | if (vis_clusters[i] == -1) |
Behdad Esfahbod | 5c29934 | 2011-09-19 14:53:26 -0400 | [diff] [blame] | 435 | vis_clusters[i] = vis_clusters[i + 1]; |
| 436 | } |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 437 | |
| 438 | #undef utf16_index |
| 439 | |
| 440 | buffer->ensure (glyphs_len); |
| 441 | if (buffer->in_error) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 442 | FAIL ("Buffer in error"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 443 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 444 | #undef FAIL |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 445 | |
| 446 | /* Set glyph infos */ |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 447 | buffer->len = 0; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 448 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 449 | { |
Behdad Esfahbod | d753ac7 | 2011-08-09 14:03:12 +0200 | [diff] [blame] | 450 | hb_glyph_info_t *info = &buffer->info[buffer->len++]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 451 | |
| 452 | info->codepoint = glyphs[i]; |
| 453 | info->cluster = vis_clusters[i]; |
| 454 | |
| 455 | /* The rest is crap. Let's store position info there for now. */ |
| 456 | info->mask = advances[i]; |
| 457 | info->var1.u32 = offsets[i].du; |
| 458 | info->var2.u32 = offsets[i].dv; |
| 459 | } |
| 460 | |
| 461 | /* Set glyph positions */ |
| 462 | buffer->clear_positions (); |
| 463 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 464 | { |
| 465 | hb_glyph_info_t *info = &buffer->info[i]; |
| 466 | hb_glyph_position_t *pos = &buffer->pos[i]; |
| 467 | |
| 468 | /* TODO vertical */ |
| 469 | pos->x_advance = info->mask; |
| 470 | pos->x_offset = info->var1.u32; |
| 471 | pos->y_offset = info->var2.u32; |
| 472 | } |
| 473 | |
| 474 | /* Wow, done! */ |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 475 | return true; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |