Font size calculation when painting in a QImage
Hello,
I'm trying to generate a modified image starting from a preexisting PNG file and drawing some text on it. I'm not getting what I think I should get :)
In particular, I have some issues with font sizes.
I have found that the PNG resolution plays a role in the calculation of the actual pixel size of the font, if I increase the resolution and keep the font size fixed, the actual rendered text is smaller. The sizes do not match what I see if I do the same in inkscape, but this is not a problem. My problem is that there seems to be a "minimum pixel size" which limits the size of the smallest font I can draw. Right now I have the PNG at 72x72 dpi and a font size of 2 looks identical to font size 10.
The code:
Code:
int main(int argc, char **argv)
{
painter
->setRenderHint
(QPainter::TextAntialiasing,
true);
painter->setPen(Qt::black);
painter->setFont(*FontText);
painter
->drawText
(QRect(0,
0,
256,
128), Qt
::TextWordWrap,
"Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
painter->setFont(*FontText2);
painter
->drawText
(QRect(0,
128,
256,
128), Qt
::TextWordWrap,
"Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
label
->setPixmap
(QPixmap::fromImage(*card
));
label->show();
return a.exec();
}
blank72.png is a 256x256 white image with 72dpi resolution.
Side question: is there any easy way to have the text justified?
Re: Font size calculation when painting in a QImage
Remember that you can set not only the point size for the font, but also the pixel size (which is dependent on the resolution).
Re: Font size calculation when painting in a QImage
There still seem to be some kind of "lower limit", even when using setPixelSize.
I've resorted to rendering in a separate image and then drawing this image while scaling it down. Of course it's not very readable, but this is not a problem since the text is more "decoration" than other.
Is there a way to control the lineskip to draw the text lines closer or again it's easier to draw into intermediate images and build the text in blocks?
Thanks in advance.
Re: Font size calculation when painting in a QImage
I guess you'd have to use QTextLayout or implement your own QAbstractDocumentLayout implementation.