For our application we're using the Avenir font.
We have two versions of it:
My system (Mac OS X) does Not have those fonts installed, as I'd like my dev machine to have similar conditions to a client machine.
Both fonts are in our application resources. we are loading them via:
// returns 0
int id = this->fontDatabase.addApplicationFont(":/fonts/avenir_light.ttf");
// returns 1
int id_b = this->fontDatabase.addApplicationFont(":/fonts/avenir_black.ttf");
// returns "Avenir LT Com"
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
// returns "Avenir Lt Com" -- same as other font)
QString family1 = QFontDatabase::applicationFontFamilies(id_b).at(0);
// This will contain two strings:
// "35 Light"
// "95 Black"
QStringList sl = this->fontDatabase.styles(family);
// this returns TRUE
bool isThereBold = this->fontDatabase.bold(family, "95 Black");
Both fonts load successfully (at least, both get good ID's, 0 and 1 respectively, not -1). When I query for their family names, both fonts return "Avenir LT Com" and I wonder if this is a problem becaue only the light font is available at runtime. -- even if I specify a weight of Bold, Black, and any high number.
The fonts are different. The black one is indeed "bold". Installing them into my Mac shows one "family" in the font book "Avenir LT Com", with two variants: normal and black. So I understand the family name is identical even though they are two different TTF files.
The only way for the same code as simple as this:
QFont font("Avenir LT Com");
font.setPixelSize(22);
font.setWeight(QFont::Light); // tried QFont::Black too...
font.setStyleStrategy(QFont::PreferAntialias);
ui->MyLabel->setFont(font);
...to work with both versions of the font is if I Install both fonts on the system (OS X), then magically, both the light and bold versions begin to work. Of course, this does not work for me as I need those fonts running correctly from my applications resources -- I can't have users install fonts for my app to work...
Am I doing something wrong here? Is Qt failing because I'm loading two fonts with the same "family" name? Is there a way I can still load those fonts from resource and use them successfully without having to install them on the target machine?
This is how the fonts look when installed on my Mac:
I got it to work.
I found what I believe to be is a Qt 4.8.5 Bug, applicable to OS X. I am not sure if this is due to OS X 10.9 I use -- Haven't tested on 10.8 or 10.7.
I load the fonts just like I indicated before. The difference should be in the usage under OS X. Although the family of those two fonts is identical ("Avenir LT Com"), they need to be addressed differently so make the bold font work.
So, this fails:
QFont font("Avenir LT Com");
font.setPixelSize(22);
font.setWeight(QFont::Bold); // tried QFont::Black too...
font.setStyleStrategy(QFont::PreferAntialias);
ui->MyLabel->setFont(font);
But this will succeed:
QFont font("Avenir LT Com 95 Black"); // I have to put the family name + style together!
font.setPixelSize(22);
font.setWeight(QFont::Bold); // ...And still set the weight!
font.setStyleStrategy(QFont::PreferAntialias);
ui->MyLabel->setFont(font);
No other changes required.
It's a Qt 4.8 bug, filed as QTBUG-30917. On the Mac you can ship your fonts in the application bundle and load them from there instead of the resources, unless you have to embed and obscure them for licensing reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With