From 02cad7cdc3ff15378f55418ff18b8494fd965396 Mon Sep 17 00:00:00 2001 From: Hparty <420024556@qq.com> Date: Wed, 8 Jan 2025 17:06:29 +0800 Subject: [PATCH] Fix the issue where StrokeWidth is affected by the Matrix before applying Stroke to a Shape. (#424) --- src/core/shapes/StrokeShape.cpp | 4 +++- test/baseline/version.json | 1 + test/src/CanvasTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/core/shapes/StrokeShape.cpp b/src/core/shapes/StrokeShape.cpp index 5b076f45..7011ce11 100644 --- a/src/core/shapes/StrokeShape.cpp +++ b/src/core/shapes/StrokeShape.cpp @@ -18,6 +18,7 @@ #include "StrokeShape.h" #include "core/shapes/MatrixShape.h" +#include "core/utils/Log.h" #include "core/utils/UniqueID.h" namespace tgfx { @@ -40,7 +41,8 @@ std::shared_ptr Shape::ApplyStroke(std::shared_ptr shape, const St return std::make_shared(std::move(shape), *stroke); } auto scaleStroke = *stroke; - scaleStroke.width *= scales.x; + DEBUG_ASSERT(scales.x != 0); + scaleStroke.width /= scales.x; shape = std::make_shared(matrixShape->shape, scaleStroke); return std::make_shared(std::move(shape), matrixShape->matrix); } diff --git a/test/baseline/version.json b/test/baseline/version.json index 7ceef311..93e9451d 100644 --- a/test/baseline/version.json +++ b/test/baseline/version.json @@ -24,6 +24,7 @@ "PictureImage": "c28a93c", "PictureImage_Path": "2212c4e", "PictureImage_Text": "b062b9a", + "StrokeShape": "a0a5068", "TileModeFallback": "af2e3ff", "YUVImage": "bc64712", "YUVImage_RGBAA": "bc64712", diff --git a/test/src/CanvasTest.cpp b/test/src/CanvasTest.cpp index 2e1ad9d9..77768259 100644 --- a/test/src/CanvasTest.cpp +++ b/test/src/CanvasTest.cpp @@ -1517,4 +1517,24 @@ TGFX_TEST(CanvasTest, DrawPathProvider) { EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/DrawPathProvider")); } + +TGFX_TEST(CanvasTest, StrokeShape) { + ContextScope scope; + auto context = scope.getContext(); + EXPECT_TRUE(context != nullptr); + auto surface = Surface::Make(context, 400, 200); + auto canvas = surface->getCanvas(); + auto path = Path(); + path.addRect(Rect::MakeXYWH(10, 10, 50, 50)); + auto shape = Shape::MakeFrom(path); + Matrix matrix = Matrix::MakeScale(2.0, 2.0); + shape = Shape::ApplyMatrix(shape, matrix); + Stroke stroke(10); + shape = Shape::ApplyStroke(shape, &stroke); + canvas->drawShape(shape, Paint()); + shape = Shape::ApplyMatrix(shape, Matrix::MakeScale(0.2f, 0.6f)); + canvas->translate(150, 0); + canvas->drawShape(shape, Paint()); + EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/StrokeShape")); +} } // namespace tgfx