2 * Copyright © 2015-2018 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 * hb-directwrite uses new/delete syntatically but as we let users
41 * to override malloc/free, we will redefine new/delete so users
42 * won't need to do that by their own.
44 void* operator new (size_t size) { return malloc (size); }
45 void* operator new [] (size_t size) { return malloc (size); }
46 void operator delete (void* pointer) { free (pointer); }
47 void operator delete [] (void* pointer) { free (pointer); }
51 * DirectWrite font stream helpers
54 // This is a font loader which provides only one font (unlike its original design).
55 // For a better implementation which was also source of this
56 // and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
57 class DWriteFontFileLoader : public IDWriteFontFileLoader
60 IDWriteFontFileStream *mFontFileStream;
62 DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream)
64 mFontFileStream = fontFileStream;
68 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
69 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
70 IFACEMETHOD_ (ULONG, Release) () { return 1; }
72 // IDWriteFontFileLoader methods
73 virtual HRESULT STDMETHODCALLTYPE CreateStreamFromKey (void const* fontFileReferenceKey,
74 uint32_t fontFileReferenceKeySize,
75 OUT IDWriteFontFileStream** fontFileStream)
77 *fontFileStream = mFontFileStream;
82 class DWriteFontFileStream : public IDWriteFontFileStream
88 DWriteFontFileStream (uint8_t *aData, uint32_t aSize)
95 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
96 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
97 IFACEMETHOD_ (ULONG, Release) () { return 1; }
99 // IDWriteFontFileStream methods
100 virtual HRESULT STDMETHODCALLTYPE ReadFileFragment (void const** fragmentStart,
103 OUT void** fragmentContext)
105 // We are required to do bounds checking.
106 if (fileOffset + fragmentSize > mSize)
109 // truncate the 64 bit fileOffset to size_t sized index into mData
110 size_t index = static_cast<size_t> (fileOffset);
112 // We should be alive for the duration of this.
113 *fragmentStart = &mData[index];
114 *fragmentContext = nullptr;
118 virtual void STDMETHODCALLTYPE ReleaseFileFragment (void* fragmentContext) { }
120 virtual HRESULT STDMETHODCALLTYPE GetFileSize (OUT UINT64* fileSize)
126 virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime (OUT UINT64* lastWriteTime)
137 struct hb_directwrite_shaper_face_data_t
139 IDWriteFactory *dwriteFactory;
140 IDWriteFontFile *fontFile;
141 IDWriteFontFileStream *fontFileStream;
142 IDWriteFontFileLoader *fontFileLoader;
143 IDWriteFontFace *fontFace;
147 hb_directwrite_shaper_face_data_t *
148 _hb_directwrite_shaper_face_data_create (hb_face_t *face)
150 hb_directwrite_shaper_face_data_t *data = new hb_directwrite_shaper_face_data_t;
151 if (unlikely (!data))
154 // TODO: factory and fontFileLoader should be cached separately
155 IDWriteFactory* dwriteFactory;
156 DWriteCreateFactory (
157 DWRITE_FACTORY_TYPE_SHARED,
158 __uuidof (IDWriteFactory),
159 (IUnknown**) &dwriteFactory
163 hb_blob_t *blob = hb_face_reference_blob (face);
164 DWriteFontFileStream *fontFileStream = new DWriteFontFileStream (
165 (uint8_t *) hb_blob_get_data (blob, nullptr),
166 hb_blob_get_length (blob));
168 DWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
169 dwriteFactory->RegisterFontFileLoader (fontFileLoader);
171 IDWriteFontFile *fontFile;
172 uint64_t fontFileKey = 0;
173 hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
174 fontFileLoader, &fontFile);
178 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
183 FAIL ("Failed to load font file from data!");
186 DWRITE_FONT_FILE_TYPE fileType;
187 DWRITE_FONT_FACE_TYPE faceType;
188 uint32_t numberOfFaces;
189 hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
190 if (FAILED (hr) || !isSupported)
191 FAIL ("Font file is not supported.");
195 IDWriteFontFace *fontFace;
196 dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
197 DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
199 data->dwriteFactory = dwriteFactory;
200 data->fontFile = fontFile;
201 data->fontFileStream = fontFileStream;
202 data->fontFileLoader = fontFileLoader;
203 data->fontFace = fontFace;
204 data->faceBlob = blob;
210 _hb_directwrite_shaper_face_data_destroy (hb_directwrite_shaper_face_data_t *data)
213 data->fontFace->Release ();
215 data->fontFile->Release ();
216 if (data->dwriteFactory)
218 if (data->fontFileLoader)
219 data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
220 data->dwriteFactory->Release ();
222 if (data->fontFileLoader)
223 delete data->fontFileLoader;
224 if (data->fontFileStream)
225 delete data->fontFileStream;
227 hb_blob_destroy (data->faceBlob);
237 struct hb_directwrite_shaper_font_data_t
241 hb_directwrite_shaper_font_data_t *
242 _hb_directwrite_shaper_font_data_create (hb_font_t *font)
244 if (unlikely (!hb_directwrite_shaper_face_data_ensure (font->face))) return nullptr;
246 hb_directwrite_shaper_font_data_t *data = new hb_directwrite_shaper_font_data_t;
247 if (unlikely (!data))
254 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_shaper_font_data_t *data)
261 * shaper shape_plan data
264 struct hb_directwrite_shaper_shape_plan_data_t {};
266 hb_directwrite_shaper_shape_plan_data_t *
267 _hb_directwrite_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
268 const hb_feature_t *user_features HB_UNUSED,
269 unsigned int num_user_features HB_UNUSED,
270 const int *coords HB_UNUSED,
271 unsigned int num_coords HB_UNUSED)
273 return (hb_directwrite_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
277 _hb_directwrite_shaper_shape_plan_data_destroy (hb_directwrite_shaper_shape_plan_data_t *data HB_UNUSED)
281 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
282 // but now is relicensed to MIT for HarfBuzz use
284 : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
288 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
289 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
290 IFACEMETHOD_ (ULONG, Release) () { return 1; }
292 // A single contiguous run of characters containing the same analysis
296 uint32_t mTextStart; // starting text position of this run
297 uint32_t mTextLength; // number of contiguous code units covered
298 uint32_t mGlyphStart; // starting glyph in the glyphs array
299 uint32_t mGlyphCount; // number of glyphs associated with this run
301 DWRITE_SCRIPT_ANALYSIS mScript;
305 inline bool ContainsTextPosition (uint32_t aTextPosition) const
307 return aTextPosition >= mTextStart &&
308 aTextPosition < mTextStart + mTextLength;
315 TextAnalysis (const wchar_t* text,
317 const wchar_t* localeName,
318 DWRITE_READING_DIRECTION readingDirection)
320 , mTextLength (textLength)
321 , mLocaleName (localeName)
322 , mReadingDirection (readingDirection)
323 , mCurrentRun (nullptr) { };
327 // delete runs, except mRunHead which is part of the TextAnalysis object
328 for (Run *run = mRunHead.nextRun; run;)
336 STDMETHODIMP GenerateResults (IDWriteTextAnalyzer* textAnalyzer,
339 // Analyzes the text using the script analyzer and returns
340 // the result as a series of runs.
344 // Initially start out with one result that covers the entire range.
345 // This result will be subdivided by the analysis processes.
346 mRunHead.mTextStart = 0;
347 mRunHead.mTextLength = mTextLength;
348 mRunHead.mBidiLevel =
349 (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
350 mRunHead.nextRun = nullptr;
351 mCurrentRun = &mRunHead;
353 // Call each of the analyzers in sequence, recording their results.
354 if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this)))
355 *runHead = &mRunHead;
360 // IDWriteTextAnalysisSource implementation
362 IFACEMETHODIMP GetTextAtPosition (uint32_t textPosition,
363 OUT wchar_t const** textString,
364 OUT uint32_t* textLength)
366 if (textPosition >= mTextLength)
368 // No text at this position, valid query though.
369 *textString = nullptr;
374 *textString = mText + textPosition;
375 *textLength = mTextLength - textPosition;
380 IFACEMETHODIMP GetTextBeforePosition (uint32_t textPosition,
381 OUT wchar_t const** textString,
382 OUT uint32_t* textLength)
384 if (textPosition == 0 || textPosition > mTextLength)
386 // Either there is no text before here (== 0), or this
387 // is an invalid position. The query is considered valid though.
388 *textString = nullptr;
394 *textLength = textPosition;
399 IFACEMETHODIMP_ (DWRITE_READING_DIRECTION)
400 GetParagraphReadingDirection () { return mReadingDirection; }
402 IFACEMETHODIMP GetLocaleName (uint32_t textPosition,
403 uint32_t* textLength,
404 wchar_t const** localeName)
410 GetNumberSubstitution (uint32_t textPosition,
411 OUT uint32_t* textLength,
412 OUT IDWriteNumberSubstitution** numberSubstitution)
414 // We do not support number substitution.
415 *numberSubstitution = nullptr;
416 *textLength = mTextLength - textPosition;
421 // IDWriteTextAnalysisSink implementation
424 SetScriptAnalysis (uint32_t textPosition,
426 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
428 SetCurrentRun (textPosition);
429 SplitCurrentRun (textPosition);
430 while (textLength > 0)
432 Run *run = FetchNextRun (&textLength);
433 run->mScript = *scriptAnalysis;
440 SetLineBreakpoints (uint32_t textPosition,
442 const DWRITE_LINE_BREAKPOINT* lineBreakpoints) { return S_OK; }
444 IFACEMETHODIMP SetBidiLevel (uint32_t textPosition,
446 uint8_t explicitLevel,
447 uint8_t resolvedLevel) { return S_OK; }
450 SetNumberSubstitution (uint32_t textPosition,
452 IDWriteNumberSubstitution* numberSubstitution) { return S_OK; }
455 Run *FetchNextRun (IN OUT uint32_t* textLength)
457 // Used by the sink setters, this returns a reference to the next run.
458 // Position and length are adjusted to now point after the current run
461 Run *origRun = mCurrentRun;
462 // Split the tail if needed (the length remaining is less than the
463 // current run's size).
464 if (*textLength < mCurrentRun->mTextLength)
465 SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
467 // Just advance the current run.
468 mCurrentRun = mCurrentRun->nextRun;
469 *textLength -= origRun->mTextLength;
471 // Return a reference to the run that was just current.
475 void SetCurrentRun (uint32_t textPosition)
477 // Move the current run to the given position.
478 // Since the analyzers generally return results in a forward manner,
479 // this will usually just return early. If not, find the
480 // corresponding run for the text position.
482 if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
485 for (Run *run = &mRunHead; run; run = run->nextRun)
486 if (run->ContainsTextPosition (textPosition))
491 assert (0); // We should always be able to find the text position in one of our runs
494 void SplitCurrentRun (uint32_t splitPosition)
498 assert (0); // SplitCurrentRun called without current run
499 // Shouldn't be calling this when no current run is set!
502 // Split the current run.
503 if (splitPosition <= mCurrentRun->mTextStart)
505 // No need to split, already the start of a run
506 // or before it. Usually the first.
509 Run *newRun = new Run;
511 *newRun = *mCurrentRun;
513 // Insert the new run in our linked list.
514 newRun->nextRun = mCurrentRun->nextRun;
515 mCurrentRun->nextRun = newRun;
517 // Adjust runs' text positions and lengths.
518 uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
519 newRun->mTextStart += splitPoint;
520 newRun->mTextLength -= splitPoint;
521 mCurrentRun->mTextLength = splitPoint;
522 mCurrentRun = newRun;
527 // (weak references are fine here, since this class is a transient
528 // stack-based helper that doesn't need to copy data)
529 uint32_t mTextLength;
530 const wchar_t* mText;
531 const wchar_t* mLocaleName;
532 DWRITE_READING_DIRECTION mReadingDirection;
534 // Current processing state.
537 // Output is a list of runs starting here
541 static inline uint16_t hb_uint16_swap (const uint16_t v)
542 { return (v >> 8) | (v << 8); }
543 static inline uint32_t hb_uint32_swap (const uint32_t v)
544 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
551 _hb_directwrite_shape_full (hb_shape_plan_t *shape_plan,
554 const hb_feature_t *features,
555 unsigned int num_features,
558 hb_face_t *face = font->face;
559 hb_directwrite_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
560 hb_directwrite_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
561 IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
562 IDWriteFontFace *fontFace = face_data->fontFace;
564 IDWriteTextAnalyzer* analyzer;
565 dwriteFactory->CreateTextAnalyzer (&analyzer);
567 unsigned int scratch_size;
568 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
569 #define ALLOCATE_ARRAY(Type, name, len) \
570 Type *name = (Type *) scratch; \
572 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
573 assert (_consumed <= scratch_size); \
574 scratch += _consumed; \
575 scratch_size -= _consumed; \
578 #define utf16_index() var1.u32
580 ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2);
582 unsigned int chars_len = 0;
583 for (unsigned int i = 0; i < buffer->len; i++)
585 hb_codepoint_t c = buffer->info[i].codepoint;
586 buffer->info[i].utf16_index () = chars_len;
587 if (likely (c <= 0xFFFFu))
588 textString[chars_len++] = c;
589 else if (unlikely (c > 0x10FFFFu))
590 textString[chars_len++] = 0xFFFDu;
593 textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
594 textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
598 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
599 /* Need log_clusters to assign features. */
601 for (unsigned int i = 0; i < buffer->len; i++)
603 hb_codepoint_t c = buffer->info[i].codepoint;
604 unsigned int cluster = buffer->info[i].cluster;
605 log_clusters[chars_len++] = cluster;
606 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
607 log_clusters[chars_len++] = cluster; /* Surrogates. */
610 // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
612 DWRITE_READING_DIRECTION readingDirection = buffer->props.direction ?
613 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
614 DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
617 * There's an internal 16-bit limit on some things inside the analyzer,
618 * but we never attempt to shape a word longer than 64K characters
619 * in a single gfxShapedWord, so we cannot exceed that limit.
621 uint32_t textLength = buffer->len;
623 TextAnalysis analysis (textString, textLength, nullptr, readingDirection);
624 TextAnalysis::Run *runHead;
626 hr = analysis.GenerateResults (analyzer, &runHead);
630 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
635 FAIL ("Analyzer failed to generate results.");
637 uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
639 bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
641 const wchar_t localeName[20] = {0};
642 if (buffer->props.language != nullptr)
644 mbstowcs ((wchar_t*) localeName,
645 hb_language_to_string (buffer->props.language), 20);
648 // TODO: it does work but doesn't care about ranges
649 DWRITE_TYPOGRAPHIC_FEATURES typographic_features;
650 typographic_features.featureCount = num_features;
653 typographic_features.features = new DWRITE_FONT_FEATURE[num_features];
654 for (unsigned int i = 0; i < num_features; ++i)
656 typographic_features.features[i].nameTag = (DWRITE_FONT_FEATURE_TAG)
657 hb_uint32_swap (features[i].tag);
658 typographic_features.features[i].parameter = features[i].value;
661 const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures =
662 (const DWRITE_TYPOGRAPHIC_FEATURES*) &typographic_features;
663 const uint32_t featureRangeLengths[] = { textLength };
666 uint16_t* clusterMap = new uint16_t[textLength];
667 DWRITE_SHAPING_TEXT_PROPERTIES* textProperties =
668 new DWRITE_SHAPING_TEXT_PROPERTIES[textLength];
670 uint16_t* glyphIndices = new uint16_t[maxGlyphCount];
671 DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties =
672 new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount];
674 hr = analyzer->GetGlyphs (textString, textLength, fontFace, false,
675 isRightToLeft, &runHead->mScript, localeName, nullptr, &dwFeatures,
676 featureRangeLengths, 1, maxGlyphCount, clusterMap, textProperties, glyphIndices,
677 glyphProperties, &glyphCount);
679 if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
681 delete [] glyphIndices;
682 delete [] glyphProperties;
686 goto retry_getglyphs;
689 FAIL ("Analyzer failed to get glyphs.");
691 float* glyphAdvances = new float[maxGlyphCount];
692 DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
694 /* The -2 in the following is to compensate for possible
695 * alignment needed after the WORD array. sizeof (WORD) == 2. */
696 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
698 sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) +
700 sizeof (DWRITE_GLYPH_OFFSET) +
702 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
704 #undef ALLOCATE_ARRAY
706 int fontEmSize = font->face->get_upem ();
708 fontEmSize = -fontEmSize;
711 fontEmSize = -fontEmSize;
712 double x_mult = (double) font->x_scale / fontEmSize;
713 double y_mult = (double) font->y_scale / fontEmSize;
715 hr = analyzer->GetGlyphPlacements (textString,
716 clusterMap, textProperties, textLength, glyphIndices,
717 glyphProperties, glyphCount, fontFace, fontEmSize,
718 false, isRightToLeft, &runHead->mScript, localeName,
719 &dwFeatures, featureRangeLengths, 1,
720 glyphAdvances, glyphOffsets);
723 FAIL ("Analyzer failed to get glyph placements.");
725 IDWriteTextAnalyzer1* analyzer1;
726 analyzer->QueryInterface (&analyzer1);
728 if (analyzer1 && lineWidth)
731 DWRITE_JUSTIFICATION_OPPORTUNITY* justificationOpportunities =
732 new DWRITE_JUSTIFICATION_OPPORTUNITY[maxGlyphCount];
733 hr = analyzer1->GetJustificationOpportunities (fontFace, fontEmSize,
734 runHead->mScript, textLength, glyphCount, textString, clusterMap,
735 glyphProperties, justificationOpportunities);
738 FAIL ("Analyzer failed to get justification opportunities.");
740 float* justifiedGlyphAdvances = new float[maxGlyphCount];
741 DWRITE_GLYPH_OFFSET* justifiedGlyphOffsets = new DWRITE_GLYPH_OFFSET[glyphCount];
742 hr = analyzer1->JustifyGlyphAdvances (lineWidth, glyphCount, justificationOpportunities,
743 glyphAdvances, glyphOffsets, justifiedGlyphAdvances, justifiedGlyphOffsets);
746 FAIL ("Analyzer failed to get justified glyph advances.");
748 DWRITE_SCRIPT_PROPERTIES scriptProperties;
749 hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
751 FAIL ("Analyzer failed to get script properties.");
752 uint32_t justificationCharacter = scriptProperties.justificationCharacter;
754 // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
755 if (justificationCharacter != 32)
757 uint16_t* modifiedClusterMap = new uint16_t[textLength];
758 retry_getjustifiedglyphs:
759 uint16_t* modifiedGlyphIndices = new uint16_t[maxGlyphCount];
760 float* modifiedGlyphAdvances = new float[maxGlyphCount];
761 DWRITE_GLYPH_OFFSET* modifiedGlyphOffsets =
762 new DWRITE_GLYPH_OFFSET[maxGlyphCount];
763 uint32_t actualGlyphsCount;
764 hr = analyzer1->GetJustifiedGlyphs (fontFace, fontEmSize, runHead->mScript,
765 textLength, glyphCount, maxGlyphCount, clusterMap, glyphIndices,
766 glyphAdvances, justifiedGlyphAdvances, justifiedGlyphOffsets,
767 glyphProperties, &actualGlyphsCount, modifiedClusterMap, modifiedGlyphIndices,
768 modifiedGlyphAdvances, modifiedGlyphOffsets);
770 if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
772 maxGlyphCount = actualGlyphsCount;
773 delete [] modifiedGlyphIndices;
774 delete [] modifiedGlyphAdvances;
775 delete [] modifiedGlyphOffsets;
777 maxGlyphCount = actualGlyphsCount;
779 goto retry_getjustifiedglyphs;
782 FAIL ("Analyzer failed to get justified glyphs.");
784 delete [] clusterMap;
785 delete [] glyphIndices;
786 delete [] glyphAdvances;
787 delete [] glyphOffsets;
789 glyphCount = actualGlyphsCount;
790 clusterMap = modifiedClusterMap;
791 glyphIndices = modifiedGlyphIndices;
792 glyphAdvances = modifiedGlyphAdvances;
793 glyphOffsets = modifiedGlyphOffsets;
795 delete [] justifiedGlyphAdvances;
796 delete [] justifiedGlyphOffsets;
800 delete [] glyphAdvances;
801 delete [] glyphOffsets;
803 glyphAdvances = justifiedGlyphAdvances;
804 glyphOffsets = justifiedGlyphOffsets;
807 delete [] justificationOpportunities;
811 /* Ok, we've got everything we need, now compose output buffer,
812 * very, *very*, carefully! */
814 /* Calculate visual-clusters. That's what we ship. */
815 for (unsigned int i = 0; i < glyphCount; i++)
816 vis_clusters[i] = -1;
817 for (unsigned int i = 0; i < buffer->len; i++)
820 &vis_clusters[log_clusters[buffer->info[i].utf16_index ()]];
821 *p = MIN (*p, buffer->info[i].cluster);
823 for (unsigned int i = 1; i < glyphCount; i++)
824 if (vis_clusters[i] == -1)
825 vis_clusters[i] = vis_clusters[i - 1];
829 if (unlikely (!buffer->ensure (glyphCount)))
830 FAIL ("Buffer in error");
834 /* Set glyph infos */
836 for (unsigned int i = 0; i < glyphCount; i++)
838 hb_glyph_info_t *info = &buffer->info[buffer->len++];
840 info->codepoint = glyphIndices[i];
841 info->cluster = vis_clusters[i];
843 /* The rest is crap. Let's store position info there for now. */
844 info->mask = glyphAdvances[i];
845 info->var1.i32 = glyphOffsets[i].advanceOffset;
846 info->var2.i32 = glyphOffsets[i].ascenderOffset;
849 /* Set glyph positions */
850 buffer->clear_positions ();
851 for (unsigned int i = 0; i < glyphCount; i++)
853 hb_glyph_info_t *info = &buffer->info[i];
854 hb_glyph_position_t *pos = &buffer->pos[i];
857 pos->x_advance = x_mult * (int32_t) info->mask;
859 x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
860 pos->y_offset = y_mult * info->var2.i32;
864 hb_buffer_reverse (buffer);
866 delete [] clusterMap;
867 delete [] glyphIndices;
868 delete [] textProperties;
869 delete [] glyphProperties;
870 delete [] glyphAdvances;
871 delete [] glyphOffsets;
874 delete [] typographic_features.features;
881 _hb_directwrite_shape (hb_shape_plan_t *shape_plan,
884 const hb_feature_t *features,
885 unsigned int num_features)
887 return _hb_directwrite_shape_full (shape_plan, font, buffer,
888 features, num_features, 0);
892 * Public [experimental] API
896 hb_directwrite_shape_experimental_width (hb_font_t *font,
898 const hb_feature_t *features,
899 unsigned int num_features,
902 static const char *shapers = "directwrite";
903 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face,
904 &buffer->props, features, num_features, &shapers);
905 hb_bool_t res = _hb_directwrite_shape_full (shape_plan, font, buffer,
906 features, num_features, width);
908 buffer->unsafe_to_break_all ();