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/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" |
23 | #include "include/private/SkMutex.h" |
24 | #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" |
29 | #include "src/core/SkLRUCache.h" |
30 | #include "src/core/SkSpan.h" |
31 | #include "src/core/SkTDPQueue.h" |
32 | #include "src/utils/SkUTF.h" |
33 | |
34 | #include <hb.h> |
35 | #include <hb-icu.h> |
36 | #include <hb-ot.h> |
37 | #include <unicode/ubidi.h> |
38 | #include <unicode/ubrk.h> |
39 | #include <unicode/umachine.h> |
40 | #include <unicode/urename.h> |
41 | #include <unicode/uscript.h> |
42 | #include <unicode/ustring.h> |
43 | #include <unicode/utext.h> |
44 | #include <unicode/utypes.h> |
45 | |
46 | #include <cstring> |
47 | #include <memory> |
48 | #include <type_traits> |
49 | #include <utility> |
50 | |
51 | #if defined(SK_USING_THIRD_PARTY_ICU) |
52 | #include "SkLoadICU.h" |
53 | #endif |
54 | |
55 | // 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 | |
64 | namespace sknonstd { |
65 | template <> struct is_bitmask_enum<hb_buffer_flags_t> : std::true_type {}; |
66 | } // namespace sknonstd |
67 | |
68 | namespace { |
69 | template <typename T,typename P,P* p> using resource = std::unique_ptr<T, SkFunctionWrapper<P, p>>; |
70 | using HBBlob = resource<hb_blob_t , decltype(hb_blob_destroy) , hb_blob_destroy >; |
71 | using HBFace = resource<hb_face_t , decltype(hb_face_destroy) , hb_face_destroy >; |
72 | using HBFont = resource<hb_font_t , decltype(hb_font_destroy) , hb_font_destroy >; |
73 | using HBBuffer = resource<hb_buffer_t , decltype(hb_buffer_destroy), hb_buffer_destroy>; |
74 | using ICUBiDi = resource<UBiDi , decltype(ubidi_close) , ubidi_close >; |
75 | using ICUBrk = resource<UBreakIterator, decltype(ubrk_close) , ubrk_close >; |
76 | using ICUUText = resource<UText , decltype(utext_close) , utext_close >; |
77 | |
78 | hb_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 | |
84 | hb_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 | |
96 | hb_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 | |
104 | unsigned 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); |
121 | font.textToGlyphs(unicode.get(), count * sizeof(SkUnichar), SkTextEncoding::kUTF32, |
122 | glyph.get(), count); |
123 | |
124 | // Copy the results back to the sparse array. |
125 | unsigned int done; |
126 | for (done = 0; done < count && glyph[done] != 0; done++) { |
127 | *glyphs = glyph[done]; |
128 | glyphs = SkTAddOffset<hb_codepoint_t>(glyphs, glyph_stride); |
129 | } |
130 | // return 'done' to allow HarfBuzz to synthesize with NFC and spaces, return 'count' to avoid |
131 | return done; |
132 | } |
133 | |
134 | hb_position_t skhb_glyph_h_advance(hb_font_t* hb_font, |
135 | void* font_data, |
136 | hb_codepoint_t hbGlyph, |
137 | void* user_data) { |
138 | SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
139 | |
140 | SkScalar advance; |
141 | SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph); |
142 | |
143 | font.getWidths(&skGlyph, 1, &advance); |
144 | if (!font.isSubpixel()) { |
145 | advance = SkScalarRoundToInt(advance); |
146 | } |
147 | return skhb_position(advance); |
148 | } |
149 | |
150 | void 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. |
189 | hb_bool_t skhb_glyph_extents(hb_font_t* hb_font, |
190 | void* font_data, |
191 | hb_codepoint_t hbGlyph, |
192 | hb_glyph_extents_t* extents, |
193 | void* user_data) { |
194 | SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
195 | SkASSERT(extents); |
196 | |
197 | SkRect sk_bounds; |
198 | SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph); |
199 | |
200 | font.getWidths(&skGlyph, 1, nullptr, &sk_bounds); |
201 | 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 | |
213 | #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 | |
218 | hb_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); |
224 | #if SK_HB_VERSION_CHECK(2, 0, 0) |
225 | hb_font_funcs_set_nominal_glyphs_func(funcs, skhb_nominal_glyphs, nullptr, nullptr); |
226 | #else |
227 | sk_ignore_unused_variable(skhb_nominal_glyphs); |
228 | #endif |
229 | hb_font_funcs_set_glyph_h_advance_func(funcs, skhb_glyph_h_advance, nullptr, nullptr); |
230 | #if SK_HB_VERSION_CHECK(1, 8, 6) |
231 | hb_font_funcs_set_glyph_h_advances_func(funcs, skhb_glyph_h_advances, nullptr, nullptr); |
232 | #else |
233 | sk_ignore_unused_variable(skhb_glyph_h_advances); |
234 | #endif |
235 | 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 | |
243 | hb_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 | |
246 | auto data = typeface.copyTableData(tag); |
247 | if (!data) { |
248 | return nullptr; |
249 | } |
250 | SkData* rawData = data.release(); |
251 | return hb_blob_create(reinterpret_cast<char*>(rawData->writable_data()), rawData->size(), |
252 | HB_MEMORY_MODE_READONLY, rawData, [](void* ctx) { |
253 | SkSafeUnref(((SkData*)ctx)); |
254 | }); |
255 | } |
256 | |
257 | HBBlob 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; })); |
264 | } else { |
265 | // 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 | |
276 | SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;) |
277 | |
278 | HBFace 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()) { |
283 | HBBlob blob(stream_to_blob(std::move(typefaceAsset))); |
284 | face.reset(hb_face_create(blob.get(), (unsigned)index)); |
285 | } 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)); })); |
290 | } |
291 | SkASSERT(face); |
292 | if (!face) { |
293 | return nullptr; |
294 | } |
295 | hb_face_set_index(face.get(), (unsigned)index); |
296 | 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 | |
306 | HBFont 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 | ) |
311 | |
312 | HBFont otFont(hb_font_create(face.get())); |
313 | SkASSERT(otFont); |
314 | if (!otFont) { |
315 | return nullptr; |
316 | } |
317 | hb_ot_font_set_funcs(otFont.get()); |
318 | int axis_count = font.getTypeface()->getVariationDesignPosition(nullptr, 0); |
319 | if (axis_count > 0) { |
320 | SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate> axis_values(axis_count); |
321 | if (font.getTypeface()->getVariationDesignPosition(axis_values, axis_count) == axis_count) { |
322 | hb_font_set_variations(otFont.get(), |
323 | reinterpret_cast<hb_variation_t*>(axis_values.get()), |
324 | axis_count); |
325 | } |
326 | } |
327 | |
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; |
338 | } |
339 | |
340 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
341 | static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
342 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
343 | return val < 0 ? 0xFFFD : val; |
344 | } |
345 | |
346 | class IcuBiDiRunIterator final : public SkShaper::BiDiRunIterator { |
347 | public: |
348 | IcuBiDiRunIterator(const char* utf8, const char* end, ICUBiDi bidi) |
349 | : fBidi(std::move(bidi)) |
350 | , fEndOfCurrentRun(utf8) |
351 | , fBegin(utf8) |
352 | , fEnd(end) |
353 | , 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); |
360 | SkUnichar u = utf8_next(&fEndOfCurrentRun, fEnd); |
361 | fUTF16LogicalPosition += SkUTF::ToUTF16(u); |
362 | UBiDiLevel level; |
363 | while (fUTF16LogicalPosition < endPosition) { |
364 | level = ubidi_getLevelAt(fBidi.get(), fUTF16LogicalPosition); |
365 | if (level != fLevel) { |
366 | break; |
367 | } |
368 | u = utf8_next(&fEndOfCurrentRun, fEnd); |
369 | |
370 | fUTF16LogicalPosition += SkUTF::ToUTF16(u); |
371 | } |
372 | } |
373 | size_t endOfCurrentRun() const override { |
374 | return fEndOfCurrentRun - fBegin; |
375 | } |
376 | bool atEnd() const override { |
377 | return fUTF16LogicalPosition == ubidi_getLength(fBidi.get()); |
378 | } |
379 | |
380 | UBiDiLevel currentLevel() const override { |
381 | return fLevel; |
382 | } |
383 | private: |
384 | ICUBiDi fBidi; |
385 | char const * fEndOfCurrentRun; |
386 | char const * const fBegin; |
387 | char const * const fEnd; |
388 | int32_t fUTF16LogicalPosition; |
389 | UBiDiLevel fLevel; |
390 | }; |
391 | |
392 | class HbIcuScriptRunIterator final : public SkShaper::ScriptRunIterator { |
393 | public: |
394 | HbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes) |
395 | : fCurrent(utf8), fBegin(utf8), fEnd(fCurrent + utf8Bytes) |
396 | , fCurrentScript(HB_SCRIPT_UNKNOWN) |
397 | {} |
398 | 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 | } |
408 | void consume() override { |
409 | SkASSERT(fCurrent < fEnd); |
410 | SkUnichar u = utf8_next(&fCurrent, fEnd); |
411 | fCurrentScript = hb_script_from_icu(u); |
412 | while (fCurrent < fEnd) { |
413 | const char* prev = fCurrent; |
414 | u = utf8_next(&fCurrent, fEnd); |
415 | const hb_script_t script = hb_script_from_icu(u); |
416 | 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 | } |
431 | size_t endOfCurrentRun() const override { |
432 | return fCurrent - fBegin; |
433 | } |
434 | bool atEnd() const override { |
435 | return fCurrent == fEnd; |
436 | } |
437 | |
438 | SkFourByteTag currentScript() const override { |
439 | return SkSetFourByteTag(HB_UNTAG(fCurrentScript)); |
440 | } |
441 | private: |
442 | char const * fCurrent; |
443 | char const * const fBegin; |
444 | char const * const fEnd; |
445 | hb_script_t fCurrentScript; |
446 | }; |
447 | |
448 | class RunIteratorQueue { |
449 | public: |
450 | void insert(SkShaper::RunIterator* runIterator, int priority) { |
451 | fEntries.insert({runIterator, priority}); |
452 | } |
453 | |
454 | bool advanceRuns() { |
455 | const SkShaper::RunIterator* leastRun = fEntries.peek().runIterator; |
456 | if (leastRun->atEnd()) { |
457 | SkASSERT(this->allRunsAreAtEnd()); |
458 | return false; |
459 | } |
460 | const size_t leastEnd = leastRun->endOfCurrentRun(); |
461 | SkShaper::RunIterator* currentRun = nullptr; |
462 | SkDEBUGCODE(size_t previousEndOfCurrentRun); |
463 | while ((currentRun = fEntries.peek().runIterator)->endOfCurrentRun() <= leastEnd) { |
464 | int priority = fEntries.peek().priority; |
465 | fEntries.pop(); |
466 | SkDEBUGCODE(previousEndOfCurrentRun = currentRun->endOfCurrentRun()); |
467 | currentRun->consume(); |
468 | SkASSERT(previousEndOfCurrentRun < currentRun->endOfCurrentRun()); |
469 | fEntries.insert({currentRun, priority}); |
470 | } |
471 | return true; |
472 | } |
473 | |
474 | size_t endOfCurrentRun() const { |
475 | return fEntries.peek().runIterator->endOfCurrentRun(); |
476 | } |
477 | |
478 | private: |
479 | bool allRunsAreAtEnd() const { |
480 | for (int i = 0; i < fEntries.count(); ++i) { |
481 | if (!fEntries.at(i).runIterator->atEnd()) { |
482 | return false; |
483 | } |
484 | } |
485 | return true; |
486 | } |
487 | |
488 | 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); |
496 | } |
497 | SkTDPQueue<Entry, CompareEntry> fEntries; |
498 | }; |
499 | |
500 | struct ShapedGlyph { |
501 | SkGlyphID fID; |
502 | uint32_t fCluster; |
503 | SkPoint fOffset; |
504 | SkVector fAdvance; |
505 | bool fMayLineBreakBefore; |
506 | bool fMustLineBreakBefore; |
507 | bool fHasVisual; |
508 | bool fGraphemeBreakBefore; |
509 | bool fUnsafeToBreak; |
510 | }; |
511 | struct ShapedRun { |
512 | 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) |
516 | {} |
517 | |
518 | SkShaper::RunHandler::Range fUtf8Range; |
519 | SkFont fFont; |
520 | UBiDiLevel fLevel; |
521 | std::unique_ptr<ShapedGlyph[]> fGlyphs; |
522 | size_t fNumGlyphs; |
523 | SkVector fAdvance; |
524 | }; |
525 | struct ShapedLine { |
526 | SkTArray<ShapedRun> runs; |
527 | SkVector fAdvance = { 0, 0 }; |
528 | }; |
529 | |
530 | constexpr bool is_LTR(UBiDiLevel level) { |
531 | return (level & 1) == 0; |
532 | } |
533 | |
534 | void append(SkShaper::RunHandler* handler, const SkShaper::RunHandler::RunInfo& runInfo, |
535 | const ShapedRun& run, size_t startGlyphIndex, size_t endGlyphIndex) { |
536 | SkASSERT(startGlyphIndex <= endGlyphIndex); |
537 | const size_t glyphLen = endGlyphIndex - startGlyphIndex; |
538 | |
539 | const auto buffer = handler->runBuffer(runInfo); |
540 | SkASSERT(buffer.glyphs); |
541 | SkASSERT(buffer.positions); |
542 | |
543 | SkVector advance = {0,0}; |
544 | for (size_t i = 0; i < glyphLen; i++) { |
545 | // Glyphs are in logical order, but output ltr since PDF readers seem to expect that. |
546 | const ShapedGlyph& glyph = run.fGlyphs[is_LTR(run.fLevel) ? startGlyphIndex + i |
547 | : endGlyphIndex - 1 - i]; |
548 | buffer.glyphs[i] = glyph.fID; |
549 | if (buffer.offsets) { |
550 | buffer.positions[i] = advance + buffer.point; |
551 | buffer.offsets[i] = glyph.fOffset; |
552 | } else { |
553 | buffer.positions[i] = advance + buffer.point + glyph.fOffset; |
554 | } |
555 | if (buffer.clusters) { |
556 | buffer.clusters[i] = glyph.fCluster; |
557 | } |
558 | advance += glyph.fAdvance; |
559 | } |
560 | handler->commitRunBuffer(runInfo); |
561 | } |
562 | |
563 | void emit(const ShapedLine& line, SkShaper::RunHandler* handler) { |
564 | // Reorder the runs and glyphs per line and write them out. |
565 | handler->beginLine(); |
566 | |
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 | |
575 | 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 = { |
580 | run.fFont, |
581 | run.fLevel, |
582 | run.fAdvance, |
583 | run.fNumGlyphs, |
584 | run.fUtf8Range |
585 | }; |
586 | handler->runInfo(info); |
587 | } |
588 | handler->commitRunInfo(); |
589 | for (int i = 0; i < numRuns; ++i) { |
590 | int logicalIndex = logicalFromVisual[i]; |
591 | |
592 | 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 | } |
602 | |
603 | handler->commitLine(); |
604 | } |
605 | |
606 | struct 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; |
650 | size_t fGlyphIndex; |
651 | }; |
652 | |
653 | class ShaperHarfBuzz : public SkShaper { |
654 | public: |
655 | ShaperHarfBuzz(HBBuffer, ICUBrk line, ICUBrk grapheme, sk_sp<SkFontMgr>); |
656 | |
657 | protected: |
658 | ICUBrk fLineBreakIterator; |
659 | ICUBrk fGraphemeBreakIterator; |
660 | |
661 | 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&, |
667 | const FontRunIterator&, |
668 | const Feature*, size_t featuresSize) const; |
669 | private: |
670 | const sk_sp<SkFontMgr> fFontMgr; |
671 | HBBuffer fBuffer; |
672 | hb_language_t fUndefinedLanguage; |
673 | |
674 | void shape(const char* utf8, size_t utf8Bytes, |
675 | const SkFont&, |
676 | bool leftToRight, |
677 | SkScalar width, |
678 | RunHandler*) const override; |
679 | |
680 | void shape(const char* utf8Text, size_t textBytes, |
681 | FontRunIterator&, |
682 | BiDiRunIterator&, |
683 | ScriptRunIterator&, |
684 | LanguageRunIterator&, |
685 | SkScalar width, |
686 | RunHandler*) const override; |
687 | |
688 | 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 | |
697 | virtual void wrap(char const * const utf8, size_t utf8Bytes, |
698 | const BiDiRunIterator&, |
699 | const LanguageRunIterator&, |
700 | const ScriptRunIterator&, |
701 | const FontRunIterator&, |
702 | RunIteratorQueue& runSegmenter, |
703 | const Feature*, size_t featuresSize, |
704 | SkScalar width, |
705 | RunHandler*) const = 0; |
706 | }; |
707 | |
708 | class ShaperDrivenWrapper : public ShaperHarfBuzz { |
709 | public: |
710 | using ShaperHarfBuzz::ShaperHarfBuzz; |
711 | private: |
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, |
718 | const Feature*, size_t featuresSize, |
719 | SkScalar width, |
720 | RunHandler*) const override; |
721 | }; |
722 | |
723 | class ShapeThenWrap : public ShaperHarfBuzz { |
724 | public: |
725 | using ShaperHarfBuzz::ShaperHarfBuzz; |
726 | private: |
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, |
733 | const Feature*, size_t featuresSize, |
734 | SkScalar width, |
735 | RunHandler*) const override; |
736 | }; |
737 | |
738 | class ShapeDontWrapOrReorder : public ShaperHarfBuzz { |
739 | public: |
740 | using ShaperHarfBuzz::ShaperHarfBuzz; |
741 | private: |
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, |
748 | const Feature*, size_t featuresSize, |
749 | SkScalar width, |
750 | RunHandler*) const override; |
751 | }; |
752 | |
753 | static std::unique_ptr<SkShaper> MakeHarfBuzz(sk_sp<SkFontMgr> fontmgr, bool correct) { |
754 | #if defined(SK_USING_THIRD_PARTY_ICU) |
755 | if (!SkLoadICU()) { |
756 | SkDEBUGF("SkLoadICU() failed!\n" ); |
757 | return nullptr; |
758 | } |
759 | #endif |
760 | HBBuffer buffer(hb_buffer_create()); |
761 | if (!buffer) { |
762 | SkDEBUGF("Could not create hb_buffer" ); |
763 | return nullptr; |
764 | } |
765 | |
766 | UErrorCode status = U_ZERO_ERROR; |
767 | 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; |
771 | } |
772 | |
773 | 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; |
777 | } |
778 | |
779 | if (correct) { |
780 | return std::make_unique<ShaperDrivenWrapper>(std::move(buffer), |
781 | std::move(lineBreakIterator), |
782 | std::move(graphemeBreakIterator), |
783 | std::move(fontmgr)); |
784 | } else { |
785 | return std::make_unique<ShapeThenWrap>(std::move(buffer), |
786 | std::move(lineBreakIterator), |
787 | std::move(graphemeBreakIterator), |
788 | std::move(fontmgr)); |
789 | } |
790 | } |
791 | |
792 | ShaperHarfBuzz::ShaperHarfBuzz(HBBuffer buffer, ICUBrk line, ICUBrk grapheme, |
793 | sk_sp<SkFontMgr> fontmgr) |
794 | : fLineBreakIterator(std::move(line)) |
795 | , fGraphemeBreakIterator(std::move(grapheme)) |
796 | , fFontMgr(std::move(fontmgr)) |
797 | , fBuffer(std::move(buffer)) |
798 | , fUndefinedLanguage(hb_language_from_string("und" , -1)) |
799 | {} |
800 | |
801 | void ShaperHarfBuzz::shape(const char* utf8, size_t utf8Bytes, |
802 | const SkFont& srcFont, |
803 | bool leftToRight, |
804 | SkScalar width, |
805 | RunHandler* handler) const |
806 | { |
807 | UBiDiLevel defaultLevel = leftToRight ? UBIDI_DEFAULT_LTR : UBIDI_DEFAULT_RTL; |
808 | |
809 | std::unique_ptr<BiDiRunIterator> bidi(MakeIcuBiDiRunIterator(utf8, utf8Bytes, defaultLevel)); |
810 | if (!bidi) { |
811 | return; |
812 | } |
813 | |
814 | std::unique_ptr<LanguageRunIterator> language(MakeStdLanguageRunIterator(utf8, utf8Bytes)); |
815 | if (!language) { |
816 | return; |
817 | } |
818 | |
819 | std::unique_ptr<ScriptRunIterator> script(MakeHbIcuScriptRunIterator(utf8, utf8Bytes)); |
820 | if (!script) { |
821 | return; |
822 | } |
823 | |
824 | std::unique_ptr<FontRunIterator> font( |
825 | MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont, |
826 | fFontMgr ? fFontMgr : SkFontMgr::RefDefault())); |
827 | if (!font) { |
828 | return; |
829 | } |
830 | |
831 | this->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, width, handler); |
832 | } |
833 | |
834 | void 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 |
841 | { |
842 | this->shape(utf8, utf8Bytes, font, bidi, script, language, nullptr, 0, width, handler); |
843 | } |
844 | |
845 | void 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 | { |
854 | SkASSERT(handler); |
855 | RunIteratorQueue runSegmenter; |
856 | 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); |
860 | |
861 | this->wrap(utf8, utf8Bytes, bidi, language, script, font, runSegmenter, |
862 | features, featuresSize, width, handler); |
863 | } |
864 | |
865 | void 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, |
871 | const Feature* features, size_t featuresSize, |
872 | SkScalar width, |
873 | RunHandler* handler) const |
874 | { |
875 | ShapedLine line; |
876 | |
877 | const char* utf8Start = nullptr; |
878 | const char* utf8End = utf8; |
879 | while (runSegmenter.advanceRuns()) { // For each item |
880 | utf8Start = utf8End; |
881 | utf8End = utf8 + runSegmenter.endOfCurrentRun(); |
882 | |
883 | ShapedRun model(RunHandler::Range(), SkFont(), 0, nullptr, 0); |
884 | bool modelNeedsRegenerated = true; |
885 | int modelGlyphOffset = 0; |
886 | |
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; |
894 | SkVector modelAdvanceOffset = {0, 0}; |
895 | |
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, |
901 | bidi, language, script, font, |
902 | features, featuresSize); |
903 | modelGlyphOffset = 0; |
904 | |
905 | SkVector advance = {0, 0}; |
906 | modelText = std::make_unique<TextProps[]>(utf8runLength + 1); |
907 | size_t modelStartCluster = utf8Start - utf8; |
908 | for (size_t i = 0; i < model.fNumGlyphs; ++i) { |
909 | SkASSERT(modelStartCluster <= model.fGlyphs[i].fCluster); |
910 | SkASSERT( model.fGlyphs[i].fCluster < (size_t)(utf8End - utf8)); |
911 | if (!model.fGlyphs[i].fUnsafeToBreak) { |
912 | modelText[model.fGlyphs[i].fCluster - modelStartCluster].glyphLen = i; |
913 | modelText[model.fGlyphs[i].fCluster - modelStartCluster].advance = advance; |
914 | } |
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; |
921 | modelAdvanceOffset = {0, 0}; |
922 | 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; |
930 | UText sUtf8UText = UTEXT_INITIALIZER; |
931 | ICUUText utf8UText(utext_openUTF8(&sUtf8UText, utf8Start, utf8runLength, &status)); |
932 | if (U_FAILURE(status)) { |
933 | SkDebugf("Could not create utf8UText: %s" , u_errorName(status)); |
934 | return; |
935 | } |
936 | ubrk_setUText(&breakIterator, utf8UText.get(), &status); |
937 | if (U_FAILURE(status)) { |
938 | SkDebugf("Could not setText on break iterator: %s" , u_errorName(status)); |
939 | return; |
940 | } |
941 | } |
942 | |
943 | ShapedRun best(RunHandler::Range(), SkFont(), 0, nullptr, 0, |
944 | { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity }); |
945 | bool bestIsInvalid = true; |
946 | bool bestUsesModelForGlyphs = false; |
947 | 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 |
956 | bool candidateUsesModelForGlyphs = false; |
957 | ShapedRun candidate = [&](const TextProps& props){ |
958 | if (props.glyphLen) { |
959 | candidateUsesModelForGlyphs = true; |
960 | return ShapedRun(RunHandler::Range(utf8Start - utf8, breakIteratorCurrent), |
961 | font.currentFont(), bidi.currentLevel(), |
962 | std::unique_ptr<ShapedGlyph[]>(), |
963 | props.glyphLen - modelGlyphOffset, |
964 | props.advance - modelAdvanceOffset); |
965 | } else { |
966 | return shape(utf8, utf8Bytes, |
967 | utf8Start, utf8Start + breakIteratorCurrent, |
968 | bidi, language, script, font, |
969 | features, featuresSize); |
970 | } |
971 | }(modelText[breakIteratorCurrent + modelTextOffset]); |
972 | auto score = [widthLeft](const ShapedRun& run) -> SkScalar { |
973 | if (run.fAdvance.fX < widthLeft) { |
974 | return run.fUtf8Range.size(); |
975 | } else { |
976 | return widthLeft - run.fAdvance.fX; |
977 | } |
978 | }; |
979 | if (bestIsInvalid || score(best) < score(candidate)) { |
980 | best = std::move(candidate); |
981 | bestIsInvalid = false; |
982 | bestUsesModelForGlyphs = candidateUsesModelForGlyphs; |
983 | } |
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()) { |
988 | emit(line, handler); |
989 | line.runs.reset(); |
990 | line.fAdvance = {0, 0}; |
991 | } else { |
992 | if (bestUsesModelForGlyphs) { |
993 | best.fGlyphs = std::make_unique<ShapedGlyph[]>(best.fNumGlyphs); |
994 | memcpy(best.fGlyphs.get(), model.fGlyphs.get() + modelGlyphOffset, |
995 | best.fNumGlyphs * sizeof(ShapedGlyph)); |
996 | modelGlyphOffset += best.fNumGlyphs; |
997 | modelTextOffset += best.fUtf8Range.size(); |
998 | modelAdvanceOffset += best.fAdvance; |
999 | } else { |
1000 | modelNeedsRegenerated = true; |
1001 | } |
1002 | utf8Start += best.fUtf8Range.size(); |
1003 | 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) { |
1008 | emit(line, handler); |
1009 | line.runs.reset(); |
1010 | line.fAdvance = {0, 0}; |
1011 | } |
1012 | } |
1013 | } |
1014 | } |
1015 | emit(line, handler); |
1016 | } |
1017 | |
1018 | void 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, |
1024 | const Feature* features, size_t featuresSize, |
1025 | SkScalar width, |
1026 | RunHandler* handler) const |
1027 | { |
1028 | SkTArray<ShapedRun> runs; |
1029 | { |
1030 | UBreakIterator& lineBreakIterator = *fLineBreakIterator; |
1031 | UBreakIterator& graphemeBreakIterator = *fGraphemeBreakIterator; |
1032 | { |
1033 | UErrorCode status = U_ZERO_ERROR; |
1034 | UText sUtf8UText = UTEXT_INITIALIZER; |
1035 | ICUUText utf8UText(utext_openUTF8(&sUtf8UText, utf8, utf8Bytes, &status)); |
1036 | if (U_FAILURE(status)) { |
1037 | SkDebugf("Could not create utf8UText: %s" , u_errorName(status)); |
1038 | return; |
1039 | } |
1040 | |
1041 | ubrk_setUText(&lineBreakIterator, utf8UText.get(), &status); |
1042 | if (U_FAILURE(status)) { |
1043 | SkDebugf("Could not setText on line break iterator: %s" , u_errorName(status)); |
1044 | return; |
1045 | } |
1046 | ubrk_setUText(&graphemeBreakIterator, utf8UText.get(), &status); |
1047 | if (U_FAILURE(status)) { |
1048 | SkDebugf("Could not setText on grapheme break iterator: %s" , u_errorName(status)); |
1049 | return; |
1050 | } |
1051 | } |
1052 | |
1053 | const char* utf8Start = nullptr; |
1054 | const char* utf8End = utf8; |
1055 | while (runSegmenter.advanceRuns()) { |
1056 | utf8Start = utf8End; |
1057 | utf8End = utf8 + runSegmenter.endOfCurrentRun(); |
1058 | |
1059 | runs.emplace_back(shape(utf8, utf8Bytes, |
1060 | utf8Start, utf8End, |
1061 | bidi, language, script, font, |
1062 | features, featuresSize)); |
1063 | ShapedRun& run = runs.back(); |
1064 | |
1065 | uint32_t previousCluster = 0xFFFFFFFF; |
1066 | for (size_t i = 0; i < run.fNumGlyphs; ++i) { |
1067 | ShapedGlyph& glyph = run.fGlyphs[i]; |
1068 | int32_t glyphCluster = glyph.fCluster; |
1069 | |
1070 | int32_t lineBreakIteratorCurrent = ubrk_current(&lineBreakIterator); |
1071 | while (lineBreakIteratorCurrent != UBRK_DONE && |
1072 | lineBreakIteratorCurrent < glyphCluster) |
1073 | { |
1074 | lineBreakIteratorCurrent = ubrk_next(&lineBreakIterator); |
1075 | } |
1076 | glyph.fMayLineBreakBefore = glyph.fCluster != previousCluster && |
1077 | 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 | |
1088 | previousCluster = glyph.fCluster; |
1089 | } |
1090 | } |
1091 | } |
1092 | |
1093 | // Iterate over the glyphs in logical order to find potential line lengths. |
1094 | { |
1095 | /** 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 | } |
1123 | } |
1124 | |
1125 | 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; |
1160 | } |
1161 | } |
1162 | } |
1163 | |
1164 | // Reorder the runs and glyphs per line and write them out. |
1165 | { |
1166 | ShapedRunGlyphIterator previousBreak(runs); |
1167 | ShapedRunGlyphIterator glyphIterator(runs); |
1168 | int previousRunIndex = -1; |
1169 | while (glyphIterator.current()) { |
1170 | const ShapedRunGlyphIterator current = glyphIterator; |
1171 | ShapedGlyph* nextGlyph = glyphIterator.next(); |
1172 | |
1173 | if (previousRunIndex != current.fRunIndex) { |
1174 | SkFontMetrics metrics; |
1175 | runs[current.fRunIndex].fFont.getMetrics(&metrics); |
1176 | previousRunIndex = current.fRunIndex; |
1177 | } |
1178 | |
1179 | // Nothing can be written until the baseline is known. |
1180 | if (!(nextGlyph == nullptr || nextGlyph->fMustLineBreakBefore)) { |
1181 | continue; |
1182 | } |
1183 | |
1184 | int numRuns = current.fRunIndex - previousBreak.fRunIndex + 1; |
1185 | 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 | |
1192 | // 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 | |
1195 | handler->beginLine(); |
1196 | |
1197 | struct SubRun { const ShapedRun& run; size_t startGlyphIndex; size_t endGlyphIndex; }; |
1198 | auto makeSubRun = [&runs, &previousBreak, ¤t, &logicalFromVisual](size_t visualIndex){ |
1199 | int logicalIndex = previousBreak.fRunIndex + logicalFromVisual[visualIndex]; |
1200 | const auto& run = runs[logicalIndex]; |
1201 | size_t startGlyphIndex = (logicalIndex == previousBreak.fRunIndex) |
1202 | ? previousBreak.fGlyphIndex |
1203 | : 0; |
1204 | size_t endGlyphIndex = (logicalIndex == current.fRunIndex) |
1205 | ? current.fGlyphIndex + 1 |
1206 | : run.fNumGlyphs; |
1207 | 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(); |
1214 | |
1215 | 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); |
1236 | } |
1237 | |
1238 | handler->commitLine(); |
1239 | |
1240 | previousRunIndex = -1; |
1241 | previousBreak = glyphIterator; |
1242 | } |
1243 | } |
1244 | } |
1245 | |
1246 | void 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, |
1252 | const Feature* features, size_t featuresSize, |
1253 | 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, |
1267 | bidi, language, script, font, |
1268 | features, featuresSize)); |
1269 | } |
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 | } |
1295 | |
1296 | ShapedRun ShaperHarfBuzz::shape(char const * const utf8, |
1297 | size_t const utf8Bytes, |
1298 | char const * const utf8Start, |
1299 | char const * const utf8End, |
1300 | const BiDiRunIterator& bidi, |
1301 | const LanguageRunIterator& language, |
1302 | const ScriptRunIterator& script, |
1303 | const FontRunIterator& font, |
1304 | Feature const * const features, size_t const featuresSize) const |
1305 | { |
1306 | size_t utf8runLength = utf8End - utf8Start; |
1307 | ShapedRun run(RunHandler::Range(utf8Start - utf8, utf8runLength), |
1308 | font.currentFont(), bidi.currentLevel(), nullptr, 0); |
1309 | |
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 | |
1315 | // 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); |
1319 | |
1320 | // 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) { |
1326 | unsigned int cluster = utf8Current - utf8; |
1327 | 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 | |
1334 | hb_direction_t direction = is_LTR(bidi.currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL; |
1335 | hb_buffer_set_direction(buffer, direction); |
1336 | hb_buffer_set_script(buffer, hb_script_from_iso15924_tag((hb_tag_t)script.currentScript())); |
1337 | // 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); |
1345 | hb_buffer_guess_segment_properties(buffer); |
1346 | |
1347 | // 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 | } |
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 | int scaleX, scaleY; |
1405 | hb_font_get_scale(hbFont.get(), &scaleX, &scaleY); |
1406 | 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; |
1414 | glyph.fOffset.fY = -(pos[i].y_offset * textSizeY); // HarfBuzz y-up, Skia y-down |
1415 | glyph.fAdvance.fX = pos[i].x_advance * textSizeX; |
1416 | glyph.fAdvance.fY = -(pos[i].y_advance * textSizeY); // HarfBuzz y-up, Skia y-down |
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> |
1440 | SkShaper::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 | |
1476 | return std::make_unique<IcuBiDiRunIterator>(utf8, utf8 + utf8Bytes, std::move(bidi)); |
1477 | } |
1478 | |
1479 | std::unique_ptr<SkShaper::ScriptRunIterator> |
1480 | SkShaper::MakeHbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes) { |
1481 | return std::make_unique<HbIcuScriptRunIterator>(utf8, utf8Bytes); |
1482 | } |
1483 | |
1484 | std::unique_ptr<SkShaper> SkShaper::MakeShaperDrivenWrapper(sk_sp<SkFontMgr> fontmgr) { |
1485 | return MakeHarfBuzz(std::move(fontmgr), true); |
1486 | } |
1487 | std::unique_ptr<SkShaper> SkShaper::MakeShapeThenWrap(sk_sp<SkFontMgr> fontmgr) { |
1488 | return MakeHarfBuzz(std::move(fontmgr), false); |
1489 | } |
1490 | std::unique_ptr<SkShaper> SkShaper::MakeShapeDontWrapOrReorder(sk_sp<SkFontMgr> fontmgr) { |
1491 | #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 | |
1503 | return std::make_unique<ShapeDontWrapOrReorder>(std::move(buffer), nullptr, nullptr, |
1504 | std::move(fontmgr)); |
1505 | } |
1506 | |