Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG export module, converting drawing commands to SVG text #367

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d420b9f
export to svg
YGaurora Nov 8, 2024
8aecd42
update code
YGaurora Nov 27, 2024
044dcc7
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Nov 28, 2024
29d6acd
modify method
YGaurora Nov 28, 2024
89e3111
format code
YGaurora Nov 28, 2024
f2cb00d
format
YGaurora Nov 28, 2024
5e695ba
blur and shadow export
YGaurora Dec 4, 2024
5172467
remove unused code
YGaurora Dec 4, 2024
46eff0a
modify state stack
YGaurora Dec 4, 2024
89d2962
save
YGaurora Dec 4, 2024
cd1b226
...
YGaurora Dec 6, 2024
ea79d6f
...
YGaurora Dec 6, 2024
03351f9
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 6, 2024
ba97d37
refactor
YGaurora Dec 6, 2024
e45554f
remove unused code
YGaurora Dec 6, 2024
6559c09
unit test
YGaurora Dec 6, 2024
2f5cb4d
format
YGaurora Dec 6, 2024
578fb4e
fix code problem and export text as text
YGaurora Dec 11, 2024
7c1349b
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 11, 2024
d19f59c
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 11, 2024
ecf6dfd
...
YGaurora Dec 11, 2024
049d00c
format
YGaurora Dec 12, 2024
92f7863
optimize code and remove mistake
YGaurora Dec 17, 2024
e775c30
。。。
YGaurora Dec 17, 2024
93ce0a3
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 17, 2024
db206f9
format code
YGaurora Dec 17, 2024
922820d
fix compile error
YGaurora Dec 17, 2024
776637e
fix
YGaurora Dec 17, 2024
510050a
。。。
YGaurora Dec 19, 2024
89cfa3c
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 19, 2024
e40d4cf
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 19, 2024
70b168b
implement write stream and exporting flags
YGaurora Dec 19, 2024
8baff71
...
YGaurora Dec 19, 2024
8637e94
...
YGaurora Dec 20, 2024
ee42709
Merge remote-tracking branch 'origin/main' into feature/yg_drawcall_t…
YGaurora Dec 23, 2024
fa93f01
write stream
YGaurora Dec 23, 2024
9b061a0
support matrix color shader
YGaurora Dec 23, 2024
324c85a
optimize comment
YGaurora Dec 23, 2024
1968596
fix auto test
YGaurora Dec 23, 2024
7fce6b1
format code
YGaurora Dec 23, 2024
a1d73fc
format code
YGaurora Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tgfx/core/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,6 @@ class Canvas {
friend class Surface;
friend class Picture;
friend class Recorder;
friend class SVGGenerator;
};
} // namespace tgfx
1 change: 1 addition & 0 deletions include/tgfx/core/ImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ class ImageFilter {
friend class InnerShadowImageFilter;
friend class ComposeImageFilter;
friend class FilterImage;
friend class ImageFilterCaster;
};
} // namespace tgfx
2 changes: 2 additions & 0 deletions include/tgfx/core/Picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace tgfx {
class Record;
class Canvas;
class DrawContext;
class SVGContext;
class MCState;
class Image;

Expand Down Expand Up @@ -61,6 +62,7 @@ class Picture {
friend class MeasureContext;
friend class RenderContext;
friend class RecordingContext;
friend class SVGContext;
friend class Image;
friend class PictureImage;
friend class Canvas;
Expand Down
1 change: 1 addition & 0 deletions include/tgfx/core/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,6 @@ class Shader {

friend class FragmentProcessor;
friend class Canvas;
friend class ShaderCaster;
};
} // namespace tgfx
65 changes: 65 additions & 0 deletions include/tgfx/svg/SVGGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <sstream>
#include "tgfx/core/Canvas.h"
#include "tgfx/gpu/Context.h"

namespace tgfx {
class SVGContext;

/**
* SVGGenerator is a class used to generate SVG text, converting drawing commands in the Canvas to
* SVG text.
*/
class SVGGenerator {
YGaurora marked this conversation as resolved.
Show resolved Hide resolved
public:
SVGGenerator() = default;
~SVGGenerator();

/**
* Begin generating SVG text and return a Canvas object for drawing SVG graphics.
*
* @param GPUContext used to convert some rendering commands into image data.
* @param size The size sets the size of the SVG, and content that exceeds the display area in the
* SVG will be clipped.
* @param isPretty Whether to format the generated SVG text with "/n"、"/t"".
* @return Canvas*
*/
Canvas* beginGenerate(Context* GPUContext, const ISize& size, bool isPretty = true);
YGaurora marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the recording canvas if it is active, or nullptr.
*/
Canvas* getCanvas() const;

/**
* Finish generating SVG text and return the generated SVG text. If no recording is active.
*/
std::string finishGenerate();
YGaurora marked this conversation as resolved.
Show resolved Hide resolved

private:
bool _actively = false;
SVGContext* _context = nullptr;
Canvas* _canvas = nullptr;
std::stringstream _svgStream;
};

} // namespace tgfx
69 changes: 69 additions & 0 deletions src/svg/Caster.cpp
YGaurora marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "Caster.h"

namespace tgfx {
std::shared_ptr<const ColorShader> ShaderCaster::CastToColorShader(
const std::shared_ptr<Shader>& shader) {
if (shader->type() == Shader::Type::Color) {
return std::static_pointer_cast<const ColorShader>(shader);
}
return nullptr;
}

std::shared_ptr<const ImageShader> ShaderCaster::CastToImageShader(
const std::shared_ptr<Shader>& shader) {
if (shader->type() == Shader::Type::Image) {
return std::static_pointer_cast<const ImageShader>(shader);
}
return nullptr;
}

std::shared_ptr<const GradientShader> ShaderCaster::CastToGradientShader(
const std::shared_ptr<Shader>& shader) {
if (shader->type() == Shader::Type::Gradient) {
return std::static_pointer_cast<const GradientShader>(shader);
}
return nullptr;
}

std::shared_ptr<const BlurImageFilter> ImageFilterCaster::CastToBlurImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter) {
if (imageFilter->type() == ImageFilter::Type::Blur) {
return std::static_pointer_cast<const BlurImageFilter>(imageFilter);
}
return nullptr;
}
std::shared_ptr<const DropShadowImageFilter> ImageFilterCaster::CastToDropShadowImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter) {
if (imageFilter->type() == ImageFilter::Type::DropShadow) {
return std::static_pointer_cast<const DropShadowImageFilter>(imageFilter);
}
return nullptr;
}

