diff --git a/inc/thorvg.h b/inc/thorvg.h index 47c41340..2debb473 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -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; diff --git a/src/examples/MultiCanvas.cpp b/src/examples/MultiCanvas.cpp index 614f9cae..b1ac9c57 100644 --- a/src/examples/MultiCanvas.cpp +++ b/src/examples/MultiCanvas.cpp @@ -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; diff --git a/src/examples/Stress.cpp b/src/examples/Stress.cpp index 9e2d556b..405d180d 100644 --- a/src/examples/Stress.cpp +++ b/src/examples/Stress.cpp @@ -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(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; } diff --git a/src/examples/Svg2.cpp b/src/examples/Svg2.cpp index 4cf61141..8a6b62f3 100644 --- a/src/examples/Svg2.cpp +++ b/src/examples/Svg2.cpp @@ -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)); } diff --git a/src/lib/tvgPictureImpl.h b/src/lib/tvgPictureImpl.h index a692ec94..1ff63e36 100644 --- a/src/lib/tvgPictureImpl.h +++ b/src/lib/tvgPictureImpl.h @@ -61,32 +61,34 @@ 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}; - 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; + 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); } + 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; } }