#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;
using namespace lyx::support;
-/* Define what mechanism is used to enforce text direction. Different
- * methods work with different Qt versions. Here we try to use both
- * methods together.
+/* Define what mechanisms are used to enforce text direction. There
+ * are two methods that work with different Qt versions. Here we try
+ * to use both methods together.
*/
// Define to use unicode override characters to force direction
#define BIDI_USE_OVERRIDE
-// Define to use flag to force direction
+// Define to use QTextLayout flag to force direction
#define BIDI_USE_FLAG
-#ifdef BIDI_USE_OVERRIDE
-# define BIDI_OFFSET 1
-/* Unicode override characters enforce drawing direction
- * Source: http://www.iamcal.com/understanding-bidirectional-text/
- * Right-to-left override is 0x202e and left-to-right override is 0x202d.
- */
-QChar const bidi_override[2] = {0x202d, 0x202e};
-#else
-# define BIDI_OFFSET 0
-#endif
-
#if !defined(BIDI_USE_OVERRIDE) && !defined(BIDI_USE_FLAG)
# error "Define at least one of BIDI_USE_OVERRIDE or BIDI_USE_FLAG"
#endif
-#if QT_VERSION < 0x050000
-inline uint qHash(double key)
-{
- return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
-}
-#endif
-
-
namespace std {
/*
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;
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)
int GuiFontMetrics::xHeight() const
{
// LATTEST(metrics_.xHeight() == ascent('x'));
- return metrics_.xHeight();
+ return xheight_;
}
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
*/
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();
}
-namespace {
-
// This holds a translation table between the original string and the
// QString that we can use with QTextLayout.
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
- 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
};
-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
*/
// Unicode character WORD JOINER
QChar const word_joiner(0x2060);
- if (!naked)
- qstr += word_joiner;
+ qstr += word_joiner;
#ifdef BIDI_USE_OVERRIDE
- if (!naked)
- qstr += bidi_override[rtl];
+ /* 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.
+ */
+ qstr += QChar(rtl ? 0x202e : 0x202d);
#endif
// Now translate the string character-by-character.
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
}
// 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());
//LYXERR0("TLH: " << dump.replace(word_joiner, "|").toStdString());
}
-}
-shared_ptr<QTextLayout const>
-GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
- double const wordspacing) const
+namespace {
+
+shared_ptr<QTextLayout>
+getTextLayout_helper(TextLayoutHelper const & tlh, double const wordspacing,
+ QFont font)
{
- PROFILE_THIS_BLOCK(getTextLayout);
- TextLayoutKey key{s, rtl, wordspacing};
- if (auto ptl = qtextlayout_cache_[key])
- return ptl;
- PROFILE_CACHE_MISS(getTextLayout);
auto const ptl = make_shared<QTextLayout>();
ptl->setCacheEnabled(true);
- QFont copy = font_;
- copy.setWordSpacing(wordspacing);
- ptl->setFont(copy);
-
+ font.setWordSpacing(wordspacing);
+ ptl->setFont(font);
#ifdef BIDI_USE_FLAG
/* Use undocumented flag to enforce drawing direction
* FIXME: This does not work with Qt 5.11 (ticket #11284).
*/
- ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
-#endif
-
-#ifdef BIDI_USE_OVERRIDE
- ptl->setText(bidi_override[rtl] + toqstr(s));
-#else
- ptl->setText(toqstr(s));
+ ptl->setFlags(tlh.rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
#endif
+ ptl->setText(tlh.qstr);
ptl->beginLayout();
ptl->createLine();
ptl->endLayout();
+
+ return ptl;
+}
+
+}
+
+shared_ptr<QTextLayout const>
+GuiFontMetrics::getTextLayout(TextLayoutHelper const & tlh,
+ double const wordspacing) const
+{
+ PROFILE_THIS_BLOCK(getTextLayout_TLH);
+ TextLayoutKey key{tlh.docstr, tlh.rtl, wordspacing};
+ if (auto ptl = qtextlayout_cache_[key])
+ return ptl;
+ PROFILE_CACHE_MISS(getTextLayout_TLH);
+ auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
+ qtextlayout_cache_.insert(key, ptl);
+ return ptl;
+}
+
+
+shared_ptr<QTextLayout const>
+GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
+ double const wordspacing) const
+{
+ PROFILE_THIS_BLOCK(getTextLayout);
+ TextLayoutKey key{s, rtl, wordspacing};
+ if (auto ptl = qtextlayout_cache_[key])
+ return ptl;
+ PROFILE_CACHE_MISS(getTextLayout);
+ TextLayoutHelper tlh(s, rtl);
+ auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
qtextlayout_cache_.insert(key, ptl);
return ptl;
}
int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
double const wordspacing) const
{
- if (pos <= 0)
- pos = 0;
- shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
- /* 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).
- */
- // BIDI_OFFSET accounts for a possible direction override
- // character in front of the string.
- int const qpos = toqstr(s.substr(0, pos)).length() + BIDI_OFFSET;
- return static_cast<int>(tl->lineForTextPosition(qpos).cursorToX(qpos));
+ TextLayoutHelper tlh(s, rtl);
+ auto ptl = getTextLayout(tlh, wordspacing);
+ // pos can be negative, see #10506.
+ int const qpos = tlh.pos2qpos(max(pos, 0));
+ return static_cast<int>(ptl->lineForTextPosition(qpos).cursorToX(qpos));
}
int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
double const wordspacing) const
{
- shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
- QTextLine const & tline = tl->lineForTextPosition(0);
+ TextLayoutHelper tlh(s, rtl);
+ auto ptl = getTextLayout(tlh, wordspacing);
+ QTextLine const & tline = ptl->lineForTextPosition(0);
int qpos = tline.xToCursor(x);
int newx = static_cast<int>(tline.cursorToX(qpos));
// The value of qpos may be wrong in rtl text (see ticket #10569).
// correct x value to the actual cursor position.
x = newx;
- /* 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).
- */
-#if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
- int pos = qstring_to_ucs4(tl->text().left(qpos)).length();
- // there may be a direction override character in front of the string.
- return max(pos - BIDI_OFFSET, 0);
-#else
- /* Due to QTBUG-25536 in 4.8.1 <= Qt < 5.1.0, the string returned
- * by QString::toUcs4 (used by qstring_to_ucs4) may have wrong
- * length. We work around the problem by trying all docstring
- * positions until the right one is found. This is slow only if
- * there are many high-plane Unicode characters. It might be
- * worthwhile to implement a dichotomy search if this shows up
- * under a profiler.
- */
- // there may be a direction override character in front of the string.
- qpos = max(qpos - BIDI_OFFSET, 0);
- int pos = min(qpos, static_cast<int>(s.length()));
- while (pos >= 0 && toqstr(s.substr(0, pos)).length() != qpos)
- --pos;
- LASSERT(pos > 0 || qpos == 0, /**/);
- return pos;
-#endif
+ return tlh.qpos2pos(qpos);
}
QTextLine const & line = tl.lineAt(i);
int const line_epos = line.textStart() + line.textLength();
int const epos = tlh.qpos2pos(line_epos);
-#if QT_VERSION >= 0x050000
// This does not take trailing spaces into account, except for the last line.
int const wid = iround(line.naturalTextWidth());
// If the line is not the last one, trailing space is always omitted.
if (i + 1 == tl.lineCount() && !s.empty() && isSpace(s.back())
&& line.textStart() <= tlh.pos2qpos(s.size() - 1))
nspc_wid = iround(line.cursorToX(tlh.pos2qpos(s.size() - 1)));
-#else
- // With some monospace fonts, the value of horizontalAdvance()
- // can be wrong with Qt4. One hypothesis is that the invisible
- // characters that we use are given a non-null width.
- // FIXME: this is slower than it could be but we'll get rid of Qt4 anyway
- docstring ss = s.substr(pos, epos - pos);
- int const wid = width(ss);
- if (!ss.empty() && isSpace(ss.back()))
- ss.pop_back();
- int const nspc_wid = i + 1 < tl.lineCount() ? width(ss) : wid;
-#endif
breaks.emplace_back(epos - pos, wid, nspc_wid);
pos = epos;
-#if 0
- // FIXME: should it be kept in some form?
- if ((force && line.textLength() == brkStrOffset) || line_wid > x)
- return {-1, line_wid};
-#endif
}
return breaks;