微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows – QT5字体渲染在各种平台上不同

我想对某些自定义小部件渲染进行可重复的测试.为了做到这一点,我将它们绘制成一个Q Image,并将结果保存为PNG.与MacOSX相比,Windows输出真的不同.

我照顾:

>在所有平台上选择相同的字体(我提供“TTF”字体文件并将代码指向它)
>绘制QImage而不是Qpixmap,正如文档所述,QImage画家应该是平台独立的
>我还选择了Antialisating和TextAntialiasing提示
>通过QFontDatabase :: font()请求字体,以便指定pointSize而不是pixelSize

我如何确保渲染在所有平台上完全一样,以便我的测试运行是可重复的?换句话说,是否可能强制QT5在所有平台上使用相同的字体引擎(例如freetype)?

**

我把这个问题解决一个简单的渲染测试程序.
所以代码看起来像:

QFontDatabase fontDb;
fontDb.addApplicationFont(".../fonts/Vera.ttf");

QImage   result(width,height,QImage::Format_RGB32);
QPainter painter(&result);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);


QBrush background(QColor(205,205,205));
painter.fillRect(0,800,600,background);

QFont font = fontDb.font("Bitstream Vera Sans","normal",10);
painter.setFont(font);

painter.setPen(QColor(0,0));
painter.drawText(10,10,"ABCD abcd 01234567");

Bitstream Vera字体可以在fontsquirel.com上下载.

看到MacOSX(左)和Win32(右)的结果,这是非常不同的:

在下面的N1ghtLight的回答和评论之后,在阅读他建议的链接之后,我更改了代码以获得字体:

QFont font = fontDb_->font(("Bitstream Vera Sans",-1);

qreal screenDPI  = QApplication::primaryScreen()->physicalDotsPerInch();
qreal RENDER_DPI = 72;

int pixelSize = (int)((qreal)10 * screenDPI / RENDER_DPI);
font.setPixelSize(pixelSize);

这似乎主要是解决了大小不一的字体的问题.至少在MacOSX上,现在的字体正好是10像素高.在Windows上,虽然字体更薄,更小一些.我还是迷路和困惑…

这是新的结果(左边的MacOSX,右边的Windows).白色刻度表示真正的10像素大小.

以下G_G下面的回答我调整了代码(Linux?移动平台呢?这很复杂…).现在,Windows和MacOSX的输出中的字体都是10个像素,仍然很不一样(左边是MacOSX,右边是Windows).

谢谢.

您的渲染DPI变量对于Windows应为96,对于OSX应为72

根据:
http://www.rfwilmut.clara.net/about/fonts.html

On a Macintosh monitor,the notional resolution is 72 dots-per -inch
(dpi),so that a graphic 72 pixels wide would notionally be 1 inch
wide – though obvIoUsly the actual size would depend on the individual
monitor. However it will always print one inch wide.

But on a Windows monitor the resolution is (usually) 96 dpi. This
means that though the picture is still 72 pixels wide,it will print
at 0.75 inches.

QFont font = fontDb_->font(("Bitstream Vera Sans",-1);
qreal screenDPI  = QApplication::primaryScreen()->physicalDotsPerInch();

#ifdef WINDOWS
qreal RENDER_DPI = 96;
#else
qreal RENDER_DPI = 72;
#endif

int pixelSize = (int)((qreal)10 * screenDPI / RENDER_DPI);
font.setPixelSize(pixelSize);

原文地址:https://www.jb51.cc/windows/369338.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