mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
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:
parent
d37cdaf57c
commit
6967b998b6
3 changed files with 52 additions and 32 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
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;
|
||||
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 (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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue