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

Unified Diff: experimental/svg/SkSVGDevice.cpp

Issue 892973002: [SkSVGDevice] Initial shader/gradient support (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « experimental/svg/SkSVGDevice.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: experimental/svg/SkSVGDevice.cpp
diff --git a/experimental/svg/SkSVGDevice.cpp b/experimental/svg/SkSVGDevice.cpp
index 4a662e7b9470c71b8922fa30cd09a1e430bb7910..d9b5700ad1eda7b3e0e7f36eab8645e37ecac2cf 100644
--- a/experimental/svg/SkSVGDevice.cpp
+++ b/experimental/svg/SkSVGDevice.cpp
@@ -11,26 +11,185 @@
#include "SkDraw.h"
#include "SkPaint.h"
#include "SkParsePath.h"
+#include "SkShader.h"
#include "SkStream.h"
#include "SkXMLWriter.h"
namespace {
mtklein 2015/02/02 16:52:48 You can probably drop this namespace for now, give
f(malita) 2015/02/02 18:05:47 Done.
-class AutoElement {
+static SkString svg_color(SkColor color) {
+ SkString colorStr;
+ colorStr.printf("rgb(%u,%u,%u)",
+ SkColorGetR(color),
+ SkColorGetG(color),
+ SkColorGetB(color));
+ return colorStr;
+}
+
+static SkScalar svg_opacity(SkColor color) {
+ return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE;
+}
+
+}
+
+class SkSVGDevice::AutoResources {
mtklein 2015/02/02 16:52:48 : SkNoncopyable for both these?
f(malita) 2015/02/02 18:05:47 Done.
public:
- AutoElement(const char name[], SkXMLWriter* writer)
- : fWriter(writer) {
+ AutoResources(const SkPaint& paint, SkXMLWriter* writer);
+
mtklein 2015/02/02 16:52:49 Maybe dumb question... if this guy doesn't have a
f(malita) 2015/02/02 18:05:47 Yeah there is no auto dtor logic for this class, b
+ const SkString& color() const { return fColor; }
+
+private:
+ SkString addLinearGradientDef(const SkShader::GradientInfo&, const SkShader*);
+
+ SkXMLWriter* fWriter;
+ SkString fColor;
+};
+
+class SkSVGDevice::AutoElement {
+public:
+ AutoElement(const char name[], SkXMLWriter* writer, bool autoClose = true)
+ : fWriter(writer)
+ , fAutoClose(autoClose) {
fWriter->startElement(name);
}
~AutoElement() {
- fWriter->endElement();
+ if (fAutoClose) {
+ fWriter->endElement();
+ }
+ }
+
+ void addAttribute(const char name[], const char val[]) {
+ fWriter->addAttribute(name, val);
+ }
+
+ void addAttribute(const char name[], const SkString& val) {
+ fWriter->addAttribute(name, val.c_str());
+ }
+
+ void addAttribute(const char name[], int32_t val) {
+ fWriter->addS32Attribute(name, val);
+ }
+
+ void addAttribute(const char name[], SkScalar val) {
+ fWriter->addScalarAttribute(name, val);
+ }
+
+ void addPaint(const SkPaint& paint, const AutoResources& resources) {
+ SkPaint::Style style = paint.getStyle();
+ if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style) {
+ fWriter->addAttribute("fill", resources.color().c_str());
+ } else {
+ fWriter->addAttribute("fill", "none");
+ }
+
+ if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Style) {
+ fWriter->addAttribute("stroke", resources.color().c_str());
+ fWriter->addScalarAttribute("stroke-width", paint.getStrokeWidth());
+ } else {
+ fWriter->addAttribute("stroke", "none");
+ }
+
+ if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) {
+ fWriter->addScalarAttribute("opacity", svg_opacity(paint.getColor()));
+ }
+ }
+
+ void addTransform(const SkMatrix &t, const char* name = NULL) {
mtklein 2015/02/02 16:52:48 SkMatrix& ?
f(malita) 2015/02/02 18:05:47 Done.
+ if (t.isIdentity()) {
+ return;
+ }
+
+ SkString tstr;
+ switch (t.getType()) {
+ case SkMatrix::kTranslate_Mask:
+ tstr.appendf("translate(%g %g)",
+ SkScalarToFloat(t.getTranslateX()),
+ SkScalarToFloat(t.getTranslateY()));
+ break;
+ case SkMatrix::kScale_Mask:
+ tstr.appendf("scale(%g %g)",
+ SkScalarToFloat(t.getScaleX()),
+ SkScalarToFloat(t.getScaleY()));
+ break;
+ default:
mtklein 2015/02/02 16:52:48 case SkMatrix::kAffineMask: <this code> defau
f(malita) 2015/02/02 18:05:47 kAffineMask is only set when rotating/skewing so i
+ tstr.appendf("matrix(%g %g %g %g %g %g)",
+ SkScalarToFloat(t.getScaleX()), SkScalarToFloat(t.getSkewY()),
+ SkScalarToFloat(t.getSkewX()), SkScalarToFloat(t.getScaleY()),
+ SkScalarToFloat(t.getTranslateX()), SkScalarToFloat(t.getTranslateY()));
+ break;
+ }
+
+ fWriter->addAttribute(SkToBool(name) ? name : "transform", tstr.c_str());
mtklein 2015/02/02 16:52:48 Can we not just default name to "transform"?
f(malita) 2015/02/02 18:05:47 Done.
}
private:
SkXMLWriter* fWriter;
+ bool fAutoClose;
};
+SkSVGDevice::AutoResources::AutoResources(const SkPaint& paint, SkXMLWriter* writer)
+ : fWriter(writer)
+ , fColor(svg_color(paint.getColor())) {
+
+ const SkShader* shader = paint.getShader();
+ if (!SkToBool(shader)) {
+ // TODO: clip support
+ return;
+ }
+
+ SkShader::GradientInfo grInfo;
+ if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) {
+ // TODO: non-linear gradient support
+ SkDebugf("unsupported shader type\n");
+ return;
+ }
+
+ AutoElement defs("defs", fWriter);
mtklein 2015/02/02 16:52:48 It might help these standalone AutoElements to jum
f(malita) 2015/02/02 18:05:47 Done.
+
+ SkAutoSTArray<16, SkColor> grColors(grInfo.fColorCount);
+ SkAutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount);
+ grInfo.fColors = grColors.get();
+ grInfo.fColorOffsets = grOffsets.get();
+
+ SkAssertResult(SkShader::kLinear_GradientType == shader->asAGradient(&grInfo));
+ SkASSERT(grInfo.fColorCount <= grColors.count());
+ SkASSERT(grInfo.fColorCount <= grOffsets.count());
+
+ fColor.printf("url(#%s)", addLinearGradientDef(grInfo, shader).c_str());
+}
+
+SkString SkSVGDevice::AutoResources::addLinearGradientDef(const SkShader::GradientInfo& info,
+ const SkShader* shader) {
+ static uint32_t linear_gradient_counter = 0;
+
+ SkString id("linear_gradient_");
+ id.appendU32(linear_gradient_counter++);
+
+ AutoElement gradient("linearGradient", fWriter);
+ gradient.addAttribute("id", id);
+ gradient.addAttribute("gradientUnits", "userSpaceOnUse");
+ gradient.addAttribute("x1", info.fPoint[0].x());
+ gradient.addAttribute("y1", info.fPoint[0].y());
+ gradient.addAttribute("x2", info.fPoint[1].x());
+ gradient.addAttribute("y2", info.fPoint[1].y());
+ gradient.addTransform(shader->getLocalMatrix(), "gradientTransform");
+
+ SkASSERT(info.fColorCount >= 2);
+ for (int i = 0; i < info.fColorCount; ++i) {
+ SkColor color = info.fColors[i];
+ SkString colorStr(svg_color(color));
+
+ AutoElement stop("stop", fWriter);
+ stop.addAttribute("offset", info.fColorOffsets[i]);
+ stop.addAttribute("stop-color", colorStr.c_str());
+
+ if (SK_AlphaOPAQUE != SkColorGetA(color)) {
+ stop.addAttribute("stop-opacity", svg_opacity(color));
+ }
+ }
+
+ return id;
}
SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkWStream* wstream) {
@@ -42,24 +201,43 @@ SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkWStream* wstream) {
}
SkSVGDevice::SkSVGDevice(const SkISize& size, SkWStream* wstream)
- : fWriter(SkNEW_ARGS(SkXMLStreamWriter, (wstream))) {
+ : fWriter(SkNEW_ARGS(SkXMLStreamWriter, (wstream)))
+ , fElementStack(0) {
fLegacyBitmap.setInfo(SkImageInfo::MakeUnknown(size.width(), size.height()));
fWriter->writeHeader();
- fWriter->startElement("svg");
- fWriter->addAttribute("xmlns", "http://www.w3.org/2000/svg");
- fWriter->addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
- fWriter->addS32Attribute("width", size.width());
- fWriter->addS32Attribute("height", size.height());
+
+
+ AutoElement elem("svg", fWriter, false);
mtklein 2015/02/02 16:52:49 Probably worth a note to point out how this tag ge
f(malita) 2015/02/02 18:05:47 Done.
+ elem.addAttribute("xmlns", "http://www.w3.org/2000/svg");
+ elem.addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+ elem.addAttribute("width", size.width());
+ elem.addAttribute("height", size.height());
+
+ this->pushElement(elem);
mtklein 2015/02/02 16:52:48 Seems like we really don't need an explicit stack
f(malita) 2015/02/02 18:05:47 That was my first thought also, but gave up initia
}
SkSVGDevice::~SkSVGDevice() {
- fWriter->endElement();
+ while (fElementStack > 0) {
+ popElement();
mtklein 2015/02/02 16:52:48 this->
f(malita) 2015/02/02 18:05:47 removed
+ }
+
fWriter->flush();
SkDELETE(fWriter);
}
+void SkSVGDevice::pushElement(const AutoElement&) {
mtklein 2015/02/02 16:52:48 Might want to assert the AutoElement isn't auto-cl
f(malita) 2015/02/02 18:05:47 removed
+ fElementStack++;
+}
+
+void SkSVGDevice::popElement() {
+ SkASSERT(fElementStack > 0);
+
+ fWriter->endElement();
+ fElementStack--;
+}
+
SkImageInfo SkSVGDevice::imageInfo() const {
return fLegacyBitmap.info();
}
@@ -68,109 +246,104 @@ const SkBitmap& SkSVGDevice::onAccessBitmap() {
return fLegacyBitmap;
}
-void SkSVGDevice::addPaint(const SkPaint& paint) {
- SkColor color = paint.getColor();
- SkString colorStr;
- colorStr.appendf("rgb(%u,%u,%u)", SkColorGetR(color), SkColorGetG(color), SkColorGetB(color));
-
- SkPaint::Style style = paint.getStyle();
- if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style) {
- fWriter->addAttribute("fill", colorStr.c_str());
- } else {
- fWriter->addAttribute("fill", "none");
- }
-
- if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Style) {
- fWriter->addAttribute("stroke", colorStr.c_str());
- fWriter->addScalarAttribute("stroke-width", paint.getStrokeWidth());
- } else {
- fWriter->addAttribute("stroke", "none");
- }
-}
-
-void SkSVGDevice::addTransform(const SkMatrix &t) {
- if (t.isIdentity()) {
- return;
- }
+void SkSVGDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
+ AutoResources resources(paint, fWriter);
+ AutoElement rect("rect", fWriter);
- SkString tstr;
- tstr.appendf("matrix(%g %g %g %g %g %g)",
- SkScalarToFloat(t.getScaleX()), SkScalarToFloat(t.getSkewY()),
- SkScalarToFloat(t.getSkewX()), SkScalarToFloat(t.getScaleY()),
- SkScalarToFloat(t.getTranslateX()), SkScalarToFloat(t.getTranslateY()));
- fWriter->addAttribute("transform", tstr.c_str());
-}
+ rect.addAttribute("x", 0);
+ rect.addAttribute("y", 0);
+ rect.addAttribute("width", this->width());
+ rect.addAttribute("height", this->height());
-void SkSVGDevice::drawPaint(const SkDraw&, const SkPaint& paint) {
- // todo
+ rect.addPaint(paint, resources);
+ rect.addTransform(*draw.fMatrix);
}
void SkSVGDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
const SkPoint[], const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawPoints()");
}
void SkSVGDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) {
- AutoElement elem("rect", fWriter);
+ AutoResources resources(paint, fWriter);
+ AutoElement rect("rect", fWriter);
- fWriter->addScalarAttribute("x", r.fLeft);
- fWriter->addScalarAttribute("y", r.fTop);
- fWriter->addScalarAttribute("width", r.width());
- fWriter->addScalarAttribute("height", r.height());
+ rect.addAttribute("x", r.fLeft);
+ rect.addAttribute("y", r.fTop);
+ rect.addAttribute("width", r.width());
+ rect.addAttribute("height", r.height());
- this->addPaint(paint);
- this->addTransform(*draw.fMatrix);
+ rect.addPaint(paint, resources);
+ rect.addTransform(*draw.fMatrix);
}
-void SkSVGDevice::drawOval(const SkDraw&, const SkRect& oval, const SkPaint& paint) {
- // todo
+void SkSVGDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
+ AutoResources resources(paint, fWriter);
+ AutoElement ellipse("ellipse", fWriter);
+
+ ellipse.addAttribute("cx", oval.centerX());
+ ellipse.addAttribute("cy", oval.centerY());
+ ellipse.addAttribute("rx", oval.width() / 2);
+ ellipse.addAttribute("ry", oval.height() / 2);
+
+ ellipse.addPaint(paint, resources);
+ ellipse.addTransform(*draw.fMatrix);
}
void SkSVGDevice::drawRRect(const SkDraw&, const SkRRect& rr, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawRRect()");
}
void SkSVGDevice::drawPath(const SkDraw& draw, const SkPath& path, const SkPaint& paint,
const SkMatrix* prePathMatrix, bool pathIsMutable) {
+ AutoResources resources(paint, fWriter);
AutoElement elem("path", fWriter);
SkString pathStr;
SkParsePath::ToSVGString(path, &pathStr);
- fWriter->addAttribute("d", pathStr.c_str());
+ elem.addAttribute("d", pathStr.c_str());
- this->addPaint(paint);
- this->addTransform(*draw.fMatrix);
+ elem.addPaint(paint, resources);
+ elem.addTransform(*draw.fMatrix);
}
void SkSVGDevice::drawBitmap(const SkDraw&, const SkBitmap& bitmap,
const SkMatrix& matrix, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawBitmap()");
}
void SkSVGDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
int x, int y, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawSprite()");
}
void SkSVGDevice::drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect* srcOrNull,
const SkRect& dst, const SkPaint& paint,
SkCanvas::DrawBitmapRectFlags flags) {
// todo
+ SkDebugf("unsupported operation: drawBitmapRect()");
}
void SkSVGDevice::drawText(const SkDraw&, const void* text, size_t len,
SkScalar x, SkScalar y, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawText()");
}
void SkSVGDevice::drawPosText(const SkDraw&, const void* text, size_t len,const SkScalar pos[],
int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawPosText()");
}
void SkSVGDevice::drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath& path,
const SkMatrix* matrix, const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawTextOnPath()");
}
void SkSVGDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
@@ -179,9 +352,11 @@ void SkSVGDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCo
const uint16_t indices[], int indexCount,
const SkPaint& paint) {
// todo
+ SkDebugf("unsupported operation: drawVertices()");
}
void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
const SkPaint&) {
// todo
+ SkDebugf("unsupported operation: drawDevice()");
}
« no previous file with comments | « experimental/svg/SkSVGDevice.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698