}
+int GuiFontMetrics::xHeight() const
+{
+// LATTEST(metrics_.xHeight() == ascent('x'));
+ return metrics_.xHeight();
+}
+
+
int GuiFontMetrics::lineWidth() const
{
return metrics_.lineWidth();
}
+namespace {
+int const outOfLimitMetric = -10000;
+}
+
+
int GuiFontMetrics::lbearing(char_type c) const
{
- if (!is_utf16(c))
+ int value = lbearing_cache_.value(c, outOfLimitMetric);
+ if (value != outOfLimitMetric)
+ return value;
+
+ if (is_utf16(c))
+ value = metrics_.leftBearing(ucs4_to_qchar(c));
+ else {
// FIXME: QFontMetrics::leftBearing does not support the
// full unicode range. Once it does, we could use:
- //return metrics_.leftBearing(toqstr(docstring(1, c)));
- return 0;
-
- return metrics_.leftBearing(ucs4_to_qchar(c));
-}
+ // metrics_.leftBearing(toqstr(docstring(1, c)));
+ value = 0;
+ }
+ lbearing_cache_.insert(c, value);
-namespace {
-int const outOfLimitMetric = -10000;
+ return value;
}
if (strwidth_cache_.contains(s))
return strwidth_cache_[s];
PROFILE_CACHE_MISS(width);
- /* For some reason QMetrics::width returns a wrong value with Qt5
- * with some arabic text. OTOH, QTextLayout is broken for single
- * characters with null width (like \not in mathed). Also, as a
- * safety measure, always use QMetrics::width with our math fonts.
+ /* Several problems have to be taken into account:
+ * * QFontMetrics::width does not 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
+ * (like \not in mathed).
+ * * While QTextLine::horizontalAdvance is the right thing to use
+ * for text strings, it does not give a good result with some
+ * characters like the \int (gyph 4) of esint.
+
+ * Also, as a safety measure, always use QFontMetrics::width with
+ * our math fonts.
*/
int w = 0;
- if (s.length() == 1
+ // is the string a single character from a math font ?
#if QT_VERSION >= 0x040800
- || font_.styleName() == "LyX"
+ bool const math_char = s.length() == 1 || font_.styleName() == "LyX";
+#else
+ bool const math_char = s.length() == 1;
#endif
- )
- w = metrics_.width(toqstr(s));
- else {
+ // keep value 0 for math chars with width 0
+ if (!math_char || metrics_.width(toqstr(s)) != 0) {
QTextLayout tl;
tl.setText(toqstr(s));
tl.setFont(font_);
tl.beginLayout();
QTextLine line = tl.createLine();
tl.endLayout();
- w = iround(line.horizontalAdvance());
+ if (math_char)
+ w = iround(line.naturalTextWidth());
+ else
+ w = iround(line.horizontalAdvance());
}
strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
return w;
PROFILE_CACHE_MISS(getTextLayout);
auto const ptl = make_shared<QTextLayout>();
ptl->setCacheEnabled(true);
- ptl->setText(toqstr(s));
QFont copy = font_;
copy.setWordSpacing(wordspacing);
ptl->setFont(copy);
+#if 1
+ /* Use unicode override characters to enforce drawing direction
+ * Source: http://www.iamcal.com/understanding-bidirectional-text/
+ */
+ if (rtl)
+ // Right-to-left override: forces to draw text right-to-left
+ ptl->setText(QChar(0x202E) + toqstr(s));
+ else
+ // Left-to-right override: forces to draw text left-to-right
+ ptl->setText(QChar(0x202D) + toqstr(s));
+#define TEXTLAYOUT_OFFSET 1
+#else
+ // FIXME: This does not work with Qt 5.11 (ticket #11284).
// Note that both setFlags and the enums are undocumented
ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
+ ptl->setText(toqstr(s));
+#define TEXTLAYOUT_OFFSET 0
+#endif
ptl->beginLayout();
ptl->createLine();
ptl->endLayout();
* not be the same when there are high-plan unicode characters
* (bug #10443).
*/
- int const qpos = toqstr(s.substr(0, pos)).length();
+ // TEXTLAYOUT_OFFSET accounts for a possible direction override
+ // character in front of the string.
+ int const qpos = toqstr(s.substr(0, pos)).length() + TEXTLAYOUT_OFFSET;
return static_cast<int>(tl->lineForTextPosition(qpos).cursorToX(qpos));
}
* (bug #10443).
*/
#if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
- return qstring_to_ucs4(tl->text().left(qpos)).length();
+ 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 - TEXTLAYOUT_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
* 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 - TEXTLAYOUT_OFFSET, 0);
int pos = min(qpos, static_cast<int>(s.length()));
while (pos >= 0 && toqstr(s.substr(0, pos)).length() != qpos)
--pos;