]> git.lyx.org Git - features.git/commitdiff
Refactor GuiFontMetrics::getLayout
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 14 Jul 2022 15:24:33 +0000 (17:24 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 10 Nov 2022 15:13:28 +0000 (16:13 +0100)
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
src/frontends/qt/GuiFontMetrics.h

index 3ee27024eb90132dd79fa3232cac52485489013c..5ee01ec469aa926183853a26cf8220700751cde5 100644 (file)
@@ -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;
 }
index 82c5839e21173a86c4f7effe951fa0be0ed1e5f7..5c32ea9de89237cc9e6822ff50b1cb4ed84c86b9 100644 (file)
@@ -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_;