| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2008 Chusslove Illich <caslav.ilic@gmx.net> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | #ifndef FONTHELPERS_P_H |
| 7 | #define FONTHELPERS_P_H |
| 8 | |
| 9 | // i18n-related helpers for fonts, common to KFont* widgets. |
| 10 | |
| 11 | #include <QCoreApplication> |
| 12 | #include <QString> |
| 13 | #include <QStringList> |
| 14 | |
| 15 | #include <map> |
| 16 | |
| 17 | #ifdef NEVERDEFINE // never true |
| 18 | // Font names up for translation, listed for extraction. |
| 19 | |
| 20 | //: Generic sans serif font presented in font choosers. When selected, |
| 21 | //: the system will choose a real font, mandated by distro settings. |
| 22 | QT_TRANSLATE_NOOP3("FontHelpers" , "Sans Serif" , "@item Font name" ); |
| 23 | //: Generic serif font presented in font choosers. When selected, |
| 24 | //: the system will choose a real font, mandated by distro settings. |
| 25 | QT_TRANSLATE_NOOP3("FontHelpers" , "Serif" , "@item Font name" ); |
| 26 | //: Generic monospace font presented in font choosers. When selected, |
| 27 | //: the system will choose a real font, mandated by distro settings. |
| 28 | QT_TRANSLATE_NOOP3("FontHelpers" , "Monospace" , "@item Font name" ); |
| 29 | |
| 30 | #endif |
| 31 | |
| 32 | /* |
| 33 | * Split the compound raw font name into family and foundry. |
| 34 | * |
| 35 | * name the raw font name reported by Qt |
| 36 | * family the storage for family name |
| 37 | * foundry the storage for foundry name |
| 38 | */ |
| 39 | inline void splitFontString(QStringView name, QString *family, QString *foundry = nullptr) |
| 40 | { |
| 41 | int p1 = name.indexOf(c: QLatin1Char('[')); |
| 42 | if (p1 < 0) { |
| 43 | if (family) { |
| 44 | *family = name.trimmed().toString(); |
| 45 | } |
| 46 | if (foundry) { |
| 47 | foundry->clear(); |
| 48 | } |
| 49 | } else { |
| 50 | int p2 = name.indexOf(c: QLatin1Char(']'), from: p1); |
| 51 | p2 = p2 > p1 ? p2 : name.length(); |
| 52 | if (family) { |
| 53 | *family = name.left(n: p1).trimmed().toString(); |
| 54 | } |
| 55 | if (foundry) { |
| 56 | *foundry = name.mid(pos: p1 + 1, n: p2 - p1 - 1).trimmed().toString(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /*! |
| 62 | * @internal |
| 63 | * |
| 64 | * Translate the font name for the user. |
| 65 | * Primarily for generic fonts like Serif, Sans-Serif, etc. |
| 66 | * |
| 67 | * \a name the raw font name reported by Qt |
| 68 | * @return translated font name |
| 69 | */ |
| 70 | inline QString translateFontName(QStringView name) |
| 71 | { |
| 72 | QString family; |
| 73 | QString foundry; |
| 74 | splitFontString(name, family: &family, foundry: &foundry); |
| 75 | |
| 76 | // Obtain any regular translations for the family and foundry. |
| 77 | QString trFamily = QCoreApplication::translate(context: "FontHelpers" , key: family.toUtf8().constData(), disambiguation: "@item Font name" ); |
| 78 | QString trFoundry = foundry; |
| 79 | if (!foundry.isEmpty()) { |
| 80 | trFoundry = QCoreApplication::translate(context: "FontHelpers" , key: foundry.toUtf8().constData(), disambiguation: "@item Font foundry" ); |
| 81 | } |
| 82 | |
| 83 | // Assemble full translation. |
| 84 | QString trfont; |
| 85 | if (foundry.isEmpty()) { |
| 86 | // i18n: Filter by which the translators can translate, or otherwise |
| 87 | // operate on the font names not put up for regular translation. |
| 88 | trfont = QCoreApplication::translate(context: "FontHelpers" , key: "%1" , disambiguation: "@item Font name" ).arg(a: trFamily); |
| 89 | } else { |
| 90 | // i18n: Filter by which the translators can translate, or otherwise |
| 91 | // operate on the font names not put up for regular translation. |
| 92 | trfont = QCoreApplication::translate(context: "FontHelpers" , key: "%1 [%2]" , disambiguation: "@item Font name [foundry]" ).arg(args&: trFamily, args&: trFoundry); |
| 93 | } |
| 94 | return trfont; |
| 95 | } |
| 96 | |
| 97 | static bool fontFamilyCompare(const QString &a, const QString &b) |
| 98 | { |
| 99 | return QString::localeAwareCompare(s1: a, s2: b) < 0; |
| 100 | } |
| 101 | |
| 102 | using FontFamiliesMap = std::map<QString, QString, decltype(fontFamilyCompare) *>; |
| 103 | |
| 104 | /*! |
| 105 | * @internal |
| 106 | * |
| 107 | * Compose locale-aware sorted list of translated font names, |
| 108 | * with generic fonts handled in a special way. |
| 109 | * The mapping of translated to raw names can be reported too if required. |
| 110 | * |
| 111 | * \a names raw font names as reported by Qt |
| 112 | * \a trToRawNames storage for mapping of translated to raw names |
| 113 | * @return sorted list of translated font names |
| 114 | */ |
| 115 | inline FontFamiliesMap translateFontNameList(const QStringList &names) |
| 116 | { |
| 117 | FontFamiliesMap trMap(fontFamilyCompare); |
| 118 | |
| 119 | for (const QString &fName : names) { |
| 120 | trMap.insert(x: {translateFontName(name: fName), fName}); |
| 121 | } |
| 122 | |
| 123 | return trMap; |
| 124 | } |
| 125 | |
| 126 | #endif |
| 127 | |