Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 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 | |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 8 | #include "SkFont.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 9 | #include "SkFontArguments.h" |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 10 | #include "SkFontMetrics.h" |
Ben Wagner | 67e3a30 | 2017-09-05 14:46:19 -0400 | [diff] [blame] | 11 | #include "SkFontMgr.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 12 | #include "SkMalloc.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 13 | #include "SkPoint.h" |
| 14 | #include "SkRefCnt.h" |
| 15 | #include "SkScalar.h" |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 16 | #include "SkShaper.h" |
| 17 | #include "SkStream.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 18 | #include "SkString.h" |
| 19 | #include "SkTArray.h" |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 20 | #include "SkTDPQueue.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 21 | #include "SkTFitsIn.h" |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 22 | #include "SkTLazy.h" |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 23 | #include "SkTemplates.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 24 | #include "SkTo.h" |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 25 | #include "SkTypeface.h" |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 26 | #include "SkTypes.h" |
| 27 | #include "SkUTF.h" |
| 28 | |
| 29 | #include <hb.h> |
| 30 | #include <hb-ot.h> |
Ben Wagner | c5aa8eb | 2019-01-24 16:15:55 -0500 | [diff] [blame] | 31 | #include <unicode/ubrk.h> |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 32 | #include <unicode/ubidi.h> |
Ben Wagner | c5aa8eb | 2019-01-24 16:15:55 -0500 | [diff] [blame] | 33 | #include <unicode/ustring.h> |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 34 | #include <unicode/urename.h> |
| 35 | #include <unicode/utext.h> |
| 36 | #include <unicode/utypes.h> |
| 37 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 38 | #include <cstring> |
| 39 | #include <locale> |
Ben Wagner | 1777424 | 2018-08-07 14:31:33 -0400 | [diff] [blame] | 40 | #include <memory> |
| 41 | #include <utility> |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 42 | |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 43 | #if defined(SK_USING_THIRD_PARTY_ICU) |
Hal Canary | 32498f0 | 2019-02-04 15:36:31 -0500 | [diff] [blame] | 44 | #include "SkLoadICU.h" |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 45 | #endif |
Hal Canary | 32498f0 | 2019-02-04 15:36:31 -0500 | [diff] [blame] | 46 | |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 47 | namespace { |
| 48 | template <class T, void(*P)(T*)> using resource = std::unique_ptr<T, SkFunctionWrapper<void, T, P>>; |
Ben Wagner | c5aa8eb | 2019-01-24 16:15:55 -0500 | [diff] [blame] | 49 | using HBBlob = resource<hb_blob_t , hb_blob_destroy >; |
| 50 | using HBFace = resource<hb_face_t , hb_face_destroy >; |
| 51 | using HBFont = resource<hb_font_t , hb_font_destroy >; |
| 52 | using HBBuffer = resource<hb_buffer_t , hb_buffer_destroy>; |
| 53 | using ICUBiDi = resource<UBiDi , ubidi_close >; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 54 | using ICUBrk = resource<UBreakIterator, ubrk_close >; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 55 | |
| 56 | HBBlob stream_to_blob(std::unique_ptr<SkStreamAsset> asset) { |
| 57 | size_t size = asset->getLength(); |
| 58 | HBBlob blob; |
| 59 | if (const void* base = asset->getMemoryBase()) { |
| 60 | blob.reset(hb_blob_create((char*)base, SkToUInt(size), |
| 61 | HB_MEMORY_MODE_READONLY, asset.release(), |
| 62 | [](void* p) { delete (SkStreamAsset*)p; })); |
| 63 | } else { |
| 64 | // SkDebugf("Extra SkStreamAsset copy\n"); |
| 65 | void* ptr = size ? sk_malloc_throw(size) : nullptr; |
| 66 | asset->read(ptr, size); |
| 67 | blob.reset(hb_blob_create((char*)ptr, SkToUInt(size), |
| 68 | HB_MEMORY_MODE_READONLY, ptr, sk_free)); |
| 69 | } |
| 70 | SkASSERT(blob); |
| 71 | hb_blob_make_immutable(blob.get()); |
| 72 | return blob; |
| 73 | } |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 74 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 75 | HBFont create_hb_font(SkTypeface* tf) { |
Hal Canary | 0dfa208 | 2018-10-31 13:02:49 -0400 | [diff] [blame] | 76 | if (!tf) { |
| 77 | return nullptr; |
| 78 | } |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 79 | int index; |
Hal Canary | ddef43f | 2018-11-16 10:53:51 -0500 | [diff] [blame] | 80 | std::unique_ptr<SkStreamAsset> typefaceAsset(tf->openStream(&index)); |
| 81 | if (!typefaceAsset) { |
| 82 | SkString name; |
| 83 | tf->getFamilyName(&name); |
| 84 | SkDebugf("Typeface '%s' has no data :(\n", name.c_str()); |
| 85 | return nullptr; |
| 86 | } |
| 87 | HBBlob blob(stream_to_blob(std::move(typefaceAsset))); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 88 | HBFace face(hb_face_create(blob.get(), (unsigned)index)); |
| 89 | SkASSERT(face); |
| 90 | if (!face) { |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 91 | return nullptr; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 92 | } |
| 93 | hb_face_set_index(face.get(), (unsigned)index); |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 94 | hb_face_set_upem(face.get(), tf->getUnitsPerEm()); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 95 | |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 96 | HBFont font(hb_font_create(face.get())); |
| 97 | SkASSERT(font); |
| 98 | if (!font) { |
| 99 | return nullptr; |
| 100 | } |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 101 | hb_ot_font_set_funcs(font.get()); |
| 102 | int axis_count = tf->getVariationDesignPosition(nullptr, 0); |
| 103 | if (axis_count > 0) { |
| 104 | SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate> axis_values(axis_count); |
| 105 | if (tf->getVariationDesignPosition(axis_values, axis_count) == axis_count) { |
| 106 | hb_font_set_variations(font.get(), |
| 107 | reinterpret_cast<hb_variation_t*>(axis_values.get()), |
| 108 | axis_count); |
| 109 | } |
| 110 | } |
| 111 | return font; |
| 112 | } |
| 113 | |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 114 | /** this version replaces invalid utf-8 sequences with code point U+FFFD. */ |
| 115 | static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 116 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 117 | if (val < 0) { |
| 118 | return 0xFFFD; // REPLACEMENT CHARACTER |
| 119 | } |
| 120 | return val; |
| 121 | } |
| 122 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 123 | class RunIterator { |
| 124 | public: |
| 125 | virtual ~RunIterator() {} |
| 126 | virtual void consume() = 0; |
| 127 | // Pointer one past the last (utf8) element in the current run. |
| 128 | virtual const char* endOfCurrentRun() const = 0; |
| 129 | virtual bool atEnd() const = 0; |
| 130 | bool operator<(const RunIterator& that) const { |
| 131 | return this->endOfCurrentRun() < that.endOfCurrentRun(); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | class BiDiRunIterator : public RunIterator { |
| 136 | public: |
| 137 | static SkTLazy<BiDiRunIterator> Make(const char* utf8, size_t utf8Bytes, UBiDiLevel level) { |
| 138 | SkTLazy<BiDiRunIterator> ret; |
| 139 | |
| 140 | // ubidi only accepts utf16 (though internally it basically works on utf32 chars). |
| 141 | // We want an ubidi_setPara(UBiDi*, UText*, UBiDiLevel, UBiDiLevel*, UErrorCode*); |
| 142 | if (!SkTFitsIn<int32_t>(utf8Bytes)) { |
| 143 | SkDebugf("Bidi error: text too long"); |
| 144 | return ret; |
| 145 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 146 | |
| 147 | UErrorCode status = U_ZERO_ERROR; |
Ben Wagner | c5aa8eb | 2019-01-24 16:15:55 -0500 | [diff] [blame] | 148 | |
| 149 | // Getting the length like this seems to always set U_BUFFER_OVERFLOW_ERROR |
| 150 | int32_t utf16Units; |
| 151 | u_strFromUTF8(nullptr, 0, &utf16Units, utf8, utf8Bytes, &status); |
| 152 | status = U_ZERO_ERROR; |
| 153 | std::unique_ptr<UChar[]> utf16(new UChar[utf16Units]); |
| 154 | u_strFromUTF8(utf16.get(), utf16Units, nullptr, utf8, utf8Bytes, &status); |
| 155 | if (U_FAILURE(status)) { |
| 156 | SkDebugf("Invalid utf8 input: %s", u_errorName(status)); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | ICUBiDi bidi(ubidi_openSized(utf16Units, 0, &status)); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 161 | if (U_FAILURE(status)) { |
| 162 | SkDebugf("Bidi error: %s", u_errorName(status)); |
| 163 | return ret; |
| 164 | } |
| 165 | SkASSERT(bidi); |
| 166 | |
| 167 | // The required lifetime of utf16 isn't well documented. |
| 168 | // It appears it isn't used after ubidi_setPara except through ubidi_getText. |
Ben Wagner | c5aa8eb | 2019-01-24 16:15:55 -0500 | [diff] [blame] | 169 | ubidi_setPara(bidi.get(), utf16.get(), utf16Units, level, nullptr, &status); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 170 | if (U_FAILURE(status)) { |
| 171 | SkDebugf("Bidi error: %s", u_errorName(status)); |
| 172 | return ret; |
| 173 | } |
| 174 | |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 175 | ret.init(utf8, utf8 + utf8Bytes, std::move(bidi)); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 176 | return ret; |
| 177 | } |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 178 | BiDiRunIterator(const char* utf8, const char* end, ICUBiDi bidi) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 179 | : fBidi(std::move(bidi)) |
| 180 | , fEndOfCurrentRun(utf8) |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 181 | , fEndOfAllRuns(end) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 182 | , fUTF16LogicalPosition(0) |
| 183 | , fLevel(UBIDI_DEFAULT_LTR) |
| 184 | {} |
| 185 | void consume() override { |
| 186 | SkASSERT(fUTF16LogicalPosition < ubidi_getLength(fBidi.get())); |
| 187 | int32_t endPosition = ubidi_getLength(fBidi.get()); |
| 188 | fLevel = ubidi_getLevelAt(fBidi.get(), fUTF16LogicalPosition); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 189 | SkUnichar u = utf8_next(&fEndOfCurrentRun, fEndOfAllRuns); |
| 190 | fUTF16LogicalPosition += SkUTF::ToUTF16(u); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 191 | UBiDiLevel level; |
| 192 | while (fUTF16LogicalPosition < endPosition) { |
| 193 | level = ubidi_getLevelAt(fBidi.get(), fUTF16LogicalPosition); |
| 194 | if (level != fLevel) { |
| 195 | break; |
| 196 | } |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 197 | u = utf8_next(&fEndOfCurrentRun, fEndOfAllRuns); |
| 198 | fUTF16LogicalPosition += SkUTF::ToUTF16(u); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | const char* endOfCurrentRun() const override { |
| 202 | return fEndOfCurrentRun; |
| 203 | } |
| 204 | bool atEnd() const override { |
| 205 | return fUTF16LogicalPosition == ubidi_getLength(fBidi.get()); |
| 206 | } |
| 207 | |
| 208 | UBiDiLevel currentLevel() const { |
| 209 | return fLevel; |
| 210 | } |
| 211 | private: |
| 212 | ICUBiDi fBidi; |
| 213 | const char* fEndOfCurrentRun; |
Hal Canary | 4014ba6 | 2018-07-24 11:33:21 -0400 | [diff] [blame] | 214 | const char* fEndOfAllRuns; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 215 | int32_t fUTF16LogicalPosition; |
| 216 | UBiDiLevel fLevel; |
| 217 | }; |
| 218 | |
| 219 | class ScriptRunIterator : public RunIterator { |
| 220 | public: |
| 221 | static SkTLazy<ScriptRunIterator> Make(const char* utf8, size_t utf8Bytes, |
| 222 | hb_unicode_funcs_t* hbUnicode) |
| 223 | { |
| 224 | SkTLazy<ScriptRunIterator> ret; |
| 225 | ret.init(utf8, utf8Bytes, hbUnicode); |
| 226 | return ret; |
| 227 | } |
| 228 | ScriptRunIterator(const char* utf8, size_t utf8Bytes, hb_unicode_funcs_t* hbUnicode) |
| 229 | : fCurrent(utf8), fEnd(fCurrent + utf8Bytes) |
| 230 | , fHBUnicode(hbUnicode) |
| 231 | , fCurrentScript(HB_SCRIPT_UNKNOWN) |
| 232 | {} |
| 233 | void consume() override { |
| 234 | SkASSERT(fCurrent < fEnd); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 235 | SkUnichar u = utf8_next(&fCurrent, fEnd); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 236 | fCurrentScript = hb_unicode_script(fHBUnicode, u); |
| 237 | while (fCurrent < fEnd) { |
| 238 | const char* prev = fCurrent; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 239 | u = utf8_next(&fCurrent, fEnd); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 240 | const hb_script_t script = hb_unicode_script(fHBUnicode, u); |
| 241 | if (script != fCurrentScript) { |
| 242 | if (fCurrentScript == HB_SCRIPT_INHERITED || fCurrentScript == HB_SCRIPT_COMMON) { |
| 243 | fCurrentScript = script; |
| 244 | } else if (script == HB_SCRIPT_INHERITED || script == HB_SCRIPT_COMMON) { |
| 245 | continue; |
| 246 | } else { |
| 247 | fCurrent = prev; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | if (fCurrentScript == HB_SCRIPT_INHERITED) { |
| 253 | fCurrentScript = HB_SCRIPT_COMMON; |
| 254 | } |
| 255 | } |
| 256 | const char* endOfCurrentRun() const override { |
| 257 | return fCurrent; |
| 258 | } |
| 259 | bool atEnd() const override { |
| 260 | return fCurrent == fEnd; |
| 261 | } |
| 262 | |
| 263 | hb_script_t currentScript() const { |
| 264 | return fCurrentScript; |
| 265 | } |
| 266 | private: |
| 267 | const char* fCurrent; |
| 268 | const char* fEnd; |
| 269 | hb_unicode_funcs_t* fHBUnicode; |
| 270 | hb_script_t fCurrentScript; |
| 271 | }; |
| 272 | |
| 273 | class FontRunIterator : public RunIterator { |
| 274 | public: |
| 275 | static SkTLazy<FontRunIterator> Make(const char* utf8, size_t utf8Bytes, |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 276 | SkFont font, |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 277 | sk_sp<SkFontMgr> fallbackMgr) |
| 278 | { |
| 279 | SkTLazy<FontRunIterator> ret; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 280 | font.setTypeface(font.refTypefaceOrDefault()); |
| 281 | HBFont hbFont = create_hb_font(font.getTypeface()); |
| 282 | if (!hbFont) { |
| 283 | SkDebugf("create_hb_font failed!\n"); |
| 284 | return ret; |
| 285 | } |
| 286 | ret.init(utf8, utf8Bytes, std::move(font), std::move(hbFont), std::move(fallbackMgr)); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 287 | return ret; |
| 288 | } |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 289 | FontRunIterator(const char* utf8, size_t utf8Bytes, SkFont font, |
| 290 | HBFont hbFont, sk_sp<SkFontMgr> fallbackMgr) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 291 | : fCurrent(utf8), fEnd(fCurrent + utf8Bytes) |
| 292 | , fFallbackMgr(std::move(fallbackMgr)) |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 293 | , fHBFont(std::move(hbFont)), fFont(std::move(font)) |
| 294 | , fFallbackHBFont(nullptr), fFallbackFont(fFont) |
| 295 | , fCurrentHBFont(fHBFont.get()), fCurrentFont(&fFont) |
| 296 | { |
| 297 | fFallbackFont.setTypeface(nullptr); |
| 298 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 299 | void consume() override { |
| 300 | SkASSERT(fCurrent < fEnd); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 301 | SkUnichar u = utf8_next(&fCurrent, fEnd); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 302 | // If the starting typeface can handle this character, use it. |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 303 | if (fFont.getTypeface()->charsToGlyphs(&u, SkTypeface::kUTF32_Encoding, nullptr, 1)) { |
| 304 | fCurrentFont = &fFont; |
| 305 | fCurrentHBFont = fHBFont.get(); |
Ben Wagner | a900ad5 | 2018-08-31 17:48:19 -0400 | [diff] [blame] | 306 | // If the current fallback can handle this character, use it. |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 307 | } else if (fFallbackFont.getTypeface() && |
| 308 | fFallbackFont.getTypeface()->charsToGlyphs(&u, SkTypeface::kUTF32_Encoding, nullptr, 1)) |
Ben Wagner | a900ad5 | 2018-08-31 17:48:19 -0400 | [diff] [blame] | 309 | { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 310 | fCurrentFont = &fFallbackFont; |
Ben Wagner | a900ad5 | 2018-08-31 17:48:19 -0400 | [diff] [blame] | 311 | fCurrentHBFont = fFallbackHBFont.get(); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 312 | // If not, try to find a fallback typeface |
| 313 | } else { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 314 | fFallbackFont.setTypeface(sk_ref_sp(fFallbackMgr->matchFamilyStyleCharacter( |
| 315 | nullptr, fFont.getTypeface()->fontStyle(), nullptr, 0, u))); |
| 316 | fFallbackHBFont = create_hb_font(fFallbackFont.getTypeface()); |
| 317 | fCurrentFont = &fFallbackFont; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 318 | fCurrentHBFont = fFallbackHBFont.get(); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | while (fCurrent < fEnd) { |
| 322 | const char* prev = fCurrent; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 323 | u = utf8_next(&fCurrent, fEnd); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 324 | |
Ben Wagner | a900ad5 | 2018-08-31 17:48:19 -0400 | [diff] [blame] | 325 | // If not using initial typeface and initial typeface has this character, stop fallback. |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 326 | if (fCurrentFont->getTypeface() != fFont.getTypeface() && |
| 327 | fFont.getTypeface()->charsToGlyphs(&u, SkTypeface::kUTF32_Encoding, nullptr, 1)) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 328 | { |
| 329 | fCurrent = prev; |
| 330 | return; |
| 331 | } |
| 332 | // If the current typeface cannot handle this character, stop using it. |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 333 | if (!fCurrentFont->getTypeface()->charsToGlyphs(&u, SkTypeface::kUTF32_Encoding, nullptr, 1)) { |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 334 | fCurrent = prev; |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | const char* endOfCurrentRun() const override { |
| 340 | return fCurrent; |
| 341 | } |
| 342 | bool atEnd() const override { |
| 343 | return fCurrent == fEnd; |
| 344 | } |
| 345 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 346 | SkFont* currentFont() const { |
| 347 | return fCurrentFont; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 348 | } |
| 349 | hb_font_t* currentHBFont() const { |
| 350 | return fCurrentHBFont; |
| 351 | } |
| 352 | private: |
| 353 | const char* fCurrent; |
| 354 | const char* fEnd; |
| 355 | sk_sp<SkFontMgr> fFallbackMgr; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 356 | HBFont fHBFont; |
| 357 | SkFont fFont; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 358 | HBFont fFallbackHBFont; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 359 | SkFont fFallbackFont; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 360 | hb_font_t* fCurrentHBFont; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 361 | SkFont* fCurrentFont; |
| 362 | }; |
| 363 | |
| 364 | class LanguageRunIterator : public RunIterator { |
| 365 | public: |
| 366 | static SkTLazy<LanguageRunIterator> Make(const char* utf8, size_t utf8Bytes) { |
| 367 | SkTLazy<LanguageRunIterator> ret; |
| 368 | ret.init(utf8, utf8Bytes); |
| 369 | return ret; |
| 370 | } |
| 371 | LanguageRunIterator(const char* utf8, size_t utf8Bytes) |
| 372 | : fCurrent(utf8), fEnd(fCurrent + utf8Bytes) |
| 373 | , fLanguage(hb_language_from_string(std::locale().name().c_str(), -1)) |
| 374 | { } |
| 375 | void consume() override { |
| 376 | // Ideally something like cld2/3 could be used, or user signals. |
| 377 | SkASSERT(fCurrent < fEnd); |
| 378 | fCurrent = fEnd; |
| 379 | } |
| 380 | const char* endOfCurrentRun() const override { |
| 381 | return fCurrent; |
| 382 | } |
| 383 | bool atEnd() const override { |
| 384 | return fCurrent == fEnd; |
| 385 | } |
| 386 | |
| 387 | hb_language_t currentLanguage() const { |
| 388 | return fLanguage; |
| 389 | } |
| 390 | private: |
| 391 | const char* fCurrent; |
| 392 | const char* fEnd; |
| 393 | hb_language_t fLanguage; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | class RunIteratorQueue { |
| 397 | public: |
| 398 | void insert(RunIterator* runIterator) { |
| 399 | fRunIterators.insert(runIterator); |
| 400 | } |
| 401 | |
| 402 | bool advanceRuns() { |
| 403 | const RunIterator* leastRun = fRunIterators.peek(); |
| 404 | if (leastRun->atEnd()) { |
| 405 | SkASSERT(this->allRunsAreAtEnd()); |
| 406 | return false; |
| 407 | } |
| 408 | const char* leastEnd = leastRun->endOfCurrentRun(); |
| 409 | RunIterator* currentRun = nullptr; |
| 410 | SkDEBUGCODE(const char* previousEndOfCurrentRun); |
| 411 | while ((currentRun = fRunIterators.peek())->endOfCurrentRun() <= leastEnd) { |
| 412 | fRunIterators.pop(); |
| 413 | SkDEBUGCODE(previousEndOfCurrentRun = currentRun->endOfCurrentRun()); |
| 414 | currentRun->consume(); |
| 415 | SkASSERT(previousEndOfCurrentRun < currentRun->endOfCurrentRun()); |
| 416 | fRunIterators.insert(currentRun); |
| 417 | } |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | const char* endOfCurrentRun() const { |
| 422 | return fRunIterators.peek()->endOfCurrentRun(); |
| 423 | } |
| 424 | |
| 425 | private: |
| 426 | bool allRunsAreAtEnd() const { |
| 427 | for (int i = 0; i < fRunIterators.count(); ++i) { |
| 428 | if (!fRunIterators.at(i)->atEnd()) { |
| 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | static bool CompareRunIterator(RunIterator* const& a, RunIterator* const& b) { |
| 436 | return *a < *b; |
| 437 | } |
| 438 | SkTDPQueue<RunIterator*, CompareRunIterator> fRunIterators; |
| 439 | }; |
| 440 | |
| 441 | struct ShapedGlyph { |
| 442 | SkGlyphID fID; |
| 443 | uint32_t fCluster; |
| 444 | SkPoint fOffset; |
| 445 | SkVector fAdvance; |
| 446 | bool fMayLineBreakBefore; |
| 447 | bool fMustLineBreakBefore; |
| 448 | bool fHasVisual; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 449 | bool fGraphemeBreakBefore; |
| 450 | bool fUnsafeToBreak; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 451 | }; |
| 452 | struct ShapedRun { |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 453 | ShapedRun(const char* utf8Start, const char* utf8End, int numGlyphs, const SkFont& font, |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 454 | UBiDiLevel level, std::unique_ptr<ShapedGlyph[]> glyphs) |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 455 | : fUtf8Start(utf8Start), fUtf8End(utf8End), fNumGlyphs(numGlyphs), fFont(font) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 456 | , fLevel(level), fGlyphs(std::move(glyphs)) |
| 457 | {} |
| 458 | |
| 459 | const char* fUtf8Start; |
| 460 | const char* fUtf8End; |
| 461 | int fNumGlyphs; |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 462 | SkFont fFont; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 463 | UBiDiLevel fLevel; |
| 464 | std::unique_ptr<ShapedGlyph[]> fGlyphs; |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 465 | SkVector fAdvance = { 0, 0 }; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 466 | }; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 467 | struct ShapedLine { |
| 468 | SkTArray<ShapedRun> runs; |
| 469 | SkVector fAdvance = { 0, 0 }; |
| 470 | }; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 471 | |
| 472 | static constexpr bool is_LTR(UBiDiLevel level) { |
| 473 | return (level & 1) == 0; |
| 474 | } |
| 475 | |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 476 | static void append(SkShaper::RunHandler* handler, const SkShaper::RunHandler::RunInfo& runInfo, |
| 477 | const ShapedRun& run, int start, int end, |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 478 | SkPoint* p) { |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 479 | unsigned len = end - start; |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 480 | |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 481 | const auto buffer = handler->newRunBuffer(runInfo, run.fFont, len, run.fUtf8End - run.fUtf8Start); |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 482 | SkASSERT(buffer.glyphs); |
| 483 | SkASSERT(buffer.positions); |
| 484 | |
| 485 | if (buffer.utf8text) { |
| 486 | memcpy(buffer.utf8text, run.fUtf8Start, run.fUtf8End - run.fUtf8Start); |
| 487 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 488 | |
| 489 | for (unsigned i = 0; i < len; i++) { |
| 490 | // Glyphs are in logical order, but output ltr since PDF readers seem to expect that. |
| 491 | const ShapedGlyph& glyph = run.fGlyphs[is_LTR(run.fLevel) ? start + i : end - 1 - i]; |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 492 | buffer.glyphs[i] = glyph.fID; |
| 493 | buffer.positions[i] = SkPoint::Make(p->fX + glyph.fOffset.fX, p->fY - glyph.fOffset.fY); |
| 494 | if (buffer.clusters) { |
| 495 | buffer.clusters[i] = glyph.fCluster; |
| 496 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 497 | p->fX += glyph.fAdvance.fX; |
| 498 | p->fY += glyph.fAdvance.fY; |
| 499 | } |
| 500 | } |
| 501 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 502 | static void emit(const ShapedLine& line, SkShaper::RunHandler* handler, |
| 503 | SkPoint point, SkPoint& currentPoint, size_t& lineIndex) |
| 504 | { |
| 505 | // Reorder the runs and glyphs per line and write them out. |
| 506 | SkScalar maxAscent = 0; |
| 507 | SkScalar maxDescent = 0; |
| 508 | SkScalar maxLeading = 0; |
| 509 | for (const ShapedRun& run : line.runs) { |
| 510 | SkFontMetrics metrics; |
| 511 | run.fFont.getMetrics(&metrics); |
| 512 | maxAscent = SkTMin(maxAscent, metrics.fAscent); |
| 513 | maxDescent = SkTMax(maxDescent, metrics.fDescent); |
| 514 | maxLeading = SkTMax(maxLeading, metrics.fLeading); |
| 515 | } |
| 516 | |
| 517 | int numRuns = line.runs.size(); |
| 518 | SkAutoSTMalloc<4, UBiDiLevel> runLevels(numRuns); |
| 519 | for (int i = 0; i < numRuns; ++i) { |
| 520 | runLevels[i] = line.runs[i].fLevel; |
| 521 | } |
| 522 | SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns); |
| 523 | ubidi_reorderVisual(runLevels, numRuns, logicalFromVisual); |
| 524 | |
| 525 | currentPoint.fY -= maxAscent; |
| 526 | |
| 527 | for (int i = 0; i < numRuns; ++i) { |
| 528 | int logicalIndex = logicalFromVisual[i]; |
| 529 | |
| 530 | const auto& run = line.runs[logicalIndex]; |
| 531 | const SkShaper::RunHandler::RunInfo info = { |
| 532 | lineIndex, |
| 533 | run.fAdvance, |
| 534 | maxAscent, |
| 535 | maxDescent, |
| 536 | maxLeading, |
| 537 | }; |
| 538 | append(handler, info, run, 0, run.fNumGlyphs, ¤tPoint); |
| 539 | } |
| 540 | |
| 541 | currentPoint.fY += maxDescent + maxLeading; |
| 542 | currentPoint.fX = point.fX; |
| 543 | |
| 544 | lineIndex++; |
| 545 | } |
| 546 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 547 | struct ShapedRunGlyphIterator { |
| 548 | ShapedRunGlyphIterator(const SkTArray<ShapedRun>& origRuns) |
| 549 | : fRuns(&origRuns), fRunIndex(0), fGlyphIndex(0) |
| 550 | { } |
| 551 | |
| 552 | ShapedRunGlyphIterator(const ShapedRunGlyphIterator& that) = default; |
| 553 | ShapedRunGlyphIterator& operator=(const ShapedRunGlyphIterator& that) = default; |
| 554 | bool operator==(const ShapedRunGlyphIterator& that) const { |
| 555 | return fRuns == that.fRuns && |
| 556 | fRunIndex == that.fRunIndex && |
| 557 | fGlyphIndex == that.fGlyphIndex; |
| 558 | } |
| 559 | bool operator!=(const ShapedRunGlyphIterator& that) const { |
| 560 | return fRuns != that.fRuns || |
| 561 | fRunIndex != that.fRunIndex || |
| 562 | fGlyphIndex != that.fGlyphIndex; |
| 563 | } |
| 564 | |
| 565 | ShapedGlyph* next() { |
| 566 | const SkTArray<ShapedRun>& runs = *fRuns; |
| 567 | SkASSERT(fRunIndex < runs.count()); |
| 568 | SkASSERT(fGlyphIndex < runs[fRunIndex].fNumGlyphs); |
| 569 | |
| 570 | ++fGlyphIndex; |
| 571 | if (fGlyphIndex == runs[fRunIndex].fNumGlyphs) { |
| 572 | fGlyphIndex = 0; |
| 573 | ++fRunIndex; |
| 574 | if (fRunIndex >= runs.count()) { |
| 575 | return nullptr; |
| 576 | } |
| 577 | } |
| 578 | return &runs[fRunIndex].fGlyphs[fGlyphIndex]; |
| 579 | } |
| 580 | |
| 581 | ShapedGlyph* current() { |
| 582 | const SkTArray<ShapedRun>& runs = *fRuns; |
| 583 | if (fRunIndex >= runs.count()) { |
| 584 | return nullptr; |
| 585 | } |
| 586 | return &runs[fRunIndex].fGlyphs[fGlyphIndex]; |
| 587 | } |
| 588 | |
| 589 | const SkTArray<ShapedRun>* fRuns; |
| 590 | int fRunIndex; |
| 591 | int fGlyphIndex; |
| 592 | }; |
| 593 | |
| 594 | } // namespace |
| 595 | |
| 596 | struct SkShaper::Impl { |
| 597 | HBFont fHarfBuzzFont; |
| 598 | HBBuffer fBuffer; |
| 599 | sk_sp<SkTypeface> fTypeface; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 600 | ICUBrk fLineBreakIterator; |
| 601 | ICUBrk fGraphemeBreakIterator; |
| 602 | |
| 603 | SkPoint shapeCorrect(RunHandler* handler, |
| 604 | const char* utf8, |
| 605 | size_t utf8Bytes, |
| 606 | SkPoint point, |
| 607 | SkScalar width, |
| 608 | RunIteratorQueue& runSegmenter, |
| 609 | const BiDiRunIterator* bidi, |
| 610 | const LanguageRunIterator* language, |
| 611 | const ScriptRunIterator* script, |
| 612 | const FontRunIterator* font) const; |
| 613 | |
| 614 | SkPoint shapeOk(RunHandler* handler, |
| 615 | const char* utf8, |
| 616 | size_t utf8Bytes, |
| 617 | SkPoint point, |
| 618 | SkScalar width, |
| 619 | RunIteratorQueue& runSegmenter, |
| 620 | const BiDiRunIterator* bidi, |
| 621 | const LanguageRunIterator* language, |
| 622 | const ScriptRunIterator* script, |
| 623 | const FontRunIterator* font) const; |
| 624 | |
| 625 | ShapedRun shape(const char* utf8, |
| 626 | size_t utf8Bytes, |
| 627 | const char* utf8Start, |
| 628 | const char* utf8End, |
| 629 | const BiDiRunIterator* bidi, |
| 630 | const LanguageRunIterator* language, |
| 631 | const ScriptRunIterator* script, |
| 632 | const FontRunIterator* font) const; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 633 | }; |
| 634 | |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 635 | SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { |
Hal Canary | 6102192 | 2019-02-06 12:29:11 -0500 | [diff] [blame] | 636 | #if defined(SK_USING_THIRD_PARTY_ICU) |
| 637 | if (!SkLoadICU()) { |
| 638 | SkDebugf("SkLoadICU() failed!\n"); |
| 639 | return; |
| 640 | } |
| 641 | #endif |
Ben Wagner | e000173 | 2017-08-31 16:26:26 -0400 | [diff] [blame] | 642 | fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); |
| 643 | fImpl->fHarfBuzzFont = create_hb_font(fImpl->fTypeface.get()); |
Florin Malita | a4e1a63 | 2019-01-22 16:27:01 -0500 | [diff] [blame] | 644 | if (!fImpl->fHarfBuzzFont) { |
| 645 | SkDebugf("create_hb_font failed!\n"); |
| 646 | } |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 647 | fImpl->fBuffer.reset(hb_buffer_create()); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 648 | SkASSERT(fImpl->fBuffer); |
| 649 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 650 | UErrorCode status = U_ZERO_ERROR; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 651 | fImpl->fLineBreakIterator.reset(ubrk_open(UBRK_LINE, "th", nullptr, 0, &status)); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 652 | if (U_FAILURE(status)) { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 653 | SkDebugf("Could not create line break iterator: %s", u_errorName(status)); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 654 | SK_ABORT(""); |
| 655 | } |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 656 | |
| 657 | fImpl->fGraphemeBreakIterator.reset(ubrk_open(UBRK_CHARACTER, "th", nullptr, 0, &status)); |
| 658 | if (U_FAILURE(status)) { |
| 659 | SkDebugf("Could not create grapheme break iterator: %s", u_errorName(status)); |
| 660 | SK_ABORT(""); |
| 661 | } |
| 662 | |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | SkShaper::~SkShaper() {} |
| 666 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 667 | bool SkShaper::good() const { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 668 | return fImpl->fBuffer && |
| 669 | fImpl->fLineBreakIterator && |
| 670 | fImpl->fGraphemeBreakIterator; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 671 | } |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 672 | |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 673 | SkPoint SkShaper::shape(RunHandler* handler, |
Kevin Lubick | 57abfe9 | 2019-01-28 13:15:51 -0500 | [diff] [blame] | 674 | const SkFont& srcFont, |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 675 | const char* utf8, |
| 676 | size_t utf8Bytes, |
| 677 | bool leftToRight, |
| 678 | SkPoint point, |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 679 | SkScalar width) const |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 680 | { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 681 | SkASSERT(handler); |
| 682 | sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefDefault(); |
| 683 | UBiDiLevel defaultLevel = leftToRight ? UBIDI_DEFAULT_LTR : UBIDI_DEFAULT_RTL; |
| 684 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 685 | RunIteratorQueue runSegmenter; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 686 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 687 | SkTLazy<BiDiRunIterator> maybeBidi(BiDiRunIterator::Make(utf8, utf8Bytes, defaultLevel)); |
| 688 | BiDiRunIterator* bidi = maybeBidi.getMaybeNull(); |
| 689 | if (!bidi) { |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 690 | return point; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 691 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 692 | runSegmenter.insert(bidi); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 693 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 694 | SkTLazy<LanguageRunIterator> maybeLanguage(LanguageRunIterator::Make(utf8, utf8Bytes)); |
| 695 | LanguageRunIterator* language = maybeLanguage.getMaybeNull(); |
| 696 | if (!language) { |
| 697 | return point; |
| 698 | } |
| 699 | runSegmenter.insert(language); |
| 700 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 701 | hb_unicode_funcs_t* hbUnicode = hb_buffer_get_unicode_funcs(fImpl->fBuffer.get()); |
| 702 | SkTLazy<ScriptRunIterator> maybeScript(ScriptRunIterator::Make(utf8, utf8Bytes, hbUnicode)); |
| 703 | ScriptRunIterator* script = maybeScript.getMaybeNull(); |
| 704 | if (!script) { |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 705 | return point; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 706 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 707 | runSegmenter.insert(script); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 708 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 709 | SkTLazy<FontRunIterator> maybeFont(FontRunIterator::Make(utf8, utf8Bytes, |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 710 | srcFont, std::move(fontMgr))); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 711 | FontRunIterator* font = maybeFont.getMaybeNull(); |
| 712 | if (!font) { |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 713 | return point; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 714 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 715 | runSegmenter.insert(font); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 716 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 717 | if (true) { |
| 718 | return fImpl->shapeCorrect(handler, utf8, utf8Bytes, point, width, |
| 719 | runSegmenter, bidi, language, script, font); |
| 720 | } else { |
| 721 | return fImpl->shapeOk(handler, utf8, utf8Bytes, point, width, |
| 722 | runSegmenter, bidi, language, script, font); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | SkPoint SkShaper::Impl::shapeCorrect(RunHandler* handler, |
| 727 | const char* utf8, |
| 728 | size_t utf8Bytes, |
| 729 | SkPoint point, |
| 730 | SkScalar width, |
| 731 | RunIteratorQueue& runSegmenter, |
| 732 | const BiDiRunIterator* bidi, |
| 733 | const LanguageRunIterator* language, |
| 734 | const ScriptRunIterator* script, |
| 735 | const FontRunIterator* font) const |
| 736 | { |
| 737 | ShapedLine line; |
| 738 | size_t lineIndex = 0; |
| 739 | SkPoint currentPoint = point; |
| 740 | |
| 741 | const char* utf8Start = nullptr; |
| 742 | const char* utf8End = utf8; |
| 743 | while (runSegmenter.advanceRuns()) { // For each item |
| 744 | utf8Start = utf8End; |
| 745 | utf8End = runSegmenter.endOfCurrentRun(); |
| 746 | |
| 747 | ShapedRun model(nullptr, nullptr, 0, SkFont(), 0, nullptr); |
| 748 | bool modelNeedsRegenerated = true; |
| 749 | int modelOffset = 0; |
| 750 | |
| 751 | struct TextProps { |
| 752 | int glyphLen = 0; |
| 753 | SkVector advance = {0, 0}; |
| 754 | }; |
| 755 | // map from character position to [safe to break, glyph position, advance] |
| 756 | std::unique_ptr<TextProps[]> modelText; |
| 757 | int modelTextOffset = 0; |
| 758 | SkVector modelTextAdvanceOffset = {0, 0}; |
| 759 | |
| 760 | while (utf8Start < utf8End) { // While there are still code points left in this item |
| 761 | size_t utf8runLength = utf8End - utf8Start; |
| 762 | if (modelNeedsRegenerated) { |
| 763 | model = shape(utf8, utf8Bytes, |
| 764 | utf8Start, utf8End, |
| 765 | bidi, language, script, font); |
| 766 | modelOffset = 0; |
| 767 | |
| 768 | SkVector advance = {0, 0}; |
| 769 | modelText.reset(new TextProps[utf8runLength + 1]()); |
| 770 | for (int i = 0; i < model.fNumGlyphs; ++i) { |
| 771 | SkASSERT(model.fGlyphs[i].fCluster < utf8runLength); |
| 772 | if (!model.fGlyphs[i].fUnsafeToBreak) { |
| 773 | modelText[model.fGlyphs[i].fCluster].glyphLen = i; |
| 774 | modelText[model.fGlyphs[i].fCluster].advance = advance; |
| 775 | } |
| 776 | advance += model.fGlyphs[i].fAdvance; |
| 777 | } |
| 778 | // Assume it is always safe to break after the end of an item |
| 779 | modelText[utf8runLength].glyphLen = model.fNumGlyphs; |
| 780 | modelText[utf8runLength].advance = model.fAdvance; |
| 781 | modelTextOffset = 0; |
| 782 | modelTextAdvanceOffset = {0, 0}; |
| 783 | modelNeedsRegenerated = false; |
| 784 | } |
| 785 | |
| 786 | // TODO: break iterator per item, but just reset position if needed? |
| 787 | // Maybe break iterator with model? |
| 788 | UBreakIterator& breakIterator = *fLineBreakIterator; |
| 789 | { |
| 790 | UErrorCode status = U_ZERO_ERROR; |
| 791 | UText utf8UText = UTEXT_INITIALIZER; |
| 792 | utext_openUTF8(&utf8UText, utf8Start, utf8runLength, &status); |
| 793 | std::unique_ptr<UText, SkFunctionWrapper<UText*, UText, utext_close>> autoClose(&utf8UText); |
| 794 | if (U_FAILURE(status)) { |
| 795 | SkDebugf("Could not create utf8UText: %s", u_errorName(status)); |
| 796 | return point; |
| 797 | } |
| 798 | ubrk_setUText(&breakIterator, &utf8UText, &status); |
| 799 | if (U_FAILURE(status)) { |
| 800 | SkDebugf("Could not setText on break iterator: %s", u_errorName(status)); |
| 801 | return point; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | ShapedRun best(nullptr, nullptr, 0, SkFont(), 0, nullptr); |
| 806 | best.fAdvance = { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity }; |
| 807 | SkScalar widthLeft = width - line.fAdvance.fX; |
| 808 | |
| 809 | for (int32_t breakIteratorCurrent = ubrk_next(&breakIterator); |
| 810 | breakIteratorCurrent != UBRK_DONE; |
| 811 | breakIteratorCurrent = ubrk_next(&breakIterator)) |
| 812 | { |
| 813 | // TODO: if past a safe to break, future safe to break will be at least as long |
| 814 | |
| 815 | // TODO: adjust breakIteratorCurrent by ignorable whitespace |
| 816 | ShapedRun candidate = modelText[breakIteratorCurrent + modelTextOffset].glyphLen |
| 817 | ? ShapedRun(utf8Start, utf8Start + breakIteratorCurrent, |
| 818 | modelText[breakIteratorCurrent + modelTextOffset].glyphLen - modelOffset, |
| 819 | *font->currentFont(), |
| 820 | bidi->currentLevel(), |
| 821 | std::unique_ptr<ShapedGlyph[]>()) |
| 822 | : shape(utf8, utf8Bytes, |
| 823 | utf8Start, utf8Start + breakIteratorCurrent, |
| 824 | bidi, language, script, font); |
| 825 | if (!candidate.fUtf8Start) { |
| 826 | //report error |
| 827 | return point; |
| 828 | } |
| 829 | if (!candidate.fGlyphs) { |
| 830 | candidate.fAdvance = modelText[breakIteratorCurrent + modelTextOffset].advance - modelTextAdvanceOffset; |
| 831 | } |
| 832 | auto score = [widthLeft](const ShapedRun& run) -> SkScalar { |
| 833 | if (run.fAdvance.fX < widthLeft) { |
| 834 | if (run.fUtf8Start == nullptr) { |
| 835 | return SK_ScalarNegativeInfinity; |
| 836 | } else { |
| 837 | return run.fUtf8End - run.fUtf8Start; |
| 838 | } |
| 839 | } else { |
| 840 | return widthLeft - run.fAdvance.fX; |
| 841 | } |
| 842 | }; |
| 843 | if (score(best) < score(candidate)) { |
| 844 | best = std::move(candidate); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | // If nothing fit (best score is negative) and the line is not empty |
| 849 | if (width < line.fAdvance.fX + best.fAdvance.fX && !line.runs.empty()) { |
| 850 | emit(line, handler, point, currentPoint, lineIndex); |
| 851 | line.runs.reset(); |
| 852 | line.fAdvance = {0, 0}; |
| 853 | } else { |
| 854 | if (!best.fGlyphs) { |
| 855 | best.fGlyphs.reset(new ShapedGlyph[best.fNumGlyphs]); |
| 856 | memcpy(best.fGlyphs.get(), model.fGlyphs.get() + modelOffset, |
| 857 | best.fNumGlyphs * sizeof(ShapedGlyph)); |
| 858 | modelOffset += best.fNumGlyphs; |
| 859 | modelTextOffset += best.fUtf8End - best.fUtf8Start; |
| 860 | modelTextAdvanceOffset += best.fAdvance; |
| 861 | } else { |
| 862 | modelNeedsRegenerated = true; |
| 863 | } |
| 864 | utf8Start = best.fUtf8End; |
| 865 | line.fAdvance += best.fAdvance; |
| 866 | line.runs.emplace_back(std::move(best)); |
| 867 | |
| 868 | // If item broken, emit line (prevent remainder from accidentally fitting) |
| 869 | if (utf8Start != utf8End) { |
| 870 | emit(line, handler, point, currentPoint, lineIndex); |
| 871 | line.runs.reset(); |
| 872 | line.fAdvance = {0, 0}; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | emit(line, handler, point, currentPoint, lineIndex); |
| 878 | return currentPoint; |
| 879 | } |
| 880 | |
| 881 | SkPoint SkShaper::Impl::shapeOk(RunHandler* handler, |
| 882 | const char* utf8, |
| 883 | size_t utf8Bytes, |
| 884 | SkPoint point, |
| 885 | SkScalar width, |
| 886 | RunIteratorQueue& runSegmenter, |
| 887 | const BiDiRunIterator* bidi, |
| 888 | const LanguageRunIterator* language, |
| 889 | const ScriptRunIterator* script, |
| 890 | const FontRunIterator* font) const |
| 891 | { |
| 892 | SkTArray<ShapedRun> runs; |
| 893 | { |
| 894 | UBreakIterator& lineBreakIterator = *fLineBreakIterator; |
| 895 | UBreakIterator& graphemeBreakIterator = *fGraphemeBreakIterator; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 896 | { |
| 897 | UErrorCode status = U_ZERO_ERROR; |
| 898 | UText utf8UText = UTEXT_INITIALIZER; |
| 899 | utext_openUTF8(&utf8UText, utf8, utf8Bytes, &status); |
| 900 | std::unique_ptr<UText, SkFunctionWrapper<UText*, UText, utext_close>> autoClose(&utf8UText); |
| 901 | if (U_FAILURE(status)) { |
| 902 | SkDebugf("Could not create utf8UText: %s", u_errorName(status)); |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 903 | return point; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 904 | } |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 905 | |
| 906 | ubrk_setUText(&lineBreakIterator, &utf8UText, &status); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 907 | if (U_FAILURE(status)) { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 908 | SkDebugf("Could not setText on line break iterator: %s", u_errorName(status)); |
| 909 | return point; |
| 910 | } |
| 911 | ubrk_setUText(&graphemeBreakIterator, &utf8UText, &status); |
| 912 | if (U_FAILURE(status)) { |
| 913 | SkDebugf("Could not setText on grapheme break iterator: %s", u_errorName(status)); |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 914 | return point; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 915 | } |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 918 | const char* utf8Start = nullptr; |
| 919 | const char* utf8End = utf8; |
| 920 | while (runSegmenter.advanceRuns()) { |
| 921 | utf8Start = utf8End; |
| 922 | utf8End = runSegmenter.endOfCurrentRun(); |
| 923 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 924 | runs.emplace_back(shape(utf8, utf8Bytes, |
| 925 | utf8Start, utf8End, |
| 926 | bidi, language, script, font)); |
| 927 | ShapedRun& run = runs.back(); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 928 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 929 | int32_t clusterOffset = utf8Start - utf8; |
| 930 | uint32_t previousCluster = 0xFFFFFFFF; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 931 | for (int i = 0; i < run.fNumGlyphs; ++i) { |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 932 | ShapedGlyph& glyph = run.fGlyphs[i]; |
| 933 | int32_t glyphCluster = glyph.fCluster + clusterOffset; |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 934 | |
| 935 | int32_t lineBreakIteratorCurrent = ubrk_current(&lineBreakIterator); |
| 936 | while (lineBreakIteratorCurrent != UBRK_DONE && |
| 937 | lineBreakIteratorCurrent < glyphCluster) |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 938 | { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 939 | lineBreakIteratorCurrent = ubrk_next(&lineBreakIterator); |
Ben Wagner | 2868b78 | 2017-08-31 14:12:27 -0400 | [diff] [blame] | 940 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 941 | glyph.fMayLineBreakBefore = glyph.fCluster != previousCluster && |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 942 | lineBreakIteratorCurrent == glyphCluster; |
| 943 | |
| 944 | int32_t graphemeBreakIteratorCurrent = ubrk_current(&graphemeBreakIterator); |
| 945 | while (graphemeBreakIteratorCurrent != UBRK_DONE && |
| 946 | graphemeBreakIteratorCurrent < glyphCluster) |
| 947 | { |
| 948 | graphemeBreakIteratorCurrent = ubrk_next(&graphemeBreakIterator); |
| 949 | } |
| 950 | glyph.fGraphemeBreakBefore = glyph.fCluster != previousCluster && |
| 951 | graphemeBreakIteratorCurrent == glyphCluster; |
| 952 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 953 | previousCluster = glyph.fCluster; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 954 | } |
| 955 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 956 | } |
| 957 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 958 | // Iterate over the glyphs in logical order to find potential line lengths. |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 959 | { |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 960 | /** The position of the beginning of the line. */ |
| 961 | ShapedRunGlyphIterator beginning(runs); |
| 962 | |
| 963 | /** The position of the candidate line break. */ |
| 964 | ShapedRunGlyphIterator candidateLineBreak(runs); |
| 965 | SkScalar candidateLineBreakWidth = 0; |
| 966 | |
| 967 | /** The position of the candidate grapheme break. */ |
| 968 | ShapedRunGlyphIterator candidateGraphemeBreak(runs); |
| 969 | SkScalar candidateGraphemeBreakWidth = 0; |
| 970 | |
| 971 | /** The position of the current location. */ |
| 972 | ShapedRunGlyphIterator current(runs); |
| 973 | SkScalar currentWidth = 0; |
| 974 | while (ShapedGlyph* glyph = current.current()) { |
| 975 | // 'Break' at graphemes until a line boundary, then only at line boundaries. |
| 976 | // Only break at graphemes if no line boundary is valid. |
| 977 | if (current != beginning) { |
| 978 | if (glyph->fGraphemeBreakBefore || glyph->fMayLineBreakBefore) { |
| 979 | // TODO: preserve line breaks <= grapheme breaks |
| 980 | // and prevent line breaks inside graphemes |
| 981 | candidateGraphemeBreak = current; |
| 982 | candidateGraphemeBreakWidth = currentWidth; |
| 983 | if (glyph->fMayLineBreakBefore) { |
| 984 | candidateLineBreak = current; |
| 985 | candidateLineBreakWidth = currentWidth; |
| 986 | } |
| 987 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 988 | } |
| 989 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 990 | SkScalar glyphWidth = glyph->fAdvance.fX; |
| 991 | // Break when overwidth, the glyph has a visual representation, and some space is used. |
| 992 | if (width < currentWidth + glyphWidth && glyph->fHasVisual && candidateGraphemeBreakWidth > 0){ |
| 993 | if (candidateLineBreak != beginning) { |
| 994 | beginning = candidateLineBreak; |
| 995 | currentWidth -= candidateLineBreakWidth; |
| 996 | candidateGraphemeBreakWidth -= candidateLineBreakWidth; |
| 997 | candidateLineBreakWidth = 0; |
| 998 | } else if (candidateGraphemeBreak != beginning) { |
| 999 | beginning = candidateGraphemeBreak; |
| 1000 | candidateLineBreak = beginning; |
| 1001 | currentWidth -= candidateGraphemeBreakWidth; |
| 1002 | candidateGraphemeBreakWidth = 0; |
| 1003 | candidateLineBreakWidth = 0; |
| 1004 | } else { |
| 1005 | SK_ABORT(""); |
| 1006 | } |
| 1007 | |
| 1008 | if (width < currentWidth) { |
| 1009 | if (width < candidateGraphemeBreakWidth) { |
| 1010 | candidateGraphemeBreak = candidateLineBreak; |
| 1011 | candidateGraphemeBreakWidth = candidateLineBreakWidth; |
| 1012 | } |
| 1013 | current = candidateGraphemeBreak; |
| 1014 | currentWidth = candidateGraphemeBreakWidth; |
| 1015 | } |
| 1016 | |
| 1017 | glyph = beginning.current(); |
| 1018 | if (glyph) { |
| 1019 | glyph->fMustLineBreakBefore = true; |
| 1020 | } |
| 1021 | |
| 1022 | } else { |
| 1023 | current.next(); |
| 1024 | currentWidth += glyphWidth; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1025 | } |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Reorder the runs and glyphs per line and write them out. |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 1030 | SkPoint currentPoint = point; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1031 | { |
| 1032 | ShapedRunGlyphIterator previousBreak(runs); |
| 1033 | ShapedRunGlyphIterator glyphIterator(runs); |
| 1034 | SkScalar maxAscent = 0; |
| 1035 | SkScalar maxDescent = 0; |
| 1036 | SkScalar maxLeading = 0; |
| 1037 | int previousRunIndex = -1; |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 1038 | size_t lineIndex = 0; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1039 | while (glyphIterator.current()) { |
| 1040 | int runIndex = glyphIterator.fRunIndex; |
| 1041 | int glyphIndex = glyphIterator.fGlyphIndex; |
| 1042 | ShapedGlyph* nextGlyph = glyphIterator.next(); |
| 1043 | |
| 1044 | if (previousRunIndex != runIndex) { |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1045 | SkFontMetrics metrics; |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 1046 | runs[runIndex].fFont.getMetrics(&metrics); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1047 | maxAscent = SkTMin(maxAscent, metrics.fAscent); |
| 1048 | maxDescent = SkTMax(maxDescent, metrics.fDescent); |
| 1049 | maxLeading = SkTMax(maxLeading, metrics.fLeading); |
| 1050 | previousRunIndex = runIndex; |
| 1051 | } |
| 1052 | |
| 1053 | // Nothing can be written until the baseline is known. |
| 1054 | if (!(nextGlyph == nullptr || nextGlyph->fMustLineBreakBefore)) { |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | currentPoint.fY -= maxAscent; |
| 1059 | |
| 1060 | int numRuns = runIndex - previousBreak.fRunIndex + 1; |
| 1061 | SkAutoSTMalloc<4, UBiDiLevel> runLevels(numRuns); |
| 1062 | for (int i = 0; i < numRuns; ++i) { |
| 1063 | runLevels[i] = runs[previousBreak.fRunIndex + i].fLevel; |
| 1064 | } |
| 1065 | SkAutoSTMalloc<4, int32_t> logicalFromVisual(numRuns); |
| 1066 | ubidi_reorderVisual(runLevels, numRuns, logicalFromVisual); |
| 1067 | |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 1068 | // step through the runs in reverse visual order and the glyphs in reverse logical order |
| 1069 | // until a visible glyph is found and force them to the end of the visual line. |
| 1070 | |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1071 | for (int i = 0; i < numRuns; ++i) { |
| 1072 | int logicalIndex = previousBreak.fRunIndex + logicalFromVisual[i]; |
| 1073 | |
| 1074 | int startGlyphIndex = (logicalIndex == previousBreak.fRunIndex) |
| 1075 | ? previousBreak.fGlyphIndex |
| 1076 | : 0; |
| 1077 | int endGlyphIndex = (logicalIndex == runIndex) |
| 1078 | ? glyphIndex + 1 |
| 1079 | : runs[logicalIndex].fNumGlyphs; |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 1080 | |
| 1081 | const auto& run = runs[logicalIndex]; |
| 1082 | const RunHandler::RunInfo info = { |
| 1083 | lineIndex, |
| 1084 | run.fAdvance, |
| 1085 | maxAscent, |
| 1086 | maxDescent, |
| 1087 | maxLeading, |
| 1088 | }; |
| 1089 | append(handler, info, run, startGlyphIndex, endGlyphIndex, ¤tPoint); |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | currentPoint.fY += maxDescent + maxLeading; |
| 1093 | currentPoint.fX = point.fX; |
| 1094 | maxAscent = 0; |
| 1095 | maxDescent = 0; |
| 1096 | maxLeading = 0; |
| 1097 | previousRunIndex = -1; |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 1098 | ++lineIndex; |
Ben Wagner | 8d45a38 | 2017-11-16 10:08:28 -0500 | [diff] [blame] | 1099 | previousBreak = glyphIterator; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 1103 | return currentPoint; |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 1104 | } |
Ben Wagner | 0ec8ec2 | 2018-09-04 18:17:13 -0400 | [diff] [blame] | 1105 | |
| 1106 | |
| 1107 | ShapedRun SkShaper::Impl::shape(const char* utf8, |
| 1108 | size_t utf8Bytes, |
| 1109 | const char* utf8Start, |
| 1110 | const char* utf8End, |
| 1111 | const BiDiRunIterator* bidi, |
| 1112 | const LanguageRunIterator* language, |
| 1113 | const ScriptRunIterator* script, |
| 1114 | const FontRunIterator* font) const |
| 1115 | { |
| 1116 | ShapedRun run(nullptr, nullptr, 0, SkFont(), 0, nullptr); |
| 1117 | |
| 1118 | hb_buffer_t* buffer = fBuffer.get(); |
| 1119 | SkAutoTCallVProc<hb_buffer_t, hb_buffer_clear_contents> autoClearBuffer(buffer); |
| 1120 | hb_buffer_set_content_type(buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); |
| 1121 | hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS); |
| 1122 | |
| 1123 | // Add precontext. |
| 1124 | hb_buffer_add_utf8(buffer, utf8, utf8Start - utf8, utf8Start - utf8, 0); |
| 1125 | |
| 1126 | // Populate the hb_buffer directly with utf8 cluster indexes. |
| 1127 | const char* utf8Current = utf8Start; |
| 1128 | while (utf8Current < utf8End) { |
| 1129 | unsigned int cluster = utf8Current - utf8Start; |
| 1130 | hb_codepoint_t u = utf8_next(&utf8Current, utf8End); |
| 1131 | hb_buffer_add(buffer, u, cluster); |
| 1132 | } |
| 1133 | |
| 1134 | // Add postcontext. |
| 1135 | hb_buffer_add_utf8(buffer, utf8Current, utf8 + utf8Bytes - utf8Current, 0, 0); |
| 1136 | |
| 1137 | size_t utf8runLength = utf8End - utf8Start; |
| 1138 | if (!SkTFitsIn<int>(utf8runLength)) { |
| 1139 | SkDebugf("Shaping error: utf8 too long"); |
| 1140 | return run; |
| 1141 | } |
| 1142 | hb_direction_t direction = is_LTR(bidi->currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL; |
| 1143 | hb_buffer_set_direction(buffer, direction); |
| 1144 | hb_buffer_set_script(buffer, script->currentScript()); |
| 1145 | hb_buffer_set_language(buffer, language->currentLanguage()); |
| 1146 | hb_buffer_guess_segment_properties(buffer); |
| 1147 | // TODO: features |
| 1148 | if (!font->currentHBFont()) { |
| 1149 | return run; |
| 1150 | } |
| 1151 | hb_shape(font->currentHBFont(), buffer, nullptr, 0); |
| 1152 | unsigned len = hb_buffer_get_length(buffer); |
| 1153 | if (len == 0) { |
| 1154 | // TODO: this isn't an error, make it look different |
| 1155 | return run; |
| 1156 | } |
| 1157 | |
| 1158 | if (direction == HB_DIRECTION_RTL) { |
| 1159 | // Put the clusters back in logical order. |
| 1160 | // Note that the advances remain ltr. |
| 1161 | hb_buffer_reverse(buffer); |
| 1162 | } |
| 1163 | hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer, nullptr); |
| 1164 | hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buffer, nullptr); |
| 1165 | |
| 1166 | if (!SkTFitsIn<int>(len)) { |
| 1167 | SkDebugf("Shaping error: too many glyphs"); |
| 1168 | return run; |
| 1169 | } |
| 1170 | |
| 1171 | run = ShapedRun(utf8Start, utf8End, len, *font->currentFont(), |
| 1172 | bidi->currentLevel(), |
| 1173 | std::unique_ptr<ShapedGlyph[]>(new ShapedGlyph[len])); |
| 1174 | int scaleX, scaleY; |
| 1175 | hb_font_get_scale(font->currentHBFont(), &scaleX, &scaleY); |
| 1176 | double textSizeY = run.fFont.getSize() / scaleY; |
| 1177 | double textSizeX = run.fFont.getSize() / scaleX * run.fFont.getScaleX(); |
| 1178 | SkVector runAdvance = { 0, 0 }; |
| 1179 | for (unsigned i = 0; i < len; i++) { |
| 1180 | ShapedGlyph& glyph = run.fGlyphs[i]; |
| 1181 | glyph.fID = info[i].codepoint; |
| 1182 | glyph.fCluster = info[i].cluster; |
| 1183 | glyph.fOffset.fX = pos[i].x_offset * textSizeX; |
| 1184 | glyph.fOffset.fY = pos[i].y_offset * textSizeY; |
| 1185 | glyph.fAdvance.fX = pos[i].x_advance * textSizeX; |
| 1186 | glyph.fAdvance.fY = pos[i].y_advance * textSizeY; |
| 1187 | |
| 1188 | SkRect bounds; |
| 1189 | SkScalar advance; |
| 1190 | SkPaint p; |
| 1191 | run.fFont.getWidthsBounds(&glyph.fID, 1, &advance, &bounds, &p); |
| 1192 | glyph.fHasVisual = !bounds.isEmpty(); //!font->currentTypeface()->glyphBoundsAreZero(glyph.fID); |
| 1193 | glyph.fUnsafeToBreak = info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
| 1194 | glyph.fMustLineBreakBefore = false; |
| 1195 | |
| 1196 | runAdvance += glyph.fAdvance; |
| 1197 | } |
| 1198 | run.fAdvance = runAdvance; |
| 1199 | |
| 1200 | return run; |
| 1201 | } |