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++
Add glyph alternates tab and ensure opentype features are painted.
Wolthera van Hövell tot Westerflier876ccea7e0c9
diff --git a/libs/flake/text/KoFontGlyphModel.cpp b/libs/flake/text/KoFontGlyphModel.cppindex 90d8db5b1d..7f15168343 100644--- a/libs/flake/text/KoFontGlyphModel.cpp+++ b/libs/flake/text/KoFontGlyphModel.cpp@@ -8,34 +8,54 @@#include <hb.h>#include <hb-ft.h>-struct KoFontGlyphModel::GlyphInfo {- GlyphInfo()- {- }- GlyphInfo(uint utf)- : ucs(utf)- {- }- uint ucs;- GlyphType type = Base;- QString baseString;-};-struct KoFontGlyphModel::CodePointInfo {- uint ucs = -1;- uint glyphIndex = 0;- QString utfString = QString();+struct KoFontGlyphModel::Private {+ struct InfoNode {+ virtual ~InfoNode() {}+ uint ucs = 0;+ uint parentUcs = 0;+ virtual int childCount() = 0;+ };++ struct GlyphInfo+ : public InfoNode {++ GlyphInfo()+ {+ }+ GlyphInfo(uint utf)+ {+ ucs = utf;+ parentUcs = utf;+ }+ ~GlyphInfo() override {}+ GlyphType type = Base;+ QString baseString;+ int layoutIndex = 0;+ int childCount() override {+ return 0;+ }+ };- QVector<GlyphInfo> glyphs;-};+ struct CodePointInfo+ : public InfoNode {+ ~CodePointInfo() override {}-struct KoFontGlyphModel::Private {+ uint glyphIndex = 0;+ QString utfString = QString();++ QVector<GlyphInfo> glyphs;+ int childCount() override {+ return glyphs.size();+ }+ };QVector<CodePointInfo> codePoints;~Private() = default;- static QVector<KoFontGlyphModel::CodePointInfo> charMap(FT_FaceSP face) {++ static QVector<CodePointInfo> charMap(FT_FaceSP face) {QVector<CodePointInfo> codePoints;FT_UInt gindex;FT_ULong charcode = FT_Get_First_Char(face.data(), &gindex);@@ -142,6 +162,7 @@ struct KoFontGlyphModel::Private {GlyphInfo gci;gci.type = OpenType;gci.baseString = tagName;+ gci.layoutIndex = i;hb_codepoint_t hbGlyphPoint = HB_SET_VALUE_INVALID;while(hb_set_next(glyphsInput.data(), &hbGlyphPoint)) {QVector<GlyphInfo> glyphs = otfData.value(hbGlyphPoint);@@ -160,22 +181,15 @@ struct KoFontGlyphModel::Private {-KoFontGlyphModel::KoFontGlyphModel(FT_FaceSP face)++KoFontGlyphModel::KoFontGlyphModel(): QAbstractItemModel(), d(new Private){+}- d->codePoints = Private::charMap(face);- QMap<uint, QVector<GlyphInfo>> otfData = Private::getOpenTypeTables(face);- QMap<uint, QVector<GlyphInfo>> VSData = Private::getVSData(face);- for(auto it = d->codePoints.begin(); it != d->codePoints.end(); it++) {- it->glyphs.append(VSData.value(it->ucs));- QVector<GlyphInfo> otfTable = otfData.value(it->glyphIndex);- for (auto glyphInfo = otfTable.begin(); glyphInfo != otfTable.end(); glyphInfo++) {- glyphInfo->ucs = it->ucs;- it->glyphs.append(*glyphInfo);- }- }+KoFontGlyphModel::~KoFontGlyphModel()+{}@@ -185,52 +199,86 @@ QVariant KoFontGlyphModel::data(const QModelIndex &index, int role) constreturn QVariant();}if (role == Qt::DisplayRole) {+ //qDebug() << Q_FUNC_INFO<< index << index.parent().isValid() << index.parent();if (!index.parent().isValid()) {return d->codePoints.value(index.row()).utfString;} else {- return d->codePoints.value(index.parent().row()).glyphs.value(index.row()).baseString;+ Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());+ Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());+ //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;+ if (glyph.type == UnicodeVaritionSelector) {+ return QString(codePoint.utfString + glyph.baseString);+ } else {+ return codePoint.utfString;+ }}} else if (role == Qt::ToolTipRole) {if (!index.parent().isValid()) {QStringList glyphNames;QString base = d->codePoints.value(index.row()).utfString;- QVector<GlyphInfo> glyphList = d->codePoints.value(index.row()).glyphs;+ 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 == UnicodeVaritionSelector) {glyphNames.append("UVS:"+base+glyph->baseString);} else if (glyph->type == OpenType) {- glyphNames.append("OTF:"+glyph->baseString);+ glyphNames.append("OTF:"+glyph->baseString+" "+QString::number(glyph->layoutIndex));}}return glyphNames.join(" ");+ } else {+ Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());+ Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());+ if (glyph.type == OpenType) {+ return QString("OTF:"+glyph.baseString+" "+QString::number(glyph.layoutIndex));+ } else {+ return QString(codePoint.utfString + glyph.baseString);+ }+ }+ } else if (role == OpenTypeFeatures) {+ QStringList features;+ if (index.parent().isValid()) {+ Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());+ Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());+ //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;+ if (glyph.type == OpenType) {+ features.append("'"+glyph.baseString+"' 1");+ }+}+ return features;}return QVariant();}QModelIndex KoFontGlyphModel::index(int row, int column, const QModelIndex &parent) const{- if (!parent.isValid()) {- CodePointInfo cpi = d->codePoints.value(row);- return createIndex(row, column, &cpi);- } else {- GlyphInfo gpi = d->codePoints.value(parent.row()).glyphs.value(row);- return createIndex(row, column, &gpi);+ //qDebug() << Q_FUNC_INFO << row << column << parent;+ if (parent.isValid() && parent.row() >= 0 && parent.row() < d->codePoints.size()) {+ Private::CodePointInfo info = d->codePoints.at(parent.row());+ if (row >= 0 && row < info.glyphs.size()) {+ return createIndex(row, column, &info.glyphs[row]);+ }++ } else if (row >= 0 && row < d->codePoints.size()) {+ return createIndex(row, column);}return QModelIndex();}QModelIndex KoFontGlyphModel::parent(const QModelIndex &child) const{- if (!child.isValid() || static_cast<CodePointInfo*>(child.internalPointer())) {+ if (!child.isValid() || !child.internalPointer()) {return QModelIndex();}- GlyphInfo *gpi = static_cast<GlyphInfo*>(child.internalPointer());- for (int i = 0; i < d->codePoints.size(); i++) {- CodePointInfo cpi = d->codePoints.at(i);- if (cpi.ucs == gpi->ucs) {- return createIndex(i, 0, &cpi);+ Private::InfoNode *node = static_cast<Private::InfoNode*>(child.internalPointer());++ if (node->parentUcs == node->ucs) {+ for(int i = 0; i < d->codePoints.size(); i++) {+ Private::CodePointInfo info = d->codePoints.at(i);+ if (info.ucs == node->ucs) {+ return index(i, 0, QModelIndex());+ }}}@@ -253,8 +301,9 @@ int KoFontGlyphModel::columnCount(const QModelIndex &parent) constbool KoFontGlyphModel::hasChildren(const QModelIndex &parent) const{- if (parent.isValid())+ if (parent.isValid()) {return !d->codePoints.value(parent.row()).glyphs.isEmpty();+ }return false;}@@ -262,9 +311,34 @@ QModelIndex KoFontGlyphModel::indexForString(QString grapheme){for (int i = 0; i < d->codePoints.size(); i++) {if (grapheme.toUcs4().startsWith(d->codePoints.at(i).ucs)) {- CodePointInfo cpi = d->codePoints.at(i);- return createIndex(i, 1, &cpi);+ QModelIndex idx = index(i, 0, QModelIndex());+ return idx;}}return QModelIndex();}++void KoFontGlyphModel::setFace(FT_FaceSP face)+{+ beginResetModel();+ d->codePoints = Private::charMap(face);+ QMap<uint, QVector<Private::GlyphInfo>> otfData = Private::getOpenTypeTables(face);+ QMap<uint, QVector<Private::GlyphInfo>> VSData = Private::getVSData(face);+ for(auto it = d->codePoints.begin(); it != d->codePoints.end(); it++) {+ it->glyphs.append(VSData.value(it->ucs));+ QVector<Private::GlyphInfo> otfTable = otfData.value(it->glyphIndex);+ for (auto glyphInfo = otfTable.begin(); glyphInfo != otfTable.end(); glyphInfo++) {+ glyphInfo->ucs = it->ucs;+ glyphInfo->parentUcs = it->ucs;+ it->glyphs.append(*glyphInfo);+ }+ }+ endResetModel();+}++QHash<int, QByteArray> KoFontGlyphModel::roleNames() const+{+ QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();+ roles[OpenTypeFeatures] = "openType";+ return roles;+}diff --git a/libs/flake/text/KoFontGlyphModel.h b/libs/flake/text/KoFontGlyphModel.hindex ff56b49c03..d976341427 100644--- a/libs/flake/text/KoFontGlyphModel.h+++ b/libs/flake/text/KoFontGlyphModel.h@@ -7,6 +7,7 @@#define KOFONTGLYPHMODEL_H#include <QAbstractItemModel>+#include <QScopedPointer>#include "KoFontLibraryResourceUtils.h"#include "kritaflake_export.h"@@ -19,6 +20,7 @@*/class KRITAFLAKE_EXPORT KoFontGlyphModel: public QAbstractItemModel{+ Q_OBJECTpublic:enum GlyphType {Base,@@ -26,7 +28,12 @@ public:OpenType};- KoFontGlyphModel(FT_FaceSP face);+ KoFontGlyphModel();+ ~KoFontGlyphModel();++ enum Roles {+ OpenTypeFeatures = Qt::UserRole + 1+ };QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;@@ -37,11 +44,12 @@ public:bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;QModelIndex indexForString(QString grapheme);+ void setFace(FT_FaceSP face);++ QHash<int, QByteArray> roleNames() const override;private:- class Private;- struct GlyphInfo;- struct CodePointInfo;+ struct Private;QScopedPointer<Private> d;};diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qmlindex a43e312242..1731317984 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml@@ -3,8 +3,10 @@** SPDX-License-Identifier: GPL-2.0-or-later*/-import QtQuick 2.0+import QtQuick 2.15+import QtQml.Models 2.12import QtQuick.Controls 2.5+import QtQuick.Layouts 1.15import org.krita.tools.text 1.0Rectangle {@@ -33,53 +35,106 @@ Rectangle {anchors.top: parent.top;}- ScrollView {+ TabBar {+ id: tabs;anchors.left: parent.left;anchors.right: parent.right;anchors.top: title.bottom;++ TabButton {+ text: "Glyph Alternates"+ }+ TabButton {+ text: "Character Map"+ }+ }++ Component {+ id: glyphDelegate;+ SvgTextLabel {+ textColor: palette.windowText;+ fillColor: palette.base;+ fontFamilies: root.fontFamilies;+ fontSize: root.fontSize;+ fontStyle: root.fontStyle;+ fontWeight: root.fontWeight;+ openTypeFeatures: model.openType;+ text: model.display;+ padding: height/8;++ width: charMap.cellWidth;+ height: charMap.cellHeight;++ property bool currentItem: GridView.isCurrentItem;+ Rectangle {+ anchors.fill: parent;+ color: "transparent";+ border.color: parent.currentItem? palette.highlight: palette.alternateBase;+ }+ MouseArea {+ anchors.fill: parent;+ onClicked: {+ //parent.currentIndex = index;+ console.log(model.toolTip)+ }+ hoverEnabled: true;+ ToolTip.text: model.toolTip;+ ToolTip.visible: containsMouse;+ }+ }+ }+++ StackLayout {+ id: stack;+ currentIndex: tabs.currentIndex;+ anchors.left: parent.left;+ anchors.right: parent.right;+ anchors.top: tabs.bottom;anchors.bottom: parent.bottom;+ // Glyph alts.+ GridView {+ id: glyphAlts;+ model: DelegateModel {+ id: glyphAltModel+ model: root.model+ property alias rIndex: root.currentIndex;+ onRIndexChanged: {+ console.log("start");+ rootIndex = parentModelIndex()+ rootIndex = modelIndex(root.currentIndex);+ console.log(rootIndex , root.currentIndex);+ }++ delegate: glyphDelegate;+ }+ focus: true;+ clip: true;++ cellWidth: (parent.width - glyphAltScroll.implicitBackgroundWidth)/8;+ cellHeight: cellWidth;- property int scrollWidth: ScrollBar.implicitWidth;+ ScrollBar.vertical: ScrollBar {+ id: glyphAltScroll;+ }++ }++ // Charmap.GridView {id: charMap;model: root.model;focus: true;+ clip: true;- anchors.fill: parent;- cellWidth: (root.width - parent.scrollWidth)/8;+ cellWidth: (parent.width - charMapScroll.implicitBackgroundWidth)/8;cellHeight: cellWidth;- delegate:SvgTextLabel {- textColor: palette.windowText;- fillColor: palette.base;- fontFamilies: root.fontFamilies;- fontSize: root.fontSize;- fontStyle: root.fontStyle;- fontWeight: root.fontWeight;- text: model.display;- padding: height/8;-- width: charMap.cellWidth;- height: charMap.cellHeight;-- property bool currentItem: GridView.isCurrentItem;- Rectangle {- anchors.fill: parent;- color: "transparent";- border.color: parent.currentItem? palette.highlight: palette.alternateBase;- }- MouseArea {- anchors.fill: parent;- onClicked: {- charMap.currentIndex = index;- console.log(model.toolTip)- }- hoverEnabled: true;- ToolTip.text: model.toolTip;- ToolTip.visible: containsMouse;- }- }+ delegate: glyphDelegate;+ ScrollBar.vertical: ScrollBar {+ id: charMapScroll;+ }}}diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cppindex 463fbd2576..96c627d742 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp@@ -20,7 +20,9 @@ KIS_DECLARE_STATIC_INITIALIZER {}-GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)+GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent)+ : KoDialog(parent)+ , m_model(new KoFontGlyphModel){setMinimumSize(500, 300);@@ -39,6 +41,7 @@ GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)if (m_quickWidget->rootObject()) {m_quickWidget->rootObject()->setProperty("titleText", "Glyph palette");+ m_quickWidget->rootObject()->setProperty("model", QVariant::fromValue(m_model));}if (!m_quickWidget->errors().empty()) {qWarning() << "Errors in " << windowTitle() << ":" << m_quickWidget->errors();@@ -48,16 +51,13 @@ GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties properties, QString text){if (m_lastUsedProperties == properties) {- if (m_quickWidget->rootObject()) {- KoFontGlyphModel *model = dynamic_cast<KoFontGlyphModel*>(m_quickWidget->rootObject()->property("model").value<QAbstractItemModel*>());- if (model) {- QModelIndex idx = model->indexForString(text);- if (idx.isValid()) {- m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));- }+ if (m_model && m_model->rowCount() > 0) {+ QModelIndex idx = m_model->indexForString(text);+ if (idx.isValid() && m_quickWidget->rootObject()) {+ m_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));+ return;}}- return;}qreal res = 72.0;QVector<int> lengths;@@ -81,12 +81,11 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propertproperties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),properties.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),style != QFont::StyleNormal);- KoFontGlyphModel *model = new KoFontGlyphModel(faces.front());- QModelIndex idx = model->indexForString(text);+ m_model->setFace(faces.front());+ QModelIndex idx = m_model->indexForString(text);if (m_quickWidget->rootObject()) {QString name = faces.front().data()->family_name;m_quickWidget->rootObject()->setProperty("titleText", QString("Glyph palette: "+name));- m_quickWidget->rootObject()->setProperty("model", QVariant::fromValue(model));m_quickWidget->rootObject()->setProperty("fontFamilies", QVariant::fromValue(families));m_quickWidget->rootObject()->setProperty("fontSize", QVariant::fromValue(size));m_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(weight));diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.hindex 84f248e6a8..699a6cfadd 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h@@ -9,6 +9,7 @@#include <KoDialog.h>#include <QtQuickWidgets/QQuickWidget>#include <KoSvgTextProperties.h>+#include <KoFontGlyphModel.h>class GlyphPaletteDialog: public KoDialog{@@ -20,6 +21,7 @@ public:private:QQuickWidget *m_quickWidget {0};KoSvgTextProperties m_lastUsedProperties;+ KoFontGlyphModel *m_model;};#endif // GLYPHPALETTEDIALOG_Hdiff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cppindex 8daa01455d..49d2a8336c 100644--- a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp@@ -11,7 +11,6 @@#include <QImage>struct Q_DECL_HIDDEN SvgTextLabel::Private {- QStringList openTypeFeatures;KoSvgTextShape* shape{nullptr};int padding = 2;};@@ -80,7 +79,7 @@ QColor SvgTextLabel::textColor() constQStringList SvgTextLabel::openTypeFeatures() const{- return d->openTypeFeatures;+ return d->shape->textProperties().property(KoSvgTextProperties::FontFeatureSettingsId).value<QStringList>();}QString SvgTextLabel::text() const@@ -159,11 +158,14 @@ void SvgTextLabel::setTextColor(QColor textColor)void SvgTextLabel::setOpenTypeFeatures(QStringList openTypeFeatures){- if (d->openTypeFeatures == openTypeFeatures)+ KoSvgTextProperties props = d->shape->textProperties();+ QStringList otf = props.property(KoSvgTextProperties::FontFeatureSettingsId).value<QStringList>();+ if (otf == openTypeFeatures)return;- d->openTypeFeatures = openTypeFeatures;- emit openTypeFeaturesChanged(d->openTypeFeatures);+ props.setProperty(KoSvgTextProperties::FontFeatureSettingsId, openTypeFeatures);+ d->shape->setPropertiesAtPos(-1, props);+ emit openTypeFeaturesChanged(openTypeFeatures);}void SvgTextLabel::setText(QString text)