[svgdevice] Add support for color shaders

Handle kColor_GradientType (which is returned by asGradient
for color shaders) as a straight color paint server.

Bug: skia:12622
Change-Id: I46fa21ed23a7824d67fc0460f92d649743e1b1f2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/469900
Commit-Queue: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@google.com>
Reviewed-by: Tyler Denniston <tdenniston@google.com>
This commit is contained in:
Florin Malita 2021-11-10 12:37:44 -05:00 committed by SkCQ
parent bdfe3b6a2e
commit a8888cff15
2 changed files with 29 additions and 3 deletions

View File

@ -404,8 +404,11 @@ void SkSVGDevice::AutoElement::addGradientShaderResources(const SkShader* shader
Resources* resources) { Resources* resources) {
SkShader::GradientInfo grInfo; SkShader::GradientInfo grInfo;
memset(&grInfo, 0, sizeof(grInfo)); memset(&grInfo, 0, sizeof(grInfo));
if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) { const auto gradient_type = shader->asAGradient(&grInfo);
// TODO: non-linear gradient support
if (gradient_type != SkShader::kColor_GradientType &&
gradient_type != SkShader::kLinear_GradientType) {
// TODO: other gradient support
return; return;
} }
@ -419,7 +422,10 @@ void SkSVGDevice::AutoElement::addGradientShaderResources(const SkShader* shader
SkASSERT(grInfo.fColorCount <= grColors.count()); SkASSERT(grInfo.fColorCount <= grColors.count());
SkASSERT(grInfo.fColorCount <= grOffsets.count()); SkASSERT(grInfo.fColorCount <= grOffsets.count());
resources->fPaintServer.printf("url(#%s)", addLinearGradientDef(grInfo, shader).c_str()); SkASSERT(grColors.size() > 0);
resources->fPaintServer = gradient_type == SkShader::kColor_GradientType
? svg_color(grColors[0])
: SkStringPrintf("url(#%s)", addLinearGradientDef(grInfo, shader).c_str());
} }
void SkSVGDevice::AutoElement::addColorFilterResources(const SkColorFilter& cf, void SkSVGDevice::AutoElement::addColorFilterResources(const SkColorFilter& cf,

View File

@ -588,4 +588,24 @@ DEF_TEST(SVGDevice_relative_path_encoding, reporter) {
REPORTER_ASSERT(reporter, !strcmp(d, "m100 50l100 0l0 100l-100 -100Z")); REPORTER_ASSERT(reporter, !strcmp(d, "m100 50l100 0l0 100l-100 -100Z"));
} }
DEF_TEST(SVGDevice_color_shader, reporter) {
SkDOM dom;
{
auto svgCanvas = MakeDOMCanvas(&dom);
SkPaint paint;
paint.setShader(SkShaders::Color(0xffffff00));
svgCanvas->drawCircle(100, 100, 100, paint);
}
const auto* rootElement = dom.finishParsing();
REPORTER_ASSERT(reporter, rootElement, "root element not found");
const auto* ellipseElement = dom.getFirstChild(rootElement, "ellipse");
REPORTER_ASSERT(reporter, ellipseElement, "ellipse element not found");
const auto* fill = dom.findAttr(ellipseElement, "fill");
REPORTER_ASSERT(reporter, fill, "fill attribute not found");
REPORTER_ASSERT(reporter, !strcmp(fill, "yellow"));
}
#endif #endif