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 std::string& path) noexcept;
|
||||||
Result load(const char* data, uint32_t size) noexcept;
|
Result load(const char* data, uint32_t size) noexcept;
|
||||||
Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy) 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 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) noexcept;
|
||||||
|
|
|
@ -33,24 +33,7 @@ void tvgDrawCmds(tvg::Canvas* canvas, const char* path, const char* name)
|
||||||
|
|
||||||
if (picture->load(buf) != tvg::Result::Success) return;
|
if (picture->load(buf) != tvg::Result::Success) return;
|
||||||
|
|
||||||
float x, y, w, h;
|
picture->size(SIZE, SIZE);
|
||||||
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);
|
|
||||||
|
|
||||||
if (canvas->push(move(picture)) != tvg::Result::Success) return;
|
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;
|
if (picture->load(buf) != tvg::Result::Success) return;
|
||||||
|
|
||||||
float x, y, w, h;
|
picture->size(SIZE, SIZE);
|
||||||
picture->viewbox(&x, &y, &w, &h);
|
picture->translate((count % NUM_PER_LINE) * SIZE, SIZE * (count / NUM_PER_LINE));
|
||||||
|
|
||||||
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);
|
|
||||||
++count;
|
++count;
|
||||||
|
|
||||||
//Duplicates
|
//Duplicates
|
||||||
for (int i = 0; i < NUM_PER_LINE - 1; i++) {
|
for (int i = 0; i < NUM_PER_LINE - 1; i++) {
|
||||||
tvg::Picture* dup = static_cast<tvg::Picture*>(picture->duplicate());
|
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);
|
pictures.push_back(dup);
|
||||||
++count;
|
++count;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,25 +21,7 @@ void tvgDrawCmds(tvg::Canvas* canvas)
|
||||||
auto picture = tvg::Picture::gen();
|
auto picture = tvg::Picture::gen();
|
||||||
if (picture->load(svg, strlen(svg)) != tvg::Result::Success) return;
|
if (picture->load(svg, strlen(svg)) != tvg::Result::Success) return;
|
||||||
|
|
||||||
float x, y, w, h;
|
picture->size(WIDTH, HEIGHT);
|
||||||
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);
|
|
||||||
|
|
||||||
canvas->push(move(picture));
|
canvas->push(move(picture));
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,32 +61,34 @@ struct Picture::Impl
|
||||||
|
|
||||||
void resize()
|
void resize()
|
||||||
{
|
{
|
||||||
uint32_t w = 0, h = 0;
|
auto sx = float(w) / loader->vw;
|
||||||
if (this->w != 0 && this->h != 0) {
|
auto sy = float(h) / loader->vh;
|
||||||
w = this->w;
|
|
||||||
h = this->h;
|
if (loader->preserveAspect) {
|
||||||
}
|
//Scale
|
||||||
else if (loader->w != 0 && loader->h != 0) {
|
auto scale = sx < sy ? sx : sy;
|
||||||
w = loader->w;
|
paint->scale(scale);
|
||||||
h = loader->h;
|
//Align
|
||||||
}
|
auto vx = loader->vx * scale;
|
||||||
if (w != 0 && h != 0) {
|
auto vy = loader->vy * scale;
|
||||||
auto sx = w / (loader->vw + (loader->vx > 0 ? loader->vx : -1 * loader->vx));
|
auto vw = loader->vw * scale;
|
||||||
auto sy = h / (loader->vh + (loader->vy > 0 ? loader->vy : -1 * loader->vy));
|
auto vh = loader->vh * scale;
|
||||||
if (!loader->preserveAspect) {
|
if (vw > vh) vy -= (float(h) - vh) * 0.5f;
|
||||||
Matrix m = {sx, 0, -loader->vx,
|
else vx -= (float(w) - vw) * 0.5f;
|
||||||
0, sy, -loader->vy,
|
paint->translate(-vx, -vy);
|
||||||
0, 0, 1};
|
} else {
|
||||||
paint->transform(m);
|
//Align
|
||||||
}
|
auto vx = loader->vx * sx;
|
||||||
else {
|
auto vy = loader->vy * sy;
|
||||||
auto scale = sx < sy ? sx : sy;
|
auto vw = loader->vw * sx;
|
||||||
paint->translate(((w - loader->vw) * scale) / 2.0, ((h - loader->vh) * scale) / 2.0);
|
auto vh = loader->vh * sy;
|
||||||
paint->scale(scale);
|
if (vw > vh) vy -= (float(h) - vh) * 0.5f;
|
||||||
paint->translate(-loader->vx, -loader->vy);
|
else vx -= (float(w) - vw) * 0.5f;
|
||||||
}
|
|
||||||
resizing = false;
|
Matrix m = {sx, 0, -vx, 0, sy, -vy, 0, 0, 1};
|
||||||
|
paint->transform(m);
|
||||||
}
|
}
|
||||||
|
resizing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t reload()
|
uint32_t reload()
|
||||||
|
@ -96,8 +98,15 @@ struct Picture::Impl
|
||||||
auto scene = loader->scene();
|
auto scene = loader->scene();
|
||||||
if (scene) {
|
if (scene) {
|
||||||
paint = scene.release();
|
paint = scene.release();
|
||||||
resizing = true;
|
|
||||||
loader->close();
|
loader->close();
|
||||||
|
|
||||||
|
if (w == 0 && h == 0) {
|
||||||
|
w = loader->w;
|
||||||
|
h = loader->h;
|
||||||
|
} else {
|
||||||
|
resize();
|
||||||
|
}
|
||||||
|
|
||||||
if (paint) return RenderUpdateFlag::None;
|
if (paint) return RenderUpdateFlag::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue