picture: replace size parameter to float.

Considering smooth-resizing on sub-pixeling.
This commit is contained in:
Hermet Park 2020-12-08 12:27:13 +09:00 committed by GitHub
parent ea969ad0e2
commit 1e78d1f845
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 27 deletions

View file

@ -283,8 +283,8 @@ public:
//TODO: Replace with size(). Remove API
Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
Result size(uint32_t w, uint32_t h) noexcept;
Result size(uint32_t* w, uint32_t* h) const noexcept;
Result size(float w, float h) noexcept;
Result size(float* w, float* h) const noexcept;
static std::unique_ptr<Picture> gen() noexcept;

View file

@ -35,9 +35,7 @@ public:
float vy = 0;
float vw = 0;
float vh = 0;
uint32_t w = 0; //default size
uint32_t h = 0; //default size
float w = 0, h = 0; //default image size
bool preserveAspect = true; //keep aspect ratio by default.
virtual ~Loader() {}

View file

@ -75,14 +75,14 @@ Result Picture::viewbox(float* x, float* y, float* w, float* h) const noexcept
}
Result Picture::size(uint32_t w, uint32_t h) noexcept
Result Picture::size(float w, float h) noexcept
{
if (pImpl->size(w, h)) return Result::Success;
return Result::InsufficientCondition;
}
Result Picture::size(uint32_t* w, uint32_t* h) const noexcept
Result Picture::size(float* w, float* h) const noexcept
{
if (w) *w = pImpl->w;
if (h) *h = pImpl->h;

View file

@ -37,8 +37,7 @@ struct Picture::Impl
uint32_t *pixels = nullptr;
Picture *picture = nullptr;
void *edata = nullptr; //engine data
uint32_t w = 0, h = 0;
float w = 0, h = 0;
bool resizing = false;
Impl(Picture* p) : picture(p)
@ -61,8 +60,8 @@ struct Picture::Impl
void resize()
{
auto sx = float(w) / loader->vw;
auto sy = float(h) / loader->vh;
auto sx = w / loader->vw;
auto sy = h / loader->vh;
if (loader->preserveAspect) {
//Scale
@ -73,8 +72,8 @@ struct Picture::Impl
auto vy = loader->vy * scale;
auto vw = loader->vw * scale;
auto vh = loader->vh * scale;
if (vw > vh) vy -= (float(h) - vh) * 0.5f;
else vx -= (float(w) - vw) * 0.5f;
if (vw > vh) vy -= (h - vh) * 0.5f;
else vx -= (w - vw) * 0.5f;
paint->translate(-vx, -vy);
} else {
//Align
@ -82,8 +81,8 @@ struct Picture::Impl
auto vy = loader->vy * sy;
auto vw = loader->vw * sx;
auto vh = loader->vh * sy;
if (vw > vh) vy -= (float(h) - vh) * 0.5f;
else vx -= (float(w) - vw) * 0.5f;
if (vw > vh) vy -= (h - vh) * 0.5f;
else vx -= (w - vw) * 0.5f;
Matrix m = {sx, 0, -vx, 0, sy, -vy, 0, 0, 1};
paint->transform(m);
@ -99,14 +98,7 @@ struct Picture::Impl
if (scene) {
paint = scene.release();
loader->close();
if (w == 0 && h == 0) {
w = loader->w;
h = loader->h;
} else {
resize();
}
if (w != loader->w && h != loader->h) resize();
if (paint) return RenderUpdateFlag::None;
}
}
@ -167,6 +159,8 @@ struct Picture::Impl
loader = LoaderMgr::loader(path);
if (!loader) return Result::NonSupport;
if (!loader->read()) return Result::Unknown;
w = loader->w;
h = loader->h;
return Result::Success;
}
@ -176,6 +170,8 @@ struct Picture::Impl
loader = LoaderMgr::loader(data, size);
if (!loader) return Result::NonSupport;
if (!loader->read()) return Result::Unknown;
w = loader->w;
h = loader->h;
return Result::Success;
}

View file

@ -2468,11 +2468,12 @@ bool SvgLoader::header()
//Return the brief resource info such as viewbox:
vx = loaderData.doc->node.doc.vx;
vy = loaderData.doc->node.doc.vy;
vw = loaderData.doc->node.doc.vw;
vh = loaderData.doc->node.doc.vh;
w = vw = loaderData.doc->node.doc.vw;
h = vh = loaderData.doc->node.doc.vh;
w = loaderData.doc->node.doc.w;
h = loaderData.doc->node.doc.h;
//Override size
if (loaderData.doc->node.doc.w > 0) w = loaderData.doc->node.doc.w;
if (loaderData.doc->node.doc.h > 0) h = loaderData.doc->node.doc.h;
preserveAspect = loaderData.doc->node.doc.preserveAspect;
} else {