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++
Implement "relayoutIsBlocked" and "fontMatchingDisabled" to speed up glyph palette.
Wolthera van Hövell tot Westerflier3f737cc045e2
diff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cppindex d5f315ad00..a5b514970b 100644--- a/libs/flake/text/KoFontRegistry.cpp+++ b/libs/flake/text/KoFontRegistry.cpp@@ -241,7 +241,7 @@ std::vector<FT_FaceSP> KoFontRegistry::facesForCSSValues(const QStringList &famiint weight,int width,int slantMode,- int slantValue,+ int slantValue, bool disableFontMatching,const QString &language){// Generate a hash for the cache.@@ -285,80 +285,6 @@ std::vector<FT_FaceSP> KoFontRegistry::facesForCSSValues(const QStringList &famid->suggestedFileNames().insert(suggestedHash, candidates);}--- FcPatternSP p(FcPatternCreate());- Q_FOREACH (const QString &family, families) {- QByteArray utfData = family.toUtf8();- const FcChar8 *vals = reinterpret_cast<FcChar8 *>(utfData.data());- FcPatternAddString(p.data(), FC_FAMILY, vals);- }-- {- QByteArray fallbackBuf = QString("sans-serif").toUtf8();- FcValue fallback;- fallback.type = FcTypeString;- fallback.u.s = reinterpret_cast<FcChar8 *>(fallbackBuf.data());- FcPatternAddWeak(p.data(), FC_FAMILY, fallback, true);- }-- for (int i = 0; i < candidates.size(); i++) {- const QPair<QString, int> file = candidates.at(i);- QByteArray utfData = file.first.toUtf8();- const FcChar8 *vals = reinterpret_cast<FcChar8 *>(utfData.data());- FcPatternAddString(p.data(), FC_FILE, vals);- FcPatternAddInteger(p.data(), FC_INDEX, file.second);- }-- if (slantMode == 1) {- FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_ITALIC);- } else if (slantMode == 2) {- FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_OBLIQUE);- } else {- FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_ROMAN);- }- FcPatternAddInteger(p.data(), FC_WEIGHT, FcWeightFromOpenType(weight));- FcPatternAddInteger(p.data(), FC_WIDTH, width);-- double pixelSize = size*(qMin(xRes, yRes)/72.0);- FcPatternAddDouble(p.data(), FC_PIXEL_SIZE, pixelSize);-- FcConfigSubstitute(nullptr, p.data(), FcMatchPattern);- FcDefaultSubstitute(p.data());--- p = [&]() {- const FcChar32 hash = FcPatternHash(p.data());- QString patternHash = families.join("+")+QString::number(hash);- // FCPatternHash breaks down when there's mutliple family names that start- // with the same letter and are the same length (Butcherman and Babylonica, Eater and Elsie, all 4 on google fonts).- const auto oldPattern = d->patterns().find(patternHash);- if (oldPattern != d->patterns().end()) {- return oldPattern.value();- } else {- d->patterns().insert(patternHash, p);- return p;- }- }();-- FcResult result = FcResultNoMatch;- FcCharSetSP charSet;- FcFontSetSP fontSet = [&]() -> FcFontSetSP {- const FcChar32 hash = FcPatternHash(p.data());- QString patternHash = families.join("+")+QString::number(hash);- const auto set = d->sets().find(patternHash);-- if (set != d->sets().end()) {- return set.value();- } else {- FcCharSet *cs = nullptr;- FcFontSetSP avalue(FcFontSort(FcConfigGetCurrent(), p.data(), FcFalse, &cs, &result));- charSet.reset(cs);- d->sets().insert(patternHash, avalue);- return avalue;- }- }();-struct FontEntry {QString fileName;int fontIndex;@@ -386,129 +312,211 @@ std::vector<FT_FaceSP> KoFontRegistry::facesForCSSValues(const QStringList &famiQVector<FontEntry> fonts;lengths.clear();- if (text.isEmpty()) {- for (int j = 0; j < fontSet->nfont; j++) {- if (std::optional<FontEntry> font = FontEntry::get(fontSet->fonts[j])) {- fonts.append(std::move(*font));- lengths.append(0);- break;- }- }+ if (disableFontMatching && !candidates.isEmpty()) {+ FontEntry entry;+ entry.fileName = candidates.first().first;+ entry.fontIndex = candidates.first().second;+ fonts.append(entry);+ lengths.append(text.size());} else {- FcCharSet *set = nullptr;- QVector<int> familyValues(text.size());- QVector<int> fallbackMatchValues(text.size());- familyValues.fill(-1);- fallbackMatchValues.fill(-1);-- // First, we're going to split up the text into graphemes. This is both- // because the css spec requires it, but also because of why the css- // spec requires it: graphemes' parts should not end up in seperate- // runs, which they will if they get assigned different fonts,- // potentially breaking ligatures and emoji sequences.- QStringList graphemes = KoCssTextUtils::textToUnicodeGraphemeClusters(text, language);-- // Parse over the fonts and graphemes and try to see if we can get the- // best match for a given grapheme.- for (int i = 0; i < fontSet->nfont; i++) {-- double fontsize = 0.0;- FcBool isScalable = false;- FcPatternGetBool(fontSet->fonts[i], FC_SCALABLE, 0, &isScalable);- FcPatternGetDouble(fontSet->fonts[i], FC_PIXEL_SIZE, 0, &fontsize);- if (!isScalable && pixelSize != fontsize) {- // For some reason, FC will sometimes consider a smaller font pixel-size- // to be more relevant to the requested pattern than a bigger one. This- // skips those fonts, but it does mean that such pixel fonts would not- // be used for fallback.- continue;++ FcPatternSP p(FcPatternCreate());+ Q_FOREACH (const QString &family, families) {+ QByteArray utfData = family.toUtf8();+ const FcChar8 *vals = reinterpret_cast<FcChar8 *>(utfData.data());+ FcPatternAddString(p.data(), FC_FAMILY, vals);+ }++ {+ QByteArray fallbackBuf = QString("sans-serif").toUtf8();+ FcValue fallback;+ fallback.type = FcTypeString;+ fallback.u.s = reinterpret_cast<FcChar8 *>(fallbackBuf.data());+ FcPatternAddWeak(p.data(), FC_FAMILY, fallback, true);+ }++ for (int i = 0; i < candidates.size(); i++) {+ const QPair<QString, int> file = candidates.at(i);+ QByteArray utfData = file.first.toUtf8();+ const FcChar8 *vals = reinterpret_cast<FcChar8 *>(utfData.data());+ FcPatternAddString(p.data(), FC_FILE, vals);+ FcPatternAddInteger(p.data(), FC_INDEX, file.second);+ }++ if (slantMode == 1) {+ FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_ITALIC);+ } else if (slantMode == 2) {+ FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_OBLIQUE);+ } else {+ FcPatternAddInteger(p.data(), FC_SLANT, FC_SLANT_ROMAN);+ }+ FcPatternAddInteger(p.data(), FC_WEIGHT, FcWeightFromOpenType(weight));+ FcPatternAddInteger(p.data(), FC_WIDTH, width);++ double pixelSize = size*(qMin(xRes, yRes)/72.0);+ FcPatternAddDouble(p.data(), FC_PIXEL_SIZE, pixelSize);++ FcConfigSubstitute(nullptr, p.data(), FcMatchPattern);+ FcDefaultSubstitute(p.data());+++ p = [&]() {+ const FcChar32 hash = FcPatternHash(p.data());+ QString patternHash = families.join("+")+QString::number(hash);+ // FCPatternHash breaks down when there's mutliple family names that start+ // with the same letter and are the same length (Butcherman and Babylonica, Eater and Elsie, all 4 on google fonts).+ const auto oldPattern = d->patterns().find(patternHash);+ if (oldPattern != d->patterns().end()) {+ return oldPattern.value();+ } else {+ d->patterns().insert(patternHash, p);+ return p;}- if (FcPatternGetCharSet(fontSet->fonts[i], FC_CHARSET, 0, &set) == FcResultMatch) {- int index = 0;- Q_FOREACH (const QString &grapheme, graphemes) {-- // Don't worry about matching controls directly,- // as they are not important to font-selection (and many- // fonts have no glyph entry for these)- if (const uint first = firstCharUcs4(grapheme); QChar::category(first) == QChar::Other_Control- || QChar::category(first) == QChar::Other_Format) {- index += grapheme.size();- continue;- }- int familyIndex = -1;- if (familyValues.at(index) == -1) {- int fallbackMatch = fallbackMatchValues.at(index);- Q_FOREACH (uint unicode, grapheme.toUcs4()) {- if (FcCharSetHasChar(set, unicode)) {- familyIndex = i;- if (fallbackMatch < 0) {- fallbackMatch = i;+ }();++ FcResult result = FcResultNoMatch;+ FcCharSetSP charSet;+ FcFontSetSP fontSet = [&]() -> FcFontSetSP {+ const FcChar32 hash = FcPatternHash(p.data());+ QString patternHash = families.join("+")+QString::number(hash);+ const auto set = d->sets().find(patternHash);++ if (set != d->sets().end()) {+ return set.value();+ } else {+ FcCharSet *cs = nullptr;+ FcFontSetSP avalue(FcFontSort(FcConfigGetCurrent(), p.data(), FcFalse, &cs, &result));+ charSet.reset(cs);+ d->sets().insert(patternHash, avalue);+ return avalue;+ }+ }();++ if (text.isEmpty()) {+ for (int j = 0; j < fontSet->nfont; j++) {+ if (std::optional<FontEntry> font = FontEntry::get(fontSet->fonts[j])) {+ fonts.append(std::move(*font));+ lengths.append(0);+ break;+ }+ }+ } else {+ FcCharSet *set = nullptr;+ QVector<int> familyValues(text.size());+ QVector<int> fallbackMatchValues(text.size());+ familyValues.fill(-1);+ fallbackMatchValues.fill(-1);++ // First, we're going to split up the text into graphemes. This is both+ // because the css spec requires it, but also because of why the css+ // spec requires it: graphemes' parts should not end up in seperate+ // runs, which they will if they get assigned different fonts,+ // potentially breaking ligatures and emoji sequences.+ QStringList graphemes = KoCssTextUtils::textToUnicodeGraphemeClusters(text, language);++ // Parse over the fonts and graphemes and try to see if we can get the+ // best match for a given grapheme.+ for (int i = 0; i < fontSet->nfont; i++) {++ double fontsize = 0.0;+ FcBool isScalable = false;+ FcPatternGetBool(fontSet->fonts[i], FC_SCALABLE, 0, &isScalable);+ FcPatternGetDouble(fontSet->fonts[i], FC_PIXEL_SIZE, 0, &fontsize);+ if (!isScalable && pixelSize != fontsize) {+ // For some reason, FC will sometimes consider a smaller font pixel-size+ // to be more relevant to the requested pattern than a bigger one. This+ // skips those fonts, but it does mean that such pixel fonts would not+ // be used for fallback.+ continue;+ }+ if (FcPatternGetCharSet(fontSet->fonts[i], FC_CHARSET, 0, &set) == FcResultMatch) {+ int index = 0;+ Q_FOREACH (const QString &grapheme, graphemes) {++ // Don't worry about matching controls directly,+ // as they are not important to font-selection (and many+ // fonts have no glyph entry for these)+ if (const uint first = firstCharUcs4(grapheme); QChar::category(first) == QChar::Other_Control+ || QChar::category(first) == QChar::Other_Format) {+ index += grapheme.size();+ continue;+ }+ int familyIndex = -1;+ if (familyValues.at(index) == -1) {+ int fallbackMatch = fallbackMatchValues.at(index);+ Q_FOREACH (uint unicode, grapheme.toUcs4()) {+ if (FcCharSetHasChar(set, unicode)) {+ familyIndex = i;+ if (fallbackMatch < 0) {+ fallbackMatch = i;+ }+ } else {+ familyIndex = -1;+ break;}- } else {- familyIndex = -1;- break;+ }+ for (int k = 0; k < grapheme.size(); k++) {+ familyValues[index + k] = familyIndex;+ fallbackMatchValues[index + k] = fallbackMatch;}}- for (int k = 0; k < grapheme.size(); k++) {- familyValues[index + k] = familyIndex;- fallbackMatchValues[index + k] = fallbackMatch;- }+ index += grapheme.size();+ }+ if (!familyValues.contains(-1)) {+ break;}- index += grapheme.size();- }- if (!familyValues.contains(-1)) {- break;}}- }- // Remove the -1 entries.- if (familyValues.contains(-1)) {- int value = -1;- Q_FOREACH (const int currentValue, familyValues) {- if (currentValue != value) {- value = currentValue;- break;+ // Remove the -1 entries.+ if (familyValues.contains(-1)) {+ int value = -1;+ Q_FOREACH (const int currentValue, familyValues) {+ if (currentValue != value) {+ value = currentValue;+ break;+ }}- }- value = qMax(0, value);- for (int i = 0; i < familyValues.size(); i++) {- if (familyValues.at(i) < 0) {- if (fallbackMatchValues.at(i) < 0) {- familyValues[i] = value;+ value = qMax(0, value);+ for (int i = 0; i < familyValues.size(); i++) {+ if (familyValues.at(i) < 0) {+ if (fallbackMatchValues.at(i) < 0) {+ familyValues[i] = value;+ } else {+ familyValues[i] = fallbackMatchValues.at(i);+ }} else {- familyValues[i] = fallbackMatchValues.at(i);+ value = familyValues.at(i);}- } else {- value = familyValues.at(i);}}- }- // Get the filenames and lengths for the entries.- int length = 0;- int startIndex = 0;- int lastIndex = familyValues.at(0);- FontEntry font{};- if (std::optional<FontEntry> f = FontEntry::get(fontSet->fonts[lastIndex])) {- font = std::move(*f);- }- for (int i = 0; i < familyValues.size(); i++) {- if (lastIndex != familyValues.at(i)) {++ // Get the filenames and lengths for the entries.+ int length = 0;+ int startIndex = 0;+ int lastIndex = familyValues.at(0);+ FontEntry font{};+ if (std::optional<FontEntry> f = FontEntry::get(fontSet->fonts[lastIndex])) {+ font = std::move(*f);+ }+ for (int i = 0; i < familyValues.size(); i++) {+ if (lastIndex != familyValues.at(i)) {+ lengths.append(text.mid(startIndex, length).size());+ fonts.append(font);+ startIndex = i;+ length = 0;+ lastIndex = familyValues.at(i);+ if (std::optional<FontEntry> f = FontEntry::get(fontSet->fonts[lastIndex])) {+ font = std::move(*f);+ }+ }+ length += 1;+ }+ if (length > 0) {lengths.append(text.mid(startIndex, length).size());fonts.append(font);- startIndex = i;- length = 0;- lastIndex = familyValues.at(i);- if (std::optional<FontEntry> f = FontEntry::get(fontSet->fonts[lastIndex])) {- font = std::move(*f);- }}- length += 1;- }- if (length > 0) {- lengths.append(text.mid(startIndex, length).size());- fonts.append(font);}}diff --git a/libs/flake/text/KoFontRegistry.h b/libs/flake/text/KoFontRegistry.hindex 9386863b3c..d1590a472b 100644--- a/libs/flake/text/KoFontRegistry.h+++ b/libs/flake/text/KoFontRegistry.h@@ -57,6 +57,7 @@ public:int width = 100,int slantMode = false,int slantValue = 0,+ bool disableFontMatching = false,const QString &language = QString());/**diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 3b0e9f0299..e2ccd82609 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1379,6 +1379,26 @@ void KoSvgTextShape::debugParsing()}}+void KoSvgTextShape::setRelayoutBlocked(const bool disable)+{+ d->isLoading = disable;+}++bool KoSvgTextShape::relayoutIsBlocked() const+{+ return d->isLoading;+}++void KoSvgTextShape::setFontMatchingDisabled(const bool disable)+{+ d->disableFontMatching = disable;+}++bool KoSvgTextShape::fontMatchingDisabled() const+{+ return d->disableFontMatching;+}+void KoSvgTextShape::paint(QPainter &painter) const{painter.save();diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 28499b7c1f..d69eead940 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -435,6 +435,37 @@ public:/// Outputs debug with the current textData tree.void debugParsing();+ /***+ * This blocks the shape from automatically calling relayout+ * when the text or properties change. Relayout needs to be called+ * in this mode.+ * Used in the SVGTextLabel.+ */+ void setRelayoutBlocked(const bool disable);++ /**+ * @brief relayoutIsBlocked+ * @return whether automatic relayout is blocked,+ * as are updates to shape listeners.+ */+ bool relayoutIsBlocked() const;++ /**+ * @brief setDisableFontMatching+ * @param disable font matching when retrieving fonts+ * for text layout (if possible).+ * This speeds up text layout, but should only be done+ * if there's only one font necessary and it can be+ * found with the KoFFWWSconverter.+ */+ void setFontMatchingDisabled(const bool disable);++ /**+ * @brief fontMatchingDisabled+ * @return whether font matching is disabled for this shape.+ */+ bool fontMatchingDisabled() const;+protected:void shapeChanged(ChangeType type, KoShape *shape) override;diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex fa849d2d08..9e6e8b655f 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -328,6 +328,9 @@ public:plainText = rhs.plainText;isBidi = rhs.isBidi;initialTextPosition = rhs.initialTextPosition;++ isLoading = rhs.isLoading;+ disableFontMatching = rhs.disableFontMatching;}TextRendering textRendering = Auto;@@ -339,6 +342,7 @@ public:KisForest<KoSvgTextContentElement> textData;bool isLoading = false; ///< Turned on when loading in text data, blocks updates to shape listeners.+ bool disableFontMatching = false; ///< Turn off font matching, which should speed up relayout slightly.QVector<CharacterResult> result;QVector<LineBox> lineBoxes;@@ -388,7 +392,8 @@ public:QVector<CharacterResult> &result,int ¤tIndex,qreal res,- bool isHorizontal);+ bool isHorizontal,+ bool disableFontMatching);void handleLineBoxAlignment(KisForest<KoSvgTextContentElement>::child_iterator parent,QVector<CharacterResult> &result, QVector<LineBox> lineBoxes,int ¤tIndex,diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex e10558b1e1..b9e9973449 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -101,6 +101,8 @@ void KoSvgTextShape::Private::relayout()this->cursorPos.clear();this->logicalToVisualCursorPos.clear();+ bool disableFontMatching = this->disableFontMatching;+if (KisForestDetail::size(textData) == 0) {return;}@@ -407,7 +409,8 @@ void KoSvgTextShape::Private::relayout()fontSizeAdjust.isAuto ? 1.0 : fontSizeAdjust.customValue,properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),properties.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),- style.style, style.slantValue.isAuto? 14: style.slantValue.customValue);+ style.style, style.slantValue.isAuto? 14: style.slantValue.customValue,+ disableFontMatching);if (properties.hasProperty(KoSvgTextProperties::TextLanguage)) {raqm_set_language(layout.data(),properties.property(KoSvgTextProperties::TextLanguage).toString().toUtf8(),@@ -721,7 +724,7 @@ void KoSvgTextShape::Private::relayout()// Compute baseline alignment.globalIndex = 0;- this->computeFontMetrics(textData.childBegin(), KoSvgTextProperties::defaultProperties(), QMap<int, int>(), 0, QPointF(), QPointF(), result, globalIndex, finalRes, isHorizontal);+ this->computeFontMetrics(textData.childBegin(), KoSvgTextProperties::defaultProperties(), QMap<int, int>(), 0, QPointF(), QPointF(), result, globalIndex, finalRes, isHorizontal, disableFontMatching);// Handle linebreaking.QPointF startPos = resolvedTransforms[0].absolutePos();@@ -1137,7 +1140,8 @@ void KoSvgTextShape::Private::computeFontMetrics( // NOLINT(readability-functionQVector<CharacterResult> &result,int ¤tIndex,qreal res,- bool isHorizontal)+ bool isHorizontal,+ bool disableFontMatching){QMap<int, int> baselineTable;@@ -1178,7 +1182,7 @@ void KoSvgTextShape::Private::computeFontMetrics( // NOLINT(readability-functionfontSizeAdjust.isAuto ? 1.0 : fontSizeAdjust.customValue,properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),properties.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),- style.style, style.slantValue.isAuto? 14: style.slantValue.customValue);+ style.style, style.slantValue.isAuto? 14: style.slantValue.customValue, disableFontMatching);hb_font_t_sp font(hb_ft_font_create_referenced(faces.front().data()));const qreal freetypePixelsToPt = (1.0 / 64.0) * (72. / res);@@ -1346,7 +1350,7 @@ void KoSvgTextShape::Private::computeFontMetrics( // NOLINT(readability-function}for (auto child = KisForestDetail::childBegin(parent); child != KisForestDetail::childEnd(parent); child++) {- computeFontMetrics(child, properties, baselineTable, fontSize, newSuperScript, newSubScript, result, currentIndex, res, isHorizontal);+ computeFontMetrics(child, properties, baselineTable, fontSize, newSuperScript, newSubScript, result, currentIndex, res, isHorizontal, disableFontMatching);}KoSvgText::Baseline baselineAdjust = KoSvgText::Baseline(properties.property(KoSvgTextProperties::AlignmentBaselineId).toInt());diff --git a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cppindex aa0019a894..52ea694eaa 100644--- a/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/SvgTextLabel.cpp@@ -19,6 +19,8 @@ struct Q_DECL_HIDDEN SvgTextLabel::Private {: shape(new KoSvgTextShape()){shape->setResolution(72, 72);+ shape->setRelayoutBlocked(true);+ shape->setFontMatchingDisabled(true);}~Private() {}@@ -31,6 +33,7 @@ struct Q_DECL_HIDDEN SvgTextLabel::Private {QScopedPointer<KoShapePainter> shapePainter;KoSvgTextProperties props;+ bool textHasChanged = false;int padding = 2;};@@ -290,6 +293,7 @@ void SvgTextLabel::componentComplete()d->shapePainter.reset(new KoShapePainter());d->shapePainter->setShapes({d->shape.data()});}+ //d->shape->setRelayoutBlocked(false);}