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++
Replace fontAscent/Descent with KoSvgText::Metrics struct.
Wolthera van Hövell tot Westerflierb371039b6fc1
diff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cppindex fae75e87bf..8ccc8a217f 100644--- a/libs/flake/text/KoFontRegistry.cpp+++ b/libs/flake/text/KoFontRegistry.cpp@@ -26,6 +26,7 @@#include "KoFontLibraryResourceUtils.h"#include "KoFFWWSConverter.h"#include "KoFontChangeTracker.h"+#include "KoWritingSystemUtils.h"#include <KisResourceLocator.h>#include FT_TRUETYPE_TABLES_H@@ -726,7 +727,7 @@ QFont::Style KoFontRegistry::slantMode(FT_FaceSP face)KoSvgText::FontMetrics KoFontRegistry::fontMetricsForCSSValues(KoCSSFontInfo info, const bool isHorizontal, const QString &text, quint32 xRes, quint32 yRes, bool disableFontMatching, const QString &language){- const QString suggestedHash = info.families.join(",")+":"+modificationsString(info, xRes, yRes);+ const QString suggestedHash = info.families.join(",")+":"+modificationsString(info, xRes, yRes)+language;KoSvgText::FontMetrics metrics;auto entry = d->fontMetrics().find(suggestedHash);if (entry != d->fontMetrics().end()) {@@ -741,18 +742,24 @@ KoSvgText::FontMetrics KoFontRegistry::fontMetricsForCSSValues(KoCSSFontInfo infyRes,disableFontMatching,language);- if (faces.empty()) return metrics;- metrics = KoFontRegistry::generateFontMetrics(faces.front(), isHorizontal);++ if (faces.empty()) return KoSvgText::FontMetrics(info.size, isHorizontal);+ metrics = KoFontRegistry::generateFontMetrics(faces.front(), isHorizontal, KoWritingSystemUtils::scriptTagForQLocaleScript(QLocale(language).script()));d->fontMetrics().insert(suggestedHash, metrics);}return metrics;}-KoSvgText::FontMetrics KoFontRegistry::generateFontMetrics(FT_FaceSP face, bool isHorizontal)+KoSvgText::FontMetrics KoFontRegistry::generateFontMetrics(FT_FaceSP face, bool isHorizontal, QString script){KoSvgText::FontMetrics metrics;hb_direction_t dir = isHorizontal? HB_DIRECTION_LTR: HB_DIRECTION_TTB;- hb_script_t script = HB_SCRIPT_UNKNOWN;+ QLatin1String scriptLatin1(script.toLatin1());+ hb_script_t scriptTag = hb_script_from_string(scriptLatin1.data(), scriptLatin1.size());+ hb_tag_t otScriptTag;+ hb_tag_t otLangTag;+ uint maxCount = 1;+ hb_ot_tags_from_script_and_language(scriptTag, nullptr, &maxCount, &otScriptTag, &maxCount, &otLangTag);hb_font_t_sp font(hb_ft_font_create_referenced(face.data()));FT_Int32 faceLoadFlags = loadFlagsForFace(face.data(), isHorizontal);@@ -824,9 +831,9 @@ KoSvgText::FontMetrics KoFontRegistry::generateFontMetrics(FT_FaceSP face, boolconst QLatin1String tagName(c, 4);hb_position_t origin = 0;if (useFallback) {- hb_ot_layout_get_baseline_with_fallback(font.data(), *it, dir, script, HB_TAG_NONE, &origin);+ hb_ot_layout_get_baseline_with_fallback(font.data(), *it, dir, otScriptTag, otLangTag, &origin);} else {- hb_ot_layout_get_baseline(font.data(), *it, dir, script, HB_TAG_NONE, &origin);+ hb_ot_layout_get_baseline(font.data(), *it, dir, otScriptTag, otLangTag, &origin);}metrics.setBaselineValueByTag(tagName, origin);}diff --git a/libs/flake/text/KoFontRegistry.h b/libs/flake/text/KoFontRegistry.hindex 2d29144f5b..b90315cf29 100644--- a/libs/flake/text/KoFontRegistry.h+++ b/libs/flake/text/KoFontRegistry.h@@ -105,7 +105,7 @@ public:bool disableFontMatching = false,const QString &language = QString());- static KoSvgText::FontMetrics generateFontMetrics(FT_FaceSP face, bool isHorizontal = true);+ static KoSvgText::FontMetrics generateFontMetrics(FT_FaceSP face, bool isHorizontal = true, QString script = QString());static int32_t loadFlagsForFace(FT_Face face, bool isHorizontal = true, int32_t loadFlags = 0);private Q_SLOTS:diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cppindex 528f68c525..c26dd19828 100644--- a/libs/flake/text/KoSvgText.cpp+++ b/libs/flake/text/KoSvgText.cpp@@ -1352,6 +1352,53 @@ QStringList fontFeaturesCaps(const FontFeatureCaps &feature, const int start, coreturn list;}+FontMetrics::FontMetrics(qreal fontSizeInPt, bool isHorizontal)+ : isVertical(!isHorizontal)+ , fontSize(fontSizeInPt * 64.0)+{+ ideographicAdvance = fontSize;+ xHeight = fontSize/2;+ capHeight = (fontSize / 5) * 4;++ subScriptOffset.second = -(fontSize / 5);+ superScriptOffset.second = (fontSize / 3);+ if (isHorizontal) {+ zeroAdvance = fontSize/2;+ spaceAdvance = fontSize/2;++ ascender = (fontSize / 5) * 4;+ descender = ascender - fontSize;++ mathematicalBaseline = xHeight/2;++ ideographicUnderBaseline = descender;+ ideographicOverBaseline = ascender;+ ideographicCenterBaseline = (ascender+descender)/2;++ hangingBaseline = (fontSize / 5) * 3;++ } else {+ zeroAdvance = fontSize;+ spaceAdvance = fontSize;++ ascender = fontSize /2;+ descender = ascender - fontSize;++ ideographicUnderBaseline = descender;+ ideographicOverBaseline = ascender;+ ideographicCenterBaseline = (ascender+descender)/2;++ mathematicalBaseline = ideographicCenterBaseline;++ alphabeticBaseline = ascender - (fontSize / 5) * 4;++ hangingBaseline = alphabeticBaseline + ((fontSize / 5) * 3);+ }++ ideographicFaceUnderBaseline = descender;+ ideographicFaceOverBaseline = ascender;+}+void FontMetrics::setBaselineValueByTag(const QLatin1String &tag, int32_t value) {if (tag == "romn") {alphabeticBaseline = value;diff --git a/libs/flake/text/KoSvgText.h b/libs/flake/text/KoSvgText.hindex 5382f03ab1..d07a878903 100644--- a/libs/flake/text/KoSvgText.h+++ b/libs/flake/text/KoSvgText.h@@ -995,7 +995,7 @@ struct FontMetrics : public boost::equality_comparable<FontMetrics> {qint32 xHeight; ///< height of X, defaults to 0.5 fontsize.qint32 capHeight; ///< Height of capital letters, defaults to ascender.QPair<qint32, qint32> subScriptOffset; ///< subscript baseline height, defaults to 1/5th em below alphabetic.- QPair<qint32, qint32> superScriptOffset; ///< superscript baseline height, defaults to 2/3rd about alphabetic.+ QPair<qint32, qint32> superScriptOffset; ///< superscript baseline height, defaults to 2/3rd above alphabetic.qint32 ascender; ///< distance from origin to top.qint32 descender; ///< distance for origin to bottom.@@ -1014,6 +1014,13 @@ struct FontMetrics : public boost::equality_comparable<FontMetrics> {qint32 hangingBaseline; ///< location of the hanging baseline used in north brahmic scripts.+ FontMetrics () {++ }++ // Generate fallback font metrics from a fontSize.+ FontMetrics (qreal fontSizeInPt, bool isHorizontal);+bool operator==(const FontMetrics & other) const {return isVertical == other.isVertical&& fontSize == other.fontSizediff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex c2ff98a7f1..4252d5790d 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -117,8 +117,7 @@ struct CharacterResult {bool overflowWrap = false;qreal fontHalfLeading; ///< Leading for both sides, can be either negative or positive.- int fontAscent; ///< Ascender, in scanline coordinates- int fontDescent; ///< Descender, in scanline coordinates+ KoSvgText::FontMetrics metrics; ///< Fontmetrics for current font, in Freetype scanline coordinates.qreal scaledHalfLeading{}; ///< Leading for both sides, can be either negative or positive, in ptqreal scaledAscent{}; ///< Ascender, in ptqreal scaledDescent{}; ///< Descender, in ptdiff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cppindex d8a75d4d0f..abc35fccb3 100644--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp@@ -508,8 +508,8 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,if (charResult.visualIndex == -1) {hb_font_t_sp font(hb_ft_font_create_referenced(currentGlyph.ftface));CursorInfo cursorInfo = charResult.cursorInfo;- qreal lineHeight = (charResult.fontAscent-charResult.fontDescent) * bitmapScale;- qreal descender = charResult.fontDescent * bitmapScale;+ qreal lineHeight = (charResult.metrics.ascender-charResult.metrics.descender) * bitmapScale;+ qreal descender = charResult.metrics.descender * bitmapScale;if (isHorizontal) {hb_position_t run = 0;hb_position_t rise = 1;@@ -615,14 +615,14 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,QRectF bbox;if (isHorizontal) {bbox = QRectF(0,- charResult.fontDescent * bitmapScale,+ charResult.metrics.descender * bitmapScale,ftTF.inverted().map(charResult.advance).x(),- (charResult.fontAscent - charResult.fontDescent) * bitmapScale);+ (charResult.metrics.ascender - charResult.metrics.descender) * bitmapScale);bbox = glyphObliqueTf.mapRect(bbox);} else {- bbox = QRectF(charResult.fontDescent * bitmapScale,+ bbox = QRectF(charResult.metrics.descender * bitmapScale,0,- (charResult.fontAscent - charResult.fontDescent) * bitmapScale,+ (charResult.metrics.ascender - charResult.metrics.descender) * bitmapScale,ftTF.inverted().map(charResult.advance).y());bbox = glyphObliqueTf.mapRect(bbox);}diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex 6b908f0044..b7d1fd8f17 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -14,6 +14,7 @@#include "KoFontRegistry.h"#include "KoSvgTextProperties.h"#include "KoColorBackground.h"+#include "KoWritingSystemUtils.h"#include <FlakeDebug.h>#include <KoPathShape.h>@@ -432,21 +433,25 @@ void KoSvgTextShape::Private::relayout()static_cast<size_t>(length));}- KoSvgText::FontMetrics metrics = KoFontRegistry::generateFontMetrics(face, isHorizontal);-+ QHash<QChar::Script, KoSvgText::FontMetrics> metricsList;for (int j=start; j<start+length; j++) {- result[j].fontAscent = metrics.ascender;- result[j].fontDescent = metrics.descender;- qreal leading = metrics.lineGap;+ //TODO: handle low/high surrogate...+ const QChar::Script currentScript = plainText.at(j).script();+ if (!metricsList.contains(currentScript)) {+ metricsList.insert(currentScript, KoFontRegistry::generateFontMetrics(face, isHorizontal, KoWritingSystemUtils::scriptTagForQCharScript(currentScript)));+ }+ result[j].metrics = metricsList.value(currentScript);++ qreal leading = result[j].metrics.lineGap;if (!lineHeight.isNormal) {if (lineHeight.isNumber) {leading = (fontSize*scaleToPixel*ftFontUnit)*lineHeight.value;- leading -= (metrics.ascender-metrics.descender);+ leading -= (result[j].metrics.ascender-result[j].metrics.descender);} else {QPointF val = ftTF.inverted().map(QPointF(lineHeight.length.value, lineHeight.length.value));leading = isHorizontal? val.x(): val.y();- leading -= (metrics.ascender-metrics.descender);+ leading -= (result[j].metrics.ascender-result[j].metrics.descender);}}result[j].fontHalfLeading = leading * 0.5;