简体   繁体   English

如何显示拉伸字体(双倍宽度/高度)?

[英]How to display stretched font (double width/height)?

I would like to represent on screen what a thermal printer prints like. 我想在屏幕上代表热敏打印机的打印效果。

The printer has special formatting and can print a font with double height or double width, so I have searched something but neither Html nor Rich Text seem to have an option to do this. 打印机具有特殊的格式,可以打印具有双倍高度或双倍宽度的字体,因此我进行了搜索,但HTML和RTF似乎都没有选择权。 I also looked at QTextBlock but couldn't find something like stretch or width ratio propriety. 我还查看了QTextBlock但找不到类似拉伸或宽度比的QTextBlock

Is drawing the font pixel by pixel the only way? 逐像素绘制字体是唯一的方法吗?

The question conflates two issues: 这个问题概括了两个问题:

  1. How to draw stretched fonts? 如何绘制拉伸字体? It's easy: change the horizontal or vertical scaling of the QPainter . 这很容易:更改QPainter的水平或垂直缩放比例。 Qt will do the rest for you. Qt将为您完成其余的工作。 Eg: 例如:

     QPainter p; p.setTransform(QTransform(2.0, 0, 0, 1.0, 0, 0))); p.drawText(0, 0, "STRETCHED"); 
  2. How to express stretch in rich text? 如何用富文本表示拉伸? That's not directly possible without modifying Qt sources - although such modifications would be quite simple. 如果不修改Qt源,那是不可能直接实现的-尽管这样的修改非常简单。 Otherwise, you can pre-render the text to an image, and use the image instead of text. 否则,您可以将文本预渲染为图像,然后使用图像代替文本。

In addition to Kuba Obre's suggestion, I have also found that QFont has the method setStretch() that does exactly what I need. 除了Kuba Obre的建议外,我还发现QFont具有方法setStretch()我的需要。

Then there are two ways to use that stretched QFont to render a piece of document: 然后,有两种方法可以使用扩展的QFont呈现文档:

  1. create a Rich Text Document ( QTextDocument ). 创建一个RTF文档( QTextDocument )。 I didn't try, but the procedure seems to be the following (it is easier to create a new RTF rather than load the whole text and then edit the desired pieces of text). 我没有尝试,但是过程似乎如下(创建新的RTF而不是加载整个文本然后编辑所需的文本要容易得多)。
    QTextCursor::setCharFormat(const QTextCharFormat & format) , using QTextCharFormat::setFontStretch(int factor) directly (no need to pass through QFont). QTextCursor::setCharFormat(const QTextCharFormat & format) ,直接使用QTextCharFormat::setFontStretch(int factor) (无需通过QFont)。 Then QTextCursor::insertText(const QString & text) . 然后QTextCursor::insertText(const QString & text)

  2. use QPainter with the stretched font. 使用QPainter和拉伸字体。 QPainter::setFont(const QFont & font) then QPainter::drawText() QPainter::setFont(const QFont & font)然后QPainter::drawText()

The second method is faster, but need to struggle with coordinates. 第二种方法更快,但是需要与坐标进行斗争。 The first method is longer but renders like a text document. 第一种方法更长,但是呈现得像文本文档一样。 I will evaluate which one is better for me. 我将评估哪个更适合我。

Hope it is useful to others, a. 希望它对其他人有用。

This is a working example using QTextDocument (Rich text format creation) 这是使用QTextDocument的工作示例(富文本格式创建)

Let's have a QTextBrowser widget in our ui, called browser 让我们在用户界面中拥有一个QTextBrowser小部件,称为browser

QFont font; //we can optionally .setFamily()
QTextDocument * rtf = new QTextDocument(this);
//QTextCursor is needed to write into the TextDocument (otherwise it is readonly)
QTextCursor * editor = new QTextCursor(rtf);
QTextCharFormat format = QTextCharFormat();
editor->insertText("Normal Text\n");
//prepare font for wide text
font.setStretch(200);
format.setFont(font);
editor->setCharFormat(format);
editor->insertText("WIDE text\n");
//prepare font for narrow text
font.setStretch(50);
format.setFont(font);
editor->setCharFormat(format);
editor->insertText("narrow text\n");

ui->browser->setDocument(rtf);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM