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 363 of file KoFontGlyphModel.cpp.

364 : QAbstractItemModel(parent)
365 , d(new Private)
366{
367}
QScopedPointer< Private > d
QModelIndex parent(const QModelIndex &child) const override

◆ ~KoFontGlyphModel()

KoFontGlyphModel::~KoFontGlyphModel ( )

Definition at line 369 of file KoFontGlyphModel.cpp.

370{
371
372}

Member Function Documentation

◆ blocks()

QVector< KoUnicodeBlockData > KoFontGlyphModel::blocks ( ) const

blocks

Returns
list of Unicode blocks available in the font.

Definition at line 574 of file KoFontGlyphModel.cpp.

575{
576 return d->blocks;
577}

References d.

◆ columnCount()

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

Definition at line 504 of file KoFontGlyphModel.cpp.

505{
506 Q_UNUSED(parent);
507 return 1;
508}

References parent().

◆ data()

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

Definition at line 381 of file KoFontGlyphModel.cpp.

382{
383 if (!index.isValid()) {
384 return QVariant();
385 }
386 if (role == Qt::DisplayRole) {
387 //qDebug() << Q_FUNC_INFO<< index << index.parent().isValid() << index.parent();
388 if (!index.parent().isValid()) {
389 return d->codePoints.value(index.row()).utfString;
390 } else {
391 const Private::CodePointInfo &codePoint = d->codePoints.value(index.parent().row());
392 const Private::GlyphInfo &glyph = codePoint.glyphs.value(index.row());
393 //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;
394 if (glyph.type == UnicodeVariationSelector) {
395 return QString(codePoint.utfString + glyph.baseString);
396 } else {
397 return codePoint.utfString;
398 }
399 }
400 } else if (role == Qt::ToolTipRole) {
401 if (!index.parent().isValid()) {
402 QStringList glyphNames;
403 const Private::CodePointInfo &codePoint = d->codePoints.value(index.row());
404 QString base = codePoint.utfString;
405 QVector<Private::GlyphInfo> glyphList = codePoint.glyphs;
406 glyphNames.append(QString("%1 (%2)").arg(base).arg(unicodeHexFromUCS(codePoint.ucs)));
407 if (glyphList.size() > 0) {
408 glyphNames.append(i18nc("@info:tooltip", "%1 glyph variants.").arg(glyphList.size()));
409 }
410 return glyphNames.join(" ");
411 } else {
412 const Private::CodePointInfo &codePoint = d->codePoints.value(index.parent().row());
413 const Private::GlyphInfo &glyph = codePoint.glyphs.value(index.row());
414 if (glyph.type == OpenType) {
415 KoOpenTypeFeatureInfo info = d->featureData.value(glyph.baseString);
416 QString parameterString = info.namedParameters.value(glyph.featureIndex-1);
417 if (parameterString.isEmpty()) {
418 return info.name.isEmpty()? glyph.baseString: info.name;
419 } else {
420 return QString("%1: %2").arg(info.name).arg(parameterString);
421 }
422 } else {
423 return QString(codePoint.utfString + glyph.baseString);
424 }
425 }
426 } else if (role == OpenTypeFeatures) {
427 QVariantMap features;
428 if (index.parent().isValid()) {
429 const Private::CodePointInfo &codePoint = d->codePoints.value(index.parent().row());
430 const Private::GlyphInfo &glyph = codePoint.glyphs.value(index.row());
431 //qDebug () << index.parent().row() << index.row() << codePoint.utfString << codePoint.ucs;
432 if (glyph.type == OpenType) {
433 features.insert(glyph.baseString, glyph.featureIndex);
434 }
435 }
436 return features;
437 } else if (role == GlyphLabel) {
438 QString glyphId;
439 if (!index.parent().isValid()) {
440 const Private::CodePointInfo &codePoint = d->codePoints.value(index.row());
441 glyphId = unicodeHexFromUCS(codePoint.ucs);
442 } else {
443 const Private::CodePointInfo &codePoint = d->codePoints.value(index.parent().row());
444 const Private::GlyphInfo &glyph = codePoint.glyphs.value(index.row());
445 if (glyph.type == OpenType) {
446 glyphId = QString("'%1' %2").arg(glyph.baseString).arg(glyph.featureIndex);
447 } else if (glyph.type == UnicodeVariationSelector) {
448 glyphId = unicodeHexFromUCS(glyph.baseString.toUcs4().first());
449 } else {
450 glyphId = unicodeHexFromUCS(codePoint.ucs);
451 }
452 }
453 return glyphId;
454 } else if (role == ChildCount) {
455 int childCount = 0;
456 if (!index.parent().isValid()) {
457 const Private::CodePointInfo &codePoint = d->codePoints.value(index.row());
458 childCount = codePoint.childCount();
459 }
460 return childCount;
461 }
462 return QVariant();
463}
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::CodePointInfo::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 579 of file KoFontGlyphModel.cpp.

