显示 HN:Svglib,一个用于 Windows 的 SVG 解析器和渲染器。
Show HN: Svglib a SVG parser and renderer for Windows

原始链接: https://github.com/bibhas2/svglib

## svglib:Windows轻量级SVG渲染器 svglib是一个Windows库,用于解析和渲染SVG文件,旨在易于集成到Win32应用程序和游戏中。它利用内置的Windows组件——Direct2D用于GPU加速渲染,XMLLite用于XML解析——无需外部依赖。 要使用svglib,请克隆仓库并在Visual Studio Community Edition(启用C++17)中构建解决方案。包含`svglib.h`并链接`svglib.lib`,以及Direct2D、Direct3D、DirectWrite和XMLLite库。 渲染过程包括初始化与窗口(`HWND`)关联的`SVGDevice`,使用`SVG::load()`加载SVG图像,然后在窗口的`WM_PAINT`处理程序中使用`SVG::render()`渲染它。 目前,svglib支持SVG规范的一个有用子集,但缺乏对`textPath`和`clipPath`的支持(未来计划支持)。**重要安全提示:** svglib信任SVG文件且不执行验证,因此仅使用受信任的来源。开发者应确保正确的`SVGDevice`初始化,以避免不可预测的行为。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 展示 HN: Svglib,一个用于 Windows 的 SVG 解析器和渲染器 (github.com/bibhas2) 4 分, leopoldj 发表于 1 小时前 | 隐藏 | 过去 | 收藏 | 讨论 svglib 是一个用于 Windows 的 SVG 文件解析器和渲染器库。它使用 Direct2D 进行 GPU 加速渲染,并使用 XMLLite 进行 XML 解析。 这旨在让 Win32 应用程序和游戏能够轻松显示 SVG 图像。 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 我们 搜索:
相关文章

原文

svglib is a SVG file parser and renderer library for Windows. It uses Direct2D for GPU assisted rendering and XMLLite for XML parsing. They are core components of Windows. You don't need to download any external libraries to compile and distribute applications that use svglib.

This is meant for Win32 applications and games to easily display SVG images. Just enough of the SVG spec is covered for the library to be useful in most situations.

You will need Visual Studio Community Edition with C++ language support.

Clone this repo.

git clone [email protected]:bibhas2/svglib.git

Clone mgui. This is used by some of the test applications only and not by svglib.

git clone [email protected]:bibhas2/mgui.git

Open the solution svglib/svglib.sln in Visual Studio Community Edition and build the solution.

Include the header file svglib.h. Link with the static library svglib.lib.

You will also need to link with Direct2D, Direct3D, DirectWrite etc. This is best done by adding these lines to one of the C++ files of your application.

#pragma comment(lib, "D3D11.lib")
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "xmllite.lib")
#pragma comment(lib, "dwrite.lib")

Finally, enable support for C++ 17 for your project.

For each HWND to be used to display a SVG, you need a SVGDevice.

SVGDevice device;
HWND m_wnd = ...;

//Setup the device
bool status = device.init(m_wnd);

//Check status

Each SVG image is reprsented by a SVGImage. We can load an image like this.

SVGImage image;

if (SVG::load(L"file.svg", device, image)) {
    //Force a WM_PAINT event
	device.redraw();
}
else {
	//Show error message
}

Render the image from the WM_PAINT handler of the window.

bool handle_event(UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
    case WM_PAINT:
        PAINTSTRUCT ps;

        //Call BeginPaint and EndPaint, or else we will get continuous
        //WM_PAINT messages.
        BeginPaint(m_wnd, &ps);

        //Clear the device with a white background
        SVG::clear(device);
        //Render the SVG
        SVG::render(device, image);

        EndPaint(m_wnd, &ps);
        break;
    case WM_SIZE:
        //Resize the device area
        device.resize();
        break;
    case WM_ERASEBKGND:
        //Handle background erase to avoid flickering 
        //during resizing and move
        break;
    default:
        //...
    }

    return true;
}

Direct2D and DirectWrite pretty much map the SVG spec 1:1. This made writing svglib fairly trivial. The only exception is textPath. I have no plans to support textPath.

clipPath is also not supported at this time. But, a support is planned in the future.

Security and Vulnerability

Always make sure that the SVGDevice was initialized properly. Using a half initialized device can cause unpredicatble results. The library does not validate the device when it's used later to load and render images.

The SVG image files must be trusted. Please note:

  • No protection is provided by the library for depth limit for recursive XML structures.
  • No overflow check is done for the coordinate values in the SVG file.
联系我们 contact @ memedata.com