common loader: return viewbox info from the vector resource.

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
This commit is contained in:
Hermet Park 2020-07-07 15:12:11 +09:00
parent d37cdaf57c
commit 6967b998b6
3 changed files with 52 additions and 32 deletions

View file

@ -23,6 +23,12 @@ namespace tvg
class Loader class Loader
{ {
public: public:
//default view box, if any.
float vx = 0;
float vy = 0;
float vw = 0;
float vh = 0;
virtual ~Loader() {} virtual ~Loader() {}
virtual bool open(const char* path) = 0; virtual bool open(const char* path) = 0;

View file

@ -74,11 +74,12 @@ struct Scene::Impl
{ {
if (loader) { if (loader) {
auto scene = loader->data(); auto scene = loader->data();
auto p = scene.release(); if (scene) {
if (!p) return false; auto p = scene.release();
paints.push_back(p); if (!p) return false;
loader->close(); paints.push_back(p);
loader.reset(nullptr); loader->close();
}
} }
if (flag & RenderUpdateFlag::Transform) { if (flag & RenderUpdateFlag::Transform) {
@ -121,38 +122,44 @@ struct Scene::Impl
bool bounds(float* px, float* py, float* pw, float* ph) bool bounds(float* px, float* py, float* pw, float* ph)
{ {
auto x = FLT_MAX; if (loader) {
auto y = FLT_MAX; if (px) *px = loader->vx;
auto w = 0.0f; if (py) *py = loader->vy;
auto h = 0.0f; 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) { for(auto paint: paints) {
auto x2 = FLT_MAX; auto x2 = FLT_MAX;
auto y2 = FLT_MAX; auto y2 = FLT_MAX;
auto w2 = 0.0f; auto w2 = 0.0f;
auto h2 = 0.0f; auto h2 = 0.0f;
if (paint->id() == PAINT_ID_SCENE) { if (paint->id() == PAINT_ID_SCENE) {
//We know renderer type, avoid dynamic_cast for performance. //We know renderer type, avoid dynamic_cast for performance.
auto scene = static_cast<Scene*>(paint); auto scene = static_cast<Scene*>(paint);
if (!SCENE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false; if (!SCENE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false;
} else { } else {
auto shape = static_cast<Shape*>(paint); auto shape = static_cast<Shape*>(paint);
if (!SHAPE_IMPL->bounds(&x2, &y2, &w2, &h2)) return false; 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 (px) *px = x;
if (x2 < x) x = x2; if (py) *py = y;
if (x + w < x2 + w2) w = (x2 + w2) - x; if (pw) *pw = w;
if (y2 < y) y = x2; if (ph) *ph = h;
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;
return true; return true;
} }

View file

@ -2262,6 +2262,13 @@ bool SvgLoader::open(const char* path)
if (content.empty()) return false; 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; return true;
} }