Line data Source code
1 : /*
2 : * Copyright 2014 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 : #include "GrPathRendering.h"
9 : #include "SkDescriptor.h"
10 : #include "SkGlyph.h"
11 : #include "SkMatrix.h"
12 : #include "SkTypeface.h"
13 : #include "GrPathRange.h"
14 :
15 0 : const GrUserStencilSettings& GrPathRendering::GetStencilPassSettings(FillType fill) {
16 0 : switch (fill) {
17 : default:
18 0 : SkFAIL("Unexpected path fill.");
19 : case GrPathRendering::kWinding_FillType: {
20 : constexpr static GrUserStencilSettings kWindingStencilPass(
21 : GrUserStencilSettings::StaticInit<
22 : 0xffff,
23 : GrUserStencilTest::kAlwaysIfInClip,
24 : 0xffff,
25 : GrUserStencilOp::kIncWrap,
26 : GrUserStencilOp::kIncWrap,
27 : 0xffff>()
28 : );
29 0 : return kWindingStencilPass;
30 : }
31 : case GrPathRendering::kEvenOdd_FillType: {
32 : constexpr static GrUserStencilSettings kEvenOddStencilPass(
33 : GrUserStencilSettings::StaticInit<
34 : 0xffff,
35 : GrUserStencilTest::kAlwaysIfInClip,
36 : 0xffff,
37 : GrUserStencilOp::kInvert,
38 : GrUserStencilOp::kInvert,
39 : 0xffff>()
40 : );
41 0 : return kEvenOddStencilPass;
42 : }
43 : }
44 : }
45 :
46 0 : class GlyphGenerator : public GrPathRange::PathGenerator {
47 : public:
48 0 : GlyphGenerator(const SkTypeface& typeface, const SkScalerContextEffects& effects,
49 : const SkDescriptor& desc)
50 0 : : fScalerContext(typeface.createScalerContext(effects, &desc))
51 : #ifdef SK_DEBUG
52 0 : , fDesc(desc.copy())
53 : #endif
54 0 : {}
55 :
56 0 : int getNumPaths() override {
57 0 : return fScalerContext->getGlyphCount();
58 : }
59 :
60 0 : void generatePath(int glyphID, SkPath* out) override {
61 0 : fScalerContext->getPath(glyphID, out);
62 0 : }
63 : #ifdef SK_DEBUG
64 0 : bool isEqualTo(const SkDescriptor& desc) const override { return *fDesc == desc; }
65 : #endif
66 : private:
67 : const std::unique_ptr<SkScalerContext> fScalerContext;
68 : #ifdef SK_DEBUG
69 : const std::unique_ptr<SkDescriptor> fDesc;
70 : #endif
71 : };
72 :
73 0 : GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface,
74 : const SkScalerContextEffects& effects,
75 : const SkDescriptor* desc,
76 : const GrStyle& style) {
77 0 : if (nullptr == typeface) {
78 0 : typeface = SkTypeface::GetDefaultTypeface();
79 0 : SkASSERT(nullptr != typeface);
80 : }
81 :
82 0 : if (desc) {
83 0 : sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, effects, *desc));
84 0 : return this->createPathRange(generator.get(), style);
85 : }
86 :
87 : SkScalerContextRec rec;
88 0 : memset(&rec, 0, sizeof(rec));
89 0 : rec.fFontID = typeface->uniqueID();
90 0 : rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
91 0 : rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
92 : // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
93 :
94 0 : SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
95 0 : SkDescriptor* genericDesc = ad.getDesc();
96 :
97 0 : genericDesc->init();
98 0 : genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
99 0 : genericDesc->computeChecksum();
100 :
101 : // No effects, so we make a dummy struct
102 0 : SkScalerContextEffects noEffects;
103 :
104 0 : sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, noEffects, *genericDesc));
105 0 : return this->createPathRange(generator.get(), style);
106 : }
|