Skip to content

Commit

Permalink
add support for jpg/webp/svg exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
yslib committed Oct 25, 2023
1 parent 8ac437b commit c6e9b1c
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 92 deletions.
35 changes: 28 additions & 7 deletions include/Layer/VGGLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,30 @@ namespace VGG::layer

enum class EImageEncode
{
IE_PNG
IE_PNG,
IE_JPEG,
IE_WEBP,
IE_RAW
};

struct ImageOptions
{
EImageEncode encode;
int position[2] = { 0, 0 };
int extend[2] = { 0, 0 };
int quality{ 100 };
};

struct SVGOptions
{
int position[2];
int extend[2];
};

struct PDFOptions
{
int position[2];
int extend[2];
int quality{ 100 };
};

class VLayer__pImpl;
Expand All @@ -55,6 +70,7 @@ class VLayer : public GraphicsLayer
virtual void render() override;
virtual void endFrame() override;
virtual void shutdown() override;

void resize(int w, int h) override;
void addRenderItem(std::shared_ptr<Renderable> item);
void addScene(std::shared_ptr<Scene> scene);
Expand Down Expand Up @@ -85,12 +101,17 @@ class VLayer : public GraphicsLayer
return m_scale;
}

void saveSKP(const std::string& path)
{
m_skpPath = path;
}

std::optional<std::vector<char>> makeImageSnapshot(const ImageOptions& opts);
void makeImageSnapshot(const ImageOptions& opts, std::ostream& os);

std::optional<std::vector<char>> makeSVG(const SVGOptions& opts);
void makeSVG(const SVGOptions& opts, std::ostream& os);

std::optional<std::vector<char>> makePDF(const PDFOptions& opts);
void makePDF(const PDFOptions& opts, std::ostream& os);

std::optional<std::vector<char>> makeSKP();
void makeSKP(std::ostream& os);
};

} // namespace VGG::layer
16 changes: 16 additions & 0 deletions include/VGG/Exporter/ImageExporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,25 @@
namespace VGG::exporter
{

enum EFileType
{
SVG,
PDF,
SKP
};

enum EImageType
{
PNG,
JPEG,
WEBP,
};

struct ImageOption
{
int imageQuality = 100;
int resolutionLevel = 2;
EImageType type{ EImageType::PNG };
};

enum class EBackend
Expand Down Expand Up @@ -78,6 +93,7 @@ class Exporter
{
return Exporter::Iterator{ *this, std::move(j), std::move(resources), opt };
}

void setOutputCallback(OutputCallback callback);
~Exporter();
};
Expand Down
21 changes: 19 additions & 2 deletions src/Entry/Exporter/ImageExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Exporter::Iterator__pImpl
float maxSurfaceSize[2];
int totalFrames{ 0 };
int index{ 0 };
const ImageOption imgOpts;
std::shared_ptr<Scene> scene;
Iterator__pImpl(Exporter::Iterator* api,
Exporter& exporter,
Expand All @@ -191,17 +192,33 @@ class Exporter::Iterator__pImpl
const ImageOption& opt)
: q_api(api)
, exporter(exporter)
, imgOpts(opt)
{
getMaxSurfaceSize(opt.resolutionLevel, maxSurfaceSize);
scene = std::make_shared<Scene>();
Layout::ExpandSymbol e(json);
scene->loadFileContent(e());
scene->setResRepo(resource);
scene->setResRepo(std::move(resource));
totalFrames = scene->frameCount();
index = 0;
exporter.d_impl->resize(maxSurfaceSize[0], maxSurfaceSize[1]);
}

layer::EImageEncode toEImageEncode(EImageType type)
{
switch (type)
{
case PNG:
return layer::EImageEncode::IE_PNG;
case JPEG:
return layer::EImageEncode::IE_JPEG;
case WEBP:
return layer::EImageEncode::IE_WEBP;
default:
return layer::EImageEncode::IE_PNG;
}
}

bool next(std::string& key, std::vector<char>& image)
{
if (index >= scene->frameCount())
Expand Down Expand Up @@ -234,7 +251,7 @@ class Exporter::Iterator__pImpl
auto scale =
calcScaleFactor(w, h, maxSurfaceSize[0], maxSurfaceSize[1], actualSize[0], actualSize[1]);
layer::ImageOptions opts;
opts.encode = layer::EImageEncode::IE_PNG;
opts.encode = toEImageEncode(imgOpts.type);
opts.position[0] = 0;
opts.position[1] = 0;
opts.extend[0] = actualSize[0];
Expand Down
8 changes: 4 additions & 4 deletions src/Layer/PaintNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,9 @@ void PaintNode::paintStyle(SkCanvas* canvas, const SkPath& path, const SkPath& o
else if (*blurType == BT_Background)
painter.blurBackgroundBegin(blur.radius, blur.radius, getBound(), &res);
else if (*blurType == BT_Motion)
DEBUG("Motion blur has not been implemeted");
DEBUG("Motion blur has not been implemented");
else if (*blurType == BT_Zoom)
DEBUG("Zoom blur has not been implemeted");
DEBUG("Zoom blur has not been implemented");
}
if (!outlineMask.isEmpty())
{
Expand All @@ -806,9 +806,9 @@ void PaintNode::paintStyle(SkCanvas* canvas, const SkPath& path, const SkPath& o
else if (*blurType == BT_Background)
painter.blurBackgroundEnd();
else if (*blurType == BT_Zoom)
DEBUG("Zoom blur has not been implemeted");
DEBUG("Zoom blur has not been implemented");
else if (*blurType == BT_Motion)
DEBUG("Zoom blur has not been implemeted");
DEBUG("Motion blur has not been implemented");
}
}
else
Expand Down
Loading

0 comments on commit c6e9b1c

Please sign in to comment.