File indexing completed on 2024-04-28 03:58:58

0001 /*
0002     SPDX-FileCopyrightText: 2008 Chusslove Illich <caslav.ilic@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #ifndef FONTHELPERS_P_H
0007 #define FONTHELPERS_P_H
0008 
0009 // i18n-related helpers for fonts, common to KFont* widgets.
0010 
0011 #include <QCoreApplication>
0012 #include <QString>
0013 #include <QStringList>
0014 
0015 #include <map>
0016 
0017 #ifdef NEVERDEFINE // never true
0018 // Font names up for translation, listed for extraction.
0019 
0020 //: Generic sans serif font presented in font choosers. When selected,
0021 //: the system will choose a real font, mandated by distro settings.
0022 QT_TRANSLATE_NOOP3("FontHelpers", "Sans Serif", "@item Font name");
0023 //: Generic serif font presented in font choosers. When selected,
0024 //: the system will choose a real font, mandated by distro settings.
0025 QT_TRANSLATE_NOOP3("FontHelpers", "Serif", "@item Font name");
0026 //: Generic monospace font presented in font choosers. When selected,
0027 //: the system will choose a real font, mandated by distro settings.
0028 QT_TRANSLATE_NOOP3("FontHelpers", "Monospace", "@item Font name");
0029 
0030 #endif
0031 
0032 /**
0033  * @internal
0034  *
0035  * Split the compound raw font name into family and foundry.
0036  *
0037  * @param name the raw font name reported by Qt
0038  * @param family the storage for family name
0039  * @param foundry the storage for foundry name
0040  */
0041 inline void splitFontString(QStringView name, QString *family, QString *foundry = nullptr)
0042 {
0043     int p1 = name.indexOf(QLatin1Char('['));
0044     if (p1 < 0) {
0045         if (family) {
0046             *family = name.trimmed().toString();
0047         }
0048         if (foundry) {
0049             foundry->clear();
0050         }
0051     } else {
0052         int p2 = name.indexOf(QLatin1Char(']'), p1);
0053         p2 = p2 > p1 ? p2 : name.length();
0054         if (family) {
0055             *family = name.left(p1).trimmed().toString();
0056         }
0057         if (foundry) {
0058             *foundry = name.mid(p1 + 1, p2 - p1 - 1).trimmed().toString();
0059         }
0060     }
0061 }
0062 
0063 /**
0064  * @internal
0065  *
0066  * Translate the font name for the user.
0067  * Primarily for generic fonts like Serif, Sans-Serif, etc.
0068  *
0069  * @param name the raw font name reported by Qt
0070  * @return translated font name
0071  */
0072 inline QString translateFontName(QStringView name)
0073 {
0074     QString family;
0075     QString foundry;
0076     splitFontString(name, &family, &foundry);
0077 
0078     // Obtain any regular translations for the family and foundry.
0079     QString trFamily = QCoreApplication::translate("FontHelpers", family.toUtf8().constData(), "@item Font name");
0080     QString trFoundry = foundry;
0081     if (!foundry.isEmpty()) {
0082         trFoundry = QCoreApplication::translate("FontHelpers", foundry.toUtf8().constData(), "@item Font foundry");
0083     }
0084 
0085     // Assemble full translation.
0086     QString trfont;
0087     if (foundry.isEmpty()) {
0088         // i18n: Filter by which the translators can translate, or otherwise
0089         // operate on the font names not put up for regular translation.
0090         trfont = QCoreApplication::translate("FontHelpers", "%1", "@item Font name").arg(trFamily);
0091     } else {
0092         // i18n: Filter by which the translators can translate, or otherwise
0093         // operate on the font names not put up for regular translation.
0094         trfont = QCoreApplication::translate("FontHelpers", "%1 [%2]", "@item Font name [foundry]").arg(trFamily, trFoundry);
0095     }
0096     return trfont;
0097 }
0098 
0099 static bool fontFamilyCompare(const QString &a, const QString &b)
0100 {
0101     return QString::localeAwareCompare(a, b) < 0;
0102 }
0103 
0104 using FontFamiliesMap = std::map<QString, QString, decltype(fontFamilyCompare) *>;
0105 
0106 /**
0107  * @internal
0108  *
0109  * Compose locale-aware sorted list of translated font names,
0110  * with generic fonts handled in a special way.
0111  * The mapping of translated to raw names can be reported too if required.
0112  *
0113  * @param names raw font names as reported by Qt
0114  * @param trToRawNames storage for mapping of translated to raw names
0115  * @return sorted list of translated font names
0116  */
0117 inline FontFamiliesMap translateFontNameList(const QStringList &names)
0118 {
0119     FontFamiliesMap trMap(fontFamilyCompare);
0120 
0121     for (const QString &fName : names) {
0122         trMap.insert({translateFontName(fName), fName});
0123     }
0124 
0125     return trMap;
0126 }
0127 
0128 #endif