mirror of
https://github.com/thorvg/thorvg.git
synced 2025-07-23 14:48:24 +00:00
common picture: revise resize method.
this implementation handles the center-aligned scale. Also removed viewbox usage since we have size() method.
This commit is contained in:
parent
6889f0ad1f
commit
20de2bfc15
5 changed files with 41 additions and 83 deletions
|
@ -280,6 +280,7 @@ public:
|
|||
Result load(const std::string& path) noexcept;
|
||||
Result load(const char* data, uint32_t size) noexcept;
|
||||
Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) noexcept;
|
||||
//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;
|
||||
|
|
|
@ -33,24 +33,7 @@ void tvgDrawCmds(tvg::Canvas* canvas, const char* path, const char* name)
|
|||
|
||||
if (picture->load(buf) != tvg::Result::Success) return;
|
||||
|
||||
float x, y, w, h;
|
||||
picture->viewbox(&x, &y, &w, &h);
|
||||
|
||||
float rate = (SIZE/(w > h ? w : h));
|
||||
picture->scale(rate);
|
||||
|
||||
x *= rate;
|
||||
y *= rate;
|
||||
w *= rate;
|
||||
h *= rate;
|
||||
|
||||
//Center Align ?
|
||||
if (w > h) {
|
||||
y -= (SIZE - h) * 0.5f;
|
||||
} else {
|
||||
x -= (SIZE - w) * 0.5f;
|
||||
}
|
||||
picture->translate(-x, -y);
|
||||
picture->size(SIZE, SIZE);
|
||||
|
||||
if (canvas->push(move(picture)) != tvg::Result::Success) return;
|
||||
|
||||
|
|
|
@ -23,31 +23,14 @@ void svgDirCallback(const char* name, const char* path, void* data)
|
|||
|
||||
if (picture->load(buf) != tvg::Result::Success) return;
|
||||
|
||||
float x, y, w, h;
|
||||
picture->viewbox(&x, &y, &w, &h);
|
||||
|
||||
float rate = (SIZE/(w > h ? w : h));
|
||||
picture->scale(rate);
|
||||
|
||||
x *= rate;
|
||||
y *= rate;
|
||||
w *= rate;
|
||||
h *= rate;
|
||||
|
||||
//Center Align ?
|
||||
if (w > h) {
|
||||
y -= (SIZE - h) * 0.5f;
|
||||
} else {
|
||||
x -= (SIZE - w) * 0.5f;
|
||||
}
|
||||
|
||||
picture->translate((count % NUM_PER_LINE) * SIZE - x, SIZE * (count / NUM_PER_LINE) - y);
|
||||
picture->size(SIZE, SIZE);
|
||||
picture->translate((count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
|
||||
++count;
|
||||
|
||||
//Duplicates
|
||||
for (int i = 0; i < NUM_PER_LINE - 1; i++) {
|
||||
tvg::Picture* dup = static_cast<tvg::Picture*>(picture->duplicate());
|
||||
dup->translate((count % NUM_PER_LINE) * SIZE - x, SIZE * (count / NUM_PER_LINE) - y);
|
||||
dup->translate((count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
|
||||
pictures.push_back(dup);
|
||||
++count;
|
||||
}
|
||||
|
|
|
@ -21,25 +21,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
|
|||
auto picture = tvg::Picture::gen();
|
||||
if (picture->load(svg, strlen(svg)) != tvg::Result::Success) return;
|
||||
|
||||
float x, y, w, h;
|
||||
picture->viewbox(&x, &y, &w, &h);
|
||||
|
||||
float rate = (WIDTH/(w > h ? w : h));
|
||||
picture->scale(rate);
|
||||
|
||||
x *= rate;
|
||||
y *= rate;
|
||||
w *= rate;
|
||||
h *= rate;
|
||||
|
||||
//Center Align ?
|
||||
if (w > h) {
|
||||
y -= (WIDTH - h) * 0.5f;
|
||||
} else {
|
||||
x -= (WIDTH - w) * 0.5f;
|
||||
}
|
||||
|
||||
picture->translate(-x, -y);
|
||||
picture->size(WIDTH, HEIGHT);
|
||||
|
||||
canvas->push(move(picture));
|
||||
}
|
||||
|
|
|
@ -61,33 +61,35 @@ struct Picture::Impl
|
|||
|
||||
void resize()
|
||||
{
|
||||
uint32_t w = 0, h = 0;
|
||||
if (this->w != 0 && this->h != 0) {
|
||||
w = this->w;
|
||||
h = this->h;
|
||||
}
|
||||
else if (loader->w != 0 && loader->h != 0) {
|
||||
w = loader->w;
|
||||
h = loader->h;
|
||||
}
|
||||
if (w != 0 && h != 0) {
|
||||
auto sx = w / (loader->vw + (loader->vx > 0 ? loader->vx : -1 * loader->vx));
|
||||
auto sy = h / (loader->vh + (loader->vy > 0 ? loader->vy : -1 * loader->vy));
|
||||
if (!loader->preserveAspect) {
|
||||
Matrix m = {sx, 0, -loader->vx,
|
||||
0, sy, -loader->vy,
|
||||
0, 0, 1};
|
||||
auto sx = float(w) / loader->vw;
|
||||
auto sy = float(h) / loader->vh;
|
||||
|
||||
if (loader->preserveAspect) {
|
||||
//Scale
|
||||
auto scale = sx < sy ? sx : sy;
|
||||
paint->scale(scale);
|
||||
//Align
|
||||
auto vx = loader->vx * scale;
|
||||
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;
|
||||
paint->translate(-vx, -vy);
|
||||
} else {
|
||||
//Align
|
||||
auto vx = loader->vx * sx;
|
||||
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;
|
||||
|
||||
Matrix m = {sx, 0, -vx, 0, sy, -vy, 0, 0, 1};
|
||||
paint->transform(m);
|
||||
}
|
||||
else {
|
||||
auto scale = sx < sy ? sx : sy;
|
||||
paint->translate(((w - loader->vw) * scale) / 2.0, ((h - loader->vh) * scale) / 2.0);
|
||||
paint->scale(scale);
|
||||
paint->translate(-loader->vx, -loader->vy);
|
||||
}
|
||||
resizing = false;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t reload()
|
||||
{
|
||||
|
@ -96,8 +98,15 @@ struct Picture::Impl
|
|||
auto scene = loader->scene();
|
||||
if (scene) {
|
||||
paint = scene.release();
|
||||
resizing = true;
|
||||
loader->close();
|
||||
|
||||
if (w == 0 && h == 0) {
|
||||
w = loader->w;
|
||||
h = loader->h;
|
||||
} else {
|
||||
resize();
|
||||
}
|
||||
|
||||
if (paint) return RenderUpdateFlag::None;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue