Krita Source Code Documentation
Loading...
Searching...
No Matches
OpenTypeFeatureModel Class Reference

The OpenTypeFeatureModel class This model keeps track of the currently set font-feature-settings property on a given piece of text, and allows editing it. It also keeps track of the available features of a given font. More...

#include <OpenTypeFeatureModel.h>

+ Inheritance diagram for OpenTypeFeatureModel:

Classes

struct  Private
 

Public Types

enum  Roles { Tag = Qt::UserRole + 1 , Sample , Parameters , Max }
 

Signals

void openTypeFeaturesChanged ()
 

Public Member Functions

Q_INVOKABLE void addFeature (const QString &tag)
 
Q_INVOKABLE QAbstractItemModel * allFeatureModel () const
 featureFilterModel
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
 OpenTypeFeatureModel (QObject *parent=nullptr)
 
QVariantMap openTypeFeatures () const
 
QModelIndex parent (const QModelIndex &index) const override
 
Q_INVOKABLE void removeFeature (const QString &tag)
 
QHash< int, QByteArray > roleNames () const override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 
void setFromTextProperties (const KoSvgTextProperties &props)
 
Q_INVOKABLE void setFromTextPropertiesModel (KoSvgTextPropertiesModel *textPropertiesModel)
 setFromTextPropertiesModel Set the current glyph model font from the lager text properties model. This allows us to use this class from qml, with little fuss.
 
void setOpenTypeFeatures (const QVariantMap &newOpenTypeFeatures)
 
 ~OpenTypeFeatureModel ()
 

Properties

QVariantMap openTypeFeatures
 

Private Attributes

const QScopedPointer< Privated
 

Detailed Description

The OpenTypeFeatureModel class This model keeps track of the currently set font-feature-settings property on a given piece of text, and allows editing it. It also keeps track of the available features of a given font.

Each feature is internally represented as a tag plus a number.

Definition at line 51 of file OpenTypeFeatureModel.h.

Member Enumeration Documentation

◆ Roles

Enumerator
Tag 

QString, opentype tag.

Sample 

QString, the sample for this feature, may be empty.

Parameters 

QVariantList, indices with names of the feature count.

Max 

int, max count, default is 1, but can be larger depending on the font.

Definition at line 62 of file OpenTypeFeatureModel.h.

62 {
63 Tag = Qt::UserRole + 1,
64 Sample,
66 Max
67 };
@ Max
int, max count, default is 1, but can be larger depending on the font.
@ Sample
QString, the sample for this feature, may be empty.
@ Tag
QString, opentype tag.
@ Parameters
QVariantList, indices with names of the feature count.

Constructor & Destructor Documentation

◆ OpenTypeFeatureModel()

OpenTypeFeatureModel::OpenTypeFeatureModel ( QObject * parent = nullptr)

Definition at line 48 of file OpenTypeFeatureModel.cpp.

49 : QAbstractItemModel(parent)
50 , d(new Private(this))
51{
52}
QModelIndex parent(const QModelIndex &index) const override
const QScopedPointer< Private > d

◆ ~OpenTypeFeatureModel()

OpenTypeFeatureModel::~OpenTypeFeatureModel ( )

Definition at line 54 of file OpenTypeFeatureModel.cpp.

55{
56}

Member Function Documentation

◆ addFeature()

void OpenTypeFeatureModel::addFeature ( const QString & tag)

Definition at line 212 of file OpenTypeFeatureModel.cpp.

213{
214 if (tag.isEmpty()) return;
215 if (d->currentFeatures.keys().contains(tag)) return;
216
217 QVariantMap dummy = d->currentFeatures;
218 dummy.insert(tag, QVariant(1));
219 const int index = dummy.keys().indexOf(tag);
220
221 beginInsertRows(QModelIndex(), index, index);
222 d->currentFeatures.insert(tag, QVariant(1));
223 endInsertRows();
225}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void openTypeFeaturesChanged()

References d, index(), and openTypeFeaturesChanged().

◆ allFeatureModel()

QAbstractItemModel * OpenTypeFeatureModel::allFeatureModel ( ) const

featureFilterModel

Returns
return the OpenTypeFeatureFilterModel that allows searching and sorting on all opentype features, whether available in the font or part of the standard.

Definition at line 239 of file OpenTypeFeatureModel.cpp.

240{
241 return d->allFeatures;
242}

References d.

◆ columnCount()

int OpenTypeFeatureModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 78 of file OpenTypeFeatureModel.cpp.

79{
80 Q_UNUSED(parent)
81 return 1;
82}

References parent().

◆ data()

QVariant OpenTypeFeatureModel::data ( const QModelIndex & index,
int role = Qt::DisplayRole ) const
override

Definition at line 84 of file OpenTypeFeatureModel.cpp.