std::shared_ptr<const InnerShadowImageFilter> ImageFilterCaster::CastToInnerShadowImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter) {
if (imageFilter->type() == ImageFilter::Type::InnerShadow) {
return std::static_pointer_cast<const InnerShadowImageFilter>(imageFilter);
}
return nullptr;
}

} // namespace tgfx
55 changes: 55 additions & 0 deletions src/svg/Caster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include <memory>
#include "core/filters/BlurImageFilter.h"
#include "core/filters/DropShadowImageFilter.h"
#include "core/filters/InnerShadowImageFilter.h"
#include "core/shaders/ColorShader.h"
#include "core/shaders/GradientShader.h"
#include "core/shaders/ImageShader.h"

#pragma once

namespace tgfx {

class ShaderCaster {
public:
static std::shared_ptr<const ColorShader> CastToColorShader(
YGaurora marked this conversation as resolved.
Show resolved Hide resolved
const std::shared_ptr<Shader>& shader);

static std::shared_ptr<const ImageShader> CastToImageShader(
const std::shared_ptr<Shader>& shader);

static std::shared_ptr<const GradientShader> CastToGradientShader(
const std::shared_ptr<Shader>& shader);
};

class ImageFilterCaster {
public:
static std::shared_ptr<const BlurImageFilter> CastToBlurImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter);

static std::shared_ptr<const DropShadowImageFilter> CastToDropShadowImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter);

static std::shared_ptr<const InnerShadowImageFilter> CastToInnerShadowImageFilter(
const std::shared_ptr<ImageFilter>& imageFilter);
};

} // namespace tgfx
Loading
Loading