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