Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: experimental/svg/model/SkSVGRenderContext.cpp

Issue 2327233003: [SVGDom] Initial linear gradient support (Closed)
Patch Set: review Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/svg/model/SkSVGRenderContext.h ('k') | experimental/svg/model/SkSVGStop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCanvas.h" 8 #include "SkCanvas.h"
9 #include "SkSVGAttribute.h" 9 #include "SkSVGAttribute.h"
10 #include "SkSVGNode.h"
10 #include "SkSVGRenderContext.h" 11 #include "SkSVGRenderContext.h"
11 #include "SkSVGTypes.h" 12 #include "SkSVGTypes.h"
12 13
13 namespace { 14 namespace {
14 15
15 SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::Length Type t) { 16 SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::Length Type t) {
16 switch (t) { 17 switch (t) {
17 case SkSVGLengthContext::LengthType::kHorizontal: 18 case SkSVGLengthContext::LengthType::kHorizontal:
18 return viewport.width(); 19 return viewport.width();
19 case SkSVGLengthContext::LengthType::kVertical: 20 case SkSVGLengthContext::LengthType::kVertical:
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 case SkSVGLineJoin::Type::kRound: 92 case SkSVGLineJoin::Type::kRound:
92 return SkPaint::kRound_Join; 93 return SkPaint::kRound_Join;
93 case SkSVGLineJoin::Type::kBevel: 94 case SkSVGLineJoin::Type::kBevel:
94 return SkPaint::kBevel_Join; 95 return SkPaint::kBevel_Join;
95 default: 96 default:
96 SkASSERT(false); 97 SkASSERT(false);
97 return SkPaint::kMiter_Join; 98 return SkPaint::kMiter_Join;
98 } 99 }
99 } 100 }
100 101
101 void applySvgPaint(const SkSVGPaint& svgPaint, SkPaint* p) { 102 void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, Sk Paint* p) {
102 switch (svgPaint.type()) { 103 switch (svgPaint.type()) {
103 case SkSVGPaint::Type::kColor: 104 case SkSVGPaint::Type::kColor:
104 p->setColor(SkColorSetA(svgPaint.color(), p->getAlpha())); 105 p->setColor(SkColorSetA(svgPaint.color(), p->getAlpha()));
105 break; 106 break;
107 case SkSVGPaint::Type::kIRI: {
108 const auto* node = ctx.findNodeById(svgPaint.iri());
109 if (!node || !node->asPaint(ctx, p)) {
110 p->setColor(SK_ColorTRANSPARENT);
111 }
112 break;
113 }
106 case SkSVGPaint::Type::kCurrentColor: 114 case SkSVGPaint::Type::kCurrentColor:
107 SkDebugf("unimplemented 'currentColor' paint type"); 115 SkDebugf("unimplemented 'currentColor' paint type");
108 // Fall through. 116 // Fall through.
109 case SkSVGPaint::Type::kNone: 117 case SkSVGPaint::Type::kNone:
110 // Fall through. 118 // Fall through.
111 case SkSVGPaint::Type::kInherit: 119 case SkSVGPaint::Type::kInherit:
112 break; 120 break;
113 } 121 }
114 } 122 }
115 123
116 inline uint8_t opacity_to_alpha(SkScalar o) { 124 inline uint8_t opacity_to_alpha(SkScalar o) {
117 return SkTo<uint8_t>(SkScalarRoundToInt(o * 255)); 125 return SkTo<uint8_t>(SkScalarRoundToInt(o * 255));
118 } 126 }
119 127
120 // Commit the selected attribute to the paint cache. 128 // Commit the selected attribute to the paint cache.
121 template <SkSVGAttribute> 129 template <SkSVGAttribute>
122 void commitToPaint(const SkSVGPresentationAttributes&, 130 void commitToPaint(const SkSVGPresentationAttributes&,
123 const SkSVGLengthContext&, 131 const SkSVGRenderContext&,
124 SkSVGPresentationContext*); 132 SkSVGPresentationContext*);
125 133
126 template <> 134 template <>
127 void commitToPaint<SkSVGAttribute::kFill>(const SkSVGPresentationAttributes& att rs, 135 void commitToPaint<SkSVGAttribute::kFill>(const SkSVGPresentationAttributes& att rs,
128 const SkSVGLengthContext&, 136 const SkSVGRenderContext& ctx,
129 SkSVGPresentationContext* pctx) { 137 SkSVGPresentationContext* pctx) {
130 applySvgPaint(*attrs.fFill.get(), &pctx->fFillPaint); 138 applySvgPaint(ctx, *attrs.fFill.get(), &pctx->fFillPaint);
131 } 139 }
132 140
133 template <> 141 template <>
134 void commitToPaint<SkSVGAttribute::kStroke>(const SkSVGPresentationAttributes& a ttrs, 142 void commitToPaint<SkSVGAttribute::kStroke>(const SkSVGPresentationAttributes& a ttrs,
135 const SkSVGLengthContext&, 143 const SkSVGRenderContext& ctx,
136 SkSVGPresentationContext* pctx) { 144 SkSVGPresentationContext* pctx) {
137 applySvgPaint(*attrs.fStroke.get(), &pctx->fStrokePaint); 145 applySvgPaint(ctx, *attrs.fStroke.get(), &pctx->fStrokePaint);
138 } 146 }
139 147
140 template <> 148 template <>
141 void commitToPaint<SkSVGAttribute::kFillOpacity>(const SkSVGPresentationAttribut es& attrs, 149 void commitToPaint<SkSVGAttribute::kFillOpacity>(const SkSVGPresentationAttribut es& attrs,
142 const SkSVGLengthContext&, 150 const SkSVGRenderContext&,
143 SkSVGPresentationContext* pctx) { 151 SkSVGPresentationContext* pctx) {
144 pctx->fFillPaint.setAlpha(opacity_to_alpha(*attrs.fFillOpacity.get())); 152 pctx->fFillPaint.setAlpha(opacity_to_alpha(*attrs.fFillOpacity.get()));
145 } 153 }
146 154
147 template <> 155 template <>
148 void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttrib utes& attrs, 156 void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttrib utes& attrs,
149 const SkSVGLengthContext&, 157 const SkSVGRenderContext&,
150 SkSVGPresentationContext* pct x) { 158 SkSVGPresentationContext* pct x) {
151 const auto& cap = *attrs.fStrokeLineCap.get(); 159 const auto& cap = *attrs.fStrokeLineCap.get();
152 if (cap.type() != SkSVGLineCap::Type::kInherit) { 160 if (cap.type() != SkSVGLineCap::Type::kInherit) {
153 pctx->fStrokePaint.setStrokeCap(toSkCap(cap)); 161 pctx->fStrokePaint.setStrokeCap(toSkCap(cap));
154 } 162 }
155 } 163 }
156 164
157 template <> 165 template <>
158 void commitToPaint<SkSVGAttribute::kStrokeLineJoin>(const SkSVGPresentationAttri butes& attrs, 166 void commitToPaint<SkSVGAttribute::kStrokeLineJoin>(const SkSVGPresentationAttri butes& attrs,
159 const SkSVGLengthContext&, 167 const SkSVGRenderContext&,
160 SkSVGPresentationContext* pc tx) { 168 SkSVGPresentationContext* pc tx) {
161 const auto& join = *attrs.fStrokeLineJoin.get(); 169 const auto& join = *attrs.fStrokeLineJoin.get();
162 if (join.type() != SkSVGLineJoin::Type::kInherit) { 170 if (join.type() != SkSVGLineJoin::Type::kInherit) {
163 pctx->fStrokePaint.setStrokeJoin(toSkJoin(join)); 171 pctx->fStrokePaint.setStrokeJoin(toSkJoin(join));
164 } 172 }
165 } 173 }
166 174
167 template <> 175 template <>
168 void commitToPaint<SkSVGAttribute::kStrokeOpacity>(const SkSVGPresentationAttrib utes& attrs, 176 void commitToPaint<SkSVGAttribute::kStrokeOpacity>(const SkSVGPresentationAttrib utes& attrs,
169 const SkSVGLengthContext&, 177 const SkSVGRenderContext&,
170 SkSVGPresentationContext* pct x) { 178 SkSVGPresentationContext* pct x) {
171 pctx->fStrokePaint.setAlpha(opacity_to_alpha(*attrs.fStrokeOpacity.get())); 179 pctx->fStrokePaint.setAlpha(opacity_to_alpha(*attrs.fStrokeOpacity.get()));
172 } 180 }
173 181
174 template <> 182 template <>
175 void commitToPaint<SkSVGAttribute::kStrokeWidth>(const SkSVGPresentationAttribut es& attrs, 183 void commitToPaint<SkSVGAttribute::kStrokeWidth>(const SkSVGPresentationAttribut es& attrs,
176 const SkSVGLengthContext& lctx, 184 const SkSVGRenderContext& ctx,
177 SkSVGPresentationContext* pctx) { 185 SkSVGPresentationContext* pctx) {
178 auto strokeWidth = lctx.resolve(*attrs.fStrokeWidth.get(), 186 auto strokeWidth = ctx.lengthContext().resolve(*attrs.fStrokeWidth.get(),
179 SkSVGLengthContext::LengthType::kOther); 187 SkSVGLengthContext::LengthTyp e::kOther);
180 pctx->fStrokePaint.setStrokeWidth(strokeWidth); 188 pctx->fStrokePaint.setStrokeWidth(strokeWidth);
181 } 189 }
182 190
183 } // anonymous ns 191 } // anonymous ns
184 192
185 SkSVGPresentationContext::SkSVGPresentationContext() 193 SkSVGPresentationContext::SkSVGPresentationContext()
186 : fInherited(SkSVGPresentationAttributes::MakeInitial()) { 194 : fInherited(SkSVGPresentationAttributes::MakeInitial()) {
187 195
188 fFillPaint.setStyle(SkPaint::kFill_Style); 196 fFillPaint.setStyle(SkPaint::kFill_Style);
189 fStrokePaint.setStyle(SkPaint::kStroke_Style); 197 fStrokePaint.setStyle(SkPaint::kStroke_Style);
190 198
191 // TODO: drive AA off presentation attrs also (shape-rendering?) 199 // TODO: drive AA off presentation attrs also (shape-rendering?)
192 fFillPaint.setAntiAlias(true); 200 fFillPaint.setAntiAlias(true);
193 fStrokePaint.setAntiAlias(true); 201 fStrokePaint.setAntiAlias(true);
194 202
195 // Commit initial values to the paint cache. 203 // Commit initial values to the paint cache.
196 SkSVGLengthContext dummy(SkSize::Make(0, 0)); 204 SkCanvas dummyCanvas(0, 0);
205 SkSVGRenderContext dummy(&dummyCanvas, SkSVGIDMapper(), SkSVGLengthContext(S kSize::Make(0, 0)),
206 *this);
207
197 commitToPaint<SkSVGAttribute::kFill>(fInherited, dummy, this); 208 commitToPaint<SkSVGAttribute::kFill>(fInherited, dummy, this);
198 commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, dummy, this); 209 commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, dummy, this);
199 commitToPaint<SkSVGAttribute::kStroke>(fInherited, dummy, this); 210 commitToPaint<SkSVGAttribute::kStroke>(fInherited, dummy, this);
200 commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, dummy, this); 211 commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, dummy, this);
201 commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, dummy, this); 212 commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, dummy, this);
202 commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, dummy, this); 213 commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, dummy, this);
203 commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, dummy, this); 214 commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, dummy, this);
204 } 215 }
205 216
206 SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas, 217 SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas,
218 const SkSVGIDMapper& mapper,
207 const SkSVGLengthContext& lctx, 219 const SkSVGLengthContext& lctx,
208 const SkSVGPresentationContext& pctx) 220 const SkSVGPresentationContext& pctx)
209 : fLengthContext(lctx) 221 : fIDMapper(mapper)
222 , fLengthContext(lctx)
210 , fPresentationContext(pctx) 223 , fPresentationContext(pctx)
211 , fCanvas(canvas) 224 , fCanvas(canvas)
212 , fCanvasSaveCount(canvas->getSaveCount()) {} 225 , fCanvasSaveCount(canvas->getSaveCount()) {}
213 226
214 SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other) 227 SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other)
215 : SkSVGRenderContext(other.fCanvas, 228 : SkSVGRenderContext(other.fCanvas,
229 other.fIDMapper,
216 *other.fLengthContext, 230 *other.fLengthContext,
217 *other.fPresentationContext) {} 231 *other.fPresentationContext) {}
218 232
219 SkSVGRenderContext::~SkSVGRenderContext() { 233 SkSVGRenderContext::~SkSVGRenderContext() {
220 fCanvas->restoreToCount(fCanvasSaveCount); 234 fCanvas->restoreToCount(fCanvasSaveCount);
221 } 235 }
222 236
237 const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const {
238 const auto* v = fIDMapper.find(id);
239 return v ? v->get() : nullptr;
240 }
241
223 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr ibutes& attrs) { 242 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr ibutes& attrs) {
224 243
225 #define ApplyLazyInheritedAttribute(ATTR) \ 244 #define ApplyLazyInheritedAttribute(ATTR) \
226 do { \ 245 do { \
227 /* All attributes should be defined on the inherited context. */ \ 246 /* All attributes should be defined on the inherited context. */ \
228 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \ 247 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \
229 const auto* value = attrs.f ## ATTR.getMaybeNull(); \ 248 const auto* value = attrs.f ## ATTR.getMaybeNull(); \
230 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get() ) { \ 249 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get() ) { \
231 /* Update the local attribute value */ \ 250 /* Update the local attribute value */ \
232 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \ 251 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \
233 /* Update the cached paints */ \ 252 /* Update the cached paints */ \
234 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *fLengthContext, \ 253 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \
235 fPresentationContext.writab le()); \ 254 fPresentationContext.writab le()); \
236 } \ 255 } \
237 } while (false) 256 } while (false)
238 257
239 ApplyLazyInheritedAttribute(Fill); 258 ApplyLazyInheritedAttribute(Fill);
240 ApplyLazyInheritedAttribute(FillOpacity); 259 ApplyLazyInheritedAttribute(FillOpacity);
241 ApplyLazyInheritedAttribute(Stroke); 260 ApplyLazyInheritedAttribute(Stroke);
242 ApplyLazyInheritedAttribute(StrokeLineCap); 261 ApplyLazyInheritedAttribute(StrokeLineCap);
243 ApplyLazyInheritedAttribute(StrokeLineJoin); 262 ApplyLazyInheritedAttribute(StrokeLineJoin);
244 ApplyLazyInheritedAttribute(StrokeOpacity); 263 ApplyLazyInheritedAttribute(StrokeOpacity);
(...skipping 13 matching lines...) Expand all
258 277
259 const SkPaint* SkSVGRenderContext::fillPaint() const { 278 const SkPaint* SkSVGRenderContext::fillPaint() const {
260 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge t()->type(); 279 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge t()->type();
261 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa int : nullptr; 280 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa int : nullptr;
262 } 281 }
263 282
264 const SkPaint* SkSVGRenderContext::strokePaint() const { 283 const SkPaint* SkSVGRenderContext::strokePaint() const {
265 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke. get()->type(); 284 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke. get()->type();
266 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke Paint : nullptr; 285 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke Paint : nullptr;
267 } 286 }
OLDNEW
« no previous file with comments | « experimental/svg/model/SkSVGRenderContext.h ('k') | experimental/svg/model/SkSVGStop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698