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 various issues related to fallback from oblique to italic and otherwise.
Wolthera van Hövell tot Westerfliera8eea6720039
diff --git a/libs/flake/text/KoCssTextUtils.cpp b/libs/flake/text/KoCssTextUtils.cppindex c9833df48c..da66da27a2 100644--- a/libs/flake/text/KoCssTextUtils.cpp+++ b/libs/flake/text/KoCssTextUtils.cpp@@ -573,3 +573,40 @@ void KoCssTextUtils::removeText(QString &text, int &start, int length)}text.remove(start, end-start);}++qreal KoCssTextUtils::cssSelectFontStyleValue(const QVector<qreal> &values, const qreal &targetValue, const qreal &defaultValue, const qreal &defaultValueUpper, const bool &shouldNotReturnDefault)+{+ // follow the CSS Fonts selection mechanism.+ // See https://drafts.csswg.org/css-fonts-4/#font-style-matching+ QVector<qreal> sortedValues = values;+ std::sort(sortedValues.begin(), sortedValues.end());+ qreal selectedValue = defaultValue;+ auto upper = std::lower_bound(sortedValues.begin(), sortedValues.end(), targetValue);+ if (upper == sortedValues.end()) {+ upper--;+ }+ auto lower = upper;+ if (lower != sortedValues.begin() && *lower > targetValue) {+ lower--;+ }++ // ... Which wants to select the lower possible selection when the value is below the default.+ if (targetValue < defaultValue) {+ selectedValue = *lower;+ // ... the higher closest value when the value is higher than the default (upper bound)+ } else if (targetValue > defaultValueUpper) {+ selectedValue = *upper;+ } else {+ // ... and if the value is between the lower and upper default bounds, first higher (within bounds)+ // then lower, then higher.+ if (*upper <= defaultValueUpper && *lower != targetValue) {+ selectedValue = *upper;+ } else {+ selectedValue = *lower;+ }+ }+ if (qFuzzyCompare(selectedValue, defaultValue) && shouldNotReturnDefault) {+ return targetValue;+ }+ return selectedValue;+}diff --git a/libs/flake/text/KoCssTextUtils.h b/libs/flake/text/KoCssTextUtils.hindex 912af91cd5..0816e6e8aa 100644--- a/libs/flake/text/KoCssTextUtils.h+++ b/libs/flake/text/KoCssTextUtils.h@@ -208,6 +208,26 @@ public:* @param length the length.*/static void removeText(QString &text, int &start, int length);++ /**+ * @brief cssSelectFontStyleValue+ * Select the closest font style value from the list,+ * following the CSS Fonts selection algorithm.+ * Note that for slant, this needs to be inverted.+ * @param values -- values list to search in.+ * @param targetValue -- the target value to search for.+ * @param defaultValue -- the (lower-end) default value for the given axis.+ * @param defaultValueUpper -- the upper-end of the default value,+ * for example, for weight this is 100 larger than default,+ * for the others it is exactly the same.+ * @param shouldNotReturnDefault -- used for the slants, as they need to fall back on one another.+ * @return closest value on this list.+ */+ static qreal cssSelectFontStyleValue(const QVector<qreal> &values,+ const qreal &targetValue,+ const qreal &defaultValue,+ const qreal &defaultValueUpper,+ const bool &shouldNotReturnDefault);};#endif // KOCSSTEXTUTILS_Hdiff --git a/libs/flake/text/KoFFWWSConverter.cpp b/libs/flake/text/KoFFWWSConverter.cppindex cd73d1faa0..ef76810419 100644--- a/libs/flake/text/KoFFWWSConverter.cpp+++ b/libs/flake/text/KoFFWWSConverter.cpp@@ -950,7 +950,7 @@ QString KoFFWWSConverter::wwsNameByFamilyName(const QString familyName, bool *fo}return name;}-+#include <KoCssTextUtils.h>QVector<FontFamilyNode> findNodesByAxis(const QVector<FontFamilyNode> &nodes, const QString axisTag, const qreal &value, const qreal &defaultValue, const qreal &defaultValueUpper) {QVector<FontFamilyNode> candidates;QVector<qreal> values;@@ -979,32 +979,8 @@ QVector<FontFamilyNode> findNodesByAxis(const QVector<FontFamilyNode> &nodes, co}// follow the CSS Fonts selection mechanism.- std::sort(values.begin(), values.end());- qreal selectedValue = defaultValue;- auto upper = std::lower_bound(values.begin(), values.end(), value);- if (upper == values.end()) {- upper--;- }- auto lower = upper;- if (lower != values.begin() && *lower > value) {- lower--;- }-- // ... Which wants to select the lower possible selection when the value is below the default.- if (value < defaultValue) {- selectedValue = *lower;- // ... the higher closest value when the value is higher than the default (upper bound)- } else if (value > defaultValueUpper) {- selectedValue = *upper;- } else {- // ... and if the value is between the lower and upper default bounds, first higher (within bounds)- // then lower, then higher.- if (*upper <= defaultValueUpper && *lower != value) {- selectedValue = *upper;- } else {- selectedValue = *lower;- }- }+ bool shouldNotReturnDefault = ((axisTag == ITALIC_TAG || axisTag == SLANT_TAG) && value != defaultValue);+ qreal selectedValue = KoCssTextUtils::cssSelectFontStyleValue(values, value, defaultValue, defaultValueUpper, shouldNotReturnDefault);Q_FOREACH (const FontFamilyNode &node, nodes) {if (node.axes.keys().contains(axisTag)) {@@ -1012,7 +988,7 @@ QVector<FontFamilyNode> findNodesByAxis(const QVector<FontFamilyNode> &nodes, coif (axis.value == selectedValue) {candidates.append(node);}- } else if (value == defaultValue) {+ } else if (value == defaultValue && !shouldNotReturnDefault) {candidates.append(node);}}diff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cppindex ff50d81a79..0d805dd231 100644--- a/libs/flake/text/KoFontRegistry.cpp+++ b/libs/flake/text/KoFontRegistry.cpp@@ -607,6 +607,9 @@ bool KoFontRegistry::configureFaces(const std::vector<FT_FaceSP> &faces,tags.insert(FT_MAKE_TAG(tag[0], tag[1], tag[2], tag[3]), axisSettings.value(tagName));}}+ const FT_Tag ITALIC_TAG = FT_MAKE_TAG('i', 't', 'a', 'l');+ const FT_Tag SLANT_TAG = FT_MAKE_TAG('s', 'l', 'n', 't');+ const int axisMultiplier = 65535;if (FT_HAS_MULTIPLE_MASTERS(face)) {FT_MM_Var *amaster = nullptr;FT_Get_MM_Var(face.data(), &amaster);@@ -616,10 +619,16 @@ bool KoFontRegistry::configureFaces(const std::vector<FT_FaceSP> &faces,for (FT_UInt i = 0; i < amaster->num_axis; i++) {FT_Var_Axis axis = amaster->axis[i];designCoords[i] = axis.def;- Q_FOREACH (FT_Tag tag, tags.keys()) {- if (axis.tag == tag) {- designCoords[i] = qBound(axis.minimum, long(tags.value(tag) * 65535), axis.maximum);- }+ if (tags.contains(axis.tag)) {+ designCoords[i] = qBound(axis.minimum, long(tags.value(axis.tag) * axisMultiplier), axis.maximum);+ } else if (axis.tag == SLANT_TAG+ && !tags.contains(SLANT_TAG)+ && tags.value(ITALIC_TAG, 0) > 0) {+ designCoords[i] = qBound(axis.minimum, long(-11.0 * axisMultiplier), axis.maximum);+ } else if (axis.tag == ITALIC_TAG+ && !tags.contains(ITALIC_TAG)+ && !qFuzzyCompare(tags.value(SLANT_TAG, 0), 0)) {+ designCoords[i] = qBound(axis.minimum, long(1.0 * axisMultiplier), axis.maximum);}}FT_Set_Var_Design_Coordinates(face.data(), amaster->num_axis, designCoords.data());diff --git a/plugins/dockers/textproperties/FontStyleModel.cpp b/plugins/dockers/textproperties/FontStyleModel.cppindex a7bc0a6eb3..11b0972eb3 100644--- a/plugins/dockers/textproperties/FontStyleModel.cpp+++ b/plugins/dockers/textproperties/FontStyleModel.cpp@@ -86,52 +86,92 @@ QVariantHash FontStyleModel::axesValues(int row)return data(index(row, 0), AxisValues).toHash();}-// TODO: consider somehow using the exact same method as in KoFFWWSConverter-QHash<int, KoSvgText::FontFamilyStyleInfo> searchAxisTag(QString tag, qreal value, QVector<qreal> values, qreal defaultVal, QHash<int, KoSvgText::FontFamilyStyleInfo> styles) {+#include <KoCssTextUtils.h>+QHash<int, KoSvgText::FontFamilyStyleInfo> searchAxisTag(const QString &tag,+ const qreal &value,+ const QVector<qreal> &values,+ const qreal &defaultVal,+ const qreal &defaultValUpper,+ const QHash<int, KoSvgText::FontFamilyStyleInfo> &styles) {QHash<int, KoSvgText::FontFamilyStyleInfo> filteredStyles;- auto valIt = std::lower_bound(values.begin(), values.end(), value);- if (valIt == values.end()) valIt--;- qreal selectedValue = *valIt;- if (valIt != values.begin()) valIt--;- if (qAbs(*valIt - value) < qAbs(selectedValue - value)) {- selectedValue = *valIt;- }+ bool shouldNotReturnDefault = ((tag == ITALIC_TAG || tag == SLANT_TAG) && value != defaultVal);+ qreal selectedValue = KoCssTextUtils::cssSelectFontStyleValue(values, value, defaultVal, defaultValUpper, shouldNotReturnDefault);for (auto it = styles.begin(); it!= styles.end(); it++) {- if (it.value().instanceCoords.value(tag, defaultVal) == selectedValue) {+ qreal val = it.value().instanceCoords.value(tag, defaultVal);+ if (val == selectedValue) {+ if (shouldNotReturnDefault && val == defaultVal) continue;filteredStyles.insert(it.key(), it.value());}}return filteredStyles;}-int FontStyleModel::rowForStyle(qreal weight, qreal width, int styleMode)+int FontStyleModel::rowForStyle(const qreal &weight, const qreal &width, const int &styleMode, const qreal &styleValue){QHash<int, KoSvgText::FontFamilyStyleInfo> styles;QVector<qreal> weights;QVector<qreal> widths;+ QVector<qreal> slants;+ QVector<qreal> italics;for (int i = 0; i< d->styles.size(); i++) {styles.insert(i, d->styles.at(i));weights.append(d->styles.value(i).instanceCoords.value(WEIGHT_TAG, 400.0));widths.append(d->styles.value(i).instanceCoords.value(WIDTH_TAG, 100.0));+ int fontStyle = d->styles.value(i).isItalic? d->styles.value(i).isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;+ if (d->styles.value(i).instanceCoords.contains(SLANT_TAG) || fontStyle == QFont::StyleOblique) {+ slants.append(d->styles.value(i).instanceCoords.value(SLANT_TAG, -11.0));+ }+ if (d->styles.value(i).instanceCoords.contains(ITALIC_TAG) || fontStyle == QFont::StyleItalic) {+ italics.append(d->styles.value(i).instanceCoords.value(ITALIC_TAG, 1.0));+ }}- std::sort(weights.begin(), weights.end());- std::sort(widths.begin(), widths.end());if (styles.size() > 1) {- styles = searchAxisTag(WEIGHT_TAG, weight, weights, 400.0, styles);+ styles = searchAxisTag(WIDTH_TAG, width, widths, 100.0, 100.0, styles);}if (styles.size() > 1) {- styles = searchAxisTag(WIDTH_TAG, width, widths, 100.0, styles);+ styles = searchAxisTag(WEIGHT_TAG, weight, weights, 400.0, 500.0, styles);}+ // Test all the italics and slant stuff, complete with fallbacks...if (styles.size() > 1) {- QHash<int, KoSvgText::FontFamilyStyleInfo> filteredStyles;- for (auto it = styles.begin(); it!= styles.end(); it++) {- int fontStyle = it.value().isItalic? it.value().isOblique? QFont::StyleOblique: QFont::StyleItalic: QFont::StyleNormal;- if (fontStyle == styleMode) {- filteredStyles.insert(it.key(), it.value());+ QHash<int, KoSvgText::FontFamilyStyleInfo> italicStyles;+ QHash<int, KoSvgText::FontFamilyStyleInfo> obliqueStyles;+ if (!slants.isEmpty()) {+ obliqueStyles = searchAxisTag(SLANT_TAG,+ styleMode == QFont::StyleItalic? -11.0: -styleValue, slants,+ 0.0,+ 0.0,+ styles);+ }+ if (!italics.isEmpty()) {+ italicStyles = searchAxisTag(ITALIC_TAG,+ 1.0,+ italics,+ 0.0,+ 0.0,+ styles);+ }+ if (styleMode == QFont::StyleItalic) {+ if (!italicStyles.isEmpty()) {+ styles = italicStyles;+ } else if (!obliqueStyles.isEmpty()) {+ styles = obliqueStyles;+ }+ } else if (styleMode == QFont::StyleOblique) {+ if (!obliqueStyles.isEmpty()) {+ styles = obliqueStyles;+ } else if (!italicStyles.isEmpty()) {+ styles = italicStyles;+ }+ } else {+ QHash<int, KoSvgText::FontFamilyStyleInfo> regularStyles;+ for (auto it = styles.begin(); it!= styles.end(); it++) {+ if (!obliqueStyles.contains(it.key()) && !italicStyles.contains(it.key())) {+ regularStyles.insert(it.key(), it.value());+ }}+ if (!regularStyles.isEmpty()) styles = regularStyles;}- if (!filteredStyles.isEmpty()) styles = filteredStyles;}return styles.isEmpty()? 0: styles.keys().first();}diff --git a/plugins/dockers/textproperties/FontStyleModel.h b/plugins/dockers/textproperties/FontStyleModel.hindex 50de75b8d9..84a036240f 100644--- a/plugins/dockers/textproperties/FontStyleModel.h+++ b/plugins/dockers/textproperties/FontStyleModel.h@@ -43,7 +43,7 @@ public:Q_INVOKABLE QVariantHash axesValues(int row);/// Find the closest style that represents the current width, weight and stylemode.- Q_INVOKABLE int rowForStyle(qreal weight, qreal width, int styleMode);+ Q_INVOKABLE int rowForStyle(const qreal &weight, const qreal &width, const int &styleMode, const qreal &styleValue);// QAbstractItemModel interfacepublic:diff --git a/plugins/dockers/textproperties/qml/FontStyle.qml b/plugins/dockers/textproperties/qml/FontStyle.qmlindex f8f213428d..764006563e 100644--- a/plugins/dockers/textproperties/qml/FontStyle.qml+++ b/plugins/dockers/textproperties/qml/FontStyle.qml@@ -40,7 +40,7 @@ CollapsibleGroupProperty {fontSlantSlope = properties.fontStyle.value;fontSynthesizeSlant = properties.fontSynthesisStyle;fontSynthesizeWeight = properties.fontSynthesisWeight;- styleCmb.currentIndex = fontStylesModel.rowForStyle(properties.fontWeight, properties.fontWidth, properties.fontStyle);+ styleCmb.currentIndex = fontStylesModel.rowForStyle(properties.fontWeight, properties.fontWidth, properties.fontStyle.style, properties.fontStyle.value);visible = properties.fontWeightState !== KoSvgTextPropertiesModel.PropertyUnset|| properties.fontStyleState !== KoSvgTextPropertiesModel.PropertyUnset|| properties.fontWidthState !== KoSvgTextPropertiesModel.PropertyUnset@@ -90,6 +90,9 @@ CollapsibleGroupProperty {properties.fontWeightState = KoSvgTextPropertiesModel.PropertySet;}+ Component.onCompleted: {+ mainWindow.connectAutoEnabler(fontSlantSpnArea);+ }titleItem: RowLayout{width: parent.width;@@ -231,15 +234,24 @@ CollapsibleGroupProperty {height: 1;Layout.columnSpan: 2;}- SpinBox {- id: fontSlantSpn- from: -90;- to: 90;- editable: true;+ MouseArea {+ id: fontSlantSpnArea;+ function autoEnable() {+ fontSlant = CssFontStyleModel.StyleOblique;+ }Layout.fillWidth: true;+ Layout.fillHeight: true;+ Layout.minimumHeight: fontSlantSpn.height;+ SpinBox {+ id: fontSlantSpn+ from: -90;+ to: 90;+ editable: true;+ enabled: fontSlant == CssFontStyleModel.StyleOblique;- wheelEnabled: true;+ wheelEnabled: true;+ }}RevertPropertyButton {