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 //TODO: Replace with size(). Remove API
Result viewbox(float* x, float* y, float* w, float* h) const noexcept; Result viewbox(float* x, float* y, float* w, float* h) const noexcept;
Result size(uint32_t w, uint32_t h) noexcept; Result size(float w, float h) noexcept;
Result size(uint32_t* w, uint32_t* h) const noexcept; Result size(float* w, float* h) const noexcept;
static std::unique_ptr<Picture> gen() noexcept; static std::unique_ptr<Picture> gen() noexcept;

View file

@ -35,9 +35,7 @@ public:
float vy = 0; float vy = 0;
float vw = 0; float vw = 0;
float vh = 0; float vh = 0;
float w = 0, h = 0; //default image size
uint32_t w = 0; //default size
uint32_t h = 0; //default size
bool preserveAspect = true; //keep aspect ratio by default. bool preserveAspect = true; //keep aspect ratio by default.
virtual ~Loader() {} 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; if (pImpl->size(w, h)) return Result::Success;
return Result::InsufficientCondition; 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 (w) *w = pImpl->w;
if (h) *h = pImpl->h; if (h) *h = pImpl->h;

View file

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

View file

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