42#define NOVASVG_INLINE inline
50#if defined(NOVASVG_BUILD_STATIC) || !defined(NOVASVG_BUILD_SHARED)
53#elif (defined(_WIN32) || defined(__CYGWIN__))
54#define NOVASVG_EXPORT __declspec(dllexport)
55#define NOVASVG_IMPORT __declspec(dllimport)
56#elif defined(__GNUC__) && (__GNUC__ >= 4)
57#define NOVASVG_EXPORT __attribute__((__visibility__("default")))
65#define NOVASVG_API NOVASVG_EXPORT
67#define NOVASVG_API NOVASVG_IMPORT
86#define NOVASVG_MAX(a, b) ((a) > (b) ? (a) : (b))
89#define NOVASVG_MIN(a, b) ((a) < (b) ? (a) : (b))
92#define NOVASVG_VERSION_MAJOR 0
93#define NOVASVG_VERSION_MINOR 10
94#define NOVASVG_VERSION_PATCH 0
96#define NOVASVG_VERSION_ENCODE(major, minor, patch) (((major) * 10000) + ((minor) * 100) + ((patch) * 1))
97#define NOVASVG_VERSION NOVASVG_VERSION_ENCODE(NOVASVG_VERSION_MAJOR, NOVASVG_VERSION_MINOR, NOVASVG_VERSION_PATCH)
99#define NOVASVG_VERSION_XSTRINGIZE(major, minor, patch) #major"."#minor"."#patch
100#define NOVASVG_VERSION_STRINGIZE(major, minor, patch) NOVASVG_VERSION_XSTRINGIZE(major, minor, patch)
101#define NOVASVG_VERSION_STRING NOVASVG_VERSION_STRINGIZE(NOVASVG_VERSION_MAJOR, NOVASVG_VERSION_MINOR, NOVASVG_VERSION_PATCH)
133using namespace render;
162using namespace render;
184 Box(
float x,
float y,
float w,
float h);
232 Matrix(
float a,
float b,
float c,
float d,
float e,
float f);
271 Matrix& translate(
float tx,
float ty);
279 Matrix& scale(
float sx,
float sy);
288 Matrix& rotate(
float angle,
float cx = 0.f,
float cy = 0.f);
296 Matrix& shear(
float shx,
float shy);
321 static Matrix translated(
float tx,
float ty);
329 static Matrix scaled(
float sx,
float sy);
338 static Matrix rotated(
float angle,
float cx = 0.f,
float cy = 0.f);
346 static Matrix sheared(
float shx,
float shy);
374 bool isTextNode()
const;
380 bool isElement()
const;
404 bool isNull()
const {
return m_node ==
nullptr; }
410 operator bool()
const {
return !isNull(); }
446 const std::string& data()
const;
452 void setData(
const std::string& data);
472 bool hasAttribute(
const std::string& name)
const;
479 const std::string& getAttribute(
const std::string& name)
const;
486 void setAttribute(
const std::string& name,
const std::string& value);
502 Bitmap renderToBitmap(
int width = -1,
int height = -1,
const Color& backgroundColor =
Color(0, 0, 0, 0))
const;
508 Matrix getLocalMatrix()
const;
514 Matrix getGlobalMatrix()
const;
520 Box getLocalBoundingBox()
const;
526 Box getGlobalBoundingBox()
const;
532 Box getBoundingBox()
const;
542 SVGElement* element(
bool layoutIfNeeded =
false)
const;
558 static std::unique_ptr<Document> loadFromFile(
const std::string& filename);
565 static std::unique_ptr<Document> loadFromData(
const std::string&
string);
572 static std::unique_ptr<Document> loadFromData(
const char* data);
580 static std::unique_ptr<Document> loadFromData(
const char* data,
size_t length);
586 void applyStyleSheet(
const std::string& content);
593 ElementList querySelectorAll(
const std::string& content)
const;
605 float height()
const;
611 Box boundingBox()
const;
637 Bitmap renderToBitmap(
int width = -1,
int height = -1,
const Color& backgroundColor =
Color(0, 0, 0, 0))
const;
645 Element elementFromPoint(
float x,
float y)
const;
652 Element getElementById(
const std::string&
id)
const;
658 Element documentElement()
const;
669 bool parse(
const char* data,
size_t length);
670 std::unique_ptr<SVGRootElement> m_rootElement;
701using namespace render;
709using namespace render;
723 : x(x), y(y), w(w), h(h)
744 : x(box.x), y(box.y), w(box.w), h(box.h)
749 : a(a), b(b), c(c), d(d), e(e), f(f)
775 return (*
this = *
this *
matrix);
815 *
this =
Matrix(1, 0, 0, 1, 0, 0);
882 return text()->
data();
928 auto canvas = Canvas::create(bitmap);
938 if(elementBounds.isEmpty())
940 if(width <= 0 && height <= 0) {
941 width =
static_cast<int>(std::ceil(elementBounds.w));
942 height =
static_cast<int>(std::ceil(elementBounds.h));
943 }
else if(width > 0 && height <= 0) {
944 height =
static_cast<int>(std::ceil(width * elementBounds.h / elementBounds.w));
945 }
else if(height > 0 && width <= 0) {
946 width =
static_cast<int>(std::ceil(height * elementBounds.w / elementBounds.h));
949 auto xScale = width / elementBounds.w;
950 auto yScale = height / elementBounds.h;
952 Matrix matrix(xScale, 0, 0, yScale, -elementBounds.x * xScale, -elementBounds.y * yScale);
953 Bitmap bitmap(width, height);
954 if(backgroundColor.
value()) bitmap.
clear(backgroundColor);
998 for(
const auto& child : element()->
children())
1006 if(element && layoutIfNeeded)
1035 std::unique_ptr<Document> document(
new Document);
1036 if(!document->parse(data, length))
1058 m_rootElement->layoutIfNeeded();
1063 m_rootElement->forceLayout();
1116 if(intrinsicWidth == 0.f || intrinsicHeight == 0.f)
1119 width =
static_cast<int>(std::ceil(intrinsicWidth));
1120 height =
static_cast<int>(std::ceil(intrinsicHeight));
1122 height =
static_cast<int>(std::ceil(
width * intrinsicHeight / intrinsicWidth));
1124 width =
static_cast<int>(std::ceil(
height * intrinsicWidth / intrinsicHeight));
1127 auto xScale =
width / intrinsicWidth;
1128 auto yScale =
height / intrinsicHeight;
1144 return m_rootElement->getElementById(
id);
1149 return m_rootElement.get();
1155 m_rootElement->layoutIfNeeded();
1156 return m_rootElement.get();
bool isNull() const
Checks if the bitmap is null.
void clear(const Color &value)
Clears the bitmap with the specified color.
Represents a 2D axis-aligned bounding box.
Box & transform(const Matrix &matrix)
Transforms the box using the specified matrix.
Box()=default
Constructs a box with zero dimensions.
Box transformed(const Matrix &matrix) const
Returns a new box transformed by the specified matrix.
Represents a color in RGBA format (0xRRGGBBAA).
uint32_t value() const
Convert the color to a 32-bit unsigned integer.
Bitmap renderToBitmap(int width=-1, int height=-1, const Color &backgroundColor=Color(0, 0, 0, 0)) const
Renders the document to a bitmap with specified dimensions.
Document & operator=(Document &&)
void forceLayout()
Forces an immediate layout update.
float width() const
Returns the intrinsic width of the document in pixels.
void render(Bitmap &bitmap, const Matrix &matrix=Matrix()) const
Renders the document onto a bitmap using a transformation matrix.
Element elementFromPoint(float x, float y) const
Returns the topmost element under the specified point.
Element getElementById(const std::string &id) const
Retrieves an element by its ID.
static std::unique_ptr< Document > loadFromData(const std::string &string)
Load an SVG document from a string.
Element documentElement() const
Retrieves the document element.
float height() const
Returns the intrinsic height of the document in pixels.
Box boundingBox() const
Returns the smallest rectangle that encloses the document content.
static std::unique_ptr< Document > loadFromFile(const std::string &filename)
Load an SVG document from a file.
void updateLayout()
Updates the layout of the document if needed.
void setAttribute(const std::string &name, const std::string &value)
Sets the value of an attribute.
Box getBoundingBox() const
Retrieves the bounding box of the element without any transformations.
Matrix getGlobalMatrix() const
Retrieves the global transformation matrix of the element.
Bitmap renderToBitmap(int width=-1, int height=-1, const Color &backgroundColor=Color(0, 0, 0, 0)) const
Renders the element to a bitmap with specified dimensions.
void render(Bitmap &bitmap, const Matrix &matrix=Matrix()) const
Renders the element onto a bitmap using a transformation matrix.
Box getLocalBoundingBox() const
Retrieves the local bounding box of the element.
Matrix getLocalMatrix() const
Retrieves the local transformation matrix of the element.
Box getGlobalBoundingBox() const
Retrieves the global bounding box of the element.
NodeList children() const
Returns the child nodes of this node.
const std::string & getAttribute(const std::string &name) const
Retrieves the value of an attribute.
Element()=default
Constructs a null element.
bool hasAttribute(const std::string &name) const
Checks if the element has a specific attribute.
Represents a 2D transformation matrix.
void reset()
Resets this matrix to the identity matrix.
static Matrix sheared(float shx, float shy)
Creates a shearing matrix with the specified factors.
Matrix & multiply(const Matrix &matrix)
Multiplies this matrix with another matrix.
Matrix & rotate(float angle, float cx=0.f, float cy=0.f)
Rotates this matrix by the specified angle around a point.
static Matrix rotated(float angle, float cx=0.f, float cy=0.f)
Creates a rotation matrix with the specified angle around a point.
static Matrix translated(float tx, float ty)
Creates a translation matrix with the specified offsets.
Matrix & invert()
Inverts this matrix.
Matrix operator*(const Matrix &matrix) const
Multiplies this matrix with another matrix.
Matrix & shear(float shx, float shy)
Shears this matrix by the specified factors.
Matrix & translate(float tx, float ty)
Translates this matrix by the specified offsets.
Matrix()=default
Initializes the matrix to the identity matrix.
Matrix & operator*=(const Matrix &matrix)
Multiplies this matrix with another matrix in place.
Matrix & scale(float sx, float sy)
Scales this matrix by the specified factors.
Matrix inverse() const
Returns the inverse of this matrix.
static Matrix scaled(float sx, float sy)
Creates a scaling matrix with the specified factors.
Node()=default
Constructs a null node.
bool isNull() const
Checks if the node is null.
bool isElement() const
Checks if the node is an element node.
TextNode toTextNode() const
Converts the node to a TextNode.
Element toElement() const
Converts the node to an Element.
bool isTextNode() const
Checks if the node is a text node.
bool operator==(const Node &node) const
Checks if two nodes are equal.
bool operator!=(const Node &node) const
Checks if two nodes are not equal.
Element parentElement() const
Returns the parent element.
const std::string & getAttribute(std::string_view name) const
virtual Rect paintBoundingBox() const
virtual Transform localTransform() const
bool hasAttribute(std::string_view name) const
const SVGNodeList & children() const
virtual void render(SVGRenderState &state) const
bool setAttribute(std::string_view name, const std::string &value)
SVGElement * elementFromPoint(float x, float y)
SVGElement * parentElement() const
SVGRootElement * rootElement() const
virtual bool isTextNode() const
virtual bool isElement() const
float intrinsicHeight() const
float intrinsicWidth() const
SVGRootElement * layoutIfNeeded()
Transform localTransform() const override
void render(SVGRenderState &state) const override
const std::string & data() const
void setData(const std::string &data)
const std::string & data() const
Returns the text content of the node.
void setData(const std::string &data)
Sets the text content of the node.
TextNode()=default
Constructs a null text node.
struct surface surface_t
Represents an image surface for drawing operations.
NOVASVG_INLINE unsigned int color_to_argb32(const color_t *color)
Converts a color to a 32-bit unsigned ARGB value.
NOVASVG_INLINE int color_parse(color_t *color, const char *data, int length)
Parses a color from a string using CSS color syntax.
Main namespace for the NovaSVG library.
std::vector< Node > NodeList
std::vector< Element > ElementList
constexpr float operator*(const Point &a, const Point &b)
int version()
Returns the version of the novasvg library encoded in a single integer.
const std::string emptyString
std::string versionString()
Returns the novasvg library version as a human-readable string in "X.Y.Z" format.
void(* novasvg_destroy_func_t)(void *closure)
Callback for cleaning up resources.
void(* novasvg_write_func_t)(void *closure, void *data, int size)
A function pointer type for a write callback.
#define NOVASVG_VERSION_STRING
Represents a color with red, green, blue, and alpha components.
A structure representing a 2D transformation matrix.
A structure representing a rectangle in 2D space.
std::string text
UTF-8; ' ' marks a forced line break ( ,.