简体   繁体   English

Qt:字体资源适用于正常体重,Bold失败

[英]Qt: Font resources work for normal weight, fail for Bold

For our application we're using the Avenir font. 对于我们的应用程序,我们使用的是Avenir字体。

We have two versions of it: 我们有两个版本:

  1. avenir_light.ttf - a light version of the font. avenir_light.ttf - 字体的简易版本。
  2. avenir_black.ttf - a bold version of the font. avenir_black.ttf - 字体的粗体版本。

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. 我的系统(Mac OS X中) 没有安装这些字体,因为我想我的dev的机器有一个客户端机器类似的条件。

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). 两种字体都成功加载(至少,两者都得到好的ID,分别为0和1,而不是-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. 当我查询他们的姓氏时,两种字体都返回“Avenir LT Com”,我想知道这是否是一个问题,因为只有光字体在运行时可用。 -- 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. 将它们安装到我的Mac中会在字体书“Avenir LT Com”中显示一个“系列”,有两种变体:普通和黑色。 So I understand the family name is identical even though they are two different TTF files. 所以我理解姓氏是相同的,即使它们是两个不同的TTF文件。

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. ...使用两种版本的字体是因为我在系统上安装了两种字体(OS X ),然后神奇地说,轻型和粗体版本都开始工作。 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? Qt是否失败,因为我正在加载具有相同“家庭”名称的两种字体? 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: 这是我在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. 我发现我认为是Qt 4.8.5 Bug,适用于OS X.我不确定这是否是由于OS X 10.9我使用 - 没有在10.8或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. 不同之处应该在OS X下的使用情况。尽管这两种字体的系列是相同的(“Avenir LT Com”),但它们需要以不同的方式进行处理,以使粗体字体起作用。

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. 这是一个Qt 4.8错误,归档为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. 在Mac上,您可以在应用程序包中发送字体并从那里加载而不是资源,除非您出于许可原因必须嵌入和隐藏它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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