• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkFont.h"
9 #include "include/core/SkFontArguments.h"
10 #include "include/core/SkFontMetrics.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkFontTypes.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSpan.h"
19 #include "include/core/SkStream.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/private/SkBitmaskEnum.h"
23 #include "include/private/SkMalloc.h"
24 #include "include/private/SkMutex.h"
25 #include "include/private/SkTArray.h"
26 #include "include/private/SkTFitsIn.h"
27 #include "include/private/SkTemplates.h"
28 #include "include/private/SkTo.h"
29 #include "modules/skshaper/include/SkShaper.h"
30 #include "modules/skunicode/include/SkUnicode.h"
31 #include "src/core/SkLRUCache.h"
32 #include "src/core/SkTDPQueue.h"
33 #include "src/utils/SkUTF.h"
34 
35 #include <hb.h>
36 #include <hb-ot.h>
37 #include <cstring>
38 #include <memory>
39 #include <type_traits>
40 #include <utility>
41 
42 // HB_FEATURE_GLOBAL_START and HB_FEATURE_GLOBAL_END were not added until HarfBuzz 2.0
43 // They would have always worked, they just hadn't been named yet.
44 #if !defined(HB_FEATURE_GLOBAL_START)
45 #  define HB_FEATURE_GLOBAL_START 0
46 #endif
47 #if !defined(HB_FEATURE_GLOBAL_END)
48 # define HB_FEATURE_GLOBAL_END ((unsigned int) -1)
49 #endif
50 
51 namespace sknonstd {
52 template <> struct is_bitmask_enum<hb_buffer_flags_t> : std::true_type {};
53 }  // namespace sknonstd
54 
55 namespace {
56 template <typename T,typename P,P* p> using resource = std::unique_ptr<T, SkFunctionWrapper<P, p>>;
57 using HBBlob   = resource<hb_blob_t     , decltype(hb_blob_destroy)  , hb_blob_destroy  >;
58 using HBFace   = resource<hb_face_t     , decltype(hb_face_destroy)  , hb_face_destroy  >;
59 using HBFont   = resource<hb_font_t     , decltype(hb_font_destroy)  , hb_font_destroy  >;
60 using HBBuffer = resource<hb_buffer_t   , decltype(hb_buffer_destroy), hb_buffer_destroy>;
61 
62 using SkUnicodeBidi = std::unique_ptr<SkBidiIterator>;
63 using SkUnicodeBreak = std::unique_ptr<SkBreakIterator>;
64 using SkUnicodeScript = std::unique_ptr<SkScriptIterator>;
65 
skhb_position(SkScalar value)66 hb_position_t skhb_position(SkScalar value) {
67     // Treat HarfBuzz hb_position_t as 16.16 fixed-point.
68     constexpr int kHbPosition1 = 1 << 16;
69     return SkScalarRoundToInt(value * kHbPosition1);
70 }
71 
skhb_glyph(hb_font_t * hb_font,void * font_data,hb_codepoint_t unicode,hb_codepoint_t variation_selector,hb_codepoint_t * glyph,void * user_data)72 hb_bool_t skhb_glyph(hb_font_t* hb_font,
73                      void* font_data,
74                      hb_codepoint_t unicode,
75                      hb_codepoint_t variation_selector,
76                      hb_codepoint_t* glyph,
77                      void* user_data) {
78     SkFont& font = *reinterpret_cast<SkFont*>(font_data);
79 
80     *glyph = font.unicharToGlyph(unicode);
81     return *glyph != 0;
82 }
83 
skhb_nominal_glyph(hb_font_t * hb_font,void * font_data,hb_codepoint_t unicode,hb_codepoint_t * glyph,void * user_data)84 hb_bool_t skhb_nominal_glyph(hb_font_t* hb_font,
85                              void* font_data,
86                              hb_codepoint_t unicode,
87                              hb_codepoint_t* glyph,
88                              void* user_data) {
89   return skhb_glyph(hb_font, font_data, unicode, 0, glyph, user_data);
90 }
91 
skhb_nominal_glyphs(hb_font_t * hb_font,void * font_data,unsigned int count,const hb_codepoint_t * unicodes,unsigned int unicode_stride,hb_codepoint_t * glyphs,unsigned int glyph_stride,void * user_data)92 unsigned skhb_nominal_glyphs(hb_font_t *hb_font, void *font_data,
93                              unsigned int count,
94                              const hb_codepoint_t *unicodes,
95                              unsigned int unicode_stride,
96                              hb_codepoint_t *glyphs,
97                              unsigned int glyph_stride,
98                              void *user_data) {
99     SkFont& font = *reinterpret_cast<SkFont*>(font_data);
100 
101     // Batch call textToGlyphs since entry cost is not cheap.
102     // Copy requred because textToGlyphs is dense and hb is strided.
103     SkAutoSTMalloc<256, SkUnichar> unicode(count);
104     for (unsigned i = 0; i < count; i++) {
105         unicode[i] = *unicodes;
106         unicodes = SkTAddOffset<const hb_codepoint_t>(unicodes, unicode_stride);
107     }
108     SkAutoSTMalloc<256, SkGlyphID> glyph(count);
109     font.textToGlyphs(unicode.get(), count * sizeof(SkUnichar), SkTextEncoding::kUTF32,
110                         glyph.get(), count);
111 
112     // Copy the results back to the sparse array.
113     unsigned int done;
114     for (done = 0; done < count && glyph[done] != 0; done++) {
115         *glyphs = glyph[done];
116         glyphs = SkTAddOffset<hb_codepoint_t>(glyphs, glyph_stride);
117     }
118     // return 'done' to allow HarfBuzz to synthesize with NFC and spaces, return 'count' to avoid
119     return done;
120 }
121 
skhb_glyph_h_advance(hb_font_t * hb_font,void * font_data,hb_codepoint_t hbGlyph,void * user_data)122 hb_position_t skhb_glyph_h_advance(hb_font_t* hb_font,
123                                    void* font_data,
124                                    hb_codepoint_t hbGlyph,
125                                    void* user_data) {
126     SkFont& font = *reinterpret_cast<SkFont*>(font_data);
127 
128     SkScalar advance;
129     SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph);
130 
131     font.getWidths(&skGlyph, 1, &advance);
132     if (!font.isSubpixel()) {
133         advance = SkScalarRoundToInt(advance);
134     }
135     return skhb_position(advance);
136 }
137 
skhb_glyph_h_advances(hb_font_t * hb_font,void * font_data,unsigned count,const hb_codepoint_t * glyphs,unsigned int glyph_stride,hb_position_t * advances,unsigned int advance_stride,void * user_data)138 void skhb_glyph_h_advances(hb_font_t* hb_font,
139                            void* font_data,
140                            unsigned count,
141                            const hb_codepoint_t* glyphs,
142                            unsigned int glyph_stride,
143                            hb_position_t* advances,
144                            unsigned int advance_stride,
145                            void* user_data) {
146     SkFont& font = *reinterpret_cast<SkFont*>(font_data);
147 
148     // Batch call getWidths since entry cost is not cheap.
149     // Copy requred because getWidths is dense and hb is strided.
150     SkAutoSTMalloc<256, SkGlyphID> glyph(count);
151     for (unsigned i = 0; i < count; i++) {
152         glyph[i] = *glyphs;
153         glyphs = SkTAddOffset<const hb_codepoint_t>(glyphs, glyph_stride);
154     }
155     SkAutoSTMalloc<256, SkScalar> advance(count);
156     font.getWidths(glyph.get(), count, advance.get());
157 
158     if (!font.isSubpixel()) {
159         for (unsigned i = 0; i < count; i++) {
160             advance[i] = SkScalarRoundToInt(advance[i]);
161         }
162     }
163 
164     // Copy the results back to the sparse array.
165     for (unsigned i = 0; i < count; i++) {
166         *advances = skhb_position(advance[i]);
167         advances = SkTAddOffset<hb_position_t>(advances, advance_stride);
168     }
169 }
170 
171 // HarfBuzz callback to retrieve glyph extents, mainly used by HarfBuzz for
172 // fallback mark positioning, i.e. the situation when the font does not have
173 // mark anchors or other mark positioning rules, but instead HarfBuzz is
174 // supposed to heuristically place combining marks around base glyphs. HarfBuzz
175 // does this by measuring "ink boxes" of glyphs, and placing them according to
176 // Unicode mark classes. Above, below, centered or left or right, etc.
skhb_glyph_extents(hb_font_t * hb_font,void * font_data,hb_codepoint_t hbGlyph,hb_glyph_extents_t * extents,void * user_data)177 hb_bool_t skhb_glyph_extents(hb_font_t* hb_font,
178                              void* font_data,
179                              hb_codepoint_t hbGlyph,
180                              hb_glyph_extents_t* extents,
181                              void* user_data) {
182     SkFont& font = *reinterpret_cast<SkFont*>(font_data);
183     SkASSERT(extents);
184 
185     SkRect sk_bounds;
186     SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph);
187 
188     font.getWidths(&skGlyph, 1, nullptr, &sk_bounds);
189     if (!font.isSubpixel()) {
190         sk_bounds.set(sk_bounds.roundOut());
191     }
192 
193     // Skia is y-down but HarfBuzz is y-up.
194     extents->x_bearing = skhb_position(sk_bounds.fLeft);
195     extents->y_bearing = skhb_position(-sk_bounds.fTop);
196     extents->width = skhb_position(sk_bounds.width());
197     extents->height = skhb_position(-sk_bounds.height());
198     return true;
199 }
200 
201 #define SK_HB_VERSION_CHECK(x, y, z) \
202     (HB_VERSION_MAJOR >  (x)) || \
203     (HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR >  (y)) || \
204     (HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR == (y) && HB_VERSION_MICRO >= (z))
205 
skhb_get_font_funcs()206 hb_font_funcs_t* skhb_get_font_funcs() {
207     static hb_font_funcs_t* const funcs = []{
208         // HarfBuzz will use the default (parent) implementation if they aren't set.
209         hb_font_funcs_t* const funcs = hb_font_funcs_create();
210         hb_font_funcs_set_variation_glyph_func(funcs, skhb_glyph, nullptr, nullptr);
211         hb_font_funcs_set_nominal_glyph_func(funcs, skhb_nominal_glyph, nullptr, nullptr);
212 #if SK_HB_VERSION_CHECK(2, 0, 0)
213         hb_font_funcs_set_nominal_glyphs_func(funcs, skhb_nominal_glyphs, nullptr, nullptr);
214 #else
215         sk_ignore_unused_variable(skhb_nominal_glyphs);
216 #endif
217         hb_font_funcs_set_glyph_h_advance_func(funcs, skhb_glyph_h_advance, nullptr, nullptr);
218 #if SK_HB_VERSION_CHECK(1, 8, 6)
219         hb_font_funcs_set_glyph_h_advances_func(funcs, skhb_glyph_h_advances, nullptr, nullptr);
220 #else
221         sk_ignore_unused_variable(skhb_glyph_h_advances);
222 #endif
223         hb_font_funcs_set_glyph_extents_func(funcs, skhb_glyph_extents, nullptr, nullptr);
224         hb_font_funcs_make_immutable(funcs);
225         return funcs;
226     }();
227     SkASSERT(funcs);
228     return funcs;
229 }
230 
skhb_get_table(hb_face_t * face,hb_tag_t tag,void * user_data)231 hb_blob_t* skhb_get_table(hb_face_t* face, hb_tag_t tag, void* user_data) {
232     SkTypeface& typeface = *reinterpret_cast<SkTypeface*>(user_data);
233 
234     auto data = typeface.copyTableData(tag);
235     if (!data) {
236         return nullptr;
237     }
238     SkData* rawData = data.release();
239     return hb_blob_create(reinterpret_cast<char*>(rawData->writable_data()), rawData->size(),
240                           HB_MEMORY_MODE_READONLY, rawData, [](void* ctx) {
241                               SkSafeUnref(((SkData*)ctx));
242                           });
243 }
244 
stream_to_blob(std::unique_ptr<SkStreamAsset> asset)245 HBBlob stream_to_blob(std::unique_ptr<SkStreamAsset> asset) {
246     size_t size = asset->getLength();
247     HBBlob blob;
248     if (const void* base = asset->getMemoryBase()) {
249         blob.reset(hb_blob_create((char*)base, SkToUInt(size),
250                                   HB_MEMORY_MODE_READONLY, asset.release(),
251                                   [](void* p) { delete (SkStreamAsset*)p; }));
252     } else {
253         // SkDebugf("Extra SkStreamAsset copy\n");
254         void* ptr = size ? sk_malloc_throw(size) : nullptr;
255         asset->read(ptr, size);
256         blob.reset(hb_blob_create((char*)ptr, SkToUInt(size),
257                                   HB_MEMORY_MODE_READONLY, ptr, sk_free));
258     }
259     SkASSERT(blob);
260     hb_blob_make_immutable(blob.get());
261     return blob;
262 }
263 
SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;)264 SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;)
265 
266 HBFace create_hb_face(const SkTypeface& typeface) {
267     int index;
268     std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openStream(&index);
269     HBFace face;
270     if (typefaceAsset && typefaceAsset->getMemoryBase()) {
271         HBBlob blob(stream_to_blob(std::move(typefaceAsset)));
272         // hb_face_create always succeeds. Check that the format is minimally recognized first.
273         // hb_face_create_for_tables may still create a working hb_face.
274         // See https://github.com/harfbuzz/harfbuzz/issues/248 .
275         unsigned int num_hb_faces = hb_face_count(blob.get());
276         if (0 < num_hb_faces && (unsigned)index < num_hb_faces) {
277             face.reset(hb_face_create(blob.get(), (unsigned)index));
278             // Check the number of glyphs as a basic sanitization step.
279             if (face && hb_face_get_glyph_count(face.get()) == 0) {
280                 face.reset();
281             }
282         }
283     }
284     if (!face) {
285         face.reset(hb_face_create_for_tables(
286             skhb_get_table,
287             const_cast<SkTypeface*>(SkRef(&typeface)),
__anond40e83c50502(void* user_data)288             [](void* user_data){ SkSafeUnref(reinterpret_cast<SkTypeface*>(user_data)); }));
289     }
290     SkASSERT(face);
291     if (!face) {
292         return nullptr;
293     }
294     hb_face_set_index(face.get(), (unsigned)index);
295     hb_face_set_upem(face.get(), typeface.getUnitsPerEm());
296 
297     SkDEBUGCODE(
298         hb_face_set_user_data(face.get(), &gDataIdKey, const_cast<SkTypeface*>(&typeface),
299                               nullptr, false);
300     )
301 
302     return face;
303 }
304 
create_hb_font(const SkFont & font,const HBFace & face)305 HBFont create_hb_font(const SkFont& font, const HBFace& face) {
306     SkDEBUGCODE(
307         void* dataId = hb_face_get_user_data(face.get(), &gDataIdKey);
308         SkASSERT(dataId == font.getTypeface());
309     )
310 
311     HBFont otFont(hb_font_create(face.get()));
312     SkASSERT(otFont);
313     if (!otFont) {
314         return nullptr;
315     }
316     hb_ot_font_set_funcs(otFont.get());
317     int axis_count = font.getTypeface()->getVariationDesignPosition(nullptr, 0);
318     if (axis_count > 0) {
319         SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate> axis_values(axis_count);
320         if (font.getTypeface()->getVariationDesignPosition(axis_values, axis_count) == axis_count) {
321             hb_font_set_variations(otFont.get(),
322                                    reinterpret_cast<hb_variation_t*>(axis_values.get()),
323                                    axis_count);
324         }
325     }
326 
327     // Creating a sub font means that non-available functions
328     // are found from the parent.
329     HBFont skFont(hb_font_create_sub_font(otFont.get()));
330     hb_font_set_funcs(skFont.get(), skhb_get_font_funcs(),
331                       reinterpret_cast<void *>(new SkFont(font)),
332                       [](void* user_data){ delete reinterpret_cast<SkFont*>(user_data); });
333     int scale = skhb_position(font.getSize());
334     hb_font_set_scale(skFont.get(), scale, scale);
335 
336     return skFont;
337 }
338 
339 /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
utf8_next(const char ** ptr,const char * end)340 static inline SkUnichar utf8_next(const char** ptr, const char* end) {
341     SkUnichar val = SkUTF::NextUTF8(ptr, end);
342     return val < 0 ? 0xFFFD : val;
343 }
344 
345 class SkUnicodeBidiRunIterator final : public SkShaper::BiDiRunIterator {
346 public:
SkUnicodeBidiRunIterator(const char * utf8,const char * end,SkUnicodeBidi bidi)347     SkUnicodeBidiRunIterator(const char* utf8, const char* end, SkUnicodeBidi bidi)
348         : fBidi(std::move(bidi))
349         , fEndOfCurrentRun(utf8)
350         , fBegin(utf8)
351         , fEnd(end)
352         , fUTF16LogicalPosition(0)
353         , fLevel(SkBidiIterator::kLTR)
354     {}
355 
consume()356     void consume() override {
357         SkASSERT(fUTF16LogicalPosition < fBidi->getLength());
358         int32_t endPosition = fBidi->getLength();
359         fLevel = fBidi->getLevelAt(fUTF16LogicalPosition);
360         SkUnichar u = utf8_next(&fEndOfCurrentRun, fEnd);
361         fUTF16LogicalPosition += SkUTF::ToUTF16(u);
362         SkBidiIterator::Level level;
363         while (fUTF16LogicalPosition < endPosition) {
364             level = fBidi->getLevelAt(fUTF16LogicalPosition);
365             if (level != fLevel) {
366                 break;
367             }
368             u = utf8_next(&fEndOfCurrentRun, fEnd);
369 
370             fUTF16LogicalPosition += SkUTF::ToUTF16(u);
371         }
372     }
endOfCurrentRun() const373     size_t endOfCurrentRun() const override {
374         return fEndOfCurrentRun - fBegin;
375     }
atEnd() const376     bool atEnd() const override {
377         return fUTF16LogicalPosition == fBidi->getLength();
378     }
currentLevel() const379     SkBidiIterator::Level currentLevel() const override {
380         return fLevel;
381     }
382 private:
383     SkUnicodeBidi fBidi;
384     char const * fEndOfCurrentRun;
385     char const * const fBegin;
386     char const * const fEnd;
387     int32_t fUTF16LogicalPosition;
388     SkBidiIterator::Level fLevel;
389 };
390 
391 class SkUnicodeHbScriptRunIterator final: public SkShaper::ScriptRunIterator {
392 public:
SkUnicodeHbScriptRunIterator(SkUnicodeScript,const char * utf8,size_t utf8Bytes)393     SkUnicodeHbScriptRunIterator(SkUnicodeScript, const char* utf8, size_t utf8Bytes)
394         : fCurrent(utf8), fBegin(utf8), fEnd(fCurrent + utf8Bytes)
395         , fCurrentScript(HB_SCRIPT_UNKNOWN)
396     {}
hb_script_for_unichar(SkUnichar u)397     hb_script_t hb_script_for_unichar(SkUnichar u) {
398          return hb_unicode_script(hb_unicode_funcs_get_default(), u);
399     }
consume()400     void consume() override {
401         SkASSERT(fCurrent < fEnd);
402         SkUnichar u = utf8_next(&fCurrent, fEnd);
403         fCurrentScript = hb_script_for_unichar(u);
404         while (fCurrent < fEnd) {
405             const char* prev = fCurrent;
406             u = utf8_next(&fCurrent, fEnd);
407             const hb_script_t script = hb_script_for_unichar(u);
408             if (script != fCurrentScript) {
409                 if (fCurrentScript == HB_SCRIPT_INHERITED || fCurrentScript == HB_SCRIPT_COMMON) {
410                     fCurrentScript = script;
411                 } else if (script == HB_SCRIPT_INHERITED || script == HB_SCRIPT_COMMON) {
412                     continue;
413                 } else {
414                     fCurrent = prev;
415                     break;
416                 }
417             }
418         }
419         if (fCurrentScript == HB_SCRIPT_INHERITED) {
420             fCurrentScript = HB_SCRIPT_COMMON;
421         }
422     }
endOfCurrentRun() const423     size_t endOfCurrentRun() const override {
424         return fCurrent - fBegin;
425     }
atEnd() const426     bool atEnd() const override {
427         return fCurrent == fEnd;
428     }
429 
currentScript() const430     SkFourByteTag currentScript() const override {
431         return SkSetFourByteTag(HB_UNTAG(fCurrentScript));
432     }
433 private:
434     char const * fCurrent;
435     char const * const fBegin;
436     char const * const fEnd;
437     hb_script_t fCurrentScript;
438 };
439 
440 class RunIteratorQueue {
441 public:
insert(SkShaper::RunIterator * runIterator,int priority)442     void insert(SkShaper::RunIterator* runIterator, int priority) {
443         fEntries.insert({runIterator, priority});
444     }
445 
advanceRuns()446     bool advanceRuns() {
447         const SkShaper::RunIterator* leastRun = fEntries.peek().runIterator;
448         if (leastRun->atEnd()) {
449             SkASSERT(this->allRunsAreAtEnd());
450             return false;
451         }
452         const size_t leastEnd = leastRun->endOfCurrentRun();
453         SkShaper::RunIterator* currentRun = nullptr;
454         SkDEBUGCODE(size_t previousEndOfCurrentRun);
455         while ((currentRun = fEntries.peek().runIterator)->endOfCurrentRun() <= leastEnd) {
456             int priority = fEntries.peek().priority;
457             fEntries.pop();
458             SkDEBUGCODE(previousEndOfCurrentRun = currentRun->endOfCurrentRun());
459             currentRun->consume();
460             SkASSERT(previousEndOfCurrentRun < currentRun->endOfCurrentRun());
461             fEntries.insert({currentRun, priority});
462         }
463         return true;
464     }
465 
endOfCurrentRun() const466     size_t endOfCurrentRun() const {
467         return fEntries.peek().runIterator->endOfCurrentRun();
468     }
469 
470 private:
allRunsAreAtEnd() const471     bool allRunsAreAtEnd() const {
472         for (int i = 0; i < fEntries.count(); ++i) {
473             if (!fEntries.at(i).runIterator->atEnd()) {
474                 return false;
475             }
476         }
477         return true;
478     }
479 
480     struct Entry {
481         SkShaper::RunIterator* runIterator;
482         int priority;
483     };
CompareEntry(Entry const & a,Entry const & b)484     static bool CompareEntry(Entry const& a, Entry const& b) {
485         size_t aEnd = a.runIterator->endOfCurrentRun();
486         size_t bEnd = b.runIterator->endOfCurrentRun();
487         return aEnd  < bEnd || (aEnd == bEnd && a.priority < b.priority);
488     }
489     SkTDPQueue<Entry, CompareEntry> fEntries;
490 };
491 
492 struct ShapedGlyph {
493     SkGlyphID fID;
494     uint32_t fCluster;
495     SkPoint fOffset;
496     SkVector fAdvance;
497     bool fMayLineBreakBefore;
498     bool fMustLineBreakBefore;
499     bool fHasVisual;
500     bool fGraphemeBreakBefore;
501     bool fUnsafeToBreak;
502 };
503 struct ShapedRun {
ShapedRun__anond40e83c50111::ShapedRun504     ShapedRun(SkShaper::RunHandler::Range utf8Range, const SkFont& font, SkBidiIterator::Level level,
505               std::unique_ptr<ShapedGlyph[]> glyphs, size_t numGlyphs, SkVector advance = {0, 0})
506         : fUtf8Range(utf8Range), fFont(font), fLevel(level)
507         , fGlyphs(std::move(glyphs)), fNumGlyphs(numGlyphs), fAdvance(advance)
508     {}
509 
510     SkShaper::RunHandler::Range fUtf8Range;
511     SkFont fFont;
512     SkBidiIterator::Level fLevel;
513     std::unique_ptr<ShapedGlyph[]> fGlyphs;
514     size_t fNumGlyphs;
515     SkVector fAdvance;
516 };
517 struct ShapedLine {
518     SkTArray<ShapedRun> runs;
519     SkVector fAdvance = { 0, 0 };
520 };
521 
is_LTR(SkBidiIterator::Level level)522 constexpr bool is_LTR(SkBidiIterator::Level level) {
523     return (level & 1) == 0;
524 }
525 
append(SkShaper::RunHandler * handler,const SkShaper::RunHandler::RunInfo & runInfo,const ShapedRun & run,size_t startGlyphIndex,size_t endGlyphIndex)526 void append(SkShaper::RunHandler* handler, const SkShaper::RunHandler::RunInfo& runInfo,
527                    const ShapedRun& run, size_t startGlyphIndex, size_t endGlyphIndex) {
528     SkASSERT(startGlyphIndex <= endGlyphIndex);
529     const size_t glyphLen = endGlyphIndex - startGlyphIndex;
530 
531     const auto buffer = handler->runBuffer(runInfo);
532     SkASSERT(buffer.glyphs);
533     SkASSERT(buffer.positions);
534 
535     SkVector advance = {0,0};
536     for (size_t i = 0; i < glyphLen; i++) {
537         // Glyphs are in logical order, but output ltr since PDF readers seem to expect that.
538         const ShapedGlyph& glyph = run.fGlyphs[is_LTR(run.fLevel) ? startGlyphIndex + i
539                                                                   : endGlyphIndex - 1 - i];
540         buffer.glyphs[i] = glyph.fID;
541         if (buffer.offsets) {
542             buffer.positions[i] = advance + buffer.point;
543             buffer.offsets[i] = glyph.fOffset;
544         } else {
545             buffer.positions[i] = advance + buffer.point + glyph.fOffset;
546         }
547         if (buffer.clusters) {
548             buffer.clusters[i] = glyph.fCluster;
549         }
550         advance += glyph.fAdvance;
551     }
552     handler->commitRunBuffer(runInfo);
553 }
554 
emit(const ShapedLine & line,SkShaper::RunHandler * handler)555 void emit(const ShapedLine& line, SkShaper::RunHandler* handler) {
556     // Reorder the runs and glyphs per line and write them out.
557     handler->beginLine();
558 
559     int numRuns = line.runs.size();
560     SkAutoSTMalloc<4, SkBidiIterator::Level> runLevels(numRuns);
561     for (int i = 0; i < numRuns; ++i) {
562         runLevels[i] = line.runs[i].fLevel;
563     }
564     SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns);
565     SkBidiIterator::ReorderVisual(runLevels, numRuns, logicalFromVisual);
566 
567     for (int i = 0; i < numRuns; ++i) {
568         int logicalIndex = logicalFromVisual[i];
569 
570         const auto& run = line.runs[logicalIndex];
571         const SkShaper::RunHandler::RunInfo info = {
572             run.fFont,
573             run.fLevel,
574             run.fAdvance,
575             run.fNumGlyphs,
576             run.fUtf8Range
577         };
578         handler->runInfo(info);
579     }
580     handler->commitRunInfo();
581     for (int i = 0; i < numRuns; ++i) {
582         int logicalIndex = logicalFromVisual[i];
583 
584         const auto& run = line.runs[logicalIndex];
585         const SkShaper::RunHandler::RunInfo info = {
586             run.fFont,
587             run.fLevel,
588             run.fAdvance,
589             run.fNumGlyphs,
590             run.fUtf8Range
591         };
592         append(handler, info, run, 0, run.fNumGlyphs);
593     }
594 
595     handler->commitLine();
596 }
597 
598 struct ShapedRunGlyphIterator {
ShapedRunGlyphIterator__anond40e83c50111::ShapedRunGlyphIterator599     ShapedRunGlyphIterator(const SkTArray<ShapedRun>& origRuns)
600         : fRuns(&origRuns), fRunIndex(0), fGlyphIndex(0)
601     { }
602 
603     ShapedRunGlyphIterator(const ShapedRunGlyphIterator& that) = default;
604     ShapedRunGlyphIterator& operator=(const ShapedRunGlyphIterator& that) = default;
operator ==__anond40e83c50111::ShapedRunGlyphIterator605     bool operator==(const ShapedRunGlyphIterator& that) const {
606         return fRuns == that.fRuns &&
607                fRunIndex == that.fRunIndex &&
608                fGlyphIndex == that.fGlyphIndex;
609     }
operator !=__anond40e83c50111::ShapedRunGlyphIterator610     bool operator!=(const ShapedRunGlyphIterator& that) const {
611         return fRuns != that.fRuns ||
612                fRunIndex != that.fRunIndex ||
613                fGlyphIndex != that.fGlyphIndex;
614     }
615 
next__anond40e83c50111::ShapedRunGlyphIterator616     ShapedGlyph* next() {
617         const SkTArray<ShapedRun>& runs = *fRuns;
618         SkASSERT(fRunIndex < runs.count());
619         SkASSERT(fGlyphIndex < runs[fRunIndex].fNumGlyphs);
620 
621         ++fGlyphIndex;
622         if (fGlyphIndex == runs[fRunIndex].fNumGlyphs) {
623             fGlyphIndex = 0;
624             ++fRunIndex;
625             if (fRunIndex >= runs.count()) {
626                 return nullptr;
627             }
628         }
629         return &runs[fRunIndex].fGlyphs[fGlyphIndex];
630     }
631 
current__anond40e83c50111::ShapedRunGlyphIterator632     ShapedGlyph* current() {
633         const SkTArray<ShapedRun>& runs = *fRuns;
634         if (fRunIndex >= runs.count()) {
635             return nullptr;
636         }
637         return &runs[fRunIndex].fGlyphs[fGlyphIndex];
638     }
639 
640     const SkTArray<ShapedRun>* fRuns;
641     int fRunIndex;
642     size_t fGlyphIndex;
643 };
644 
645 class ShaperHarfBuzz : public SkShaper {
646 public:
647     ShaperHarfBuzz(std::unique_ptr<SkUnicode>,
648                    SkUnicodeBreak line,
649                    SkUnicodeBreak grapheme,
650                    HBBuffer,
651                    sk_sp<SkFontMgr>);
652 
653 protected:
654     std::unique_ptr<SkUnicode> fUnicode;
655     SkUnicodeBreak fLineBreakIterator;
656     SkUnicodeBreak fGraphemeBreakIterator;
657 
658     ShapedRun shape(const char* utf8, size_t utf8Bytes,
659                     const char* utf8Start,
660                     const char* utf8End,
661                     const BiDiRunIterator&,
662                     const LanguageRunIterator&,
663                     const ScriptRunIterator&,
664                     const FontRunIterator&,
665                     const Feature*, size_t featuresSize) const;
666 private:
667     const sk_sp<SkFontMgr> fFontMgr;
668     HBBuffer               fBuffer;
669     hb_language_t          fUndefinedLanguage;
670 
671     void shape(const char* utf8, size_t utf8Bytes,
672                const SkFont&,
673                bool leftToRight,
674                SkScalar width,
675                RunHandler*) const override;
676 
677     void shape(const char* utf8Text, size_t textBytes,
678                FontRunIterator&,
679                BiDiRunIterator&,
680                ScriptRunIterator&,
681                LanguageRunIterator&,
682                SkScalar width,
683                RunHandler*) const override;
684 
685     void shape(const char* utf8Text, size_t textBytes,
686                FontRunIterator&,
687                BiDiRunIterator&,
688                ScriptRunIterator&,
689                LanguageRunIterator&,
690                const Feature*, size_t featuresSize,
691                SkScalar width,
692                RunHandler*) const override;
693 
694     virtual void wrap(char const * const utf8, size_t utf8Bytes,
695                       const BiDiRunIterator&,
696                       const LanguageRunIterator&,
697                       const ScriptRunIterator&,
698                       const FontRunIterator&,
699                       RunIteratorQueue& runSegmenter,
700                       const Feature*, size_t featuresSize,
701                       SkScalar width,
702                       RunHandler*) const = 0;
703 };
704 
705 class ShaperDrivenWrapper : public ShaperHarfBuzz {
706 public:
707     using ShaperHarfBuzz::ShaperHarfBuzz;
708 private:
709     void wrap(char const * const utf8, size_t utf8Bytes,
710               const BiDiRunIterator&,
711               const LanguageRunIterator&,
712               const ScriptRunIterator&,
713               const FontRunIterator&,
714               RunIteratorQueue& runSegmenter,
715               const Feature*, size_t featuresSize,
716               SkScalar width,
717               RunHandler*) const override;
718 };
719 
720 class ShapeThenWrap : public ShaperHarfBuzz {
721 public:
722     using ShaperHarfBuzz::ShaperHarfBuzz;
723 private:
724     void wrap(char const * const utf8, size_t utf8Bytes,
725               const BiDiRunIterator&,
726               const LanguageRunIterator&,
727               const ScriptRunIterator&,
728               const FontRunIterator&,
729               RunIteratorQueue& runSegmenter,
730               const Feature*, size_t featuresSize,
731               SkScalar width,
732               RunHandler*) const override;
733 };
734 
735 class ShapeDontWrapOrReorder : public ShaperHarfBuzz {
736 public:
737     using ShaperHarfBuzz::ShaperHarfBuzz;
738 private:
739     void wrap(char const * const utf8, size_t utf8Bytes,
740               const BiDiRunIterator&,
741               const LanguageRunIterator&,
742               const ScriptRunIterator&,
743               const FontRunIterator&,
744               RunIteratorQueue& runSegmenter,
745               const Feature*, size_t featuresSize,
746               SkScalar width,
747               RunHandler*) const override;
748 };
749 
MakeHarfBuzz(sk_sp<SkFontMgr> fontmgr,bool correct)750 static std::unique_ptr<SkShaper> MakeHarfBuzz(sk_sp<SkFontMgr> fontmgr, bool correct) {
751     HBBuffer buffer(hb_buffer_create());
752     if (!buffer) {
753         SkDEBUGF("Could not create hb_buffer");
754         return nullptr;
755     }
756 
757     auto unicode = SkUnicode::Make();
758     if (!unicode) {
759         return nullptr;
760     }
761     auto lineIter = unicode->makeBreakIterator("th", SkUnicode::BreakType::kLines);
762     if (!lineIter) {
763         return nullptr;
764     }
765     auto graphIter = unicode->makeBreakIterator("th", SkUnicode::BreakType::kGraphemes);
766     if (!graphIter) {
767         return nullptr;
768     }
769 
770     if (correct) {
771         return std::make_unique<ShaperDrivenWrapper>(std::move(unicode),
772             std::move(lineIter), std::move(graphIter), std::move(buffer), std::move(fontmgr));
773     } else {
774         return std::make_unique<ShapeThenWrap>(std::move(unicode),
775             std::move(lineIter), std::move(graphIter), std::move(buffer), std::move(fontmgr));
776     }
777 }
778 
ShaperHarfBuzz(std::unique_ptr<SkUnicode> unicode,SkUnicodeBreak lineIter,SkUnicodeBreak graphIter,HBBuffer buffer,sk_sp<SkFontMgr> fontmgr)779 ShaperHarfBuzz::ShaperHarfBuzz(std::unique_ptr<SkUnicode> unicode,
780     SkUnicodeBreak lineIter, SkUnicodeBreak graphIter, HBBuffer buffer, sk_sp<SkFontMgr> fontmgr)
781     : fUnicode(std::move(unicode))
782     , fLineBreakIterator(std::move(lineIter))
783     , fGraphemeBreakIterator(std::move(graphIter))
784     , fFontMgr(std::move(fontmgr))
785     , fBuffer(std::move(buffer))
786     , fUndefinedLanguage(hb_language_from_string("und", -1))
787 { }
788 
shape(const char * utf8,size_t utf8Bytes,const SkFont & srcFont,bool leftToRight,SkScalar width,RunHandler * handler) const789 void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
790                            const SkFont& srcFont,
791                            bool leftToRight,
792                            SkScalar width,
793                            RunHandler* handler) const
794 {
795     SkBidiIterator::Level defaultLevel = leftToRight ? SkBidiIterator::kLTR : SkBidiIterator::kRTL;
796     std::unique_ptr<BiDiRunIterator> bidi(MakeSkUnicodeBidiRunIterator(fUnicode.get(),
797                                                                        utf8,
798                                                                        utf8Bytes,
799                                                                        defaultLevel));
800 
801     if (!bidi) {
802         return;
803     }
804 
805     std::unique_ptr<LanguageRunIterator> language(MakeStdLanguageRunIterator(utf8, utf8Bytes));
806     if (!language) {
807         return;
808     }
809 
810     std::unique_ptr<ScriptRunIterator> script(MakeSkUnicodeHbScriptRunIterator(fUnicode.get(),
811                                                                                        utf8,
812                                                                                        utf8Bytes));
813     if (!script) {
814         return;
815     }
816 
817     std::unique_ptr<FontRunIterator> font(
818                 MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont,
819                                        fFontMgr ? fFontMgr : SkFontMgr::RefDefault()));
820     if (!font) {
821         return;
822     }
823 
824     this->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, width, handler);
825 }
826 
shape(const char * utf8,size_t utf8Bytes,FontRunIterator & font,BiDiRunIterator & bidi,ScriptRunIterator & script,LanguageRunIterator & language,SkScalar width,RunHandler * handler) const827 void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
828                            FontRunIterator& font,
829                            BiDiRunIterator& bidi,
830                            ScriptRunIterator& script,
831                            LanguageRunIterator& language,
832                            SkScalar width,
833                            RunHandler* handler) const
834 {
835     this->shape(utf8, utf8Bytes, font, bidi, script, language, nullptr, 0, width, handler);
836 }
837 
shape(const char * utf8,size_t utf8Bytes,FontRunIterator & font,BiDiRunIterator & bidi,ScriptRunIterator & script,LanguageRunIterator & language,const Feature * features,size_t featuresSize,SkScalar width,RunHandler * handler) const838 void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
839                            FontRunIterator& font,
840                            BiDiRunIterator& bidi,
841                            ScriptRunIterator& script,
842                            LanguageRunIterator& language,
843                            const Feature* features, size_t featuresSize,
844                            SkScalar width,
845                            RunHandler* handler) const
846 {
847     SkASSERT(handler);
848     RunIteratorQueue runSegmenter;
849     runSegmenter.insert(&font,     3); // The font iterator is always run last in case of tie.
850     runSegmenter.insert(&bidi,     2);
851     runSegmenter.insert(&script,   1);
852     runSegmenter.insert(&language, 0);
853 
854     this->wrap(utf8, utf8Bytes, bidi, language, script, font, runSegmenter,
855                features, featuresSize, width, handler);
856 }
857 
wrap(char const * const utf8,size_t utf8Bytes,const BiDiRunIterator & bidi,const LanguageRunIterator & language,const ScriptRunIterator & script,const FontRunIterator & font,RunIteratorQueue & runSegmenter,const Feature * features,size_t featuresSize,SkScalar width,RunHandler * handler) const858 void ShaperDrivenWrapper::wrap(char const * const utf8, size_t utf8Bytes,
859                                const BiDiRunIterator& bidi,
860                                const LanguageRunIterator& language,
861                                const ScriptRunIterator& script,
862                                const FontRunIterator& font,
863                                RunIteratorQueue& runSegmenter,
864                                const Feature* features, size_t featuresSize,
865                                SkScalar width,
866                                RunHandler* handler) const
867 {
868     ShapedLine line;
869 
870     const char* utf8Start = nullptr;
871     const char* utf8End = utf8;
872     while (runSegmenter.advanceRuns()) {  // For each item
873         utf8Start = utf8End;
874         utf8End = utf8 + runSegmenter.endOfCurrentRun();
875 
876         ShapedRun model(RunHandler::Range(), SkFont(), 0, nullptr, 0);
877         bool modelNeedsRegenerated = true;
878         int modelGlyphOffset = 0;
879 
880         struct TextProps {
881             int glyphLen = 0;
882             SkVector advance = {0, 0};
883         };
884         // map from character position to [safe to break, glyph position, advance]
885         std::unique_ptr<TextProps[]> modelText;
886         int modelTextOffset = 0;
887         SkVector modelAdvanceOffset = {0, 0};
888 
889         while (utf8Start < utf8End) {  // While there are still code points left in this item
890             size_t utf8runLength = utf8End - utf8Start;
891             if (modelNeedsRegenerated) {
892                 model = shape(utf8, utf8Bytes,
893                               utf8Start, utf8End,
894                               bidi, language, script, font,
895                               features, featuresSize);
896                 modelGlyphOffset = 0;
897 
898                 SkVector advance = {0, 0};
899                 modelText = std::make_unique<TextProps[]>(utf8runLength + 1);
900                 size_t modelStartCluster = utf8Start - utf8;
901                 for (size_t i = 0; i < model.fNumGlyphs; ++i) {
902                     SkASSERT(modelStartCluster <= model.fGlyphs[i].fCluster);
903                     SkASSERT(                     model.fGlyphs[i].fCluster < (size_t)(utf8End - utf8));
904                     if (!model.fGlyphs[i].fUnsafeToBreak) {
905                         modelText[model.fGlyphs[i].fCluster - modelStartCluster].glyphLen = i;
906                         modelText[model.fGlyphs[i].fCluster - modelStartCluster].advance = advance;
907                     }
908                     advance += model.fGlyphs[i].fAdvance;
909                 }
910                 // Assume it is always safe to break after the end of an item
911                 modelText[utf8runLength].glyphLen = model.fNumGlyphs;
912                 modelText[utf8runLength].advance = model.fAdvance;
913                 modelTextOffset = 0;
914                 modelAdvanceOffset = {0, 0};
915                 modelNeedsRegenerated = false;
916             }
917 
918             // TODO: break iterator per item, but just reset position if needed?
919             // Maybe break iterator with model?
920             if (!fLineBreakIterator->setText(utf8Start, utf8runLength)) {
921                 return;
922             }
923             SkBreakIterator& breakIterator = *fLineBreakIterator;
924 
925             ShapedRun best(RunHandler::Range(), SkFont(), 0, nullptr, 0,
926                            { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity });
927             bool bestIsInvalid = true;
928             bool bestUsesModelForGlyphs = false;
929             SkScalar widthLeft = width - line.fAdvance.fX;
930 
931             for (int32_t breakIteratorCurrent = breakIterator.next();
932                  !breakIterator.isDone();
933                  breakIteratorCurrent = breakIterator.next())
934             {
935                 // TODO: if past a safe to break, future safe to break will be at least as long
936 
937                 // TODO: adjust breakIteratorCurrent by ignorable whitespace
938                 bool candidateUsesModelForGlyphs = false;
939                 ShapedRun candidate = [&](const TextProps& props){
940                     if (props.glyphLen) {
941                         candidateUsesModelForGlyphs = true;
942                         return ShapedRun(RunHandler::Range(utf8Start - utf8, breakIteratorCurrent),
943                                          font.currentFont(), bidi.currentLevel(),
944                                          std::unique_ptr<ShapedGlyph[]>(),
945                                          props.glyphLen - modelGlyphOffset,
946                                          props.advance - modelAdvanceOffset);
947                     } else {
948                         return shape(utf8, utf8Bytes,
949                                      utf8Start, utf8Start + breakIteratorCurrent,
950                                      bidi, language, script, font,
951                                      features, featuresSize);
952                     }
953                 }(modelText[breakIteratorCurrent + modelTextOffset]);
954                 auto score = [widthLeft](const ShapedRun& run) -> SkScalar {
955                     if (run.fAdvance.fX < widthLeft) {
956                         return run.fUtf8Range.size();
957                     } else {
958                         return widthLeft - run.fAdvance.fX;
959                     }
960                 };
961                 if (bestIsInvalid || score(best) < score(candidate)) {
962                     best = std::move(candidate);
963                     bestIsInvalid = false;
964                     bestUsesModelForGlyphs = candidateUsesModelForGlyphs;
965                 }
966             }
967 
968             // If nothing fit (best score is negative) and the line is not empty
969             if (width < line.fAdvance.fX + best.fAdvance.fX && !line.runs.empty()) {
970                 emit(line, handler);
971                 line.runs.reset();
972                 line.fAdvance = {0, 0};
973             } else {
974                 if (bestUsesModelForGlyphs) {
975                     best.fGlyphs = std::make_unique<ShapedGlyph[]>(best.fNumGlyphs);
976                     memcpy(best.fGlyphs.get(), model.fGlyphs.get() + modelGlyphOffset,
977                            best.fNumGlyphs * sizeof(ShapedGlyph));
978                     modelGlyphOffset += best.fNumGlyphs;
979                     modelTextOffset += best.fUtf8Range.size();
980                     modelAdvanceOffset += best.fAdvance;
981                 } else {
982                     modelNeedsRegenerated = true;
983                 }
984                 utf8Start += best.fUtf8Range.size();
985                 line.fAdvance += best.fAdvance;
986                 line.runs.emplace_back(std::move(best));
987 
988                 // If item broken, emit line (prevent remainder from accidentally fitting)
989                 if (utf8Start != utf8End) {
990                     emit(line, handler);
991                     line.runs.reset();
992                     line.fAdvance = {0, 0};
993                 }
994             }
995         }
996     }
997     emit(line, handler);
998 }
999 
wrap(char const * const utf8,size_t utf8Bytes,const BiDiRunIterator & bidi,const LanguageRunIterator & language,const ScriptRunIterator & script,const FontRunIterator & font,RunIteratorQueue & runSegmenter,const Feature * features,size_t featuresSize,SkScalar width,RunHandler * handler) const1000 void ShapeThenWrap::wrap(char const * const utf8, size_t utf8Bytes,
1001                          const BiDiRunIterator& bidi,
1002                          const LanguageRunIterator& language,
1003                          const ScriptRunIterator& script,
1004                          const FontRunIterator& font,
1005                          RunIteratorQueue& runSegmenter,
1006                          const Feature* features, size_t featuresSize,
1007                          SkScalar width,
1008                          RunHandler* handler) const
1009 {
1010     SkTArray<ShapedRun> runs;
1011 {
1012     if (!fLineBreakIterator->setText(utf8, utf8Bytes)) {
1013         return;
1014     }
1015     if (!fGraphemeBreakIterator->setText(utf8, utf8Bytes)) {
1016         return;
1017     }
1018 
1019     SkBreakIterator& lineBreakIterator = *fLineBreakIterator;
1020     SkBreakIterator& graphemeBreakIterator = *fGraphemeBreakIterator;
1021     const char* utf8Start = nullptr;
1022     const char* utf8End = utf8;
1023     while (runSegmenter.advanceRuns()) {
1024         utf8Start = utf8End;
1025         utf8End = utf8 + runSegmenter.endOfCurrentRun();
1026 
1027         runs.emplace_back(shape(utf8, utf8Bytes,
1028                                 utf8Start, utf8End,
1029                                 bidi, language, script, font,
1030                                 features, featuresSize));
1031         ShapedRun& run = runs.back();
1032 
1033         uint32_t previousCluster = 0xFFFFFFFF;
1034         for (size_t i = 0; i < run.fNumGlyphs; ++i) {
1035             ShapedGlyph& glyph = run.fGlyphs[i];
1036             int32_t glyphCluster = glyph.fCluster;
1037 
1038             int32_t lineBreakIteratorCurrent = lineBreakIterator.current();
1039             while (!lineBreakIterator.isDone() && lineBreakIteratorCurrent < glyphCluster)
1040             {
1041                 lineBreakIteratorCurrent = lineBreakIterator.next();
1042             }
1043             glyph.fMayLineBreakBefore = glyph.fCluster != previousCluster &&
1044                                         lineBreakIteratorCurrent == glyphCluster;
1045 
1046             int32_t graphemeBreakIteratorCurrent = graphemeBreakIterator.current();
1047             while (!graphemeBreakIterator.isDone() && graphemeBreakIteratorCurrent < glyphCluster)
1048             {
1049                 graphemeBreakIteratorCurrent = graphemeBreakIterator.next();
1050             }
1051             glyph.fGraphemeBreakBefore = glyph.fCluster != previousCluster &&
1052                                          graphemeBreakIteratorCurrent == glyphCluster;
1053 
1054             previousCluster = glyph.fCluster;
1055         }
1056     }
1057 }
1058 
1059 // Iterate over the glyphs in logical order to find potential line lengths.
1060 {
1061     /** The position of the beginning of the line. */
1062     ShapedRunGlyphIterator beginning(runs);
1063 
1064     /** The position of the candidate line break. */
1065     ShapedRunGlyphIterator candidateLineBreak(runs);
1066     SkScalar candidateLineBreakWidth = 0;
1067 
1068     /** The position of the candidate grapheme break. */
1069     ShapedRunGlyphIterator candidateGraphemeBreak(runs);
1070     SkScalar candidateGraphemeBreakWidth = 0;
1071 
1072     /** The position of the current location. */
1073     ShapedRunGlyphIterator current(runs);
1074     SkScalar currentWidth = 0;
1075     while (ShapedGlyph* glyph = current.current()) {
1076         // 'Break' at graphemes until a line boundary, then only at line boundaries.
1077         // Only break at graphemes if no line boundary is valid.
1078         if (current != beginning) {
1079             if (glyph->fGraphemeBreakBefore || glyph->fMayLineBreakBefore) {
1080                 // TODO: preserve line breaks <= grapheme breaks
1081                 // and prevent line breaks inside graphemes
1082                 candidateGraphemeBreak = current;
1083                 candidateGraphemeBreakWidth = currentWidth;
1084                 if (glyph->fMayLineBreakBefore) {
1085                     candidateLineBreak = current;
1086                     candidateLineBreakWidth = currentWidth;
1087                 }
1088             }
1089         }
1090 
1091         SkScalar glyphWidth = glyph->fAdvance.fX;
1092         // Break when overwidth, the glyph has a visual representation, and some space is used.
1093         if (width < currentWidth + glyphWidth && glyph->fHasVisual && candidateGraphemeBreakWidth > 0){
1094             if (candidateLineBreak != beginning) {
1095                 beginning = candidateLineBreak;
1096                 currentWidth -= candidateLineBreakWidth;
1097                 candidateGraphemeBreakWidth -= candidateLineBreakWidth;
1098                 candidateLineBreakWidth = 0;
1099             } else if (candidateGraphemeBreak != beginning) {
1100                 beginning = candidateGraphemeBreak;
1101                 candidateLineBreak = beginning;
1102                 currentWidth -= candidateGraphemeBreakWidth;
1103                 candidateGraphemeBreakWidth = 0;
1104                 candidateLineBreakWidth = 0;
1105             } else {
1106                 SK_ABORT("");
1107             }
1108 
1109             if (width < currentWidth) {
1110                 if (width < candidateGraphemeBreakWidth) {
1111                     candidateGraphemeBreak = candidateLineBreak;
1112                     candidateGraphemeBreakWidth = candidateLineBreakWidth;
1113                 }
1114                 current = candidateGraphemeBreak;
1115                 currentWidth = candidateGraphemeBreakWidth;
1116             }
1117 
1118             glyph = beginning.current();
1119             if (glyph) {
1120                 glyph->fMustLineBreakBefore = true;
1121             }
1122 
1123         } else {
1124             current.next();
1125             currentWidth += glyphWidth;
1126         }
1127     }
1128 }
1129 
1130 // Reorder the runs and glyphs per line and write them out.
1131 {
1132     ShapedRunGlyphIterator previousBreak(runs);
1133     ShapedRunGlyphIterator glyphIterator(runs);
1134     int previousRunIndex = -1;
1135     while (glyphIterator.current()) {
1136         const ShapedRunGlyphIterator current = glyphIterator;
1137         ShapedGlyph* nextGlyph = glyphIterator.next();
1138 
1139         if (previousRunIndex != current.fRunIndex) {
1140             SkFontMetrics metrics;
1141             runs[current.fRunIndex].fFont.getMetrics(&metrics);
1142             previousRunIndex = current.fRunIndex;
1143         }
1144 
1145         // Nothing can be written until the baseline is known.
1146         if (!(nextGlyph == nullptr || nextGlyph->fMustLineBreakBefore)) {
1147             continue;
1148         }
1149 
1150         int numRuns = current.fRunIndex - previousBreak.fRunIndex + 1;
1151         SkAutoSTMalloc<4, SkBidiIterator::Level> runLevels(numRuns);
1152         for (int i = 0; i < numRuns; ++i) {
1153             runLevels[i] = runs[previousBreak.fRunIndex + i].fLevel;
1154         }
1155         SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns);
1156         SkBidiIterator::ReorderVisual(runLevels, numRuns, logicalFromVisual);
1157 
1158         // step through the runs in reverse visual order and the glyphs in reverse logical order
1159         // until a visible glyph is found and force them to the end of the visual line.
1160 
1161         handler->beginLine();
1162 
1163         struct SubRun { const ShapedRun& run; size_t startGlyphIndex; size_t endGlyphIndex; };
1164         auto makeSubRun = [&runs, &previousBreak, &current, &logicalFromVisual](size_t visualIndex){
1165             int logicalIndex = previousBreak.fRunIndex + logicalFromVisual[visualIndex];
1166             const auto& run = runs[logicalIndex];
1167             size_t startGlyphIndex = (logicalIndex == previousBreak.fRunIndex)
1168                                    ? previousBreak.fGlyphIndex
1169                                    : 0;
1170             size_t endGlyphIndex = (logicalIndex == current.fRunIndex)
1171                                  ? current.fGlyphIndex + 1
1172                                  : run.fNumGlyphs;
1173             return SubRun{ run, startGlyphIndex, endGlyphIndex };
1174         };
1175         auto makeRunInfo = [](const SubRun& sub) {
1176             uint32_t startUtf8 = sub.run.fGlyphs[sub.startGlyphIndex].fCluster;
1177             uint32_t endUtf8 = (sub.endGlyphIndex < sub.run.fNumGlyphs)
1178                              ? sub.run.fGlyphs[sub.endGlyphIndex].fCluster
1179                              : sub.run.fUtf8Range.end();
1180 
1181             SkVector advance = SkVector::Make(0, 0);
1182             for (size_t i = sub.startGlyphIndex; i < sub.endGlyphIndex; ++i) {
1183                 advance += sub.run.fGlyphs[i].fAdvance;
1184             }
1185 
1186             return RunHandler::RunInfo{
1187                 sub.run.fFont,
1188                 sub.run.fLevel,
1189                 advance,
1190                 sub.endGlyphIndex - sub.startGlyphIndex,
1191                 RunHandler::Range(startUtf8, endUtf8 - startUtf8)
1192             };
1193         };
1194 
1195         for (int i = 0; i < numRuns; ++i) {
1196             handler->runInfo(makeRunInfo(makeSubRun(i)));
1197         }
1198         handler->commitRunInfo();
1199         for (int i = 0; i < numRuns; ++i) {
1200             SubRun sub = makeSubRun(i);
1201             append(handler, makeRunInfo(sub), sub.run, sub.startGlyphIndex, sub.endGlyphIndex);
1202         }
1203 
1204         handler->commitLine();
1205 
1206         previousRunIndex = -1;
1207         previousBreak = glyphIterator;
1208     }
1209 }
1210 }
1211 
wrap(char const * const utf8,size_t utf8Bytes,const BiDiRunIterator & bidi,const LanguageRunIterator & language,const ScriptRunIterator & script,const FontRunIterator & font,RunIteratorQueue & runSegmenter,const Feature * features,size_t featuresSize,SkScalar width,RunHandler * handler) const1212 void ShapeDontWrapOrReorder::wrap(char const * const utf8, size_t utf8Bytes,
1213                                   const BiDiRunIterator& bidi,
1214                                   const LanguageRunIterator& language,
1215                                   const ScriptRunIterator& script,
1216                                   const FontRunIterator& font,
1217                                   RunIteratorQueue& runSegmenter,
1218                                   const Feature* features, size_t featuresSize,
1219                                   SkScalar width,
1220                                   RunHandler* handler) const
1221 {
1222     sk_ignore_unused_variable(width);
1223     SkTArray<ShapedRun> runs;
1224 
1225     const char* utf8Start = nullptr;
1226     const char* utf8End = utf8;
1227     while (runSegmenter.advanceRuns()) {
1228         utf8Start = utf8End;
1229         utf8End = utf8 + runSegmenter.endOfCurrentRun();
1230 
1231         runs.emplace_back(shape(utf8, utf8Bytes,
1232                                 utf8Start, utf8End,
1233                                 bidi, language, script, font,
1234                                 features, featuresSize));
1235     }
1236 
1237     handler->beginLine();
1238     for (const auto& run : runs) {
1239         const RunHandler::RunInfo info = {
1240             run.fFont,
1241             run.fLevel,
1242             run.fAdvance,
1243             run.fNumGlyphs,
1244             run.fUtf8Range
1245         };
1246         handler->runInfo(info);
1247     }
1248     handler->commitRunInfo();
1249     for (const auto& run : runs) {
1250         const RunHandler::RunInfo info = {
1251             run.fFont,
1252             run.fLevel,
1253             run.fAdvance,
1254             run.fNumGlyphs,
1255             run.fUtf8Range
1256         };
1257         append(handler, info, run, 0, run.fNumGlyphs);
1258     }
1259     handler->commitLine();
1260 }
1261 
1262 class HBLockedFaceCache {
1263 public:
HBLockedFaceCache(SkLRUCache<SkFontID,HBFace> & lruCache,SkMutex & mutex)1264     HBLockedFaceCache(SkLRUCache<SkFontID, HBFace>& lruCache, SkMutex& mutex)
1265         : fLRUCache(lruCache), fMutex(mutex)
1266     {
1267         fMutex.acquire();
1268     }
1269     HBLockedFaceCache(const HBLockedFaceCache&) = delete;
1270     HBLockedFaceCache& operator=(const HBLockedFaceCache&) = delete;
1271     // Required until C++17 copy elision
1272     HBLockedFaceCache(HBLockedFaceCache&&) = default;
1273     HBLockedFaceCache& operator=(HBLockedFaceCache&&) = delete;
1274 
~HBLockedFaceCache()1275     ~HBLockedFaceCache() {
1276         fMutex.release();
1277     }
1278 
find(SkFontID fontId)1279     HBFace* find(SkFontID fontId) {
1280         return fLRUCache.find(fontId);
1281     }
insert(SkFontID fontId,HBFace hbFace)1282     HBFace* insert(SkFontID fontId, HBFace hbFace) {
1283         return fLRUCache.insert(fontId, std::move(hbFace));
1284     }
reset()1285     void reset() {
1286         fLRUCache.reset();
1287     }
1288 private:
1289     SkLRUCache<SkFontID, HBFace>& fLRUCache;
1290     SkMutex& fMutex;
1291 };
get_hbFace_cache()1292 static HBLockedFaceCache get_hbFace_cache() {
1293     static SkMutex gHBFaceCacheMutex;
1294     static SkLRUCache<SkFontID, HBFace> gHBFaceCache(100);
1295     return HBLockedFaceCache(gHBFaceCache, gHBFaceCacheMutex);
1296 }
1297 
shape(char const * const utf8,size_t const utf8Bytes,char const * const utf8Start,char const * const utf8End,const BiDiRunIterator & bidi,const LanguageRunIterator & language,const ScriptRunIterator & script,const FontRunIterator & font,Feature const * const features,size_t const featuresSize) const1298 ShapedRun ShaperHarfBuzz::shape(char const * const utf8,
1299                                   size_t const utf8Bytes,
1300                                   char const * const utf8Start,
1301                                   char const * const utf8End,
1302                                   const BiDiRunIterator& bidi,
1303                                   const LanguageRunIterator& language,
1304                                   const ScriptRunIterator& script,
1305                                   const FontRunIterator& font,
1306                                   Feature const * const features, size_t const featuresSize) const
1307 {
1308     size_t utf8runLength = utf8End - utf8Start;
1309     ShapedRun run(RunHandler::Range(utf8Start - utf8, utf8runLength),
1310                   font.currentFont(), bidi.currentLevel(), nullptr, 0);
1311 
1312     hb_buffer_t* buffer = fBuffer.get();
1313     SkAutoTCallVProc<hb_buffer_t, hb_buffer_clear_contents> autoClearBuffer(buffer);
1314     hb_buffer_set_content_type(buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
1315     hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
1316 
1317     // Documentation for HB_BUFFER_FLAG_BOT/EOT at 763e5466c0a03a7c27020e1e2598e488612529a7.
1318     // Currently BOT forces a dotted circle when first codepoint is a mark; EOT has no effect.
1319     // Avoid adding dotted circle, re-evaluate if BOT/EOT change. See https://skbug.com/9618.
1320     // hb_buffer_set_flags(buffer, HB_BUFFER_FLAG_BOT | HB_BUFFER_FLAG_EOT);
1321 
1322     // Add precontext.
1323     hb_buffer_add_utf8(buffer, utf8, utf8Start - utf8, utf8Start - utf8, 0);
1324 
1325     // Populate the hb_buffer directly with utf8 cluster indexes.
1326     const char* utf8Current = utf8Start;
1327     while (utf8Current < utf8End) {
1328         unsigned int cluster = utf8Current - utf8;
1329         hb_codepoint_t u = utf8_next(&utf8Current, utf8End);
1330         hb_buffer_add(buffer, u, cluster);
1331     }
1332 
1333     // Add postcontext.
1334     hb_buffer_add_utf8(buffer, utf8Current, utf8 + utf8Bytes - utf8Current, 0, 0);
1335 
1336     hb_direction_t direction = is_LTR(bidi.currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL;
1337     hb_buffer_set_direction(buffer, direction);
1338     hb_buffer_set_script(buffer, hb_script_from_iso15924_tag((hb_tag_t)script.currentScript()));
1339     // Buffers with HB_LANGUAGE_INVALID race since hb_language_get_default is not thread safe.
1340     // The user must provide a language, but may provide data hb_language_from_string cannot use.
1341     // Use "und" for the undefined language in this case (RFC5646 4.1 5).
1342     hb_language_t hbLanguage = hb_language_from_string(language.currentLanguage(), -1);
1343     if (hbLanguage == HB_LANGUAGE_INVALID) {
1344         hbLanguage = fUndefinedLanguage;
1345     }
1346     hb_buffer_set_language(buffer, hbLanguage);
1347     hb_buffer_guess_segment_properties(buffer);
1348 
1349     // TODO: better cache HBFace (data) / hbfont (typeface)
1350     // An HBFace is expensive (it sanitizes the bits).
1351     // An HBFont is fairly inexpensive.
1352     // An HBFace is actually tied to the data, not the typeface.
1353     // The size of 100 here is completely arbitrary and used to match libtxt.
1354     HBFont hbFont;
1355     {
1356         HBLockedFaceCache cache = get_hbFace_cache();
1357         SkFontID dataId = font.currentFont().getTypeface()->uniqueID();
1358         HBFace* hbFaceCached = cache.find(dataId);
1359         if (!hbFaceCached) {
1360             HBFace hbFace(create_hb_face(*font.currentFont().getTypeface()));
1361             hbFaceCached = cache.insert(dataId, std::move(hbFace));
1362         }
1363         hbFont = create_hb_font(font.currentFont(), *hbFaceCached);
1364     }
1365     if (!hbFont) {
1366         return run;
1367     }
1368 
1369     SkSTArray<32, hb_feature_t> hbFeatures;
1370     for (const auto& feature : SkMakeSpan(features, featuresSize)) {
1371         if (feature.end < SkTo<size_t>(utf8Start - utf8) ||
1372                           SkTo<size_t>(utf8End   - utf8)  <= feature.start)
1373         {
1374             continue;
1375         }
1376         if (feature.start <= SkTo<size_t>(utf8Start - utf8) &&
1377                              SkTo<size_t>(utf8End   - utf8) <= feature.end)
1378         {
1379             hbFeatures.push_back({ (hb_tag_t)feature.tag, feature.value,
1380                                    HB_FEATURE_GLOBAL_START, HB_FEATURE_GLOBAL_END});
1381         } else {
1382             hbFeatures.push_back({ (hb_tag_t)feature.tag, feature.value,
1383                                    SkTo<unsigned>(feature.start), SkTo<unsigned>(feature.end)});
1384         }
1385     }
1386 
1387     hb_shape(hbFont.get(), buffer, hbFeatures.data(), hbFeatures.size());
1388     unsigned len = hb_buffer_get_length(buffer);
1389     if (len == 0) {
1390         return run;
1391     }
1392 
1393     if (direction == HB_DIRECTION_RTL) {
1394         // Put the clusters back in logical order.
1395         // Note that the advances remain ltr.
1396         hb_buffer_reverse(buffer);
1397     }
1398     hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer, nullptr);
1399     hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buffer, nullptr);
1400 
1401     run = ShapedRun(RunHandler::Range(utf8Start - utf8, utf8runLength),
1402                     font.currentFont(), bidi.currentLevel(),
1403                     std::unique_ptr<ShapedGlyph[]>(new ShapedGlyph[len]), len);
1404 
1405     // Undo skhb_position with (1.0/(1<<16)) and scale as needed.
1406     double SkScalarFromHBPosX = +(1.52587890625e-5) * run.fFont.getScaleX();
1407     double SkScalarFromHBPosY = -(1.52587890625e-5);  // HarfBuzz y-up, Skia y-down
1408     SkVector runAdvance = { 0, 0 };
1409     for (unsigned i = 0; i < len; i++) {
1410         ShapedGlyph& glyph = run.fGlyphs[i];
1411         glyph.fID = info[i].codepoint;
1412         glyph.fCluster = info[i].cluster;
1413         glyph.fOffset.fX = pos[i].x_offset * SkScalarFromHBPosX;
1414         glyph.fOffset.fY = pos[i].y_offset * SkScalarFromHBPosY;
1415         glyph.fAdvance.fX = pos[i].x_advance * SkScalarFromHBPosX;
1416         glyph.fAdvance.fY = pos[i].y_advance * SkScalarFromHBPosY;
1417 
1418         SkRect bounds;
1419         SkScalar advance;
1420         SkPaint p;
1421         run.fFont.getWidthsBounds(&glyph.fID, 1, &advance, &bounds, &p);
1422         glyph.fHasVisual = !bounds.isEmpty(); //!font->currentTypeface()->glyphBoundsAreZero(glyph.fID);
1423 #if SK_HB_VERSION_CHECK(1, 5, 0)
1424         glyph.fUnsafeToBreak = info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1425 #else
1426         glyph.fUnsafeToBreak = false;
1427 #endif
1428         glyph.fMustLineBreakBefore = false;
1429 
1430         runAdvance += glyph.fAdvance;
1431     }
1432     run.fAdvance = runAdvance;
1433 
1434     return run;
1435 }
1436 
1437 }  // namespace
1438 
1439 std::unique_ptr<SkShaper::BiDiRunIterator>
MakeIcuBiDiRunIterator(const char * utf8,size_t utf8Bytes,uint8_t bidiLevel)1440 SkShaper::MakeIcuBiDiRunIterator(const char* utf8, size_t utf8Bytes, uint8_t bidiLevel) {
1441     auto unicode = SkUnicode::Make();
1442     if (!unicode) {
1443         return nullptr;
1444     }
1445     return SkShaper::MakeSkUnicodeBidiRunIterator(unicode.get(),
1446                                                   utf8,
1447                                                   utf8Bytes,
1448                                                   bidiLevel);
1449 }
1450 
1451 std::unique_ptr<SkShaper::BiDiRunIterator>
MakeSkUnicodeBidiRunIterator(SkUnicode * unicode,const char * utf8,size_t utf8Bytes,uint8_t bidiLevel)1452 SkShaper::MakeSkUnicodeBidiRunIterator(SkUnicode* unicode, const char* utf8, size_t utf8Bytes, uint8_t bidiLevel) {
1453     // ubidi only accepts utf16 (though internally it basically works on utf32 chars).
1454     // We want an ubidi_setPara(UBiDi*, UText*, UBiDiLevel, UBiDiLevel*, UErrorCode*);
1455     if (!SkTFitsIn<int32_t>(utf8Bytes)) {
1456         SkDEBUGF("Bidi error: text too long");
1457         return nullptr;
1458     }
1459 
1460     int32_t utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Bytes);
1461     if (utf16Units < 0) {
1462         SkDEBUGF("Invalid utf8 input\n");
1463         return nullptr;
1464     }
1465 
1466     std::unique_ptr<uint16_t[]> utf16(new uint16_t[utf16Units]);
1467     (void)SkUTF::UTF8ToUTF16(utf16.get(), utf16Units, utf8, utf8Bytes);
1468 
1469     auto bidiDir = (bidiLevel % 2 == 0) ? SkBidiIterator::kLTR : SkBidiIterator::kRTL;
1470     SkUnicodeBidi bidi = unicode->makeBidiIterator(utf16.get(), utf16Units, bidiDir);
1471     if (!bidi) {
1472         SkDEBUGF("Bidi error\n");
1473         return nullptr;
1474     }
1475 
1476     return std::make_unique<SkUnicodeBidiRunIterator>(utf8, utf8 + utf8Bytes, std::move(bidi));
1477 }
1478 
1479 std::unique_ptr<SkShaper::ScriptRunIterator>
MakeHbIcuScriptRunIterator(const char * utf8,size_t utf8Bytes)1480 SkShaper::MakeHbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes) {
1481     auto unicode = SkUnicode::Make();
1482     if (!unicode) {
1483         return nullptr;
1484     }
1485     return SkShaper::MakeSkUnicodeHbScriptRunIterator(unicode.get(), utf8, utf8Bytes);
1486 }
1487 
1488 std::unique_ptr<SkShaper::ScriptRunIterator>
MakeSkUnicodeHbScriptRunIterator(SkUnicode * unicode,const char * utf8,size_t utf8Bytes)1489 SkShaper::MakeSkUnicodeHbScriptRunIterator(SkUnicode* unicode, const char* utf8, size_t utf8Bytes) {
1490     auto script = unicode->makeScriptIterator();
1491     if (!script) {
1492         return nullptr;
1493     }
1494     return std::make_unique<SkUnicodeHbScriptRunIterator>(std::move(script), utf8, utf8Bytes);
1495 }
1496 
MakeShaperDrivenWrapper(sk_sp<SkFontMgr> fontmgr)1497 std::unique_ptr<SkShaper> SkShaper::MakeShaperDrivenWrapper(sk_sp<SkFontMgr> fontmgr) {
1498     return MakeHarfBuzz(std::move(fontmgr), true);
1499 }
MakeShapeThenWrap(sk_sp<SkFontMgr> fontmgr)1500 std::unique_ptr<SkShaper> SkShaper::MakeShapeThenWrap(sk_sp<SkFontMgr> fontmgr) {
1501     return MakeHarfBuzz(std::move(fontmgr), false);
1502 }
MakeShapeDontWrapOrReorder(sk_sp<SkFontMgr> fontmgr)1503 std::unique_ptr<SkShaper> SkShaper::MakeShapeDontWrapOrReorder(sk_sp<SkFontMgr> fontmgr) {
1504     HBBuffer buffer(hb_buffer_create());
1505     if (!buffer) {
1506         SkDEBUGF("Could not create hb_buffer");
1507         return nullptr;
1508     }
1509 
1510     auto unicode = SkUnicode::Make();
1511     if (!unicode) {
1512         return nullptr;
1513     }
1514 
1515     return std::make_unique<ShapeDontWrapOrReorder>
1516         (std::move(unicode), nullptr, nullptr, std::move(buffer), std::move(fontmgr));
1517 }
1518 
PurgeHarfBuzzCache()1519 void SkShaper::PurgeHarfBuzzCache() {
1520     HBLockedFaceCache cache = get_hbFace_cache();
1521     cache.reset();
1522 }
1523