2 * Copyright © 2015-2019 Ebrahim Byagowi
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 #include "hb-shaper-impl.hh"
30 #include "hb-directwrite.h"
34 * hb-directwrite uses new/delete syntatically but as we let users
35 * to override malloc/free, we will redefine new/delete so users
36 * won't need to do that by their own.
38 void* operator new (size_t size) { return malloc (size); }
39 void* operator new [] (size_t size) { return malloc (size); }
40 void operator delete (void* pointer) { free (pointer); }
41 void operator delete [] (void* pointer) { free (pointer); }
45 * DirectWrite font stream helpers
48 // This is a font loader which provides only one font (unlike its original design).
49 // For a better implementation which was also source of this
50 // and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
51 class DWriteFontFileLoader : public IDWriteFontFileLoader
54 IDWriteFontFileStream *mFontFileStream;
56 DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream)
57 { mFontFileStream = fontFileStream; }
60 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
62 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
63 IFACEMETHOD_ (ULONG, Release) () { return 1; }
65 // IDWriteFontFileLoader methods
66 virtual HRESULT STDMETHODCALLTYPE
67 CreateStreamFromKey (void const* fontFileReferenceKey,
68 uint32_t fontFileReferenceKeySize,
69 OUT IDWriteFontFileStream** fontFileStream)
71 *fontFileStream = mFontFileStream;
75 virtual ~DWriteFontFileLoader() {}
78 class DWriteFontFileStream : public IDWriteFontFileStream
84 DWriteFontFileStream (uint8_t *aData, uint32_t aSize)
91 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
93 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
94 IFACEMETHOD_ (ULONG, Release) () { return 1; }
96 // IDWriteFontFileStream methods
97 virtual HRESULT STDMETHODCALLTYPE
98 ReadFileFragment (void const** fragmentStart,
101 OUT void** fragmentContext)
103 // We are required to do bounds checking.
104 if (fileOffset + fragmentSize > mSize) return E_FAIL;
106 // truncate the 64 bit fileOffset to size_t sized index into mData
107 size_t index = static_cast<size_t> (fileOffset);
109 // We should be alive for the duration of this.
110 *fragmentStart = &mData[index];
111 *fragmentContext = nullptr;
115 virtual void STDMETHODCALLTYPE
116 ReleaseFileFragment (void* fragmentContext) {}
118 virtual HRESULT STDMETHODCALLTYPE
119 GetFileSize (OUT UINT64* fileSize)
125 virtual HRESULT STDMETHODCALLTYPE
126 GetLastWriteTime (OUT UINT64* lastWriteTime) { return E_NOTIMPL; }
128 virtual ~DWriteFontFileStream() {}
136 struct hb_directwrite_face_data_t
138 IDWriteFactory *dwriteFactory;
139 IDWriteFontFile *fontFile;
140 DWriteFontFileStream *fontFileStream;
141 DWriteFontFileLoader *fontFileLoader;
142 IDWriteFontFace *fontFace;
146 hb_directwrite_face_data_t *
147 _hb_directwrite_shaper_face_data_create (hb_face_t *face)
149 hb_directwrite_face_data_t *data = new hb_directwrite_face_data_t;
150 if (unlikely (!data))
153 // TODO: factory and fontFileLoader should be cached separately
154 IDWriteFactory* dwriteFactory;
155 DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED, __uuidof (IDWriteFactory),
156 (IUnknown**) &dwriteFactory);
159 hb_blob_t *blob = hb_face_reference_blob (face);
160 DWriteFontFileStream *fontFileStream;
161 fontFileStream = new DWriteFontFileStream ((uint8_t *) hb_blob_get_data (blob, nullptr),
162 hb_blob_get_length (blob));
164 DWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
165 dwriteFactory->RegisterFontFileLoader (fontFileLoader);
167 IDWriteFontFile *fontFile;
168 uint64_t fontFileKey = 0;
169 hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
170 fontFileLoader, &fontFile);
174 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
179 FAIL ("Failed to load font file from data!");
182 DWRITE_FONT_FILE_TYPE fileType;
183 DWRITE_FONT_FACE_TYPE faceType;
184 uint32_t numberOfFaces;
185 hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
186 if (FAILED (hr) || !isSupported)
187 FAIL ("Font file is not supported.");
191 IDWriteFontFace *fontFace;
192 dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
193 DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
195 data->dwriteFactory = dwriteFactory;
196 data->fontFile = fontFile;
197 data->fontFileStream = fontFileStream;
198 data->fontFileLoader = fontFileLoader;
199 data->fontFace = fontFace;
200 data->faceBlob = blob;
206 _hb_directwrite_shaper_face_data_destroy (hb_directwrite_face_data_t *data)
209 data->fontFace->Release ();
211 data->fontFile->Release ();
212 if (data->dwriteFactory)
214 if (data->fontFileLoader)
215 data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
216 data->dwriteFactory->Release ();
218 if (data->fontFileLoader)
219 delete data->fontFileLoader;
220 if (data->fontFileStream)
221 delete data->fontFileStream;
223 hb_blob_destroy (data->faceBlob);
233 struct hb_directwrite_font_data_t {};
235 hb_directwrite_font_data_t *
236 _hb_directwrite_shaper_font_data_create (hb_font_t *font)
238 hb_directwrite_font_data_t *data = new hb_directwrite_font_data_t;
239 if (unlikely (!data))
246 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_font_data_t *data)
252 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
253 // but now is relicensed to MIT for HarfBuzz use
254 class TextAnalysis : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
258 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
260 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
261 IFACEMETHOD_ (ULONG, Release) () { return 1; }
263 // A single contiguous run of characters containing the same analysis
267 uint32_t mTextStart; // starting text position of this run
268 uint32_t mTextLength; // number of contiguous code units covered
269 uint32_t mGlyphStart; // starting glyph in the glyphs array
270 uint32_t mGlyphCount; // number of glyphs associated with this run
272 DWRITE_SCRIPT_ANALYSIS mScript;
276 bool ContainsTextPosition (uint32_t aTextPosition) const
278 return aTextPosition >= mTextStart &&
279 aTextPosition < mTextStart + mTextLength;
286 TextAnalysis (const wchar_t* text, uint32_t textLength,
287 const wchar_t* localeName, DWRITE_READING_DIRECTION readingDirection)
288 : mTextLength (textLength), mText (text), mLocaleName (localeName),
289 mReadingDirection (readingDirection), mCurrentRun (nullptr) {}
292 // delete runs, except mRunHead which is part of the TextAnalysis object
293 for (Run *run = mRunHead.nextRun; run;)
302 GenerateResults (IDWriteTextAnalyzer* textAnalyzer, Run **runHead)
304 // Analyzes the text using the script analyzer and returns
305 // the result as a series of runs.
309 // Initially start out with one result that covers the entire range.
310 // This result will be subdivided by the analysis processes.
311 mRunHead.mTextStart = 0;
312 mRunHead.mTextLength = mTextLength;
313 mRunHead.mBidiLevel =
314 (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
315 mRunHead.nextRun = nullptr;
316 mCurrentRun = &mRunHead;
318 // Call each of the analyzers in sequence, recording their results.
319 if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this)))
320 *runHead = &mRunHead;
325 // IDWriteTextAnalysisSource implementation
328 GetTextAtPosition (uint32_t textPosition,
329 OUT wchar_t const** textString,
330 OUT uint32_t* textLength)
332 if (textPosition >= mTextLength)
334 // No text at this position, valid query though.
335 *textString = nullptr;
340 *textString = mText + textPosition;
341 *textLength = mTextLength - textPosition;
347 GetTextBeforePosition (uint32_t textPosition,
348 OUT wchar_t const** textString,
349 OUT uint32_t* textLength)
351 if (textPosition == 0 || textPosition > mTextLength)
353 // Either there is no text before here (== 0), or this
354 // is an invalid position. The query is considered valid though.
355 *textString = nullptr;
361 *textLength = textPosition;
366 IFACEMETHODIMP_ (DWRITE_READING_DIRECTION)
367 GetParagraphReadingDirection () { return mReadingDirection; }
369 IFACEMETHODIMP GetLocaleName (uint32_t textPosition, uint32_t* textLength,
370 wchar_t const** localeName)
374 GetNumberSubstitution (uint32_t textPosition,
375 OUT uint32_t* textLength,
376 OUT IDWriteNumberSubstitution** numberSubstitution)
378 // We do not support number substitution.
379 *numberSubstitution = nullptr;
380 *textLength = mTextLength - textPosition;
385 // IDWriteTextAnalysisSink implementation
388 SetScriptAnalysis (uint32_t textPosition, uint32_t textLength,
389 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
391 SetCurrentRun (textPosition);
392 SplitCurrentRun (textPosition);
393 while (textLength > 0)
395 Run *run = FetchNextRun (&textLength);
396 run->mScript = *scriptAnalysis;
403 SetLineBreakpoints (uint32_t textPosition,
405 const DWRITE_LINE_BREAKPOINT* lineBreakpoints)
408 IFACEMETHODIMP SetBidiLevel (uint32_t textPosition, uint32_t textLength,
409 uint8_t explicitLevel, uint8_t resolvedLevel)
413 SetNumberSubstitution (uint32_t textPosition, uint32_t textLength,
414 IDWriteNumberSubstitution* numberSubstitution)
418 Run *FetchNextRun (IN OUT uint32_t* textLength)
420 // Used by the sink setters, this returns a reference to the next run.
421 // Position and length are adjusted to now point after the current run
424 Run *origRun = mCurrentRun;
425 // Split the tail if needed (the length remaining is less than the
426 // current run's size).
427 if (*textLength < mCurrentRun->mTextLength)
428 SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
430 // Just advance the current run.
431 mCurrentRun = mCurrentRun->nextRun;
432 *textLength -= origRun->mTextLength;
434 // Return a reference to the run that was just current.
438 void SetCurrentRun (uint32_t textPosition)
440 // Move the current run to the given position.
441 // Since the analyzers generally return results in a forward manner,
442 // this will usually just return early. If not, find the
443 // corresponding run for the text position.
445 if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
448 for (Run *run = &mRunHead; run; run = run->nextRun)
449 if (run->ContainsTextPosition (textPosition))
454 assert (0); // We should always be able to find the text position in one of our runs
457 void SplitCurrentRun (uint32_t splitPosition)
461 assert (0); // SplitCurrentRun called without current run
462 // Shouldn't be calling this when no current run is set!
465 // Split the current run.
466 if (splitPosition <= mCurrentRun->mTextStart)
468 // No need to split, already the start of a run
469 // or before it. Usually the first.
472 Run *newRun = new Run;
474 *newRun = *mCurrentRun;
476 // Insert the new run in our linked list.
477 newRun->nextRun = mCurrentRun->nextRun;
478 mCurrentRun->nextRun = newRun;
480 // Adjust runs' text positions and lengths.
481 uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
482 newRun->mTextStart += splitPoint;
483 newRun->mTextLength -= splitPoint;
484 mCurrentRun->mTextLength = splitPoint;
485 mCurrentRun = newRun;
490 // (weak references are fine here, since this class is a transient
491 // stack-based helper that doesn't need to copy data)
492 uint32_t mTextLength;
493 const wchar_t* mText;
494 const wchar_t* mLocaleName;
495 DWRITE_READING_DIRECTION mReadingDirection;
497 // Current processing state.
500 // Output is a list of runs starting here
504 static inline uint16_t hb_uint16_swap (const uint16_t v)
505 { return (v >> 8) | (v << 8); }
506 static inline uint32_t hb_uint32_swap (const uint32_t v)
507 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
514 _hb_directwrite_shape_full (hb_shape_plan_t *shape_plan,
517 const hb_feature_t *features,
518 unsigned int num_features,
521 hb_face_t *face = font->face;
522 const hb_directwrite_face_data_t *face_data = face->data.directwrite;
523 IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
524 IDWriteFontFace *fontFace = face_data->fontFace;
526 IDWriteTextAnalyzer* analyzer;
527 dwriteFactory->CreateTextAnalyzer (&analyzer);
529 unsigned int scratch_size;
530 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
531 #define ALLOCATE_ARRAY(Type, name, len) \
532 Type *name = (Type *) scratch; \
534 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
535 assert (_consumed <= scratch_size); \
536 scratch += _consumed; \
537 scratch_size -= _consumed; \
540 #define utf16_index() var1.u32
542 ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2);
544 unsigned int chars_len = 0;
545 for (unsigned int i = 0; i < buffer->len; i++)
547 hb_codepoint_t c = buffer->info[i].codepoint;
548 buffer->info[i].utf16_index () = chars_len;
549 if (likely (c <= 0xFFFFu))
550 textString[chars_len++] = c;
551 else if (unlikely (c > 0x10FFFFu))
552 textString[chars_len++] = 0xFFFDu;
555 textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
556 textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
560 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
561 /* Need log_clusters to assign features. */
563 for (unsigned int i = 0; i < buffer->len; i++)
565 hb_codepoint_t c = buffer->info[i].codepoint;
566 unsigned int cluster = buffer->info[i].cluster;
567 log_clusters[chars_len++] = cluster;
568 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
569 log_clusters[chars_len++] = cluster; /* Surrogates. */
572 // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
574 DWRITE_READING_DIRECTION readingDirection;
575 readingDirection = buffer->props.direction ?
576 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
577 DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
580 * There's an internal 16-bit limit on some things inside the analyzer,
581 * but we never attempt to shape a word longer than 64K characters
582 * in a single gfxShapedWord, so we cannot exceed that limit.
584 uint32_t textLength = buffer->len;
586 TextAnalysis analysis (textString, textLength, nullptr, readingDirection);
587 TextAnalysis::Run *runHead;
589 hr = analysis.GenerateResults (analyzer, &runHead);
593 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
598 FAIL ("Analyzer failed to generate results.");
600 uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
602 bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
604 const wchar_t localeName[20] = {0};
605 if (buffer->props.language != nullptr)
606 mbstowcs ((wchar_t*) localeName,
607 hb_language_to_string (buffer->props.language), 20);
609 // TODO: it does work but doesn't care about ranges
610 DWRITE_TYPOGRAPHIC_FEATURES typographic_features;
611 typographic_features.featureCount = num_features;
614 typographic_features.features = new DWRITE_FONT_FEATURE[num_features];
615 for (unsigned int i = 0; i < num_features; ++i)
617 typographic_features.features[i].nameTag = (DWRITE_FONT_FEATURE_TAG)
618 hb_uint32_swap (features[i].tag);
619 typographic_features.features[i].parameter = features[i].value;
622 const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures;
623 dwFeatures = (const DWRITE_TYPOGRAPHIC_FEATURES*) &typographic_features;
624 const uint32_t featureRangeLengths[] = { textLength };
627 uint16_t* clusterMap;
628 clusterMap = new uint16_t[textLength];
629 DWRITE_SHAPING_TEXT_PROPERTIES* textProperties;
630 textProperties = new DWRITE_SHAPING_TEXT_PROPERTIES[textLength];
632 uint16_t* glyphIndices = new uint16_t[maxGlyphCount];
633 DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties;
634 glyphProperties = new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount];
636 hr = analyzer->GetGlyphs (textString, textLength, fontFace, false,
637 isRightToLeft, &runHead->mScript, localeName,
638 nullptr, &dwFeatures, featureRangeLengths, 1,
639 maxGlyphCount, clusterMap, textProperties,
640 glyphIndices, glyphProperties, &glyphCount);
642 if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
644 delete [] glyphIndices;
645 delete [] glyphProperties;
649 goto retry_getglyphs;
652 FAIL ("Analyzer failed to get glyphs.");
654 float* glyphAdvances = new float[maxGlyphCount];
655 DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
657 /* The -2 in the following is to compensate for possible
658 * alignment needed after the WORD array. sizeof (WORD) == 2. */
659 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
661 sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) +
663 sizeof (DWRITE_GLYPH_OFFSET) +
665 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
667 #undef ALLOCATE_ARRAY
669 int fontEmSize = font->face->get_upem ();
670 if (fontEmSize < 0) fontEmSize = -fontEmSize;
672 if (fontEmSize < 0) fontEmSize = -fontEmSize;
673 double x_mult = (double) font->x_scale / fontEmSize;
674 double y_mult = (double) font->y_scale / fontEmSize;
676 hr = analyzer->GetGlyphPlacements (textString, clusterMap, textProperties,
677 textLength, glyphIndices, glyphProperties,
678 glyphCount, fontFace, fontEmSize,
679 false, isRightToLeft, &runHead->mScript, localeName,
680 &dwFeatures, featureRangeLengths, 1,
681 glyphAdvances, glyphOffsets);
684 FAIL ("Analyzer failed to get glyph placements.");
686 IDWriteTextAnalyzer1* analyzer1;
687 analyzer->QueryInterface (&analyzer1);
689 if (analyzer1 && lineWidth)
691 DWRITE_JUSTIFICATION_OPPORTUNITY* justificationOpportunities =
692 new DWRITE_JUSTIFICATION_OPPORTUNITY[maxGlyphCount];
693 hr = analyzer1->GetJustificationOpportunities (fontFace, fontEmSize, runHead->mScript,
694 textLength, glyphCount, textString,
695 clusterMap, glyphProperties,
696 justificationOpportunities);
699 FAIL ("Analyzer failed to get justification opportunities.");
701 float* justifiedGlyphAdvances = new float[maxGlyphCount];
702 DWRITE_GLYPH_OFFSET* justifiedGlyphOffsets = new DWRITE_GLYPH_OFFSET[glyphCount];
703 hr = analyzer1->JustifyGlyphAdvances (lineWidth, glyphCount, justificationOpportunities,
704 glyphAdvances, glyphOffsets, justifiedGlyphAdvances,
705 justifiedGlyphOffsets);
707 if (FAILED (hr)) FAIL ("Analyzer failed to get justify glyph advances.");
709 DWRITE_SCRIPT_PROPERTIES scriptProperties;
710 hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
711 if (FAILED (hr)) FAIL ("Analyzer failed to get script properties.");
712 uint32_t justificationCharacter = scriptProperties.justificationCharacter;
714 // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
715 if (justificationCharacter != 32)
717 uint16_t* modifiedClusterMap = new uint16_t[textLength];
718 retry_getjustifiedglyphs:
719 uint16_t* modifiedGlyphIndices = new uint16_t[maxGlyphCount];
720 float* modifiedGlyphAdvances = new float[maxGlyphCount];
721 DWRITE_GLYPH_OFFSET* modifiedGlyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
722 uint32_t actualGlyphsCount;
723 hr = analyzer1->GetJustifiedGlyphs (fontFace, fontEmSize, runHead->mScript,
724 textLength, glyphCount, maxGlyphCount,
725 clusterMap, glyphIndices, glyphAdvances,
726 justifiedGlyphAdvances, justifiedGlyphOffsets,
727 glyphProperties, &actualGlyphsCount,
728 modifiedClusterMap, modifiedGlyphIndices,
729 modifiedGlyphAdvances, modifiedGlyphOffsets);
731 if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
733 maxGlyphCount = actualGlyphsCount;
734 delete [] modifiedGlyphIndices;
735 delete [] modifiedGlyphAdvances;
736 delete [] modifiedGlyphOffsets;
738 maxGlyphCount = actualGlyphsCount;
740 goto retry_getjustifiedglyphs;
743 FAIL ("Analyzer failed to get justified glyphs.");
745 delete [] clusterMap;
746 delete [] glyphIndices;
747 delete [] glyphAdvances;
748 delete [] glyphOffsets;
750 glyphCount = actualGlyphsCount;
751 clusterMap = modifiedClusterMap;
752 glyphIndices = modifiedGlyphIndices;
753 glyphAdvances = modifiedGlyphAdvances;
754 glyphOffsets = modifiedGlyphOffsets;
756 delete [] justifiedGlyphAdvances;
757 delete [] justifiedGlyphOffsets;
761 delete [] glyphAdvances;
762 delete [] glyphOffsets;
764 glyphAdvances = justifiedGlyphAdvances;
765 glyphOffsets = justifiedGlyphOffsets;
768 delete [] justificationOpportunities;
771 /* Ok, we've got everything we need, now compose output buffer,
772 * very, *very*, carefully! */
774 /* Calculate visual-clusters. That's what we ship. */
775 for (unsigned int i = 0; i < glyphCount; i++)
776 vis_clusters[i] = (uint32_t) -1;
777 for (unsigned int i = 0; i < buffer->len; i++)
780 &vis_clusters[log_clusters[buffer->info[i].utf16_index ()]];
781 *p = MIN (*p, buffer->info[i].cluster);
783 for (unsigned int i = 1; i < glyphCount; i++)
784 if (vis_clusters[i] == (uint32_t) -1)
785 vis_clusters[i] = vis_clusters[i - 1];
789 if (unlikely (!buffer->ensure (glyphCount)))
790 FAIL ("Buffer in error");
794 /* Set glyph infos */
796 for (unsigned int i = 0; i < glyphCount; i++)
798 hb_glyph_info_t *info = &buffer->info[buffer->len++];
800 info->codepoint = glyphIndices[i];
801 info->cluster = vis_clusters[i];
803 /* The rest is crap. Let's store position info there for now. */
804 info->mask = glyphAdvances[i];
805 info->var1.i32 = glyphOffsets[i].advanceOffset;
806 info->var2.i32 = glyphOffsets[i].ascenderOffset;
809 /* Set glyph positions */
810 buffer->clear_positions ();
811 for (unsigned int i = 0; i < glyphCount; i++)
813 hb_glyph_info_t *info = &buffer->info[i];
814 hb_glyph_position_t *pos = &buffer->pos[i];
817 pos->x_advance = x_mult * (int32_t) info->mask;
818 pos->x_offset = x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
819 pos->y_offset = y_mult * info->var2.i32;
822 if (isRightToLeft) hb_buffer_reverse (buffer);
824 delete [] clusterMap;
825 delete [] glyphIndices;
826 delete [] textProperties;
827 delete [] glyphProperties;
828 delete [] glyphAdvances;
829 delete [] glyphOffsets;
832 delete [] typographic_features.features;
839 _hb_directwrite_shape (hb_shape_plan_t *shape_plan,
842 const hb_feature_t *features,
843 unsigned int num_features)
845 return _hb_directwrite_shape_full (shape_plan, font, buffer,
846 features, num_features, 0);
850 * Public [experimental] API
854 hb_directwrite_shape_experimental_width (hb_font_t *font,
856 const hb_feature_t *features,
857 unsigned int num_features,
860 static const char *shapers = "directwrite";
861 hb_shape_plan_t *shape_plan;
862 shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
863 features, num_features, &shapers);
864 hb_bool_t res = _hb_directwrite_shape_full (shape_plan, font, buffer,
865 features, num_features, width);
867 buffer->unsafe_to_break_all ();
872 struct _hb_directwrite_font_table_context {
873 IDWriteFontFace *face;
878 _hb_directwrite_table_data_release (void *data)
880 _hb_directwrite_font_table_context *context = (_hb_directwrite_font_table_context *) data;
881 context->face->ReleaseFontTable (context->table_context);
886 reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
888 IDWriteFontFace *dw_face = ((IDWriteFontFace *) user_data);
893 if (!dw_face || FAILED (dw_face->TryGetFontTable (hb_uint32_swap (tag), &data,
894 &length, &table_context, &exists)))
897 if (!data || !exists || !length)
899 dw_face->ReleaseFontTable (table_context);
903 _hb_directwrite_font_table_context *context = new _hb_directwrite_font_table_context;
904 context->face = dw_face;
905 context->table_context = table_context;
907 return hb_blob_create ((const char *) data, length, HB_MEMORY_MODE_READONLY,
908 context, _hb_directwrite_table_data_release);
912 _hb_directwrite_font_release (void *data)
915 ((IDWriteFontFace *) data)->Release ();
919 * hb_directwrite_face_create:
924 hb_directwrite_face_create (IDWriteFontFace *font_face)
927 font_face->AddRef ();
928 return hb_face_create_for_tables (reference_table, font_face,
929 _hb_directwrite_font_release);