简体   繁体   中英

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: 在此处输入图片说明

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

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.

I guess it's about copyright issues ? There are lots of ttf fonts available for completely free commercial use. EG 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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