21

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!

2
  • 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, 2014 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? Mar 2, 2017 at 10:04

4 Answers 4

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

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.

6

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

0
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

1
  • 1
    Unfortunately all of the interesting parts of SVG are stored in attributes of it's XML schema, so a plain XML parser is essentially worthless as it only does about 10% of the job of interpreting SVG. Feb 4, 2020 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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