KDEUI
fonthelpers.cpp
Go to the documentation of this file.
00001 /* 00002 Requires the Qt widget libraries, available at no cost at 00003 http://www.troll.no 00004 00005 Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "fonthelpers_p.h" 00024 00025 #include "klocale.h" 00026 00027 #ifdef NEVERDEFINE // never true 00028 // Font names up for translation, listed for extraction. 00029 00030 // i18n: Generic sans serif font presented in font choosers. When selected, 00031 // the system will choose a real font, mandated by distro settings. 00032 I18N_NOOP2("@item Font name", "Sans Serif") 00033 // i18n: Generic serif font presented in font choosers. When selected, 00034 // the system will choose a real font, mandated by distro settings. 00035 I18N_NOOP2("@item Font name", "Serif") 00036 // i18n: Generic monospace font presented in font choosers. When selected, 00037 // the system will choose a real font, mandated by distro settings. 00038 I18N_NOOP2("@item Font name", "Monospace") 00039 00040 #endif 00041 00042 void splitFontString (const QString &name, QString *family, QString *foundry) 00043 { 00044 int p1 = name.indexOf('['); 00045 if (p1 < 0) { 00046 if (family) { 00047 *family = name.trimmed(); 00048 } 00049 if (foundry) { 00050 foundry->clear(); 00051 } 00052 } else { 00053 int p2 = name.indexOf(']', p1); 00054 p2 = p2 > p1 ? p2 : name.length(); 00055 if (family) { 00056 *family = name.left(p1).trimmed(); 00057 } 00058 if (foundry) { 00059 *foundry = name.mid(p1 + 1, p2 - p1 - 1).trimmed(); 00060 } 00061 } 00062 } 00063 00064 QString translateFontName (const QString &name) 00065 { 00066 QString family, foundry; 00067 splitFontString(name, &family, &foundry); 00068 00069 // Obtain any regular translations for the family and foundry. 00070 QString trFamily = i18nc("@item Font name", family.toUtf8()); 00071 QString trFoundry = foundry; 00072 if (!foundry.isEmpty()) { 00073 trFoundry = i18nc("@item Font foundry", foundry.toUtf8()); 00074 } 00075 00076 // Assemble full translation. 00077 QString trfont; 00078 if (foundry.isEmpty()) { 00079 // i18n: Filter by which the translators can translate, or otherwise 00080 // operate on the font names not put up for regular translation. 00081 trfont = i18nc("@item Font name", "%1", trFamily); 00082 } else { 00083 // i18n: Filter by which the translators can translate, or otherwise 00084 // operate on the font names not put up for regular translation. 00085 trfont = i18nc("@item Font name [foundry]", "%1 [%2]", 00086 trFamily, trFoundry); 00087 } 00088 return trfont; 00089 } 00090 00091 static bool localeLessThan (const QString &a, const QString &b) 00092 { 00093 return QString::localeAwareCompare(a, b) < 0; 00094 } 00095 00096 QStringList translateFontNameList (const QStringList &names, 00097 QHash<QString, QString> *trToRawNames) 00098 { 00099 // Generic fonts, in the inverse of desired order. 00100 QStringList genericNames; 00101 genericNames.append("Monospace"); 00102 genericNames.append("Serif"); 00103 genericNames.append("Sans Serif"); 00104 00105 // Translate fonts, but do not add generics to the list right away. 00106 QStringList trNames; 00107 QHash<QString, QString> trMap; 00108 foreach (const QString &name, names) { 00109 QString trName = translateFontName(name); 00110 if (!genericNames.contains(name)) { 00111 trNames.append(trName); 00112 } 00113 trMap.insert(trName, name); 00114 } 00115 00116 // Sort real fonts alphabetically. 00117 qSort(trNames.begin(), trNames.end(), localeLessThan); 00118 00119 // Prepend generic fonts, in the predefined order. 00120 foreach (const QString &genericName, genericNames) { 00121 QString trGenericName = translateFontName(genericName); 00122 if (trMap.contains(trGenericName)) { 00123 trNames.prepend(trGenericName); 00124 } 00125 } 00126 00127 if (trToRawNames) { 00128 *trToRawNames = trMap; 00129 } 00130 return trNames; 00131 }
KDE 4.6 API Reference