blob: 053490be924e31375c2dd3cbed6d21dddba735fa [file] [log] [blame]
Florin Malita9867f612018-12-12 10:54:49 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Wagner1383a382019-04-03 17:53:53 -040018
19#include <limits.h>
20#include <string.h>
21#include <locale>
22#include <string>
23#include <utility>
Florin Malita9867f612018-12-12 10:54:49 -050024
Florin Malita42684332019-07-26 14:54:40 -040025std::unique_ptr<SkShaper> SkShaper::Make(sk_sp<SkFontMgr> fontmgr) {
Ben Wagnerb0591942019-02-15 14:46:18 -050026#ifdef SK_SHAPER_HARFBUZZ_AVAILABLE
Florin Malita42684332019-07-26 14:54:40 -040027 std::unique_ptr<SkShaper> shaper = SkShaper::MakeShaperDrivenWrapper(std::move(fontmgr));
Ben Wagnerb0591942019-02-15 14:46:18 -050028 if (shaper) {
29 return shaper;
30 }
31#endif
32 return SkShaper::MakePrimitive();
33}
34
35SkShaper::SkShaper() {}
36SkShaper::~SkShaper() {}
37
Ben Wagner1383a382019-04-03 17:53:53 -040038/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */
39static 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
44class FontMgrRunIterator final : public SkShaper::FontRunIterator {
45public:
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
110private:
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
120std::unique_ptr<SkShaper::FontRunIterator>
121SkShaper::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 Wagner1383a382019-04-03 17:53:53 -0400127std::unique_ptr<SkShaper::LanguageRunIterator>
128SkShaper::MakeStdLanguageRunIterator(const char* utf8, size_t utf8Bytes) {
Mike Reedc7221652019-05-30 14:09:33 -0400129 return skstd::make_unique<TrivialLanguageRunIterator>(std::locale().name().c_str(), utf8Bytes);
Ben Wagner1383a382019-04-03 17:53:53 -0400130}
131
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400132void SkTextBlobBuilderRunHandler::beginLine() {
133 fCurrentPosition = fOffset;
134 fMaxRunAscent = 0;
135 fMaxRunDescent = 0;
136 fMaxRunLeading = 0;
137}
138void 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
146void SkTextBlobBuilderRunHandler::commitRunInfo() {
147 fCurrentPosition.fY -= fMaxRunAscent;
148}
149
150SkShaper::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 Wagner454e5fb2019-02-08 17:46:38 -0500156 if (runBuffer.utf8text && fUtf8Text) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400157 memcpy(runBuffer.utf8text, fUtf8Text + info.utf8Range.begin(), utf8RangeSize);
Ben Wagner454e5fb2019-02-08 17:46:38 -0500158 }
159 fClusters = runBuffer.clusters;
160 fGlyphCount = glyphCount;
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400161 fClusterOffset = info.utf8Range.begin();
Ben Wagner454e5fb2019-02-08 17:46:38 -0500162
Florin Malita9867f612018-12-12 10:54:49 -0500163 return { runBuffer.glyphs,
Mike Reed22451cc2019-01-01 15:40:28 -0500164 runBuffer.points(),
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400165 nullptr,
166 runBuffer.clusters,
167 fCurrentPosition };
Florin Malita9867f612018-12-12 10:54:49 -0500168}
169
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400170void SkTextBlobBuilderRunHandler::commitRunBuffer(const RunInfo& info) {
Ben Wagner18ea3c72019-02-15 16:35:48 -0500171 SkASSERT(0 <= fClusterOffset);
Ben Wagner454e5fb2019-02-08 17:46:38 -0500172 for (int i = 0; i < fGlyphCount; ++i) {
Ben Wagner18ea3c72019-02-15 16:35:48 -0500173 SkASSERT(fClusters[i] >= (unsigned)fClusterOffset);
Ben Wagner454e5fb2019-02-08 17:46:38 -0500174 fClusters[i] -= fClusterOffset;
175 }
Ben Wagner3bdb69c2019-04-01 19:01:09 -0400176 fCurrentPosition += info.fAdvance;
177}
178void SkTextBlobBuilderRunHandler::commitLine() {
179 fOffset += { 0, fMaxRunDescent + fMaxRunLeading - fMaxRunAscent };
Ben Wagner454e5fb2019-02-08 17:46:38 -0500180}
181
Florin Malita950243d2019-01-11 11:08:35 -0500182sk_sp<SkTextBlob> SkTextBlobBuilderRunHandler::makeBlob() {
Florin Malita9867f612018-12-12 10:54:49 -0500183 return fBuilder.make();
184}