QPainter::rotate 禁用绘制文本的抗锯齿功能

发布于 2024-12-13 09:07:00 字数 1112 浏览 6 评论 0原文

我使用 QPainter::setRenderHint(QPainter::Antialiasing, true) 告诉 Qt 我希望它对我所做的任何绘图进行抗锯齿处理(在本例中,使用 drawText() 绘制的文本代码>)。

这工作正常,文本看起来不错,直到我想旋转我正在绘制的像素图,例如

Painter.translate(0, height());
Painter.rotate(-90);

(将 QPainter 逆时针旋转 90 度并将其带回到视图中)

rotate() 的调用似乎对任何绘制的文本禁用抗锯齿 - 文本以正确的旋转绘制,但没有抗锯齿。其他事情似乎不受影响 - 例如,drawLine() 仍然绘制一条很好的抗锯齿线。

知道我做错了什么吗?

编辑:毫不奇怪,将文本添加到路径然后填充该路径会给我抗锯齿、旋转的文本。如果可能的话,我宁愿避开这条路线。

编辑(再次):我尝试在我正在使用的字体上使用 QFont::setStyleStrategy(QFont::PreferAntialias) ,但没有效果。然而,更多的实验表明,像 Arial 这样的基本字体在旋转时仍然会产生抗锯齿文本,而我的自定义字体(Swiss721 BlkCn BT 对于任何感兴趣的人来说)不会。此外,虽然这个问题存在于 Windows 7 上,但我在 Ubuntu 上运行时没有遇到同样的问题。 这篇常见问题解答文章似乎建议 Qt 依靠主机操作系统来处理字体抗锯齿,那么 Windows 在处理这种特定字体(这是 TrueType,就像 Arial 一样)的渲染时可能会遇到什么样的问题?

编辑(上次,我保证):将字体大小提高到 16pt 或以上可以解决问题。问题似乎出在将我的特定字体渲染到 16pt 以下 - 也许与上面博客文章中提到的内容有关?:

在 Windows 2000 上,字体通常不会在一定范围内(例如大小 8-16)进行抗锯齿处理,以使文本更加清晰易读。

I use QPainter::setRenderHint(QPainter::Antialiasing, true) to tell Qt that I want it to antialias any drawing I do (in this case, text drawn with drawText()).

This works fine and the text looks good, until I want to rotate the pixmap I'm drawing to e.g.

Painter.translate(0, height());
Painter.rotate(-90);

(to rotate the QPainter 90 degrees counterclockwise and bring it back into view)

The call to rotate() seems to disable antialiasing for any text drawn - the text is drawn at the correct rotation but without antialiasing. Other things seem unaffected - e.g. drawLine() still draws a nicely antialiased line.

Any idea what I'm doing wrong?

EDIT: Unsurprisingly, adding the text to a path and then filling that path gives me antialiased, rotatated text. I'd rather avoid this route if possible though.

EDIT (again): I've tried using QFont::setStyleStrategy(QFont::PreferAntialias) on the font I'm using, with no effect. However, some more experimentation shows that a basic font like Arial will still produce antialiased text when rotated, whereas my custom font (Swiss721 BlkCn BT for anyone who's interested) will not. Moreover, while this problem exists on Windows 7, I don't have the same issue when running on Ubuntu. This FAQ article would seem to suggest that Qt looks to the host OS to handle font antialiasing, so what kind of issues might Windows have in handling the rendering of this particular font (which is a TrueType, just like Arial)?

EDIT (last time, I promise): Upping the font size to 16pt or above kills the problem. It would seem that the issue is with rendering my particular font below 16pt - perhaps something to do with what was mentioned in the above blog article?:

On Windows 2000 fonts are usually not antialiased within a certain range (say sizes 8-16) to make text more crisp and readable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-12-20 09:07:00

实际上,我最近有机会参与 Qt 代码的这一部分,我认为您所看到的行为与 Qt 中的以下两个错误有关:

如果我没记错的话(不是 100%)确定,您实际看到的是 Windows 上 ClearType 渲染的丢失。当应用转换时,Qt 以抛出 ClearType 信息的方式获取字形像素,因此事物看起来更加锯齿状。

如果您想自己查看代码,最有可能的位置是 /src/gui/text/qfontengine_win.cpp。您也可以尝试关闭 ClearType 并查看它们是否看起来相似。

I've actually had occasion to be in this part of the Qt code recently, and I think the behavior you are seeing is related to the following two bugs in Qt:

If I remember correctly (not 100%) sure, what you are actually seeing is the loss of the ClearType rendering on Windows. When there is a transformation applied, Qt gets the glyph pixels in a way that throws out the ClearType information, so things look more jagged.

If you wanted to look at the code yourself, the most likely place is /src/gui/text/qfontengine_win.cpp. You could also try turning off ClearType and see if they look similar.

楠木可依 2024-12-20 09:07:00

一种“猜测”涉及您的 RenderHint。您在这里使用QPainter::Antialiasing。文档:“表示引擎应该在可能的情况下对基元的边缘进行抗锯齿处理。”线条、矩形等基元。请尝试使用 QPainter::TextAntialiasing 代替。

注意:RenderHints 是标志,因此如果需要的话您可以对它们进行位或运算(听起来确实如此)。

One "guess" concerns your RenderHint. You use QPainter::Antialiasing here. Docu: "Indicates that the engine should antialias edges of primitives if possible." Primitives like lines, rects, etc. Try QPainter::TextAntialiasing instead.

Note: The RenderHints are flags, so you can bit-OR them if need be (and sounds like it is).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文