From 17af011eae83e7e49142303968a184961147d591 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 5 May 2020 20:38:26 +0900 Subject: [PATCH] common scene: support bounds() method this method returns boundary of a scene. Change-Id: I1a32c8e034f53822008048f1d8ae7062fb72cca3 --- src/lib/tvgScene.cpp | 10 ++++++++++ src/lib/tvgSceneImpl.h | 23 +++++++++++++++++++++++ src/lib/tvgShape.cpp | 4 ++-- src/lib/tvgShapeImpl.h | 6 ++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/lib/tvgScene.cpp b/src/lib/tvgScene.cpp index 9b9844b..471d2ab 100644 --- a/src/lib/tvgScene.cpp +++ b/src/lib/tvgScene.cpp @@ -78,6 +78,16 @@ int Scene::rotate(float degree) noexcept int Scene::bounds(float& x, float& y, float& w, float& h) const noexcept { + auto impl = pImpl.get(); + assert(impl); + + x = FLT_MAX; + y = FLT_MAX; + w = 0; + h = 0; + + if (!impl->bounds(x, y, w, h)) return -1; + return 0; } diff --git a/src/lib/tvgSceneImpl.h b/src/lib/tvgSceneImpl.h index 31a3614..a2ce90d 100644 --- a/src/lib/tvgSceneImpl.h +++ b/src/lib/tvgSceneImpl.h @@ -71,6 +71,29 @@ struct Scene::Impl } return true; } + + bool bounds(float& x, float& y, float& w, float& h) + { + for(auto paint: paints) { + auto x2 = FLT_MAX; + auto y2 = FLT_MAX; + auto w2 = 0.0f; + auto h2 = 0.0f; + + if (auto scene = dynamic_cast(paint)) { + if (!SCENE_IMPL->bounds(x2, y2, w2, h2)) return false; + } else if (auto shape = dynamic_cast(paint)) { + if (!SHAPE_IMPL->bounds(x2, y2, w2, h2)) return false; + } + + //Merge regions + if (x2 < x) x = x2; + if (x + w < x2 + w2) w = (x2 + w2) - x; + if (y2 < y) y = x2; + if (y + h < y2 + h2) h = (y2 + h2) - y; + } + return true; + } }; #endif //_TVG_SCENE_IMPL_H_ \ No newline at end of file diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index 1e5545d..272b194 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -273,9 +273,9 @@ int Shape::rotate(float degree) noexcept int Shape::bounds(float& x, float& y, float& w, float& h) const noexcept { auto impl = pImpl.get(); - assert(impl && impl->path); + assert(impl); - if (!impl->path->bounds(x, y, w, h)) return -1; + if (!impl->bounds(x, y, w, h)) return -1; return 0; } diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index 63b7477..5894b1e 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -72,6 +72,12 @@ struct Shape::Impl if (edata) return true; return false; } + + bool bounds(float& x, float& y, float& w, float& h) + { + assert(path); + return path->bounds(x, y, w, h); + } }; #endif //_TVG_SHAPE_IMPL_H_ \ No newline at end of file -- 2.7.4