Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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); |
|
robertphillips
2016/09/12 19:31:52
overlength line ?
f(malita)
2016/09/12 19:52:09
Done.
| |
| 205 SkSVGRenderContext dummy(&dummyCanvas, SkSVGIDMapper(), SkSVGLengthContext(S kSize::Make(0, 0)), *this); | |
| 206 | |
| 197 commitToPaint<SkSVGAttribute::kFill>(fInherited, dummy, this); | 207 commitToPaint<SkSVGAttribute::kFill>(fInherited, dummy, this); |
| 198 commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, dummy, this); | 208 commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, dummy, this); |
| 199 commitToPaint<SkSVGAttribute::kStroke>(fInherited, dummy, this); | 209 commitToPaint<SkSVGAttribute::kStroke>(fInherited, dummy, this); |
| 200 commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, dummy, this); | 210 commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, dummy, this); |
| 201 commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, dummy, this); | 211 commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, dummy, this); |
| 202 commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, dummy, this); | 212 commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, dummy, this); |
| 203 commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, dummy, this); | 213 commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, dummy, this); |
| 204 } | 214 } |
| 205 | 215 |
| 206 SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas, | 216 SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas, |
| 217 const SkSVGIDMapper& mapper, | |
| 207 const SkSVGLengthContext& lctx, | 218 const SkSVGLengthContext& lctx, |
| 208 const SkSVGPresentationContext& pctx) | 219 const SkSVGPresentationContext& pctx) |
| 209 : fLengthContext(lctx) | 220 : fIDMapper(mapper) |
| 221 , fLengthContext(lctx) | |
| 210 , fPresentationContext(pctx) | 222 , fPresentationContext(pctx) |
| 211 , fCanvas(canvas) | 223 , fCanvas(canvas) |
| 212 , fCanvasSaveCount(canvas->getSaveCount()) {} | 224 , fCanvasSaveCount(canvas->getSaveCount()) {} |
| 213 | 225 |
| 214 SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other) | 226 SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other) |
| 215 : SkSVGRenderContext(other.fCanvas, | 227 : SkSVGRenderContext(other.fCanvas, |
| 228 other.fIDMapper, | |
| 216 *other.fLengthContext, | 229 *other.fLengthContext, |
| 217 *other.fPresentationContext) {} | 230 *other.fPresentationContext) {} |
| 218 | 231 |
| 219 SkSVGRenderContext::~SkSVGRenderContext() { | 232 SkSVGRenderContext::~SkSVGRenderContext() { |
| 220 fCanvas->restoreToCount(fCanvasSaveCount); | 233 fCanvas->restoreToCount(fCanvasSaveCount); |
| 221 } | 234 } |
| 222 | 235 |
| 236 const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const { | |
| 237 const auto* v = fIDMapper.find(id); | |
| 238 return v ? v->get() : nullptr; | |
| 239 } | |
| 240 | |
| 223 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr ibutes& attrs) { | 241 void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr ibutes& attrs) { |
| 224 | 242 |
| 225 #define ApplyLazyInheritedAttribute(ATTR) \ | 243 #define ApplyLazyInheritedAttribute(ATTR) \ |
| 226 do { \ | 244 do { \ |
| 227 /* All attributes should be defined on the inherited context. */ \ | 245 /* All attributes should be defined on the inherited context. */ \ |
| 228 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \ | 246 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \ |
| 229 const auto* value = attrs.f ## ATTR.getMaybeNull(); \ | 247 const auto* value = attrs.f ## ATTR.getMaybeNull(); \ |
| 230 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get() ) { \ | 248 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get() ) { \ |
| 231 /* Update the local attribute value */ \ | 249 /* Update the local attribute value */ \ |
| 232 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \ | 250 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \ |
| 233 /* Update the cached paints */ \ | 251 /* Update the cached paints */ \ |
| 234 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *fLengthContext, \ | 252 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \ |
| 235 fPresentationContext.writab le()); \ | 253 fPresentationContext.writab le()); \ |
| 236 } \ | 254 } \ |
| 237 } while (false) | 255 } while (false) |
| 238 | 256 |
| 239 ApplyLazyInheritedAttribute(Fill); | 257 ApplyLazyInheritedAttribute(Fill); |
| 240 ApplyLazyInheritedAttribute(FillOpacity); | 258 ApplyLazyInheritedAttribute(FillOpacity); |
| 241 ApplyLazyInheritedAttribute(Stroke); | 259 ApplyLazyInheritedAttribute(Stroke); |
| 242 ApplyLazyInheritedAttribute(StrokeLineCap); | 260 ApplyLazyInheritedAttribute(StrokeLineCap); |
| 243 ApplyLazyInheritedAttribute(StrokeLineJoin); | 261 ApplyLazyInheritedAttribute(StrokeLineJoin); |
| 244 ApplyLazyInheritedAttribute(StrokeOpacity); | 262 ApplyLazyInheritedAttribute(StrokeOpacity); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 258 | 276 |
| 259 const SkPaint* SkSVGRenderContext::fillPaint() const { | 277 const SkPaint* SkSVGRenderContext::fillPaint() const { |
| 260 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge t()->type(); | 278 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.ge t()->type(); |
| 261 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa int : nullptr; | 279 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPa int : nullptr; |
| 262 } | 280 } |
| 263 | 281 |
| 264 const SkPaint* SkSVGRenderContext::strokePaint() const { | 282 const SkPaint* SkSVGRenderContext::strokePaint() const { |
| 265 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke. get()->type(); | 283 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke. get()->type(); |
| 266 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke Paint : nullptr; | 284 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStroke Paint : nullptr; |
| 267 } | 285 } |
| OLD | NEW |