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

The KoFontGlyphModel class Creates a tree model of all the glyphs in a given face. More...

#include <KoFontGlyphModel.h>

+ Inheritance diagram for KoFontGlyphModel:

Classes

struct  Private
 

Public Types

enum  GlyphType { Base , UnicodeVariationSelector , OpenType }
 
enum  Roles { OpenTypeFeatures = Qt::UserRole + 1 , GlyphLabel , ChildCount }
 

Public Member Functions

QVector< KoUnicodeBlockDatablocks () const
 blocks
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
QMap< QString, KoOpenTypeFeatureInfofeatureInfo () const
 featureInfo
 
bool hasChildren (const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex indexForString (QString grapheme)
 
 KoFontGlyphModel (QObject *parent=nullptr)
 
QModelIndex parent (const QModelIndex &child) const override
 
QHash< int, QByteArray > roleNames () const override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
void setFace (FT_FaceSP face, QLatin1String language=QLatin1String(), bool samplesOnly=false)
 setFace set the face to retrieve glyph data for.
 
 ~KoFontGlyphModel ()
 

Private Attributes

QScopedPointer< Privated
 

Detailed Description

The KoFontGlyphModel class Creates a tree model of all the glyphs in a given face.

The primary parents are the basic codepoints, the children of those parents (if any), are glyph variations.

Definition at line 23 of file KoFontGlyphModel.h.

Member Enumeration Documentation

◆ GlyphType

Enumerator
Base 
UnicodeVariationSelector 
OpenType 

Definition at line 27 of file KoFontGlyphModel.h.

◆ Roles

Enumerator
OpenTypeFeatures 
GlyphLabel 
ChildCount 

Definition at line 36 of file KoFontGlyphModel.h.

Constructor & Destructor Documentation

◆ KoFontGlyphModel()

KoFontGlyphModel::KoFontGlyphModel ( QObject * parent = nullptr)

Definition at line 326 of file KoFontGlyphModel.cpp.

327 : QAbstractItemModel(parent)
328 , d(new Private)
329{
330}
QScopedPointer< Private > d
QModelIndex parent(const QModelIndex &child) const override

◆ ~KoFontGlyphModel()

KoFontGlyphModel::~KoFontGlyphModel ( )

Definition at line 332 of file KoFontGlyphModel.cpp.

333{
334
335}

Member Function Documentation

◆ blocks()

QVector< KoUnicodeBlockData > KoFontGlyphModel::blocks ( ) const

blocks

Returns
list of Unicode blocks available in the font.

Definition at line 539 of file KoFontGlyphModel.cpp.

540{
541 return d->blocks;
542}

References d.

◆ columnCount()

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

Definition at line 470 of file KoFontGlyphModel.cpp.

471{
472 Q_UNUSED(parent);
473 return 1;
474}

References parent().

◆ data()

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

Definition at line 344 of file KoFontGlyphModel.cpp.

345{
346 if (!index.isValid()) {
347 return QVariant();
348 }
349 if (role == Qt::DisplayRole) {
350 //qDebug() << Q_FUNC_INFO<< index << index.parent().isValid() << index.parent();
351 if (!index.parent().isValid()) {
352 return d->codePoints.value(index.row()).utfString;
353 } else {
354 Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());
355 Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());
356 //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;
357 if (glyph.type == UnicodeVariationSelector) {
358 return QString(codePoint.utfString + glyph.baseString);
359 } else {
360 return codePoint.utfString;
361 }
362 }
363 } else if (role == Qt::ToolTipRole) {
364 if (!index.parent().isValid()) {
365 QStringList glyphNames;
366 Private::CodePointInfo codePoint = d->codePoints.value(index.row());
367 QString base = codePoint.utfString;
368 QVector<Private::GlyphInfo> glyphList = codePoint.glyphs;
369 glyphNames.append(QString("%1 (%2)").arg(base).arg(unicodeHexFromUCS(codePoint.ucs)));
370 if (glyphList.size() > 0) {
371 glyphNames.append(i18nc("@info:tooltip", "%1 glyph variants.").arg(glyphList.size()));
372 }
373 return glyphNames.join(" ");
374 } else {
375 Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());
376 Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());
377 if (glyph.type == OpenType) {
378 KoOpenTypeFeatureInfo info = d->featureData.value(glyph.baseString);
379 QString parameterString = info.namedParameters.value(glyph.featureIndex-1);
380 if (parameterString.isEmpty()) {
381 return info.name.isEmpty()? glyph.baseString: info.name;
382 } else {
383 return QString("%1: %2").arg(info.name).arg(parameterString);
384 }
385 } else {
386 return QString(codePoint.utfString + glyph.baseString);
387 }
388 }
389 } else if (role == OpenTypeFeatures) {
390 QVariantMap features;
391 if (index.parent().isValid()) {
392 Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());
393 Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());
394 //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;
395 if (glyph.type == OpenType) {
396 features.insert(glyph.baseString, glyph.featureIndex);
397 }
398 }
399 return features;
400 } else if (role == GlyphLabel) {
401 QString glyphId;
402 if (!index.parent().isValid()) {
403 Private::CodePointInfo codePoint = d->codePoints.value(index.row());
404 glyphId = unicodeHexFromUCS(codePoint.ucs);
405 } else {
406 Private::CodePointInfo codePoint = d->codePoints.value(index.parent().row());
407 Private::GlyphInfo glyph = codePoint.glyphs.value(index.row());
408 if (glyph.type == OpenType) {
409 glyphId = QString("'%1' %2").arg(glyph.baseString).arg(glyph.featureIndex);
410 } else if (glyph.type == UnicodeVariationSelector) {
411 glyphId = unicodeHexFromUCS(glyph.baseString.toUcs4().first());
412 } else {
413 glyphId = unicodeHexFromUCS(codePoint.ucs);
414 }
415 }
416 return glyphId;
417 } else if (role == ChildCount) {
418 int childCount = 0;
419 if (!index.parent().isValid()) {
420 Private::CodePointInfo codePoint = d->codePoints.value(index.row());
421 childCount = codePoint.childCount();
422 }
423 return childCount;
424 }
425 return QVariant();
426}
QString unicodeHexFromUCS(const uint codePoint)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QString name
User-friendly name.

