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

Add SVG import and rendering features #419

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
af6427b
svg to dom,and svg render
YGaurora Nov 14, 2024
b1db872
commit before context structure refactor
YGaurora Nov 15, 2024
f44ade3
debug and unit test
YGaurora Nov 20, 2024
7ed7413
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Nov 25, 2024
2d7b6ae
debug render
YGaurora Nov 25, 2024
ab8d893
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Nov 28, 2024
e24c3e9
format
YGaurora Nov 28, 2024
ccbed6f
typo
YGaurora Nov 28, 2024
0634b65
modify SVGFeGaussianBlur sigma scale
YGaurora Dec 2, 2024
1c6d8f8
save
YGaurora Dec 2, 2024
77ae5bc
formate code
YGaurora Dec 5, 2024
89ad985
...
YGaurora Dec 6, 2024
b481e14
format
YGaurora Dec 12, 2024
6a5cd29
format code
YGaurora Dec 12, 2024
a482430
format
YGaurora Dec 17, 2024
fff9fa5
...
YGaurora Dec 18, 2024
348f70e
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Dec 18, 2024
0d86fbb
...
YGaurora Dec 18, 2024
dc52e04
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Dec 23, 2024
e4e18a8
save
YGaurora Dec 24, 2024
19c3c4a
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Dec 26, 2024
de6c97f
...
YGaurora Dec 26, 2024
4061f68
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Dec 27, 2024
e5160a5
...
YGaurora Dec 30, 2024
ac521d1
...
YGaurora Dec 30, 2024
13c0a7b
interface for user and base attribute class define
YGaurora Dec 30, 2024
966e60b
...
YGaurora Dec 30, 2024
0a88c2b
。。。
YGaurora Jan 2, 2025
a0083fc
Merge branch 'feature/SVG_parser_interface_base_attr' into feature/sv…
YGaurora Jan 3, 2025
ad18a8c
...
YGaurora Jan 3, 2025
cb1e492
...
YGaurora Jan 3, 2025
bccd2c7
typo
YGaurora Jan 3, 2025
9f60098
typo
YGaurora Jan 3, 2025
ba1c7c2
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Jan 15, 2025
2beb3f7
SVG render
YGaurora Jan 22, 2025
b6f5bfc
。。。
YGaurora Jan 22, 2025
df5ce50
typo
YGaurora Jan 23, 2025
04e1c44
optimize
YGaurora Jan 24, 2025
2dcbdc7
.
YGaurora Jan 25, 2025
6d1506d
Merge remote-tracking branch 'origin/main' into feature/svg_parse_to_dom
YGaurora Jan 25, 2025
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
111 changes: 111 additions & 0 deletions include/tgfx/core/FontStyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2025 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 <algorithm>
#include <cstddef>
#include <memory>
#include <string>

namespace tgfx {

/**
* Font style object
*/
class FontStyle {
public:
enum class Weight {
Invisible,
Thin,
ExtraLight,
Light,
Normal,
Medium,
SemiBold,
Bold,
ExtraBold,
Black,
ExtraBlack,
};

enum class Width {
UltraCondensed,
ExtraCondensed,
Condensed,
SemiCondensed,
Normal,
SemiExpanded,
Expanded,
ExtraExpanded,
UltraExpanded,
};

enum class Slant {
Upright,
Italic,
Oblique,
};

constexpr FontStyle(Weight weight, Width width, Slant slant)
: value(static_cast<uint32_t>(std::clamp(weight, Weight::Invisible, Weight::ExtraBlack)) +
(static_cast<uint32_t>(std::clamp(width, Width::UltraCondensed, Width::UltraExpanded))
<< 16) +
(static_cast<uint32_t>(std::clamp(slant, Slant::Upright, Slant::Oblique)) << 24)) {
}

constexpr FontStyle() : FontStyle{Weight::Normal, Width::Normal, Slant::Upright} {
}

bool operator==(const FontStyle& rhs) const {
return value == rhs.value;
}

Weight weight() const {
return static_cast<Weight>(value & 0xFFFF);
}

Width width() const {
return static_cast<Width>((value >> 16) & 0xFF);
}

Slant slant() const {
return static_cast<Slant>((value >> 24) & 0xFF);
}

static constexpr FontStyle Normal() {
return FontStyle(Weight::Normal, Width::Normal, Slant::Upright);
}

static constexpr FontStyle Bold() {
return FontStyle(Weight::Bold, Width::Normal, Slant::Upright);
}

static constexpr FontStyle Italic() {
return FontStyle(Weight::Normal, Width::Normal, Slant::Italic);
}

static constexpr FontStyle BoldItalic() {
return FontStyle(Weight::Bold, Width::Normal, Slant::Italic);
}

private:
uint32_t value;
};

} // namespace tgfx
13 changes: 13 additions & 0 deletions include/tgfx/core/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <memory>
#include <string>
#include "tgfx/core/Data.h"

