18

I want to parse SVG files in C++ on OS X, and I need a library. It should be simple, lightweight and should not have many dependencies. My SVG files are also simple - they contain only simple shapes and paths like rectangles and transformation info. Do you know of any parsers?

Note: I want to get beyond XML struct parsed shape informations like rectangle width, height and path points, in floats etc. I already tried libtinysvg, and it does not event parse most of w3c samples and has a few dependencies of its own!

  • 1
    Parsing svg is trivial; it's just a xml. However interpreting it is another matter. Any library worth publishing will try to interpret it reasonably completely, but that requires a fairly featureful vector drawing library that will appear as dependency. – Jan Hudec May 28 '14 at 17:07
  • By the tags & text, I think you are trying to say that you prefer a C++ solution, but could live with C - am I correct? And, since this is an old question, did you ever find a solution? Could you share it? – Mawg Mar 2 '17 at 10:04
8

New SVG++ library is a good choice for SVG reading in C++, except that it is not lightweight and requires Boost library. But as it is header-only library and uses only header-only libraries from Boost, you only need to fetch both SVG++ and Boost and add them to include paths, no building required.

6

The QT Framework includes QT SVG, a module able to generate and render SVG files.

4

You have a few options:

  • librsvg from the Cairo Graphics project
  • extend libsvgtiny to meet your needs
  • extract the SVG layer from Amaya
  • use an xml parser and then parse properties as needed
  • for C++ and significant dependencies: QT, webkit or chromium

Note on libsvgtiny: It is developed by the Netsurf Browser project and has very minimal dependencies that many other projects would have probably bundled in, but they intentionally separate out different functionality (they also parse html, xml and css, so the common parsing bits are a separate library). I have built a basic svg viewer from their example code in nsfb that static compiled down to under 300kb with the libxcb backend using musl-libc (glibc + libx11 + librsvg + all their dependencies is well over 10x that)

Note on librsvg: It is the most commonly used, but has significantly more dependencies.

Note on Amaya: Not in active development, only mentioned in case it is close to what you are trying to do to begin with. Versions prior to 9.x only required gtk+-1.X (now Wx which is cross platform)

Note on QT: if you are using C++ wanting a cross platform gui, then this is probably a good choice, since many C++ gui toolkits (fltk, FOX, etc...) don't have builtin svg capability

webkit or chromium embedded: These are cross platform and offer plenty of overkill. You will not only get SVG but also a lot of stuff you may not need... but since all programs evolve until they can do mail, this will put you ahead of the game later.

Xml parsers: Too many to mention, but you may be able to extract a bit of code from tinysvg for additional properties

My recommendation: start with svgtiny and add any capabilities that you need. The Netsurf developers are extremely open and helpful.

2

SVG is a special type of XML (source). So any C/C++ library that allows you to parse XML will also allow you to parse SVG.

Many libaries are posted as answers to the question Best open XML parser for C++. Some of them are

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.