Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Make the localized font style names and axes work.
Wolthera van Hövell tot Westerflier17b85022c388
diff --git a/libs/flake/resources/KoFontFamily.cpp b/libs/flake/resources/KoFontFamily.cppindex 63b8099d09..de375b53d5 100644--- a/libs/flake/resources/KoFontFamily.cpp+++ b/libs/flake/resources/KoFontFamily.cpp@@ -57,10 +57,10 @@ QImage generateImage(QString sample, QString fontFamily, bool isColor) {return img;}-QHash<QString, QVariant> stringHashtoVariantHash(QHash<QString, QString> names) {+QHash<QString, QVariant> localeHashtoVariantHash(QHash<QLocale, QString> names) {QHash<QString, QVariant> newNames;- Q_FOREACH(const QString key, names.keys()) {- newNames.insert(key, QVariant::fromValue(names.value(key)));+ Q_FOREACH(const QLocale key, names.keys()) {+ newNames.insert(key.bcp47Name(), QVariant::fromValue(names.value(key)));}return newNames;}@@ -72,9 +72,9 @@ KoFontFamily::KoFontFamily(KoFontFamilyWWSRepresentation representation){setName(representation.fontFamilyName);addMetaData(TYPOGRAPHIC_NAME, representation.typographicFamilyName);- addMetaData(LOCALIZED_FONT_FAMILY, stringHashtoVariantHash(representation.localizedFontFamilyNames));- addMetaData(LOCALIZED_TYPOGRAPHIC_NAME, stringHashtoVariantHash(representation.localizedTypographicFamily));- addMetaData(LOCALIZED_TYPOGRAPHIC_STYLE, stringHashtoVariantHash(representation.localizedTypographicStyles));+ addMetaData(LOCALIZED_FONT_FAMILY, localeHashtoVariantHash(representation.localizedFontFamilyNames));+ addMetaData(LOCALIZED_TYPOGRAPHIC_NAME, localeHashtoVariantHash(representation.localizedTypographicFamily));+ addMetaData(LOCALIZED_TYPOGRAPHIC_STYLE, localeHashtoVariantHash(representation.localizedTypographicStyles));QVariantHash samples;Q_FOREACH(const QLocale::Script &key, representation.sampleStrings.keys()) {diff --git a/libs/flake/text/KoFFWWSConverter.cpp b/libs/flake/text/KoFFWWSConverter.cppindex 6638994a20..474a3f3ae5 100644--- a/libs/flake/text/KoFFWWSConverter.cpp+++ b/libs/flake/text/KoFFWWSConverter.cpp@@ -25,11 +25,11 @@ struct FontFamilySizeInfo {qreal low = -1;qreal high = -1;qreal designSize = 0;- QHash<QString, QString> localizedLabels;+ QHash<QLocale, QString> localizedLabels;QString debugInfo() const {QString label;if (!localizedLabels.isEmpty()) {- label = localizedLabels.value("en", localizedLabels.values().first());+ label = localizedLabels.value(QLocale(QLocale::English), localizedLabels.values().first());}return QString("Optical Size Info: OS2=%1, label: %2, min: %3, max: %4, designSize: %5").arg(os2table? "true": "false").arg(label).arg(low).arg(high).arg(designSize);}@@ -60,10 +60,10 @@ struct FontFamilyNode {QStringList otherFiles;- QHash<QString, QString> localizedFontFamilies;- QHash<QString, QString> localizedFontStyle;- QHash<QString, QString> localizedTypographicStyle;- QHash<QString, QString> localizedWWSStyle;+ QHash<QLocale, QString> localizedFontFamilies;+ QHash<QLocale, QString> localizedFontStyle;+ QHash<QLocale, QString> localizedTypographicStyle;+ QHash<QLocale, QString> localizedWWSStyle;QHash<QString, KoSvgText::FontFamilyAxis> axes;QList<KoSvgText::FontFamilyStyleInfo> styleInfo;@@ -400,58 +400,60 @@ bool KoFFWWSConverter::addFontFromFile(const QString &filename, const int index,uint numEntries = 0;const hb_ot_name_entry_t *entries = hb_ot_name_list_names(hbFace, &numEntries);- QHash<QString, QString> ribbiFamilyNames;- QHash<QString, QString> ribbiStyleNames;- QHash<QString, QString> WWSFamilyNames;- QHash<QString, QString> WWSStyleNames;- QHash<QString, QString> typographicFamilyNames;- QHash<QString, QString> typographicStyleNames;+ QHash<QLocale, QString> ribbiFamilyNames;+ QHash<QLocale, QString> ribbiStyleNames;+ QHash<QLocale, QString> WWSFamilyNames;+ QHash<QLocale, QString> WWSStyleNames;+ QHash<QLocale, QString> typographicFamilyNames;+ QHash<QLocale, QString> typographicStyleNames;for (uint i = 0; i < numEntries; i++) {hb_ot_name_entry_t entry = entries[i];QString lang(hb_language_to_string(entry.language));+ QLocale locale(lang);uint length = hb_ot_name_get_utf8(hbFace, entry.name_id, entry.language, nullptr, nullptr)+1;char buff[length];hb_ot_name_get_utf8(hbFace, entry.name_id, entry.language, &length, buff);QString name = QString::fromUtf8(buff, length);if (entry.name_id == HB_OT_NAME_ID_FONT_FAMILY) {- ribbiFamilyNames.insert(lang, name);+ ribbiFamilyNames.insert(locale, name);} else if (entry.name_id == HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY) {- typographicFamilyNames.insert(lang, name);+ typographicFamilyNames.insert(locale, name);} else if (entry.name_id == HB_OT_NAME_ID_WWS_FAMILY) {- WWSFamilyNames.insert(lang, name);+ WWSFamilyNames.insert(locale, name);} else if (entry.name_id == HB_OT_NAME_ID_FONT_SUBFAMILY) {- ribbiStyleNames.insert(lang, name);+ ribbiStyleNames.insert(locale, name);} else if (entry.name_id == HB_OT_NAME_ID_TYPOGRAPHIC_SUBFAMILY) {- typographicStyleNames.insert(lang, name);+ typographicStyleNames.insert(locale, name);} else if (entry.name_id == HB_OT_NAME_ID_WWS_SUBFAMILY) {- WWSStyleNames.insert(lang, name);+ WWSStyleNames.insert(locale, name);} else if (entry.name_id > 0) { // Fonts made by Adobe seem to use the copyright id (0) as the input when the given value is empty.if (axisNameIDs.keys().contains(entry.name_id)) {- fontFamily.axes[axisNameIDs.value(entry.name_id)].localizedLabels.insert(lang, name);+ fontFamily.axes[axisNameIDs.value(entry.name_id)].localizedLabels.insert(locale, name);} else if (entry.name_id == sizeNameId) {- fontFamily.sizeInfo.localizedLabels.insert(lang, name);+ fontFamily.sizeInfo.localizedLabels.insert(locale, name);} else if (instanceNameIDs.contains(entry.name_id)) {int idx = instanceNameIDs.indexOf(entry.name_id);- fontFamily.styleInfo[idx].localizedLabels.insert(lang, name);+ fontFamily.styleInfo[idx].localizedLabels.insert(locale, name);}}}+ QLocale english(QLocale::English);if (!typographicFamilyNames.isEmpty()) {- typographicFamily.fontFamily = typographicFamilyNames.value("en", typographicFamilyNames.values().first());+ typographicFamily.fontFamily = typographicFamilyNames.value(english, typographicFamilyNames.values().first());typographicFamily.localizedFontFamilies = typographicFamilyNames;}fontFamily.localizedTypographicStyle = typographicStyleNames;if (!ribbiFamilyNames.isEmpty()) {- fontFamily.fontFamily = ribbiFamilyNames.value("en", ribbiFamilyNames.values().first());+ fontFamily.fontFamily = ribbiFamilyNames.value(english, ribbiFamilyNames.values().first());fontFamily.localizedFontFamilies = ribbiFamilyNames;}if (!ribbiStyleNames.isEmpty()) {- fontFamily.fontStyle = ribbiStyleNames.value("en", ribbiStyleNames.values().first());+ fontFamily.fontStyle = ribbiStyleNames.value(english, ribbiStyleNames.values().first());fontFamily.localizedFontStyle = ribbiStyleNames;}if (!WWSFamilyNames.isEmpty()) {- wwsFamily.fontFamily = WWSFamilyNames.value("en", WWSFamilyNames.values().first());+ wwsFamily.fontFamily = WWSFamilyNames.value(english, WWSFamilyNames.values().first());wwsFamily.localizedFontFamilies = WWSFamilyNames;}fontFamily.localizedWWSStyle = WWSStyleNames;@@ -822,7 +824,7 @@ KoFontFamilyWWSRepresentation createRepresentation(KisForest<FontFamilyNode>::ch} else if (!subFamily->localizedFontStyle.isEmpty()) {style.localizedLabels = subFamily->localizedFontStyle;} else {- style.localizedLabels.insert("en", subFamily->fontStyle);+ style.localizedLabels.insert(QLocale(QLocale::English), subFamily->fontStyle);}style.isItalic = subFamily->isItalic;style.isOblique = subFamily->isOblique;diff --git a/libs/flake/text/KoFFWWSConverter.h b/libs/flake/text/KoFFWWSConverter.hindex 8c0beeeea7..5b4a7cbecc 100644--- a/libs/flake/text/KoFFWWSConverter.h+++ b/libs/flake/text/KoFFWWSConverter.h@@ -19,9 +19,9 @@ struct KoFontFamilyWWSRepresentation {QString fontFamilyName;QString typographicFamilyName;- QHash<QString, QString> localizedFontFamilyNames;- QHash<QString, QString> localizedTypographicFamily;- QHash<QString, QString> localizedTypographicStyles;+ QHash<QLocale, QString> localizedFontFamilyNames;+ QHash<QLocale, QString> localizedTypographicFamily;+ QHash<QLocale, QString> localizedTypographicStyles;QHash<QLocale::Script, QString> sampleStrings; /// sample string used to generate the preview;QList<QLocale> supportedLanguages;diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cppindex 3eb57f3999..03ee7ed689 100644--- a/libs/flake/text/KoSvgText.cpp+++ b/libs/flake/text/KoSvgText.cpp@@ -1075,7 +1075,7 @@ QDataStream &operator<<(QDataStream &out, const KoSvgText::FontFamilyAxis &axis)root.setAttribute("variable", axis.variableAxis? "true": "false");for(auto it = axis.localizedLabels.begin(); it != axis.localizedLabels.end(); it++) {QDomElement name = doc.createElement("name");- name.setAttribute("lang", it.key());+ name.setAttribute("lang", it.key().bcp47Name());name.setAttribute("value", it.value());root.appendChild(name);}@@ -1102,7 +1102,7 @@ QDataStream &operator>>(QDataStream &in, KoSvgText::FontFamilyAxis &axis) {QDomElement name = names.at(i).toElement();QString lang = name.attribute("lang");QString value = name.attribute("value");- axis.localizedLabels.insert(lang, value);+ axis.localizedLabels.insert(QLocale(lang), value);}return in;@@ -1128,7 +1128,7 @@ QDataStream &operator<<(QDataStream &out, const KoSvgText::FontFamilyStyleInfo &}for(auto it = style.localizedLabels.begin(); it != style.localizedLabels.end(); it++) {QDomElement name = doc.createElement("name");- name.setAttribute("lang", it.key());+ name.setAttribute("lang", it.key().bcp47Name());name.setAttribute("value", it.value());root.appendChild(name);}@@ -1150,7 +1150,7 @@ QDataStream &operator>>(QDataStream &in, KoSvgText::FontFamilyStyleInfo &style)QDomElement name = names.at(i).toElement();QString lang = name.attribute("lang");QString value = name.attribute("value");- style.localizedLabels.insert(lang, value);+ style.localizedLabels.insert(QLocale(lang), value);}QDomNodeList coords = root.elementsByTagName("coord");for(int i = 0; i < coords.size(); i++) {diff --git a/libs/flake/text/KoSvgText.h b/libs/flake/text/KoSvgText.hindex 6a455a0c72..c6b38dc651 100644--- a/libs/flake/text/KoSvgText.h+++ b/libs/flake/text/KoSvgText.h@@ -13,6 +13,7 @@#include <QTextCharFormat>#include <QVariant>#include <QVector>+#include <QLocale>#include <array>#include <boost/operators.hpp>#include <boost/optional.hpp>@@ -726,7 +727,7 @@ struct FontFamilyAxis : public boost::equality_comparable<FontFamilyAxis> {}QString tag;- QHash<QString, QString> localizedLabels;+ QHash<QLocale, QString> localizedLabels;qreal min = -1;qreal max = -1;qreal value = 0;@@ -737,7 +738,7 @@ struct FontFamilyAxis : public boost::equality_comparable<FontFamilyAxis> {QString debugInfo() const {QString label;if (!localizedLabels.isEmpty()) {- label = localizedLabels.value("en", localizedLabels.values().first());+ label = localizedLabels.value(QLocale(QLocale::English), localizedLabels.values().first());}return QString("Axis: %1 (%2), min: %3, default:%4, max: %5").arg(tag).arg(label).arg(min).arg(value).arg(max);}@@ -764,7 +765,7 @@ enum FontFormatType {Q_ENUM_NS(FontFormatType)struct FontFamilyStyleInfo : public boost::equality_comparable<FontFamilyStyleInfo> {- QHash<QString, QString> localizedLabels;+ QHash<QLocale, QString> localizedLabels;QHash<QString, float> instanceCoords;bool isItalic = false;@@ -773,7 +774,7 @@ struct FontFamilyStyleInfo : public boost::equality_comparable<FontFamilyStyleInQString debugInfo() const {QString label;if (!localizedLabels.isEmpty()) {- label = localizedLabels.value("en", localizedLabels.values().first());+ label = localizedLabels.value(QLocale(QLocale::English), localizedLabels.values().first());}QStringList coords;for (int i = 0; i < instanceCoords.size(); i++) {diff --git a/plugins/dockers/textproperties/FontAxesModel.cpp b/plugins/dockers/textproperties/FontAxesModel.cppindex 71cd486707..99fedf988f 100644--- a/plugins/dockers/textproperties/FontAxesModel.cpp+++ b/plugins/dockers/textproperties/FontAxesModel.cpp@@ -89,11 +89,10 @@ QVariant FontAxesModel::data(const QModelIndex &index, int role) constKoSvgText::FontFamilyAxis axis = d->axes.at(index.row());if (role == Qt::DisplayRole) {if (axis.localizedLabels.isEmpty()) return axis.tag;- QString label = axis.localizedLabels.value("en", axis.localizedLabels.values().first());+ QString label = axis.localizedLabels.value(QLocale(QLocale::English), axis.localizedLabels.values().first());Q_FOREACH(const QLocale &locale, d->locales) {- QString bcp = locale.bcp47Name();- if (axis.localizedLabels.keys().contains(bcp)) {- label = axis.localizedLabels.value(bcp, label);+ if (axis.localizedLabels.keys().contains(locale)) {+ label = axis.localizedLabels.value(locale, label);break;}}diff --git a/plugins/dockers/textproperties/FontStyleModel.cpp b/plugins/dockers/textproperties/FontStyleModel.cppindex bd923cd8fd..cf0530e1d4 100644--- a/plugins/dockers/textproperties/FontStyleModel.cpp+++ b/plugins/dockers/textproperties/FontStyleModel.cpp@@ -154,11 +154,10 @@ QVariant FontStyleModel::data(const QModelIndex &index, int role) constKoSvgText::FontFamilyStyleInfo style = d->styles.at(index.row());if (role == Qt::DisplayRole) {if (style.localizedLabels.isEmpty()) return QString();- QString label = style.localizedLabels.value("en", style.localizedLabels.values().first());+ QString label = style.localizedLabels.value(QLocale(QLocale::English), style.localizedLabels.values().first());Q_FOREACH(const QLocale &locale, d->locales) {- QString bcp = locale.bcp47Name();- if (style.localizedLabels.keys().contains(bcp)) {- label = style.localizedLabels.value(bcp, label);+ if (style.localizedLabels.keys().contains(locale)) {+ label = style.localizedLabels.value(locale, label);break;}}diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex 35b5b1142e..42fcaf53d1 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -127,9 +127,17 @@ TextPropertiesDock::TextPropertiesDock()d->fontTagFilterProxyModel->sort(KisAbstractResourceModel::Name);d->tagModel = new KisTagModel(ResourceType::FontFamilies);+ QList<QLocale> locales;+ Q_FOREACH (const QString langCode, KLocalizedString::languages()) {+ locales.append(QLocale(langCode));+ }+ d->axesModel.setLocales(locales);+ d->stylesModel.setLocales(locales);+connect(d->textModel, SIGNAL(axisValuesChanged(const QVariantHash&)), &d->axesModel, SLOT(setAxisValues(const QVariantHash&)));connect(&d->axesModel, SIGNAL(axisValuesChanged()), this, SLOT(slotUpdateAxesValues()));+m_quickWidget->rootContext()->setContextProperty("textPropertiesModel", d->textModel);m_quickWidget->rootContext()->setContextProperty("fontFamiliesModel", QVariant::fromValue(d->fontTagFilterProxyModel));m_quickWidget->rootContext()->setContextProperty("fontTagModel", QVariant::fromValue(d->tagModel));