namespace tgfx {
/**
Expand All @@ -35,6 +36,11 @@ class Stream {
*/
static std::unique_ptr<Stream> MakeFromFile(const std::string& filePath);

/**
* Creates a stream from the specified data. Returns nullptr on failure.
*/
static std::unique_ptr<Stream> MakeFromData(std::shared_ptr<Data> data);

/**
* Returns the total length of the stream. If this cannot be done, returns 0.
*/
Expand Down Expand Up @@ -64,6 +70,13 @@ class Stream {
* beginning after this call returns.
*/
virtual bool rewind() = 0;

/**
* Returns the starting address for the data. If this cannot be done, returns nullptr.
*/
virtual const void* getMemoryBase() {
return nullptr;
}
};

/**
Expand Down
4 changes: 4 additions & 0 deletions include/tgfx/core/Typeface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unordered_map>
#include <vector>
#include "tgfx/core/Data.h"
#include "tgfx/core/FontStyle.h"

namespace tgfx {
/**
Expand Down Expand Up @@ -56,6 +57,9 @@ class Typeface {
static std::shared_ptr<Typeface> MakeFromName(const std::string& fontFamily,
const std::string& fontStyle);

static std::shared_ptr<Typeface> MakeFromStyle(const std::string& fontFamily,
FontStyle fontStyle);

/**
* Creates a new typeface for the given file path and ttc index. Returns nullptr if the typeface
* can't be created.
Expand Down
57 changes: 57 additions & 0 deletions include/tgfx/svg/FontManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2025 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 <cstddef>
#include <memory>
#include <string>
#include "tgfx/core/FontStyle.h"
#include "tgfx/core/Typeface.h"

namespace tgfx {
/**
* FontManager provides functionality to enumerate Typefaces and match them based on FontStyle.
*/
class FontManager {
public:
/**
* Destructor for FontManager
*/
virtual ~FontManager() = default;

/**
* Match a font family name and style, and return the corresponding Typeface
*/
std::shared_ptr<Typeface> matchTypeface(const std::string& familyName, FontStyle style) const;

/**
* Match a font family name, style, character, and language, and return the corresponding Typeface
*/
std::shared_ptr<Typeface> getFallbackTypeface(const std::string& familyName, FontStyle style,
Unichar character) const;

private:
virtual std::shared_ptr<Typeface> onMatchTypeface(const std::string& familyName,
FontStyle style) const = 0;
virtual std::shared_ptr<Typeface> onGetFallbackTypeface(const std::string& familyName,
FontStyle style,
Unichar character) const = 0;
};

} // namespace tgfx
52 changes: 52 additions & 0 deletions include/tgfx/svg/ResourceLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2025 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 <memory>
#include <string>
#include "tgfx/core/Data.h"
#include "tgfx/core/Image.h"

