From 7730f8ba8b66978a3340213420c31c9e6d148d58 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 24 Jul 2025 18:16:31 +0900 Subject: [PATCH] renderer: properly support stroke bounding box after scaling. The stroke bounding box in ThorVG was previously only approximated, as the stroke transformation was mistakenly omitted. This has now been enhanced. The stroke join/corner is still not taken into account in the bounding box computation... --- examples/BoundingBox.cpp | 3 ++- src/renderer/tvgShape.h | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/BoundingBox.cpp b/examples/BoundingBox.cpp index 98aa94d3..3d708dbd 100644 --- a/examples/BoundingBox.cpp +++ b/examples/BoundingBox.cpp @@ -256,10 +256,11 @@ struct UserExample : tvgexam::Example shape->moveTo(0, 0); shape->lineTo(150, 100); shape->lineTo(0, 100); + shape->close(); shape->fill(255, 0, 255); shape->strokeWidth(30); shape->strokeFill(0, 255, 255); - shape->close(); + shape->strokeJoin(tvg::StrokeJoin::Round); tvg::Matrix m = {1.8794f, -0.6840f, 0.0f, 0.6840f, 1.8794f, 0.0f, 0.0f, 0.0f, 1.0f}; shape->transform(m); diff --git a/src/renderer/tvgShape.h b/src/renderer/tvgShape.h index 58ee4d01..2fff92cb 100644 --- a/src/renderer/tvgShape.h +++ b/src/renderer/tvgShape.h @@ -125,10 +125,15 @@ struct ShapeImpl : Shape //Stroke feathering if (stroking && rs.stroke) { - x -= rs.stroke->width * 0.5f; - y -= rs.stroke->width * 0.5f; - w += rs.stroke->width; - h += rs.stroke->width; + //Use geometric mean for feathering. + //Join, Cap wouldn't be considered. Generate stroke outline and compute bbox for accurate size? + auto sx = sqrt(m.e11 * m.e11 + m.e21 * m.e21); + auto sy = sqrt(m.e12 * m.e12 + m.e22 * m.e22); + auto feather = rs.stroke->width * sqrt(sx * sy); + x -= feather * 0.5f; + y -= feather * 0.5f; + w += feather; + h += feather; } pt4[0] = {x, y};