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++
Untangle some line-calculation problems in vertical text layout.
Wolthera van Hövell tot Westerflierda8d86f8faed
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex c2952e2cd1..b7d35238f8 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1533,6 +1533,15 @@ void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements)pen.setColor(QColor(255, 128, 0, 128));painter.setPen(pen);painter.drawRect(chunk.boundingBox);++ pen.setColor(QColor(255, 0, 0, 128));+ pen.setStyle(Qt::DashDotDotLine);+ painter.setPen(pen);+ painter.drawLine(chunk.length.translated(lineBox.baselineTop));+ pen.setColor(QColor(0, 128, 0, 128));+ pen.setStyle(Qt::DashDotLine);+ painter.setPen(pen);+ painter.drawLine(chunk.length.translated(lineBox.baselineBottom));}}}diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cppindex 24f5e15134..535638a0f9 100644--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp@@ -28,24 +28,15 @@ namespace KoSvgTextShapeLayoutFunc*/void calculateLineHeight(CharacterResult cr, double &ascent, double &descent, bool isHorizontal, bool compare){- double offset = isHorizontal? cr.totalBaselineOffset().y(): cr.totalBaselineOffset().x();- double offsetAsc = 0.0;- double offsetDsc = 0.0;- if (cr.scaledAscent <= 0) {- offsetAsc = cr.scaledAscent - cr.scaledHalfLeading;- offsetDsc = cr.scaledDescent + cr.scaledHalfLeading;- } else {- offsetAsc = cr.scaledAscent + cr.scaledHalfLeading;- offsetDsc = cr.scaledDescent - cr.scaledHalfLeading;- }- offsetAsc += offset;- offsetDsc += offset;+ QRectF lineHeightBox = cr.lineHeightBox().translated(cr.totalBaselineOffset());+ double offsetAsc = isHorizontal? lineHeightBox.top(): lineHeightBox.right();+ double offsetDsc = isHorizontal? lineHeightBox.bottom(): lineHeightBox.left();if (!compare) {ascent = offsetAsc;descent = offsetDsc;} else {- if (cr.scaledAscent <= 0) {+ if (isHorizontal) {ascent = qMin(offsetAsc, ascent);descent = qMax(offsetDsc, descent);} else {@@ -104,6 +95,7 @@ void addWordToLine(QVector<CharacterResult> &result,}currentPos = lineAdvance;currentChunk.chunkIndices += wordIndices;+ currentChunk.length.setP2(lineAdvance);currentLine.setCurrentChunk(currentChunk);wordIndices.clear();}@@ -143,8 +135,9 @@ static QPointF lineHeightOffset(KoSvgText::WritingMode writingMode,}}- qreal expectedLineTop = currentLine.actualLineTop > 0? qMax(currentLine.expectedLineTop, currentLine.actualLineTop):- qMin(currentLine.expectedLineTop, currentLine.actualLineTop);+ // 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);if (writingMode == KoSvgText::HorizontalTB) {currentLine.baselineTop = QPointF(0, currentLine.actualLineTop);diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex 3fde408107..1906c27598 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -130,7 +130,7 @@ struct CharacterResult {*/QRectF layoutBox() const {return isHorizontal? QRectF(0, advance.y()+scaledAscent, advance.x(), scaledDescent-scaledAscent)- : QRectF(advance.x()-scaledDescent, 0, scaledDescent-scaledAscent, advance.y());+ : QRectF(advance.x()+scaledDescent, 0, scaledAscent-scaledDescent, advance.y());}/*** @brief lineHeightBox@@ -166,8 +166,8 @@ struct CharacterResult {scaledDescent -= newOrigin.y();scaledAscent -= newOrigin.y();} else {- scaledDescent += newOrigin.x();- scaledAscent += newOrigin.x();+ scaledDescent -= newOrigin.x();+ scaledAscent -= newOrigin.x();}}@@ -249,12 +249,12 @@ struct LineBox {QVector<LineChunk> chunks;int currentChunk = -1;- qreal expectedLineTop = 0;+ qreal expectedLineTop = 0; ///< Because fonts can affect lineheight mid-line, and this affects wrapping, this estimates the line-height.qreal actualLineTop = 0;qreal actualLineBottom = 0;- QPointF baselineTop = QPointF();- QPointF baselineBottom = QPointF();+ QPointF baselineTop = QPointF(); ///< Used to identify the top of the line for baseline-alignment.+ QPointF baselineBottom = QPointF(); ///< Used to identify the bottom of the line for baseline-alignment.QPointF textIndent = QPointF();bool firstLine = false;diff --git a/libs/flake/text/KoSvgTextShape_p_output.cpp b/libs/flake/text/KoSvgTextShape_p_output.cppindex 2706ac652e..10e7552171 100644--- a/libs/flake/text/KoSvgTextShape_p_output.cpp+++ b/libs/flake/text/KoSvgTextShape_p_output.cpp@@ -545,7 +545,7 @@ void KoSvgTextShape::Private::paintDebug(QPainter &painter,penColor.setAlpha(192);pen.setColor(penColor);painter.setPen(pen);- painter.drawPolygon(tf.map(result.at(i).inkBoundingBox));+ painter.drawPolygon(tf.map(result.at(i).layoutBox()));penColor.setAlpha(96);pen.setColor(penColor);