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++

Make ligature caret position retrieval only happen when necessary.

Wolthera van Hövell tot Westerflier97e0a9bd47d2
diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.h
index 5cde7eed5d..15758f1d79 100644
--- a/libs/flake/text/KoSvgTextShape_p.h
+++ b/libs/flake/text/KoSvgTextShape_p.h
@@ -588,6 +588,10 @@ public:
CharacterResult &charResult,
QPointF &totalAdvanceFTFontCoordinates);
+ static QVector<QPointF> getLigatureCarets(const KoSvgText::ResolutionHandler &resHandler,
+ const bool isHorizontal,
+ raqm_glyph_t &currentGlyph);
+
static std::pair<QTransform, qreal> loadGlyphOnly(const QTransform &ftTF,
FT_Int32 faceLoadFlags,
bool isHorizontal,
diff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
index 15884de8d8..3141f65e23 100644
--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp
@@ -513,20 +513,6 @@ bool KoSvgTextShape::Private::loadGlyph(const KoSvgText::ResolutionHandler &resH
caret.translate(-QPointF(-offset, -descender));
cursorInfo.caret = ftTF.map(glyphObliqueTf.map(caret));
- QVector<QPointF> positions;
- // Ligature caret list only uses direction to determine whether horizontal or vertical.
- hb_direction_t dir = HB_DIRECTION_LTR;
-
- uint numCarets = hb_ot_layout_get_ligature_carets(font.data(), dir, currentGlyph.index, 0, nullptr, nullptr);
- for (uint i=0; i<numCarets; i++) {
- hb_position_t caretPos = 0;
- uint caretCount = 1;
- hb_ot_layout_get_ligature_carets(font.data(), dir, currentGlyph.index, 0, &caretCount, &caretPos);
- QPointF pos(caretPos, 0);
- positions.append(ftTF.map(pos));
- }
-
- cursorInfo.offsets = positions;
} else {
qreal slope = 0;
if (charResult.metrics.caretRun != 0 && charResult.metrics.caretRise !=0) {
@@ -634,6 +620,25 @@ bool KoSvgTextShape::Private::loadGlyph(const KoSvgText::ResolutionHandler &resH
return true;
}
+QVector<QPointF> KoSvgTextShape::Private::getLigatureCarets(const KoSvgText::ResolutionHandler &resHandler, const bool isHorizontal, raqm_glyph_t &currentGlyph)
+{
+ QVector<QPointF> positions;
+ const QTransform ftTF = resHandler.freeTypeToPointTransform();
+ // Ligature caret list only uses direction to determine whether horizontal or vertical.
+ hb_direction_t dir = isHorizontal? HB_DIRECTION_LTR: HB_DIRECTION_TTB;
+ hb_font_t_sp font(hb_ft_font_create_referenced(currentGlyph.ftface));
+
+ uint numCarets = hb_ot_layout_get_ligature_carets(font.data(), dir, currentGlyph.index, 0, nullptr, nullptr);
+ for (uint i=0; i<numCarets; i++) {
+ hb_position_t caretPos = 0;
+ uint caretCount = 1;
+ hb_ot_layout_get_ligature_carets(font.data(), dir, currentGlyph.index, 0, &caretCount, &caretPos);
+ QPointF pos = isHorizontal? QPointF(caretPos, 0): QPointF(0, caretPos);
+ positions.append(ftTF.map(pos));
+ }
+ return positions;
+}
+
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
static QPainterPath convertFromFreeTypeOutline(FT_GlyphSlotRec *glyphSlot)
{
diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cpp
index 8b4a0b299c..17cdc3712a 100644
--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp
+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp
@@ -535,6 +535,10 @@ void KoSvgTextShape::Private::relayout()
KIS_ASSERT(count <= INT32_MAX);
+ // For detecting ligatures.
+ int previousGlyph = -1;
+ int previousCluster = -1;
+
for (int i = 0; i < static_cast<int>(count); i++) {
raqm_glyph_t currentGlyph = glyphs[i];
KIS_ASSERT(currentGlyph.cluster <= INT32_MAX);
@@ -566,12 +570,28 @@ void KoSvgTextShape::Private::relayout()
continue;
}
+ /// Ligature caret testing. We try to test when there's a gap between clusters and glyphs.
+ /// This gap happens because a single glyph covers multiple clusters.
+ /// We do this because testing for caret positions is surprisingly heavy.
+ if (((cluster - previousCluster) > (i-previousGlyph)) && previousCluster >= 0) {
+ raqm_glyph_t previousGlyph = glyphs[i];
+ result[previousCluster].cursorInfo.offsets = getLigatureCarets(resHandler, isHorizontal, previousGlyph);
+ }
+ // Always test last one.
+ if (i+1 == count) {
+ charResult.cursorInfo.offsets = getLigatureCarets(resHandler, isHorizontal, currentGlyph);
+ }
+
charResult.visualIndex = i;
logicalToVisual.insert(cluster, i);
charResult.middle = false;
result[cluster] = charResult;
+ if (previousCluster != cluster) {
+ previousGlyph = i;
+ previousCluster = cluster;
+ }
}
// fix it so that characters that are in the 'middle' due to either being