summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/android/qandroidplatformfontdatabase.cpp
blob: 82a10dac07583280319eac32836421a829ef82d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QDir>
#include <QLocale>

#include "qandroidplatformfontdatabase.h"

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

QString QAndroidPlatformFontDatabase::fontDir() const
{
    return "/system/fonts"_L1;
}

QFont QAndroidPlatformFontDatabase::defaultFont() const
{
    return QFont("Roboto"_L1);
}

void QAndroidPlatformFontDatabase::populateFontDatabase()
{
    QString fontpath = fontDir();
    QDir dir(fontpath);

    if (Q_UNLIKELY(!dir.exists())) {
        qFatal("QFontDatabase: Cannot find font directory %s - is Qt installed correctly?",
               qPrintable(fontpath));
    }

    QStringList nameFilters;
    nameFilters << "*.ttf"_L1
                << "*.otf"_L1
                << "*.ttc"_L1;

    const auto entries = dir.entryInfoList(nameFilters, QDir::Files);
    for (const QFileInfo &fi : entries) {
        const QByteArray file = QFile::encodeName(fi.absoluteFilePath());
        QFreeTypeFontDatabase::addTTFile(QByteArray(), file);
    }
}

QStringList QAndroidPlatformFontDatabase::fallbacksForFamily(const QString &family,
                                                             QFont::Style style,
                                                             QFont::StyleHint styleHint,
                                                             QChar::Script script) const
{
    QStringList result;

    // Prepend CJK fonts by the locale.
    QLocale locale = QLocale::system();
    switch (locale.language()) {
    case QLocale::Chinese: {
        switch (locale.territory()) {
        case QLocale::China:
        case QLocale::Singapore:
            result.append(QStringLiteral("Noto Sans Mono CJK SC"));
            break;
        case QLocale::Taiwan:
        case QLocale::HongKong:
        case QLocale::Macao:
            result.append(QStringLiteral("Noto Sans Mono CJK TC"));
            break;
        default:
            // no modifications.
            break;
        }
        break;
    }
    case QLocale::Japanese:
        result.append(QStringLiteral("Noto Sans Mono CJK JP"));
        break;
    case QLocale::Korean:
        result.append(QStringLiteral("Noto Sans Mono CJK KR"));
        break;
    default:
        // no modifications.
        break;
    }

    if (styleHint == QFont::Monospace || styleHint == QFont::Courier)
        result.append(QString(qgetenv("QT_ANDROID_FONTS_MONOSPACE")).split(u';'));
    else if (styleHint == QFont::Serif)
        result.append(QString(qgetenv("QT_ANDROID_FONTS_SERIF")).split(u';'));
    else
        result.append(QString(qgetenv("QT_ANDROID_FONTS")).split(u';'));
    result.append(QFreeTypeFontDatabase::fallbacksForFamily(family, style, styleHint, script));

    return result;
}

QT_END_NAMESPACE