mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-12 15:34:22 +00:00
Wasm: allow iterator and add functions for layers revising (#730)
* common: move iterator functionality into separate IteratorModule * wasm: allow internal lib dependencies and iterator * wasm: added functions for layers revising This patch adds functions to thorvgwasm.cpp: layers() - that return a list of paints in a picture bounds() - that returns a bounds of a given paint setOpacity() - that sets the opacity of a given paint
This commit is contained in:
parent
f732479acc
commit
2a2ccb30bd
6 changed files with 165 additions and 15 deletions
|
@ -50,7 +50,7 @@ protected: \
|
||||||
friend Canvas; \
|
friend Canvas; \
|
||||||
friend Scene; \
|
friend Scene; \
|
||||||
friend Picture; \
|
friend Picture; \
|
||||||
friend SaveModule; \
|
friend IteratorModule; \
|
||||||
|
|
||||||
|
|
||||||
#define _TVG_DECALRE_IDENTIFIER() \
|
#define _TVG_DECALRE_IDENTIFIER() \
|
||||||
|
@ -62,6 +62,7 @@ namespace tvg
|
||||||
{
|
{
|
||||||
|
|
||||||
class RenderMethod;
|
class RenderMethod;
|
||||||
|
class IteratorModule;
|
||||||
class SaveModule;
|
class SaveModule;
|
||||||
class Scene;
|
class Scene;
|
||||||
class Picture;
|
class Picture;
|
||||||
|
|
|
@ -19,6 +19,7 @@ source_file = [
|
||||||
'tvgLzw.h',
|
'tvgLzw.h',
|
||||||
'tvgPictureImpl.h',
|
'tvgPictureImpl.h',
|
||||||
'tvgRender.h',
|
'tvgRender.h',
|
||||||
|
'tvgIteratorModule.h',
|
||||||
'tvgSaveModule.h',
|
'tvgSaveModule.h',
|
||||||
'tvgSceneImpl.h',
|
'tvgSceneImpl.h',
|
||||||
'tvgShapeImpl.h',
|
'tvgShapeImpl.h',
|
||||||
|
|
42
src/lib/tvgIteratorModule.h
Normal file
42
src/lib/tvgIteratorModule.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
|
||||||
|
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef _TVG_ITERATOR_MODULE_H_
|
||||||
|
#define _TVG_ITERATOR_MODULE_H_
|
||||||
|
|
||||||
|
#include "tvgPaint.h"
|
||||||
|
|
||||||
|
namespace tvg
|
||||||
|
{
|
||||||
|
|
||||||
|
class IteratorModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//Utility Method: Iterator Delegator
|
||||||
|
Iterator* iterator(const Paint* paint)
|
||||||
|
{
|
||||||
|
return paint->pImpl->iterator();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //_TVG_ITERATOR_MODULE_H_
|
|
@ -22,26 +22,20 @@
|
||||||
#ifndef _TVG_SAVE_MODULE_H_
|
#ifndef _TVG_SAVE_MODULE_H_
|
||||||
#define _TVG_SAVE_MODULE_H_
|
#define _TVG_SAVE_MODULE_H_
|
||||||
|
|
||||||
#include "tvgPaint.h"
|
#include "tvgIteratorModule.h"
|
||||||
|
|
||||||
namespace tvg
|
namespace tvg
|
||||||
{
|
{
|
||||||
|
|
||||||
class SaveModule
|
class SaveModule : public IteratorModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~SaveModule() {}
|
virtual ~SaveModule() {}
|
||||||
|
|
||||||
virtual bool save(Paint* paint, const string& path, bool compress) = 0;
|
virtual bool save(Paint* paint, const string& path, bool compress) = 0;
|
||||||
virtual bool close() = 0;
|
virtual bool close() = 0;
|
||||||
|
|
||||||
//Utility Method: Iterator Delegator
|
|
||||||
Iterator* iterator(const Paint* paint)
|
|
||||||
{
|
|
||||||
return paint->pImpl->iterator();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //_TVG_SAVE_MODULE_H_
|
#endif //_TVG_SAVE_MODULE_H_
|
||||||
|
|
|
@ -40,12 +40,12 @@ thorvg_dep = declare_dependency(
|
||||||
)
|
)
|
||||||
|
|
||||||
if (cc.get_id() == 'emscripten')
|
if (cc.get_id() == 'emscripten')
|
||||||
|
|
||||||
subdir('wasm')
|
subdir('wasm')
|
||||||
|
|
||||||
executable('thorvg-wasm',
|
executable('thorvg-wasm',
|
||||||
[],
|
[],
|
||||||
dependencies : [thorvg_dep, thorvg_wasm_dep],
|
include_directories : headers,
|
||||||
|
dependencies : [thorvg_lib_dep, thorvg_wasm_dep],
|
||||||
)
|
)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <thorvg.h>
|
#include <thorvg.h>
|
||||||
|
#include "tvgIteratorModule.h"
|
||||||
|
|
||||||
#include <emscripten/bind.h>
|
#include <emscripten/bind.h>
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ using namespace tvg;
|
||||||
|
|
||||||
string defaultData("<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M.10681413.09784845 1000.0527.01592069V1000.0851L.06005738 999.9983Z\" fill=\"#09bbf1\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><g fill=\"#09bbf1\"><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\"/></g></svg>");
|
string defaultData("<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M.10681413.09784845 1000.0527.01592069V1000.0851L.06005738 999.9983Z\" fill=\"#09bbf1\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><g fill=\"#09bbf1\"><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\"/></g></svg>");
|
||||||
|
|
||||||
class __attribute__((visibility("default"))) ThorvgWasm
|
class __attribute__((visibility("default"))) ThorvgWasm : public IteratorModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static unique_ptr<ThorvgWasm> create()
|
static unique_ptr<ThorvgWasm> create()
|
||||||
|
@ -157,6 +158,47 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val layers()
|
||||||
|
{
|
||||||
|
//returns an array of a structure Layer: [id] [depth] [type] [composite]
|
||||||
|
mLayers.reset();
|
||||||
|
sublayers(&mLayers, mPicture, 0);
|
||||||
|
|
||||||
|
return val(typed_memory_view(mLayers.count * sizeof(Layer) / sizeof(uint32_t), (uint32_t *)(mLayers.data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool setOpacity(uint32_t paintId, uint8_t opacity)
|
||||||
|
{
|
||||||
|
const Paint* paint = findPaintById(mPicture, paintId, nullptr);
|
||||||
|
if (!paint) return false;
|
||||||
|
const_cast<Paint*>(paint)->opacity(opacity);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
val bounds(uint32_t paintId)
|
||||||
|
{
|
||||||
|
Array<const Paint *> parents;
|
||||||
|
const Paint* paint = findPaintById(mPicture, paintId, &parents);
|
||||||
|
if (!paint) return val(typed_memory_view<float>(0, nullptr));
|
||||||
|
paint->bounds(&mBounds[0], &mBounds[1], &mBounds[2], &mBounds[3]);
|
||||||
|
|
||||||
|
mBounds[2] += mBounds[0];
|
||||||
|
mBounds[3] += mBounds[1];
|
||||||
|
|
||||||
|
for (auto paint = parents.data; paint < (parents.data + parents.count); ++paint) {
|
||||||
|
auto m = const_cast<Paint*>(*paint)->transform();
|
||||||
|
mBounds[0] = mBounds[0] * m.e11 + m.e13;
|
||||||
|
mBounds[1] = mBounds[1] * m.e22 + m.e23;
|
||||||
|
mBounds[2] = mBounds[2] * m.e11 + m.e13;
|
||||||
|
mBounds[3] = mBounds[3] * m.e22 + m.e23;
|
||||||
|
}
|
||||||
|
|
||||||
|
mBounds[2] -= mBounds[0];
|
||||||
|
mBounds[3] -= mBounds[1];
|
||||||
|
|
||||||
|
return val(typed_memory_view(4, mBounds));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit ThorvgWasm()
|
explicit ThorvgWasm()
|
||||||
{
|
{
|
||||||
|
@ -183,6 +225,69 @@ private:
|
||||||
if (mPicture) mPicture->size(width, height);
|
if (mPicture) mPicture->size(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Layer
|
||||||
|
{
|
||||||
|
uint32_t paint; //cast of a paint pointer
|
||||||
|
uint32_t depth;
|
||||||
|
uint32_t type;
|
||||||
|
uint32_t composite;
|
||||||
|
};
|
||||||
|
void sublayers(Array<Layer>* layers, const Paint* paint, uint32_t depth)
|
||||||
|
{
|
||||||
|
//paint
|
||||||
|
if (paint->id() != TVG_CLASS_ID_SHAPE) {
|
||||||
|
auto it = this->iterator(paint);
|
||||||
|
if (it->count() > 0) {
|
||||||
|
layers->reserve(layers->count + it->count());
|
||||||
|
it->begin();
|
||||||
|
while (auto child = it->next()) {
|
||||||
|
uint32_t type = child->id();
|
||||||
|
layers->push({.paint = reinterpret_cast<uint32_t>(child), .depth = depth + 1, .type = type, .composite = static_cast<uint32_t>(CompositeMethod::None)});
|
||||||
|
sublayers(layers, child, depth + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//composite
|
||||||
|
const Paint* compositeTarget = nullptr;
|
||||||
|
CompositeMethod composite = paint->composite(&compositeTarget);
|
||||||
|
if (compositeTarget && composite != CompositeMethod::None) {
|
||||||
|
uint32_t type = compositeTarget->id();
|
||||||
|
layers->push({.paint = reinterpret_cast<uint32_t>(compositeTarget), .depth = depth, .type = type, .composite = static_cast<uint32_t>(composite)});
|
||||||
|
sublayers(layers, compositeTarget, depth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Paint* findPaintById(const Paint* parent, uint32_t paintId, Array<const Paint *>* parents) {
|
||||||
|
//validate paintId is correct and exists in the picture
|
||||||
|
if (reinterpret_cast<uint32_t>(parent) == paintId) {
|
||||||
|
if (parents) parents->push(parent);
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
//paint
|
||||||
|
if (parent->id() != TVG_CLASS_ID_SHAPE) {
|
||||||
|
auto it = this->iterator(parent);
|
||||||
|
if (it->count() > 0) {
|
||||||
|
it->begin();
|
||||||
|
while (auto child = it->next()) {
|
||||||
|
if (auto paint = findPaintById(child, paintId, parents)) {
|
||||||
|
if (parents) parents->push(parent);
|
||||||
|
return paint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//composite
|
||||||
|
const Paint* compositeTarget = nullptr;
|
||||||
|
CompositeMethod composite = parent->composite(&compositeTarget);
|
||||||
|
if (compositeTarget && composite != CompositeMethod::None) {
|
||||||
|
if (auto paint = findPaintById(compositeTarget, paintId, parents)) {
|
||||||
|
if (parents) parents->push(parent);
|
||||||
|
return paint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string mErrorMsg;
|
string mErrorMsg;
|
||||||
unique_ptr< SwCanvas > mSwCanvas = nullptr;
|
unique_ptr< SwCanvas > mSwCanvas = nullptr;
|
||||||
|
@ -191,6 +296,9 @@ private:
|
||||||
|
|
||||||
uint32_t mWidth{0};
|
uint32_t mWidth{0};
|
||||||
uint32_t mHeight{0};
|
uint32_t mHeight{0};
|
||||||
|
|
||||||
|
Array<Layer> mLayers;
|
||||||
|
float mBounds[4];
|
||||||
float mOriginalSize[2];
|
float mOriginalSize[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -205,5 +313,9 @@ EMSCRIPTEN_BINDINGS(thorvg_bindings) {
|
||||||
.function("render", &ThorvgWasm::render)
|
.function("render", &ThorvgWasm::render)
|
||||||
.function("originalSize", &ThorvgWasm::originalSize)
|
.function("originalSize", &ThorvgWasm::originalSize)
|
||||||
|
|
||||||
.function("saveTvg", &ThorvgWasm::saveTvg);
|
.function("saveTvg", &ThorvgWasm::saveTvg)
|
||||||
|
|
||||||
|
.function("layers", &ThorvgWasm::layers)
|
||||||
|
.function("bounds", &ThorvgWasm::bounds)
|
||||||
|
.function("setOpacity", &ThorvgWasm::setOpacity);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue