JustWe
JustWe

Reputation: 4484

Qt how to create font from image?

I need to use some custom font for numbers in my Qt app, but company ask me to use image as font, font file is not allowed(For example: ttf).

the font image like this:enter image description here

So i wonder is possible to converting the font image to QFont? like a Qt font generator. Or any other solution?

Upvotes: 0

Views: 1629

Answers (2)

Michael B.
Michael B.

Reputation: 145

There is nothing out of the box that supports what you are looking for. You would need to get around this company rule by building your own TrueTypeFont data programmatically from your image, keeping it in memory only and load that into your application with addApplicationFontFromData(const QByteArray & fontData).

Unfortunately I cannot find a single library that does this, and the TTF format appears dauntingly complex. I would suggest looking into this open source project for font creation. Perhaps you can use this as a starting point? http://fontforge.github.io/en-US/

You may want to find out the reason your company is telling you that you cannot use a custom font. (I just remembered reading that QT has difficulties storing a font as a "resource". If it's not due to copyright issues, then you could suggest not using an image, but storing the entire file in the source code in a text format like base-64. Then convert it back into binary at startup, then load it up as a new font from a QByteArray.

Upvotes: 1

Aaron
Aaron

Reputation: 1191

I guess it's about copyright issues ? There are lots of ttf fonts available for completely free commercial use. E.G. have a look at Droid Sans Mono : https://www.google.com/fonts/specimen/Droid+Sans+Mono

Otherwise it would be good to know the reason why ttf is not allowed.

It is possible to create a font from a vector graphic.

If you have resonable high resolution images of every letter (if not already vector graphics) you can try to convert them to vector graphics (SVG).

Afterwards you can use tools to convert SVG to a font. This would be most likely a ttf font which can be used with QFont.

Upvotes: 0

Related Questions