From e2057d10f19c39dbba75d701d7b8692b2a45577a Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 2 Sep 2022 12:10:36 +0900 Subject: [PATCH] examples: updated svg. revised the logic to align svg images in the center of the grids. this is the subsequent changes to 3939b61770c14602d7e349ba0baa21d9f4d97a44. --- src/examples/Svg.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/examples/Svg.cpp b/src/examples/Svg.cpp index 53e95020..a22ef36b 100644 --- a/src/examples/Svg.cpp +++ b/src/examples/Svg.cpp @@ -48,8 +48,22 @@ void svgDirCallback(const char* name, const char* path, void* data) if (picture->load(buf) != tvg::Result::Success) return; - picture->size(SIZE, SIZE); - picture->translate((count % NUM_PER_ROW) * SIZE, (count / NUM_PER_ROW) * (HEIGHT / NUM_PER_COL)); + //image scaling preserving its aspect ratio + float scale; + float shiftX = 0.0f, shiftY = 0.0f; + float w, h; + picture->size(&w, &h); + + if (w > h) { + scale = SIZE / w; + shiftY = (SIZE - h * scale) * 0.5f; + } else { + scale = SIZE / h; + shiftX = (SIZE - w * scale) * 0.5f; + } + + picture->scale(scale); + picture->translate((count % NUM_PER_ROW) * SIZE + shiftX, (count / NUM_PER_ROW) * (HEIGHT / NUM_PER_COL) + shiftY); pictures.push_back(move(picture));