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.
27 #ifdef HAVE_DIRECTWRITE
29 #include "hb-shaper-impl.hh"
33 #include "hb-directwrite.h"
36 /* Declare object creator for dynamic support of DWRITE */
37 typedef HRESULT (* WINAPI t_DWriteCreateFactory)(
38 DWRITE_FACTORY_TYPE factoryType,
44 * hb-directwrite uses new/delete syntatically but as we let users
45 * to override malloc/free, we will redefine new/delete so users
46 * won't need to do that by their own.
48 void* operator new (size_t size) { return malloc (size); }
49 void* operator new [] (size_t size) { return malloc (size); }
50 void operator delete (void* pointer) { free (pointer); }
51 void operator delete [] (void* pointer) { free (pointer); }
55 * DirectWrite font stream helpers
58 // This is a font loader which provides only one font (unlike its original design).
59 // For a better implementation which was also source of this
60 // and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
61 class DWriteFontFileLoader : public IDWriteFontFileLoader
64 IDWriteFontFileStream *mFontFileStream;
66 DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream)
67 { mFontFileStream = fontFileStream; }
70 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
72 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
73 IFACEMETHOD_ (ULONG, Release) () { return 1; }
75 // IDWriteFontFileLoader methods
76 virtual HRESULT STDMETHODCALLTYPE
77 CreateStreamFromKey (void const* fontFileReferenceKey,
78 uint32_t fontFileReferenceKeySize,
79 OUT IDWriteFontFileStream** fontFileStream)
81 *fontFileStream = mFontFileStream;
85 virtual ~DWriteFontFileLoader() {}
88 class DWriteFontFileStream : public IDWriteFontFileStream
94 DWriteFontFileStream (uint8_t *aData, uint32_t aSize)
100 // IUnknown interface
101 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
103 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
104 IFACEMETHOD_ (ULONG, Release) () { return 1; }
106 // IDWriteFontFileStream methods
107 virtual HRESULT STDMETHODCALLTYPE
108 ReadFileFragment (void const** fragmentStart,
111 OUT void** fragmentContext)
113 // We are required to do bounds checking.
114 if (fileOffset + fragmentSize > mSize) return E_FAIL;
116 // truncate the 64 bit fileOffset to size_t sized index into mData
117 size_t index = static_cast<size_t> (fileOffset);
119 // We should be alive for the duration of this.
120 *fragmentStart = &mData[index];
121 *fragmentContext = nullptr;
125 virtual void STDMETHODCALLTYPE
126 ReleaseFileFragment (void* fragmentContext) {}
128 virtual HRESULT STDMETHODCALLTYPE
129 GetFileSize (OUT UINT64* fileSize)
135 virtual HRESULT STDMETHODCALLTYPE
136 GetLastWriteTime (OUT UINT64* lastWriteTime) { return E_NOTIMPL; }
138 virtual ~DWriteFontFileStream() {}
146 struct hb_directwrite_face_data_t
149 IDWriteFactory *dwriteFactory;
150 IDWriteFontFile *fontFile;
151 DWriteFontFileStream *fontFileStream;
152 DWriteFontFileLoader *fontFileLoader;
153 IDWriteFontFace *fontFace;
157 hb_directwrite_face_data_t *
158 _hb_directwrite_shaper_face_data_create (hb_face_t *face)
160 hb_directwrite_face_data_t *data = new hb_directwrite_face_data_t;
161 if (unlikely (!data))
166 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
170 data->dwrite_dll = LoadLibrary (TEXT ("DWRITE"));
171 if (unlikely (!data->dwrite_dll))
172 FAIL ("Cannot find DWrite.DLL");
174 t_DWriteCreateFactory p_DWriteCreateFactory;
176 #if defined(__GNUC__)
177 #pragma GCC diagnostic push
178 #pragma GCC diagnostic ignored "-Wcast-function-type"
181 p_DWriteCreateFactory = (t_DWriteCreateFactory)
182 GetProcAddress (data->dwrite_dll, "DWriteCreateFactory");
184 #if defined(__GNUC__)
185 #pragma GCC diagnostic pop
188 if (unlikely (!p_DWriteCreateFactory))
189 FAIL ("Cannot find DWriteCreateFactory().");
193 // TODO: factory and fontFileLoader should be cached separately
194 IDWriteFactory* dwriteFactory;
195 hr = p_DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED, __uuidof (IDWriteFactory),
196 (IUnknown**) &dwriteFactory);
198 if (unlikely (hr != S_OK))
199 FAIL ("Failed to run DWriteCreateFactory().");
201 hb_blob_t *blob = hb_face_reference_blob (face);
202 DWriteFontFileStream *fontFileStream;
203 fontFileStream = new DWriteFontFileStream ((uint8_t *) hb_blob_get_data (blob, nullptr),
204 hb_blob_get_length (blob));
206 DWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
207 dwriteFactory->RegisterFontFileLoader (fontFileLoader);
209 IDWriteFontFile *fontFile;
210 uint64_t fontFileKey = 0;
211 hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
212 fontFileLoader, &fontFile);
215 FAIL ("Failed to load font file from data!");
218 DWRITE_FONT_FILE_TYPE fileType;
219 DWRITE_FONT_FACE_TYPE faceType;
220 uint32_t numberOfFaces;
221 hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
222 if (FAILED (hr) || !isSupported)
223 FAIL ("Font file is not supported.");
227 IDWriteFontFace *fontFace;
228 dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
229 DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
231 data->dwriteFactory = dwriteFactory;
232 data->fontFile = fontFile;
233 data->fontFileStream = fontFileStream;
234 data->fontFileLoader = fontFileLoader;
235 data->fontFace = fontFace;
236 data->faceBlob = blob;
242 _hb_directwrite_shaper_face_data_destroy (hb_directwrite_face_data_t *data)
245 data->fontFace->Release ();
247 data->fontFile->Release ();
248 if (data->dwriteFactory)
250 if (data->fontFileLoader)
251 data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
252 data->dwriteFactory->Release ();
254 if (data->fontFileLoader)
255 delete data->fontFileLoader;
256 if (data->fontFileStream)
257 delete data->fontFileStream;
259 hb_blob_destroy (data->faceBlob);
260 if (data->dwrite_dll)
261 FreeLibrary (data->dwrite_dll);
271 struct hb_directwrite_font_data_t {};
273 hb_directwrite_font_data_t *
274 _hb_directwrite_shaper_font_data_create (hb_font_t *font)
276 hb_directwrite_font_data_t *data = new hb_directwrite_font_data_t;
277 if (unlikely (!data))
284 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_font_data_t *data)
290 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
291 // but now is relicensed to MIT for HarfBuzz use
292 class TextAnalysis : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
296 IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
298 IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
299 IFACEMETHOD_ (ULONG, Release) () { return 1; }
301 // A single contiguous run of characters containing the same analysis
305 uint32_t mTextStart; // starting text position of this run
306 uint32_t mTextLength; // number of contiguous code units covered
307 uint32_t mGlyphStart; // starting glyph in the glyphs array
308 uint32_t mGlyphCount; // number of glyphs associated with this run
310 DWRITE_SCRIPT_ANALYSIS mScript;
314 bool ContainsTextPosition (uint32_t aTextPosition) const
316 return aTextPosition >= mTextStart &&
317 aTextPosition < mTextStart + mTextLength;
324 TextAnalysis (const wchar_t* text, uint32_t textLength,
325 const wchar_t* localeName, DWRITE_READING_DIRECTION readingDirection)
326 : mTextLength (textLength), mText (text), mLocaleName (localeName),
327 mReadingDirection (readingDirection), mCurrentRun (nullptr) {}
330 // delete runs, except mRunHead which is part of the TextAnalysis object
331 for (Run *run = mRunHead.nextRun; run;)
340 GenerateResults (IDWriteTextAnalyzer* textAnalyzer, Run **runHead)
342 // Analyzes the text using the script analyzer and returns
343 // the result as a series of runs.
347 // Initially start out with one result that covers the entire range.
348 // This result will be subdivided by the analysis processes.
349 mRunHead.mTextStart = 0;
350 mRunHead.mTextLength = mTextLength;
351 mRunHead.mBidiLevel =
352 (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
353 mRunHead.nextRun = nullptr;
354 mCurrentRun = &mRunHead;
356 // Call each of the analyzers in sequence, recording their results.
357 if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this)))
358 *runHead = &mRunHead;
363 // IDWriteTextAnalysisSource implementation
366 GetTextAtPosition (uint32_t textPosition,
367 OUT wchar_t const** textString,
368 OUT uint32_t* textLength)
370 if (textPosition >= mTextLength)
372 // No text at this position, valid query though.
373 *textString = nullptr;
378 *textString = mText + textPosition;
379 *textLength = mTextLength - textPosition;
385 GetTextBeforePosition (uint32_t textPosition,
386 OUT wchar_t const** textString,
387 OUT uint32_t* textLength)
389 if (textPosition == 0 || textPosition > mTextLength)
391 // Either there is no text before here (== 0), or this
392 // is an invalid position. The query is considered valid though.
393 *textString = nullptr;
399 *textLength = textPosition;
404 IFACEMETHODIMP_ (DWRITE_READING_DIRECTION)
405 GetParagraphReadingDirection () { return mReadingDirection; }
407 IFACEMETHODIMP GetLocaleName (uint32_t textPosition, uint32_t* textLength,
408 wchar_t const** localeName)
412 GetNumberSubstitution (uint32_t textPosition,
413 OUT uint32_t* textLength,
414 OUT IDWriteNumberSubstitution** numberSubstitution)
416 // We do not support number substitution.
417 *numberSubstitution = nullptr;
418 *textLength = mTextLength - textPosition;
423 // IDWriteTextAnalysisSink implementation
426 SetScriptAnalysis (uint32_t textPosition, uint32_t textLength,
427 DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
429 SetCurrentRun (textPosition);
430 SplitCurrentRun (textPosition);
431 while (textLength > 0)
433 Run *run = FetchNextRun (&textLength);
434 run->mScript = *scriptAnalysis;
441 SetLineBreakpoints (uint32_t textPosition,
443 const DWRITE_LINE_BREAKPOINT* lineBreakpoints)
446 IFACEMETHODIMP SetBidiLevel (uint32_t textPosition, uint32_t textLength,
447 uint8_t explicitLevel, uint8_t resolvedLevel)
451 SetNumberSubstitution (uint32_t textPosition, uint32_t textLength,
452 IDWriteNumberSubstitution* numberSubstitution)
456 Run *FetchNextRun (IN OUT uint32_t* textLength)
458 // Used by the sink setters, this returns a reference to the next run.
459 // Position and length are adjusted to now point after the current run
462 Run *origRun = mCurrentRun;
463 // Split the tail if needed (the length remaining is less than the
464 // current run's size).
465 if (*textLength < mCurrentRun->mTextLength)
466 SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
468 // Just advance the current run.
469 mCurrentRun = mCurrentRun->nextRun;
470 *textLength -= origRun->mTextLength;
472 // Return a reference to the run that was just current.
476 void SetCurrentRun (uint32_t textPosition)
478 // Move the current run to the given position.
479 // Since the analyzers generally return results in a forward manner,
480 // this will usually just return early. If not, find the
481 // corresponding run for the text position.
483 if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
486 for (Run *run = &mRunHead; run; run = run->nextRun)
487 if (run->ContainsTextPosition (textPosition))
492 assert (0); // We should always be able to find the text position in one of our runs
495 void SplitCurrentRun (uint32_t splitPosition)
499 assert (0); // SplitCurrentRun called without current run
500 // Shouldn't be calling this when no current run is set!
503 // Split the current run.
504 if (splitPosition <= mCurrentRun->mTextStart)
506 // No need to split, already the start of a run
507 // or before it. Usually the first.
510 Run *newRun = new Run;
512 *newRun = *mCurrentRun;
514 // Insert the new run in our linked list.
515 newRun->nextRun = mCurrentRun->nextRun;
516 mCurrentRun->nextRun = newRun;
518 // Adjust runs' text positions and lengths.
519 uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
520 newRun->mTextStart += splitPoint;
521 newRun->mTextLength -= splitPoint;
522 mCurrentRun->mTextLength = splitPoint;
523 mCurrentRun = newRun;
528 // (weak references are fine here, since this class is a transient
529 // stack-based helper that doesn't need to copy data)
530 uint32_t mTextLength;
531 const wchar_t* mText;
532 const wchar_t* mLocaleName;
533 DWRITE_READING_DIRECTION mReadingDirection;
535 // Current processing state.
538 // Output is a list of runs starting here
547 _hb_directwrite_shape_full (hb_shape_plan_t *shape_plan,
550 const hb_feature_t *features,
551 unsigned int num_features,
554 hb_face_t *face = font->face;
555 const hb_directwrite_face_data_t *face_data = face->data.directwrite;
556 IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
557 IDWriteFontFace *fontFace = face_data->fontFace;
559 IDWriteTextAnalyzer* analyzer;
560 dwriteFactory->CreateTextAnalyzer (&analyzer);
562 unsigned int scratch_size;
563 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
564 #define ALLOCATE_ARRAY(Type, name, len) \
565 Type *name = (Type *) scratch; \
567 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
568 assert (_consumed <= scratch_size); \
569 scratch += _consumed; \
570 scratch_size -= _consumed; \
573 #define utf16_index() var1.u32
575 ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2);
577 unsigned int chars_len = 0;
578 for (unsigned int i = 0; i < buffer->len; i++)
580 hb_codepoint_t c = buffer->info[i].codepoint;
581 buffer->info[i].utf16_index () = chars_len;
582 if (likely (c <= 0xFFFFu))
583 textString[chars_len++] = c;
584 else if (unlikely (c > 0x10FFFFu))
585 textString[chars_len++] = 0xFFFDu;
588 textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
589 textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
593 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
594 /* Need log_clusters to assign features. */
596 for (unsigned int i = 0; i < buffer->len; i++)
598 hb_codepoint_t c = buffer->info[i].codepoint;
599 unsigned int cluster = buffer->info[i].cluster;
600 log_clusters[chars_len++] = cluster;
601 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
602 log_clusters[chars_len++] = cluster; /* Surrogates. */
605 // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
607 DWRITE_READING_DIRECTION readingDirection;
608 readingDirection = buffer->props.direction ?
609 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
610 DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
613 * There's an internal 16-bit limit on some things inside the analyzer,
614 * but we never attempt to shape a word longer than 64K characters
615 * in a single gfxShapedWord, so we cannot exceed that limit.
617 uint32_t textLength = buffer->len;
619 TextAnalysis analysis (textString, textLength, nullptr, readingDirection);
620 TextAnalysis::Run *runHead;
622 hr = analysis.GenerateResults (analyzer, &runHead);
626 DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
631 FAIL ("Analyzer failed to generate results.");
633 uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
635 bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
637 const wchar_t localeName[20] = {0};
638 if (buffer->props.language)
639 mbstowcs ((wchar_t*) localeName,
640 hb_language_to_string (buffer->props.language), 20);
642 // TODO: it does work but doesn't care about ranges
643 DWRITE_TYPOGRAPHIC_FEATURES typographic_features;
644 typographic_features.featureCount = num_features;
647 typographic_features.features = new DWRITE_FONT_FEATURE[num_features];
648 for (unsigned int i = 0; i < num_features; ++i)
650 typographic_features.features[i].nameTag = (DWRITE_FONT_FEATURE_TAG)
651 hb_uint32_swap (features[i].tag);
652 typographic_features.features[i].parameter = features[i].value;
655 const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures;
656 dwFeatures = (const DWRITE_TYPOGRAPHIC_FEATURES*) &typographic_features;
657 const uint32_t featureRangeLengths[] = { textLength };
660 uint16_t* clusterMap;
661 clusterMap = new uint16_t[textLength];
662 DWRITE_SHAPING_TEXT_PROPERTIES* textProperties;
663 textProperties = new DWRITE_SHAPING_TEXT_PROPERTIES[textLength];
665 uint16_t* glyphIndices = new uint16_t[maxGlyphCount];
666 DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties;
667 glyphProperties = new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount];
669 hr = analyzer->GetGlyphs (textString, textLength, fontFace, false,
670 isRightToLeft, &runHead->mScript, localeName,
671 nullptr, &dwFeatures, featureRangeLengths, 1,
672 maxGlyphCount, clusterMap, textProperties,
673 glyphIndices, glyphProperties, &glyphCount);
675 if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
677 delete [] glyphIndices;
678 delete [] glyphProperties;
682 goto retry_getglyphs;
685 FAIL ("Analyzer failed to get glyphs.");
687 float* glyphAdvances = new float[maxGlyphCount];
688 DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
690 /* The -2 in the following is to compensate for possible
691 * alignment needed after the WORD array. sizeof (WORD) == 2. */
692 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
694 sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) +
696 sizeof (DWRITE_GLYPH_OFFSET) +
698 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
700 #undef ALLOCATE_ARRAY
702 int fontEmSize = font->face->get_upem ();
703 if (fontEmSize < 0) fontEmSize = -fontEmSize;
705 if (fontEmSize < 0) fontEmSize = -fontEmSize;
706 double x_mult = (double) font->x_scale / fontEmSize;
707 double y_mult = (double) font->y_scale / fontEmSize;
709 hr = analyzer->GetGlyphPlacements (textString, clusterMap, textProperties,
710 textLength, glyphIndices, glyphProperties,
711 glyphCount, fontFace, fontEmSize,
712 false, isRightToLeft, &runHead->mScript, localeName,
713 &dwFeatures, featureRangeLengths, 1,
714 glyphAdvances, glyphOffsets);
717 FAIL ("Analyzer failed to get glyph placements.");
719 IDWriteTextAnalyzer1* analyzer1;
720 analyzer->QueryInterface (&analyzer1);
722 if (analyzer1 && lineWidth)
724 DWRITE_JUSTIFICATION_OPPORTUNITY* justificationOpportunities =
725 new DWRITE_JUSTIFICATION_OPPORTUNITY[maxGlyphCount];
726 hr = analyzer1->GetJustificationOpportunities (fontFace, fontEmSize, runHead->mScript,
727 textLength, glyphCount, textString,
728 clusterMap, glyphProperties,
729 justificationOpportunities);
732 FAIL ("Analyzer failed to get justification opportunities.");
734 float* justifiedGlyphAdvances = new float[maxGlyphCount];
735 DWRITE_GLYPH_OFFSET* justifiedGlyphOffsets = new DWRITE_GLYPH_OFFSET[glyphCount];
736 hr = analyzer1->JustifyGlyphAdvances (lineWidth, glyphCount, justificationOpportunities,
737 glyphAdvances, glyphOffsets, justifiedGlyphAdvances,
738 justifiedGlyphOffsets);
740 if (FAILED (hr)) FAIL ("Analyzer failed to get justify glyph advances.");
742 DWRITE_SCRIPT_PROPERTIES scriptProperties;
743 hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
744 if (FAILED (hr)) FAIL ("Analyzer failed to get script properties.");
745 uint32_t justificationCharacter = scriptProperties.justificationCharacter;
747 // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
748 if (justificationCharacter != 32)
750 uint16_t* modifiedClusterMap = new uint16_t[textLength];
751 retry_getjustifiedglyphs:
752 uint16_t* modifiedGlyphIndices = new uint16_t[maxGlyphCount];
753 float* modifiedGlyphAdvances = new float[maxGlyphCount];
754 DWRITE_GLYPH_OFFSET* modifiedGlyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
755 uint32_t actualGlyphsCount;
756 hr = analyzer1->GetJustifiedGlyphs (fontFace, fontEmSize, runHead->mScript,
757 textLength, glyphCount, maxGlyphCount,
758 clusterMap, glyphIndices, glyphAdvances,
759 justifiedGlyphAdvances, justifiedGlyphOffsets,
760 glyphProperties, &actualGlyphsCount,
761 modifiedClusterMap, modifiedGlyphIndices,
762 modifiedGlyphAdvances, modifiedGlyphOffsets);
764 if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
766 maxGlyphCount = actualGlyphsCount;
767 delete [] modifiedGlyphIndices;
768 delete [] modifiedGlyphAdvances;
769 delete [] modifiedGlyphOffsets;
771 maxGlyphCount = actualGlyphsCount;
773 goto retry_getjustifiedglyphs;
776 FAIL ("Analyzer failed to get justified glyphs.");
778 delete [] clusterMap;
779 delete [] glyphIndices;
780 delete [] glyphAdvances;
781 delete [] glyphOffsets;
783 glyphCount = actualGlyphsCount;
784 clusterMap = modifiedClusterMap;
785 glyphIndices = modifiedGlyphIndices;
786 glyphAdvances = modifiedGlyphAdvances;
787 glyphOffsets = modifiedGlyphOffsets;
789 delete [] justifiedGlyphAdvances;
790 delete [] justifiedGlyphOffsets;
794 delete [] glyphAdvances;
795 delete [] glyphOffsets;
797 glyphAdvances = justifiedGlyphAdvances;
798 glyphOffsets = justifiedGlyphOffsets;
801 delete [] justificationOpportunities;
804 /* Ok, we've got everything we need, now compose output buffer,
805 * very, *very*, carefully! */
807 /* Calculate visual-clusters. That's what we ship. */
808 for (unsigned int i = 0; i < glyphCount; i++)
809 vis_clusters[i] = (uint32_t) -1;
810 for (unsigned int i = 0; i < buffer->len; i++)
813 &vis_clusters[log_clusters[buffer->info[i].utf16_index ()]];
814 *p = hb_min (*p, buffer->info[i].cluster);
816 for (unsigned int i = 1; i < glyphCount; i++)
817 if (vis_clusters[i] == (uint32_t) -1)
818 vis_clusters[i] = vis_clusters[i - 1];
822 if (unlikely (!buffer->ensure (glyphCount)))
823 FAIL ("Buffer in error");
827 /* Set glyph infos */
829 for (unsigned int i = 0; i < glyphCount; i++)
831 hb_glyph_info_t *info = &buffer->info[buffer->len++];
833 info->codepoint = glyphIndices[i];
834 info->cluster = vis_clusters[i];
836 /* The rest is crap. Let's store position info there for now. */
837 info->mask = glyphAdvances[i];
838 info->var1.i32 = glyphOffsets[i].advanceOffset;
839 info->var2.i32 = glyphOffsets[i].ascenderOffset;
842 /* Set glyph positions */
843 buffer->clear_positions ();
844 for (unsigned int i = 0; i < glyphCount; i++)
846 hb_glyph_info_t *info = &buffer->info[i];
847 hb_glyph_position_t *pos = &buffer->pos[i];
850 pos->x_advance = x_mult * (int32_t) info->mask;
851 pos->x_offset = x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
852 pos->y_offset = y_mult * info->var2.i32;
855 if (isRightToLeft) hb_buffer_reverse (buffer);
857 delete [] clusterMap;
858 delete [] glyphIndices;
859 delete [] textProperties;
860 delete [] glyphProperties;
861 delete [] glyphAdvances;
862 delete [] glyphOffsets;
865 delete [] typographic_features.features;
872 _hb_directwrite_shape (hb_shape_plan_t *shape_plan,
875 const hb_feature_t *features,
876 unsigned int num_features)
878 return _hb_directwrite_shape_full (shape_plan, font, buffer,
879 features, num_features, 0);
882 HB_UNUSED static bool
883 _hb_directwrite_shape_experimental_width (hb_font_t *font,
885 const hb_feature_t *features,
886 unsigned int num_features,
889 static const char *shapers = "directwrite";
890 hb_shape_plan_t *shape_plan;
891 shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
892 features, num_features, &shapers);
893 hb_bool_t res = _hb_directwrite_shape_full (shape_plan, font, buffer,
894 features, num_features, width);
896 buffer->unsafe_to_break_all ();
901 struct _hb_directwrite_font_table_context {
902 IDWriteFontFace *face;
907 _hb_directwrite_table_data_release (void *data)
909 _hb_directwrite_font_table_context *context = (_hb_directwrite_font_table_context *) data;
910 context->face->ReleaseFontTable (context->table_context);
915 _hb_directwrite_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
917 IDWriteFontFace *dw_face = ((IDWriteFontFace *) user_data);
922 if (!dw_face || FAILED (dw_face->TryGetFontTable (hb_uint32_swap (tag), &data,
923 &length, &table_context, &exists)))
926 if (!data || !exists || !length)
928 dw_face->ReleaseFontTable (table_context);
932 _hb_directwrite_font_table_context *context = new _hb_directwrite_font_table_context;
933 context->face = dw_face;
934 context->table_context = table_context;
936 return hb_blob_create ((const char *) data, length, HB_MEMORY_MODE_READONLY,
937 context, _hb_directwrite_table_data_release);
941 _hb_directwrite_font_release (void *data)
944 ((IDWriteFontFace *) data)->Release ();
948 * hb_directwrite_face_create:
949 * @font_face: a DirectWrite IDWriteFontFace object.
951 * Return value: #hb_face_t object corresponding to the given input
956 hb_directwrite_face_create (IDWriteFontFace *font_face)
959 font_face->AddRef ();
960 return hb_face_create_for_tables (_hb_directwrite_reference_table, font_face,
961 _hb_directwrite_font_release);
965 * hb_directwrite_face_get_font_face:
966 * @face: a #hb_face_t object
968 * Return value: DirectWrite IDWriteFontFace object corresponding to the given input
973 hb_directwrite_face_get_font_face (hb_face_t *face)
975 return face->data.directwrite->fontFace;