Fix computation of string width when using a QTextLayout

It was not a good idea to rely on QTextLine::naturalTextWidth() to
compute a string width. The correct method is horizontalAdvance().

Also round the value to the nearest pixel, since this is what
QFontMetrics::width() does.

By contrast with the code in 2.3.x/master, this code had to be adapted
for Qt < 4.7, where horizontalAdvance() is not defined and
naturalTextWidth() has to be used instead. The fix is thus only
effective starting from Qt 4.7.

Fixes bug #10700 (and maybe others).

(cherry picked from commit c874641e95)
This commit is contained in:
Jean-Marc Lasgouttes 2017-08-28 12:05:35 +02:00
parent 6b0509da0c
commit 48904cc5a1
2 changed files with 16 additions and 5 deletions

View File

@ -23,6 +23,7 @@
#include "support/convert.h" #include "support/convert.h"
#include "support/lassert.h" #include "support/lassert.h"
#include "support/lyxlib.h"
#include <QByteArray> #include <QByteArray>
@ -198,7 +199,11 @@ int GuiFontMetrics::width(docstring const & s) const
tl.beginLayout(); tl.beginLayout();
QTextLine line = tl.createLine(); QTextLine line = tl.createLine();
tl.endLayout(); tl.endLayout();
w = int(line.naturalTextWidth()); #if QT_VERSION >= 0x040700
w = iround(line.horizontalAdvance());
#else
w = iround(line.naturalTextWidth());
#endif
} }
strwidth_cache_.insert(s, w, s.size() * sizeof(char_type)); strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
return w; return w;
@ -363,7 +368,12 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
line.setLineWidth(x); line.setLineWidth(x);
tl.createLine(); tl.createLine();
tl.endLayout(); tl.endLayout();
if ((force && line.textLength() == offset) || int(line.naturalTextWidth()) > x) #if QT_VERSION >= 0x040700
int const line_wid = iround(line.horizontalAdvance());
#else
int const line_wid = iround(line.naturalTextWidth());
#endif
if ((force && line.textLength() == offset) || line_wid > x)
return make_pair(-1, -1); return make_pair(-1, -1);
/* Since QString is UTF-16 and docstring is UCS-4, the offsets may /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
* not be the same when there are high-plan unicode characters * not be the same when there are high-plan unicode characters
@ -388,8 +398,7 @@ GuiFontMetrics::breakAt_helper(docstring const & s, int const x,
--len; --len;
LASSERT(len > 0 || qlen == 0, /**/); LASSERT(len > 0 || qlen == 0, /**/);
#endif #endif
// The -1 is here to account for the leading zerow_nbsp. return make_pair(len, line_wid);
return make_pair(len, int(line.naturalTextWidth()));
} }

View File

@ -101,6 +101,8 @@ What's new
- Fix hole in selection for some zoom and justification values (bug - Fix hole in selection for some zoom and justification values (bug
8883). 8883).
- Fix some rare cases of bad on-screen line breaking (bug #10700).
- Fix cursor state after double/triple click in mathed (bug #10686). - Fix cursor state after double/triple click in mathed (bug #10686).
- Avoid a case of stuck cursor after entering an inset (bug 10630). - Avoid a case of stuck cursor after entering an inset (bug 10630).