187ab3f9da9da74c3a3b853ac237f14e8528341d
[platform/upstream/harfbuzz.git] / src / hb-directwrite.cc
1 /*
2  * Copyright © 2015-2018  Ebrahim Byagowi
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
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.
11  *
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
16  * DAMAGE.
17  *
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.
23  */
24
25 #include "hb-private.hh"
26 #include "hb-debug.hh"
27 #define HB_SHAPER directwrite
28 #include "hb-shaper-impl-private.hh"
29
30 #include <DWrite_1.h>
31
32 #include "hb-directwrite.h"
33
34
35 HB_SHAPER_DATA_ENSURE_DEFINE (directwrite, face)
36 HB_SHAPER_DATA_ENSURE_DEFINE (directwrite, font)
37
38
39 /*
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.
43  */
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); }
48
49
50 /*
51  * DirectWrite font stream helpers
52  */
53
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
58 {
59 private:
60   IDWriteFontFileStream *mFontFileStream;
61 public:
62   DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream)
63   {
64     mFontFileStream = fontFileStream;
65   }
66
67   // IUnknown interface
68   IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
69   IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
70   IFACEMETHOD_ (ULONG, Release) () { return 1; }
71
72   // IDWriteFontFileLoader methods
73   virtual HRESULT STDMETHODCALLTYPE CreateStreamFromKey (void const* fontFileReferenceKey,
74     uint32_t fontFileReferenceKeySize,
75     OUT IDWriteFontFileStream** fontFileStream)
76   {
77     *fontFileStream = mFontFileStream;
78     return S_OK;
79   }
80 };
81
82 class DWriteFontFileStream : public IDWriteFontFileStream
83 {
84 private:
85   uint8_t *mData;
86   uint32_t mSize;
87 public:
88   DWriteFontFileStream (uint8_t *aData, uint32_t aSize)
89   {
90     mData = aData;
91     mSize = aSize;
92   }
93
94   // IUnknown interface
95   IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
96   IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
97   IFACEMETHOD_ (ULONG, Release) () { return 1; }
98
99   // IDWriteFontFileStream methods
100   virtual HRESULT STDMETHODCALLTYPE ReadFileFragment (void const** fragmentStart,
101     UINT64 fileOffset,
102     UINT64 fragmentSize,
103     OUT void** fragmentContext)
104   {
105     // We are required to do bounds checking.
106     if (fileOffset + fragmentSize > mSize)
107       return E_FAIL;
108
109     // truncate the 64 bit fileOffset to size_t sized index into mData
110     size_t index = static_cast<size_t> (fileOffset);
111
112     // We should be alive for the duration of this.
113     *fragmentStart = &mData[index];
114     *fragmentContext = nullptr;
115     return S_OK;
116   }
117
118   virtual void STDMETHODCALLTYPE ReleaseFileFragment (void* fragmentContext) { }
119
120   virtual HRESULT STDMETHODCALLTYPE GetFileSize (OUT UINT64* fileSize)
121   {
122     *fileSize = mSize;
123     return S_OK;
124   }
125
126   virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime (OUT UINT64* lastWriteTime)
127   {
128     return E_NOTIMPL;
129   }
130 };
131
132
133 /*
134 * shaper face data
135 */
136
137 struct hb_directwrite_shaper_face_data_t
138 {
139   IDWriteFactory *dwriteFactory;
140   IDWriteFontFile *fontFile;
141   IDWriteFontFileStream *fontFileStream;
142   IDWriteFontFileLoader *fontFileLoader;
143   IDWriteFontFace *fontFace;
144   hb_blob_t *faceBlob;
145 };
146
147 hb_directwrite_shaper_face_data_t *
148 _hb_directwrite_shaper_face_data_create (hb_face_t *face)
149 {
150   hb_directwrite_shaper_face_data_t *data = new hb_directwrite_shaper_face_data_t;
151   if (unlikely (!data))
152     return nullptr;
153
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
160   );
161
162   HRESULT hr;
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));
167
168   DWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
169   dwriteFactory->RegisterFontFileLoader (fontFileLoader);
170
171   IDWriteFontFile *fontFile;
172   uint64_t fontFileKey = 0;
173   hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
174       fontFileLoader, &fontFile);
175
176 #define FAIL(...) \
177   HB_STMT_START { \
178     DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
179     return nullptr; \
180   } HB_STMT_END;
181
182   if (FAILED (hr))
183     FAIL ("Failed to load font file from data!");
184
185   BOOL isSupported;
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.");
192
193 #undef FAIL
194
195   IDWriteFontFace *fontFace;
196   dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
197     DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
198
199   data->dwriteFactory = dwriteFactory;
200   data->fontFile = fontFile;
201   data->fontFileStream = fontFileStream;
202   data->fontFileLoader = fontFileLoader;
203   data->fontFace = fontFace;
204   data->faceBlob = blob;
205
206   return data;
207 }
208
209 void
210 _hb_directwrite_shaper_face_data_destroy (hb_directwrite_shaper_face_data_t *data)
211 {
212   if (data->fontFace)
213     data->fontFace->Release ();
214   if (data->fontFile)
215     data->fontFile->Release ();
216   if (data->dwriteFactory)
217   {
218     if (data->fontFileLoader)
219       data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
220     data->dwriteFactory->Release ();
221   }
222   if (data->fontFileLoader)
223     delete data->fontFileLoader;
224   if (data->fontFileStream)
225     delete data->fontFileStream;
226   if (data->faceBlob)
227     hb_blob_destroy (data->faceBlob);
228   if (data)
229     delete data;
230 }
231
232
233 /*
234  * shaper font data
235  */
236
237 struct hb_directwrite_shaper_font_data_t
238 {
239 };
240
241 hb_directwrite_shaper_font_data_t *
242 _hb_directwrite_shaper_font_data_create (hb_font_t *font)
243 {
244   if (unlikely (!hb_directwrite_shaper_face_data_ensure (font->face))) return nullptr;
245
246   hb_directwrite_shaper_font_data_t *data = new hb_directwrite_shaper_font_data_t;
247   if (unlikely (!data))
248     return nullptr;
249
250   return data;
251 }
252
253 void
254 _hb_directwrite_shaper_font_data_destroy (hb_directwrite_shaper_font_data_t *data)
255 {
256   delete data;
257 }
258
259
260 /*
261  * shaper shape_plan data
262  */
263
264 struct hb_directwrite_shaper_shape_plan_data_t {};
265
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)
272 {
273   return (hb_directwrite_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
274 }
275
276 void
277 _hb_directwrite_shaper_shape_plan_data_destroy (hb_directwrite_shaper_shape_plan_data_t *data HB_UNUSED)
278 {
279 }
280
281 // Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
282 // but now is relicensed to MIT for HarfBuzz use
283 class TextAnalysis
284   : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
285 {
286 public:
287
288   IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject) { return S_OK; }
289   IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
290   IFACEMETHOD_ (ULONG, Release) () { return 1; }
291
292   // A single contiguous run of characters containing the same analysis
293   // results.
294   struct Run
295   {
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
300     // text
301     DWRITE_SCRIPT_ANALYSIS mScript;
302     uint8_t mBidiLevel;
303     bool mIsSideways;
304
305     inline bool ContainsTextPosition (uint32_t aTextPosition) const
306     {
307       return aTextPosition >= mTextStart &&
308              aTextPosition <  mTextStart + mTextLength;
309     }
310
311     Run *nextRun;
312   };
313
314 public:
315   TextAnalysis (const wchar_t* text,
316     uint32_t textLength,
317     const wchar_t* localeName,
318     DWRITE_READING_DIRECTION readingDirection)
319     : mText (text)
320     , mTextLength (textLength)
321     , mLocaleName (localeName)
322     , mReadingDirection (readingDirection)
323     , mCurrentRun (nullptr) { };
324
325   ~TextAnalysis ()
326   {
327     // delete runs, except mRunHead which is part of the TextAnalysis object
328     for (Run *run = mRunHead.nextRun; run;)
329     {
330       Run *origRun = run;
331       run = run->nextRun;
332       delete origRun;
333     }
334   }
335
336   STDMETHODIMP GenerateResults (IDWriteTextAnalyzer* textAnalyzer,
337     Run **runHead)
338   {
339     // Analyzes the text using the script analyzer and returns
340     // the result as a series of runs.
341
342     HRESULT hr = S_OK;
343
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;
352
353     // Call each of the analyzers in sequence, recording their results.
354     if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this)))
355       *runHead = &mRunHead;
356
357     return hr;
358   }
359
360   // IDWriteTextAnalysisSource implementation
361
362   IFACEMETHODIMP GetTextAtPosition (uint32_t textPosition,
363     OUT wchar_t const** textString,
364     OUT uint32_t* textLength)
365   {
366     if (textPosition >= mTextLength)
367     {
368       // No text at this position, valid query though.
369       *textString = nullptr;
370       *textLength = 0;
371     }
372     else
373     {
374       *textString = mText + textPosition;
375       *textLength = mTextLength - textPosition;
376     }
377     return S_OK;
378   }
379
380   IFACEMETHODIMP GetTextBeforePosition (uint32_t textPosition,
381     OUT wchar_t const** textString,
382     OUT uint32_t* textLength)
383   {
384     if (textPosition == 0 || textPosition > mTextLength)
385     {
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;
389       *textLength = 0;
390     }
391     else
392     {
393       *textString = mText;
394       *textLength = textPosition;
395     }
396     return S_OK;
397   }
398
399   IFACEMETHODIMP_ (DWRITE_READING_DIRECTION)
400     GetParagraphReadingDirection () { return mReadingDirection; }
401
402   IFACEMETHODIMP GetLocaleName (uint32_t textPosition,
403     uint32_t* textLength,
404     wchar_t const** localeName)
405   {
406     return S_OK;
407   }
408
409   IFACEMETHODIMP
410     GetNumberSubstitution (uint32_t textPosition,
411     OUT uint32_t* textLength,
412     OUT IDWriteNumberSubstitution** numberSubstitution)
413   {
414     // We do not support number substitution.
415     *numberSubstitution = nullptr;
416     *textLength = mTextLength - textPosition;
417
418     return S_OK;
419   }
420
421   // IDWriteTextAnalysisSink implementation
422
423   IFACEMETHODIMP
424     SetScriptAnalysis (uint32_t textPosition,
425     uint32_t textLength,
426     DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
427   {
428     SetCurrentRun (textPosition);
429     SplitCurrentRun (textPosition);
430     while (textLength > 0)
431     {
432       Run *run = FetchNextRun (&textLength);
433       run->mScript = *scriptAnalysis;
434     }
435
436     return S_OK;
437   }
438
439   IFACEMETHODIMP
440     SetLineBreakpoints (uint32_t textPosition,
441     uint32_t textLength,
442     const DWRITE_LINE_BREAKPOINT* lineBreakpoints) { return S_OK; }
443
444   IFACEMETHODIMP SetBidiLevel (uint32_t textPosition,
445     uint32_t textLength,
446     uint8_t explicitLevel,
447     uint8_t resolvedLevel) { return S_OK; }
448
449   IFACEMETHODIMP
450     SetNumberSubstitution (uint32_t textPosition,
451     uint32_t textLength,
452     IDWriteNumberSubstitution* numberSubstitution) { return S_OK; }
453
454 protected:
455   Run *FetchNextRun (IN OUT uint32_t* textLength)
456   {
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
459     // being returned.
460
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);
466     else
467       // Just advance the current run.
468       mCurrentRun = mCurrentRun->nextRun;
469     *textLength -= origRun->mTextLength;
470
471     // Return a reference to the run that was just current.
472     return origRun;
473   }
474
475   void SetCurrentRun (uint32_t textPosition)
476   {
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.
481
482     if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
483       return;
484
485     for (Run *run = &mRunHead; run; run = run->nextRun)
486       if (run->ContainsTextPosition (textPosition))
487       {
488         mCurrentRun = run;
489         return;
490       }
491     assert (0); // We should always be able to find the text position in one of our runs
492   }
493
494   void SplitCurrentRun (uint32_t splitPosition)
495   {
496     if (!mCurrentRun)
497     {
498       assert (0); // SplitCurrentRun called without current run
499       // Shouldn't be calling this when no current run is set!
500       return;
501     }
502     // Split the current run.
503     if (splitPosition <= mCurrentRun->mTextStart)
504     {
505       // No need to split, already the start of a run
506       // or before it. Usually the first.
507       return;
508     }
509     Run *newRun = new Run;
510
511     *newRun = *mCurrentRun;
512
513     // Insert the new run in our linked list.
514     newRun->nextRun = mCurrentRun->nextRun;
515     mCurrentRun->nextRun = newRun;
516
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;
523   }
524
525 protected:
526   // Input
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;
533
534   // Current processing state.
535   Run *mCurrentRun;
536
537   // Output is a list of runs starting here
538   Run  mRunHead;
539 };
540
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); }
545
546 /*
547  * shaper
548  */
549
550 static hb_bool_t
551 _hb_directwrite_shape_full (hb_shape_plan_t    *shape_plan,
552   hb_font_t          *font,
553   hb_buffer_t        *buffer,
554   const hb_feature_t *features,
555   unsigned int        num_features,
556   float               lineWidth)
557 {
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;
563
564   IDWriteTextAnalyzer* analyzer;
565   dwriteFactory->CreateTextAnalyzer (&analyzer);
566
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; \
571   { \
572     unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
573     assert (_consumed <= scratch_size); \
574     scratch += _consumed; \
575     scratch_size -= _consumed; \
576   }
577
578 #define utf16_index() var1.u32
579
580   ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2);
581
582   unsigned int chars_len = 0;
583   for (unsigned int i = 0; i < buffer->len; i++)
584   {
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;
591     else
592     {
593       textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
594       textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
595     }
596   }
597
598   ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
599   /* Need log_clusters to assign features. */
600   chars_len = 0;
601   for (unsigned int i = 0; i < buffer->len; i++)
602   {
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. */
608   }
609
610   // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
611
612   DWRITE_READING_DIRECTION readingDirection = buffer->props.direction ?
613     DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
614     DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
615
616   /*
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.
620   */
621   uint32_t textLength = buffer->len;
622
623   TextAnalysis analysis (textString, textLength, nullptr, readingDirection);
624   TextAnalysis::Run *runHead;
625   HRESULT hr;
626   hr = analysis.GenerateResults (analyzer, &runHead);
627
628 #define FAIL(...) \
629   HB_STMT_START { \
630     DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
631     return false; \
632   } HB_STMT_END;
633
634   if (FAILED (hr))
635     FAIL ("Analyzer failed to generate results.");
636
637   uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
638   uint32_t glyphCount;
639   bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
640
641   const wchar_t localeName[20] = {0};
642   if (buffer->props.language != nullptr)
643   {
644     mbstowcs ((wchar_t*) localeName,
645       hb_language_to_string (buffer->props.language), 20);
646   }
647
648   // TODO: it does work but doesn't care about ranges
649   DWRITE_TYPOGRAPHIC_FEATURES typographic_features;
650   typographic_features.featureCount = num_features;
651   if (num_features)
652   {
653     typographic_features.features = new DWRITE_FONT_FEATURE[num_features];
654     for (unsigned int i = 0; i < num_features; ++i)
655     {
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;
659     }
660   }
661   const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures =
662     (const DWRITE_TYPOGRAPHIC_FEATURES*) &typographic_features;
663   const uint32_t featureRangeLengths[] = { textLength };
664   //
665
666   uint16_t* clusterMap = new uint16_t[textLength];
667   DWRITE_SHAPING_TEXT_PROPERTIES* textProperties =
668     new DWRITE_SHAPING_TEXT_PROPERTIES[textLength];
669 retry_getglyphs:
670   uint16_t* glyphIndices = new uint16_t[maxGlyphCount];
671   DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties =
672     new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount];
673
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);
678
679   if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
680   {
681     delete [] glyphIndices;
682     delete [] glyphProperties;
683
684     maxGlyphCount *= 2;
685
686     goto retry_getglyphs;
687   }
688   if (FAILED (hr))
689     FAIL ("Analyzer failed to get glyphs.");
690
691   float* glyphAdvances = new float[maxGlyphCount];
692   DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
693
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)
697          / (sizeof (WORD) +
698             sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) +
699             sizeof (int) +
700             sizeof (DWRITE_GLYPH_OFFSET) +
701             sizeof (uint32_t));
702   ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
703
704 #undef ALLOCATE_ARRAY
705
706   int fontEmSize = font->face->get_upem ();
707   if (fontEmSize < 0)
708     fontEmSize = -fontEmSize;
709
710   if (fontEmSize < 0)
711     fontEmSize = -fontEmSize;
712   double x_mult = (double) font->x_scale / fontEmSize;
713   double y_mult = (double) font->y_scale / fontEmSize;
714
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);
721
722   if (FAILED (hr))
723     FAIL ("Analyzer failed to get glyph placements.");
724
725   IDWriteTextAnalyzer1* analyzer1;
726   analyzer->QueryInterface (&analyzer1);
727
728   if (analyzer1 && lineWidth)
729   {
730
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);
736
737     if (FAILED (hr))
738       FAIL ("Analyzer failed to get justification opportunities.");
739
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);
744
745     if (FAILED (hr))
746       FAIL ("Analyzer failed to get justified glyph advances.");
747
748     DWRITE_SCRIPT_PROPERTIES scriptProperties;
749     hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
750     if (FAILED (hr))
751       FAIL ("Analyzer failed to get script properties.");
752     uint32_t justificationCharacter = scriptProperties.justificationCharacter;
753
754     // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
755     if (justificationCharacter != 32)
756     {
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);
769
770       if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
771       {
772         maxGlyphCount = actualGlyphsCount;
773         delete [] modifiedGlyphIndices;
774         delete [] modifiedGlyphAdvances;
775         delete [] modifiedGlyphOffsets;
776
777         maxGlyphCount = actualGlyphsCount;
778
779         goto retry_getjustifiedglyphs;
780       }
781       if (FAILED (hr))
782         FAIL ("Analyzer failed to get justified glyphs.");
783
784       delete [] clusterMap;
785       delete [] glyphIndices;
786       delete [] glyphAdvances;
787       delete [] glyphOffsets;
788
789       glyphCount = actualGlyphsCount;
790       clusterMap = modifiedClusterMap;
791       glyphIndices = modifiedGlyphIndices;
792       glyphAdvances = modifiedGlyphAdvances;
793       glyphOffsets = modifiedGlyphOffsets;
794
795       delete [] justifiedGlyphAdvances;
796       delete [] justifiedGlyphOffsets;
797     }
798     else
799     {
800       delete [] glyphAdvances;
801       delete [] glyphOffsets;
802
803       glyphAdvances = justifiedGlyphAdvances;
804       glyphOffsets = justifiedGlyphOffsets;
805     }
806
807     delete [] justificationOpportunities;
808
809   }
810
811   /* Ok, we've got everything we need, now compose output buffer,
812    * very, *very*, carefully! */
813
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++)
818   {
819     uint32_t *p =
820       &vis_clusters[log_clusters[buffer->info[i].utf16_index ()]];
821     *p = MIN (*p, buffer->info[i].cluster);
822   }
823   for (unsigned int i = 1; i < glyphCount; i++)
824     if (vis_clusters[i] == -1)
825       vis_clusters[i] = vis_clusters[i - 1];
826
827 #undef utf16_index
828
829   if (unlikely (!buffer->ensure (glyphCount)))
830     FAIL ("Buffer in error");
831
832 #undef FAIL
833
834   /* Set glyph infos */
835   buffer->len = 0;
836   for (unsigned int i = 0; i < glyphCount; i++)
837   {
838     hb_glyph_info_t *info = &buffer->info[buffer->len++];
839
840     info->codepoint = glyphIndices[i];
841     info->cluster = vis_clusters[i];
842
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;
847   }
848
849   /* Set glyph positions */
850   buffer->clear_positions ();
851   for (unsigned int i = 0; i < glyphCount; i++)
852   {
853     hb_glyph_info_t *info = &buffer->info[i];
854     hb_glyph_position_t *pos = &buffer->pos[i];
855
856     /* TODO vertical */
857     pos->x_advance = x_mult * (int32_t) info->mask;
858     pos->x_offset =
859       x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
860     pos->y_offset = y_mult * info->var2.i32;
861   }
862
863   if (isRightToLeft)
864     hb_buffer_reverse (buffer);
865
866   delete [] clusterMap;
867   delete [] glyphIndices;
868   delete [] textProperties;
869   delete [] glyphProperties;
870   delete [] glyphAdvances;
871   delete [] glyphOffsets;
872
873   if (num_features)
874     delete [] typographic_features.features;
875
876   /* Wow, done! */
877   return true;
878 }
879
880 hb_bool_t
881 _hb_directwrite_shape (hb_shape_plan_t    *shape_plan,
882   hb_font_t          *font,
883   hb_buffer_t        *buffer,
884   const hb_feature_t *features,
885   unsigned int        num_features)
886 {
887   return _hb_directwrite_shape_full (shape_plan, font, buffer,
888     features, num_features, 0);
889 }
890
891 /*
892  * Public [experimental] API
893  */
894
895 hb_bool_t
896 hb_directwrite_shape_experimental_width (hb_font_t          *font,
897   hb_buffer_t        *buffer,
898   const hb_feature_t *features,
899   unsigned int        num_features,
900   float               width)
901 {
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);
907
908   buffer->unsafe_to_break_all ();
909
910   return res;
911 }