blob: 7b77fd30aec1910bef231bd504bc59c6e103103e [file] [log] [blame]
Ben Wagnera25fbef2017-08-30 13:56:19 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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/SkStream.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
21#include "include/private/SkBitmaskEnum.h"
22#include "include/private/SkMalloc.h"
Ben Wagner4651b992020-05-13 10:44:49 -040023#include "include/private/SkMutex.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/private/SkTArray.h"
25#include "include/private/SkTFitsIn.h"
26#include "include/private/SkTemplates.h"
27#include "include/private/SkTo.h"
28#include "modules/skshaper/include/SkShaper.h"
Ben Wagner4651b992020-05-13 10:44:49 -040029#include "src/core/SkLRUCache.h"
Ben Wagner24ee4e02019-10-11 10:36:10 -040030#include "src/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/core/SkTDPQueue.h"
32#include "src/utils/SkUTF.h"
Ben Wagner17774242018-08-07 14:31:33 -040033
34#include <hb.h>
Ben Wagner1383a382019-04-03 17:53:53 -040035#include <hb-icu.h>
Ben Wagner17774242018-08-07 14:31:33 -040036#include <hb-ot.h>
Ben Wagner17774242018-08-07 14:31:33 -040037#include <unicode/ubidi.h>
Ben Wagner1383a382019-04-03 17:53:53 -040038#include <unicode/ubrk.h>
39#include <unicode/umachine.h>
Ben Wagner17774242018-08-07 14:31:33 -040040#include <unicode/urename.h>
Ben Wagner1383a382019-04-03 17:53:53 -040041#include <unicode/uscript.h>
42#include <unicode/ustring.h>
Ben Wagner17774242018-08-07 14:31:33 -040043#include <unicode/utext.h>
44#include <unicode/utypes.h>
45
Ben Wagner0ec8ec22018-09-04 18:17:13 -040046#include <cstring>
Ben Wagner17774242018-08-07 14:31:33 -040047#include <memory>
Ben Wagner1383a382019-04-03 17:53:53 -040048#include <type_traits>
Ben Wagner17774242018-08-07 14:31:33 -040049#include <utility>
Ben Wagnera25fbef2017-08-30 13:56:19 -040050
Hal Canary61021922019-02-06 12:29:11 -050051#if defined(SK_USING_THIRD_PARTY_ICU)
Hal Canary32498f02019-02-04 15:36:31 -050052#include "SkLoadICU.h"
Hal Canary61021922019-02-06 12:29:11 -050053#endif
Hal Canary32498f02019-02-04 15:36:31 -050054
Ben Wagner24ee4e02019-10-11 10:36:10 -040055// HB_FEATURE_GLOBAL_START and HB_FEATURE_GLOBAL_END were not added until HarfBuzz 2.0
56// They would have always worked, they just hadn't been named yet.
57#if !defined(HB_FEATURE_GLOBAL_START)
58# define HB_FEATURE_GLOBAL_START 0
59#endif
60#if !defined(HB_FEATURE_GLOBAL_END)
61# define HB_FEATURE_GLOBAL_END ((unsigned int) -1)
62#endif
63
Mike Kleindc976a92020-04-30 06:45:25 -050064namespace sknonstd {
Ben Wagner2fc14742019-02-06 16:37:44 -050065template <> struct is_bitmask_enum<hb_buffer_flags_t> : std::true_type {};
66}
67
Ben Wagnera25fbef2017-08-30 13:56:19 -040068namespace {
Ben Wagner24ee4e02019-10-11 10:36:10 -040069template <typename T,typename P,P* p> using resource = std::unique_ptr<T, SkFunctionWrapper<P, p>>;
70using HBBlob = resource<hb_blob_t , decltype(hb_blob_destroy) , hb_blob_destroy >;
71using HBFace = resource<hb_face_t , decltype(hb_face_destroy) , hb_face_destroy >;
72using HBFont = resource<hb_font_t , decltype(hb_font_destroy) , hb_font_destroy >;
73using HBBuffer = resource<hb_buffer_t , decltype(hb_buffer_destroy), hb_buffer_destroy>;
74using ICUBiDi = resource<UBiDi , decltype(ubidi_close) , ubidi_close >;
75using ICUBrk = resource<UBreakIterator, decltype(ubrk_close) , ubrk_close >;
76using ICUUText = resource<UText , decltype(utext_close) , utext_close >;
Ben Wagnera25fbef2017-08-30 13:56:19 -040077
Ben Wagnerf61c9362019-02-13 12:01:45 -050078hb_position_t skhb_position(SkScalar value) {
79 // Treat HarfBuzz hb_position_t as 16.16 fixed-point.
80 constexpr int kHbPosition1 = 1 << 16;
81 return SkScalarRoundToInt(value * kHbPosition1);
82}
83
84hb_bool_t skhb_glyph(hb_font_t* hb_font,
85 void* font_data,
86 hb_codepoint_t unicode,
87 hb_codepoint_t variation_selector,
88 hb_codepoint_t* glyph,
89 void* user_data) {
90 SkFont& font = *reinterpret_cast<SkFont*>(font_data);
91
92 *glyph = font.unicharToGlyph(unicode);
93 return *glyph != 0;
94}
95
96hb_bool_t skhb_nominal_glyph(hb_font_t* hb_font,
97 void* font_data,
98 hb_codepoint_t unicode,
99 hb_codepoint_t* glyph,
100 void* user_data) {
101 return skhb_glyph(hb_font, font_data, unicode, 0, glyph, user_data);
102}
103
104unsigned skhb_nominal_glyphs(hb_font_t *hb_font, void *font_data,
105 unsigned int count,
106 const hb_codepoint_t *unicodes,
107 unsigned int unicode_stride,
108 hb_codepoint_t *glyphs,
109 unsigned int glyph_stride,
110 void *user_data) {
111 SkFont& font = *reinterpret_cast<SkFont*>(font_data);
112
113 // Batch call textToGlyphs since entry cost is not cheap.
114 // Copy requred because textToGlyphs is dense and hb is strided.
115 SkAutoSTMalloc<256, SkUnichar> unicode(count);
116 for (unsigned i = 0; i < count; i++) {
117 unicode[i] = *unicodes;
118 unicodes = SkTAddOffset<const hb_codepoint_t>(unicodes, unicode_stride);
119 }
120 SkAutoSTMalloc<256, SkGlyphID> glyph(count);
Ben Wagner51e15a62019-05-07 15:38:46 -0400121 font.textToGlyphs(unicode.get(), count * sizeof(SkUnichar), SkTextEncoding::kUTF32,
Ben Wagnerf61c9362019-02-13 12:01:45 -0500122 glyph.get(), count);
123
124 // Copy the results back to the sparse array.
Ben Wagnerbedbb072019-07-31 15:21:03 -0400125 unsigned int done;
126 for (done = 0; done < count && glyph[done] != 0; done++) {
127 *glyphs = glyph[done];
Ben Wagnerf61c9362019-02-13 12:01:45 -0500128 glyphs = SkTAddOffset<hb_codepoint_t>(glyphs, glyph_stride);
129 }
Ben Wagnerbedbb072019-07-31 15:21:03 -0400130 // return 'done' to allow HarfBuzz to synthesize with NFC and spaces, return 'count' to avoid
131 return done;
Ben Wagnerf61c9362019-02-13 12:01:45 -0500132}
133
134hb_position_t skhb_glyph_h_advance(hb_font_t* hb_font,
135 void* font_data,
Ben Wagner32b45b32020-01-22 13:47:07 -0500136 hb_codepoint_t hbGlyph,
Ben Wagnerf61c9362019-02-13 12:01:45 -0500137 void* user_data) {
138 SkFont& font = *reinterpret_cast<SkFont*>(font_data);
139
140 SkScalar advance;
Ben Wagner32b45b32020-01-22 13:47:07 -0500141 SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500142
Ben Wagner32b45b32020-01-22 13:47:07 -0500143 font.getWidths(&skGlyph, 1, &advance);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500144 if (!font.isSubpixel()) {
145 advance = SkScalarRoundToInt(advance);
146 }
147 return skhb_position(advance);
148}
149
150void skhb_glyph_h_advances(hb_font_t* hb_font,
151 void* font_data,
152 unsigned count,
153 const hb_codepoint_t* glyphs,
154 unsigned int glyph_stride,
155 hb_position_t* advances,
156 unsigned int advance_stride,
157 void* user_data) {
158 SkFont& font = *reinterpret_cast<SkFont*>(font_data);
159
160 // Batch call getWidths since entry cost is not cheap.
161 // Copy requred because getWidths is dense and hb is strided.
162 SkAutoSTMalloc<256, SkGlyphID> glyph(count);
163 for (unsigned i = 0; i < count; i++) {
164 glyph[i] = *glyphs;
165 glyphs = SkTAddOffset<const hb_codepoint_t>(glyphs, glyph_stride);
166 }
167 SkAutoSTMalloc<256, SkScalar> advance(count);
168 font.getWidths(glyph.get(), count, advance.get());
169
170 if (!font.isSubpixel()) {
171 for (unsigned i = 0; i < count; i++) {
172 advance[i] = SkScalarRoundToInt(advance[i]);
173 }
174 }
175
176 // Copy the results back to the sparse array.
177 for (unsigned i = 0; i < count; i++) {
178 *advances = skhb_position(advance[i]);
179 advances = SkTAddOffset<hb_position_t>(advances, advance_stride);
180 }
181}
182
183// HarfBuzz callback to retrieve glyph extents, mainly used by HarfBuzz for
184// fallback mark positioning, i.e. the situation when the font does not have
185// mark anchors or other mark positioning rules, but instead HarfBuzz is
186// supposed to heuristically place combining marks around base glyphs. HarfBuzz
187// does this by measuring "ink boxes" of glyphs, and placing them according to
188// Unicode mark classes. Above, below, centered or left or right, etc.
189hb_bool_t skhb_glyph_extents(hb_font_t* hb_font,
190 void* font_data,
Ben Wagner32b45b32020-01-22 13:47:07 -0500191 hb_codepoint_t hbGlyph,
Ben Wagnerf61c9362019-02-13 12:01:45 -0500192 hb_glyph_extents_t* extents,
193 void* user_data) {
194 SkFont& font = *reinterpret_cast<SkFont*>(font_data);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500195 SkASSERT(extents);
196
197 SkRect sk_bounds;
Ben Wagner32b45b32020-01-22 13:47:07 -0500198 SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500199
Ben Wagner32b45b32020-01-22 13:47:07 -0500200 font.getWidths(&skGlyph, 1, nullptr, &sk_bounds);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500201 if (!font.isSubpixel()) {
202 sk_bounds.set(sk_bounds.roundOut());
203 }
204
205 // Skia is y-down but HarfBuzz is y-up.
206 extents->x_bearing = skhb_position(sk_bounds.fLeft);
207 extents->y_bearing = skhb_position(-sk_bounds.fTop);
208 extents->width = skhb_position(sk_bounds.width());
209 extents->height = skhb_position(-sk_bounds.height());
210 return true;
211}
212
Kevin Lubick867da4b2019-02-22 15:55:39 -0500213#define SK_HB_VERSION_CHECK(x, y, z) \
214 (HB_VERSION_MAJOR > (x)) || \
215 (HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR > (y)) || \
216 (HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR == (y) && HB_VERSION_MICRO >= (z))
217
Ben Wagnerf61c9362019-02-13 12:01:45 -0500218hb_font_funcs_t* skhb_get_font_funcs() {
219 static hb_font_funcs_t* const funcs = []{
220 // HarfBuzz will use the default (parent) implementation if they aren't set.
221 hb_font_funcs_t* const funcs = hb_font_funcs_create();
222 hb_font_funcs_set_variation_glyph_func(funcs, skhb_glyph, nullptr, nullptr);
223 hb_font_funcs_set_nominal_glyph_func(funcs, skhb_nominal_glyph, nullptr, nullptr);
Kevin Lubick867da4b2019-02-22 15:55:39 -0500224#if SK_HB_VERSION_CHECK(2, 0, 0)
Ben Wagnerf61c9362019-02-13 12:01:45 -0500225 hb_font_funcs_set_nominal_glyphs_func(funcs, skhb_nominal_glyphs, nullptr, nullptr);
Kevin Lubick867da4b2019-02-22 15:55:39 -0500226#else
227 sk_ignore_unused_variable(skhb_nominal_glyphs);
228#endif
Ben Wagnerf61c9362019-02-13 12:01:45 -0500229 hb_font_funcs_set_glyph_h_advance_func(funcs, skhb_glyph_h_advance, nullptr, nullptr);
Kevin Lubick867da4b2019-02-22 15:55:39 -0500230#if SK_HB_VERSION_CHECK(1, 8, 6)
Ben Wagnerf61c9362019-02-13 12:01:45 -0500231 hb_font_funcs_set_glyph_h_advances_func(funcs, skhb_glyph_h_advances, nullptr, nullptr);
Kevin Lubick867da4b2019-02-22 15:55:39 -0500232#else
233 sk_ignore_unused_variable(skhb_glyph_h_advances);
234#endif
Ben Wagnerf61c9362019-02-13 12:01:45 -0500235 hb_font_funcs_set_glyph_extents_func(funcs, skhb_glyph_extents, nullptr, nullptr);
236 hb_font_funcs_make_immutable(funcs);
237 return funcs;
238 }();
239 SkASSERT(funcs);
240 return funcs;
241}
242
243hb_blob_t* skhb_get_table(hb_face_t* face, hb_tag_t tag, void* user_data) {
244 SkTypeface& typeface = *reinterpret_cast<SkTypeface*>(user_data);
245
Mike Reed6d907fa2019-07-24 14:51:34 -0400246 auto data = typeface.copyTableData(tag);
247 if (!data) {
Hal Canary0dfa2082018-10-31 13:02:49 -0400248 return nullptr;
249 }
Mike Reed6d907fa2019-07-24 14:51:34 -0400250 SkData* rawData = data.release();
251 return hb_blob_create(reinterpret_cast<char*>(rawData->writable_data()), rawData->size(),
Ben Wagner4651b992020-05-13 10:44:49 -0400252 HB_MEMORY_MODE_READONLY, rawData, [](void* ctx) {
Ben Wagner89e1f602019-09-27 17:02:53 -0400253 SkSafeUnref(((SkData*)ctx));
Mike Reed6d907fa2019-07-24 14:51:34 -0400254 });
Ben Wagnerf61c9362019-02-13 12:01:45 -0500255}
256
Ben Wagner4651b992020-05-13 10:44:49 -0400257HBBlob stream_to_blob(std::unique_ptr<SkStreamAsset> asset) {
258 size_t size = asset->getLength();
259 HBBlob blob;
260 if (const void* base = asset->getMemoryBase()) {
261 blob.reset(hb_blob_create((char*)base, SkToUInt(size),
262 HB_MEMORY_MODE_READONLY, asset.release(),
263 [](void* p) { delete (SkStreamAsset*)p; }));
Mike Reed7a4ea2ba2020-05-19 01:35:09 +0000264 } else {
Ben Wagner4651b992020-05-13 10:44:49 -0400265 // SkDebugf("Extra SkStreamAsset copy\n");
266 void* ptr = size ? sk_malloc_throw(size) : nullptr;
267 asset->read(ptr, size);
268 blob.reset(hb_blob_create((char*)ptr, SkToUInt(size),
269 HB_MEMORY_MODE_READONLY, ptr, sk_free));
270 }
271 SkASSERT(blob);
272 hb_blob_make_immutable(blob.get());
273 return blob;
274}
275
276SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;)
277
278HBFace create_hb_face(const SkTypeface& typeface) {
279 int index;
280 std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openStream(&index);
281 HBFace face;
282 if (typefaceAsset && typefaceAsset->getMemoryBase()) {
Mike Reed7a4ea2ba2020-05-19 01:35:09 +0000283 HBBlob blob(stream_to_blob(std::move(typefaceAsset)));
284 face.reset(hb_face_create(blob.get(), (unsigned)index));
Ben Wagner4651b992020-05-13 10:44:49 -0400285 } else {
286 face.reset(hb_face_create_for_tables(
287 skhb_get_table,
288 const_cast<SkTypeface*>(SkRef(&typeface)),
289 [](void* user_data){ SkSafeUnref(reinterpret_cast<SkTypeface*>(user_data)); }));
Hal Canaryddef43f2018-11-16 10:53:51 -0500290 }
Ben Wagnera25fbef2017-08-30 13:56:19 -0400291 SkASSERT(face);
292 if (!face) {
Ben Wagnere0001732017-08-31 16:26:26 -0400293 return nullptr;
Ben Wagnera25fbef2017-08-30 13:56:19 -0400294 }
295 hb_face_set_index(face.get(), (unsigned)index);
Ben Wagner4651b992020-05-13 10:44:49 -0400296 hb_face_set_upem(face.get(), typeface.getUnitsPerEm());
297
298 SkDEBUGCODE(
299 hb_face_set_user_data(face.get(), &gDataIdKey, const_cast<SkTypeface*>(&typeface),
300 nullptr, false);
301 )
302
303 return face;
304}
305
306HBFont create_hb_font(const SkFont& font, const HBFace& face) {
307 SkDEBUGCODE(
308 void* dataId = hb_face_get_user_data(face.get(), &gDataIdKey);
309 SkASSERT(dataId == font.getTypeface());
310 )
Ben Wagnera25fbef2017-08-30 13:56:19 -0400311
Ben Wagnerf61c9362019-02-13 12:01:45 -0500312 HBFont otFont(hb_font_create(face.get()));
313 SkASSERT(otFont);
314 if (!otFont) {
Ben Wagnere0001732017-08-31 16:26:26 -0400315 return nullptr;
316 }
Ben Wagnerf61c9362019-02-13 12:01:45 -0500317 hb_ot_font_set_funcs(otFont.get());
318 int axis_count = font.getTypeface()->getVariationDesignPosition(nullptr, 0);
Ben Wagnere0001732017-08-31 16:26:26 -0400319 if (axis_count > 0) {
320 SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate> axis_values(axis_count);
Ben Wagnerf61c9362019-02-13 12:01:45 -0500321 if (font.getTypeface()->getVariationDesignPosition(axis_values, axis_count) == axis_count) {
322 hb_font_set_variations(otFont.get(),
Ben Wagnere0001732017-08-31 16:26:26 -0400323 reinterpret_cast<hb_variation_t*>(axis_values.get()),
324 axis_count);
325 }
326 }
Ben Wagnerf61c9362019-02-13 12:01:45 -0500327
328 // Creating a sub font means that non-available functions
329 // are found from the parent.
330 HBFont skFont(hb_font_create_sub_font(otFont.get()));
331 hb_font_set_funcs(skFont.get(), skhb_get_font_funcs(),
332 reinterpret_cast<void *>(new SkFont(font)),
333 [](void* user_data){ delete reinterpret_cast<SkFont*>(user_data); });
334 int scale = skhb_position(font.getSize());
335 hb_font_set_scale(skFont.get(), scale, scale);
336
337 return skFont;
Ben Wagnere0001732017-08-31 16:26:26 -0400338}
339
Ben Wagner1383a382019-04-03 17:53:53 -0400340/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
Hal Canaryf107a2f2018-07-25 16:52:48 -0400341static inline SkUnichar utf8_next(const char** ptr, const char* end) {
342 SkUnichar val = SkUTF::NextUTF8(ptr, end);
Ben Wagner1383a382019-04-03 17:53:53 -0400343 return val < 0 ? 0xFFFD : val;
Hal Canaryf107a2f2018-07-25 16:52:48 -0400344}
345
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400346class IcuBiDiRunIterator final : public SkShaper::BiDiRunIterator {
Ben Wagner8d45a382017-11-16 10:08:28 -0500347public:
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400348 IcuBiDiRunIterator(const char* utf8, const char* end, ICUBiDi bidi)
Ben Wagner8d45a382017-11-16 10:08:28 -0500349 : fBidi(std::move(bidi))
350 , fEndOfCurrentRun(utf8)
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400351 , fBegin(utf8)
352 , fEnd(end)
Ben Wagner8d45a382017-11-16 10:08:28 -0500353 , fUTF16LogicalPosition(0)
354 , fLevel(UBIDI_DEFAULT_LTR)
355 {}
356 void consume() override {
357 SkASSERT(fUTF16LogicalPosition < ubidi_getLength(fBidi.get()));
358 int32_t endPosition = ubidi_getLength(fBidi.get());
359 fLevel = ubidi_getLevelAt(fBidi.get(), fUTF16LogicalPosition);
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400360 SkUnichar u = utf8_next(&fEndOfCurrentRun, fEnd);
Hal Canaryf107a2f2018-07-25 16:52:48 -0400361 fUTF16LogicalPosition += SkUTF::ToUTF16(u);
Ben Wagner8d45a382017-11-16 10:08:28 -0500362 UBiDiLevel level;
363 while (fUTF16LogicalPosition < endPosition) {
364 level = ubidi_getLevelAt(fBidi.get(), fUTF16LogicalPosition);
365 if (level != fLevel) {
366 break;
367 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400368 u = utf8_next(&fEndOfCurrentRun, fEnd);
Julia Lavrova526df262019-08-21 17:49:44 -0400369
Hal Canaryf107a2f2018-07-25 16:52:48 -0400370 fUTF16LogicalPosition += SkUTF::ToUTF16(u);
Ben Wagner8d45a382017-11-16 10:08:28 -0500371 }
372 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400373 size_t endOfCurrentRun() const override {
374 return fEndOfCurrentRun - fBegin;
Ben Wagner8d45a382017-11-16 10:08:28 -0500375 }
376 bool atEnd() const override {
377 return fUTF16LogicalPosition == ubidi_getLength(fBidi.get());
378 }
379
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400380 UBiDiLevel currentLevel() const override {
Ben Wagner8d45a382017-11-16 10:08:28 -0500381 return fLevel;
382 }
383private:
384 ICUBiDi fBidi;
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400385 char const * fEndOfCurrentRun;
386 char const * const fBegin;
387 char const * const fEnd;
Ben Wagner8d45a382017-11-16 10:08:28 -0500388 int32_t fUTF16LogicalPosition;
389 UBiDiLevel fLevel;
390};
391
Ben Wagner1383a382019-04-03 17:53:53 -0400392class HbIcuScriptRunIterator final : public SkShaper::ScriptRunIterator {
Ben Wagner8d45a382017-11-16 10:08:28 -0500393public:
Ben Wagner1383a382019-04-03 17:53:53 -0400394 HbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes)
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400395 : fCurrent(utf8), fBegin(utf8), fEnd(fCurrent + utf8Bytes)
Ben Wagner8d45a382017-11-16 10:08:28 -0500396 , fCurrentScript(HB_SCRIPT_UNKNOWN)
397 {}
Ben Wagner1383a382019-04-03 17:53:53 -0400398 static hb_script_t hb_script_from_icu(SkUnichar u) {
399 UErrorCode status = U_ZERO_ERROR;
400 UScriptCode scriptCode = uscript_getScript(u, &status);
401
402 if (U_FAILURE (status)) {
403 return HB_SCRIPT_UNKNOWN;
404 }
405
406 return hb_icu_script_to_script(scriptCode);
407 }
Ben Wagner8d45a382017-11-16 10:08:28 -0500408 void consume() override {
409 SkASSERT(fCurrent < fEnd);
Hal Canaryf107a2f2018-07-25 16:52:48 -0400410 SkUnichar u = utf8_next(&fCurrent, fEnd);
Ben Wagner1383a382019-04-03 17:53:53 -0400411 fCurrentScript = hb_script_from_icu(u);
Ben Wagner8d45a382017-11-16 10:08:28 -0500412 while (fCurrent < fEnd) {
413 const char* prev = fCurrent;
Hal Canaryf107a2f2018-07-25 16:52:48 -0400414 u = utf8_next(&fCurrent, fEnd);
Ben Wagner1383a382019-04-03 17:53:53 -0400415 const hb_script_t script = hb_script_from_icu(u);
Ben Wagner8d45a382017-11-16 10:08:28 -0500416 if (script != fCurrentScript) {
417 if (fCurrentScript == HB_SCRIPT_INHERITED || fCurrentScript == HB_SCRIPT_COMMON) {
418 fCurrentScript = script;
419 } else if (script == HB_SCRIPT_INHERITED || script == HB_SCRIPT_COMMON) {
420 continue;
421 } else {
422 fCurrent = prev;
423 break;
424 }
425 }
426 }
427 if (fCurrentScript == HB_SCRIPT_INHERITED) {
428 fCurrentScript = HB_SCRIPT_COMMON;
429 }
430 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400431 size_t endOfCurrentRun() const override {
432 return fCurrent - fBegin;
Ben Wagner8d45a382017-11-16 10:08:28 -0500433 }
434 bool atEnd() const override {
435 return fCurrent == fEnd;
436 }
437
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400438 SkFourByteTag currentScript() const override {
439 return SkSetFourByteTag(HB_UNTAG(fCurrentScript));
Ben Wagner8d45a382017-11-16 10:08:28 -0500440 }
441private:
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400442 char const * fCurrent;
443 char const * const fBegin;
444 char const * const fEnd;
Ben Wagner8d45a382017-11-16 10:08:28 -0500445 hb_script_t fCurrentScript;
446};
447
Ben Wagner8d45a382017-11-16 10:08:28 -0500448class RunIteratorQueue {
449public:
Ben Wagner1ca50522019-10-01 17:54:28 -0400450 void insert(SkShaper::RunIterator* runIterator, int priority) {
451 fEntries.insert({runIterator, priority});
Ben Wagner8d45a382017-11-16 10:08:28 -0500452 }
453
454 bool advanceRuns() {
Ben Wagner1ca50522019-10-01 17:54:28 -0400455 const SkShaper::RunIterator* leastRun = fEntries.peek().runIterator;
Ben Wagner8d45a382017-11-16 10:08:28 -0500456 if (leastRun->atEnd()) {
457 SkASSERT(this->allRunsAreAtEnd());
458 return false;
459 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400460 const size_t leastEnd = leastRun->endOfCurrentRun();
461 SkShaper::RunIterator* currentRun = nullptr;
462 SkDEBUGCODE(size_t previousEndOfCurrentRun);
Ben Wagner1ca50522019-10-01 17:54:28 -0400463 while ((currentRun = fEntries.peek().runIterator)->endOfCurrentRun() <= leastEnd) {
464 int priority = fEntries.peek().priority;
465 fEntries.pop();
Ben Wagner8d45a382017-11-16 10:08:28 -0500466 SkDEBUGCODE(previousEndOfCurrentRun = currentRun->endOfCurrentRun());
467 currentRun->consume();
468 SkASSERT(previousEndOfCurrentRun < currentRun->endOfCurrentRun());
Ben Wagner1ca50522019-10-01 17:54:28 -0400469 fEntries.insert({currentRun, priority});
Ben Wagner8d45a382017-11-16 10:08:28 -0500470 }
471 return true;
472 }
473
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400474 size_t endOfCurrentRun() const {
Ben Wagner1ca50522019-10-01 17:54:28 -0400475 return fEntries.peek().runIterator->endOfCurrentRun();
Ben Wagner8d45a382017-11-16 10:08:28 -0500476 }
477
478private:
479 bool allRunsAreAtEnd() const {
Ben Wagner1ca50522019-10-01 17:54:28 -0400480 for (int i = 0; i < fEntries.count(); ++i) {
481 if (!fEntries.at(i).runIterator->atEnd()) {
Ben Wagner8d45a382017-11-16 10:08:28 -0500482 return false;
483 }
484 }
485 return true;
486 }
487
Ben Wagner1ca50522019-10-01 17:54:28 -0400488 struct Entry {
489 SkShaper::RunIterator* runIterator;
490 int priority;
491 };
492 static bool CompareEntry(Entry const& a, Entry const& b) {
493 size_t aEnd = a.runIterator->endOfCurrentRun();
494 size_t bEnd = b.runIterator->endOfCurrentRun();
495 return aEnd < bEnd || (aEnd == bEnd && a.priority < b.priority);
Ben Wagner8d45a382017-11-16 10:08:28 -0500496 }
Ben Wagner1ca50522019-10-01 17:54:28 -0400497 SkTDPQueue<Entry, CompareEntry> fEntries;
Ben Wagner8d45a382017-11-16 10:08:28 -0500498};
499
500struct ShapedGlyph {
501 SkGlyphID fID;
502 uint32_t fCluster;
503 SkPoint fOffset;
504 SkVector fAdvance;
505 bool fMayLineBreakBefore;
506 bool fMustLineBreakBefore;
507 bool fHasVisual;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400508 bool fGraphemeBreakBefore;
509 bool fUnsafeToBreak;
Ben Wagner8d45a382017-11-16 10:08:28 -0500510};
511struct ShapedRun {
Ben Wagner7415a422019-03-25 15:38:22 -0400512 ShapedRun(SkShaper::RunHandler::Range utf8Range, const SkFont& font, UBiDiLevel level,
513 std::unique_ptr<ShapedGlyph[]> glyphs, size_t numGlyphs, SkVector advance = {0, 0})
514 : fUtf8Range(utf8Range), fFont(font), fLevel(level)
515 , fGlyphs(std::move(glyphs)), fNumGlyphs(numGlyphs), fAdvance(advance)
Ben Wagner8d45a382017-11-16 10:08:28 -0500516 {}
517
Ben Wagner7415a422019-03-25 15:38:22 -0400518 SkShaper::RunHandler::Range fUtf8Range;
Mike Reed6d595682018-12-05 17:28:14 -0500519 SkFont fFont;
Ben Wagner8d45a382017-11-16 10:08:28 -0500520 UBiDiLevel fLevel;
521 std::unique_ptr<ShapedGlyph[]> fGlyphs;
Ben Wagner7415a422019-03-25 15:38:22 -0400522 size_t fNumGlyphs;
523 SkVector fAdvance;
Ben Wagner8d45a382017-11-16 10:08:28 -0500524};
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400525struct ShapedLine {
526 SkTArray<ShapedRun> runs;
527 SkVector fAdvance = { 0, 0 };
528};
Ben Wagner8d45a382017-11-16 10:08:28 -0500529
Ben Wagner1383a382019-04-03 17:53:53 -0400530constexpr bool is_LTR(UBiDiLevel level) {
Ben Wagner8d45a382017-11-16 10:08:28 -0500531 return (level & 1) == 0;
532}
533
Ben Wagner1383a382019-04-03 17:53:53 -0400534void append(SkShaper::RunHandler* handler, const SkShaper::RunHandler::RunInfo& runInfo,
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400535 const ShapedRun& run, size_t startGlyphIndex, size_t endGlyphIndex) {
Ben Wagner7415a422019-03-25 15:38:22 -0400536 SkASSERT(startGlyphIndex <= endGlyphIndex);
537 const size_t glyphLen = endGlyphIndex - startGlyphIndex;
Florin Malita9867f612018-12-12 10:54:49 -0500538
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400539 const auto buffer = handler->runBuffer(runInfo);
Florin Malita9867f612018-12-12 10:54:49 -0500540 SkASSERT(buffer.glyphs);
541 SkASSERT(buffer.positions);
542
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400543 SkVector advance = {0,0};
Ben Wagner7415a422019-03-25 15:38:22 -0400544 for (size_t i = 0; i < glyphLen; i++) {
Ben Wagner8d45a382017-11-16 10:08:28 -0500545 // Glyphs are in logical order, but output ltr since PDF readers seem to expect that.
Ben Wagner7415a422019-03-25 15:38:22 -0400546 const ShapedGlyph& glyph = run.fGlyphs[is_LTR(run.fLevel) ? startGlyphIndex + i
547 : endGlyphIndex - 1 - i];
Florin Malita9867f612018-12-12 10:54:49 -0500548 buffer.glyphs[i] = glyph.fID;
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400549 if (buffer.offsets) {
550 buffer.positions[i] = advance + buffer.point;
Ben Wagnerbdf2c872019-10-24 17:06:37 -0400551 buffer.offsets[i] = glyph.fOffset;
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400552 } else {
Ben Wagnerbdf2c872019-10-24 17:06:37 -0400553 buffer.positions[i] = advance + buffer.point + glyph.fOffset;
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400554 }
Florin Malita9867f612018-12-12 10:54:49 -0500555 if (buffer.clusters) {
556 buffer.clusters[i] = glyph.fCluster;
557 }
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400558 advance += glyph.fAdvance;
Ben Wagner8d45a382017-11-16 10:08:28 -0500559 }
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400560 handler->commitRunBuffer(runInfo);
Ben Wagner8d45a382017-11-16 10:08:28 -0500561}
562
Ben Wagner1383a382019-04-03 17:53:53 -0400563void emit(const ShapedLine& line, SkShaper::RunHandler* handler) {
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400564 // Reorder the runs and glyphs per line and write them out.
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400565 handler->beginLine();
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400566
567 int numRuns = line.runs.size();
568 SkAutoSTMalloc<4, UBiDiLevel> runLevels(numRuns);
569 for (int i = 0; i < numRuns; ++i) {
570 runLevels[i] = line.runs[i].fLevel;
571 }
572 SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns);
573 ubidi_reorderVisual(runLevels, numRuns, logicalFromVisual);
574
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400575 for (int i = 0; i < numRuns; ++i) {
576 int logicalIndex = logicalFromVisual[i];
577
578 const auto& run = line.runs[logicalIndex];
579 const SkShaper::RunHandler::RunInfo info = {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400580 run.fFont,
581 run.fLevel,
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400582 run.fAdvance,
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400583 run.fNumGlyphs,
584 run.fUtf8Range
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400585 };
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400586 handler->runInfo(info);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400587 }
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400588 handler->commitRunInfo();
589 for (int i = 0; i < numRuns; ++i) {
590 int logicalIndex = logicalFromVisual[i];
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400591
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400592 const auto& run = line.runs[logicalIndex];
593 const SkShaper::RunHandler::RunInfo info = {
594 run.fFont,
595 run.fLevel,
596 run.fAdvance,
597 run.fNumGlyphs,
598 run.fUtf8Range
599 };
600 append(handler, info, run, 0, run.fNumGlyphs);
601 }
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400602
Florin Malita500133b2019-02-07 10:56:55 -0500603 handler->commitLine();
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400604}
605
Ben Wagner8d45a382017-11-16 10:08:28 -0500606struct ShapedRunGlyphIterator {
607 ShapedRunGlyphIterator(const SkTArray<ShapedRun>& origRuns)
608 : fRuns(&origRuns), fRunIndex(0), fGlyphIndex(0)
609 { }
610
611 ShapedRunGlyphIterator(const ShapedRunGlyphIterator& that) = default;
612 ShapedRunGlyphIterator& operator=(const ShapedRunGlyphIterator& that) = default;
613 bool operator==(const ShapedRunGlyphIterator& that) const {
614 return fRuns == that.fRuns &&
615 fRunIndex == that.fRunIndex &&
616 fGlyphIndex == that.fGlyphIndex;
617 }
618 bool operator!=(const ShapedRunGlyphIterator& that) const {
619 return fRuns != that.fRuns ||
620 fRunIndex != that.fRunIndex ||
621 fGlyphIndex != that.fGlyphIndex;
622 }
623
624 ShapedGlyph* next() {
625 const SkTArray<ShapedRun>& runs = *fRuns;
626 SkASSERT(fRunIndex < runs.count());
627 SkASSERT(fGlyphIndex < runs[fRunIndex].fNumGlyphs);
628
629 ++fGlyphIndex;
630 if (fGlyphIndex == runs[fRunIndex].fNumGlyphs) {
631 fGlyphIndex = 0;
632 ++fRunIndex;
633 if (fRunIndex >= runs.count()) {
634 return nullptr;
635 }
636 }
637 return &runs[fRunIndex].fGlyphs[fGlyphIndex];
638 }
639
640 ShapedGlyph* current() {
641 const SkTArray<ShapedRun>& runs = *fRuns;
642 if (fRunIndex >= runs.count()) {
643 return nullptr;
644 }
645 return &runs[fRunIndex].fGlyphs[fGlyphIndex];
646 }
647
648 const SkTArray<ShapedRun>* fRuns;
649 int fRunIndex;
Ben Wagner7415a422019-03-25 15:38:22 -0400650 size_t fGlyphIndex;
Ben Wagner8d45a382017-11-16 10:08:28 -0500651};
652
Ben Wagner51874e32019-04-04 15:21:20 -0400653class ShaperHarfBuzz : public SkShaper {
Ben Wagnerb0591942019-02-15 14:46:18 -0500654public:
Florin Malita42684332019-07-26 14:54:40 -0400655 ShaperHarfBuzz(HBBuffer, ICUBrk line, ICUBrk grapheme, sk_sp<SkFontMgr>);
656
Ben Wagner51874e32019-04-04 15:21:20 -0400657protected:
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400658 ICUBrk fLineBreakIterator;
659 ICUBrk fGraphemeBreakIterator;
660
Ben Wagner51874e32019-04-04 15:21:20 -0400661 ShapedRun shape(const char* utf8, size_t utf8Bytes,
662 const char* utf8Start,
663 const char* utf8End,
664 const BiDiRunIterator&,
665 const LanguageRunIterator&,
666 const ScriptRunIterator&,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400667 const FontRunIterator&,
668 const Feature*, size_t featuresSize) const;
Ben Wagner51874e32019-04-04 15:21:20 -0400669private:
Florin Malita42684332019-07-26 14:54:40 -0400670 const sk_sp<SkFontMgr> fFontMgr;
671 HBBuffer fBuffer;
Ben Wagner94aed0b2020-06-08 10:54:10 -0400672 hb_language_t fUndefinedLanguage;
Ben Wagner51874e32019-04-04 15:21:20 -0400673
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400674 void shape(const char* utf8, size_t utf8Bytes,
675 const SkFont&,
676 bool leftToRight,
677 SkScalar width,
678 RunHandler*) const override;
Ben Wagnerb0591942019-02-15 14:46:18 -0500679
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400680 void shape(const char* utf8Text, size_t textBytes,
681 FontRunIterator&,
682 BiDiRunIterator&,
683 ScriptRunIterator&,
684 LanguageRunIterator&,
685 SkScalar width,
686 RunHandler*) const override;
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400687
Ben Wagner24ee4e02019-10-11 10:36:10 -0400688 void shape(const char* utf8Text, size_t textBytes,
689 FontRunIterator&,
690 BiDiRunIterator&,
691 ScriptRunIterator&,
692 LanguageRunIterator&,
693 const Feature*, size_t featuresSize,
694 SkScalar width,
695 RunHandler*) const override;
696
Ben Wagner51874e32019-04-04 15:21:20 -0400697 virtual void wrap(char const * const utf8, size_t utf8Bytes,
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400698 const BiDiRunIterator&,
699 const LanguageRunIterator&,
700 const ScriptRunIterator&,
701 const FontRunIterator&,
702 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400703 const Feature*, size_t featuresSize,
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400704 SkScalar width,
Ben Wagner51874e32019-04-04 15:21:20 -0400705 RunHandler*) const = 0;
Ben Wagner8d45a382017-11-16 10:08:28 -0500706};
707
Ben Wagner51874e32019-04-04 15:21:20 -0400708class ShaperDrivenWrapper : public ShaperHarfBuzz {
709public:
710 using ShaperHarfBuzz::ShaperHarfBuzz;
711private:
712 void wrap(char const * const utf8, size_t utf8Bytes,
713 const BiDiRunIterator&,
714 const LanguageRunIterator&,
715 const ScriptRunIterator&,
716 const FontRunIterator&,
717 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400718 const Feature*, size_t featuresSize,
Ben Wagner51874e32019-04-04 15:21:20 -0400719 SkScalar width,
720 RunHandler*) const override;
721};
Ben Wagnerb0591942019-02-15 14:46:18 -0500722
Ben Wagner51874e32019-04-04 15:21:20 -0400723class ShapeThenWrap : public ShaperHarfBuzz {
724public:
725 using ShaperHarfBuzz::ShaperHarfBuzz;
726private:
727 void wrap(char const * const utf8, size_t utf8Bytes,
728 const BiDiRunIterator&,
729 const LanguageRunIterator&,
730 const ScriptRunIterator&,
731 const FontRunIterator&,
732 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400733 const Feature*, size_t featuresSize,
Ben Wagner51874e32019-04-04 15:21:20 -0400734 SkScalar width,
735 RunHandler*) const override;
736};
737
Ben Wagner6bb79bb2019-05-15 10:50:20 -0400738class ShapeDontWrapOrReorder : public ShaperHarfBuzz {
739public:
740 using ShaperHarfBuzz::ShaperHarfBuzz;
741private:
742 void wrap(char const * const utf8, size_t utf8Bytes,
743 const BiDiRunIterator&,
744 const LanguageRunIterator&,
745 const ScriptRunIterator&,
746 const FontRunIterator&,
747 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400748 const Feature*, size_t featuresSize,
Ben Wagner6bb79bb2019-05-15 10:50:20 -0400749 SkScalar width,
750 RunHandler*) const override;
751};
752
Florin Malita42684332019-07-26 14:54:40 -0400753static std::unique_ptr<SkShaper> MakeHarfBuzz(sk_sp<SkFontMgr> fontmgr, bool correct) {
Ben Wagner51874e32019-04-04 15:21:20 -0400754 #if defined(SK_USING_THIRD_PARTY_ICU)
Hal Canary61021922019-02-06 12:29:11 -0500755 if (!SkLoadICU()) {
Ben Wagner51874e32019-04-04 15:21:20 -0400756 SkDEBUGF("SkLoadICU() failed!\n");
757 return nullptr;
Hal Canary61021922019-02-06 12:29:11 -0500758 }
Ben Wagner51874e32019-04-04 15:21:20 -0400759 #endif
760 HBBuffer buffer(hb_buffer_create());
761 if (!buffer) {
762 SkDEBUGF("Could not create hb_buffer");
763 return nullptr;
764 }
Ben Wagner8d45a382017-11-16 10:08:28 -0500765
Ben Wagner8d45a382017-11-16 10:08:28 -0500766 UErrorCode status = U_ZERO_ERROR;
Ben Wagner51874e32019-04-04 15:21:20 -0400767 ICUBrk lineBreakIterator(ubrk_open(UBRK_LINE, "th", nullptr, 0, &status));
768 if (!lineBreakIterator || U_FAILURE(status)) {
769 SkDEBUGF("Could not create line break iterator: %s", u_errorName(status));
770 return nullptr;
Ben Wagner8d45a382017-11-16 10:08:28 -0500771 }
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400772
Ben Wagner51874e32019-04-04 15:21:20 -0400773 ICUBrk graphemeBreakIterator(ubrk_open(UBRK_CHARACTER, "th", nullptr, 0, &status));
774 if (!graphemeBreakIterator || U_FAILURE(status)) {
775 SkDEBUGF("Could not create grapheme break iterator: %s", u_errorName(status));
776 return nullptr;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400777 }
778
Ben Wagner51874e32019-04-04 15:21:20 -0400779 if (correct) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500780 return std::make_unique<ShaperDrivenWrapper>(std::move(buffer),
Florin Malita42684332019-07-26 14:54:40 -0400781 std::move(lineBreakIterator),
782 std::move(graphemeBreakIterator),
783 std::move(fontmgr));
Ben Wagner51874e32019-04-04 15:21:20 -0400784 } else {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500785 return std::make_unique<ShapeThenWrap>(std::move(buffer),
Florin Malita42684332019-07-26 14:54:40 -0400786 std::move(lineBreakIterator),
787 std::move(graphemeBreakIterator),
788 std::move(fontmgr));
Ben Wagner51874e32019-04-04 15:21:20 -0400789 }
Ben Wagnera25fbef2017-08-30 13:56:19 -0400790}
791
Florin Malita42684332019-07-26 14:54:40 -0400792ShaperHarfBuzz::ShaperHarfBuzz(HBBuffer buffer, ICUBrk line, ICUBrk grapheme,
793 sk_sp<SkFontMgr> fontmgr)
Ben Wagner51874e32019-04-04 15:21:20 -0400794 : fLineBreakIterator(std::move(line))
795 , fGraphemeBreakIterator(std::move(grapheme))
Florin Malita42684332019-07-26 14:54:40 -0400796 , fFontMgr(std::move(fontmgr))
Ben Wagner51874e32019-04-04 15:21:20 -0400797 , fBuffer(std::move(buffer))
Ben Wagner94aed0b2020-06-08 10:54:10 -0400798 , fUndefinedLanguage(hb_language_from_string("und", -1))
Ben Wagner51874e32019-04-04 15:21:20 -0400799{}
Ben Wagnera25fbef2017-08-30 13:56:19 -0400800
Ben Wagner51874e32019-04-04 15:21:20 -0400801void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
802 const SkFont& srcFont,
803 bool leftToRight,
804 SkScalar width,
805 RunHandler* handler) const
Ben Wagner8d45a382017-11-16 10:08:28 -0500806{
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400807 UBiDiLevel defaultLevel = leftToRight ? UBIDI_DEFAULT_LTR : UBIDI_DEFAULT_RTL;
808
Ben Wagner1383a382019-04-03 17:53:53 -0400809 std::unique_ptr<BiDiRunIterator> bidi(MakeIcuBiDiRunIterator(utf8, utf8Bytes, defaultLevel));
Ben Wagner8d45a382017-11-16 10:08:28 -0500810 if (!bidi) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400811 return;
Ben Wagnera25fbef2017-08-30 13:56:19 -0400812 }
Ben Wagnera25fbef2017-08-30 13:56:19 -0400813
Ben Wagner1383a382019-04-03 17:53:53 -0400814 std::unique_ptr<LanguageRunIterator> language(MakeStdLanguageRunIterator(utf8, utf8Bytes));
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400815 if (!language) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400816 return;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400817 }
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400818
Ben Wagner1383a382019-04-03 17:53:53 -0400819 std::unique_ptr<ScriptRunIterator> script(MakeHbIcuScriptRunIterator(utf8, utf8Bytes));
Ben Wagner8d45a382017-11-16 10:08:28 -0500820 if (!script) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400821 return;
Ben Wagnera25fbef2017-08-30 13:56:19 -0400822 }
Ben Wagnera25fbef2017-08-30 13:56:19 -0400823
Florin Malita42684332019-07-26 14:54:40 -0400824 std::unique_ptr<FontRunIterator> font(
825 MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont,
826 fFontMgr ? fFontMgr : SkFontMgr::RefDefault()));
Ben Wagner8d45a382017-11-16 10:08:28 -0500827 if (!font) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400828 return;
Ben Wagnera25fbef2017-08-30 13:56:19 -0400829 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400830
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400831 this->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, width, handler);
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400832}
833
Ben Wagner51874e32019-04-04 15:21:20 -0400834void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
835 FontRunIterator& font,
836 BiDiRunIterator& bidi,
837 ScriptRunIterator& script,
838 LanguageRunIterator& language,
839 SkScalar width,
840 RunHandler* handler) const
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400841{
Ben Wagner24ee4e02019-10-11 10:36:10 -0400842 this->shape(utf8, utf8Bytes, font, bidi, script, language, nullptr, 0, width, handler);
843}
844
845void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes,
846 FontRunIterator& font,
847 BiDiRunIterator& bidi,
848 ScriptRunIterator& script,
849 LanguageRunIterator& language,
850 const Feature* features, size_t featuresSize,
851 SkScalar width,
852 RunHandler* handler) const
853{
Ben Wagner1ca50522019-10-01 17:54:28 -0400854 SkASSERT(handler);
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400855 RunIteratorQueue runSegmenter;
Ben Wagner1ca50522019-10-01 17:54:28 -0400856 runSegmenter.insert(&font, 3); // The font iterator is always run last in case of tie.
857 runSegmenter.insert(&bidi, 2);
858 runSegmenter.insert(&script, 1);
859 runSegmenter.insert(&language, 0);
Ben Wagnera25fbef2017-08-30 13:56:19 -0400860
Ben Wagner24ee4e02019-10-11 10:36:10 -0400861 this->wrap(utf8, utf8Bytes, bidi, language, script, font, runSegmenter,
862 features, featuresSize, width, handler);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400863}
864
Ben Wagner51874e32019-04-04 15:21:20 -0400865void ShaperDrivenWrapper::wrap(char const * const utf8, size_t utf8Bytes,
866 const BiDiRunIterator& bidi,
867 const LanguageRunIterator& language,
868 const ScriptRunIterator& script,
869 const FontRunIterator& font,
870 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400871 const Feature* features, size_t featuresSize,
Ben Wagner51874e32019-04-04 15:21:20 -0400872 SkScalar width,
873 RunHandler* handler) const
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400874{
875 ShapedLine line;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400876
877 const char* utf8Start = nullptr;
878 const char* utf8End = utf8;
879 while (runSegmenter.advanceRuns()) { // For each item
880 utf8Start = utf8End;
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400881 utf8End = utf8 + runSegmenter.endOfCurrentRun();
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400882
Ben Wagner7415a422019-03-25 15:38:22 -0400883 ShapedRun model(RunHandler::Range(), SkFont(), 0, nullptr, 0);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400884 bool modelNeedsRegenerated = true;
Ben Wagner7415a422019-03-25 15:38:22 -0400885 int modelGlyphOffset = 0;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400886
887 struct TextProps {
888 int glyphLen = 0;
889 SkVector advance = {0, 0};
890 };
891 // map from character position to [safe to break, glyph position, advance]
892 std::unique_ptr<TextProps[]> modelText;
893 int modelTextOffset = 0;
Ben Wagner7415a422019-03-25 15:38:22 -0400894 SkVector modelAdvanceOffset = {0, 0};
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400895
896 while (utf8Start < utf8End) { // While there are still code points left in this item
897 size_t utf8runLength = utf8End - utf8Start;
898 if (modelNeedsRegenerated) {
899 model = shape(utf8, utf8Bytes,
900 utf8Start, utf8End,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400901 bidi, language, script, font,
902 features, featuresSize);
Ben Wagner7415a422019-03-25 15:38:22 -0400903 modelGlyphOffset = 0;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400904
905 SkVector advance = {0, 0};
John Stilesfbd050b2020-08-03 13:21:46 -0400906 modelText = std::make_unique<TextProps[]>(utf8runLength + 1);
Ben Wagnerb9cc1c62019-02-14 14:12:48 -0500907 size_t modelStartCluster = utf8Start - utf8;
Ben Wagner7415a422019-03-25 15:38:22 -0400908 for (size_t i = 0; i < model.fNumGlyphs; ++i) {
Ben Wagner84cc4612019-02-14 17:13:21 -0500909 SkASSERT(modelStartCluster <= model.fGlyphs[i].fCluster);
Ben Wagner2fe1e232019-02-14 17:37:02 -0500910 SkASSERT( model.fGlyphs[i].fCluster < (size_t)(utf8End - utf8));
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400911 if (!model.fGlyphs[i].fUnsafeToBreak) {
Ben Wagnerb9cc1c62019-02-14 14:12:48 -0500912 modelText[model.fGlyphs[i].fCluster - modelStartCluster].glyphLen = i;
913 modelText[model.fGlyphs[i].fCluster - modelStartCluster].advance = advance;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400914 }
915 advance += model.fGlyphs[i].fAdvance;
916 }
917 // Assume it is always safe to break after the end of an item
918 modelText[utf8runLength].glyphLen = model.fNumGlyphs;
919 modelText[utf8runLength].advance = model.fAdvance;
920 modelTextOffset = 0;
Ben Wagner7415a422019-03-25 15:38:22 -0400921 modelAdvanceOffset = {0, 0};
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400922 modelNeedsRegenerated = false;
923 }
924
925 // TODO: break iterator per item, but just reset position if needed?
926 // Maybe break iterator with model?
927 UBreakIterator& breakIterator = *fLineBreakIterator;
928 {
929 UErrorCode status = U_ZERO_ERROR;
Ben Wagner723a8772019-08-16 11:36:58 -0400930 UText sUtf8UText = UTEXT_INITIALIZER;
931 ICUUText utf8UText(utext_openUTF8(&sUtf8UText, utf8Start, utf8runLength, &status));
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400932 if (U_FAILURE(status)) {
933 SkDebugf("Could not create utf8UText: %s", u_errorName(status));
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400934 return;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400935 }
Ben Wagner723a8772019-08-16 11:36:58 -0400936 ubrk_setUText(&breakIterator, utf8UText.get(), &status);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400937 if (U_FAILURE(status)) {
938 SkDebugf("Could not setText on break iterator: %s", u_errorName(status));
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400939 return;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400940 }
941 }
942
Ben Wagner7415a422019-03-25 15:38:22 -0400943 ShapedRun best(RunHandler::Range(), SkFont(), 0, nullptr, 0,
944 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity });
945 bool bestIsInvalid = true;
946 bool bestUsesModelForGlyphs = false;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400947 SkScalar widthLeft = width - line.fAdvance.fX;
948
949 for (int32_t breakIteratorCurrent = ubrk_next(&breakIterator);
950 breakIteratorCurrent != UBRK_DONE;
951 breakIteratorCurrent = ubrk_next(&breakIterator))
952 {
953 // TODO: if past a safe to break, future safe to break will be at least as long
954
955 // TODO: adjust breakIteratorCurrent by ignorable whitespace
Ben Wagner7415a422019-03-25 15:38:22 -0400956 bool candidateUsesModelForGlyphs = false;
957 ShapedRun candidate = [&](const TextProps& props){
958 if (props.glyphLen) {
959 candidateUsesModelForGlyphs = true;
960 return ShapedRun(RunHandler::Range(utf8Start - utf8, breakIteratorCurrent),
Ben Wagner1e08a7c2019-03-27 15:37:13 -0400961 font.currentFont(), bidi.currentLevel(),
Ben Wagner7415a422019-03-25 15:38:22 -0400962 std::unique_ptr<ShapedGlyph[]>(),
963 props.glyphLen - modelGlyphOffset,
964 props.advance - modelAdvanceOffset);
965 } else {
966 return shape(utf8, utf8Bytes,
967 utf8Start, utf8Start + breakIteratorCurrent,
Ben Wagner24ee4e02019-10-11 10:36:10 -0400968 bidi, language, script, font,
969 features, featuresSize);
Ben Wagner7415a422019-03-25 15:38:22 -0400970 }
971 }(modelText[breakIteratorCurrent + modelTextOffset]);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400972 auto score = [widthLeft](const ShapedRun& run) -> SkScalar {
973 if (run.fAdvance.fX < widthLeft) {
Ben Wagner7415a422019-03-25 15:38:22 -0400974 return run.fUtf8Range.size();
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400975 } else {
976 return widthLeft - run.fAdvance.fX;
977 }
978 };
Ben Wagner7415a422019-03-25 15:38:22 -0400979 if (bestIsInvalid || score(best) < score(candidate)) {
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400980 best = std::move(candidate);
Ben Wagner7415a422019-03-25 15:38:22 -0400981 bestIsInvalid = false;
982 bestUsesModelForGlyphs = candidateUsesModelForGlyphs;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400983 }
984 }
985
986 // If nothing fit (best score is negative) and the line is not empty
987 if (width < line.fAdvance.fX + best.fAdvance.fX && !line.runs.empty()) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400988 emit(line, handler);
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400989 line.runs.reset();
990 line.fAdvance = {0, 0};
991 } else {
Ben Wagner7415a422019-03-25 15:38:22 -0400992 if (bestUsesModelForGlyphs) {
John Stilesfbd050b2020-08-03 13:21:46 -0400993 best.fGlyphs = std::make_unique<ShapedGlyph[]>(best.fNumGlyphs);
Ben Wagner7415a422019-03-25 15:38:22 -0400994 memcpy(best.fGlyphs.get(), model.fGlyphs.get() + modelGlyphOffset,
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400995 best.fNumGlyphs * sizeof(ShapedGlyph));
Ben Wagner7415a422019-03-25 15:38:22 -0400996 modelGlyphOffset += best.fNumGlyphs;
997 modelTextOffset += best.fUtf8Range.size();
998 modelAdvanceOffset += best.fAdvance;
Ben Wagner0ec8ec22018-09-04 18:17:13 -0400999 } else {
1000 modelNeedsRegenerated = true;
1001 }
Ben Wagner7415a422019-03-25 15:38:22 -04001002 utf8Start += best.fUtf8Range.size();
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001003 line.fAdvance += best.fAdvance;
1004 line.runs.emplace_back(std::move(best));
1005
1006 // If item broken, emit line (prevent remainder from accidentally fitting)
1007 if (utf8Start != utf8End) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001008 emit(line, handler);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001009 line.runs.reset();
1010 line.fAdvance = {0, 0};
1011 }
1012 }
1013 }
1014 }
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001015 emit(line, handler);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001016}
1017
Ben Wagner51874e32019-04-04 15:21:20 -04001018void ShapeThenWrap::wrap(char const * const utf8, size_t utf8Bytes,
1019 const BiDiRunIterator& bidi,
1020 const LanguageRunIterator& language,
1021 const ScriptRunIterator& script,
1022 const FontRunIterator& font,
1023 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001024 const Feature* features, size_t featuresSize,
Ben Wagner51874e32019-04-04 15:21:20 -04001025 SkScalar width,
1026 RunHandler* handler) const
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001027{
1028 SkTArray<ShapedRun> runs;
1029{
1030 UBreakIterator& lineBreakIterator = *fLineBreakIterator;
1031 UBreakIterator& graphemeBreakIterator = *fGraphemeBreakIterator;
Ben Wagner8d45a382017-11-16 10:08:28 -05001032 {
1033 UErrorCode status = U_ZERO_ERROR;
Ben Wagner723a8772019-08-16 11:36:58 -04001034 UText sUtf8UText = UTEXT_INITIALIZER;
1035 ICUUText utf8UText(utext_openUTF8(&sUtf8UText, utf8, utf8Bytes, &status));
Ben Wagner8d45a382017-11-16 10:08:28 -05001036 if (U_FAILURE(status)) {
1037 SkDebugf("Could not create utf8UText: %s", u_errorName(status));
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001038 return;
Ben Wagner8d45a382017-11-16 10:08:28 -05001039 }
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001040
Ben Wagner723a8772019-08-16 11:36:58 -04001041 ubrk_setUText(&lineBreakIterator, utf8UText.get(), &status);
Ben Wagner8d45a382017-11-16 10:08:28 -05001042 if (U_FAILURE(status)) {
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001043 SkDebugf("Could not setText on line break iterator: %s", u_errorName(status));
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001044 return;
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001045 }
Ben Wagner723a8772019-08-16 11:36:58 -04001046 ubrk_setUText(&graphemeBreakIterator, utf8UText.get(), &status);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001047 if (U_FAILURE(status)) {
1048 SkDebugf("Could not setText on grapheme break iterator: %s", u_errorName(status));
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001049 return;
Ben Wagner8d45a382017-11-16 10:08:28 -05001050 }
Ben Wagnera25fbef2017-08-30 13:56:19 -04001051 }
1052
Ben Wagner8d45a382017-11-16 10:08:28 -05001053 const char* utf8Start = nullptr;
1054 const char* utf8End = utf8;
1055 while (runSegmenter.advanceRuns()) {
1056 utf8Start = utf8End;
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001057 utf8End = utf8 + runSegmenter.endOfCurrentRun();
Ben Wagner8d45a382017-11-16 10:08:28 -05001058
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001059 runs.emplace_back(shape(utf8, utf8Bytes,
1060 utf8Start, utf8End,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001061 bidi, language, script, font,
1062 features, featuresSize));
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001063 ShapedRun& run = runs.back();
Ben Wagnera25fbef2017-08-30 13:56:19 -04001064
Ben Wagner8d45a382017-11-16 10:08:28 -05001065 uint32_t previousCluster = 0xFFFFFFFF;
Ben Wagner7415a422019-03-25 15:38:22 -04001066 for (size_t i = 0; i < run.fNumGlyphs; ++i) {
Ben Wagner8d45a382017-11-16 10:08:28 -05001067 ShapedGlyph& glyph = run.fGlyphs[i];
Ben Wagnerb9cc1c62019-02-14 14:12:48 -05001068 int32_t glyphCluster = glyph.fCluster;
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001069
1070 int32_t lineBreakIteratorCurrent = ubrk_current(&lineBreakIterator);
1071 while (lineBreakIteratorCurrent != UBRK_DONE &&
1072 lineBreakIteratorCurrent < glyphCluster)
Ben Wagner8d45a382017-11-16 10:08:28 -05001073 {
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001074 lineBreakIteratorCurrent = ubrk_next(&lineBreakIterator);
Ben Wagner2868b782017-08-31 14:12:27 -04001075 }
Ben Wagner8d45a382017-11-16 10:08:28 -05001076 glyph.fMayLineBreakBefore = glyph.fCluster != previousCluster &&
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001077 lineBreakIteratorCurrent == glyphCluster;
1078
1079 int32_t graphemeBreakIteratorCurrent = ubrk_current(&graphemeBreakIterator);
1080 while (graphemeBreakIteratorCurrent != UBRK_DONE &&
1081 graphemeBreakIteratorCurrent < glyphCluster)
1082 {
1083 graphemeBreakIteratorCurrent = ubrk_next(&graphemeBreakIterator);
1084 }
1085 glyph.fGraphemeBreakBefore = glyph.fCluster != previousCluster &&
1086 graphemeBreakIteratorCurrent == glyphCluster;
1087
Ben Wagner8d45a382017-11-16 10:08:28 -05001088 previousCluster = glyph.fCluster;
Ben Wagnera25fbef2017-08-30 13:56:19 -04001089 }
1090 }
Ben Wagner8d45a382017-11-16 10:08:28 -05001091}
1092
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001093// Iterate over the glyphs in logical order to find potential line lengths.
Ben Wagner8d45a382017-11-16 10:08:28 -05001094{
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001095 /** The position of the beginning of the line. */
1096 ShapedRunGlyphIterator beginning(runs);
1097
1098 /** The position of the candidate line break. */
1099 ShapedRunGlyphIterator candidateLineBreak(runs);
1100 SkScalar candidateLineBreakWidth = 0;
1101
1102 /** The position of the candidate grapheme break. */
1103 ShapedRunGlyphIterator candidateGraphemeBreak(runs);
1104 SkScalar candidateGraphemeBreakWidth = 0;
1105
1106 /** The position of the current location. */
1107 ShapedRunGlyphIterator current(runs);
1108 SkScalar currentWidth = 0;
1109 while (ShapedGlyph* glyph = current.current()) {
1110 // 'Break' at graphemes until a line boundary, then only at line boundaries.
1111 // Only break at graphemes if no line boundary is valid.
1112 if (current != beginning) {
1113 if (glyph->fGraphemeBreakBefore || glyph->fMayLineBreakBefore) {
1114 // TODO: preserve line breaks <= grapheme breaks
1115 // and prevent line breaks inside graphemes
1116 candidateGraphemeBreak = current;
1117 candidateGraphemeBreakWidth = currentWidth;
1118 if (glyph->fMayLineBreakBefore) {
1119 candidateLineBreak = current;
1120 candidateLineBreakWidth = currentWidth;
1121 }
1122 }
Ben Wagner8d45a382017-11-16 10:08:28 -05001123 }
1124
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001125 SkScalar glyphWidth = glyph->fAdvance.fX;
1126 // Break when overwidth, the glyph has a visual representation, and some space is used.
1127 if (width < currentWidth + glyphWidth && glyph->fHasVisual && candidateGraphemeBreakWidth > 0){
1128 if (candidateLineBreak != beginning) {
1129 beginning = candidateLineBreak;
1130 currentWidth -= candidateLineBreakWidth;
1131 candidateGraphemeBreakWidth -= candidateLineBreakWidth;
1132 candidateLineBreakWidth = 0;
1133 } else if (candidateGraphemeBreak != beginning) {
1134 beginning = candidateGraphemeBreak;
1135 candidateLineBreak = beginning;
1136 currentWidth -= candidateGraphemeBreakWidth;
1137 candidateGraphemeBreakWidth = 0;
1138 candidateLineBreakWidth = 0;
1139 } else {
1140 SK_ABORT("");
1141 }
1142
1143 if (width < currentWidth) {
1144 if (width < candidateGraphemeBreakWidth) {
1145 candidateGraphemeBreak = candidateLineBreak;
1146 candidateGraphemeBreakWidth = candidateLineBreakWidth;
1147 }
1148 current = candidateGraphemeBreak;
1149 currentWidth = candidateGraphemeBreakWidth;
1150 }
1151
1152 glyph = beginning.current();
1153 if (glyph) {
1154 glyph->fMustLineBreakBefore = true;
1155 }
1156
1157 } else {
1158 current.next();
1159 currentWidth += glyphWidth;
Ben Wagner8d45a382017-11-16 10:08:28 -05001160 }
Ben Wagner8d45a382017-11-16 10:08:28 -05001161 }
1162}
1163
1164// Reorder the runs and glyphs per line and write them out.
1165{
1166 ShapedRunGlyphIterator previousBreak(runs);
1167 ShapedRunGlyphIterator glyphIterator(runs);
Ben Wagner8d45a382017-11-16 10:08:28 -05001168 int previousRunIndex = -1;
1169 while (glyphIterator.current()) {
Ben Wagner00330082019-09-16 15:56:51 -04001170 const ShapedRunGlyphIterator current = glyphIterator;
Ben Wagner8d45a382017-11-16 10:08:28 -05001171 ShapedGlyph* nextGlyph = glyphIterator.next();
1172
Ben Wagner00330082019-09-16 15:56:51 -04001173 if (previousRunIndex != current.fRunIndex) {
Mike Reedb5784ac2018-11-12 09:35:15 -05001174 SkFontMetrics metrics;
Ben Wagner00330082019-09-16 15:56:51 -04001175 runs[current.fRunIndex].fFont.getMetrics(&metrics);
1176 previousRunIndex = current.fRunIndex;
Ben Wagner8d45a382017-11-16 10:08:28 -05001177 }
1178
1179 // Nothing can be written until the baseline is known.
1180 if (!(nextGlyph == nullptr || nextGlyph->fMustLineBreakBefore)) {
1181 continue;
1182 }
1183
Ben Wagner00330082019-09-16 15:56:51 -04001184 int numRuns = current.fRunIndex - previousBreak.fRunIndex + 1;
Ben Wagner8d45a382017-11-16 10:08:28 -05001185 SkAutoSTMalloc<4, UBiDiLevel> runLevels(numRuns);
1186 for (int i = 0; i < numRuns; ++i) {
1187 runLevels[i] = runs[previousBreak.fRunIndex + i].fLevel;
1188 }
1189 SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns);
1190 ubidi_reorderVisual(runLevels, numRuns, logicalFromVisual);
1191
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001192 // step through the runs in reverse visual order and the glyphs in reverse logical order
1193 // until a visible glyph is found and force them to the end of the visual line.
1194
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001195 handler->beginLine();
Ben Wagner8d45a382017-11-16 10:08:28 -05001196
Ben Wagner00330082019-09-16 15:56:51 -04001197 struct SubRun { const ShapedRun& run; size_t startGlyphIndex; size_t endGlyphIndex; };
1198 auto makeSubRun = [&runs, &previousBreak, &current, &logicalFromVisual](size_t visualIndex){
1199 int logicalIndex = previousBreak.fRunIndex + logicalFromVisual[visualIndex];
1200 const auto& run = runs[logicalIndex];
Ben Wagner7415a422019-03-25 15:38:22 -04001201 size_t startGlyphIndex = (logicalIndex == previousBreak.fRunIndex)
1202 ? previousBreak.fGlyphIndex
1203 : 0;
Ben Wagner00330082019-09-16 15:56:51 -04001204 size_t endGlyphIndex = (logicalIndex == current.fRunIndex)
1205 ? current.fGlyphIndex + 1
Ben Wagner3bdb69c2019-04-01 19:01:09 -04001206 : run.fNumGlyphs;
Ben Wagner00330082019-09-16 15:56:51 -04001207 return SubRun{ run, startGlyphIndex, endGlyphIndex };
1208 };
1209 auto makeRunInfo = [](const SubRun& sub) {
1210 uint32_t startUtf8 = sub.run.fGlyphs[sub.startGlyphIndex].fCluster;
1211 uint32_t endUtf8 = (sub.endGlyphIndex < sub.run.fNumGlyphs)
1212 ? sub.run.fGlyphs[sub.endGlyphIndex].fCluster
1213 : sub.run.fUtf8Range.end();
Florin Malita950243d2019-01-11 11:08:35 -05001214
Ben Wagner00330082019-09-16 15:56:51 -04001215 SkVector advance = SkVector::Make(0, 0);
1216 for (size_t i = sub.startGlyphIndex; i < sub.endGlyphIndex; ++i) {
1217 advance += sub.run.fGlyphs[i].fAdvance;
1218 }
1219
1220 return RunHandler::RunInfo{
1221 sub.run.fFont,
1222 sub.run.fLevel,
1223 advance,
1224 sub.endGlyphIndex - sub.startGlyphIndex,
1225 RunHandler::Range(startUtf8, endUtf8 - startUtf8)
1226 };
1227 };
1228
1229 for (int i = 0; i < numRuns; ++i) {
1230 handler->runInfo(makeRunInfo(makeSubRun(i)));
1231 }
1232 handler->commitRunInfo();
1233 for (int i = 0; i < numRuns; ++i) {
1234 SubRun sub = makeSubRun(i);
1235 append(handler, makeRunInfo(sub), sub.run, sub.startGlyphIndex, sub.endGlyphIndex);
Ben Wagner8d45a382017-11-16 10:08:28 -05001236 }
1237
Florin Malita500133b2019-02-07 10:56:55 -05001238 handler->commitLine();
1239
Ben Wagner8d45a382017-11-16 10:08:28 -05001240 previousRunIndex = -1;
1241 previousBreak = glyphIterator;
1242 }
1243}
Ben Wagnera25fbef2017-08-30 13:56:19 -04001244}
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001245
Ben Wagner6bb79bb2019-05-15 10:50:20 -04001246void ShapeDontWrapOrReorder::wrap(char const * const utf8, size_t utf8Bytes,
1247 const BiDiRunIterator& bidi,
1248 const LanguageRunIterator& language,
1249 const ScriptRunIterator& script,
1250 const FontRunIterator& font,
1251 RunIteratorQueue& runSegmenter,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001252 const Feature* features, size_t featuresSize,
Ben Wagner6bb79bb2019-05-15 10:50:20 -04001253 SkScalar width,
1254 RunHandler* handler) const
1255{
1256 sk_ignore_unused_variable(width);
1257 SkTArray<ShapedRun> runs;
1258
1259 const char* utf8Start = nullptr;
1260 const char* utf8End = utf8;
1261 while (runSegmenter.advanceRuns()) {
1262 utf8Start = utf8End;
1263 utf8End = utf8 + runSegmenter.endOfCurrentRun();
1264
1265 runs.emplace_back(shape(utf8, utf8Bytes,
1266 utf8Start, utf8End,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001267 bidi, language, script, font,
1268 features, featuresSize));
Ben Wagner6bb79bb2019-05-15 10:50:20 -04001269 }
1270
1271 handler->beginLine();
1272 for (const auto& run : runs) {
1273 const RunHandler::RunInfo info = {
1274 run.fFont,
1275 run.fLevel,
1276 run.fAdvance,
1277 run.fNumGlyphs,
1278 run.fUtf8Range
1279 };
1280 handler->runInfo(info);
1281 }
1282 handler->commitRunInfo();
1283 for (const auto& run : runs) {
1284 const RunHandler::RunInfo info = {
1285 run.fFont,
1286 run.fLevel,
1287 run.fAdvance,
1288 run.fNumGlyphs,
1289 run.fUtf8Range
1290 };
1291 append(handler, info, run, 0, run.fNumGlyphs);
1292 }
1293 handler->commitLine();
1294}
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001295
Ben Wagner51874e32019-04-04 15:21:20 -04001296ShapedRun ShaperHarfBuzz::shape(char const * const utf8,
Ben Wagner7415a422019-03-25 15:38:22 -04001297 size_t const utf8Bytes,
1298 char const * const utf8Start,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001299 char const * const utf8End,
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001300 const BiDiRunIterator& bidi,
1301 const LanguageRunIterator& language,
1302 const ScriptRunIterator& script,
Ben Wagner24ee4e02019-10-11 10:36:10 -04001303 const FontRunIterator& font,
1304 Feature const * const features, size_t const featuresSize) const
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001305{
Ben Wagner7415a422019-03-25 15:38:22 -04001306 size_t utf8runLength = utf8End - utf8Start;
1307 ShapedRun run(RunHandler::Range(utf8Start - utf8, utf8runLength),
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001308 font.currentFont(), bidi.currentLevel(), nullptr, 0);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001309
1310 hb_buffer_t* buffer = fBuffer.get();
1311 SkAutoTCallVProc<hb_buffer_t, hb_buffer_clear_contents> autoClearBuffer(buffer);
1312 hb_buffer_set_content_type(buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
1313 hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
1314
Ben Wagner56a48bb2020-01-16 10:07:41 -05001315 // Documentation for HB_BUFFER_FLAG_BOT/EOT at 763e5466c0a03a7c27020e1e2598e488612529a7.
1316 // Currently BOT forces a dotted circle when first codepoint is a mark; EOT has no effect.
1317 // Avoid adding dotted circle, re-evaluate if BOT/EOT change. See https://skbug.com/9618.
1318 // hb_buffer_set_flags(buffer, HB_BUFFER_FLAG_BOT | HB_BUFFER_FLAG_EOT);
Ben Wagner2fc14742019-02-06 16:37:44 -05001319
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001320 // Add precontext.
1321 hb_buffer_add_utf8(buffer, utf8, utf8Start - utf8, utf8Start - utf8, 0);
1322
1323 // Populate the hb_buffer directly with utf8 cluster indexes.
1324 const char* utf8Current = utf8Start;
1325 while (utf8Current < utf8End) {
Ben Wagnerb9cc1c62019-02-14 14:12:48 -05001326 unsigned int cluster = utf8Current - utf8;
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001327 hb_codepoint_t u = utf8_next(&utf8Current, utf8End);
1328 hb_buffer_add(buffer, u, cluster);
1329 }
1330
1331 // Add postcontext.
1332 hb_buffer_add_utf8(buffer, utf8Current, utf8 + utf8Bytes - utf8Current, 0, 0);
1333
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001334 hb_direction_t direction = is_LTR(bidi.currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL;
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001335 hb_buffer_set_direction(buffer, direction);
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001336 hb_buffer_set_script(buffer, hb_script_from_iso15924_tag((hb_tag_t)script.currentScript()));
Ben Wagner94aed0b2020-06-08 10:54:10 -04001337 // Buffers with HB_LANGUAGE_INVALID race since hb_language_get_default is not thread safe.
1338 // The user must provide a language, but may provide data hb_language_from_string cannot use.
1339 // Use "und" for the undefined language in this case (RFC5646 4.1 5).
1340 hb_language_t hbLanguage = hb_language_from_string(language.currentLanguage(), -1);
1341 if (hbLanguage == HB_LANGUAGE_INVALID) {
1342 hbLanguage = fUndefinedLanguage;
1343 }
1344 hb_buffer_set_language(buffer, hbLanguage);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001345 hb_buffer_guess_segment_properties(buffer);
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001346
Ben Wagner4651b992020-05-13 10:44:49 -04001347 // TODO: better cache HBFace (data) / hbfont (typeface)
1348 // An HBFace is expensive (it sanitizes the bits).
1349 // An HBFont is fairly inexpensive.
1350 // An HBFace is actually tied to the data, not the typeface.
1351 // The size of 100 here is completely arbitrary and used to match libtxt.
1352 static SkLRUCache<SkFontID, HBFace> gHBFaceCache(100);
1353 static SkMutex gHBFaceCacheMutex;
1354 HBFont hbFont;
1355 {
1356 SkAutoMutexExclusive lock(gHBFaceCacheMutex);
1357 SkFontID dataId = font.currentFont().getTypeface()->uniqueID();
1358 HBFace* hbFaceCached = gHBFaceCache.find(dataId);
1359 if (!hbFaceCached) {
1360 HBFace hbFace(create_hb_face(*font.currentFont().getTypeface()));
1361 hbFaceCached = gHBFaceCache.insert(dataId, std::move(hbFace));
1362 }
1363 hbFont = create_hb_font(font.currentFont(), *hbFaceCached);
1364 }
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001365 if (!hbFont) {
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001366 return run;
1367 }
Ben Wagner24ee4e02019-10-11 10:36:10 -04001368
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());
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001388 unsigned len = hb_buffer_get_length(buffer);
1389 if (len == 0) {
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001390 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
Ben Wagner7415a422019-03-25 15:38:22 -04001401 run = ShapedRun(RunHandler::Range(utf8Start - utf8, utf8runLength),
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001402 font.currentFont(), bidi.currentLevel(),
Ben Wagner454e5fb2019-02-08 17:46:38 -05001403 std::unique_ptr<ShapedGlyph[]>(new ShapedGlyph[len]), len);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001404 int scaleX, scaleY;
Ben Wagner1e08a7c2019-03-27 15:37:13 -04001405 hb_font_get_scale(hbFont.get(), &scaleX, &scaleY);
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001406 double textSizeY = run.fFont.getSize() / scaleY;
1407 double textSizeX = run.fFont.getSize() / scaleX * run.fFont.getScaleX();
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 * textSizeX;
Ben Wagnerbdf2c872019-10-24 17:06:37 -04001414 glyph.fOffset.fY = -(pos[i].y_offset * textSizeY); // HarfBuzz y-up, Skia y-down
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001415 glyph.fAdvance.fX = pos[i].x_advance * textSizeX;
Ben Wagnerbdf2c872019-10-24 17:06:37 -04001416 glyph.fAdvance.fY = -(pos[i].y_advance * textSizeY); // HarfBuzz y-up, Skia y-down
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001417
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);
Kevin Lubick867da4b2019-02-22 15:55:39 -05001423#if SK_HB_VERSION_CHECK(1, 5, 0)
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001424 glyph.fUnsafeToBreak = info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
Kevin Lubick867da4b2019-02-22 15:55:39 -05001425#else
1426 glyph.fUnsafeToBreak = false;
1427#endif
Ben Wagner0ec8ec22018-09-04 18:17:13 -04001428 glyph.fMustLineBreakBefore = false;
1429
1430 runAdvance += glyph.fAdvance;
1431 }
1432 run.fAdvance = runAdvance;
1433
1434 return run;
1435}
Ben Wagner51874e32019-04-04 15:21:20 -04001436
1437} // namespace
1438
1439std::unique_ptr<SkShaper::BiDiRunIterator>
1440SkShaper::MakeIcuBiDiRunIterator(const char* utf8, size_t utf8Bytes, uint8_t bidiLevel) {
1441 // ubidi only accepts utf16 (though internally it basically works on utf32 chars).
1442 // We want an ubidi_setPara(UBiDi*, UText*, UBiDiLevel, UBiDiLevel*, UErrorCode*);
1443 if (!SkTFitsIn<int32_t>(utf8Bytes)) {
1444 SkDEBUGF("Bidi error: text too long");
1445 return nullptr;
1446 }
1447
1448 UErrorCode status = U_ZERO_ERROR;
1449
1450 // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR
1451 int32_t utf16Units;
1452 u_strFromUTF8(nullptr, 0, &utf16Units, utf8, utf8Bytes, &status);
1453 status = U_ZERO_ERROR;
1454 std::unique_ptr<UChar[]> utf16(new UChar[utf16Units]);
1455 u_strFromUTF8(utf16.get(), utf16Units, nullptr, utf8, utf8Bytes, &status);
1456 if (U_FAILURE(status)) {
1457 SkDEBUGF("Invalid utf8 input: %s", u_errorName(status));
1458 return nullptr;
1459 }
1460
1461 ICUBiDi bidi(ubidi_openSized(utf16Units, 0, &status));
1462 if (U_FAILURE(status)) {
1463 SkDEBUGF("Bidi error: %s", u_errorName(status));
1464 return nullptr;
1465 }
1466 SkASSERT(bidi);
1467
1468 // The required lifetime of utf16 isn't well documented.
1469 // It appears it isn't used after ubidi_setPara except through ubidi_getText.
1470 ubidi_setPara(bidi.get(), utf16.get(), utf16Units, bidiLevel, nullptr, &status);
1471 if (U_FAILURE(status)) {
1472 SkDEBUGF("Bidi error: %s", u_errorName(status));
1473 return nullptr;
1474 }
1475
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001476 return std::make_unique<IcuBiDiRunIterator>(utf8, utf8 + utf8Bytes, std::move(bidi));
Ben Wagner51874e32019-04-04 15:21:20 -04001477}
1478
1479std::unique_ptr<SkShaper::ScriptRunIterator>
1480SkShaper::MakeHbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001481 return std::make_unique<HbIcuScriptRunIterator>(utf8, utf8Bytes);
Ben Wagner51874e32019-04-04 15:21:20 -04001482}
1483
Florin Malita42684332019-07-26 14:54:40 -04001484std::unique_ptr<SkShaper> SkShaper::MakeShaperDrivenWrapper(sk_sp<SkFontMgr> fontmgr) {
1485 return MakeHarfBuzz(std::move(fontmgr), true);
Ben Wagner51874e32019-04-04 15:21:20 -04001486}
Florin Malita42684332019-07-26 14:54:40 -04001487std::unique_ptr<SkShaper> SkShaper::MakeShapeThenWrap(sk_sp<SkFontMgr> fontmgr) {
1488 return MakeHarfBuzz(std::move(fontmgr), false);
Ben Wagner51874e32019-04-04 15:21:20 -04001489}
Florin Malita42684332019-07-26 14:54:40 -04001490std::unique_ptr<SkShaper> SkShaper::MakeShapeDontWrapOrReorder(sk_sp<SkFontMgr> fontmgr) {
Ben Wagner6bb79bb2019-05-15 10:50:20 -04001491 #if defined(SK_USING_THIRD_PARTY_ICU)
1492 if (!SkLoadICU()) {
1493 SkDEBUGF("SkLoadICU() failed!\n");
1494 return nullptr;
1495 }
1496 #endif
1497 HBBuffer buffer(hb_buffer_create());
1498 if (!buffer) {
1499 SkDEBUGF("Could not create hb_buffer");
1500 return nullptr;
1501 }
1502
Mike Kleinf46d5ca2019-12-11 10:45:01 -05001503 return std::make_unique<ShapeDontWrapOrReorder>(std::move(buffer), nullptr, nullptr,
Florin Malita42684332019-07-26 14:54:40 -04001504 std::move(fontmgr));
Ben Wagner6bb79bb2019-05-15 10:50:20 -04001505}