首页 > 解决方案 > 如何在 Qt/hook QPainter 中渲染没有抗锯齿的 SVG 以避免抗锯齿?

问题描述

我需要在 Qt中渲染一个没有抗锯齿的纯矢量 SVG;具体来说,我不希望生成的图像包含与源文件中指定的颜色不完全相同的任何颜色。

在 a 中加载 SVG 时QImage,Qt 默认使用抗锯齿;即使使用显式QSvgRenderer::renderQPainter::Antialiasing渲染提示也会被忽略。

事实证明这是显而易见的,因为QSVGTinyDocument::draw似乎是实际开始绘图的人正在强制它设置

//sets default style on the painter
//### not the most optimal way
mapSourceToTarget(p, bounds);
QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin);
pen.setMiterLimit(4);
p->setPen(pen);
p->setBrush(Qt::black);
p->setRenderHint(QPainter::Antialiasing);
p->setRenderHint(QPainter::SmoothPixmapTransform);

为了规避这一点,我尝试创建一个自定义QPaintEngine包装默认包装(然后子类甚至QImage返回虚拟方法QPaintEngine中的包装paintEngine()),但我找不到任何干净的方法来拦截和更改渲染提示到实际QPaintEngine.

QPaintEngine::update()方法似乎是 aQPaintEngine接收标志的方式,接收对 a 的引用QPaintEngineState,这是一个可怕的 kludge,只提供 getter,而这反过来

此外,即使只是提供一个just-forwarding也会QPaintEngine导致QPainter代码内部出现段错误,所以我很快就停止了。


长话短说:

标签: c++qtsvgantialiasing

解决方案


The only solution I can think of is to implement your own SVG renderer. This is trivial using nanosvg, yet few people decide to do this. Also, like almost every other parser out there, nanosvg does not support every SVG feature (like filters, ...). Anyway, here's an example. You will have complete control, whether you want (anti)aliasing or not.


推荐阅读