common loader: return viewbox info from the vector resource. 85/237985/2
authorHermet Park <chuneon.park@samsung.com>
Tue, 7 Jul 2020 06:12:11 +0000 (15:12 +0900)
committerHermet Park <chuneon.park@samsung.com>
Tue, 7 Jul 2020 06:17:43 +0000 (15:17 +0900)
if a scene loads a vector resource, it must have viewbox info from the design,
That viewbox will be used as bounding box so that user can scale up/down
the scene by its requirements.

Change-Id: Iafa39af23118a03de207c745364d56c837892e1b

src/lib/tvgLoader.h
src/lib/tvgSceneImpl.h
src/loaders/svg_loader/tvgSvgLoader.cpp

index 8a1c918..8f5bb49 100644 (file)
@@ -23,6 +23,12 @@ namespace tvg
 class Loader
 {
 public:
+    //default view box, if any.
+    float vx = 0;
+    float vy = 0;
+    float vw = 0;
+    float vh = 0;
+
     virtual ~Loader() {}
 
     virtual bool open(const char* path) = 0;
index 4e412d4..a70991c 100644 (file)
@@ -74,11 +74,12 @@ struct Scene::Impl
     {
         if (loader) {
             auto scene = loader->data();
-            auto p = scene.release();
-            if (!p) return false;
-            paints.push_back(p);
-            loader->close();
-            loader.reset(nullptr);
+            if (scene) {
+                auto p = scene.release();
+                if (!p) return false;
+                paints.push_back(p);
+                loader->close();
+            }
         }
 
         if (flag & RenderUpdateFlag::Transform) {
@@ -121,38 +122,44 @@ struct Scene::Impl
 
     bool bounds(float* px, float* py, float* pw, float* ph)
     {
-        auto x = FLT_MAX;
-        auto y = FLT_MAX;
-        auto w = 0.0f;
-        auto h = 0.0f;
-
-        for(auto paint: paints) {
-            auto x2 = FLT_MAX;
-            auto y2 = FLT_MAX;
-            auto w2 = 0.0f;
-            auto h2 = 0.0f;
-
-            if (paint->id() == PAINT_ID_SCENE) {
-                //We know renderer type, avoid dynamic_cast for performance.
-                auto scene = static_cast<Scene*>(paint);
-                if (!SCENE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false;
-            } else {
-                auto shape = static_cast<Shape*>(paint);
-                if (!SHAPE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false;
+        if (loader) {
+            if (px) *px = loader->vx;
+            if (py) *py = loader->vy;
+            if (pw) *pw = loader->vw;
+            if (ph) *ph = loader->vh;
+        } else {
+            auto x = FLT_MAX;
+            auto y = FLT_MAX;
+            auto w = 0.0f;
+            auto h = 0.0f;
+
+            for(auto paint: paints) {
+                auto x2 = FLT_MAX;
+                auto y2 = FLT_MAX;
+                auto w2 = 0.0f;
+                auto h2 = 0.0f;
+
+                if (paint->id() == PAINT_ID_SCENE) {
+                    //We know renderer type, avoid dynamic_cast for performance.
+                    auto scene = static_cast<Scene*>(paint);
+                    if (!SCENE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false;
+                } else {
+                    auto shape = static_cast<Shape*>(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;
             }
 
-            //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;
+            if (px) *px = x;
+            if (py) *py = y;
+            if (pw) *pw = w;
+            if (ph) *ph = h;
         }
-
-        if (px) *px = x;
-        if (py) *py = y;
-        if (pw) *pw = w;
-        if (ph) *ph = h;
-
         return true;
     }
 
index 928c754..d258005 100644 (file)
@@ -2262,6 +2262,13 @@ bool SvgLoader::open(const char* path)
         if (content.empty()) return false;
     }
 
+    //FIXME: Verify this resource is normal SVG, otherwise return false
+    //Also, return the brief resource info such as viewbox:
+    //this->vx = ?
+    //this->vy = ?
+    //this->vw = ?
+    //this->vh = ?
+
     return true;
 }