References KoFontGlyphModel::Private::GlyphInfo::baseString, KoFontGlyphModel::Private::CodePointInfo::childCount(), ChildCount, d, KoFontGlyphModel::Private::GlyphInfo::featureIndex, GlyphLabel, KoFontGlyphModel::Private::CodePointInfo::glyphs, index(), KoOpenTypeFeatureInfo::name, KoOpenTypeFeatureInfo::namedParameters, OpenType, OpenTypeFeatures, KoFontGlyphModel::Private::GlyphInfo::type, KoFontGlyphModel::Private::InfoNode::ucs, unicodeHexFromUCS(), UnicodeVariationSelector, and KoFontGlyphModel::Private::CodePointInfo::utfString.

◆ featureInfo()

QMap< QString, KoOpenTypeFeatureInfo > KoFontGlyphModel::featureInfo ( ) const

featureInfo

Returns
list of OpenTypeFeatures available in the font.

Definition at line 544 of file KoFontGlyphModel.cpp.

545{
546 return d->featureData;
547}

References d.

◆ hasChildren()

bool KoFontGlyphModel::hasChildren ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 476 of file KoFontGlyphModel.cpp.

477{
478 if (parent.isValid()) {
479 return !d->codePoints.value(parent.row()).glyphs.isEmpty();
480 }
481 return false;
482}

References d, and parent().

◆ index()

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

Definition at line 428 of file KoFontGlyphModel.cpp.

429{
430 if (parent.isValid() && parent.row() >= 0 && parent.row() < d->codePoints.size()) {
431 Private::CodePointInfo info = d->codePoints.at(parent.row());
432 if (row >= 0 && row < info.glyphs.size()) {
433 Private::GlyphInfo glyphInfo = info.glyphs.at(row);
434 return createIndex(row, column, &glyphInfo);
435 }
436
437 } else if (row >= 0 && row < d->codePoints.size()) {
438 return createIndex(row, column);
439 }
440 return QModelIndex();
441}

References d, KoFontGlyphModel::Private::CodePointInfo::glyphs, and parent().

◆ indexForString()

QModelIndex KoFontGlyphModel::indexForString ( QString grapheme)

Definition at line 484 of file KoFontGlyphModel.cpp.

485{
486 for (int i = 0; i < d->codePoints.size(); i++) {
487 if (grapheme.toUcs4().startsWith(d->codePoints.at(i).ucs)) {
488 QModelIndex idx = index(i, 0, QModelIndex());
489 return idx;
490 }
491 }
492 return QModelIndex();
493}

References d, and index().

◆ parent()

QModelIndex KoFontGlyphModel::parent ( const QModelIndex & child) const
override