85{
86 if (!index.isValid())
87 return QVariant();
88
89 const QString feature = d->currentFeatures.keys().at(index.row());
90 KoOpenTypeFeatureInfo info = d->featureByTag(QLatin1String(feature.toLatin1()));
91 if (role == Qt::DisplayRole) {
92 return info.name;
93 } else if (role == Qt::ToolTipRole) {
94 return info.description;
95 } else if (role == Qt::EditRole) {
96 return d->currentFeatures.value(feature, QVariant(0)).toInt();
97 } else if (role == Tag) {
98 return feature;
99 } else if (role == Sample) {
100 return info.sample;
101 } else if (role == Parameters) {
102 QVariantList features;
103 for (int i = 0; i <= info.maxValue; i++) {
104 QVariantMap map;
105 map.insert("value", QVariant(i));
106 QString entry = i > 0? info.namedParameters.value(i-1): QString();
107 if (entry.isEmpty()) {
108 if (i == 1 && info.maxValue == 1) {
109 entry = info.name + ": " + i18nc("Feature value toggle", "On");
110 } else if (i == 0) {
111 entry = info.name + ": " + i18nc("Feature value toggle", "Off");
112 } else {
113 entry = info.name + ": " + QString::number(i);
114 }
115 }
116 map.insert("display", entry);
117 features.append(map);
118 }
119 return features;
120 } else if (role == Max) {
121 return info.maxValue;
122 }
123 return QVariant();
124}
QString description
Description of the feature.
QString name
User-friendly name.
QString sample
Sample of the feature, if any. Only used by CVXX features and retrieved from the font.
int maxValue
The maximum value possible, this is by default 1 (on), but for alternate substitution(gsub 3),...

References d, KoOpenTypeFeatureInfo::description, index(), Max, KoOpenTypeFeatureInfo::maxValue, KoOpenTypeFeatureInfo::name, KoOpenTypeFeatureInfo::namedParameters, Parameters, KoOpenTypeFeatureInfo::sample, Sample, and Tag.

◆ flags()

Qt::ItemFlags OpenTypeFeatureModel::flags ( const QModelIndex & index) const
override

Definition at line 144 of file OpenTypeFeatureModel.cpp.

145{
146 Qt::ItemFlags flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsEditable;
147
148 return flags;
149}
Qt::ItemFlags flags(const QModelIndex &index) const override

References flags(), and index().

◆ index()

QModelIndex OpenTypeFeatureModel::index ( int row,
int column,
const QModelIndex & parent = QModelIndex() ) const
override

Definition at line 58 of file OpenTypeFeatureModel.cpp.

59{
60 Q_UNUSED(parent)
61 if (column != 0) return QModelIndex();
62 if (row >= 0 && row < d->currentFeatures.size()) return createIndex(row, column, &row);
63 return QModelIndex();
64}

References parent().

◆ openTypeFeatures()

QVariantMap OpenTypeFeatureModel::openTypeFeatures ( ) const

Definition at line 197 of file OpenTypeFeatureModel.cpp.

198{
199 return d->currentFeatures;
200}

References d.

◆ openTypeFeaturesChanged

void OpenTypeFeatureModel::openTypeFeaturesChanged ( )
signal

◆ parent()

QModelIndex OpenTypeFeatureModel::parent ( const QModelIndex & index) const
override

Definition at line 66 of file OpenTypeFeatureModel.cpp.

67{
68 Q_UNUSED(index)
69 return QModelIndex();
70}

References index().

◆ removeFeature()

void OpenTypeFeatureModel::removeFeature ( const QString & tag)

Definition at line 227 of file OpenTypeFeatureModel.cpp.

228{
229 if (tag.isEmpty()) return;
230 const int index = d->currentFeatures.keys().indexOf(tag);
231 if (index < 0) return;
232
233 beginRemoveRows(QModelIndex(), index, index);
234 d->currentFeatures.remove(tag);
235 endRemoveRows();
237}

References d, index(), and openTypeFeaturesChanged().

◆ roleNames()

QHash< int, QByteArray > OpenTypeFeatureModel::roleNames ( ) const
override

Definition at line 151 of file OpenTypeFeatureModel.cpp.

152{
153 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
154 roles[Tag] = "tag";
155 roles[Sample] = "sample";
156 roles[Parameters] = "parameters";
157 roles[Max] = "max";
158 return roles;
159}

References Max, Parameters, Sample, and Tag.

◆ rowCount()

int OpenTypeFeatureModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 72 of file OpenTypeFeatureModel.cpp.

73{
74 Q_UNUSED(parent)
75 return d->currentFeatures.size();
76}

References d, and parent().

◆ setData()

bool OpenTypeFeatureModel::setData ( const QModelIndex & index,
const QVariant & value,
int role = Qt::EditRole )
override

Definition at line 126 of file OpenTypeFeatureModel.cpp.