namespace tgfx {

/**
* ResourceLoader is an interface for loading resources (e.g. images, font) from
* external sources.
*/
class ResourceLoader {
public:
/**
* Data is a wrapper for raw data.
*/
virtual ~ResourceLoader() = default;

/**
* Load a generic resource specified by |path| + |name|, and return as an Data object.
*/
virtual std::shared_ptr<Data> load(const std::string& resourcePath,
const std::string& resourceName) const = 0;

/**
* Load an image asset specified by |path| + |name|, and returns the Image object.
*/
virtual std::shared_ptr<Image> loadImage(const std::string& resourcePath,
const std::string& resourceName) const = 0;
};

} // namespace tgfx
118 changes: 118 additions & 0 deletions include/tgfx/svg/SVGAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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 "tgfx/svg/SVGTypes.h"

namespace tgfx {
enum class SVGAttribute {
ClipRule,
Color,
ColorInterpolation,
ColorInterpolationFilters,
Cx, // <circle>, <ellipse>, <radialGradient>: center x position
Cy, // <circle>, <ellipse>, <radialGradient>: center y position
Fill,
FillOpacity,
FillRule,
Filter,
FilterUnits,
FontFamily,
FontSize,
FontStyle,
FontWeight,
Fx, // <radialGradient>: focal point x position
Fy, // <radialGradient>: focal point y position
GradientUnits,
GradientTransform,
Height,
Href,
Opacity,
Points,
PreserveAspectRatio,
R, // <circle>, <radialGradient>: radius
Rx, // <ellipse>,<rect>: horizontal (corner) radius
Ry, // <ellipse>,<rect>: vertical (corner) radius
SpreadMethod,
Stroke,
StrokeDashArray,
StrokeDashOffset,
StrokeOpacity,
StrokeLineCap,
StrokeLineJoin,
StrokeMiterLimit,
StrokeWidth,
Transform,
Text,
TextAnchor,
ViewBox,
Visibility,
Width,
X,
X1, // <line>: first endpoint x
X2, // <line>: second endpoint x
Y,
Y1, // <line>: first endpoint y
Y2, // <line>: second endpoint y

Unknown,
};

struct SVGPresentationAttributes {
static SVGPresentationAttributes MakeInitial();

SVGProperty<SVGPaint, true> Fill;
SVGProperty<SVGNumberType, true> FillOpacity;
SVGProperty<SVGFillRule, true> FillRule;
SVGProperty<SVGFillRule, true> ClipRule;

SVGProperty<SVGPaint, true> Stroke;
SVGProperty<SVGDashArray, true> StrokeDashArray;
SVGProperty<SVGLength, true> StrokeDashOffset;
SVGProperty<SVGLineCap, true> StrokeLineCap;
SVGProperty<SVGLineJoin, true> StrokeLineJoin;
SVGProperty<SVGNumberType, true> StrokeMiterLimit;
SVGProperty<SVGNumberType, true> StrokeOpacity;
SVGProperty<SVGLength, true> StrokeWidth;

SVGProperty<SVGVisibility, true> Visibility;

SVGProperty<SVGColorType, true> Color;
SVGProperty<SVGColorspace, true> ColorInterpolation;
SVGProperty<SVGColorspace, true> ColorInterpolationFilters;

SVGProperty<SVGFontFamily, true> FontFamily;
SVGProperty<SVGFontStyle, true> FontStyle;
SVGProperty<SVGFontSize, true> FontSize;
SVGProperty<SVGFontWeight, true> FontWeight;
SVGProperty<SVGTextAnchor, true> TextAnchor;

// uninherited
SVGProperty<SVGNumberType, false> Opacity;
SVGProperty<SVGFuncIRI, false> ClipPath;
SVGProperty<SVGDisplay, false> Display;
SVGProperty<SVGFuncIRI, false> Mask;
SVGProperty<SVGFuncIRI, false> Filter;
SVGProperty<SVGColor, false> StopColor;
SVGProperty<SVGNumberType, false> StopOpacity;
SVGProperty<SVGColor, false> FloodColor;
SVGProperty<SVGNumberType, false> FloodOpacity;
SVGProperty<SVGColor, false> LightingColor;
};
} // namespace tgfx
Loading
Loading