OLD | NEW |
(Empty) | |
| 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 |
| 34 typedef 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 /* |
| 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 |
| 61 static bool |
| 62 populate_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->fa
ce, 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_FACES
IZE) |
| 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 |
| 95 static hb_user_data_key_t hb_uniscribe_data_key; |
| 96 |
| 97 |
| 98 static struct hb_uniscribe_face_data_t { |
| 99 HANDLE fh; |
| 100 } _hb_uniscribe_face_data_nil = {0}; |
| 101 |
| 102 static 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 |
| 110 static 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, &hb_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 |
| 121 hb_blob_t *blob = hb_face_reference_blob (face); |
| 122 unsigned int blob_length; |
| 123 const char *blob_data = hb_blob_get_data (blob, &blob_length); |
| 124 if (unlikely (!blob_length)) |
| 125 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); |
| 126 |
| 127 DWORD num_fonts_installed; |
| 128 data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_font
s_installed); |
| 129 hb_blob_destroy (blob); |
| 130 if (unlikely (!data->fh)) |
| 131 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); |
| 132 |
| 133 |
| 134 if (unlikely (!hb_face_set_user_data (face, &hb_uniscribe_data_key, data, |
| 135 (hb_destroy_func_t) _hb_uniscribe_face_d
ata_destroy, |
| 136 FALSE))) |
| 137 { |
| 138 _hb_uniscribe_face_data_destroy (data); |
| 139 data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &hb_uniscri
be_data_key); |
| 140 if (data) |
| 141 return data; |
| 142 else |
| 143 return &_hb_uniscribe_face_data_nil; |
| 144 } |
| 145 |
| 146 return data; |
| 147 } |
| 148 |
| 149 |
| 150 static struct hb_uniscribe_font_data_t { |
| 151 HDC hdc; |
| 152 LOGFONTW log_font; |
| 153 HFONT hfont; |
| 154 SCRIPT_CACHE script_cache; |
| 155 } _hb_uniscribe_font_data_nil = {NULL, NULL, NULL}; |
| 156 |
| 157 static void |
| 158 _hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data) |
| 159 { |
| 160 if (data->hdc) |
| 161 ReleaseDC (NULL, data->hdc); |
| 162 if (data->hfont) |
| 163 DeleteObject (data->hfont); |
| 164 if (data->script_cache) |
| 165 ScriptFreeCache (&data->script_cache); |
| 166 free (data); |
| 167 } |
| 168 |
| 169 static hb_uniscribe_font_data_t * |
| 170 _hb_uniscribe_font_get_data (hb_font_t *font) |
| 171 { |
| 172 hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user
_data (font, &hb_uniscribe_data_key); |
| 173 if (likely (data)) return data; |
| 174 |
| 175 data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_
t)); |
| 176 if (unlikely (!data)) |
| 177 return &_hb_uniscribe_font_data_nil; |
| 178 |
| 179 data->hdc = GetDC (NULL); |
| 180 |
| 181 if (unlikely (!populate_log_font (&data->log_font, data->hdc, font))) |
| 182 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); |
| 183 else { |
| 184 data->hfont = CreateFontIndirectW (&data->log_font); |
| 185 if (unlikely (!data->hfont)) |
| 186 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); |
| 187 if (!SelectObject (data->hdc, data->hfont)) |
| 188 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); |
| 189 } |
| 190 |
| 191 if (unlikely (!hb_font_set_user_data (font, &hb_uniscribe_data_key, data, |
| 192 (hb_destroy_func_t) _hb_uniscribe_font_d
ata_destroy, |
| 193 FALSE))) |
| 194 { |
| 195 _hb_uniscribe_font_data_destroy (data); |
| 196 data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &hb_uniscri
be_data_key); |
| 197 if (data) |
| 198 return data; |
| 199 else |
| 200 return &_hb_uniscribe_font_data_nil; |
| 201 } |
| 202 |
| 203 return data; |
| 204 } |
| 205 |
| 206 LOGFONTW * |
| 207 hb_uniscribe_font_get_logfontw (hb_font_t *font) |
| 208 { |
| 209 hb_uniscribe_font_data_t *font_data = _hb_uniscribe_font_get_data (font); |
| 210 if (unlikely (!font_data)) |
| 211 return NULL; |
| 212 return &font_data->log_font; |
| 213 } |
| 214 |
| 215 HFONT |
| 216 hb_uniscribe_font_get_hfont (hb_font_t *font) |
| 217 { |
| 218 hb_uniscribe_font_data_t *font_data = _hb_uniscribe_font_get_data (font); |
| 219 if (unlikely (!font_data)) |
| 220 return 0; |
| 221 return font_data->hfont; |
| 222 } |
| 223 |
| 224 |
| 225 hb_bool_t |
| 226 hb_uniscribe_shape (hb_font_t *font, |
| 227 hb_buffer_t *buffer, |
| 228 const hb_feature_t *features, |
| 229 unsigned int num_features, |
| 230 const char * const *shaper_options) |
| 231 { |
| 232 buffer->guess_properties (); |
| 233 |
| 234 #define FAIL(...) \ |
| 235 HB_STMT_START { \ |
| 236 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ |
| 237 return FALSE; \ |
| 238 } HB_STMT_END; |
| 239 |
| 240 hb_uniscribe_face_data_t *face_data = _hb_uniscribe_face_get_data (font->face)
; |
| 241 if (unlikely (!face_data->fh)) |
| 242 FAIL ("Couldn't get face data"); |
| 243 |
| 244 hb_uniscribe_font_data_t *font_data = _hb_uniscribe_font_get_data (font); |
| 245 if (unlikely (!font_data->hfont)) |
| 246 FAIL ("Couldn't get font font"); |
| 247 |
| 248 if (unlikely (!buffer->len)) |
| 249 return TRUE; |
| 250 |
| 251 HRESULT hr; |
| 252 |
| 253 retry: |
| 254 |
| 255 unsigned int scratch_size; |
| 256 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); |
| 257 |
| 258 /* Allocate char buffers; they all fit */ |
| 259 |
| 260 #define ALLOCATE_ARRAY(Type, name, len) \ |
| 261 Type *name = (Type *) scratch; \ |
| 262 scratch += len * sizeof (name[0]); \ |
| 263 scratch_size -= len * sizeof (name[0]); |
| 264 |
| 265 #define utf16_index() var1.u32 |
| 266 |
| 267 WCHAR *pchars = (WCHAR *) scratch; |
| 268 unsigned int chars_len = 0; |
| 269 for (unsigned int i = 0; i < buffer->len; i++) { |
| 270 hb_codepoint_t c = buffer->info[i].codepoint; |
| 271 buffer->info[i].utf16_index() = chars_len; |
| 272 if (likely (c < 0x10000)) |
| 273 pchars[chars_len++] = c; |
| 274 else if (unlikely (c >= 0x110000)) |
| 275 pchars[chars_len++] = 0xFFFD; |
| 276 else { |
| 277 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); |
| 278 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); |
| 279 } |
| 280 } |
| 281 |
| 282 ALLOCATE_ARRAY (WCHAR, wchars, chars_len); |
| 283 ALLOCATE_ARRAY (WORD, log_clusters, chars_len); |
| 284 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); |
| 285 |
| 286 /* On Windows, we don't care about alignment...*/ |
| 287 unsigned int glyphs_size = scratch_size / (sizeof (WORD) + |
| 288 sizeof (SCRIPT_GLYPHPROP) + |
| 289 sizeof (int) + |
| 290 sizeof (GOFFSET) + |
| 291 sizeof (uint32_t)); |
| 292 |
| 293 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); |
| 294 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); |
| 295 ALLOCATE_ARRAY (int, advances, glyphs_size); |
| 296 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); |
| 297 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); |
| 298 |
| 299 |
| 300 #define MAX_ITEMS 10 |
| 301 |
| 302 SCRIPT_ITEM items[MAX_ITEMS + 1]; |
| 303 SCRIPT_CONTROL bidi_control = {0}; |
| 304 SCRIPT_STATE bidi_state = {0}; |
| 305 WIN_ULONG script_tags[MAX_ITEMS]; |
| 306 int item_count; |
| 307 |
| 308 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ |
| 309 //bidi_control.fMergeNeutralItems = TRUE; |
| 310 *(uint32_t*)&bidi_control |= 1<<24; |
| 311 |
| 312 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0
: 1; |
| 313 // bidi_state.fOverrideDirection = 1; |
| 314 |
| 315 hr = ScriptItemizeOpenType (wchars, |
| 316 chars_len, |
| 317 MAX_ITEMS, |
| 318 &bidi_control, |
| 319 &bidi_state, |
| 320 items, |
| 321 script_tags, |
| 322 &item_count); |
| 323 if (unlikely (FAILED (hr))) |
| 324 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); |
| 325 |
| 326 #undef MAX_ITEMS |
| 327 |
| 328 int *range_char_counts = NULL; |
| 329 TEXTRANGE_PROPERTIES **range_properties = NULL; |
| 330 int range_count = 0; |
| 331 if (num_features) { |
| 332 /* TODO setup ranges */ |
| 333 } |
| 334 |
| 335 OPENTYPE_TAG language_tag = hb_ot_tag_from_language (buffer->props.language); |
| 336 |
| 337 unsigned int glyphs_offset = 0; |
| 338 unsigned int glyphs_len; |
| 339 for (unsigned int i = 0; i < item_count; i++) |
| 340 { |
| 341 unsigned int chars_offset = items[i].iCharPos; |
| 342 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; |
| 343 OPENTYPE_TAG script_tag = script_tags[i]; /* XXX buffer->props.script */ |
| 344 |
| 345 hr = ScriptShapeOpenType (font_data->hdc, |
| 346 &font_data->script_cache, |
| 347 &items[i].a, |
| 348 script_tag, |
| 349 language_tag, |
| 350 range_char_counts, |
| 351 range_properties, |
| 352 range_count, |
| 353 wchars + chars_offset, |
| 354 item_chars_len, |
| 355 glyphs_size - glyphs_offset, |
| 356 /* out */ |
| 357 log_clusters + chars_offset, |
| 358 char_props + chars_offset, |
| 359 glyphs + glyphs_offset, |
| 360 glyph_props + glyphs_offset, |
| 361 (int *) &glyphs_len); |
| 362 |
| 363 if (unlikely (items[i].a.fNoGlyphIndex)) |
| 364 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); |
| 365 if (unlikely (hr == E_OUTOFMEMORY)) |
| 366 { |
| 367 buffer->ensure (buffer->allocated * 2); |
| 368 if (buffer->in_error) |
| 369 FAIL ("Buffer resize failed"); |
| 370 goto retry; |
| 371 } |
| 372 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) |
| 373 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); |
| 374 if (unlikely (FAILED (hr))) |
| 375 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); |
| 376 |
| 377 hr = ScriptPlaceOpenType (font_data->hdc, |
| 378 &font_data->script_cache, |
| 379 &items[i].a, |
| 380 script_tag, |
| 381 language_tag, |
| 382 range_char_counts, |
| 383 range_properties, |
| 384 range_count, |
| 385 wchars + chars_offset, |
| 386 log_clusters + chars_offset, |
| 387 char_props + chars_offset, |
| 388 item_chars_len, |
| 389 glyphs + glyphs_offset, |
| 390 glyph_props + glyphs_offset, |
| 391 glyphs_len, |
| 392 /* out */ |
| 393 advances + glyphs_offset, |
| 394 offsets + glyphs_offset, |
| 395 NULL); |
| 396 if (unlikely (FAILED (hr))) |
| 397 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); |
| 398 |
| 399 glyphs_offset += glyphs_len; |
| 400 } |
| 401 glyphs_len = glyphs_offset; |
| 402 |
| 403 /* Ok, we've got everything we need, now compose output buffer, |
| 404 * very, *very*, carefully! */ |
| 405 |
| 406 /* Calculate visual-clusters. That's what we ship. */ |
| 407 for (unsigned int i = 0; i < glyphs_len; i++) |
| 408 vis_clusters[i] = -1; |
| 409 for (unsigned int i = 0; i < buffer->len; i++) { |
| 410 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; |
| 411 *p = MIN (*p, buffer->info[i].cluster); |
| 412 } |
| 413 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { |
| 414 for (unsigned int i = 1; i < glyphs_len; i++) |
| 415 if (!glyph_props[i].sva.fClusterStart) |
| 416 vis_clusters[i] = vis_clusters[i - 1]; |
| 417 } else { |
| 418 for (int i = glyphs_len - 2; i >= 0; i--) |
| 419 if (!glyph_props[i].sva.fClusterStart) |
| 420 vis_clusters[i] = vis_clusters[i + 1]; |
| 421 } |
| 422 |
| 423 #undef utf16_index |
| 424 |
| 425 buffer->ensure (glyphs_len); |
| 426 if (buffer->in_error) |
| 427 FAIL ("Buffer in error"); |
| 428 |
| 429 #undef FAIL |
| 430 |
| 431 /* Set glyph infos */ |
| 432 buffer->len = 0; |
| 433 for (unsigned int i = 0; i < glyphs_len; i++) |
| 434 { |
| 435 hb_glyph_info_t *info = &buffer->info[buffer->len++]; |
| 436 |
| 437 info->codepoint = glyphs[i]; |
| 438 info->cluster = vis_clusters[i]; |
| 439 |
| 440 /* The rest is crap. Let's store position info there for now. */ |
| 441 info->mask = advances[i]; |
| 442 info->var1.u32 = offsets[i].du; |
| 443 info->var2.u32 = offsets[i].dv; |
| 444 } |
| 445 |
| 446 /* Set glyph positions */ |
| 447 buffer->clear_positions (); |
| 448 for (unsigned int i = 0; i < glyphs_len; i++) |
| 449 { |
| 450 hb_glyph_info_t *info = &buffer->info[i]; |
| 451 hb_glyph_position_t *pos = &buffer->pos[i]; |
| 452 |
| 453 /* TODO vertical */ |
| 454 pos->x_advance = info->mask; |
| 455 pos->x_offset = info->var1.u32; |
| 456 pos->y_offset = info->var2.u32; |
| 457 } |
| 458 |
| 459 /* Wow, done! */ |
| 460 return TRUE; |
| 461 } |
| 462 |
| 463 |
OLD | NEW |