2

有人可以提供一个如何加载 .svg 文件并使用 C/C++ 和任何库显示它的示例吗?我想知道您是否会使用 SDL、cairo 或什么。

4

5 回答 5

3

地狱通,

查看http://cairographics.org/cairomm/

可能会让事情变得容易一些。

于 2009-08-31T19:39:04.497 回答
2

正如 Pavel 所说,我相信QtSvg是正确的选择。它更易于使用,但在我们的团队中,我们遇到了 QtSvg 的性能问题,尤其是在 Linux 上。所以我们决定直接手动解析 SVG 文件 XML 并使用 Qt 本身进行渲染。事实证明这要快得多。

伪代码:-

// Read the SVG file using XML parser (SAX)
void SvgReader::readFile()
{
  QFile file(<some svg filename>);
  if (!file.open(QFile::ReadOnly | QFile::Text)) {
    qWarning("Cannot open file");
    return;
  }
  QString localName;
  QXmlStreamAttributes attributes;
  pXml = new QXmlStreamReader(&file);
  pXml->setNamespaceProcessing(false);
  while (!pXml->atEnd()) {
    switch (pXml->readNext()) {
        case QXmlStreamReader::StartElement:
          localName = pXml->name().toString();
          if (localName.compare(<some element path>) == 0) {
            attributes = pXml->attributes();
            QStringRef data  = attributes.value(<some attribute name>);
            // parse/use your data;
          }
        // similarly other case statements 
    }
  }
}
于 2009-09-01T03:32:51.750 回答
1

QtSvg模块可能会有所帮助。

于 2009-08-31T19:45:14.500 回答
1

你可以试试boost.svg_plot

于 2009-09-01T06:36:52.753 回答
0

你可以试试lunasvg

#include <lunasvg/document.h>

int main()
{
    auto document = Document::loadFromFile("example.svg");
    auto bitmap = document->renderToBitmap();

    // do something useful with bitmap here.
}

于 2020-07-06T12:02:17.943 回答