Skip to content

Commit

Permalink
Fix the issue where StrokeWidth is affected by the Matrix before appl…
Browse files Browse the repository at this point in the history
…ying Stroke to a Shape. (#424)
  • Loading branch information
Hparty authored Jan 8, 2025
1 parent 7a9c80a commit 02cad7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/shapes/StrokeShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "StrokeShape.h"
#include "core/shapes/MatrixShape.h"
#include "core/utils/Log.h"
#include "core/utils/UniqueID.h"

namespace tgfx {
Expand All @@ -40,7 +41,8 @@ std::shared_ptr<Shape> Shape::ApplyStroke(std::shared_ptr<Shape> shape, const St
return std::make_shared<StrokeShape>(std::move(shape), *stroke);
}
auto scaleStroke = *stroke;
scaleStroke.width *= scales.x;
DEBUG_ASSERT(scales.x != 0);
scaleStroke.width /= scales.x;
shape = std::make_shared<StrokeShape>(matrixShape->shape, scaleStroke);
return std::make_shared<MatrixShape>(std::move(shape), matrixShape->matrix);
}
Expand Down
1 change: 1 addition & 0 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"PictureImage": "c28a93c",
"PictureImage_Path": "2212c4e",
"PictureImage_Text": "b062b9a",
"StrokeShape": "a0a5068",
"TileModeFallback": "af2e3ff",
"YUVImage": "bc64712",
"YUVImage_RGBAA": "bc64712",
Expand Down
20 changes: 20 additions & 0 deletions test/src/CanvasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 02cad7c

Please sign in to comment.