[LyX/master] Refactor GuiFontMetrics::getLayout

Jean-Marc Lasgouttes lasgouttes at lyx.org
Wed Nov 16 10:16:13 UTC 2022


commit 7fdc6046069295b0993a99e34cc3d29fa1224fd7
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Thu Jul 14 17:24:33 2022 +0200

    Refactor GuiFontMetrics::getLayout
    
    This allows to create a new version that takes an already built
    TextLayoutHelper struct as parameter.
    
    No intended change.
    
    See discussion in bug #10117.
---
 src/frontends/qt/GuiFontMetrics.cpp |   63 ++++++++++++++++++++++------------
 src/frontends/qt/GuiFontMetrics.h   |    9 ++++-
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/src/frontends/qt/GuiFontMetrics.cpp b/src/frontends/qt/GuiFontMetrics.cpp
index 3ee2702..5ee01ec 100644
--- a/src/frontends/qt/GuiFontMetrics.cpp
+++ b/src/frontends/qt/GuiFontMetrics.cpp
@@ -352,8 +352,6 @@ uint qHash(TextLayoutKey const & key)
 }
 
 
-namespace {
-
 // This holds a translation table between the original string and the
 // QString that we can use with QTextLayout.
 struct TextLayoutHelper
@@ -438,39 +436,60 @@ TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
 	//LYXERR0("TLH: " << dump.replace(word_joiner, "|").toStdString());
 }
 
-}
 
-shared_ptr<QTextLayout const>
-GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
-                              double const wordspacing) const
+namespace {
+
+shared_ptr<QTextLayout>
+getTextLayout_helper(TextLayoutHelper const & tlh, double const wordspacing,
+                     QFont font)
 {
-	PROFILE_THIS_BLOCK(getTextLayout);
-	TextLayoutKey key{s, rtl, wordspacing};
-	if (auto ptl = qtextlayout_cache_[key])
-		return ptl;
-	PROFILE_CACHE_MISS(getTextLayout);
 	auto const ptl = make_shared<QTextLayout>();
 	ptl->setCacheEnabled(true);
-	QFont copy = font_;
-	copy.setWordSpacing(wordspacing);
-	ptl->setFont(copy);
-
+	font.setWordSpacing(wordspacing);
+	ptl->setFont(font);
 #ifdef BIDI_USE_FLAG
 	/* Use undocumented flag to enforce drawing direction
 	 * FIXME: This does not work with Qt 5.11 (ticket #11284).
 	 */
-	ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
-#endif
-
-#ifdef BIDI_USE_OVERRIDE
-	ptl->setText(bidi_override[rtl] + toqstr(s));
-#else
-	ptl->setText(toqstr(s));
+	ptl->setFlags(tlh.rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
 #endif
+	ptl->setText(tlh.qstr);
 
 	ptl->beginLayout();
 	ptl->createLine();
 	ptl->endLayout();
+
+	return ptl;
+}
+
+}
+
+shared_ptr<QTextLayout const>
+GuiFontMetrics::getTextLayout(TextLayoutHelper const & tlh,
+                              double const wordspacing) const
+{
+	PROFILE_THIS_BLOCK(getTextLayout_TLH);
+	TextLayoutKey key{tlh.docstr, tlh.rtl, wordspacing};
+	if (auto ptl = qtextlayout_cache_[key])
+		return ptl;
+	PROFILE_CACHE_MISS(getTextLayout_TLH);
+	auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
+	qtextlayout_cache_.insert(key, ptl);
+	return ptl;
+}
+
+
+shared_ptr<QTextLayout const>
+GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
+                              double const wordspacing) const
+{
+	PROFILE_THIS_BLOCK(getTextLayout);
+	TextLayoutKey key{s, rtl, wordspacing};
+	if (auto ptl = qtextlayout_cache_[key])
+		return ptl;
+	PROFILE_CACHE_MISS(getTextLayout);
+	TextLayoutHelper tlh(s, rtl);
+	auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
 	qtextlayout_cache_.insert(key, ptl);
 	return ptl;
 }
diff --git a/src/frontends/qt/GuiFontMetrics.h b/src/frontends/qt/GuiFontMetrics.h
index 82c5839..5c32ea9 100644
--- a/src/frontends/qt/GuiFontMetrics.h
+++ b/src/frontends/qt/GuiFontMetrics.h
@@ -27,6 +27,8 @@
 namespace lyx {
 namespace frontend {
 
+struct TextLayoutHelper;
+
 struct BreakStringKey
 {
 	bool operator==(BreakStringKey const & key) const {
@@ -97,14 +99,17 @@ public:
 
 	/// Return a pointer to a cached QTextLayout object
 	std::shared_ptr<QTextLayout const>
-	getTextLayout(docstring const & s, bool const rtl,
-	              double const wordspacing) const;
+	getTextLayout(docstring const & s, bool const rtl, double const wordspacing) const;
 
 private:
 
 	Breaks breakString_helper(docstring const & s, int first_wid, int wid,
 	                          bool rtl, bool force) const;
 
+	/// A different version of getTextLayout for internal use
+	std::shared_ptr<QTextLayout const>
+	getTextLayout(TextLayoutHelper const & tlh, double const wordspacing) const;
+
 	/// The font
 	QFont font_;
 


More information about the lyx-cvs mailing list