Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Fix the glyph palette model for DejaVu Sans

Wolthera van Hövell tot Westerflierf6a483e279ee
diff --git a/libs/flake/text/KoFontGlyphModel.cpp b/libs/flake/text/KoFontGlyphModel.cpp
index 3c078c3dd0..bb934f0d79 100644
--- a/libs/flake/text/KoFontGlyphModel.cpp
+++ b/libs/flake/text/KoFontGlyphModel.cpp
@@ -35,6 +35,14 @@ struct KoFontGlyphModel::Private {
int childCount() override {
return 0;
}
+
+ bool compare(const GlyphInfo &other) {
+ return type == other.type
+ && ucs == other.ucs
+ && parentUcs == other.parentUcs
+ && baseString == other.baseString
+ && featureIndex == other.featureIndex;
+ }
};
struct CodePointInfo
@@ -48,6 +56,18 @@ struct KoFontGlyphModel::Private {
int childCount() override {
return glyphs.size();
}
+
+ bool addToGlyphsIfNotAlready(GlyphInfo glyph) {
+ bool addToGlyphs = true;
+ for (auto g = glyphs.begin(); g != glyphs.end(); g++) {
+ addToGlyphs = !g->compare(glyph);
+ if (!addToGlyphs) break;
+ }
+ if (addToGlyphs) {
+ glyphs.append(glyph);
+ }
+ return addToGlyphs;
+ }
};
QVector<CodePointInfo> codePoints;
QMap<QString, KoOpenTypeFeatureInfo> featureData;
@@ -107,8 +127,7 @@ struct KoFontGlyphModel::Private {
QMap<QString, KoOpenTypeFeatureInfo> featureInfo = previousFeatureInfo;
hb_face_t_sp hbFace(hb_ft_face_create_referenced(face.data()));
hb_tag_t table = gpos? HB_OT_TAG_GPOS: HB_OT_TAG_GSUB;
- uint targetLanguage = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
- uint targetScript = 0;
+ uint targetLanguageIndex = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
QVector<hb_language_t> localeTags;
Q_FOREACH(const QString locale, locales) {
@@ -119,38 +138,36 @@ struct KoFontGlyphModel::Private {
hb_language_t languageTag = lang.isEmpty()? HB_LANGUAGE_INVALID: hb_language_from_string(lang.data(), lang.size());
uint scriptCount = hb_ot_layout_table_get_script_tags(hbFace.data(), table, 0, nullptr, nullptr);
QVector<hb_tag_t> tags;
+ QVector<uint> scriptIndices;
for (uint script = 0; script < scriptCount; script++) {
- uint languageCount = hb_ot_layout_script_get_language_tags(hbFace.data(), table, script, 0, nullptr, nullptr);
-
- for(uint j = 0; j < languageCount; j++) {
- hb_tag_t langTag;
- uint count = 1;
- hb_ot_layout_script_get_language_tags(hbFace.data(), table, script, j, &count, &langTag);
- if (count < 1) continue;
- if (hb_ot_tag_to_language(langTag) == languageTag) {
- targetLanguage = j;
- targetScript = script;
+ uint scriptCount = 1;
+ hb_tag_t scriptTag;
+ uint scriptIndex;
+ hb_ot_layout_table_get_script_tags(hbFace.data(), table, script, &scriptCount, &scriptTag);
+ if (!hb_ot_layout_table_select_script(hbFace.data(), table, 1, &scriptTag, &scriptIndex, &scriptTag)) {
+ continue;
+ }
+ scriptIndices.append(scriptIndex);
+
+ uint languageCount = hb_ot_layout_script_get_language_tags(hbFace.data(), table, scriptIndex, 0, nullptr, nullptr);
+
+ bool foundLanguage = false;
+
+ for(uint j = 0; j < languageCount; j++) {
+ hb_tag_t langTag;
+ uint count = 1;
+ hb_ot_layout_script_get_language_tags(hbFace.data(), table, scriptIndex, j, &count, &langTag);
+ if (count < 1) continue;
+ if (hb_ot_tag_to_language(langTag) == languageTag) {
+ uint languageIndex;
+ if (hb_ot_layout_script_select_language(hbFace.data(), table, scriptIndex, count, &langTag, &languageIndex)) {
+ targetLanguageIndex = languageIndex;
+ foundLanguage = true;
break;
}
}
- }
-
- uint featureCount = hb_ot_layout_language_get_feature_tags(hbFace.data(),
- table,
- targetScript,
- targetLanguage,
- 0, nullptr, nullptr);
- hb_ot_layout_language_get_feature_tags(hbFace.data(), table, targetScript,
- targetLanguage,
- 0, nullptr, nullptr);
- for(uint k = 0; k < featureCount; k++) {
- uint count = 1;
- hb_tag_t features;
- hb_ot_layout_language_get_feature_tags(hbFace.data(), table, targetScript,
- targetLanguage,
- k, &count, &features);
- if (count < 1) continue;
- tags.append(features);
+ }
+ if (foundLanguage) break;
}
QHash<quint32, int> glyphToCodepoint;
@@ -158,164 +175,194 @@ struct KoFontGlyphModel::Private {
glyphToCodepoint.insert(charMap.at(i).glyphIndex, i);
}
- KoOpenTypeFeatureInfoFactory factory;
- for (auto it = tags.begin(); it != tags.end(); it++) {
- char c[4];
- hb_tag_to_string(*it, c);
- const QLatin1String tagName(c, 4);
- uint featureIndex;
+ for (auto scriptIt = scriptIndices.begin(); scriptIt != scriptIndices.end(); scriptIt++) {
+ uint targetScriptIndex = *scriptIt;
+ uint featureCount = hb_ot_layout_language_get_feature_tags(hbFace.data(),
+ table,
+ targetScriptIndex,
+ targetLanguageIndex,
+ 0, nullptr, nullptr);
+ hb_ot_layout_language_get_feature_tags(hbFace.data(), table, targetScriptIndex,
+ targetLanguageIndex,
+ 0, nullptr, nullptr);
+ for(uint k = 0; k < featureCount; k++) {
+ uint count = 1;
+ hb_tag_t features;
+ hb_ot_layout_language_get_feature_tags(hbFace.data(), table, targetScriptIndex,
+ targetLanguageIndex,
+ k, &count, &features);
+ if (count < 1) continue;
+ tags.append(features);
+ }
+
+
+ KoOpenTypeFeatureInfoFactory factory;
+ QVector<uint> featureIndicesProcessed;
QVector<uint> lookUpsProcessed;
- bool found = hb_ot_layout_language_find_feature (hbFace.data(), table,
- targetScript,
- targetLanguage,
- *it,
- &featureIndex );
+ for (auto tagIt = tags.begin(); tagIt != tags.end(); tagIt++) {
+ char c[4];
+ hb_tag_to_string(*tagIt, c);
+ const QLatin1String tagName(c, 4);
+ uint featureIndex;
- if (!found) {
- continue;
- }
- hb_ot_name_id_t labelId = HB_OT_NAME_ID_INVALID;
- hb_ot_name_id_t toolTipId = HB_OT_NAME_ID_INVALID;
- hb_ot_name_id_t sampleId = HB_OT_NAME_ID_INVALID;
- uint namedParameters;
- hb_ot_name_id_t firstParamId = HB_OT_NAME_ID_INVALID;
- KoOpenTypeFeatureInfo info = factory.infoByTag(tagName);
- if (hb_ot_layout_feature_get_name_ids(hbFace.data(), table, featureIndex, &labelId, &toolTipId, &sampleId, &namedParameters, &firstParamId)) {
- QVector<hb_ot_name_id_t> nameIds = {labelId, toolTipId, sampleId};
- if (firstParamId != HB_OT_NAME_ID_INVALID) {
- for (uint i = 0; i < namedParameters; i++) {
- nameIds += firstParamId + i;
- }
- }
+ bool found = hb_ot_layout_language_find_feature (hbFace.data(), table,
+ targetScriptIndex,
+ targetLanguageIndex,
+ *tagIt,
+ &featureIndex );
- for(auto nameId = nameIds.begin(); nameId != nameIds.end(); nameId++) {
- if (*nameId == HB_OT_NAME_ID_INVALID) {
- continue;
- }
- QVector<hb_language_t> testLang;
- uint length = 0;
- if (*nameId == sampleId) {
- testLang.append(languageTag);
- } else {
- testLang = localeTags;
- }
- testLang.append(HB_LANGUAGE_INVALID);
- for (auto tag = testLang.begin(); tag != testLang.end(); tag++) {
- length = hb_ot_name_get_utf8(hbFace.data(), *nameId, *tag, nullptr, nullptr);
- if (length > 0) {
- length+=1;
- break;
+ if (!found || featureIndicesProcessed.contains(featureIndex)) {
+ continue;
+ }
+ featureIndicesProcessed.append(featureIndex);
+
+ KoOpenTypeFeatureInfo info = featureInfo.value(tagName, factory.infoByTag(tagName));
+ if (!featureInfo.contains(tagName)) {
+ hb_ot_name_id_t labelId = HB_OT_NAME_ID_INVALID;
+ hb_ot_name_id_t toolTipId = HB_OT_NAME_ID_INVALID;
+ hb_ot_name_id_t sampleId = HB_OT_NAME_ID_INVALID;
+ uint namedParameters;
+ hb_ot_name_id_t firstParamId = HB_OT_NAME_ID_INVALID;
+
+ if (hb_ot_layout_feature_get_name_ids(hbFace.data(), table, featureIndex, &labelId, &toolTipId, &sampleId, &namedParameters, &firstParamId)) {
+ QVector<hb_ot_name_id_t> nameIds = {labelId, toolTipId, sampleId};
+ if (firstParamId != HB_OT_NAME_ID_INVALID) {
+ for (uint i = 0; i < namedParameters; i++) {
+ nameIds += firstParamId + i;
+ }
}
- }
- std::vector<char> buff(length);
- hb_ot_name_get_utf8(hbFace.data(), *nameId, languageTag, &length, buff.data());
- if (length > 0) {
- const QString nameString = QString::fromUtf8(buff.data(), length);
- if (*nameId == labelId) {
- info.name = nameString;
- } else if (*nameId == toolTipId) {
- info.description = nameString;
- } else if (*nameId == sampleId) {
- info.sample = nameString;
- } else {
- info.namedParameters.append(nameString);
+ for(auto nameId = nameIds.begin(); nameId != nameIds.end(); nameId++) {
+ if (*nameId == HB_OT_NAME_ID_INVALID) {
+ continue;
+ }
+ QVector<hb_language_t> testLang;
+ uint length = 0;
+ if (*nameId == sampleId) {
+ testLang.append(languageTag);
+ } else {
+ testLang = localeTags;
+ }
+ testLang.append(HB_LANGUAGE_INVALID);
+ for (auto tag = testLang.begin(); tag != testLang.end(); tag++) {
+ length = hb_ot_name_get_utf8(hbFace.data(), *nameId, *tag, nullptr, nullptr);
+ if (length > 0) {
+ length+=1;
+ break;
+ }
+ }
+
+ std::vector<char> buff(length);
+ hb_ot_name_get_utf8(hbFace.data(), *nameId, languageTag, &length, buff.data());
+ if (length > 0) {
+ const QString nameString = QString::fromUtf8(buff.data(), length);
+ if (*nameId == labelId) {
+ info.name = nameString;
+ } else if (*nameId == toolTipId) {
+ info.description = nameString;
+ } else if (*nameId == sampleId) {
+ info.sample = nameString;
+ } else {
+ info.namedParameters.append(nameString);
+ }
+ }
}
}
- }
- }
-
- featureInfo.insert(tagName, info);
- if (!info.glyphPalette || (samplesOnly && !info.sample.isEmpty())) {
- continue;
- }
- QStringList samples;
- int lookupCount = hb_ot_layout_feature_get_lookups (hbFace.data(), table,
- featureIndex,
- 0,
- nullptr,
- nullptr );
- for (int i = 0; i < lookupCount; ++i) {
- uint maxCount = 1;
- uint lookUpIndex = 0;
- hb_ot_layout_feature_get_lookups (hbFace.data(), table,
- featureIndex,
- i,
- &maxCount,
- &lookUpIndex );
- if (maxCount < 1 || lookUpsProcessed.contains(lookUpIndex)) {
+ featureInfo.insert(tagName, info);
+ }
+ if (!info.glyphPalette || (samplesOnly && !info.sample.isEmpty())) {
continue;
}
- // https://github.com/harfbuzz/harfbuzz/issues/673 suggest against checking the lookups,
- // but if we don't know the input glyphs, initialization can get really slow.
- // Given this is run only when the model is created, this should be fine for now.
-
- hb_set_t_sp glyphsBefore (hb_set_create());
- hb_set_t_sp glyphsInput (hb_set_create());
- hb_set_t_sp glyphsAfter (hb_set_create());
- hb_set_t_sp glyphsOutput (hb_set_create());
-
- hb_ot_layout_lookup_collect_glyphs (hbFace.data(), table,
- lookUpIndex,
- glyphsBefore.data(),
- glyphsInput.data(),
- glyphsAfter.data(),
- glyphsOutput.data() );
-
- GlyphInfo gci;
- gci.type = OpenType;
- gci.baseString = tagName;
-
- hb_codepoint_t currentGlyph = HB_SET_VALUE_INVALID;
- while(hb_set_next(glyphsInput.data(), &currentGlyph)) {
- if (!glyphToCodepoint.contains(currentGlyph)) continue;
- const int codePointLocation = glyphToCodepoint.value(currentGlyph);
- CodePointInfo codePointInfo = charMap.at(codePointLocation);
- gci.ucs = codePointInfo.ucs;
- bool addSample = false;
-
- uint alt_count = hb_ot_layout_lookup_get_glyph_alternates (hbFace.data(),
- lookUpIndex, currentGlyph,
- 0,
- nullptr, nullptr);
-
- if (alt_count > 0) {
- // 0 is the default value.
- for(uint j = 1; j < alt_count; ++j) {
- gci.featureIndex = j;
- codePointInfo.glyphs.append(gci);
- }
- info.maxValue = qMax(int(alt_count), info.maxValue);
- addSample = true;
- } else {
- gci.featureIndex = 1;
- codePointInfo.glyphs.append(gci);
- addSample = true;
+ QStringList samples;
+ int lookupCount = hb_ot_layout_feature_get_lookups (hbFace.data(), table,
+ featureIndex,
+ 0,
+ nullptr,
+ nullptr );
+ for (int i = 0; i < lookupCount; ++i) {
+ uint maxCount = 1;
+ uint lookUpIndex = 0;
+ hb_ot_layout_feature_get_lookups (hbFace.data(), table,
+ featureIndex,
+ i,
+ &maxCount,
+ &lookUpIndex );
+ if (maxCount < 1 || lookUpsProcessed.contains(lookUpIndex)) {
+ continue;
}
- charMap[codePointLocation] = codePointInfo;
- if (samples.size() < 6 && addSample) {
- samples.append(QString::fromUcs4(&gci.ucs, 1));
+
+ // https://github.com/harfbuzz/harfbuzz/issues/673 suggest against checking the lookups,
+ // but if we don't know the input glyphs, initialization can get really slow.
+ // Given this is run only when the model is created, this should be fine for now.
+
+ hb_set_t_sp glyphsBefore (hb_set_create());
+ hb_set_t_sp glyphsInput (hb_set_create());
+ hb_set_t_sp glyphsAfter (hb_set_create());
+ hb_set_t_sp glyphsOutput (hb_set_create());
+
+ hb_ot_layout_lookup_collect_glyphs (hbFace.data(), table,
+ lookUpIndex,
+ glyphsBefore.data(),
+ glyphsInput.data(),
+ glyphsAfter.data(),
+ glyphsOutput.data() );
+
+ GlyphInfo gci;
+ gci.type = OpenType;
+ gci.baseString = tagName;
+
+ hb_codepoint_t currentGlyph = HB_SET_VALUE_INVALID;
+ while(hb_set_next(glyphsInput.data(), &currentGlyph)) {
+ if (!glyphToCodepoint.contains(currentGlyph)) continue;
+ const int codePointLocation = glyphToCodepoint.value(currentGlyph);
+ CodePointInfo codePointInfo = charMap.at(codePointLocation);
+ gci.ucs = codePointInfo.ucs;
+ bool addSample = false;
+
+ uint alt_count = hb_ot_layout_lookup_get_glyph_alternates (hbFace.data(),
+ lookUpIndex, currentGlyph,
+ 0,
+ nullptr, nullptr);
+
+ if (alt_count > 0) {
+ // 0 is the default value.
+ for(uint j = 1; j < alt_count; ++j) {
+ gci.featureIndex = j;
+
+ bool addToGlyphs = codePointInfo.addToGlyphsIfNotAlready(gci);
+ if (addToGlyphs && !addSample) {
+ addSample = true;
+ }
+ }
+ info.maxValue = qMax(int(alt_count), info.maxValue);
+ } else {
+ gci.featureIndex = 1;
+ addSample = codePointInfo.addToGlyphsIfNotAlready(gci);
+ }
+ charMap[codePointLocation] = codePointInfo;
+ if (samples.size() < 6 && addSample) {
+ samples.append(QString::fromUcs4(&gci.ucs, 1));
+ }
+ if (samples.size() >= 6 && samplesOnly) {
+ break;
+ }
}
- if (samples.size() >= 6 && samplesOnly) {
- break;
+
+ lookUpsProcessed.append(lookUpIndex);
+ if (info.sample.isEmpty() && !samples.isEmpty()) {
+ info.sample = samples.join(" ");
}
- }
+ featureInfo.insert(tagName, info);
- lookUpsProcessed.append(lookUpIndex);
- if (info.sample.isEmpty() && !samples.isEmpty()) {
- info.sample = samples.join(" ");
}
- featureInfo.insert(tagName, info);
}
-
}
-
return featureInfo;
}
};
@@ -521,6 +568,7 @@ void KoFontGlyphModel::setFace(FT_FaceSP face, QLatin1String language, bool samp
}
}
std::sort(d->blocks.begin(), d->blocks.end(), sortBlocks);
+
d->featureData = Private::getOpenTypeTables(face, d->codePoints, QMap<QString, KoOpenTypeFeatureInfo>() , false, samplesOnly, KLocalizedString::languages(), language);
d->featureData = Private::getOpenTypeTables(face, d->codePoints, d->featureData, true, samplesOnly, KLocalizedString::languages(), language);