]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiFontMetrics.cpp
Make code a bit easier to read
[lyx.git] / src / frontends / qt / GuiFontMetrics.cpp
index cc3a32929bc5299132fb83d80b5eec7f6f233606..0e69ffd998d09dafeb16240ead15c8c013455a80 100644 (file)
@@ -33,7 +33,7 @@
 #if QT_VERSION >= 0x050100
 #include <QtMath>
 #else
-#define qDegreesToRadians(degree) (degree * (M_PI / 180))
+#define qDegreesToRadians(degree) (degree) * (M_PI / 180)
 #endif
 
 using namespace std;
@@ -53,14 +53,6 @@ using namespace lyx::support;
 #endif
 
 
-#if QT_VERSION < 0x050000
-inline uint qHash(double key)
-{
-       return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
-}
-#endif
-
-
 namespace std {
 
 /*
@@ -91,7 +83,9 @@ int const strwidth_cache_max_cost = 1024 * 1024;
 int const breakstr_cache_max_cost = 10 * 1024 * 1024;
 // Qt 5.x already has its own caching of QTextLayout objects
 // but it does not seem to work well on MacOS X.
-#if (QT_VERSION < 0x050000) || defined(Q_OS_MAC)
+#if defined(Q_OS_MAC)
+// For some reason, the built-in cache of QTextLayout does not work or
+// exist on macOS.
 // Limit qtextlayout_cache_ size to 500 elements (we do not know the
 // size of the QTextLayout objects anyway).
 int const qtextlayout_cache_max_size = 500;
@@ -123,7 +117,7 @@ inline QChar const ucs4_to_qchar(char_type const ucs4)
 
 
 GuiFontMetrics::GuiFontMetrics(QFont const & font)
-       : font_(font), metrics_(font, 0),
+       : font_(font), metrics_(font, 0), xheight_(metrics_.xHeight()),
          strwidth_cache_(strwidth_cache_max_cost),
          breakstr_cache_(breakstr_cache_max_cost),
          qtextlayout_cache_(qtextlayout_cache_max_size)
@@ -146,11 +140,6 @@ GuiFontMetrics::GuiFontMetrics(QFont const & font)
                        slope_ = defaultSlope;
                LYXERR(Debug::FONT, "Italic slope: " << slope_);
        }
-       // If those characters have a non-zero width, we need to avoid them.
-       // This happens with Qt4 with monospace fonts
-       needs_naked_ = width(QString() + QChar(0x2060) + QChar(0x202d) + QChar(0x202e)) > 0;
-       // if (needs_naked_)
-       //      LYXERR0("Font " << font.family() << " needs naked text layouts!");
 }
 
 
@@ -178,7 +167,7 @@ int GuiFontMetrics::em() const
 int GuiFontMetrics::xHeight() const
 {
 //     LATTEST(metrics_.xHeight() == ascent('x'));
-       return metrics_.xHeight();
+       return xheight_;
 }
 
 
@@ -268,7 +257,7 @@ int GuiFontMetrics::width(docstring const & s) const
                return *wid_p;
        PROFILE_CACHE_MISS(width);
        /* Several problems have to be taken into account:
-        * * QFontMetrics::width does not returns a wrong value with Qt5 with
+        * * QFontMetrics::width returns a wrong value with Qt5 with
         *   some arabic text, since the glyph-shaping operations are not
         *   done (documented in Qt5).
         * * QTextLayout is broken for single characters with null width
@@ -291,11 +280,7 @@ int GuiFontMetrics::width(docstring const & s) const
        */
        int w = 0;
        // is the string a single character from a math font ?
-#if QT_VERSION >= 0x040800
        bool const math_char = s.length() == 1 && font_.styleName() == "LyX";
-#else
-       bool const math_char = s.length() == 1;
-#endif
        if (math_char) {
                QString const qs = toqstr(s);
                int br_width = metrics_.boundingRect(qs).width();
@@ -353,9 +338,7 @@ struct TextLayoutHelper
        /// Create the helper
        /// \c s is the original string
        /// \c isrtl is true if the string is right-to-left
-       /// \c naked is true to disable the insertion of zero width annotations
-       /// FIXME: remove \c naked argument when Qt4 support goes away.
-       TextLayoutHelper(docstring const & s, bool isrtl, bool naked = false);
+       TextLayoutHelper(docstring const & s, bool isrtl);
 
        /// translate QString index to docstring index
        docstring::size_type qpos2pos(int qpos) const
@@ -379,7 +362,7 @@ private:
 };
 
 
-TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
+TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl)
        : docstr(s), rtl(isrtl)
 {
        // Reserve memory for performance purpose
@@ -395,16 +378,14 @@ TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
         */
        // Unicode character WORD JOINER
        QChar const word_joiner(0x2060);
-       if (!naked)
-               qstr += word_joiner;
+       qstr += word_joiner;
 
 #ifdef BIDI_USE_OVERRIDE
        /* Unicode override characters enforce drawing direction
         * Source: http://www.iamcal.com/understanding-bidirectional-text/
         * Left-to-right override is 0x202d and right-to-left override is 0x202e.
         */
-       if (!naked)
-               qstr += QChar(rtl ? 0x202e : 0x202d);
+       qstr += QChar(rtl ? 0x202e : 0x202d);
 #endif
 
        // Now translate the string character-by-character.
@@ -412,7 +393,7 @@ TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
        for (char_type const c : s) {
                // insert a word joiner character between consecutive spaces
                bool const is_space = isSpace(c);
-               if (!naked && is_space && was_space)
+               if (is_space && was_space)
                        qstr += word_joiner;
                was_space = is_space;
                // Remember the QString index at this point
@@ -425,8 +406,7 @@ TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
        }
 
        // Final word joiner (see above)
-       if (!naked)
-               qstr += word_joiner;
+       qstr += word_joiner;
 
        // Add virtual position at the end of the string
        pos2qpos_.push_back(qstr.size());
@@ -487,7 +467,7 @@ GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
        if (auto ptl = qtextlayout_cache_[key])
                return ptl;
        PROFILE_CACHE_MISS(getTextLayout);
-       TextLayoutHelper tlh(s, rtl, needs_naked_);
+       TextLayoutHelper tlh(s, rtl);
        auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
        qtextlayout_cache_.insert(key, ptl);
        return ptl;
@@ -497,7 +477,7 @@ GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
                           double const wordspacing) const
 {
-       TextLayoutHelper tlh(s, rtl, needs_naked_);
+       TextLayoutHelper tlh(s, rtl);
        auto ptl = getTextLayout(tlh, wordspacing);
        // pos can be negative, see #10506.
        int const qpos = tlh.pos2qpos(max(pos, 0));
@@ -508,7 +488,7 @@ int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
                           double const wordspacing) const
 {
-       TextLayoutHelper tlh(s, rtl, needs_naked_);
+       TextLayoutHelper tlh(s, rtl);
        auto ptl = getTextLayout(tlh, wordspacing);
        QTextLine const & tline = ptl->lineForTextPosition(0);
        int qpos = tline.xToCursor(x);
@@ -546,7 +526,7 @@ FontMetrics::Breaks
 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
                                    bool rtl, bool force) const
 {
-       TextLayoutHelper const tlh(s, rtl, needs_naked_);
+       TextLayoutHelper const tlh(s, rtl);
 
        QTextLayout tl;
 #ifdef BIDI_USE_FLAG