hb-directwrite-shape.cc (18958B)
1 /* 2 * This is part of HarfBuzz, a text shaping library. 3 * 4 * Permission is hereby granted, without written agreement and without 5 * license or royalty fees, to use, copy, modify, and distribute this 6 * software and its documentation for any purpose, provided that the 7 * above copyright notice and the following two paragraphs appear in 8 * all copies of this software. 9 * 10 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 12 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 13 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 14 * DAMAGE. 15 * 16 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 17 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 18 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 19 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 20 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 21 * 22 * Author(s): Behdad Esfahbod 23 */ 24 25 #include "hb.hh" 26 27 #ifdef HAVE_DIRECTWRITE 28 29 #include "hb-shaper-impl.hh" 30 31 #include "hb-directwrite.hh" 32 33 #include "hb-ms-feature-ranges.hh" 34 35 36 /* 37 * shaper face data 38 */ 39 40 hb_directwrite_face_data_t * 41 _hb_directwrite_shaper_face_data_create (hb_face_t *face) 42 { 43 hb_blob_t *blob = hb_face_reference_blob (face); 44 45 hb_directwrite_face_data_t *data = (hb_directwrite_face_data_t *) dw_face_create (blob, face->index); 46 47 hb_blob_destroy (blob); 48 49 return data; 50 } 51 52 void 53 _hb_directwrite_shaper_face_data_destroy (hb_directwrite_face_data_t *data) 54 { 55 ((IDWriteFontFace *) data)->Release (); 56 } 57 58 59 /* 60 * shaper font data 61 */ 62 63 struct hb_directwrite_font_data_t {}; 64 65 hb_directwrite_font_data_t * 66 _hb_directwrite_shaper_font_data_create (hb_font_t *font) 67 { 68 IDWriteFontFace *fontFace = (IDWriteFontFace *) (const void *) font->face->data.directwrite; 69 70 /* 71 * Set up variations. 72 */ 73 IDWriteFontFace5 *fontFaceVariations = nullptr; 74 { 75 IDWriteFontFace5 *fontFace5; 76 if (SUCCEEDED (fontFace->QueryInterface (__uuidof (IDWriteFontFace5), (void **) &fontFace5))) 77 { 78 IDWriteFontResource *fontResource; 79 if (SUCCEEDED (fontFace5->GetFontResource (&fontResource))) 80 { 81 hb_vector_t<DWRITE_FONT_AXIS_VALUE> axis_values; 82 if (likely (axis_values.resize_exact (font->num_coords))) 83 { 84 for (unsigned int i = 0; i < font->num_coords; i++) 85 { 86 hb_ot_var_axis_info_t info; 87 unsigned int c = 1; 88 hb_ot_var_get_axis_infos (font->face, i, &c, &info); 89 axis_values[i].axisTag = (DWRITE_FONT_AXIS_TAG) hb_uint32_swap (info.tag); 90 axis_values[i].value = i < font->num_coords ? 91 hb_clamp (font->design_coords[i], info.min_value, info.max_value) : 92 info.default_value; 93 } 94 95 fontResource->CreateFontFace (DWRITE_FONT_SIMULATIONS::DWRITE_FONT_SIMULATIONS_NONE, 96 axis_values.arrayZ, axis_values.length, &fontFaceVariations); 97 } 98 fontResource->Release (); 99 } 100 fontFace5->Release (); 101 } 102 } 103 104 return (hb_directwrite_font_data_t *) fontFaceVariations; 105 } 106 107 void 108 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_font_data_t *data) 109 { 110 ((IDWriteFontFace *) (const void *) data)->Release (); 111 } 112 113 114 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project 115 // but now is relicensed to MIT for HarfBuzz use 116 class TextAnalysis : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink 117 { 118 private: 119 hb_reference_count_t mRefCount; 120 public: 121 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) 122 { return S_OK; } 123 IFACEMETHOD_ (ULONG, AddRef) () 124 { 125 return mRefCount.inc () + 1; 126 } 127 IFACEMETHOD_ (ULONG, Release) () 128 { 129 signed refCount = mRefCount.dec () - 1; 130 assert (refCount >= 0); 131 if (refCount) 132 return refCount; 133 delete this; 134 return 0; 135 } 136 137 // A single contiguous run of characters containing the same analysis 138 // results. 139 struct Run 140 { 141 uint32_t mTextStart; // starting text position of this run 142 uint32_t mTextLength; // number of contiguous code units covered 143 uint32_t mGlyphStart; // starting glyph in the glyphs array 144 uint32_t mGlyphCount; // number of glyphs associated with this run 145 // text 146 DWRITE_SCRIPT_ANALYSIS mScript; 147 uint8_t mBidiLevel; 148 bool mIsSideways; 149 150 bool ContainsTextPosition (uint32_t aTextPosition) const 151 { 152 return aTextPosition >= mTextStart && 153 aTextPosition < mTextStart + mTextLength; 154 } 155 156 Run *nextRun; 157 }; 158 159 public: 160 TextAnalysis (const wchar_t* text, uint32_t textLength, 161 const wchar_t* localeName, DWRITE_READING_DIRECTION readingDirection) 162 : mTextLength (textLength), mText (text), mLocaleName (localeName), 163 mReadingDirection (readingDirection), mCurrentRun (nullptr) 164 { 165 mRefCount.init (); 166 } 167 virtual ~TextAnalysis () 168 { 169 // delete runs, except mRunHead which is part of the TextAnalysis object 170 for (Run *run = mRunHead.nextRun; run;) 171 { 172 Run *origRun = run; 173 run = run->nextRun; 174 delete origRun; 175 } 176 } 177 178 STDMETHODIMP 179 GenerateResults (IDWriteTextAnalyzer* textAnalyzer, Run **runHead) 180 { 181 // Analyzes the text using the script analyzer and returns 182 // the result as a series of runs. 183 184 HRESULT hr = S_OK; 185 186 // Initially start out with one result that covers the entire range. 187 // This result will be subdivided by the analysis processes. 188 mRunHead.mTextStart = 0; 189 mRunHead.mTextLength = mTextLength; 190 mRunHead.mBidiLevel = 191 (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT); 192 mRunHead.nextRun = nullptr; 193 mCurrentRun = &mRunHead; 194 195 // Call each of the analyzers in sequence, recording their results. 196 if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this))) 197 *runHead = &mRunHead; 198 199 return hr; 200 } 201 202 // IDWriteTextAnalysisSource implementation 203 204 IFACEMETHODIMP 205 GetTextAtPosition (uint32_t textPosition, 206 OUT wchar_t const** textString, 207 OUT uint32_t* textLength) 208 { 209 if (textPosition >= mTextLength) 210 { 211 // No text at this position, valid query though. 212 *textString = nullptr; 213 *textLength = 0; 214 } 215 else 216 { 217 *textString = mText + textPosition; 218 *textLength = mTextLength - textPosition; 219 } 220 return S_OK; 221 } 222 223 IFACEMETHODIMP 224 GetTextBeforePosition (uint32_t textPosition, 225 OUT wchar_t const** textString, 226 OUT uint32_t* textLength) 227 { 228 if (textPosition == 0 || textPosition > mTextLength) 229 { 230 // Either there is no text before here (== 0), or this 231 // is an invalid position. The query is considered valid though. 232 *textString = nullptr; 233 *textLength = 0; 234 } 235 else 236 { 237 *textString = mText; 238 *textLength = textPosition; 239 } 240 return S_OK; 241 } 242 243 IFACEMETHODIMP_ (DWRITE_READING_DIRECTION) 244 GetParagraphReadingDirection () { return mReadingDirection; } 245 246 IFACEMETHODIMP GetLocaleName (uint32_t textPosition, uint32_t* textLength, 247 wchar_t const** localeName) 248 { return S_OK; } 249 250 IFACEMETHODIMP 251 GetNumberSubstitution (uint32_t textPosition, 252 OUT uint32_t* textLength, 253 OUT IDWriteNumberSubstitution** numberSubstitution) 254 { 255 // We do not support number substitution. 256 *numberSubstitution = nullptr; 257 *textLength = mTextLength - textPosition; 258 259 return S_OK; 260 } 261 262 // IDWriteTextAnalysisSink implementation 263 264 IFACEMETHODIMP 265 SetScriptAnalysis (uint32_t textPosition, uint32_t textLength, 266 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis) 267 { 268 SetCurrentRun (textPosition); 269 SplitCurrentRun (textPosition); 270 while (textLength > 0) 271 { 272 Run *run = FetchNextRun (&textLength); 273 run->mScript = *scriptAnalysis; 274 } 275 276 return S_OK; 277 } 278 279 IFACEMETHODIMP 280 SetLineBreakpoints (uint32_t textPosition, 281 uint32_t textLength, 282 const DWRITE_LINE_BREAKPOINT* lineBreakpoints) 283 { return S_OK; } 284 285 IFACEMETHODIMP SetBidiLevel (uint32_t textPosition, uint32_t textLength, 286 uint8_t explicitLevel, uint8_t resolvedLevel) 287 { return S_OK; } 288 289 IFACEMETHODIMP 290 SetNumberSubstitution (uint32_t textPosition, uint32_t textLength, 291 IDWriteNumberSubstitution* numberSubstitution) 292 { return S_OK; } 293 294 protected: 295 Run *FetchNextRun (IN OUT uint32_t* textLength) 296 { 297 // Used by the sink setters, this returns a reference to the next run. 298 // Position and length are adjusted to now point after the current run 299 // being returned. 300 301 Run *origRun = mCurrentRun; 302 // Split the tail if needed (the length remaining is less than the 303 // current run's size). 304 if (*textLength < mCurrentRun->mTextLength) 305 SplitCurrentRun (mCurrentRun->mTextStart + *textLength); 306 else 307 // Just advance the current run. 308 mCurrentRun = mCurrentRun->nextRun; 309 *textLength -= origRun->mTextLength; 310 311 // Return a reference to the run that was just current. 312 return origRun; 313 } 314 315 void SetCurrentRun (uint32_t textPosition) 316 { 317 // Move the current run to the given position. 318 // Since the analyzers generally return results in a forward manner, 319 // this will usually just return early. If not, find the 320 // corresponding run for the text position. 321 322 if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition)) 323 return; 324 325 for (Run *run = &mRunHead; run; run = run->nextRun) 326 if (run->ContainsTextPosition (textPosition)) 327 { 328 mCurrentRun = run; 329 return; 330 } 331 assert (0); // We should always be able to find the text position in one of our runs 332 } 333 334 void SplitCurrentRun (uint32_t splitPosition) 335 { 336 if (!mCurrentRun) 337 { 338 assert (0); // SplitCurrentRun called without current run 339 // Shouldn't be calling this when no current run is set! 340 return; 341 } 342 // Split the current run. 343 if (splitPosition <= mCurrentRun->mTextStart) 344 { 345 // No need to split, already the start of a run 346 // or before it. Usually the first. 347 return; 348 } 349 Run *newRun = new Run; 350 351 *newRun = *mCurrentRun; 352 353 // Insert the new run in our linked list. 354 newRun->nextRun = mCurrentRun->nextRun; 355 mCurrentRun->nextRun = newRun; 356 357 // Adjust runs' text positions and lengths. 358 uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart; 359 newRun->mTextStart += splitPoint; 360 newRun->mTextLength -= splitPoint; 361 mCurrentRun->mTextLength = splitPoint; 362 mCurrentRun = newRun; 363 } 364 365 protected: 366 // Input 367 // (weak references are fine here, since this class is a transient 368 // stack-based helper that doesn't need to copy data) 369 uint32_t mTextLength; 370 const wchar_t* mText; 371 const wchar_t* mLocaleName; 372 DWRITE_READING_DIRECTION mReadingDirection; 373 374 // Current processing state. 375 Run *mCurrentRun; 376 377 // Output is a list of runs starting here 378 Run mRunHead; 379 }; 380 381 /* 382 * shaper 383 */ 384 385 hb_bool_t 386 _hb_directwrite_shape (hb_shape_plan_t *shape_plan, 387 hb_font_t *font, 388 hb_buffer_t *buffer, 389 const hb_feature_t *features, 390 unsigned int num_features) 391 { 392 IDWriteFontFace *fontFace = (IDWriteFontFace *) (const void *) font->data.directwrite; 393 auto *global = get_directwrite_global (); 394 if (unlikely (!global)) 395 return false; 396 IDWriteFactory *dwriteFactory = global->dwriteFactory; 397 398 IDWriteTextAnalyzer* analyzer; 399 dwriteFactory->CreateTextAnalyzer (&analyzer); 400 401 unsigned int scratch_size; 402 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); 403 #define ALLOCATE_ARRAY(Type, name, len) \ 404 Type *name = (Type *) scratch; \ 405 do { \ 406 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ 407 assert (_consumed <= scratch_size); \ 408 scratch += _consumed; \ 409 scratch_size -= _consumed; \ 410 } while (0) 411 412 #define utf16_index() var1.u32 413 414 ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2); 415 416 unsigned int chars_len = 0; 417 for (unsigned int i = 0; i < buffer->len; i++) 418 { 419 hb_codepoint_t c = buffer->info[i].codepoint; 420 buffer->info[i].utf16_index () = chars_len; 421 if (likely (c <= 0xFFFFu)) 422 textString[chars_len++] = c; 423 else if (unlikely (c > 0x10FFFFu)) 424 textString[chars_len++] = 0xFFFDu; 425 else 426 { 427 textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10); 428 textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1)); 429 } 430 } 431 432 ALLOCATE_ARRAY (WORD, log_clusters, chars_len); 433 /* Need log_clusters to assign features. */ 434 chars_len = 0; 435 for (unsigned int i = 0; i < buffer->len; i++) 436 { 437 hb_codepoint_t c = buffer->info[i].codepoint; 438 unsigned int cluster = buffer->info[i].cluster; 439 log_clusters[chars_len++] = cluster; 440 if (hb_in_range (c, 0x10000u, 0x10FFFFu)) 441 log_clusters[chars_len++] = cluster; /* Surrogates. */ 442 } 443 444 DWRITE_READING_DIRECTION readingDirection; 445 readingDirection = buffer->props.direction ? 446 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT : 447 DWRITE_READING_DIRECTION_LEFT_TO_RIGHT; 448 449 /* 450 * There's an internal 16-bit limit on some things inside the analyzer, 451 * but we never attempt to shape a word longer than 64K characters 452 * in a single gfxShapedWord, so we cannot exceed that limit. 453 */ 454 uint32_t textLength = chars_len; 455 456 TextAnalysis analysis (textString, textLength, nullptr, readingDirection); 457 TextAnalysis::Run *runHead; 458 HRESULT hr; 459 hr = analysis.GenerateResults (analyzer, &runHead); 460 461 #define FAIL(...) \ 462 HB_STMT_START { \ 463 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \ 464 return false; \ 465 } HB_STMT_END 466 467 if (FAILED (hr)) 468 FAIL ("Analyzer failed to generate results."); 469 470 uint32_t maxGlyphCount = 3 * textLength / 2 + 16; 471 uint32_t glyphCount; 472 bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); 473 474 const wchar_t localeName[20] = {0}; 475 if (buffer->props.language) 476 mbstowcs ((wchar_t*) localeName, 477 hb_language_to_string (buffer->props.language), 20); 478 479 /* 480 * Set up features. 481 */ 482 static_assert ((sizeof (DWRITE_TYPOGRAPHIC_FEATURES) == sizeof (hb_ms_features_t)), ""); 483 static_assert ((sizeof (DWRITE_FONT_FEATURE) == sizeof (hb_ms_feature_t)), ""); 484 hb_vector_t<hb_ms_features_t *> range_features; 485 hb_vector_t<uint32_t> range_char_counts; 486 487 // https://github.com/harfbuzz/harfbuzz/pull/5114 488 // The data allocated by these two vectors are used by the above two, so they 489 // should remain alive as long as the above two are. 490 hb_vector_t<hb_ms_feature_t> feature_records; 491 hb_vector_t<hb_ms_range_record_t> range_records; 492 if (num_features) 493 { 494 if (hb_ms_setup_features (features, num_features, feature_records, range_records)) 495 { 496 hb_ms_make_feature_ranges (feature_records, 497 range_records, 498 0, 499 chars_len, 500 log_clusters, 501 range_features, 502 range_char_counts); 503 } 504 } 505 506 uint16_t* clusterMap; 507 clusterMap = new uint16_t[textLength]; 508 DWRITE_SHAPING_TEXT_PROPERTIES* textProperties; 509 textProperties = new DWRITE_SHAPING_TEXT_PROPERTIES[textLength]; 510 511 retry_getglyphs: 512 uint16_t* glyphIndices = new uint16_t[maxGlyphCount]; 513 DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties; 514 glyphProperties = new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount]; 515 516 hr = analyzer->GetGlyphs (textString, 517 chars_len, 518 fontFace, 519 false, 520 isRightToLeft, 521 &runHead->mScript, 522 localeName, 523 nullptr, 524 (const DWRITE_TYPOGRAPHIC_FEATURES**) range_features.arrayZ, 525 range_char_counts.arrayZ, 526 range_features.length, 527 maxGlyphCount, 528 clusterMap, 529 textProperties, 530 glyphIndices, 531 glyphProperties, 532 &glyphCount); 533 534 if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))) 535 { 536 delete [] glyphIndices; 537 delete [] glyphProperties; 538 539 maxGlyphCount *= 2; 540 541 goto retry_getglyphs; 542 } 543 if (FAILED (hr)) 544 FAIL ("Analyzer failed to get glyphs."); 545 546 float* glyphAdvances = new float[maxGlyphCount]; 547 DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount]; 548 549 /* The -2 in the following is to compensate for possible 550 * alignment needed after the WORD array. sizeof (WORD) == 2. */ 551 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2) 552 / (sizeof (WORD) + 553 sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) + 554 sizeof (int) + 555 sizeof (DWRITE_GLYPH_OFFSET) + 556 sizeof (uint32_t)); 557 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); 558 559 #undef ALLOCATE_ARRAY 560 561 unsigned fontEmSize = font->face->get_upem (); 562 563 float x_mult = font->x_multf; 564 float y_mult = font->y_multf; 565 566 hr = analyzer->GetGlyphPlacements (textString, 567 clusterMap, 568 textProperties, 569 chars_len, 570 glyphIndices, 571 glyphProperties, 572 glyphCount, 573 fontFace, 574 fontEmSize, 575 false, 576 isRightToLeft, 577 &runHead->mScript, 578 localeName, 579 (const DWRITE_TYPOGRAPHIC_FEATURES**) range_features.arrayZ, 580 range_char_counts.arrayZ, 581 range_features.length, 582 glyphAdvances, 583 glyphOffsets); 584 585 if (FAILED (hr)) 586 FAIL ("Analyzer failed to get glyph placements."); 587 588 /* Ok, we've got everything we need, now compose output buffer, 589 * very, *very*, carefully! */ 590 591 /* Calculate visual-clusters. That's what we ship. */ 592 for (unsigned int i = 0; i < glyphCount; i++) 593 vis_clusters[i] = (uint32_t) -1; 594 for (unsigned int i = 0; i < buffer->len; i++) 595 { 596 uint32_t *p = 597 &vis_clusters[log_clusters[buffer->info[i].utf16_index ()]]; 598 *p = hb_min (*p, buffer->info[i].cluster); 599 } 600 for (unsigned int i = 1; i < glyphCount; i++) 601 if (vis_clusters[i] == (uint32_t) -1) 602 vis_clusters[i] = vis_clusters[i - 1]; 603 604 #undef utf16_index 605 606 if (unlikely (!buffer->ensure (glyphCount))) 607 FAIL ("Buffer in error"); 608 609 #undef FAIL 610 611 /* Set glyph infos */ 612 buffer->len = 0; 613 for (unsigned int i = 0; i < glyphCount; i++) 614 { 615 hb_glyph_info_t *info = &buffer->info[buffer->len++]; 616 617 info->codepoint = glyphIndices[i]; 618 info->cluster = vis_clusters[i]; 619 620 /* The rest is crap. Let's store position info there for now. */ 621 info->mask = glyphAdvances[i]; 622 info->var1.i32 = glyphOffsets[i].advanceOffset; 623 info->var2.i32 = glyphOffsets[i].ascenderOffset; 624 } 625 626 /* Set glyph positions */ 627 buffer->clear_positions (); 628 for (unsigned int i = 0; i < glyphCount; i++) 629 { 630 hb_glyph_info_t *info = &buffer->info[i]; 631 hb_glyph_position_t *pos = &buffer->pos[i]; 632 633 /* TODO vertical */ 634 pos->x_advance = round (x_mult * (int32_t) info->mask); 635 pos->x_offset = round (x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32)); 636 pos->y_offset = round (y_mult * info->var2.i32); 637 } 638 639 if (isRightToLeft) hb_buffer_reverse (buffer); 640 641 buffer->clear_glyph_flags (); 642 buffer->unsafe_to_break (); 643 644 delete [] clusterMap; 645 delete [] glyphIndices; 646 delete [] textProperties; 647 delete [] glyphProperties; 648 delete [] glyphAdvances; 649 delete [] glyphOffsets; 650 651 /* Wow, done! */ 652 return true; 653 } 654 655 656 #endif