Definition at line 443 of file KoFontGlyphModel.cpp.

444{
445 if (!child.isValid() || !child.internalPointer()) {
446 return QModelIndex();
447 }
448 Private::InfoNode *node = static_cast<Private::InfoNode*>(child.internalPointer());
449 if (node) {
450 const uint targetUcs = node->ucs;
451 for(int i = 0; i < d->codePoints.size(); i++) {
452 Private::CodePointInfo info = d->codePoints.at(i);
453 if (info.ucs == targetUcs) {
454 return createIndex(i, 0);
455 }
456 }
457 }
458
459 return QModelIndex();
460}
unsigned int uint

References d, and KoFontGlyphModel::Private::InfoNode::ucs.

◆ roleNames()

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

Definition at line 530 of file KoFontGlyphModel.cpp.

531{
532 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
533 roles[OpenTypeFeatures] = "openType";
534 roles[GlyphLabel] = "glyphLabel";
535 roles[ChildCount] = "childCount";
536 return roles;
537}

References ChildCount, GlyphLabel, and OpenTypeFeatures.

◆ rowCount()

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

Definition at line 462 of file KoFontGlyphModel.cpp.

463{
464 if (parent.isValid()) {
465 return d->codePoints.value(parent.row()).glyphs.size();
466 }
467 return d->codePoints.size();
468}

References d, and parent().

◆ setFace()

void KoFontGlyphModel::setFace ( FT_FaceSP face,
QLatin1String language = QLatin1String(),
bool samplesOnly = false )

setFace set the face to retrieve glyph data for.

Parameters
face– the face.
language– the language for which to retrieve data for, OpenType data can have different glyphs depending on the language.
samplesOnly– Whether to only retrieve enough data for 6 samples, or to retrieve the full glyph layout. Turning this on speeds up loading.

Definition at line 499 of file KoFontGlyphModel.cpp.

500{
501 beginResetModel();
502 d->codePoints = Private::charMap(face);
503 d->blocks.clear();
504 QMap<uint, QVector<Private::GlyphInfo>> VSData = Private::getVSData(face);
505 KoUnicodeBlockDataFactory blockFactory;
506
507 for(auto it = d->codePoints.begin(); it != d->codePoints.end(); it++) {
508 it->glyphs.append(VSData.value(it->ucs));
509
510 auto block = d->blocks.begin();
511 for (; block != d->blocks.end(); block++) {
512 if (block->match(it->ucs)) {
513 break;
514 }
515 }
516 if (block == d->blocks.end()) {
517 KoUnicodeBlockData newBlock = blockFactory.blockForUCS(it->ucs);
518 if (newBlock != KoUnicodeBlockDataFactory::noBlock()) {
519 d->blocks.append(newBlock);
520 }
521 }
522 }
523 std::sort(d->blocks.begin(), d->blocks.end(), sortBlocks);
524 d->featureData = Private::getOpenTypeTables(face, d->codePoints, QMap<QString, KoOpenTypeFeatureInfo>() , false, samplesOnly, KLocalizedString::languages(), language);
525 d->featureData = Private::getOpenTypeTables(face, d->codePoints, d->featureData, true, samplesOnly, KLocalizedString::languages(), language);
526
527 endResetModel();
528}
static bool sortBlocks(const KoUnicodeBlockData &a, const KoUnicodeBlockData &b)
static QMap< QString, KoOpenTypeFeatureInfo > getOpenTypeTables(FT_FaceSP face, QVector< CodePointInfo > &charMap, QMap< QString, KoOpenTypeFeatureInfo > previousFeatureInfo, bool gpos, bool samplesOnly, QStringList locales, QLatin1String lang=QLatin1String())
static QVector< CodePointInfo > charMap(FT_FaceSP face)
static QMap< uint, QVector< GlyphInfo > > getVSData(FT_FaceSP face)
static KoUnicodeBlockData noBlock()
KoUnicodeBlockData blockForUCS(const uint &codepoint)

References KoUnicodeBlockDataFactory::blockForUCS(), KoFontGlyphModel::Private::charMap(), d, KoFontGlyphModel::Private::getOpenTypeTables(), KoFontGlyphModel::Private::getVSData(), KoUnicodeBlockDataFactory::noBlock(), and sortBlocks().

Member Data Documentation

◆ d

QScopedPointer<Private> KoFontGlyphModel::d
private

Definition at line 77 of file KoFontGlyphModel.h.


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