]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontMetrics.cpp
Amend f441590c
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
index 8d0b0269980c9a79a5be70bc2732103aaa751b9e..c58be2aee3c60c83475f199040288010bce52e99 100644 (file)
@@ -23,8 +23,6 @@
 
 #include "support/lassert.h"
 
-#include <QTextLayout>
-
 using namespace std;
 using namespace lyx::support;
 
@@ -56,7 +54,8 @@ inline QChar const ucs4_to_qchar(char_type const ucs4)
 // Limit strwidth_cache_ size to 512kB of string data
 GuiFontMetrics::GuiFontMetrics(QFont const & font)
        : font_(font), metrics_(font, 0),
-         strwidth_cache_(1 << 19)
+         strwidth_cache_(1 << 19),
+         tl_cache_rtl_(false), tl_cache_wordspacing_(-1.0)
 {
 }
 
@@ -170,29 +169,36 @@ int GuiFontMetrics::signedWidth(docstring const & s) const
                return width(s);
 }
 
-namespace {
-void setTextLayout(QTextLayout & tl, docstring const & s, QFont font,
-                   bool const rtl, double const wordspacing)
+
+QTextLayout const &
+GuiFontMetrics::getTextLayout(docstring const & s, QFont font,
+                              bool const rtl, double const wordspacing) const
 {
-       tl.setText(toqstr(s));
-       font.setWordSpacing(wordspacing);
-       tl.setFont(font);
-       // Note that both setFlags and the enums are undocumented
-       tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
-       tl.beginLayout();
-       tl.createLine();
-       tl.endLayout();
-}
+       if (s != tl_cache_s_ || font != tl_cache_font_ || rtl != tl_cache_rtl_
+           || wordspacing != tl_cache_wordspacing_) {
+               tl_cache_.setText(toqstr(s));
+               font.setWordSpacing(wordspacing);
+               tl_cache_.setFont(font);
+               // Note that both setFlags and the enums are undocumented
+               tl_cache_.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
+               tl_cache_.beginLayout();
+               tl_cache_.createLine();
+               tl_cache_.endLayout();
+               tl_cache_s_ = s;
+               tl_cache_font_ = font;
+               tl_cache_rtl_ = rtl;
+               tl_cache_wordspacing_ = wordspacing;
+       }
+       return tl_cache_;
 }
 
 
 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
                           double const wordspacing) const
 {
-       QTextLayout tl;
        QFont copy = font_;
        copy.setWordSpacing(wordspacing);
-       setTextLayout(tl, s, font_, rtl, wordspacing);
+       QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
        return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
 }
 
@@ -200,8 +206,7 @@ int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
                           double const wordspacing) const
 {
-       QTextLayout tl;
-       setTextLayout(tl, s, font_, rtl, wordspacing);
+       QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
        int pos = tl.lineForTextPosition(0).xToCursor(x);
        // correct x value to the actual cursor position.
        x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));