将像素大小转换为点大小,以在多个平台上显示字体 [英] Convert pixel size to point size for fonts on multiple platforms

查看:85
本文介绍了将像素大小转换为点大小,以在多个平台上显示字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在多个平台之间转换点大小和像素大小的方法.

我有一个必须在多平台上运行的Qt应用程序,包括在平板电脑上的嵌入式Linux.
期望用户可以将应用程序创建的文件保存在桌面(Windows或Linux)上,然后在自定义设备上打开.

数据由图形和文本组成-QcodeicsScene 上的 QGraphicsItem .一些文本项是富文本",因此我们可以更改文本片段的字体.

对于普通文本(包括所有UI文本),我们使用像素大小而不是点大小来获得相似的外观.但是富文本使我反感: QTextCharFormat 没有 pixelSize()选项.仅 setFontPointSize() fontPointSize().我可以先使用 font().setPixelSize()然后使用 setFont(),但是结果是,使用 html()方法进行保存时,我丢失了所有字体信息.(一定是qt错误吗?)

因此,我需要的是能够在所有地方使用像素大小,然后计算点大小以将其设置在段落上(并在读取大小时反转).

但是-像素大小和点大小之间是什么关系?如果我确定了两者,那么对于当前平台上的给定字体,我可以建立某种方程式来使用吗?

编辑-我发现了一个有趣的帖子-它似乎可以满足我的要求,但仅适用于OSX.

解决方案

是的,我认为有可能:

  double ptToPx(double pt,double dpi){返回pt/72 * dpi}double pxToPt(double px,double dpi){返回px * 72/dpi}...双dpi = QGuiApplication :: primaryScreen()-> physicalDotsPerInch();qDebug()<<"12点是"<<ptToPx(12,dpi)<<"px";qDebug()<<"26像素为"<<pxToPt(26,dpi)<<"pt"; 

I need a way to translate point size and pixel size between multiple platforms.

I have a Qt application that must run on multi-platform, including an embedded Linux on a type of tablet.
It is expected that users can save files created by the application, on a desktop (either windows or linux) and open on the custom device.

The data consists of drawings, and text - QGraphicsItems on a QGraphicsScene. Some text items are "rich text", so we can change font on fragments of text.

For normal text, including all UI text, we used pixel size instead of point size to achieve a similar look. But rich text defies me: the QTextCharFormat doesn't have a pixelSize() option. Only setFontPointSize() and fontPointSize(). I can use font().setPixelSize() then setFont() but the result is that when saving, using the html() method, I lose all font information. (Must be a qt bug ?)

So, what I need is to be able to use pixel size everywhere, and then calculate the point size to set it on paragraphs (and go in reverse when reading sizes).

But - what is the relation between pixel size and point size ? If I determine both, for a given font, on the current platform, can I establish some sort of equation to use ?

Edit - I found an interesting post - it seems to do what I want, but it is specific to only OSX. https://stackoverflow.com/a/25929628/1217150
My target platforms, Windows / Linux / OSX but also, especially, a custom tablet running embedded Linux, and possibly in the future Android devices.

Qt 4.8

Edit - using the conversion in answer below, left text using setPixelSize(20) and right text using setPointSize(20 * screenDpi) where

qreal screenDpi = QApplication::desktop()->physicalDpiX() / 72.;

Note the size is not the same... (running in windows, have not yet tested on other platforms)

I even tried

#ifdef Q_OS_WIN32
    qreal screenDpi = QApplication::desktop()->physicalDpiX() / 96.;
#else
    qreal screenDpi = QApplication::desktop()->physicalDpiX() / 72.;
#endif

解决方案

Yes, I think it is possible:

double ptToPx(double pt, double dpi) {
    return pt/72*dpi
}

double pxToPt(double px, double dpi) {
    return px*72/dpi
}

...

double dpi = QGuiApplication::primaryScreen()->physicalDotsPerInch();
qDebug() << "12 pt is" << ptToPx(12, dpi) << "px";
qDebug() << "26 px is" << pxToPt(26, dpi) << "pt";

这篇关于将像素大小转换为点大小,以在多个平台上显示字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