How remove extra spaces from character using QFontMetrics?
-
Hi, I'm studying QPainter my goal is to get the width size of a character with QFontMetrics. The problem is that width is returned with extra spaces around character. I don't want this.
As you can see Hello! has between characters some space, it's not near vertical line green o yellow.
-
@zabitqt
I do not know what these extra spaces you show are/come from. Certainly I would not expect printingHello
in a font to produce as much spacing as that. There is a picture at int QFontMetrics::horizontalAdvance(QChar ch) const, but I don't think your spacing is to do with that? Suggest you provide a minimal compilable example. -
Smart people with knowledge of typography have decided this behavior long before computers were a thing. If you don't know typography it is a bad idea to change this.
Have a look at kerning and letter spacing inside the QFont class.
-
@zabitqt
I do not know what these extra spaces you show are/come from. Certainly I would not expect printingHello
in a font to produce as much spacing as that. There is a picture at int QFontMetrics::horizontalAdvance(QChar ch) const, but I don't think your spacing is to do with that? Suggest you provide a minimal compilable example. -
@JonB If I draw Hello I see it correctly but if I draw character per character I see a space between character.
-
@JonB If I draw Hello I see it correctly but if I draw character per character I see a space between character.
@zabitqt said in How remove extra spaces from character using QFontMetrics?:
If I draw Hello I see it correctly but if I draw character per character I see a space between character.
This is because the font has embedded information how to adjust the distance between specific neighboring characters. If you draw them separately, this information is lost for the font renderer.
Here is the definition of kerning from Wikipedia:
In typography, kerning is the process of adjusting the spacing between characters in a proportional font, usually to achieve a visually pleasing result. Kerning adjusts the space between individual letterforms.
I am not sure if Qt provides any way to get to this kind of information. You can disable kerning (for a whole word and not individual letters) and see if you get the same result when you are drawing the letters separately. If this is the case you know that you need to further research kerning.