How to Use LunaSVG: A Lightweight SVG Rendering Library in C++

Sep 23, 2022 | Programming

LunaSVG is a lightweight and portable library designed for rendering and manipulating Scalable Vector Graphics (SVG) files in C++. Its efficient handling makes it a great choice for projects that need a robust SVG solution without the bloat. In this article, we will guide you through the basic usage, installation, and provide troubleshooting tips to make your experience smooth sailing.

Basic Usage of LunaSVG

To get started with LunaSVG, you’ll need to load an SVG file and convert it into a bitmap format, such as PNG. This process can be likened to painting a beautiful scene: first, you gather your supplies (the SVG file), then you create your canvas (the bitmap), and finally, you apply your colors (rendering it as an image) for everyone to admire!

Here’s a simple example:

#include <lunasvg.h>
using namespace lunasvg; 

int main() {
    auto document = Document::loadFromFile("tiger.svg");
    if (document == nullptr)
        return -1; 
    
    auto bitmap = document->renderToBitmap(); 
    if (bitmap.isNull())
        return -1; 

    bitmap.writeToPng("tiger.png");
    return 0;
}

In this snippet:

  • Document::loadFromFile("tiger.svg"); loads the SVG file.
  • document->renderToBitmap(); renders the loaded document to a bitmap.
  • bitmap.writeToPng("tiger.png"); saves the bitmap as a PNG file.

Installation

You can install LunaSVG using either Meson or CMake. Here are the steps for both methods:

Using Meson

git clone https://github.com/sammycagelunasvg.git
cd lunasvg
meson setup build
meson compile -C build
meson install -C build

Using CMake

git clone https://github.com/sammycagelunasvg.git
cd lunasvg
cmake -B build .
cmake --build build
cmake --install build

Demo: Converting SVG to PNG

LunaSVG comes with a handy command-line tool, svg2png, for converting SVG files to PNG format. Think of it as your personal digital assistant, transforming your intricate SVG designs into widely usable PNG graphics effortlessly!

Usage

svg2png [filename] [resolution] [bgColor]

Examples

$ svg2png input.svg
$ svg2png input.svg 512x512
$ svg2png input.svg 512x512 0xff00ffff

Features You Should Know

LunaSVG supports a wide range of graphical features as outlined in the SVG 1.1 and SVG 1.2 Tiny specifications. Some of the elements it can handle include:

  • circle
  • clipPath
  • ellipse
  • g
  • image
  • line
  • linearGradient
  • marker
  • mask
  • path
  • polygon
  • polyline
  • rect
  • text
  • svg

However, note that LunaSVG does not support animation, filters, and scripts as it is primarily designed for static rendering. This specialization allows for a more lightweight and efficient library.

Troubleshooting

If you encounter any issues while using LunaSVG, consider the following troubleshooting tips:

  • Error Loading File: Ensure that your SVG file is properly formatted and exists in the specified directory.
  • Bitmap Null: Check if the SVG file has any unsupported features or if the file is corrupted. Ensure that you are following the SVG standards.
  • Installation Problems: Make sure you have the necessary permissions to install the library, and verify that all dependencies are met.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Projects Using LunaSVG

Many projects have successfully adopted LunaSVG for their graphical needs. Here are a few notable ones:

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox