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