580{
581 return d->featureData;
582}

References d.

◆ hasChildren()

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

Definition at line 510 of file KoFontGlyphModel.cpp.

511{
512 if (parent.isValid()) {
513 return !d->codePoints.value(parent.row()).glyphs.isEmpty();
514 }
515 return false;
516}

References d, and parent().

◆ index()

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

Definition at line 465 of file KoFontGlyphModel.cpp.

466{
467 if (parent.isValid() && parent.row() >= 0 && parent.row() < d->codePoints.size()) {
468 const Private::CodePointInfo &info = d->codePoints.at(parent.row());
469 if (row >= 0 && row < info.glyphs.size()) {
470 const Private::GlyphInfo &glyphInfo = info.glyphs.at(row);
471 return createIndex(row, column, static_cast<quintptr>(glyphInfo.ucs));
472 }
473
474 } else if (row >= 0 && row < d->codePoints.size()) {
475 return createIndex(row, column, static_cast<quintptr>(invalidUnicodeCodePoint));
476 }
477 return QModelIndex();
478}
static constexpr uint invalidUnicodeCodePoint

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

◆ indexForString()

QModelIndex KoFontGlyphModel::indexForString ( QString grapheme)

Definition at line 518 of file KoFontGlyphModel.cpp.

519{
520 for (int i = 0; i < d->codePoints.size(); i++) {
521 if (grapheme.toUcs4().startsWith(d->codePoints.at(i).ucs)) {
522 QModelIndex idx = index(i, 0, QModelIndex());
523 return idx;
524 }
525 }
526 return QModelIndex();
527}

References d, and index().

◆ parent()

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

Definition at line 480 of file KoFontGlyphModel.cpp.

481{
482 if (!child.isValid() || child.internalId() == invalidUnicodeCodePoint) {
483 return QModelIndex();
484 }
485 const uint targetUcs = static_cast<uint>(child.internalId());
486 for(int i = 0; i < d->codePoints.size(); i++) {
487 const Private::CodePointInfo &info = d->codePoints.at(i);
488 if (info.ucs == targetUcs) {
489 return createIndex(i, 0);
490 }
491 }
492
493 return QModelIndex();
494}
unsigned int uint

References d, invalidUnicodeCodePoint, and KoFontGlyphModel::Private::CodePointInfo::ucs.

◆ roleNames()

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

Definition at line 565 of file KoFontGlyphModel.cpp.

566{
567 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
568 roles[OpenTypeFeatures] = "openType";
569 roles[GlyphLabel] = "glyphLabel";
570 roles[ChildCount] = "childCount";
571 return roles;
572}

References ChildCount, GlyphLabel, and OpenTypeFeatures.

◆ rowCount()

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

Definition at line 496 of file KoFontGlyphModel.cpp.

497{
498 if (parent.isValid()) {
499 return d->codePoints.value(parent.row()).glyphs.size();
500 }
501 return d->codePoints.size();
502}

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 533 of file KoFontGlyphModel.cpp.

534{
535 beginResetModel();
536 d->codePoints = Private::charMap(face);
537 d->blocks.clear();
538 QMap<uint, QVector<Private::GlyphInfo>> VSData = Private::getVSData(face);
539 KoUnicodeBlockDataFactory blockFactory;
540
541 for(auto it = d->codePoints.begin(); it != d->codePoints.end(); it++) {
542 it->glyphs.append(VSData.value(it->ucs));
543
544 auto block = d->blocks.begin();
545 for (; block != d->blocks.end(); block++) {
546 if (block->match(it->ucs)) {
547 break;
548 }
549 }
550 if (block == d->blocks.end()) {
551 KoUnicodeBlockData newBlock = blockFactory.blockForUCS(it->ucs);
552 if (newBlock != KoUnicodeBlockDataFactory::noBlock()) {
553 d->blocks.append(newBlock);
554 }
555 }
556 }
557 std::sort(d->blocks.begin(), d->blocks.end(), sortBlocks);
558
559 d->featureData = Private::getOpenTypeTables(face, d->codePoints, QMap<QString, KoOpenTypeFeatureInfo>() , false, samplesOnly, KLocalizedString::languages(), language);
560 d->featureData = Private::getOpenTypeTables(face, d->codePoints, d->featureData, true, samplesOnly, KLocalizedString::languages(), language);
561
562 endResetModel();
563}
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: