How to (properly) output multilingual text in Qt-Embedded?

狂风中的少年 提交于 2019-12-23 18:42:28

问题


My target system is:

  • linux 3.3.7,
  • Qt Embedded (open source edition) 4.8,
  • Droid fonts (taken from fonts-droid_20111207+git-1_all.deb Debian package and copied into /usr/lib/fonts directory),
  • Linux Framebuffer for main Qt GUI application,
  • everything is build by Buildroot package.

My test application is very simple: just one dialog box with a few static QLabel on it (one for Chinese, one for Arabic, one for Cyrillic, etc).

When I run it on my linux desktop, all labels are shown correctly. But when it runs on my target system some text disappears.

After some research, I'd found this difference in the behavior of Qt framework: QFontDatabase class reports that there is only 4 Droid font families on my desktop system:

Droid Sans [unknown]
Droid Sans [monotype]
Droid Sans Mono
Droid Serif

But the same QFontDatabase class reports that there are lots of separated font families on my target system:

Droid Arabic Naskh
Droid Sans
Droid Sans Armenian
Droid Sans Ethiopic
Droid Sans Fallback
Droid Sans Georgian
Droid Sans Hebrew
Droid Sans Japanese
Droid Sans Mono
Droid Sans Thai
Droid Serif

As the result, if I change "default" font family for my application (via -fn command line option or manually via calling setFont() inside my application) some text labels are shown but other are not (for example, when I use "Droid Sans Hebrew" font family, Korean text is missing but Hebrew/Arabic are ok).

So, my questions are: what is proper way to output multilingual text in a Qt Embedded application? Why did the "Droid Sans" family become separated? Is there any way to combine them together?

Thanks.


回答1:


I've created a small test application, which loads font from a file and then uses it in GUI.

#include <QtGui>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    /* Load font data from file in the same directory as executable */
    QFile fontFile("BaroqueScript.ttf");
    if (!fontFile.open(QIODevice::ReadOnly)) {
        qCritical() << "failed to open font file";
    }
    QByteArray fontData = fontFile.readAll();

    /* Register font to the QFontDatabase */
    if (QFontDatabase::addApplicationFontFromData(fontData) == -1) {
        qCritical() << "failed to add a font";
    }

    /* Create font object and verify font family */
    QFont font("Baroque Script", 10, QFont::Bold);
    QFontInfo fontInfo(font);
    qDebug() << "Expected:" <<  font.family() << "Real:" << fontInfo.family();

    /* Produce GUI which uses loaded font */
    QLabel label("Hello, world");
    label.setFont(font);
    label.show();

    return app.exec();
}



回答2:


@qehgt can you please give your CJK and arabic font file sizes? I have faced similar issue... the problem is due to limitation on font cache size. I think its approximately 3MB. So Increasing font cache size is one possibility or you got to dynamically load font files based on the language selected. Hope this helps.. :)




回答3:


Well, looks like the solution is found at last.

There is a bug in Qt Embedded Rendering Engine: for some reasons it uses "QPF2" Font Engine (QFontEngineQPF) for rendering text in "broken" scripts (Hebrew/Arabic/Thai/Korean in my case).

To avoid/fix this issue, just need to run an application with QWS_NO_SHARE_FONTS=1 environment variable (and with -fn "Droid Sans" command line parameter as well).

After that all text in all languages is displayed without any issue.



来源:https://stackoverflow.com/questions/14125081/how-to-properly-output-multilingual-text-in-qt-embedded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!