]> git.lyx.org Git - lyx.git/commitdiff
Change FontMetrics::breakAt to return a position
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Sat, 10 Jul 2021 21:21:27 +0000 (23:21 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 7 Dec 2021 16:04:46 +0000 (17:04 +0100)
Since we intend to break the row element in two, it is not good to
truncate the string too early.

Moreover, the row element width is now set at this point, even if no
breaking occurs.

src/Row.cpp
src/frontends/FontMetrics.h
src/frontends/qt/GuiFontMetrics.cpp
src/frontends/qt/GuiFontMetrics.h

index 16ae9aecb6e0f988535493cd8f5841f2ef2e039a..2f6db3deaf35b22af36753721a29f00d0b74b215 100644 (file)
@@ -132,19 +132,19 @@ pos_type Row::Element::x2pos(int &x) const
 
 bool Row::Element::breakAt(int w, bool force)
 {
-       if (type != STRING || dim.wid <= w)
+       if (type != STRING)
                return false;
 
        FontMetrics const & fm = theFontMetrics(font);
-       int x = w;
-       if(fm.breakAt(str, x, isRTL(), force)) {
-               dim.wid = x;
-               endpos = pos + str.length();
+       dim.wid = w;
+       int const i = fm.breakAt(str, dim.wid, isRTL(), force);
+       if (i != -1) {
+               str.erase(i);
+               endpos = pos + i;
                //lyxerr << "breakAt(" << w << ")  Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
-               return true;
        }
 
-       return false;
+       return i != - 1;
 }
 
 
index 2a6ffead8a9043771dbb9c4147b7aae625230a33..b562ebfac18fa03ee1042d0fb9951c9895ac8659 100644 (file)
@@ -122,13 +122,14 @@ public:
         */
        virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const = 0;
        /**
-        * Break string at width at most x.
-        * \return true if successful
+        * Break string s at width at most x.
+        * \return break position (-1 if not successful)
+        * \param position x is updated to real width
         * \param rtl is true for right-to-left layout
         * \param force is false for breaking at word separator, true for
         *   arbitrary position.
         */
-       virtual bool breakAt(docstring & s, int & x, bool rtl, bool force) const = 0;
+       virtual int breakAt(docstring const & s, int & x, bool rtl, bool force) const = 0;
        /// return char dimension for the font.
        virtual Dimension const dimension(char_type c) const = 0;
        /**
index 5e8f53503db58bde0372cb10734ec6fad91c2ac8..25bf7a4ff7be287085b477e7dab7f0ecdf22fac6 100644 (file)
@@ -533,7 +533,7 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
        tl.endLayout();
        int const line_wid = iround(line.horizontalAdvance());
        if ((force && line.textLength() == offset) || line_wid > x)
-               return {-1, -1};
+               return {-1, line_wid};
        /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
         * not be the same when there are high-plan unicode characters
         * (bug #10443).
@@ -557,6 +557,9 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
                --len;
        LASSERT(len > 0 || qlen == 0, /**/);
 #endif
+       // si la chaîne est déjà trop courte, on ne coupe pas
+       if (len == static_cast<int>(s.length()))
+               len = -1;
        return {len, line_wid};
 }
 
@@ -568,7 +571,7 @@ uint qHash(BreakAtKey const & key)
 }
 
 
-bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
+int GuiFontMetrics::breakAt(docstring const & s, int & x, bool const rtl, bool const force) const
 {
        PROFILE_THIS_BLOCK(breakAt);
        if (s.empty())
@@ -583,11 +586,8 @@ bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const
                pp = breakAt_helper(s, x, rtl, force);
                breakat_cache_.insert(key, pp, sizeof(key) + s.size() * sizeof(char_type));
        }
-       if (pp.first == -1)
-               return false;
-       s = s.substr(0, pp.first);
        x = pp.second;
-       return true;
+       return pp.first;
 }
 
 
index 9c7ce89ae02fdf3c12d1203bf75dad6cd939cddd..ef8588a5f81a57ad83688b0dc282ef09e9c506ff 100644 (file)
@@ -77,7 +77,7 @@ public:
        int signedWidth(docstring const & s) const override;
        int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
        int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
-       bool breakAt(docstring & s, int & x, bool rtl, bool force) const override;
+       int breakAt(docstring const & s, int & x, bool rtl, bool force) const override;
        Dimension const dimension(char_type c) const override;
 
        void rectText(docstring const & str,