From ba25cb80eb91a3e6efb03112cbf5423d0a812be7 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Wed, 27 Oct 2021 02:55:22 +0200 Subject: [PATCH] svg_loader: shape's bounds without a stroke A shape's bounds used for a gradient transformation shouldn't take a stroke into account. --- src/loaders/svg/tvgSvgSceneBuilder.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/loaders/svg/tvgSvgSceneBuilder.cpp b/src/loaders/svg/tvgSvgSceneBuilder.cpp index fe7a2db5..dd9a2eca 100644 --- a/src/loaders/svg/tvgSvgSceneBuilder.cpp +++ b/src/loaders/svg/tvgSvgSceneBuilder.cpp @@ -271,7 +271,17 @@ static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float v if (style->fill.paint.none) { //Do nothing } else if (style->fill.paint.gradient) { - if (!style->fill.paint.gradient->userSpace) vg->bounds(&vx, &vy, &vw, &vh, false); + if (!style->fill.paint.gradient->userSpace) { + vg->bounds(&vx, &vy, &vw, &vh, false); + //According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph) + //a stroke width should be ignored for bounding box calculations + if (auto strokeHalfW = 0.5f * vg->strokeWidth()) { + vx += strokeHalfW; + vy += strokeHalfW; + vw -= strokeHalfW; + vh -= strokeHalfW; + } + } if (style->fill.paint.gradient->type == SvgGradientType::Linear) { auto linear = _applyLinearGradientProperty(style->fill.paint.gradient, vg, vx, vy, vw, vh, style->fill.opacity); @@ -310,7 +320,17 @@ static void _applyProperty(SvgNode* node, Shape* vg, float vx, float vy, float v if (style->stroke.paint.none) { //Do nothing } else if (style->stroke.paint.gradient) { - if (!style->stroke.paint.gradient->userSpace) vg->bounds(&vx, &vy, &vw, &vh, false); + if (!style->stroke.paint.gradient->userSpace) { + //According to: https://www.w3.org/TR/SVG11/coords.html#ObjectBoundingBoxUnits (the last paragraph) + //a stroke width should be ignored for bounding box calculations + vg->bounds(&vx, &vy, &vw, &vh, false); + if (auto strokeHalfW = 0.5f * vg->strokeWidth()) { + vx += strokeHalfW; + vy += strokeHalfW; + vw -= strokeHalfW; + vh -= strokeHalfW; + } + } if (style->stroke.paint.gradient->type == SvgGradientType::Linear) { auto linear = _applyLinearGradientProperty(style->stroke.paint.gradient, vg, vx, vy, vw, vh, style->stroke.opacity);