From 1831f6caaccb7d75e23459fca3b1f7fe9df07766 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Sat, 10 Jul 2021 23:21:27 +0200 Subject: [PATCH] Change FontMetrics::breakAt to return a position 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 | 14 +++++++------- src/frontends/FontMetrics.h | 7 ++++--- src/frontends/qt/GuiFontMetrics.cpp | 12 ++++++------ src/frontends/qt/GuiFontMetrics.h | 2 +- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Row.cpp b/src/Row.cpp index 16ae9aecb6..2f6db3deaf 100644 --- a/src/Row.cpp +++ b/src/Row.cpp @@ -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; } diff --git a/src/frontends/FontMetrics.h b/src/frontends/FontMetrics.h index 2a6ffead8a..b562ebfac1 100644 --- a/src/frontends/FontMetrics.h +++ b/src/frontends/FontMetrics.h @@ -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; /** diff --git a/src/frontends/qt/GuiFontMetrics.cpp b/src/frontends/qt/GuiFontMetrics.cpp index 5e8f53503d..25bf7a4ff7 100644 --- a/src/frontends/qt/GuiFontMetrics.cpp +++ b/src/frontends/qt/GuiFontMetrics.cpp @@ -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(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; } diff --git a/src/frontends/qt/GuiFontMetrics.h b/src/frontends/qt/GuiFontMetrics.h index 9c7ce89ae0..ef8588a5f8 100644 --- a/src/frontends/qt/GuiFontMetrics.h +++ b/src/frontends/qt/GuiFontMetrics.h @@ -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, -- 2.39.2