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 tab-size property.

Wolthera van Hövell tot Westerflier51886a303468
diff --git a/libs/flake/text/KoFontRegistry.cpp b/libs/flake/text/KoFontRegistry.cpp
index dbe618009d..b9122d1400 100644
--- a/libs/flake/text/KoFontRegistry.cpp
+++ b/libs/flake/text/KoFontRegistry.cpp
@@ -785,24 +785,33 @@ KoSvgText::FontMetrics KoFontRegistry::generateFontMetrics(FT_FaceSP face, bool
// Fontsize and advances.
metrics.fontSize = isHorizontal? face.data()->size->metrics.y_ppem*64.0: face.data()->size->metrics.x_ppem*64.0;
- if(uint charIndex = FT_Get_Char_Index(face.data(), ' ') > 0) {
- FT_Load_Glyph(face.data(), charIndex, faceLoadFlags);
- metrics.spaceAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ if(uint charIndex = FT_Get_Char_Index(face.data(), 0x20) > 0) {
+ if (FT_Load_Glyph(face.data(), charIndex, faceLoadFlags) == FT_Err_Ok) {
+ metrics.spaceAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ } else {
+ metrics.spaceAdvance = isHorizontal? metrics.fontSize/2: metrics.fontSize;
+ }
} else {
metrics.spaceAdvance = isHorizontal? metrics.fontSize/2: metrics.fontSize;
}
if(uint charIndex = FT_Get_Char_Index(face.data(), '0') > 0) {
- FT_Load_Glyph(face.data(), charIndex, faceLoadFlags);
- metrics.zeroAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ if(FT_Load_Glyph(face.data(), charIndex, faceLoadFlags) == FT_Err_Ok) {
+ metrics.zeroAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ } else {
+ metrics.zeroAdvance = isHorizontal? metrics.fontSize/2: metrics.fontSize;
+ }
} else {
metrics.zeroAdvance = isHorizontal? metrics.fontSize/2: metrics.fontSize;
}
bool isIdeographic = false;
if(uint charIndex = FT_Get_Char_Index(face.data(), 0x6C34) > 0) {
- FT_Load_Glyph(face.data(), charIndex, faceLoadFlags);
- metrics.ideographicAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ if (FT_Load_Glyph(face.data(), charIndex, faceLoadFlags) == FT_Err_Ok) {
+ metrics.ideographicAdvance = isHorizontal? face.data()->glyph->advance.x: face.data()->glyph->advance.y;
+ } else {
+ metrics.ideographicAdvance = metrics.fontSize;
+ }
isIdeographic = true;
} else {
metrics.ideographicAdvance = metrics.fontSize;
diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cpp
index 5748726c93..343877b7cc 100644
--- a/libs/flake/text/KoSvgText.cpp
+++ b/libs/flake/text/KoSvgText.cpp
@@ -748,10 +748,17 @@ QString writeTextIndent(const TextIndentInfo textIndent)
TabSizeInfo parseTabSize(const QString &value, const SvgLoadingContext &context)
{
TabSizeInfo tabSizeInfo;
- tabSizeInfo.value = KisDomUtils::toDouble(value, &tabSizeInfo.isNumber);
- if (!tabSizeInfo.isNumber) {
+ qreal val = KisDomUtils::toDouble(value, &tabSizeInfo.isNumber);
+ if (tabSizeInfo.isNumber) {
+ tabSizeInfo.value = qMax(0.0, val);
+ } else {
tabSizeInfo.length = SvgUtil::parseTextUnitStruct(context.currentGC(), value);
}
+ if ((tabSizeInfo.isNumber && tabSizeInfo.value < 0) || tabSizeInfo.length.value < 0) {
+ tabSizeInfo.isNumber = true;
+ tabSizeInfo.value = 0;
+ tabSizeInfo.length.value = 0;
+ }
return tabSizeInfo;
}
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
index 34a29d9901..a71913cdcc 100644
--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
@@ -484,6 +484,7 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,
QPainterPath currentShape;
while (it.hasNext()) {
int index = it.next();
+ result[index].calculateAndApplyTabsize(wordAdvance + currentPos, isHorizontal);
CharacterResult charResult = result.at(index);
if (!charResult.addressable) {
continue;
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
index 7ddd171445..1c0c77f6f7 100644
--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
@@ -84,6 +84,7 @@ void addWordToLine(QVector<CharacterResult> &result,
calculateLineHeight(cr, currentLine.actualLineTop, currentLine.actualLineBottom, isHorizontal, !cr.anchored_chunk);
cr.cssPosition = currentPos;
+ cr.calculateAndApplyTabsize(currentPos, isHorizontal);
currentPos += cr.advance;
lineAdvance = currentPos;
@@ -478,6 +479,7 @@ QVector<LineBox> breakLines(const KoSvgTextProperties &properties,
QListIterator<int> it(logicalToVisual.keys());
while (it.hasNext()) {
int index = it.next();
+ result[index].calculateAndApplyTabsize(wordAdvance + currentPos, isHorizontal);
CharacterResult charResult = result.at(index);
if (!charResult.addressable) {
continue;
@@ -542,6 +544,7 @@ QVector<LineBox> breakLines(const KoSvgTextProperties &properties,
QVector<int> partialWord;
currentLine.firstLine = firstLine;
Q_FOREACH (const int i, wordIndices) {
+ result[i].calculateAndApplyTabsize(wordAdvance + currentPos, isHorizontal);
wordAdvance += result.at(i).advance;
wordLength = isHorizontal ? wordAdvance.x() : wordAdvance.y();
if (wordLength <= inlineSize.customValue) {
@@ -563,6 +566,7 @@ QVector<LineBox> breakLines(const KoSvgTextProperties &properties,
if (!inlineSize.isAuto) {
currentPos += currentLine.textIndent;
}
+ result[i].calculateAndApplyTabsize(wordAdvance + currentPos, isHorizontal);
wordAdvance = result.at(i).advance;
partialWord.append(i);
}
diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.h
index 8a936e1ae0..dc18c777f4 100644
--- a/libs/flake/text/KoSvgTextShape_p.h
+++ b/libs/flake/text/KoSvgTextShape_p.h
@@ -23,6 +23,7 @@
#include <QPointF>
#include <QRectF>
#include <QVector>
+#include <QtMath>
#include <variant>
@@ -124,6 +125,20 @@ struct CharacterResult {
qreal scaledAscent{}; ///< Ascender, in pt
qreal scaledDescent{}; ///< Descender, in pt
+ std::optional<qreal> tabSize; ///< If present, this is a tab and it should align to multiples of this tabSize value.
+
+ void calculateAndApplyTabsize(QPointF currentPos, bool isHorizontal) {
+ if (!tabSize) return;
+ if (*tabSize == qInf() || qIsNaN(*tabSize)) return;
+
+ if (*tabSize > 0) {
+ qreal remainder = *tabSize - (isHorizontal? fmod(currentPos.x(), *tabSize): fmod(currentPos.y(), *tabSize));
+ advance = isHorizontal? QPointF(remainder, advance.y()): QPointF(advance.x(), remainder);
+ } else {
+ advance = isHorizontal? QPointF(0, advance.y()): QPointF(advance.x(), 0);
+ }
+ }
+
/**
* @brief layoutBox
* @return a dynamically calculated layoutBox, this is different from the Ink bounding box.
@@ -412,7 +427,6 @@ public:
void relayout();
bool loadGlyph(const QTransform &ftTF,
- const QMap<int, KoSvgText::TabSizeInfo> &tabSizeInfo,
FT_Int32 faceLoadFlags,
bool isHorizontal,
char32_t firstCodepoint,
diff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
index 828b04df29..76070ab184 100644
--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
@@ -474,7 +474,6 @@ std::pair<QTransform, qreal> KoSvgTextShape::Private::loadGlyphOnly(const QTrans
*/
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,
- const QMap<int, KoSvgText::TabSizeInfo> &tabSizeInfo,
const FT_Int32 faceLoadFlags,
const bool isHorizontal,
const char32_t firstCodepoint,
@@ -486,14 +485,6 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,
// mean a 1/64 value.
const qreal ftFontUnit = 64.0;
- const int cluster = static_cast<int>(currentGlyph.cluster);
-
- QPointF spaceAdvance;
- if (tabSizeInfo.contains(cluster)) {
- FT_Load_Glyph(currentGlyph.ftface, FT_Get_Char_Index(currentGlyph.ftface, ' '), faceLoadFlags);
- spaceAdvance = QPointF(currentGlyph.ftface->glyph->advance.x, currentGlyph.ftface->glyph->advance.y);
- }
-
/// The matrix for Italic (oblique) synthesis of outline glyphs, or for
/// adjusting the bounding box of bitmap glyphs.
QTransform glyphObliqueTf;
@@ -554,15 +545,7 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,
{
QPointF advance(currentGlyph.x_advance, currentGlyph.y_advance);
- if (tabSizeInfo.contains(cluster)) {
- KoSvgText::TabSizeInfo tabSize = tabSizeInfo.value(cluster);
- qreal newAdvance = tabSize.length.value * ftFontUnit;
- if (tabSize.isNumber) {
- QPointF extraSpacing = isHorizontal ? QPointF(tabSize.extraSpacing * ftFontUnit, 0) : QPointF(0, tabSize.extraSpacing * ftFontUnit);
- advance = (spaceAdvance + extraSpacing) * tabSize.value;
- } else {
- advance = isHorizontal ? QPointF(newAdvance, advance.y()) : QPointF(advance.x(), newAdvance);
- }
+ if (charResult.tabSize) {
charResult.glyph.emplace<Glyph::Outline>();
}
@@ -621,6 +604,9 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,
charResult.scaledHalfLeading = ftTF.map(QPointF(charResult.fontHalfLeading, charResult.fontHalfLeading)).x();
charResult.scaledAscent = isHorizontal? scaledBBox.top(): scaledBBox.right();
charResult.scaledDescent = isHorizontal? scaledBBox.bottom(): scaledBBox.left();
+ if (charResult.tabSize) {
+ charResult.tabSize = ftTF.map(QPointF(*charResult.tabSize, *charResult.tabSize)).x();
+ }
if(!qFuzzyCompare(bitmapScale, 1.0)) {
charResult.metrics.scaleBaselines(bitmapScale);
diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cpp
index ca905fc6d3..dd64d9fa05 100644
--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp
@@ -277,8 +277,6 @@ void KoSvgTextShape::Private::relayout()
}
this->resolveTransforms(textData.childBegin(), text, result, globalIndex, isHorizontal, wrapped, false, resolvedTransforms, collapseChars, KoSvgTextProperties::defaultProperties());
- QMap<int, KoSvgText::TabSizeInfo> tabSizeInfo;
-
// pass everything to a css-compatible text-layout algortihm.
raqm_t_sp layout(raqm_create());
@@ -420,9 +418,7 @@ void KoSvgTextShape::Private::relayout()
cr.cursorInfo.isWordBoundary = (wordBreaks[start + i] == WORDBREAK_BREAK);
cr.cursorInfo.color = fillColor;
- if (text.at(start + i) == QChar::Tabulation) {
- tabSizeInfo.insert(start + i, tabInfo);
- }
+
if (resolvedTransforms.at(start + i).startsNewChunk()) {
raqm_set_arbitrary_run_break(layout.data(), static_cast<size_t>(start + i), true);
@@ -502,6 +498,16 @@ void KoSvgTextShape::Private::relayout()
const KoSvgText::FontMetrics currentMetrics = properties.applyLineHeight(result[j].metrics);
+ if (text.at(j) == QChar::Tabulation) {
+ qreal tabSize = 0;
+ if (tabInfo.isNumber) {
+ tabSize = (result[j].metrics.spaceAdvance + (tabInfo.extraSpacing*ftFontUnit)) * tabInfo.value;
+ } else {
+ tabSize = tabInfo.length.value * ftFontUnit;
+ }
+ result[j].tabSize = tabSize;
+ }
+
result[j].fontHalfLeading = currentMetrics.lineGap / 2;
result[j].fontStyle = synthesizeStyle? style.style: QFont::StyleNormal;
result[j].fontWeight = synthesizeWeight? properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(): 400;
@@ -557,7 +563,6 @@ void KoSvgTextShape::Private::relayout()
}
if (!this->loadGlyph(ftTF,
- tabSizeInfo,
faceLoadFlags,
isHorizontal,
codepoint,
diff --git a/plugins/dockers/textproperties/qml/TabSize.qml b/plugins/dockers/textproperties/qml/TabSize.qml
index e91bade640..56d2fd2162 100644
--- a/plugins/dockers/textproperties/qml/TabSize.qml
+++ b/plugins/dockers/textproperties/qml/TabSize.qml
@@ -33,8 +33,6 @@ TextPropertyBase {
id: converter;
dpi: dpi;
dataMultiplier: tabSizeSpn.multiplier;
- userValue: tabSizeSpn.value;
- userUnit: tabSizeUnitCmb.currentValue;
onUserValueChanged: tabSizeSpn.value = userValue;
onUserUnitChanged: tabSizeUnitCmb.currentIndex = tabSizeUnitCmb.indexOfValue(userUnit);
@@ -44,7 +42,7 @@ TextPropertyBase {
blockSignals = true;
converter.dpi = canvasDPI;
converter.setFontMetricsFromTextPropertiesModel(properties);
- converter.setDataValueAndUnit(properties.tabSize.unit, properties.tabSize.value);
+ converter.setDataValueAndUnit(properties.tabSize.value, properties.tabSize.unit);
visible = properties.tabSizeState !== KoSvgTextPropertiesModel.PropertyUnset;
blockSignals = false;
}
@@ -95,7 +93,8 @@ TextPropertyBase {
id: tabSizeSpn;
Layout.fillWidth: true;
from: 0;
- to: 100 * multiplier;
+ to: 999 * multiplier;
+ onValueChanged: converter.userValue = value;
}
SqueezedComboBox {
id: tabSizeUnitCmb;