File indexing completed on 2024-03-24 15:27:32
0001 /* 0002 Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "fonthelpers_p.h" 0021 0022 #include <QCoreApplication> 0023 0024 #ifdef NEVERDEFINE // never true 0025 // Font names up for translation, listed for extraction. 0026 0027 // i18n: Generic sans serif font presented in font choosers. When selected, 0028 // the system will choose a real font, mandated by distro settings. 0029 I18N_NOOP2("@item Font name", "Sans Serif") 0030 // i18n: Generic serif font presented in font choosers. When selected, 0031 // the system will choose a real font, mandated by distro settings. 0032 I18N_NOOP2("@item Font name", "Serif") 0033 // i18n: Generic monospace font presented in font choosers. When selected, 0034 // the system will choose a real font, mandated by distro settings. 0035 I18N_NOOP2("@item Font name", "Monospace") 0036 0037 #endif 0038 0039 static void splitFontString(const QString &name, QString *family, QString *foundry) 0040 { 0041 int p1 = name.indexOf('['); 0042 if (p1 < 0) { 0043 if (family) { 0044 *family = name.trimmed(); 0045 } 0046 if (foundry) { 0047 foundry->clear(); 0048 } 0049 } else { 0050 int p2 = name.indexOf(']', p1); 0051 p2 = p2 > p1 ? p2 : name.length(); 0052 if (family) { 0053 *family = name.left(p1).trimmed(); 0054 } 0055 if (foundry) { 0056 *foundry = name.mid(p1 + 1, p2 - p1 - 1).trimmed(); 0057 } 0058 } 0059 } 0060 0061 static QString translateFontName(const QString &name) 0062 { 0063 QString family, foundry; 0064 splitFontString(name, &family, &foundry); 0065 0066 // Obtain any regular translations for the family and foundry. 0067 QString trFamily = QCoreApplication::translate("FontHelpers", family.toUtf8(), "@item Font name"); 0068 QString trFoundry = foundry; 0069 if (!foundry.isEmpty()) { 0070 trFoundry = QCoreApplication::translate("FontHelpers", foundry.toUtf8(), "@item Font foundry"); 0071 } 0072 0073 // Assemble full translation. 0074 QString trfont; 0075 if (foundry.isEmpty()) { 0076 // i18n: Filter by which the translators can translate, or otherwise 0077 // operate on the font names not put up for regular translation. 0078 trfont = QCoreApplication::translate("FontHelpers", "%1", "@item Font name").arg(trFamily); 0079 } else { 0080 // i18n: Filter by which the translators can translate, or otherwise 0081 // operate on the font names not put up for regular translation. 0082 trfont = QCoreApplication::translate("FontHelpers", "%1 [%2]", "@item Font name [foundry]") 0083 .arg(trFamily).arg(trFoundry); 0084 } 0085 return trfont; 0086 } 0087 0088 static bool localeLessThan(const QString &a, const QString &b) 0089 { 0090 return QString::localeAwareCompare(a, b) < 0; 0091 } 0092 0093 static QStringList translateFontNameList(const QStringList &names, 0094 QHash<QString, QString> *trToRawNames) 0095 { 0096 // Generic fonts, in the inverse of desired order. 0097 QStringList genericNames; 0098 genericNames.append("Monospace"); 0099 genericNames.append("Serif"); 0100 genericNames.append("Sans Serif"); 0101 0102 // Translate fonts, but do not add generics to the list right away. 0103 QStringList trNames; 0104 QHash<QString, QString> trMap; 0105 foreach (const QString &name, names) { 0106 QString trName = translateFontName(name); 0107 if (!genericNames.contains(name)) { 0108 trNames.append(trName); 0109 } 0110 trMap.insert(trName, name); 0111 } 0112 0113 // Sort real fonts alphabetically. 0114 std::sort(trNames.begin(), trNames.end(), localeLessThan); 0115 0116 // Prepend generic fonts, in the predefined order. 0117 foreach (const QString &genericName, genericNames) { 0118 QString trGenericName = translateFontName(genericName); 0119 if (trMap.contains(trGenericName)) { 0120 trNames.prepend(trGenericName); 0121 } 0122 } 0123 0124 if (trToRawNames) { 0125 *trToRawNames = trMap; 0126 } 0127 return trNames; 0128 }