加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

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

发布时间:2020-12-25 07:02:29 所属栏目:Windows 来源:网络整理
导读:我想对某些自定义小部件渲染进行可重复的测试.为了做到这一点,我将它们绘制成一个Q Image,并将结果保存为PNG.与MacOSX相比,Windows的输出真的不同. 我照顾: 在所有平台上选择相同的字体(我提供“TTF”字体文件并将代码指向它) 绘制QImage而不是QPixmap,正如

我想对某些自定义小部件渲染进行可重复的测试.为了做到这一点,我将它们绘制成一个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);

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读