2 * Copyright © 2015-2016 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.
25 #include "hb-private.hh"
26 #include "hb-debug.hh"
27 #define HB_SHAPER directwrite
28 #include "hb-shaper-impl-private.hh"
32 #include "hb-directwrite.h"
35 HB_SHAPER_DATA_ENSURE_DEFINE(directwrite, face)
36 HB_SHAPER_DATA_ENSURE_DEFINE(directwrite, font)
40 * DirectWrite font stream helpers
43 // This is a font loader which provides only one font (unlike its original design).
44 // For a better implementation which was also source of this
45 // and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
46 class DWriteFontFileLoader : public IDWriteFontFileLoader
49 IDWriteFontFileStream *mFontFileStream;
51 DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream) {
52 mFontFileStream = fontFileStream;
56 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
57 IFACEMETHOD_(ULONG, AddRef)() { return 1; }
58 IFACEMETHOD_(ULONG, Release)() { return 1; }
60 // IDWriteFontFileLoader methods
61 virtual HRESULT STDMETHODCALLTYPE CreateStreamFromKey(void const* fontFileReferenceKey,
62 UINT32 fontFileReferenceKeySize,
63 OUT IDWriteFontFileStream** fontFileStream)
65 *fontFileStream = mFontFileStream;
70 class DWriteFontFileStream : public IDWriteFontFileStream
76 DWriteFontFileStream(uint8_t *aData, uint32_t aSize)
83 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
84 IFACEMETHOD_(ULONG, AddRef)() { return 1; }
85 IFACEMETHOD_(ULONG, Release)() { return 1; }
87 // IDWriteFontFileStream methods
88 virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart,
91 OUT void** fragmentContext)
93 // We are required to do bounds checking.
94 if (fileOffset + fragmentSize > mSize) {
98 // truncate the 64 bit fileOffset to size_t sized index into mData
99 size_t index = static_cast<size_t> (fileOffset);
101 // We should be alive for the duration of this.
102 *fragmentStart = &mData[index];
103 *fragmentContext = nullptr;
107 virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext) { }
109 virtual HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64* fileSize)
115 virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime)
126 struct hb_directwrite_shaper_face_data_t {
127 IDWriteFactory *dwriteFactory;
128 IDWriteFontFile *fontFile;
129 IDWriteFontFileStream *fontFileStream;
130 IDWriteFontFileLoader *fontFileLoader;
131 IDWriteFontFace *fontFace;
135 hb_directwrite_shaper_face_data_t *
136 _hb_directwrite_shaper_face_data_create(hb_face_t *face)
138 hb_directwrite_shaper_face_data_t *data =
139 (hb_directwrite_shaper_face_data_t *) malloc (sizeof (hb_directwrite_shaper_face_data_t));
140 if (unlikely (!data))
143 // TODO: factory and fontFileLoader should be cached separately
144 IDWriteFactory* dwriteFactory;
145 DWriteCreateFactory (
146 DWRITE_FACTORY_TYPE_SHARED,
147 __uuidof (IDWriteFactory),
148 (IUnknown**) &dwriteFactory
152 hb_blob_t *blob = hb_face_reference_blob (face);
153 IDWriteFontFileStream *fontFileStream = new DWriteFontFileStream (
154 (uint8_t*) hb_blob_get_data (blob, nullptr), hb_blob_get_length (blob));
156 IDWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
157 dwriteFactory->RegisterFontFileLoader (fontFileLoader);
159 IDWriteFontFile *fontFile;
160 uint64_t fontFileKey = 0;
161 hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
162 fontFileLoader, &fontFile);
166 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
171 FAIL ("Failed to load font file from data!");
176 DWRITE_FONT_FILE_TYPE fileType;
177 DWRITE_FONT_FACE_TYPE faceType;
178 UINT32 numberOfFaces;
179 hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
180 if (FAILED (hr) || !isSupported) {
181 FAIL ("Font file is not supported.");
187 IDWriteFontFace *fontFace;
188 dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
189 DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
191 data->dwriteFactory = dwriteFactory;
192 data->fontFile = fontFile;
193 data->fontFileStream = fontFileStream;
194 data->fontFileLoader = fontFileLoader;
195 data->fontFace = fontFace;
196 data->faceBlob = blob;
202 _hb_directwrite_shaper_face_data_destroy(hb_directwrite_shaper_face_data_t *data)
205 data->fontFace->Release ();
207 data->fontFile->Release ();
208 if (data->dwriteFactory) {
209 if (data->fontFileLoader)
210 data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
211 data->dwriteFactory->Release ();
213 if (data->fontFileLoader)
214 delete data->fontFileLoader;
215 if (data->fontFileStream)
216 delete data->fontFileStream;
218 hb_blob_destroy (data->faceBlob);
228 struct hb_directwrite_shaper_font_data_t {
231 hb_directwrite_shaper_font_data_t *
232 _hb_directwrite_shaper_font_data_create (hb_font_t *font)
234 if (unlikely (!hb_directwrite_shaper_face_data_ensure (font->face))) return nullptr;
236 hb_directwrite_shaper_font_data_t *data =
237 (hb_directwrite_shaper_font_data_t *) malloc (sizeof (hb_directwrite_shaper_font_data_t));
238 if (unlikely (!data))
245 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_shaper_font_data_t *data)
252 * shaper shape_plan data
255 struct hb_directwrite_shaper_shape_plan_data_t {};
257 hb_directwrite_shaper_shape_plan_data_t *
258 _hb_directwrite_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
259 const hb_feature_t *user_features HB_UNUSED,
260 unsigned int num_user_features HB_UNUSED,
261 const int *coords HB_UNUSED,
262 unsigned int num_coords HB_UNUSED)
264 return (hb_directwrite_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
268 _hb_directwrite_shaper_shape_plan_data_destroy (hb_directwrite_shaper_shape_plan_data_t *data HB_UNUSED)
272 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
273 // but now is relicensed to MIT for HarfBuzz use
275 : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
279 IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
280 IFACEMETHOD_(ULONG, AddRef)() { return 1; }
281 IFACEMETHOD_(ULONG, Release)() { return 1; }
283 // A single contiguous run of characters containing the same analysis
287 uint32_t mTextStart; // starting text position of this run
288 uint32_t mTextLength; // number of contiguous code units covered
289 uint32_t mGlyphStart; // starting glyph in the glyphs array
290 uint32_t mGlyphCount; // number of glyphs associated with this run of
292 DWRITE_SCRIPT_ANALYSIS mScript;
296 inline bool ContainsTextPosition(uint32_t aTextPosition) const
298 return aTextPosition >= mTextStart
299 && aTextPosition < mTextStart + mTextLength;
306 TextAnalysis(const wchar_t* text,
308 const wchar_t* localeName,
309 DWRITE_READING_DIRECTION readingDirection)
311 , mTextLength(textLength)
312 , mLocaleName(localeName)
313 , mReadingDirection(readingDirection)
314 , mCurrentRun(nullptr) { };
317 // delete runs, except mRunHead which is part of the TextAnalysis object
318 for (Run *run = mRunHead.nextRun; run;) {
325 STDMETHODIMP GenerateResults(IDWriteTextAnalyzer* textAnalyzer,
327 // Analyzes the text using the script analyzer and returns
328 // the result as a series of runs.
332 // Initially start out with one result that covers the entire range.
333 // This result will be subdivided by the analysis processes.
334 mRunHead.mTextStart = 0;
335 mRunHead.mTextLength = mTextLength;
336 mRunHead.mBidiLevel =
337 (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
338 mRunHead.nextRun = nullptr;
339 mCurrentRun = &mRunHead;
341 // Call each of the analyzers in sequence, recording their results.
342 if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this))) {
343 *runHead = &mRunHead;
349 // IDWriteTextAnalysisSource implementation
351 IFACEMETHODIMP GetTextAtPosition(uint32_t textPosition,
352 OUT wchar_t const** textString,
353 OUT uint32_t* textLength)
355 if (textPosition >= mTextLength) {
356 // No text at this position, valid query though.
357 *textString = nullptr;
361 *textString = mText + textPosition;
362 *textLength = mTextLength - textPosition;
367 IFACEMETHODIMP GetTextBeforePosition(uint32_t textPosition,
368 OUT wchar_t const** textString,
369 OUT uint32_t* textLength)
371 if (textPosition == 0 || textPosition > mTextLength) {
372 // Either there is no text before here (== 0), or this
373 // is an invalid position. The query is considered valid though.
374 *textString = nullptr;
379 *textLength = textPosition;
384 IFACEMETHODIMP_(DWRITE_READING_DIRECTION)
385 GetParagraphReadingDirection() { return mReadingDirection; }
387 IFACEMETHODIMP GetLocaleName(uint32_t textPosition,
388 uint32_t* textLength,
389 wchar_t const** localeName)
395 GetNumberSubstitution(uint32_t textPosition,
396 OUT uint32_t* textLength,
397 OUT IDWriteNumberSubstitution** numberSubstitution)
399 // We do not support number substitution.
400 *numberSubstitution = nullptr;
401 *textLength = mTextLength - textPosition;
406 // IDWriteTextAnalysisSink implementation
409 SetScriptAnalysis(uint32_t textPosition,
411 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
413 SetCurrentRun(textPosition);
414 SplitCurrentRun(textPosition);
415 while (textLength > 0)
417 Run *run = FetchNextRun(&textLength);
418 run->mScript = *scriptAnalysis;
425 SetLineBreakpoints(uint32_t textPosition,
427 const DWRITE_LINE_BREAKPOINT* lineBreakpoints) { return S_OK; }
429 IFACEMETHODIMP SetBidiLevel(uint32_t textPosition,
431 uint8_t explicitLevel,
432 uint8_t resolvedLevel) { return S_OK; }
435 SetNumberSubstitution(uint32_t textPosition,
437 IDWriteNumberSubstitution* numberSubstitution) { return S_OK; }
440 Run *FetchNextRun(IN OUT uint32_t* textLength)
442 // Used by the sink setters, this returns a reference to the next run.
443 // Position and length are adjusted to now point after the current run
446 Run *origRun = mCurrentRun;
447 // Split the tail if needed (the length remaining is less than the
448 // current run's size).
449 if (*textLength < mCurrentRun->mTextLength)
451 SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
455 // Just advance the current run.
456 mCurrentRun = mCurrentRun->nextRun;
458 *textLength -= origRun->mTextLength;
460 // Return a reference to the run that was just current.
464 void SetCurrentRun(uint32_t textPosition)
466 // Move the current run to the given position.
467 // Since the analyzers generally return results in a forward manner,
468 // this will usually just return early. If not, find the
469 // corresponding run for the text position.
471 if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
476 for (Run *run = &mRunHead; run; run = run->nextRun) {
477 if (run->ContainsTextPosition (textPosition))
483 //NS_NOTREACHED("We should always be able to find the text position in one \
487 void SplitCurrentRun(uint32_t splitPosition)
491 //NS_ASSERTION(false, "SplitCurrentRun called without current run.");
492 // Shouldn't be calling this when no current run is set!
495 // Split the current run.
496 if (splitPosition <= mCurrentRun->mTextStart)
498 // No need to split, already the start of a run
499 // or before it. Usually the first.
502 Run *newRun = (Run*) malloc (sizeof (Run));
504 *newRun = *mCurrentRun;
506 // Insert the new run in our linked list.
507 newRun->nextRun = mCurrentRun->nextRun;
508 mCurrentRun->nextRun = newRun;
510 // Adjust runs' text positions and lengths.
511 uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
512 newRun->mTextStart += splitPoint;
513 newRun->mTextLength -= splitPoint;
514 mCurrentRun->mTextLength = splitPoint;
515 mCurrentRun = newRun;
520 // (weak references are fine here, since this class is a transient
521 // stack-based helper that doesn't need to copy data)
522 uint32_t mTextLength;
523 const wchar_t* mText;
524 const wchar_t* mLocaleName;
525 DWRITE_READING_DIRECTION mReadingDirection;
527 // Current processing state.
530 // Output is a list of runs starting here
534 static inline uint16_t hb_uint16_swap (const uint16_t v)
535 { return (v >> 8) | (v << 8); }
536 static inline uint32_t hb_uint32_swap (const uint32_t v)
537 { return (hb_uint16_swap(v) << 16) | hb_uint16_swap(v >> 16); }
544 _hb_directwrite_shape_full(hb_shape_plan_t *shape_plan,
547 const hb_feature_t *features,
548 unsigned int num_features,
551 hb_face_t *face = font->face;
552 hb_directwrite_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
553 hb_directwrite_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
554 IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
555 IDWriteFontFace *fontFace = face_data->fontFace;
557 IDWriteTextAnalyzer* analyzer;
558 dwriteFactory->CreateTextAnalyzer(&analyzer);
560 unsigned int scratch_size;
561 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
562 #define ALLOCATE_ARRAY(Type, name, len) \
563 Type *name = (Type *) scratch; \
565 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
566 assert (_consumed <= scratch_size); \
567 scratch += _consumed; \
568 scratch_size -= _consumed; \
571 #define utf16_index() var1.u32
573 ALLOCATE_ARRAY(wchar_t, textString, buffer->len * 2);
575 unsigned int chars_len = 0;
576 for (unsigned int i = 0; i < buffer->len; i++)
578 hb_codepoint_t c = buffer->info[i].codepoint;
579 buffer->info[i].utf16_index() = chars_len;
580 if (likely(c <= 0xFFFFu))
581 textString[chars_len++] = c;
582 else if (unlikely(c > 0x10FFFFu))
583 textString[chars_len++] = 0xFFFDu;
585 textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
586 textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
590 ALLOCATE_ARRAY(WORD, log_clusters, chars_len);
593 /* Need log_clusters to assign features. */
595 for (unsigned int i = 0; i < buffer->len; i++)
597 hb_codepoint_t c = buffer->info[i].codepoint;
598 unsigned int cluster = buffer->info[i].cluster;
599 log_clusters[chars_len++] = cluster;
600 if (hb_in_range(c, 0x10000u, 0x10FFFFu))
601 log_clusters[chars_len++] = cluster; /* Surrogates. */
605 // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
607 DWRITE_READING_DIRECTION readingDirection = buffer->props.direction ?
608 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
609 DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
612 * There's an internal 16-bit limit on some things inside the analyzer,
613 * but we never attempt to shape a word longer than 64K characters
614 * in a single gfxShapedWord, so we cannot exceed that limit.
616 uint32_t textLength = buffer->len;
618 TextAnalysis analysis(textString, textLength, nullptr, readingDirection);
619 TextAnalysis::Run *runHead;
621 hr = analysis.GenerateResults(analyzer, &runHead);
625 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
631 FAIL ("Analyzer failed to generate results.");
635 uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
637 bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
639 const wchar_t localeName[20] = {0};
640 if (buffer->props.language != nullptr)
642 mbstowcs ((wchar_t*) localeName,
643 hb_language_to_string (buffer->props.language), 20);
646 DWRITE_TYPOGRAPHIC_FEATURES singleFeatures;
647 singleFeatures.featureCount = num_features;
650 DWRITE_FONT_FEATURE* dwfeatureArray = (DWRITE_FONT_FEATURE*)
651 malloc (sizeof (DWRITE_FONT_FEATURE) * num_features);
652 for (unsigned int i = 0; i < num_features; ++i)
654 dwfeatureArray[i].nameTag = (DWRITE_FONT_FEATURE_TAG)
655 hb_uint32_swap (features[i].tag);
656 dwfeatureArray[i].parameter = features[i].value;
658 singleFeatures.features = dwfeatureArray;
660 const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures =
661 (const DWRITE_TYPOGRAPHIC_FEATURES*) &singleFeatures;
662 const uint32_t featureRangeLengths[] = { textLength };
664 uint16_t* clusterMap = (uint16_t*) malloc (textLength * sizeof (uint16_t));
665 DWRITE_SHAPING_TEXT_PROPERTIES* textProperties = (DWRITE_SHAPING_TEXT_PROPERTIES*)
666 malloc (textLength * sizeof (DWRITE_SHAPING_TEXT_PROPERTIES));
668 uint16_t* glyphIndices = (uint16_t*) malloc (maxGlyphCount * sizeof (uint16_t));
669 DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties = (DWRITE_SHAPING_GLYPH_PROPERTIES*)
670 malloc (maxGlyphCount * sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES));
672 hr = analyzer->GetGlyphs (textString, textLength, fontFace, false,
673 isRightToLeft, &runHead->mScript, localeName, nullptr, &dwFeatures,
674 featureRangeLengths, 1, maxGlyphCount, clusterMap, textProperties, glyphIndices,
675 glyphProperties, &glyphCount);
677 if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
680 free (glyphProperties);
684 goto retry_getglyphs;
688 FAIL ("Analyzer failed to get glyphs.");
692 float* glyphAdvances = (float*) malloc (maxGlyphCount * sizeof (float));
693 DWRITE_GLYPH_OFFSET* glyphOffsets = (DWRITE_GLYPH_OFFSET*)
694 malloc(maxGlyphCount * sizeof (DWRITE_GLYPH_OFFSET));
696 /* The -2 in the following is to compensate for possible
697 * alignment needed after the WORD array. sizeof(WORD) == 2. */
698 unsigned int glyphs_size = (scratch_size * sizeof(int) - 2)
700 sizeof(DWRITE_SHAPING_GLYPH_PROPERTIES) +
702 sizeof(DWRITE_GLYPH_OFFSET) +
704 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
706 #undef ALLOCATE_ARRAY
708 int fontEmSize = font->face->get_upem();
710 fontEmSize = -fontEmSize;
713 fontEmSize = -fontEmSize;
714 double x_mult = (double) font->x_scale / fontEmSize;
715 double y_mult = (double) font->y_scale / fontEmSize;
717 hr = analyzer->GetGlyphPlacements (textString,
718 clusterMap, textProperties, textLength, glyphIndices,
719 glyphProperties, glyphCount, fontFace, fontEmSize,
720 false, isRightToLeft, &runHead->mScript, localeName,
721 &dwFeatures, featureRangeLengths, 1,
722 glyphAdvances, glyphOffsets);
726 FAIL ("Analyzer failed to get glyph placements.");
730 IDWriteTextAnalyzer1* analyzer1;
731 analyzer->QueryInterface (&analyzer1);
733 if (analyzer1 && lineWidth)
736 DWRITE_JUSTIFICATION_OPPORTUNITY* justificationOpportunities =
737 (DWRITE_JUSTIFICATION_OPPORTUNITY*)
738 malloc (maxGlyphCount * sizeof (DWRITE_JUSTIFICATION_OPPORTUNITY));
739 hr = analyzer1->GetJustificationOpportunities (fontFace, fontEmSize,
740 runHead->mScript, textLength, glyphCount, textString, clusterMap,
741 glyphProperties, justificationOpportunities);
745 FAIL ("Analyzer failed to get justification opportunities.");
749 float* justifiedGlyphAdvances =
750 (float*) malloc (maxGlyphCount * sizeof (float));
751 DWRITE_GLYPH_OFFSET* justifiedGlyphOffsets = (DWRITE_GLYPH_OFFSET*)
752 malloc (glyphCount * sizeof (DWRITE_GLYPH_OFFSET));
753 hr = analyzer1->JustifyGlyphAdvances (lineWidth, glyphCount, justificationOpportunities,
754 glyphAdvances, glyphOffsets, justifiedGlyphAdvances, justifiedGlyphOffsets);
758 FAIL("Analyzer failed to get justified glyph advances.");
762 DWRITE_SCRIPT_PROPERTIES scriptProperties;
763 hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
766 FAIL("Analyzer failed to get script properties.");
769 uint32_t justificationCharacter = scriptProperties.justificationCharacter;
771 // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
772 if (justificationCharacter != 32)
774 uint16_t* modifiedClusterMap = (uint16_t*) malloc (textLength * sizeof (uint16_t));
775 retry_getjustifiedglyphs:
776 uint16_t* modifiedGlyphIndices = (uint16_t*) malloc (maxGlyphCount * sizeof (uint16_t));
777 float* modifiedGlyphAdvances = (float*) malloc (maxGlyphCount * sizeof (float));
778 DWRITE_GLYPH_OFFSET* modifiedGlyphOffsets = (DWRITE_GLYPH_OFFSET*)
779 malloc (maxGlyphCount * sizeof (DWRITE_GLYPH_OFFSET));
780 uint32_t actualGlyphsCount;
781 hr = analyzer1->GetJustifiedGlyphs (fontFace, fontEmSize, runHead->mScript,
782 textLength, glyphCount, maxGlyphCount, clusterMap, glyphIndices,
783 glyphAdvances, justifiedGlyphAdvances, justifiedGlyphOffsets,
784 glyphProperties, &actualGlyphsCount, modifiedClusterMap, modifiedGlyphIndices,
785 modifiedGlyphAdvances, modifiedGlyphOffsets);
787 if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
789 maxGlyphCount = actualGlyphsCount;
790 free (modifiedGlyphIndices);
791 free (modifiedGlyphAdvances);
792 free (modifiedGlyphOffsets);
794 maxGlyphCount = actualGlyphsCount;
796 goto retry_getjustifiedglyphs;
800 FAIL ("Analyzer failed to get justified glyphs.");
806 free (glyphAdvances);
809 glyphCount = actualGlyphsCount;
810 clusterMap = modifiedClusterMap;
811 glyphIndices = modifiedGlyphIndices;
812 glyphAdvances = modifiedGlyphAdvances;
813 glyphOffsets = modifiedGlyphOffsets;
815 free (justifiedGlyphAdvances);
816 free (justifiedGlyphOffsets);
820 free (glyphAdvances);
823 glyphAdvances = justifiedGlyphAdvances;
824 glyphOffsets = justifiedGlyphOffsets;
827 free (justificationOpportunities);
831 /* Ok, we've got everything we need, now compose output buffer,
832 * very, *very*, carefully! */
834 /* Calculate visual-clusters. That's what we ship. */
835 for (unsigned int i = 0; i < glyphCount; i++)
836 vis_clusters[i] = -1;
837 for (unsigned int i = 0; i < buffer->len; i++)
840 &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
841 *p = MIN (*p, buffer->info[i].cluster);
843 for (unsigned int i = 1; i < glyphCount; i++)
844 if (vis_clusters[i] == -1)
845 vis_clusters[i] = vis_clusters[i - 1];
849 if (unlikely (!buffer->ensure (glyphCount)))
850 FAIL ("Buffer in error");
854 /* Set glyph infos */
856 for (unsigned int i = 0; i < glyphCount; i++)
858 hb_glyph_info_t *info = &buffer->info[buffer->len++];
860 info->codepoint = glyphIndices[i];
861 info->cluster = vis_clusters[i];
863 /* The rest is crap. Let's store position info there for now. */
864 info->mask = glyphAdvances[i];
865 info->var1.i32 = glyphOffsets[i].advanceOffset;
866 info->var2.i32 = glyphOffsets[i].ascenderOffset;
869 /* Set glyph positions */
870 buffer->clear_positions ();
871 for (unsigned int i = 0; i < glyphCount; i++)
873 hb_glyph_info_t *info = &buffer->info[i];
874 hb_glyph_position_t *pos = &buffer->pos[i];
877 pos->x_advance = x_mult * (int32_t) info->mask;
879 x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
880 pos->y_offset = y_mult * info->var2.i32;
884 hb_buffer_reverse (buffer);
888 free (textProperties);
889 free (glyphProperties);
890 free (glyphAdvances);
894 free (singleFeatures.features);
901 _hb_directwrite_shape(hb_shape_plan_t *shape_plan,
904 const hb_feature_t *features,
905 unsigned int num_features)
907 return _hb_directwrite_shape_full(shape_plan, font, buffer,
908 features, num_features, 0);
912 * Public [experimental] API
916 hb_directwrite_shape_experimental_width(hb_font_t *font,
918 const hb_feature_t *features,
919 unsigned int num_features,
922 static char *shapers = "directwrite";
923 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face,
924 &buffer->props, features, num_features, &shapers);
925 hb_bool_t res = _hb_directwrite_shape_full (shape_plan, font, buffer,
926 features, num_features, width);
928 buffer->unsafe_to_break_all ();