An open API service indexing awesome lists of open source software.

https://github.com/vincentlaucsb/svg

A simple header-only library for generating SVG files from C++
https://github.com/vincentlaucsb/svg

cpp svg

Last synced: 12 months ago
JSON representation

A simple header-only library for generating SVG files from C++

Awesome Lists containing this project

README

          

# SVG for C++

[![Build Status](https://travis-ci.org/vincentlaucsb/svg.svg?branch=master)](https://travis-ci.org/vincentlaucsb/svg) [![codecov](https://codecov.io/gh/vincentlaucsb/svg/branch/master/graph/badge.svg)](https://codecov.io/gh/vincentlaucsb/svg)

## Purpose
This a header-only library for generating SVG files from a simple C++ interface. It can also perform non-trivial tasks such as calculating a bounding box for an SVG's elements, or merging several graphics together.

[Want to see more? Read the documentation.](https://vincentlaucsb.github.io/svg/)

## Basic Usage

```
#include "svg.hpp"
#include

int main() {
SVG::SVG root;

// Basic CSS support
root.style("circle").set_attr("fill", "#000000")
.set_attr("stroke", "#000000");
root.style("rect#my_rectangle").set_attr("fill", "red");

// Method 1 of adding elements - add_child<>()
auto shapes = root.add_child();
auto rect = shapes->add_child("my_rectangle");

// Method 2 of adding elements - operator<<
*shapes << SVG::Circle(-100, -100, 100) << SVG::Circle(100, 100, 100);

// Reference elements by id, tag, class name, etc...
root.get_element_by_id("my_rectangle")
->set_attr("x", 20).set_attr("y", 20)
.set_attr("width", 40).set_attr("height", 40);

std::cout << "There are " << root.get_children().size() <<
" circles." << std::endl;

// Automatically scale width and height to fit elements
root.autoscale();

// Output our drawing
std::ofstream outfile("my_drawing.svg");
outfile << std::string(root);
}
```

### Output

```


<![CDATA[
circle {
fill: #000000;
stroke: #000000;
}
rect#my_rectangle {
fill: red;
}
]]>





```

## Simple Animations
This package supports creating basic animations via CSS keyframes via the frame_animate() function.