我照顾:
>在所有平台上选择相同的字体(我提供“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).
谢谢.
根据:
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);