• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #ifndef SkStrikeSpec_DEFINED
9 #define SkStrikeSpec_DEFINED
10 
11 #include "src/core/SkDescriptor.h"
12 #include "src/core/SkStrikeCache.h"
13 #include "src/core/SkStrikeForGPU.h"
14 
15 #if SK_SUPPORT_GPU
16 #include "src/gpu/text/GrSDFTControl.h"
17 class GrStrikeCache;
18 class GrTextStrike;
19 #endif
20 
21 class SkFont;
22 class SkPaint;
23 class SkStrikeCache;
24 class SkSurfaceProps;
25 
26 class SkStrikeSpec {
27 public:
28     SkStrikeSpec(const SkStrikeSpec&) = default;
29     SkStrikeSpec& operator=(const SkStrikeSpec&) = delete;
30 
31     SkStrikeSpec(SkStrikeSpec&&) = default;
32     SkStrikeSpec& operator=(SkStrikeSpec&&) = delete;
33 
34     ~SkStrikeSpec() = default;
35 
36     // Create a strike spec for mask style cache entries.
37     static SkStrikeSpec MakeMask(
38             const SkFont& font,
39             const SkPaint& paint,
40             const SkSurfaceProps& surfaceProps,
41             SkScalerContextFlags scalerContextFlags,
42             const SkMatrix& deviceMatrix);
43 
44     // Create a strike spec for path style cache entries.
45     static SkStrikeSpec MakePath(
46             const SkFont& font,
47             const SkPaint& paint,
48             const SkSurfaceProps& surfaceProps,
49             SkScalerContextFlags scalerContextFlags);
50 
51     static SkStrikeSpec MakeSourceFallback(const SkFont& font,
52                                            const SkPaint& paint,
53                                            const SkSurfaceProps& surfaceProps,
54                                            SkScalerContextFlags scalerContextFlags,
55                                            SkScalar maxSourceGlyphDimension);
56 
57     // Create a canonical strike spec for device-less measurements.
58     static SkStrikeSpec MakeCanonicalized(
59             const SkFont& font, const SkPaint* paint = nullptr);
60 
61     // Create a strike spec without a device, and does not switch over to path for large sizes.
62     // This means that strikeToSourceRatio() is always 1.
63     static SkStrikeSpec MakeWithNoDevice(const SkFont& font, const SkPaint* paint = nullptr);
64 
65     // Make a canonical strike spec for device-less measurements using default typeface and size.
66     static SkStrikeSpec MakeDefault();
67 
68     // Make a strike spec for PDF Vector strikes
69     static SkStrikeSpec MakePDFVector(const SkTypeface& typeface, int* size);
70 
71 #if SK_SUPPORT_GPU
72     // Create a strike spec for scaled distance field text.
73     static std::tuple<SkStrikeSpec, SkScalar, SkScalar> MakeSDFT(
74             const SkFont& font,
75             const SkPaint& paint,
76             const SkSurfaceProps& surfaceProps,
77             const SkMatrix& deviceMatrix,
78             const GrSDFTControl& control);
79 
80     sk_sp<GrTextStrike> findOrCreateGrStrike(GrStrikeCache* cache) const;
81 #endif
82 
83     SkScopedStrikeForGPU findOrCreateScopedStrike(SkStrikeForGPUCacheInterface* cache) const;
84 
85     sk_sp<SkStrike> findOrCreateStrike(
86             SkStrikeCache* cache = SkStrikeCache::GlobalStrikeCache()) const;
87 
strikeToSourceRatio()88     SkScalar strikeToSourceRatio() const { return fStrikeToSourceRatio; }
isEmpty()89     bool isEmpty() const { return SkScalarNearlyZero(fStrikeToSourceRatio); }
descriptor()90     const SkDescriptor& descriptor() const { return *fAutoDescriptor.getDesc(); }
91     static bool ShouldDrawAsPath(const SkPaint& paint, const SkFont& font, const SkMatrix& matrix);
92     SkString dump() const;
93 
94 private:
95     SkStrikeSpec(
96             const SkFont& font,
97             const SkPaint& paint,
98             const SkSurfaceProps& surfaceProps,
99             SkScalerContextFlags scalerContextFlags,
100             const SkMatrix& deviceMatrix,
101             SkScalar strikeToSourceRatio);
102 
103     SkAutoDescriptor fAutoDescriptor;
104     sk_sp<SkMaskFilter> fMaskFilter;
105     sk_sp<SkPathEffect> fPathEffect;
106     sk_sp<SkTypeface> fTypeface;
107     const SkScalar fStrikeToSourceRatio;
108 };
109 
110 class SkBulkGlyphMetrics {
111 public:
112     explicit SkBulkGlyphMetrics(const SkStrikeSpec& spec);
113     SkSpan<const SkGlyph*> glyphs(SkSpan<const SkGlyphID> glyphIDs);
114     const SkGlyph* glyph(SkGlyphID glyphID);
115 
116 private:
117     static constexpr int kTypicalGlyphCount = 20;
118     SkAutoSTArray<kTypicalGlyphCount, const SkGlyph*> fGlyphs;
119     sk_sp<SkStrike> fStrike;
120 };
121 
122 class SkBulkGlyphMetricsAndPaths {
123 public:
124     explicit SkBulkGlyphMetricsAndPaths(const SkStrikeSpec& spec);
125     explicit SkBulkGlyphMetricsAndPaths(sk_sp<SkStrike>&& strike);
126     SkSpan<const SkGlyph*> glyphs(SkSpan<const SkGlyphID> glyphIDs);
127     const SkGlyph* glyph(SkGlyphID glyphID);
128     void findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos,
129                         const SkGlyph* glyph, SkScalar* array, int* count);
130 
131 private:
132     static constexpr int kTypicalGlyphCount = 20;
133     SkAutoSTArray<kTypicalGlyphCount, const SkGlyph*> fGlyphs;
134     sk_sp<SkStrike> fStrike;
135 };
136 
137 class SkBulkGlyphMetricsAndImages {
138 public:
139     explicit SkBulkGlyphMetricsAndImages(const SkStrikeSpec& spec);
140     explicit SkBulkGlyphMetricsAndImages(sk_sp<SkStrike>&& strike);
141     SkSpan<const SkGlyph*> glyphs(SkSpan<const SkPackedGlyphID> packedIDs);
142     const SkGlyph* glyph(SkPackedGlyphID packedID);
143     const SkDescriptor& descriptor() const;
144 
145 private:
146     static constexpr int kTypicalGlyphCount = 64;
147     SkAutoSTArray<kTypicalGlyphCount, const SkGlyph*> fGlyphs;
148     sk_sp<SkStrike> fStrike;
149 };
150 
151 #endif  // SkStrikeSpec_DEFINED
152