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

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

◆ ~KoFontGlyphModel()

KoFontGlyphModel::~KoFontGlyphModel ( )

Definition at line 368 of file KoFontGlyphModel.cpp.

369{
370
371}

Member Function Documentation

◆ blocks()

QVector< KoUnicodeBlockData > KoFontGlyphModel::blocks ( ) const

blocks

Returns
list of Unicode blocks available in the font.

Definition at line 572 of file KoFontGlyphModel.cpp.

573{
574 return d->blocks;
575}

References d.

◆ columnCount()

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

Definition at line 503 of file KoFontGlyphModel.cpp.

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

References parent().

◆ data()

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

Definition at line 380 of file KoFontGlyphModel.cpp.

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

578{
579 return d->featureData;
580}

References d.

◆ hasChildren()

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

Definition at line 509 of file KoFontGlyphModel.cpp.

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

References d, and parent().

◆ index()

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

Definition at line 464 of file KoFontGlyphModel.cpp.

465{
466 if (parent.isValid() && parent.row() >= 0 && parent.row() < d->codePoints.size()) {
467 const Private::CodePointInfo &info = d->codePoints.at(parent.row());
468 if (row >= 0 && row < info.glyphs.size()) {
469 const Private::GlyphInfo &glyphInfo = info.glyphs.at(row);
470 return createIndex(row, column, static_cast<quintptr>(glyphInfo.ucs));
471 }
472
473 } else if (row >= 0 && row < d->codePoints.size()) {
474 return createIndex(row, column, static_cast<quintptr>(invalidUnicodeCodePoint));
475 }
476 return QModelIndex();
477}
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 517 of file KoFontGlyphModel.cpp.

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

References d, and index().

◆ parent()

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

Definition at line 479 of file KoFontGlyphModel.cpp.

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

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

◆ roleNames()

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

Definition at line 563 of file KoFontGlyphModel.cpp.

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

References ChildCount, GlyphLabel, and OpenTypeFeatures.

◆ rowCount()

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

Definition at line 495 of file KoFontGlyphModel.cpp.

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

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

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

References KoFontGlyphModel::Private::charMap(), d, 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: