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++
Improve localization of glyphmodel.
Wolthera van Hövell tot Westerflierc1a8d1570ec4
diff --git a/libs/flake/text/KoFontGlyphModel.cpp b/libs/flake/text/KoFontGlyphModel.cppindex 820d8602de..490f805dd9 100644--- a/libs/flake/text/KoFontGlyphModel.cpp+++ b/libs/flake/text/KoFontGlyphModel.cpp@@ -101,7 +101,7 @@ struct KoFontGlyphModel::Private {return vsData;}- static QMap<QString, KoOpenTypeFeatureInfo> getOpenTypeTables(FT_FaceSP face, QVector<CodePointInfo> &charMap, QLatin1String lang = QLatin1String()) {+ static QMap<QString, KoOpenTypeFeatureInfo> getOpenTypeTables(FT_FaceSP face, QVector<CodePointInfo> &charMap, QStringList locales, QLatin1String lang = QLatin1String()) {// All of this was referenced from Inkscape's OpenTypeUtil.cpp::readOpenTypeGsubTableQMap<QString, KoOpenTypeFeatureInfo> featureInfo;hb_face_t_sp hbFace(hb_ft_face_create_referenced(face.data()));@@ -109,6 +109,12 @@ struct KoFontGlyphModel::Private {uint targetLanguage = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;uint targetScript = 0;+ QVector<hb_language_t> localeTags;+ Q_FOREACH(const QString locale, locales) {+ QLatin1String l(locale.split("_").join("-").toLatin1());+ localeTags.append(hb_language_from_string(l.data(), l.size()));+ }+hb_language_t languageTag = lang.isEmpty()? HB_LANGUAGE_INVALID: hb_language_from_string(lang.data(), lang.size());uint scriptCount = hb_ot_layout_table_get_script_tags(hbFace.data(), table, 0, nullptr, nullptr);QVector<hb_tag_t> tags;@@ -177,7 +183,22 @@ struct KoFontGlyphModel::Private {}for(auto nameId = nameIds.begin(); nameId != nameIds.end(); nameId++) {- uint length = hb_ot_name_get_utf8(hbFace.data(), *nameId, languageTag, nullptr, nullptr)+1;+ QVector<hb_language_t> testLang;+ uint length = 0;+ if (*nameId == sampleId) {+ testLang.append(languageTag);+ } else {+ testLang = localeTags;+ }+ testLang.append(HB_LANGUAGE_INVALID);+ for (auto tag = testLang.begin(); tag != testLang.end(); tag++) {+ length = hb_ot_name_get_utf8(hbFace.data(), *nameId, *tag, nullptr, nullptr);+ if (length > 0) {+ length+=1;+ break;+ }+ }+std::vector<char> buff(length);hb_ot_name_get_utf8(hbFace.data(), *nameId, languageTag, &length, buff.data());if (length > 0) {@@ -283,6 +304,13 @@ KoFontGlyphModel::~KoFontGlyphModel()}+QString unicodeHexFromUCS(const uint codePoint) {+ QByteArray ba;+ ba.setNum(codePoint, 16);+ QString hex = QString(ba);+ return QString("U+%1").arg(hex, hex.size() > 4? 6: 4, '0');+}+QVariant KoFontGlyphModel::data(const QModelIndex &index, int role) const{if (!index.isValid()) {@@ -305,15 +333,12 @@ QVariant KoFontGlyphModel::data(const QModelIndex &index, int role) const} else if (role == Qt::ToolTipRole) {if (!index.parent().isValid()) {QStringList glyphNames;- QString base = d->codePoints.value(index.row()).utfString;- QVector<Private::GlyphInfo> glyphList = d->codePoints.value(index.row()).glyphs;- glyphNames.append(QString("%1 glyph variants:").arg(glyphList.size()));- for(auto glyph = glyphList.begin(); glyph != glyphList.end(); glyph++) {- if (glyph->type == UnicodeVariationSelector) {- glyphNames.append("UVS:"+base+glyph->baseString);- } else if (glyph->type == OpenType) {- glyphNames.append("OTF:"+glyph->baseString+" "+QString::number(glyph->featureIndex));- }+ Private::CodePointInfo codePoint = d->codePoints.value(index.row());+ QString base = codePoint.utfString;+ QVector<Private::GlyphInfo> glyphList = codePoint.glyphs;+ glyphNames.append(QString("%1 (%2)").arg(base).arg(unicodeHexFromUCS(codePoint.ucs)));+ if (glyphList.size() > 0) {+ glyphNames.append(i18nc("@info:tooltip", "%1 glyph variants.").arg(glyphList.size()));}return glyphNames.join(" ");} else {@@ -347,20 +372,16 @@ QVariant KoFontGlyphModel::data(const QModelIndex &index, int role) constQString glyphId;if (!index.parent().isValid()) {Private::CodePointInfo codePoint = d->codePoints.value(index.row());- QByteArray ba;- ba.setNum(codePoint.ucs, 16);- QString hex = QString(ba);- glyphId = QString("U+%1").arg(hex, hex.size() > 4? 6: 4, '0');+ glyphId = unicodeHexFromUCS(codePoint.ucs);} else {Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());if (glyph.type == OpenType) {glyphId = QString("'%1' %2").arg(glyph.baseString).arg(glyph.featureIndex);} else if (glyph.type == UnicodeVariationSelector) {- QByteArray ba;- ba.setNum(glyph.baseString.toUcs4().first(), 16);- QString hex = QString(ba);- glyphId = QString("U+%1").arg(hex, hex.size() > 4? 6: 4, '0');+ glyphId = unicodeHexFromUCS(glyph.baseString.toUcs4().first());+ } else {+ glyphId = unicodeHexFromUCS(codePoint.ucs);}}return glyphId;@@ -445,7 +466,7 @@ static bool sortBlocks(const KoUnicodeBlockData &a, const KoUnicodeBlockData &b)return a.start < b.start;}-void KoFontGlyphModel::setFace(FT_FaceSP face)+void KoFontGlyphModel::setFace(FT_FaceSP face, QLatin1String language){beginResetModel();d->codePoints = Private::charMap(face);@@ -470,7 +491,7 @@ void KoFontGlyphModel::setFace(FT_FaceSP face)}}std::sort(d->blocks.begin(), d->blocks.end(), sortBlocks);- d->featureData = Private::getOpenTypeTables(face, d->codePoints);+ d->featureData = Private::getOpenTypeTables(face, d->codePoints, KLocalizedString::languages(), language);endResetModel();}diff --git a/libs/flake/text/KoFontGlyphModel.h b/libs/flake/text/KoFontGlyphModel.hindex a8c3871567..297aacf5cf 100644--- a/libs/flake/text/KoFontGlyphModel.h+++ b/libs/flake/text/KoFontGlyphModel.h@@ -47,7 +47,7 @@ public:bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;QModelIndex indexForString(QString grapheme);- void setFace(FT_FaceSP face);+ void setFace(FT_FaceSP face, QLatin1String language = QLatin1String());QHash<int, QByteArray> roleNames() const override;diff --git a/libs/flake/text/KoOpenTypeFeatureInfoFactory.cpp b/libs/flake/text/KoOpenTypeFeatureInfoFactory.cppindex 95c2041786..270c5c50e8 100644--- a/libs/flake/text/KoOpenTypeFeatureInfoFactory.cpp+++ b/libs/flake/text/KoOpenTypeFeatureInfoFactory.cpp@@ -147,7 +147,7 @@ KoOpenTypeFeatureInfoFactory::KoOpenTypeFeatureInfoFactory(){KoOpenTypeFeatureInfo::GSUB1}, true));initialMap.append(KoOpenTypeFeatureInfo(QLatin1String("flac"),i18nc("@title", "Flattened accent forms"),- i18nc("@tooltip", "Replaces accents on capital letters with flattened forms. Does nothing in Krita, as this is not a math engine."),+ i18nc("@tooltip", "Replaces accents on capital letters with flattened forms."),{KoOpenTypeFeatureInfo::GSUB1}));initialMap.append(KoOpenTypeFeatureInfo(QLatin1String("frac"),i18nc("@title", "Fractions"),@@ -187,7 +187,7 @@ KoOpenTypeFeatureInfoFactory::KoOpenTypeFeatureInfoFactory(){KoOpenTypeFeatureInfo::GSUB1}, true));initialMap.append(KoOpenTypeFeatureInfo(QLatin1String("ssty"),i18nc("@title", "Math script style alternates"),- i18nc("@tooltip", "Replaces glyphs with ones more suited for super- and subscripts. Does nothing in Krita."),+ i18nc("@tooltip", "Replaces glyphs with ones more suited for super- and subscripts."),{KoOpenTypeFeatureInfo::GSUB3}));initialMap.append(KoOpenTypeFeatureInfo(QLatin1String("subs"),i18nc("@title", "Subscript"),@@ -540,7 +540,7 @@ KoOpenTypeFeatureInfoFactory::KoOpenTypeFeatureInfoFactory()//character variants- for (int i = 1; i <= 20; i++) {+ for (int i = 1; i <= 99; i++) {char *tag{new char[4]{'c', 'v', '0', '0'}};tag[2] = (i/10) + '0';tag[3] = (i%10) + '0';diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qmlindex d75a40f2fe..abdf9e8dd0 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml@@ -22,6 +22,7 @@ Rectangle {property double fontWidth : 100;property int fontStyle: 0;property double fontSlant: 0.0;+ property string language: "";SystemPalette {id: sysPalette;colorGroup: SystemPalette.Active@@ -60,6 +61,7 @@ Rectangle {fontStyle: root.fontStyle;fontWidth: root.fontWidth;fontSlant: root.fontSlant;+ language: root.language;replace: true;Layout.fillHeight: true;Layout.fillWidth: true;@@ -139,6 +141,7 @@ Rectangle {fontSize: root.fontSize;fontStyle: root.fontStyle;fontWeight: root.fontWeight;+ language: root.language;onGlyphDoubleClicked: (index, mouse)=> {mainWindow.slotInsertRichText(index)};diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.cppindex 9d4bb78202..c92d6cafbc 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.cpp@@ -82,7 +82,7 @@ void GlyphPaletteAltPopup::setModel(QAbstractItemModel *model)}}-void GlyphPaletteAltPopup::setMarkup(const QStringList &families, const int size, const int weight, const int width, const QFont::Style style)+void GlyphPaletteAltPopup::setMarkup(const QStringList &families, const int size, const int weight, const int width, const QFont::Style style, const QString &language){if (m_quickWidget->rootObject()) {m_quickWidget->rootObject()->setProperty("fontFamilies", QVariant::fromValue(families));@@ -90,6 +90,7 @@ void GlyphPaletteAltPopup::setMarkup(const QStringList &families, const int sizem_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(weight));m_quickWidget->rootObject()->setProperty("fontWidth", QVariant::fromValue(width));m_quickWidget->rootObject()->setProperty("fontStyle", QVariant::fromValue(style));+ m_quickWidget->rootObject()->setProperty("language", QVariant::fromValue(language));}}diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.h b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.hindex 31bd72f6d0..9483fb4954 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.h+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAltPopup.h@@ -46,7 +46,7 @@ public:* @brief setMarkup* Set the css font markup to be used inside the palette.*/- void setMarkup(const QStringList &families, const int size, const int weight, const int width, const QFont::Style style);+ void setMarkup(const QStringList &families, const int size, const int weight, const int width, const QFont::Style style, const QString &language);public Q_SLOTS:diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAlts.qml b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAlts.qmlindex 9073234be5..879728107a 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAlts.qml+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteAlts.qml@@ -16,6 +16,7 @@ Rectangle {property double fontWidth: 100;property int fontStyle: 0;property double fontSlant: 0.0;+ property string language: "";SystemPalette {id: sysPalette;colorGroup: SystemPalette.Active@@ -44,6 +45,7 @@ Rectangle {fontSize: root.fontSize;fontStyle: root.fontStyle;fontWeight: root.fontWeight;+ language: root.language;onGlyphClicked: (index, mouse)=> {if (!root.replace) {mainWindow.slotInsertRichText(root.parentIndex, index, root.replace)diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cppindex 44ba490b48..2219fafb94 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp@@ -113,8 +113,9 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(const QPair<KoSvgTextProperstyle.style,style.slantValue.isAuto? 14: style.slantValue.customValue);+ QString language = properties.second.propertyOrDefault(KoSvgTextProperties::TextLanguage).toString();if (faces.empty()) return;- m_model->setFace(faces.front());+ m_model->setFace(faces.front(), QLatin1String(language.toLatin1()));QModelIndex idx = m_model->indexForString(text);if (m_quickWidget->rootObject()) {@@ -123,12 +124,13 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(const QPair<KoSvgTextProperm_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(weight));m_quickWidget->rootObject()->setProperty("fontWidth", QVariant::fromValue(width));m_quickWidget->rootObject()->setProperty("fontStyle", QVariant::fromValue(style.style));+ m_quickWidget->rootObject()->setProperty("language", QVariant::fromValue(language));if (idx.isValid()) {m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));}}if (m_altPopup) {- m_altPopup->setMarkup(families, size, weight, width, style.style);+ m_altPopup->setMarkup(families, size, weight, width, style.style, language);}m_lastUsedProperties = properties.first;}diff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cppindex 41882c349e..b16541cdf1 100644--- a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp@@ -107,6 +107,11 @@ int SvgTextLabel::padding() constreturn d->padding;}+QString SvgTextLabel::language() const+{+ return d->shape->textProperties().property(KoSvgTextProperties::TextLanguage).toString();+}+void SvgTextLabel::setFontFamilies(QStringList fontFamilies){KoSvgTextProperties props = d->shape->textProperties();@@ -209,6 +214,18 @@ void SvgTextLabel::setPadding(int padding)emit paddingChanged(d->padding);}+void SvgTextLabel::setLanguage(QString language)+{+ KoSvgTextProperties props = d->shape->textProperties();+ const QString lang = props.property(KoSvgTextProperties::TextLanguage).toString();+ if (lang == language) {+ return;+ }+ props.setProperty(KoSvgTextProperties::TextLanguage, language);+ d->shape->setPropertiesAtPos(-1, props);+ emit languageChanged(language);+}+void SvgTextLabel::updateShape(){d->shape->relayout();diff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.hindex fb22b461f8..bd93303dc1 100644--- a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.h@@ -29,6 +29,7 @@ class SvgTextLabel : public QQuickPaintedItemQ_PROPERTY(QStringList openTypeFeatures READ openTypeFeatures WRITE setOpenTypeFeatures NOTIFY openTypeFeaturesChanged)Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)Q_PROPERTY(int padding READ padding WRITE setPadding NOTIFY paddingChanged)+ Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged)public:SvgTextLabel(QQuickItem *parent = nullptr);@@ -52,6 +53,8 @@ public:int padding() const;+ QString language() const;+public Q_SLOTS:void setFontFamilies(QStringList fontFamilies);@@ -69,6 +72,8 @@ public Q_SLOTS:void setPadding(int padding);+ void setLanguage(QString language);+Q_SIGNALS:void fontFamiliesChanged(QStringList);@@ -86,6 +91,8 @@ Q_SIGNALS:void paddingChanged(int padding);+ void languageChanged(QString language);+private:void updateShape();