Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Google, Inc. |
| 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 | |
| 27 | #define _WIN32_WINNT 0x0500 |
| 28 | |
| 29 | #include "hb-private.hh" |
| 30 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 31 | #include <windows.h> |
| 32 | #include <usp10.h> |
| 33 | |
| 34 | typedef ULONG WIN_ULONG; |
| 35 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 36 | #include "hb-uniscribe.h" |
| 37 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 38 | #include "hb-ot-name-private.hh" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 39 | #include "hb-ot-tag.h" |
| 40 | |
| 41 | #include "hb-font-private.hh" |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 42 | #include "hb-buffer-private.hh" |
| 43 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 44 | |
| 45 | |
| 46 | #ifndef HB_DEBUG_UNISCRIBE |
| 47 | #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0) |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | DWORD GetFontData( |
| 53 | __in HDC hdc, |
| 54 | __in DWORD dwTable, |
| 55 | __in DWORD dwOffset, |
| 56 | __out LPVOID lpvBuffer, |
| 57 | __in DWORD cbData |
| 58 | ); |
| 59 | */ |
| 60 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 61 | static bool |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 62 | populate_log_font (LOGFONTW *lf, |
| 63 | HDC hdc, |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 64 | hb_font_t *font) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 65 | { |
| 66 | memset (lf, 0, sizeof (*lf)); |
| 67 | int dpi = GetDeviceCaps (hdc, LOGPIXELSY); |
Behdad Esfahbod | 3fd2b5b | 2011-08-06 22:59:54 -0400 | [diff] [blame] | 68 | lf->lfHeight = -font->y_scale; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 69 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 70 | hb_blob_t *blob = Sanitizer<name>::sanitize (hb_face_reference_table (font->face, HB_TAG ('n','a','m','e'))); |
| 71 | const name *name_table = Sanitizer<name>::lock_instance (blob); |
| 72 | unsigned int len = name_table->get_name (3, 1, 0x409, 4, |
| 73 | lf->lfFaceName, |
| 74 | sizeof (lf->lfFaceName[0]) * LF_FACESIZE) |
| 75 | / sizeof (lf->lfFaceName[0]); |
Behdad Esfahbod | 826e227 | 2011-08-07 03:53:42 -0400 | [diff] [blame] | 76 | hb_blob_destroy (blob); |
| 77 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 78 | if (unlikely (!len)) { |
| 79 | DEBUG_MSG (UNISCRIBE, NULL, "Didn't find English name table entry"); |
| 80 | return FALSE; |
| 81 | } |
| 82 | if (unlikely (len >= LF_FACESIZE)) { |
| 83 | DEBUG_MSG (UNISCRIBE, NULL, "Font name too long"); |
| 84 | return FALSE; |
| 85 | } |
Behdad Esfahbod | e9c71fa | 2011-08-07 00:00:27 -0400 | [diff] [blame] | 86 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 87 | for (unsigned int i = 0; i < len; i++) |
| 88 | lf->lfFaceName[i] = hb_be_uint16 (lf->lfFaceName[i]); |
| 89 | lf->lfFaceName[len] = 0; |
Behdad Esfahbod | e9c71fa | 2011-08-07 00:00:27 -0400 | [diff] [blame] | 90 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 91 | return TRUE; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 94 | |
Behdad Esfahbod | 255f176 | 2011-08-09 08:35:07 +0200 | [diff] [blame^] | 95 | static struct hb_uniscribe_face_data_t { |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 96 | HANDLE fh; |
| 97 | } _hb_uniscribe_face_data_nil = {0}; |
| 98 | |
| 99 | static hb_user_data_key_t uniscribe_face_data_key; |
| 100 | |
| 101 | static void |
| 102 | _hb_uniscribe_face_data_destroy (hb_uniscribe_face_data_t *data) |
| 103 | { |
| 104 | if (data->fh) |
| 105 | RemoveFontMemResourceEx (data->fh); |
| 106 | free (data); |
| 107 | } |
| 108 | |
| 109 | static hb_uniscribe_face_data_t * |
| 110 | _hb_uniscribe_face_get_data (hb_face_t *face) |
| 111 | { |
| 112 | hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key); |
| 113 | if (likely (data)) return data; |
| 114 | |
| 115 | data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t)); |
| 116 | if (unlikely (!data)) |
| 117 | return &_hb_uniscribe_face_data_nil; |
| 118 | |
| 119 | hb_blob_t *blob = hb_face_reference_blob (face); |
| 120 | unsigned int blob_length; |
| 121 | const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 122 | if (unlikely (!blob_length)) |
| 123 | DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
| 124 | |
| 125 | DWORD num_fonts_installed; |
| 126 | data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_fonts_installed); |
| 127 | hb_blob_destroy (blob); |
| 128 | if (unlikely (!data->fh)) |
| 129 | DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
| 130 | |
| 131 | if (unlikely (!hb_face_set_user_data (face, &uniscribe_face_data_key, data, |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 132 | (hb_destroy_func_t) _hb_uniscribe_face_data_destroy, |
| 133 | FALSE))) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 134 | { |
| 135 | _hb_uniscribe_face_data_destroy (data); |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 136 | data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key); |
| 137 | if (data) |
| 138 | return data; |
| 139 | else |
| 140 | return &_hb_uniscribe_face_data_nil; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return data; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static struct hb_uniscribe_font_data_t { |
| 148 | HDC hdc; |
| 149 | HFONT hfont; |
| 150 | SCRIPT_CACHE script_cache; |
| 151 | } _hb_uniscribe_font_data_nil = {NULL, NULL, NULL}; |
| 152 | |
| 153 | static hb_user_data_key_t uniscribe_font_data_key; |
| 154 | |
| 155 | static void |
| 156 | _hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data) |
| 157 | { |
| 158 | if (data->hdc) |
| 159 | ReleaseDC (NULL, data->hdc); |
| 160 | if (data->hfont) |
| 161 | DeleteObject (data->hfont); |
| 162 | if (data->script_cache) |
| 163 | ScriptFreeCache (&data->script_cache); |
| 164 | free (data); |
| 165 | } |
| 166 | |
| 167 | static hb_uniscribe_font_data_t * |
| 168 | _hb_uniscribe_font_get_data (hb_font_t *font) |
| 169 | { |
| 170 | hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key); |
| 171 | if (likely (data)) return data; |
| 172 | |
| 173 | data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t)); |
| 174 | if (unlikely (!data)) |
| 175 | return &_hb_uniscribe_font_data_nil; |
| 176 | |
| 177 | data->hdc = GetDC (NULL); |
| 178 | |
| 179 | LOGFONTW log_font; |
| 180 | if (unlikely (!populate_log_font (&log_font, data->hdc, font))) |
| 181 | DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
| 182 | else { |
| 183 | data->hfont = CreateFontIndirectW (&log_font); |
| 184 | if (unlikely (!data->hfont)) |
| 185 | DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); |
| 186 | if (!SelectObject (data->hdc, data->hfont)) |
| 187 | DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); |
| 188 | } |
| 189 | |
| 190 | if (unlikely (!hb_font_set_user_data (font, &uniscribe_font_data_key, data, |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 191 | (hb_destroy_func_t) _hb_uniscribe_font_data_destroy, |
| 192 | FALSE))) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 193 | { |
| 194 | _hb_uniscribe_font_data_destroy (data); |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 195 | data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key); |
| 196 | if (data) |
| 197 | return data; |
| 198 | else |
| 199 | return &_hb_uniscribe_font_data_nil; |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return data; |
| 203 | } |
| 204 | |
| 205 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 206 | hb_bool_t |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 207 | hb_uniscribe_shape (hb_font_t *font, |
| 208 | hb_buffer_t *buffer, |
| 209 | const hb_feature_t *features, |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 210 | unsigned int num_features, |
| 211 | const char *shaper_options) |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 212 | { |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 213 | buffer->guess_properties (); |
| 214 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 215 | #define FAIL(...) \ |
| 216 | HB_STMT_START { \ |
| 217 | DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ |
| 218 | return FALSE; \ |
| 219 | } HB_STMT_END; |
| 220 | |
| 221 | hb_uniscribe_face_data_t *face_data = _hb_uniscribe_face_get_data (font->face); |
Behdad Esfahbod | a9057eb | 2011-08-09 00:47:55 +0200 | [diff] [blame] | 222 | if (unlikely (!face_data->fh)) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 223 | FAIL ("Couldn't get face data"); |
| 224 | |
| 225 | hb_uniscribe_font_data_t *font_data = _hb_uniscribe_font_get_data (font); |
Behdad Esfahbod | a9057eb | 2011-08-09 00:47:55 +0200 | [diff] [blame] | 226 | if (unlikely (!font_data->hfont)) |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 227 | FAIL ("Couldn't get font font"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 228 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 229 | if (unlikely (!buffer->len)) |
| 230 | return TRUE; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 231 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 232 | HRESULT hr; |
| 233 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 234 | retry: |
| 235 | |
| 236 | unsigned int scratch_size; |
| 237 | char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); |
| 238 | |
| 239 | /* Allocate char buffers; they all fit */ |
| 240 | |
| 241 | #define ALLOCATE_ARRAY(Type, name, len) \ |
| 242 | Type *name = (Type *) scratch; \ |
| 243 | scratch += len * sizeof (name[0]); \ |
| 244 | scratch_size -= len * sizeof (name[0]); |
| 245 | |
| 246 | #define utf16_index() var1.u32 |
| 247 | |
| 248 | WCHAR *pchars = (WCHAR *) scratch; |
| 249 | unsigned int chars_len = 0; |
| 250 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 251 | hb_codepoint_t c = buffer->info[i].codepoint; |
| 252 | buffer->info[i].utf16_index() = chars_len; |
| 253 | if (likely (c < 0x10000)) |
| 254 | pchars[chars_len++] = c; |
| 255 | else if (unlikely (c >= 0x110000)) |
| 256 | pchars[chars_len++] = 0xFFFD; |
| 257 | else { |
| 258 | pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); |
| 259 | pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | ALLOCATE_ARRAY (WCHAR, wchars, chars_len); |
| 264 | ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
| 265 | ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
| 266 | |
| 267 | /* On Windows, we don't care about alignment...*/ |
| 268 | unsigned int glyphs_size = scratch_size / (sizeof (WORD) + |
| 269 | sizeof (SCRIPT_GLYPHPROP) + |
| 270 | sizeof (int) + |
| 271 | sizeof (GOFFSET) + |
| 272 | sizeof (uint32_t)); |
| 273 | |
| 274 | ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); |
| 275 | ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); |
| 276 | ALLOCATE_ARRAY (int, advances, glyphs_size); |
| 277 | ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); |
| 278 | ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); |
| 279 | |
| 280 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 281 | #define MAX_ITEMS 10 |
| 282 | |
| 283 | SCRIPT_ITEM items[MAX_ITEMS + 1]; |
| 284 | SCRIPT_STATE bidi_state = {0}; |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 285 | WIN_ULONG script_tags[MAX_ITEMS]; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 286 | int item_count; |
| 287 | |
| 288 | bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; |
| 289 | bidi_state.fOverrideDirection = 1; |
| 290 | |
| 291 | hr = ScriptItemizeOpenType (wchars, |
| 292 | chars_len, |
| 293 | MAX_ITEMS, |
| 294 | NULL, |
| 295 | &bidi_state, |
| 296 | items, |
| 297 | script_tags, |
| 298 | &item_count); |
| 299 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 300 | FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 301 | |
| 302 | #undef MAX_ITEMS |
| 303 | |
| 304 | int *range_char_counts = NULL; |
| 305 | TEXTRANGE_PROPERTIES **range_properties = NULL; |
| 306 | int range_count = 0; |
| 307 | if (num_features) { |
| 308 | /* XXX setup ranges */ |
| 309 | } |
| 310 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 311 | OPENTYPE_TAG language_tag = hb_ot_tag_from_language (buffer->props.language); |
| 312 | |
| 313 | unsigned int glyphs_offset = 0; |
| 314 | unsigned int glyphs_len; |
| 315 | for (unsigned int i = 0; i < item_count; i++) |
| 316 | { |
| 317 | unsigned int chars_offset = items[i].iCharPos; |
| 318 | unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
| 319 | OPENTYPE_TAG script_tag = script_tags[i]; /* XXX buffer->props.script */ |
| 320 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 321 | hr = ScriptShapeOpenType (font_data->hdc, |
| 322 | &font_data->script_cache, |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 323 | &items[i].a, |
| 324 | script_tag, |
| 325 | language_tag, |
| 326 | range_char_counts, |
| 327 | range_properties, |
| 328 | range_count, |
| 329 | wchars + chars_offset, |
| 330 | item_chars_len, |
| 331 | glyphs_size - glyphs_offset, |
| 332 | /* out */ |
| 333 | log_clusters + chars_offset, |
| 334 | char_props + chars_offset, |
| 335 | glyphs + glyphs_offset, |
| 336 | glyph_props + glyphs_offset, |
| 337 | (int *) &glyphs_len); |
| 338 | |
| 339 | if (unlikely (items[i].a.fNoGlyphIndex)) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 340 | FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 341 | if (unlikely (hr == E_OUTOFMEMORY)) |
| 342 | { |
| 343 | buffer->ensure (buffer->allocated * 2); |
| 344 | if (buffer->in_error) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 345 | FAIL ("Buffer resize failed"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 346 | goto retry; |
| 347 | } |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 348 | if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
| 349 | FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 350 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 351 | FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 352 | |
Behdad Esfahbod | bf3eef5 | 2011-08-09 00:13:24 +0200 | [diff] [blame] | 353 | hr = ScriptPlaceOpenType (font_data->hdc, |
| 354 | &font_data->script_cache, |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 355 | &items[i].a, |
| 356 | script_tag, |
| 357 | language_tag, |
| 358 | range_char_counts, |
| 359 | range_properties, |
| 360 | range_count, |
| 361 | wchars + chars_offset, |
| 362 | log_clusters + chars_offset, |
| 363 | char_props + chars_offset, |
| 364 | item_chars_len, |
| 365 | glyphs + glyphs_offset, |
| 366 | glyph_props + glyphs_offset, |
| 367 | glyphs_len, |
| 368 | /* out */ |
| 369 | advances + glyphs_offset, |
| 370 | offsets + glyphs_offset, |
| 371 | NULL); |
| 372 | if (unlikely (FAILED (hr))) |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 373 | FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 374 | |
| 375 | glyphs_offset += glyphs_len; |
| 376 | } |
| 377 | glyphs_len = glyphs_offset; |
| 378 | |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 379 | /* Ok, we've got everything we need, now compose output buffer, |
| 380 | * very, *very*, carefully! */ |
| 381 | |
| 382 | /* Calculate visual-clusters. That's what we ship. */ |
| 383 | for (unsigned int i = 0; i < buffer->len; i++) |
Behdad Esfahbod | 577326b | 2011-08-07 01:04:40 -0400 | [diff] [blame] | 384 | vis_clusters[i] = 0; |
| 385 | for (unsigned int i = 0; i < buffer->len; i++) { |
| 386 | uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
| 387 | *p = MIN (*p, buffer->info[i].cluster); |
| 388 | } |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 389 | for (unsigned int i = 1; i < glyphs_len; i++) |
| 390 | if (!glyph_props[i].sva.fClusterStart) |
| 391 | vis_clusters[i] = vis_clusters[i - 1]; |
| 392 | |
| 393 | #undef utf16_index |
| 394 | |
| 395 | buffer->ensure (glyphs_len); |
| 396 | if (buffer->in_error) |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 397 | FAIL ("Buffer in error"); |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 398 | |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 399 | #undef FAIL |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 400 | |
| 401 | /* Set glyph infos */ |
| 402 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 403 | { |
| 404 | hb_glyph_info_t *info = &buffer->info[i]; |
| 405 | |
| 406 | info->codepoint = glyphs[i]; |
| 407 | info->cluster = vis_clusters[i]; |
| 408 | |
| 409 | /* The rest is crap. Let's store position info there for now. */ |
| 410 | info->mask = advances[i]; |
| 411 | info->var1.u32 = offsets[i].du; |
| 412 | info->var2.u32 = offsets[i].dv; |
| 413 | } |
Behdad Esfahbod | 2eb474a | 2011-08-07 00:59:38 -0400 | [diff] [blame] | 414 | buffer->len = glyphs_len; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 415 | |
| 416 | /* Set glyph positions */ |
| 417 | buffer->clear_positions (); |
| 418 | for (unsigned int i = 0; i < glyphs_len; i++) |
| 419 | { |
| 420 | hb_glyph_info_t *info = &buffer->info[i]; |
| 421 | hb_glyph_position_t *pos = &buffer->pos[i]; |
| 422 | |
| 423 | /* TODO vertical */ |
| 424 | pos->x_advance = info->mask; |
| 425 | pos->x_offset = info->var1.u32; |
| 426 | pos->y_offset = info->var2.u32; |
| 427 | } |
| 428 | |
| 429 | /* Wow, done! */ |
Behdad Esfahbod | 02aeca9 | 2011-08-04 22:31:05 -0400 | [diff] [blame] | 430 | return TRUE; |
Behdad Esfahbod | 0fbb2dc | 2011-08-03 19:55:04 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |