How to detect missing font characters
Is there a way to开发者_运维问答 detect that all characters are displayed properly with the current font? In some environments and fonts certain characters are replaced with a square symbol.
I'd like to automatically verify that all characters used in the GUI are supported by the current font.
I found a possible solution using the QFontMetrics class. Here is a an example function to query whether all characters are available in the current text of a QLabel:
bool charactersMissing(const QLabel& label) {
QFontMetrics metrics(label.font());
for (int i = 0; i < label.text().size(); ++i) {
if (!metrics.inFont(label.text().at(i))) {
return true;
}
}
return false;
}
Of course displaying to the user which character is missing would be good, but of course that has to be done with a different font :)
According to this discussion, I don't think the sample code of QFontMetrics will work well. https://bugreports.qt-project.org/browse/QTBUG-1732
The QFontMetrics inFont() function depend on QFont class, and it seems you have to set the StyleStrategy to QFont::NoFontMerging. But actually, the NoFontMerging flag doesn't work in that way you think, so the inFont function still return true if other fonts in your system have this glyph.
BTW, I make a verification tool in python finally.
https://github.com/diro/pyGlyphChecker
A possible solution...
Depending upon your application the following might be a possible solution
(or completetly not applicable) :
Popup a dialog-box with a sample paragraph containing all the characters that you intend to use.
Render the paragraph using the FONT u have in mind.
Display a message to the user to
-- Select OK (if he/she can read the text i.e. only if no "squares" visible)
-- Select a different FONT (if any "squares" are visible)
NOTE: In case you are wondering, this is not a very uncommon behaviour for apps. MS-WORD does this when it detects non-standard encoding in a doc file.
GoodLUCK!!
精彩评论