Wrong font size with QPainter::drawText
-
Hello, this is my first posting. Unfortunately I do not found any information in the forum, which can help me to solve my problem.
For several months I am working on developing an application for an embedded Linux system. For graphical output I use Qt Embedded in version 4.5.3.
Till now I do not have any serious problems. Now I've come across a problem that I can not solve.
Actually, I only want to display some text strings at particular position with a certain size and certain font. Selecting the desired font works fine. But the position and size of the displayed text string does not match with my expectance. As a result, I get the correct text on the screen. But the characters are much smaller than they are set in the code.
An example:
I want to display a text with the Courier New font in a size of 20.
With this settings in a raster graphics editor or in a word processor I get characters which are about 30 pixels high.!http://s1.directupload.net/images/111109/o3ahjm9v.jpg(Highly enlarged view of displayed text in raster graphics editor in a pixel grid)!
(Highly enlarged view of displayed text in raster graphics editor in a pixel grid)If I display the same text with the same font setting on my embedded Qt device in a rectangle with a height of 30 pixels, the characters occupy in height only about 2 / 3 of the rectangle.
!http://s7.directupload.net/images/111109/hs65h4c2.jpg(Photo from the text output on the screen of my embedded device)!
(Photo from the text output on the screen of my embedded device)For the output of the string in the paint event I use:
Qfont::setPixelSize oder Qfont::setPointSize to set the size of the font
and
QPainter::drawText ( int x, int y, int width, int height, int flags, const QString & text, QRect * boundingRect = 0 ) to display the text.@/* White square: size and position, such as text area below */
brush.setColor(QColor(0xFFFFFFFF));
painter.setBrush(brush);
painter.drawRect(100, 100, 32, 30);
painter.setBrush(Qt::NoBrush);/* display text */
pen.setColor(0xFF000000);
painter.setPen(pen);
font.setFamily((QFontDatabase::applicationFontFamilies(0).at(0).toLocal8Bit().constData()));
font.setPointSize(20);
//font.setPixelSize(20); //alternative
painter.setFont(font);@In the startup script of the device i have found those entries:
@export QWS_DISPLAY=LinuxFb:mmWidth=95.0 :mmHeight=54.0:/dev/fb0
export QWS_SIZE=480x272@
These values are correct.How can I solve this problem?
I am grateful for your helpful postings.
-
The handling with fonts is not something trivial, it's easy to fall in a lot of pitfall. The first one is the definition of the unit of the height of a font: what means a font a 20 in term of unit? 20 pixel, 20 mm, ... in fact it's 20 pica (or point) which is a unit defined by the typograph and it's 1/72 inch (see "wiki:pica":http://en.wikipedia.org/wiki/Pica_(typography)).
Now using a word processor, the word processor convert the size of the page (normally a A4 paper size: ~20cmx~30cm) using the size of the screen and the density of pixel of you screen (dpi) to represent exactly (at zoom level 100%) th page and the famous: 20pica = 20x4.2mm = 8.4 cm.
Now there is different way to measure the height of a font, or the glyph size with normally ascent + descent (top and bottom of the baseline), or the distance between 2 baseline.
Finally even you do all fine until here, you could have other problem due to font limitation, like non scalable. Which means you don't have the desired size, and the draw engine use the first possible smaller size.
Now to return to Qt, you have normally all the information necessary using QFontMetrics. I hope I havne't afraid you, but if you want to do like a word processor (or like PDF) it's an art (Adobe is exactly famous because of their fonts)!
-
@stiefeljuice: Take a look at "QFontMetricsF::boundingRectF()":http://developer.qt.nokia.com/doc/qt-4.7/qfontmetricsf.html .
-
[quote author="BilbonSacquet" date="1321564649"]The handling with fonts is not something trivial, it's easy to fall in a lot of pitfall. The first one is the definition of the unit of the height of a font: what means a font a 20 in term of unit? 20 pixel, 20 mm, ... in fact it's 20 pica (or point) which is a unit defined by the typograph and it's 1/72 inch (see "wiki:pica":http://en.wikipedia.org/wiki/Pica_(typography)).
Now using a word processor, the word processor convert the size of the page (normally a A4 paper size: ~20cmx~30cm) using the size of the screen and the density of pixel of you screen (dpi) to represent exactly (at zoom level 100%) th page and the famous: 20pica = 20x4.2mm = 8.4 cm.
Now there is different way to measure the height of a font, or the glyph size with normally ascent + descent (top and bottom of the baseline), or the distance between 2 baseline.
Finally even you do all fine until here, you could have other problem due to font limitation, like non scalable. Which means you don't have the desired size, and the draw engine use the first possible smaller size.
Now to return to Qt, you have normally all the information necessary using QFontMetrics. I hope I havne't afraid you, but if you want to do like a word processor (or like PDF) it's an art (Adobe is exactly famous because of their fonts)! [/quote]
Thank you very much for the detailed answer.
I was afraid that it would not be trivial.Is there any way to set the Qt font rendering parameters?
It must not be as precise like in Adobe's PDF. -
In addition to the TrueType Fonts Qt on the device supports also other font types:
- PostScript Type1 (PFA / PFB)
- Bitmap Distribution Format (BDF)
- CID-keyed Type 1
- Compact Font Format (CFF)
- OpenType fonts
- SFNT-based bitmap fonts
- Portable Compiled Format (PCF)
- Microsoft Windows Font File Format (Windows FNT)
- Portable Font Resource (PFR)
Can I use some of these font standards to achive more accurate result?