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 SkStrikeInterface_DEFINED
9 #define SkStrikeInterface_DEFINED
10 
11 #include <memory>
12 
13 #include "SkPoint.h"
14 #include "SkSpan.h"
15 #include "SkTypes.h"
16 
17 class SkDescriptor;
18 class SkGlyph;
19 class SkMaskFilter;
20 class SkPathEffect;
21 class SkTypeface;
22 
23 // TODO: rename SkScalerContextEffects -> SkStrikeEffects
24 struct SkScalerContextEffects {
SkScalerContextEffectsSkScalerContextEffects25     SkScalerContextEffects() : fPathEffect(nullptr), fMaskFilter(nullptr) {}
SkScalerContextEffectsSkScalerContextEffects26     SkScalerContextEffects(SkPathEffect* pe, SkMaskFilter* mf)
27             : fPathEffect(pe), fMaskFilter(mf) {}
SkScalerContextEffectsSkScalerContextEffects28     explicit SkScalerContextEffects(const SkPaint& paint)
29             : fPathEffect(paint.getPathEffect())
30             , fMaskFilter(paint.getMaskFilter()) {}
31 
32     SkPathEffect*   fPathEffect;
33     SkMaskFilter*   fMaskFilter;
34 };
35 
36 class SkStrikeSpec {
37 public:
SkStrikeSpec(const SkDescriptor & desc,const SkTypeface & typeface,const SkScalerContextEffects & effects)38     SkStrikeSpec(const SkDescriptor& desc,
39                  const SkTypeface& typeface,
40                  const SkScalerContextEffects& effects)
41             : fDesc{desc}
42             , fTypeface{typeface}
43             , fEffects{effects} {}
44 
desc()45     const SkDescriptor& desc() const { return fDesc; }
typeface()46     const SkTypeface& typeface() const { return fTypeface; }
effects()47     SkScalerContextEffects effects() const {return fEffects; }
48 
49 private:
50     const SkDescriptor& fDesc;
51     const SkTypeface& fTypeface;
52     const SkScalerContextEffects fEffects;
53 };
54 
55 struct SkGlyphPos {
56     const SkGlyph* glyph;
57     SkPoint position;
58 };
59 
60 struct SkPathPos {
61     const SkPath* path;
62     SkPoint position;
63 };
64 
65 class SkStrikeInterface {
66 public:
67     virtual ~SkStrikeInterface() = default;
68     virtual SkVector rounding() const = 0;
69     virtual const SkDescriptor& getDescriptor() const = 0;
70     virtual SkStrikeSpec strikeSpec() const = 0;
71 
72     // glyphMetrics writes its results to result, but only returns a subspan of result.
73     virtual int glyphMetrics(const SkGlyphID[], const SkPoint[], int n, SkGlyphPos result[]) = 0;
74     virtual const SkGlyph& getGlyphMetrics(SkGlyphID glyphID, SkPoint position) = 0;
75     virtual bool decideCouldDrawFromPath(const SkGlyph& glyph) = 0;
76     virtual void onAboutToExitScope() = 0;
77 
78     struct Deleter {
operatorDeleter79         void operator()(SkStrikeInterface* ptr) const {
80             ptr->onAboutToExitScope();
81         }
82     };
83 };
84 
85 using SkScopedStrike = std::unique_ptr<SkStrikeInterface, SkStrikeInterface::Deleter>;
86 
87 class SkStrikeCacheInterface {
88 public:
89     virtual ~SkStrikeCacheInterface() = default;
90     virtual SkScopedStrike findOrCreateScopedStrike(const SkDescriptor& desc,
91                                                     const SkScalerContextEffects& effects,
92                                                     const SkTypeface& typeface) = 0;
93 };
94 
95 #endif  //SkStrikeInterface_DEFINED
96