127{
128 if (data(index, role) != value) {
129 // FIXME: Implement me!
130
131 if (index.isValid() && role == Qt::EditRole) {
132 const QString feature = d->currentFeatures.keys().at(index.row());
133 d->currentFeatures.insert(feature, value.toInt());
135 emit dataChanged(index, index, {role});
136 return true;
137 }
138 return false;
139 }
140
141 return false;
142}
float value(const T *src, size_t ch)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override

References d, data(), index(), openTypeFeaturesChanged(), and value().

◆ setFromTextProperties()

void OpenTypeFeatureModel::setFromTextProperties ( const KoSvgTextProperties & props)

Definition at line 161 of file OpenTypeFeatureModel.cpp.

162{
163 const bool changeFont = props.cssFontInfo() != d->fontInfo;
164 const QVariantMap newFeatures = props.propertyOrDefault(KoSvgTextProperties::FontFeatureSettingsId).toMap();
165 const bool changeFeatures = d->currentFeatures != newFeatures;
166
167 if (!changeFont && !changeFeatures) return;
168
169 beginResetModel();
170 if (changeFont) {
171 const qreal res = 72.0;
172 QVector<int> lengths;
173 const KoCSSFontInfo info = props.cssFontInfo();
174 const std::vector<FT_FaceSP> faces = KoFontRegistry::instance()->facesForCSSValues(
175 lengths,
176 info,
177 QString(),
178 static_cast<quint32>(res),
179 static_cast<quint32>(res));
180
181 if (!faces.empty()) {
182 QString language = props.propertyOrDefault(KoSvgTextProperties::TextLanguage).toString();
183 // NOTE: We're retrieving only enough data for 6 samples here, to speed up loading.
184 d->glyphModel->setFace(faces.front(), QLatin1String(language.toLatin1()), true);
185 d->allFeatures->setAvailableFeatures(d->availableFeatures());
186 d->fontInfo = props.cssFontInfo();
187 }
188 }
189
190 if (changeFeatures) {
191 d->currentFeatures = newFeatures;
193 }
194 endResetModel();
195}
std::vector< FT_FaceSP > facesForCSSValues(QVector< int > &lengths, KoCSSFontInfo info=KoCSSFontInfo(), const QString &text="", quint32 xRes=72, quint32 yRes=72, bool disableFontMatching=false, const QString &language=QString())
facesForCSSValues This selects a font with fontconfig using the given values. If "text" is not empty ...
static KoFontRegistry * instance()
@ FontFeatureSettingsId
QStringList.
@ TextLanguage
a language string.
KoCSSFontInfo cssFontInfo() const
cssFontInfo
QVariant propertyOrDefault(PropertyId id) const
The KoCSSFontInfo class Convenience struct to make it easier to use KoFontRegistry....

References KoSvgTextProperties::cssFontInfo(), d, KoFontRegistry::facesForCSSValues(), KoSvgTextProperties::FontFeatureSettingsId, KoFontRegistry::instance(), openTypeFeaturesChanged(), KoSvgTextProperties::propertyOrDefault(), and KoSvgTextProperties::TextLanguage.

◆ setFromTextPropertiesModel()

void OpenTypeFeatureModel::setFromTextPropertiesModel ( KoSvgTextPropertiesModel * textPropertiesModel)

setFromTextPropertiesModel Set the current glyph model font from the lager text properties model. This allows us to use this class from qml, with little fuss.

Parameters
newTextProperties– the lager model to set from.

Definition at line 244 of file OpenTypeFeatureModel.cpp.

245{
247 if (textPropertiesModel) {
248 KoSvgTextPropertyData data = textPropertiesModel->textData.get();
249 main = data.commonProperties;
250 main.inheritFrom(data.inheritedProperties);
251 }
253}
lager::cursor< KoSvgTextPropertyData > textData
void setFromTextProperties(const KoSvgTextProperties &props)
int main(int argc, char **argv)
Definition main.cpp:26
The KoSvgTextPropertyData struct.

References data(), main(), setFromTextProperties(), and KoSvgTextPropertiesModel::textData.

◆ setOpenTypeFeatures()

void OpenTypeFeatureModel::setOpenTypeFeatures ( const QVariantMap & newOpenTypeFeatures)

Definition at line 202 of file OpenTypeFeatureModel.cpp.

203{
204 if (d->currentFeatures == newOpenTypeFeatures)
205 return;
206 beginResetModel();
207 d->currentFeatures = newOpenTypeFeatures;
208 endResetModel();
210}

References d, and openTypeFeaturesChanged().

Member Data Documentation

◆ d

const QScopedPointer<Private> OpenTypeFeatureModel::d
private

Definition at line 121 of file OpenTypeFeatureModel.h.

Property Documentation

◆ openTypeFeatures

QVariantMap OpenTypeFeatureModel::openTypeFeatures
readwrite

Definition at line 56 of file OpenTypeFeatureModel.h.


The documentation for this class was generated from the following files: