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