Debian Codegraph @

src/core/SkStrikeSpec.cpp

raw
1 /*
2 * Copyright 2019 The Android Open Source Project
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 "src/core/SkStrikeSpec.h"
9
10 #include "include/core/SkFont.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkSurfaceProps.h"
15 #include "src/core/SkFontPriv.h"
16 #include "src/core/SkGlyph.h"
17 #include "src/core/SkStrike.h"
18 #include "src/core/SkStrikeCache.h"
19 #include "src/text/StrikeForGPU.h"
20
21 #include <utility>
22
23 SkStrikeSpec::SkStrikeSpec(const SkDescriptor& descriptor, sk_sp<SkTypeface> typeface)
24 : fAutoDescriptor{descriptor}
25 , fTypeface{std::move(typeface)} {}
26
27 SkStrikeSpec::SkStrikeSpec(const SkStrikeSpec&) = default;
28 SkStrikeSpec::SkStrikeSpec(SkStrikeSpec&&) = default;
29 SkStrikeSpec::~SkStrikeSpec() = default;
30
31 SkStrikeSpec SkStrikeSpec::MakeMask(const SkFont& font, const SkPaint& paint,
32 const SkSurfaceProps& surfaceProps,
33 SkScalerContextFlags scalerContextFlags,
34 const SkMatrix& deviceMatrix) {
35
36 return SkStrikeSpec(font, paint, surfaceProps, scalerContextFlags, deviceMatrix);
37 }
38
39 SkStrikeSpec SkStrikeSpec::MakeTransformMask(const SkFont& font,
40 const SkPaint& paint,
41 const SkSurfaceProps& surfaceProps,
42 SkScalerContextFlags scalerContextFlags,
43 const SkMatrix& deviceMatrix) {
44 SkFont sourceFont{font};
45 sourceFont.setSubpixel(false);
46 return SkStrikeSpec(sourceFont, paint, surfaceProps, scalerContextFlags, deviceMatrix);
47 }
48
49 std::tuple<SkStrikeSpec, SkScalar> SkStrikeSpec::MakePath(
50 const SkFont& font, const SkPaint& paint,
51 const SkSurfaceProps& surfaceProps,
52 SkScalerContextFlags scalerContextFlags) {
53
54 // setup our std runPaint, in hopes of getting hits in the cache
55 SkPaint pathPaint{paint};
56 SkFont pathFont{font};
57
58 // The sub-pixel position will always happen when transforming to the screen.
59 pathFont.setSubpixel(false);
60
61 // The factor to get from the size stored in the strike to the size needed for
62 // the source.
63 SkScalar strikeToSourceScale = pathFont.setupForAsPaths(&pathPaint);
64
65 return {SkStrikeSpec(pathFont, pathPaint, surfaceProps, scalerContextFlags, SkMatrix::I()),
66 strikeToSourceScale};
67 }
68
69 std::tuple<SkStrikeSpec, SkScalar> SkStrikeSpec::MakeCanonicalized(
70 const SkFont& font, const SkPaint* paint) {
71 SkPaint canonicalizedPaint;
72 if (paint != nullptr) {
73 canonicalizedPaint = *paint;
74 }
75
76 const SkFont* canonicalizedFont = &font;
77 std::optional<SkFont> pathFont;
78 SkScalar strikeToSourceScale = 1;
79 if (ShouldDrawAsPath(canonicalizedPaint, font, SkMatrix::I())) {
80 pathFont = font;
81 canonicalizedFont = &pathFont.value();
82 strikeToSourceScale = pathFont->setupForAsPaths(nullptr);
83 canonicalizedPaint.reset();
84 }
85
86 return {SkStrikeSpec(*canonicalizedFont, canonicalizedPaint, SkSurfaceProps(),
87 SkScalerContextFlags::kFakeGammaAndBoostContrast, SkMatrix::I()),
88 strikeToSourceScale};
89 }
90
91 SkStrikeSpec SkStrikeSpec::MakeWithNoDevice(const SkFont& font, const SkPaint* paint,
92 SkScalerContextFlags flags) {
93 SkPaint setupPaint;
94 if (paint != nullptr) {
95 setupPaint = *paint;
96 }
97
98 return SkStrikeSpec(font, setupPaint, SkSurfaceProps(), flags, SkMatrix::I());
99 }
100
101 bool SkStrikeSpec::ShouldDrawAsPath(
102 const SkPaint& paint, const SkFont& font, const SkMatrix& viewMatrix) {
103
104 // hairline glyphs are fast enough, so we don't need to cache them
105 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
106 return true;
107 }
108
109 // we don't cache perspective
110 if (viewMatrix.hasPerspective()) {
111 return true;
112 }
113
114 SkMatrix textMatrix = SkFontPriv::MakeTextMatrix(font);
115 textMatrix.postConcat(viewMatrix);
116
117 // we have a self-imposed maximum, just to limit memory-usage
118 constexpr SkScalar memoryLimit = 256;
119 constexpr SkScalar maxSizeSquared = memoryLimit * memoryLimit;
120
121 auto distance = [&textMatrix](int XIndex, int YIndex) {
122 return textMatrix[XIndex] * textMatrix[XIndex] + textMatrix[YIndex] * textMatrix[YIndex];
123 };
124
125 return distance(SkMatrix::kMScaleX, SkMatrix::kMSkewY ) > maxSizeSquared
126 || distance(SkMatrix::kMSkewX, SkMatrix::kMScaleY) > maxSizeSquared;
127 }
128
129 SkString SkStrikeSpec::dump() const {
130 return fAutoDescriptor.getDesc()->dumpRec();
131 }
132
133 SkStrikeSpec::SkStrikeSpec(const SkFont& font, const SkPaint& paint,
134 const SkSurfaceProps& surfaceProps,
135 SkScalerContextFlags scalerContextFlags,
136 const SkMatrix& deviceMatrix) {
137 SkScalerContextEffects effects;
138
139 SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
140 font, paint, surfaceProps, scalerContextFlags, deviceMatrix,
141 &fAutoDescriptor, &effects);
142
143 fMaskFilter = sk_ref_sp(effects.fMaskFilter);
144 fPathEffect = sk_ref_sp(effects.fPathEffect);
145 fTypeface = font.refTypeface();
146 }
147
148 sk_sp<sktext::StrikeForGPU> SkStrikeSpec::findOrCreateScopedStrike(
149 sktext::StrikeForGPUCacheInterface* cache) const {
150 return cache->findOrCreateScopedStrike(*this);
151 }
152
153 sk_sp<SkStrike> SkStrikeSpec::findOrCreateStrike() const {
154 return SkStrikeCache::GlobalStrikeCache()->findOrCreateStrike(*this);
155 }
156
157 sk_sp<SkStrike> SkStrikeSpec::findOrCreateStrike(SkStrikeCache* cache) const {
158 return cache->findOrCreateStrike(*this);
159 }
160
161 SkBulkGlyphMetrics::SkBulkGlyphMetrics(const SkStrikeSpec& spec)
162 : fStrike{spec.findOrCreateStrike()} { }
163
164 SkBulkGlyphMetrics::~SkBulkGlyphMetrics() = default;
165
166 SkSpan<const SkGlyph*> SkBulkGlyphMetrics::glyphs(SkSpan<const SkGlyphID> glyphIDs) {
167 fGlyphs.reset(glyphIDs.size());
168 return fStrike->metrics(glyphIDs, fGlyphs.get());
169 }
170
171 const SkGlyph* SkBulkGlyphMetrics::glyph(SkGlyphID glyphID) {
172 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
173 }
174
175 SkBulkGlyphMetricsAndPaths::SkBulkGlyphMetricsAndPaths(const SkStrikeSpec& spec)
176 : fStrike{spec.findOrCreateStrike()} { }
177
178 SkBulkGlyphMetricsAndPaths::SkBulkGlyphMetricsAndPaths(sk_sp<SkStrike>&& strike)
179 : fStrike{std::move(strike)} { }
180
181 SkBulkGlyphMetricsAndPaths::~SkBulkGlyphMetricsAndPaths() = default;
182
183 SkSpan<const SkGlyph*> SkBulkGlyphMetricsAndPaths::glyphs(SkSpan<const SkGlyphID> glyphIDs) {
184 fGlyphs.reset(glyphIDs.size());
185 return fStrike->preparePaths(glyphIDs, fGlyphs.get());
186 }
187
188 const SkGlyph* SkBulkGlyphMetricsAndPaths::glyph(SkGlyphID glyphID) {
189 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
190 }
191
192 void SkBulkGlyphMetricsAndPaths::findIntercepts(
193 const SkScalar* bounds, SkScalar scale, SkScalar xPos,
194 const SkGlyph* glyph, SkScalar* array, int* count) {
195 // TODO(herb): remove this abominable const_cast. Do the intercepts really need to be on the
196 // glyph?
197 fStrike->findIntercepts(bounds, scale, xPos, const_cast<SkGlyph*>(glyph), array, count);
198 }
199
200 SkBulkGlyphMetricsAndDrawables::SkBulkGlyphMetricsAndDrawables(const SkStrikeSpec& spec)
201 : fStrike{spec.findOrCreateStrike()} { }
202
203 SkBulkGlyphMetricsAndDrawables::SkBulkGlyphMetricsAndDrawables(sk_sp<SkStrike>&& strike)
204 : fStrike{std::move(strike)} { }
205
206 SkBulkGlyphMetricsAndDrawables::~SkBulkGlyphMetricsAndDrawables() = default;
207
208 SkSpan<const SkGlyph*> SkBulkGlyphMetricsAndDrawables::glyphs(SkSpan<const SkGlyphID> glyphIDs) {
209 fGlyphs.reset(glyphIDs.size());
210 return fStrike->prepareDrawables(glyphIDs, fGlyphs.get());
211 }
212
213 const SkGlyph* SkBulkGlyphMetricsAndDrawables::glyph(SkGlyphID glyphID) {
214 return this->glyphs(SkSpan<const SkGlyphID>{&glyphID, 1})[0];
215 }
216
217 SkBulkGlyphMetricsAndImages::SkBulkGlyphMetricsAndImages(const SkStrikeSpec& spec)
218 : fStrike{spec.findOrCreateStrike()} { }
219
220 SkBulkGlyphMetricsAndImages::SkBulkGlyphMetricsAndImages(sk_sp<SkStrike>&& strike)
221 : fStrike{std::move(strike)} { }
222
223 SkBulkGlyphMetricsAndImages::~SkBulkGlyphMetricsAndImages() = default;
224
225 SkSpan<const SkGlyph*> SkBulkGlyphMetricsAndImages::glyphs(SkSpan<const SkPackedGlyphID> glyphIDs) {
226 fGlyphs.reset(glyphIDs.size());
227 return fStrike->prepareImages(glyphIDs, fGlyphs.get());
228 }
229
230 const SkGlyph* SkBulkGlyphMetricsAndImages::glyph(SkPackedGlyphID packedID) {
231 return this->glyphs(SkSpan<const SkPackedGlyphID>{&packedID, 1})[0];
232 }
233
234 const SkDescriptor& SkBulkGlyphMetricsAndImages::descriptor() const {
235 return fStrike->getDescriptor();
236 }