Brad
Brad

Reputation: 10660

Render a vector graphic (.svg) in C++

My and a friend are working on a 2D game where the graphics will be .svg files and we will scale them appropriately either by rasterizing them first, or rendering them directly on a surface (which still would require rasterization at some point).

The problem is, I've been looking all day to find a library that will allow me to take an .svg file and eventually get it to render in allegro. As far as I know, it would involve rasterization into some sort of format that allegro can read and then allegro could render the "flattened" image.

So what are some C++ libraries I could use for taking an .SVG file and "flattening" it so I can render it? The library obviously needs to support scaling too so I can scale the vector graphic then rasterize it.

I'm using Windows and Visual C++ Express 2010.

I've tried Cairo, but it only allows writing of .svg files and doesn't allow you to read the .svg file. I've also looked into librsvg which works with Cario, but I was having a lot of trouble getting it to work properly on Windows (because it has loads of GNOME dependencies). If you have any guides for getting these to work (on Windows) that would be great too.

Upvotes: 13

Views: 41427

Answers (8)

user1095108
user1095108

Reputation: 14603

With the help of nanosvg and many other c++ svg parsers, adding svg rendering capability to your application should be trivial. The recipe is as follows: svg parser + vector rendering library = trivial svg rendering. The vector rendering library can be cairo and a number of other libraries (nanovg comes to mind, as well as a number of other vg libraries). Here's an example of how to support svg rendering with cairo + fltk + nanosvg combo. Now, all the svg parsers, as well as cairo itself, along with other renderers, have bugs/shortcomings, but basic svg support should never present a problem.

Upvotes: 1

igagis
igagis

Reputation: 2079

I have recently put together an SVG renderer library in C++: https://github.com/igagis/svgren

It uses AGG for rendering to off-screen surface. Supports gradients and all kind of shapes.

Upvotes: 2

MNS
MNS

Reputation: 1394

I was looking for a real quick way to render SVG file in a Windows OS based MFC project.

In that case, Microsoft Browser Web Browser ActiveX Control is found to be an ideal solution.

enter image description here

And here is the result of loading SVG file using the Browser control.

Rendering SVG file using a Browser Control

Upvotes: 0

Samuel
Samuel

Reputation: 51

https://github.com/sammycage/lunasvg is a nice svg parsing, rendering and manipulating library. It is written with pure c++

Upvotes: 5

Andrey A. Ugolnik
Andrey A. Ugolnik

Reputation: 51

Personally, I using NanoSVG in my Simple Viewer GL. It's allowing me easy to load and rasterize SVG images in few lines of code. But this library has weak SVG support.

Upvotes: 1

Vijay Mathew
Vijay Mathew

Reputation: 27164

The wxsvg library allows loading and manipulating SVG files. Qt also has an SVG module.

Upvotes: 10

Jaime Ivan Cervantes
Jaime Ivan Cervantes

Reputation: 3697

I'm coming a little late to the conversation, but I would suggest you to look at Nano SVG, an extremely lightweight svg renderer that doesn't need cairo/libsvg. I got nanosvg compiled and working in a couple of hours. It's very basic, but it gets the job done.

Upvotes: 7

OlegMax
OlegMax

Reputation: 325

SVG++ library provides advanced support for SVG reading, so that rendering SVG with allegro can be implemented in reasonable time.

Upvotes: 2

Related Questions