Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkFont.h" |
| 9 | #include "include/core/SkFontMetrics.h" |
| 10 | #include "include/core/SkFontMgr.h" |
| 11 | #include "include/core/SkString.h" |
| 12 | #include "include/core/SkTypeface.h" |
| 13 | #include "include/private/SkTFitsIn.h" |
| 14 | #include "modules/skshaper/include/SkShaper.h" |
| 15 | #include "src/core/SkMakeUnique.h" |
| 16 | #include "src/core/SkTextBlobPriv.h" |
| 17 | #include "src/utils/SkUTF.h" |
Ben Wagner | 1383a38 | 2019-04-03 17:53:53 -0400 | [diff] [blame] | 18 | |
| 19 | #include <limits.h> |
| 20 | #include <string.h> |
| 21 | #include <locale> |
| 22 | #include <string> |
| 23 | #include <utility> |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 24 | |
Florin Malita | 4268433 | 2019-07-26 14:54:40 -0400 | [diff] [blame^] | 25 | std::unique_ptr<SkShaper> SkShaper::Make(sk_sp<SkFontMgr> fontmgr) { |
Ben Wagner | b059194 | 2019-02-15 14:46:18 -0500 | [diff] [blame] | 26 | #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE |
Florin Malita | 4268433 | 2019-07-26 14:54:40 -0400 | [diff] [blame^] | 27 | std::unique_ptr<SkShaper> shaper = SkShaper::MakeShaperDrivenWrapper(std::move(fontmgr)); |
Ben Wagner | b059194 | 2019-02-15 14:46:18 -0500 | [diff] [blame] | 28 | if (shaper) { |
| 29 | return shaper; |
| 30 | } |
| 31 | #endif |
| 32 | return SkShaper::MakePrimitive(); |
| 33 | } |
| 34 | |
| 35 | SkShaper::SkShaper() {} |
| 36 | SkShaper::~SkShaper() {} |
| 37 | |
Ben Wagner | 1383a38 | 2019-04-03 17:53:53 -0400 | [diff] [blame] | 38 | /** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 39 | static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 40 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 41 | return val < 0 ? 0xFFFD : val; |
| 42 | } |
| 43 | |
| 44 | class FontMgrRunIterator final : public SkShaper::FontRunIterator { |
| 45 | public: |
| 46 | FontMgrRunIterator(const char* utf8, size_t utf8Bytes, const SkFont& font, |
| 47 | sk_sp<SkFontMgr> fallbackMgr) |
| 48 | : fCurrent(utf8), fBegin(utf8), fEnd(fCurrent + utf8Bytes) |
| 49 | , fFallbackMgr(std::move(fallbackMgr)) |
| 50 | , fFont(font) |
| 51 | , fFallbackFont(fFont) |
| 52 | , fCurrentFont(nullptr) |
| 53 | { |
| 54 | fFont.setTypeface(font.refTypefaceOrDefault()); |
| 55 | fFallbackFont.setTypeface(nullptr); |
| 56 | } |
| 57 | void consume() override { |
| 58 | SkASSERT(fCurrent < fEnd); |
| 59 | SkUnichar u = utf8_next(&fCurrent, fEnd); |
| 60 | // If the starting typeface can handle this character, use it. |
| 61 | if (fFont.unicharToGlyph(u)) { |
| 62 | fCurrentFont = &fFont; |
| 63 | // If the current fallback can handle this character, use it. |
| 64 | } else if (fFallbackFont.getTypeface() && fFallbackFont.unicharToGlyph(u)) { |
| 65 | fCurrentFont = &fFallbackFont; |
| 66 | // If not, try to find a fallback typeface |
| 67 | } else { |
| 68 | sk_sp<SkTypeface> candidate(fFallbackMgr->matchFamilyStyleCharacter( |
| 69 | nullptr, fFont.getTypeface()->fontStyle(), nullptr, 0, u)); |
| 70 | if (candidate) { |
| 71 | fFallbackFont.setTypeface(std::move(candidate)); |
| 72 | fCurrentFont = &fFallbackFont; |
| 73 | } else { |
| 74 | fCurrentFont = &fFont; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | while (fCurrent < fEnd) { |
| 79 | const char* prev = fCurrent; |
| 80 | u = utf8_next(&fCurrent, fEnd); |
| 81 | |
| 82 | // End run if not using initial typeface and initial typeface has this character. |
| 83 | if (fCurrentFont->getTypeface() != fFont.getTypeface() && fFont.unicharToGlyph(u)) { |
| 84 | fCurrent = prev; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // End run if current typeface does not have this character and some other font does. |
| 89 | if (!fCurrentFont->unicharToGlyph(u)) { |
| 90 | sk_sp<SkTypeface> candidate(fFallbackMgr->matchFamilyStyleCharacter( |
| 91 | nullptr, fFont.getTypeface()->fontStyle(), nullptr, 0, u)); |
| 92 | if (candidate) { |
| 93 | fCurrent = prev; |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | size_t endOfCurrentRun() const override { |
| 100 | return fCurrent - fBegin; |
| 101 | } |
| 102 | bool atEnd() const override { |
| 103 | return fCurrent == fEnd; |
| 104 | } |
| 105 | |
| 106 | const SkFont& currentFont() const override { |
| 107 | return *fCurrentFont; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | char const * fCurrent; |
| 112 | char const * const fBegin; |
| 113 | char const * const fEnd; |
| 114 | sk_sp<SkFontMgr> fFallbackMgr; |
| 115 | SkFont fFont; |
| 116 | SkFont fFallbackFont; |
| 117 | SkFont* fCurrentFont; |
| 118 | }; |
| 119 | |
| 120 | std::unique_ptr<SkShaper::FontRunIterator> |
| 121 | SkShaper::MakeFontMgrRunIterator(const char* utf8, size_t utf8Bytes, |
| 122 | const SkFont& font, sk_sp<SkFontMgr> fallback) |
| 123 | { |
| 124 | return skstd::make_unique<FontMgrRunIterator>(utf8, utf8Bytes, font, std::move(fallback)); |
| 125 | } |
| 126 | |
Ben Wagner | 1383a38 | 2019-04-03 17:53:53 -0400 | [diff] [blame] | 127 | std::unique_ptr<SkShaper::LanguageRunIterator> |
| 128 | SkShaper::MakeStdLanguageRunIterator(const char* utf8, size_t utf8Bytes) { |
Mike Reed | c722165 | 2019-05-30 14:09:33 -0400 | [diff] [blame] | 129 | return skstd::make_unique<TrivialLanguageRunIterator>(std::locale().name().c_str(), utf8Bytes); |
Ben Wagner | 1383a38 | 2019-04-03 17:53:53 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 132 | void SkTextBlobBuilderRunHandler::beginLine() { |
| 133 | fCurrentPosition = fOffset; |
| 134 | fMaxRunAscent = 0; |
| 135 | fMaxRunDescent = 0; |
| 136 | fMaxRunLeading = 0; |
| 137 | } |
| 138 | void SkTextBlobBuilderRunHandler::runInfo(const RunInfo& info) { |
| 139 | SkFontMetrics metrics; |
| 140 | info.fFont.getMetrics(&metrics); |
| 141 | fMaxRunAscent = SkTMin(fMaxRunAscent, metrics.fAscent); |
| 142 | fMaxRunDescent = SkTMax(fMaxRunDescent, metrics.fDescent); |
| 143 | fMaxRunLeading = SkTMax(fMaxRunLeading, metrics.fLeading); |
| 144 | } |
| 145 | |
| 146 | void SkTextBlobBuilderRunHandler::commitRunInfo() { |
| 147 | fCurrentPosition.fY -= fMaxRunAscent; |
| 148 | } |
| 149 | |
| 150 | SkShaper::RunHandler::Buffer SkTextBlobBuilderRunHandler::runBuffer(const RunInfo& info) { |
| 151 | int glyphCount = SkTFitsIn<int>(info.glyphCount) ? info.glyphCount : INT_MAX; |
| 152 | int utf8RangeSize = SkTFitsIn<int>(info.utf8Range.size()) ? info.utf8Range.size() : INT_MAX; |
| 153 | |
| 154 | const auto& runBuffer = SkTextBlobBuilderPriv::AllocRunTextPos(&fBuilder, info.fFont, glyphCount, |
| 155 | utf8RangeSize, SkString()); |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 156 | if (runBuffer.utf8text && fUtf8Text) { |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 157 | memcpy(runBuffer.utf8text, fUtf8Text + info.utf8Range.begin(), utf8RangeSize); |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 158 | } |
| 159 | fClusters = runBuffer.clusters; |
| 160 | fGlyphCount = glyphCount; |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 161 | fClusterOffset = info.utf8Range.begin(); |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 162 | |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 163 | return { runBuffer.glyphs, |
Mike Reed | 22451cc | 2019-01-01 15:40:28 -0500 | [diff] [blame] | 164 | runBuffer.points(), |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 165 | nullptr, |
| 166 | runBuffer.clusters, |
| 167 | fCurrentPosition }; |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 170 | void SkTextBlobBuilderRunHandler::commitRunBuffer(const RunInfo& info) { |
Ben Wagner | 18ea3c7 | 2019-02-15 16:35:48 -0500 | [diff] [blame] | 171 | SkASSERT(0 <= fClusterOffset); |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 172 | for (int i = 0; i < fGlyphCount; ++i) { |
Ben Wagner | 18ea3c7 | 2019-02-15 16:35:48 -0500 | [diff] [blame] | 173 | SkASSERT(fClusters[i] >= (unsigned)fClusterOffset); |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 174 | fClusters[i] -= fClusterOffset; |
| 175 | } |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 176 | fCurrentPosition += info.fAdvance; |
| 177 | } |
| 178 | void SkTextBlobBuilderRunHandler::commitLine() { |
| 179 | fOffset += { 0, fMaxRunDescent + fMaxRunLeading - fMaxRunAscent }; |
Ben Wagner | 454e5fb | 2019-02-08 17:46:38 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Florin Malita | 950243d | 2019-01-11 11:08:35 -0500 | [diff] [blame] | 182 | sk_sp<SkTextBlob> SkTextBlobBuilderRunHandler::makeBlob() { |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 183 | return fBuilder.make(); |
| 184 | } |