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 calculating the lineheight for text-in-shape, while ensuring dominant baseline works.

Wolthera van Hövell tot Westerflierbb0073cba230
diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cpp
index 6b0aa662bb..31a11240a4 100644
--- a/libs/flake/text/KoSvgText.cpp
+++ b/libs/flake/text/KoSvgText.cpp
@@ -1478,7 +1478,7 @@ int FontMetrics::valueForBaselineValue(Baseline baseline) const {
baselineVal = isVertical? descender + ideographicCenterBaseline: descender;
break;
case BaselineTextTop:
- baselineVal = isVertical? ascender + ideographicCenterBaseline: descender;
+ baselineVal = isVertical? ascender + ideographicCenterBaseline: ascender;
break;
default:
break;
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
index b1f35e2491..34a29d9901 100644
--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp
@@ -256,7 +256,7 @@ static bool pointLessThanVertical(const QPointF &a, const QPointF &b)
}
static QVector<QLineF>
-findLineBoxesForFirstPos(QPainterPath shape, QPointF firstPos, QRectF wordBox, KoSvgText::WritingMode writingMode)
+findLineBoxesForFirstPos(QPainterPath shape, QPointF firstPos, const QRectF wordBox, KoSvgText::WritingMode writingMode)
{
QVector<QLineF> lines;
@@ -378,10 +378,15 @@ findLineBoxesForFirstPos(QPainterPath shape, QPointF firstPos, QRectF wordBox, K
return lines;
}
+/**
+ * @brief getEstimatedHeight
+ * Adjust the wordbox with the estimated height.
+ */
+
static void getEstimatedHeight(QVector<CharacterResult> &result,
- int index,
+ const int index,
QRectF &wordBox,
- QRectF boundingBox,
+ const QRectF boundingBox,
KoSvgText::WritingMode writingMode)
{
bool isHorizontal = writingMode == KoSvgText::HorizontalTB;
@@ -552,11 +557,12 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,
bool foundFirst = false;
bool needNewLine = true;
// add text indent to wordbox.
- getEstimatedHeight(result, index, wordBox, currentShape.boundingRect(), writingMode);
if (!currentShape.isEmpty()) {
// we're going to try and get an offset line first before trying get first pos.
// This gives more stable results on curved shapes.
+ getEstimatedHeight(result, index, wordBox, currentShape.boundingRect(), writingMode);
currentPos -= writingMode == KoSvgText::VerticalRL? wordBox.topRight(): wordBox.topLeft();
+
currentLine = LineBox(findLineBoxesForFirstPos(currentShape, currentPos, wordBox, writingMode), ltr, indent);
qreal length = isHorizontal? wordBox.width(): wordBox.height();
for (int i = 0; i < currentLine.chunks.size(); i++) {
@@ -581,6 +587,7 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,
break;
}
currentShape = shapesIt.next();
+ getEstimatedHeight(result, index, wordBox, currentShape.boundingRect(), writingMode);
bool indentPercent = textIndentInfo.length.unit == KoSvgText::CssLengthPercentage::Percentage;
qreal textIdentValue = textIndentInfo.length.value;
if (isHorizontal) {
@@ -610,15 +617,18 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,
}
if (foundFirst) {
+
if (needNewLine) {
currentLine = LineBox(findLineBoxesForFirstPos(currentShape, currentPos, wordBox, writingMode), ltr, indent);
// We could set this to find the first fitting width, but it's better to try and improve the precision of the firstpos algorithm,
// as this gives more stable results.
currentLine.setCurrentChunkForPos(currentPos, isHorizontal);
}
+ const qreal expectedLineT = isHorizontal? wordBox.top():
+ writingMode == KoSvgText::VerticalRL? wordBox.right(): wordBox.left();
+
currentLine.firstLine = firstLine;
- currentLine.expectedLineTop = isHorizontal? -wordBox.top():
- writingMode == KoSvgText::VerticalRL? wordBox.right(): wordBox.left();
+ currentLine.expectedLineTop = expectedLineT;
currentLine.justifyLine = align == KoSvgText::AlignJustify;
currentPos = currentLine.chunk().length.p1() + indent;
lineOffset = currentPos;
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
index 82b5163699..b1b8d934bc 100644
--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp
@@ -112,6 +112,7 @@ static QPointF lineHeightOffset(KoSvgText::WritingMode writingMode,
QPointF lineBottom;
QPointF correctionOffset; ///< This is for determining the difference between
///< a predicted line-height (for text-in-shape) and the actual line-height.
+ ///< Note: this is required to be positive.
if (currentLine.chunks.isEmpty()) {
return QPointF();
@@ -135,19 +136,18 @@ static QPointF lineHeightOffset(KoSvgText::WritingMode writingMode,
}
// We do qmin/qmax here so that later, the correction offset will not go below either value.
- qreal expectedLineTop = writingMode == KoSvgText::HorizontalTB? qMin(currentLine.expectedLineTop, currentLine.actualLineTop):
- qMax(currentLine.expectedLineTop, currentLine.actualLineTop);
-
+ const qreal expectedLineTop = writingMode == KoSvgText::HorizontalTB? qMin(currentLine.expectedLineTop, currentLine.actualLineTop):
+ qMax(currentLine.expectedLineTop, currentLine.actualLineTop);
if (writingMode == KoSvgText::HorizontalTB) {
currentLine.baselineTop = QPointF(0, currentLine.actualLineTop);
currentLine.baselineBottom = QPointF(0, currentLine.actualLineBottom);
- correctionOffset = QPointF(0, expectedLineTop) - currentLine.baselineTop;
+ correctionOffset = QPointF(0, -expectedLineTop) + currentLine.baselineTop;
lineTop = -currentLine.baselineTop;
lineBottom = currentLine.baselineBottom;
} else if (writingMode == KoSvgText::VerticalLR) {
currentLine.baselineTop = QPointF(currentLine.actualLineTop, 0);
currentLine.baselineBottom = QPointF(currentLine.actualLineBottom, 0);
- correctionOffset = QPointF(expectedLineTop, 0) - currentLine.baselineTop;
+ correctionOffset = QPointF(-expectedLineTop, 0) + currentLine.baselineTop;
// Note: while Vertical LR goes left-to-right in its lines, its lines themselves are
// oriented with the top pointed in the positive x direction.
lineBottom = currentLine.baselineTop;
@@ -155,7 +155,7 @@ static QPointF lineHeightOffset(KoSvgText::WritingMode writingMode,
} else {
currentLine.baselineTop = QPointF(currentLine.actualLineTop, 0);
currentLine.baselineBottom = QPointF(currentLine.actualLineBottom, 0);
- correctionOffset = QPointF(-expectedLineTop, 0) + currentLine.baselineTop;
+ correctionOffset = QPointF(expectedLineTop, 0) - currentLine.baselineTop;
lineTop = -currentLine.baselineTop;
lineBottom = currentLine.baselineBottom;
}