2 * \file GuiFontMetrics.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "GuiFontMetrics.h"
16 #include "qt_helpers.h"
18 #include "Dimension.h"
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lassert.h"
23 #include "support/lstrings.h" // for breakString_helper with qt4
24 #include "support/lyxlib.h"
26 #define DISABLE_PMPROF
27 #include "support/pmprof.h"
33 #if QT_VERSION >= 0x050100
36 #define qDegreesToRadians(degree) (degree * (M_PI / 180))
40 using namespace lyx::support;
42 /* Define what mechanism is used to enforce text direction. Different
43 * methods work with different Qt versions. Here we try to use both
46 // Define to use unicode override characters to force direction
47 #define BIDI_USE_OVERRIDE
48 // Define to use flag to force direction
51 #ifdef BIDI_USE_OVERRIDE
52 # define BIDI_OFFSET 1
53 /* Unicode override characters enforce drawing direction
54 * Source: http://www.iamcal.com/understanding-bidirectional-text/
55 * Right-to-left override is 0x202e and left-to-right override is 0x202d.
57 QChar const bidi_override[2] = {0x202d, 0x202e};
59 # define BIDI_OFFSET 0
62 #if !defined(BIDI_USE_OVERRIDE) && !defined(BIDI_USE_FLAG)
63 # error "Define at least one of BIDI_USE_OVERRIDE or BIDI_USE_FLAG"
67 #if QT_VERSION < 0x050000
68 inline uint qHash(double key)
70 return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
78 * Argument-dependent lookup implies that this function shall be
79 * declared in the namespace of its argument. But this is std
80 * namespace, since lyx::docstring is just std::basic_string<wchar_t>.
82 uint qHash(lyx::docstring const & s)
84 return qHash(QByteArray(reinterpret_cast<char const *>(s.data()),
85 s.size() * sizeof(lyx::docstring::value_type)));
95 // Maximal size/cost for various caches. See QCache documentation to
96 // see what cost means.
98 // Limit strwidth_cache_ total cost to 1MB of string data.
99 int const strwidth_cache_max_cost = 1024 * 1024;
100 // Limit breakstr_cache_ total cost to 10MB of string data.
101 // This is useful for documents with very large insets.
102 int const breakstr_cache_max_cost = 10 * 1024 * 1024;
103 // Qt 5.x already has its own caching of QTextLayout objects
104 // but it does not seem to work well on MacOS X.
105 #if (QT_VERSION < 0x050000) || defined(Q_OS_MAC)
106 // Limit qtextlayout_cache_ size to 500 elements (we do not know the
107 // size of the QTextLayout objects anyway).
108 int const qtextlayout_cache_max_size = 500;
111 int const qtextlayout_cache_max_size = 0;
116 * Convert a UCS4 character into a QChar.
117 * This is a hack (it does only make sense for the common part of the UCS4
118 * and UTF16 encodings) and should not be used.
119 * This does only exist because of performance reasons (a real conversion
120 * using iconv is too slow on windows).
122 * This is no real conversion but a simple cast in reality. This is the reason
123 * why this works well for symbol fonts used in mathed too, even though
124 * these are not real ucs4 characters. These are codepoints in the
125 * computer modern fonts used, nothing unicode related.
126 * See comment in GuiPainter::text() for more explanation.
128 inline QChar const ucs4_to_qchar(char_type const ucs4)
130 LATTEST(is_utf16(ucs4));
131 return QChar(static_cast<unsigned short>(ucs4));
136 GuiFontMetrics::GuiFontMetrics(QFont const & font)
137 : font_(font), metrics_(font, 0),
138 strwidth_cache_(strwidth_cache_max_cost),
139 breakstr_cache_(breakstr_cache_max_cost),
140 qtextlayout_cache_(qtextlayout_cache_max_size)
142 // Determine italic slope
143 double const defaultSlope = tan(qDegreesToRadians(19.0));
144 QRawFont raw = QRawFont::fromFont(font);
145 QByteArray post(raw.fontTable("post"));
146 if (post.length() == 0) {
147 slope_ = defaultSlope;
148 LYXERR(Debug::FONT, "Screen font doesn't have 'post' table.");
150 // post table description:
151 // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html
152 int32_t italicAngle = qFromBigEndian(*reinterpret_cast<int32_t *>(post.data() + 4));
153 double angle = italicAngle / 65536.0; // Fixed-point 16.16 to floating-point
154 slope_ = -tan(qDegreesToRadians(angle));
155 // Correct italic fonts with zero slope
156 if (slope_ == 0.0 && font.italic())
157 slope_ = defaultSlope;
158 LYXERR(Debug::FONT, "Italic slope: " << slope_);
163 int GuiFontMetrics::maxAscent() const
165 return metrics_.ascent();
169 int GuiFontMetrics::maxDescent() const
171 // We add 1 as the value returned by QT is different than X
172 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
174 return metrics_.descent() + 1;
178 int GuiFontMetrics::em() const
180 return QFontInfo(font_).pixelSize();
184 int GuiFontMetrics::xHeight() const
186 // LATTEST(metrics_.xHeight() == ascent('x'));
187 return metrics_.xHeight();
191 int GuiFontMetrics::lineWidth() const
193 return metrics_.lineWidth();
197 int GuiFontMetrics::underlinePos() const
199 return metrics_.underlinePos();
203 int GuiFontMetrics::strikeoutPos() const
205 return metrics_.strikeOutPos();
209 bool GuiFontMetrics::italic() const
211 return font_.italic();
215 double GuiFontMetrics::italicSlope() const
222 int const outOfLimitMetric = -10000;
226 int GuiFontMetrics::lbearing(char_type c) const
228 int value = lbearing_cache_.value(c, outOfLimitMetric);
229 if (value != outOfLimitMetric)
233 value = metrics_.leftBearing(ucs4_to_qchar(c));
235 // FIXME: QFontMetrics::leftBearing does not support the
236 // full unicode range. Once it does, we could use:
237 // metrics_.leftBearing(toqstr(docstring(1, c)));
241 lbearing_cache_.insert(c, value);
247 int GuiFontMetrics::rbearing(char_type c) const
249 int value = rbearing_cache_.value(c, outOfLimitMetric);
250 if (value != outOfLimitMetric)
253 // Qt rbearing is from the right edge of the char's width().
255 QChar sc = ucs4_to_qchar(c);
256 value = width(c) - metrics_.rightBearing(sc);
258 // FIXME: QFontMetrics::leftBearing does not support the
259 // full unicode range. Once it does, we could use:
260 // metrics_.rightBearing(toqstr(docstring(1, c)));
264 rbearing_cache_.insert(c, value);
270 int GuiFontMetrics::width(docstring const & s) const
272 PROFILE_THIS_BLOCK(width);
273 if (int * wid_p = strwidth_cache_.object_ptr(s))
275 PROFILE_CACHE_MISS(width);
276 /* Several problems have to be taken into account:
277 * * QFontMetrics::width does not returns a wrong value with Qt5 with
278 * some arabic text, since the glyph-shaping operations are not
279 * done (documented in Qt5).
280 * * QTextLayout is broken for single characters with null width
281 * (like \not in mathed).
282 * * While QTextLine::horizontalAdvance is the right thing to use
283 * for text strings, it does not give a good result with some
284 * characters like the \int (gyph 4) of esint.
286 * The metrics of some of our math fonts (eg. esint) are such that
287 * QTextLine::horizontalAdvance leads, more or less, in the middle
288 * of a symbol. This is the horizontal position where a subscript
289 * should be drawn, so that the superscript has to be moved rightward.
290 * This is done when the kerning() method of the math insets returns
291 * a positive value. The problem with this choice is that navigating
292 * a formula becomes weird. For example, a selection extends only over
293 * about half of the symbol. In order to avoid this, with our math
294 * fonts we use QTextLine::naturalTextWidth, so that a superscript can
295 * be drawn right after the symbol, and move the subscript leftward by
296 * recording a negative value for the kerning.
299 // is the string a single character from a math font ?
300 #if QT_VERSION >= 0x040800
301 bool const math_char = s.length() == 1 && font_.styleName() == "LyX";
303 bool const math_char = s.length() == 1;
306 QString const qs = toqstr(s);
307 int br_width = metrics_.boundingRect(qs).width();
308 #if QT_VERSION >= 0x050b00
309 int s_width = metrics_.horizontalAdvance(qs);
311 int s_width = metrics_.width(qs);
313 // keep value 0 for math chars with width 0
315 w = max(br_width, s_width);
318 tl.setText(toqstr(s));
321 QTextLine line = tl.createLine();
323 w = iround(line.horizontalAdvance());
325 strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
330 int GuiFontMetrics::width(QString const & ucs2) const
332 return width(qstring_to_ucs4(ucs2));
336 int GuiFontMetrics::signedWidth(docstring const & s) const
342 return -width(s.substr(1, s.size() - 1));
348 uint qHash(TextLayoutKey const & key)
350 double params = (2 * key.rtl - 1) * key.ws;
351 return std::qHash(key.s) ^ ::qHash(params);
357 // This holds a translation table between the original string and the
358 // QString that we can use with QTextLayout.
359 struct TextLayoutHelper
361 /// Create the helper
362 /// \c s is the original string
363 /// \c isrtl is true if the string is right-to-left
364 /// \c naked is true to disable the insertion of zero width annotations
365 TextLayoutHelper(docstring const & s, bool isrtl, bool naked = false);
367 /// translate QString index to docstring index
368 docstring::size_type qpos2pos(int qpos) const
370 return lower_bound(pos2qpos_.begin(), pos2qpos_.end(), qpos) - pos2qpos_.begin();
373 /// Translate docstring index to QString index
374 int pos2qpos(docstring::size_type pos) const { return pos2qpos_[pos]; }
376 // The original string
380 // is string right-to-left?
384 // This vector contains the QString pos for each string position
385 vector<int> pos2qpos_;
389 TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
390 : docstr(s), rtl(isrtl)
392 // Reserve memory for performance purpose
393 pos2qpos_.reserve(s.size());
394 qstr.reserve(2 * s.size());
396 /* Qt will not break at a leading or trailing space, and we need
397 * that sometimes, see http://www.lyx.org/trac/ticket/9921.
399 * To work around the problem, we enclose the string between
400 * word joiner characters so that the QTextLayout algorithm will
401 * agree to break the text at these extremal spaces.
403 // Unicode character WORD JOINER
404 QChar const word_joiner(0x2060);
408 #ifdef BIDI_USE_OVERRIDE
410 qstr += bidi_override[rtl];
413 // Now translate the string character-by-character.
414 for (char_type const c : s) {
415 // Remember the QString index at this point
416 pos2qpos_.push_back(qstr.size());
417 // Performance: UTF-16 characters are easier
419 qstr += ucs4_to_qchar(c);
424 // Final word joiner (see above)
428 // Add virtual position at the end of the string
429 pos2qpos_.push_back(qstr.size());
431 //QString dump = qstr;
432 //LYXERR0("TLH: " << dump.replace(word_joiner, "|").toStdString());
437 shared_ptr<QTextLayout const>
438 GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
439 double const wordspacing) const
441 PROFILE_THIS_BLOCK(getTextLayout);
442 TextLayoutKey key{s, rtl, wordspacing};
443 if (auto ptl = qtextlayout_cache_[key])
445 PROFILE_CACHE_MISS(getTextLayout);
446 auto const ptl = make_shared<QTextLayout>();
447 ptl->setCacheEnabled(true);
449 copy.setWordSpacing(wordspacing);
453 /* Use undocumented flag to enforce drawing direction
454 * FIXME: This does not work with Qt 5.11 (ticket #11284).
456 ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
459 #ifdef BIDI_USE_OVERRIDE
460 ptl->setText(bidi_override[rtl] + toqstr(s));
462 ptl->setText(toqstr(s));
468 qtextlayout_cache_.insert(key, ptl);
473 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
474 double const wordspacing) const
478 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
479 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
480 * not be the same when there are high-plan unicode characters
483 // BIDI_OFFSET accounts for a possible direction override
484 // character in front of the string.
485 int const qpos = toqstr(s.substr(0, pos)).length() + BIDI_OFFSET;
486 return static_cast<int>(tl->lineForTextPosition(qpos).cursorToX(qpos));
490 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
491 double const wordspacing) const
493 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
494 QTextLine const & tline = tl->lineForTextPosition(0);
495 int qpos = tline.xToCursor(x);
496 int newx = static_cast<int>(tline.cursorToX(qpos));
497 // The value of qpos may be wrong in rtl text (see ticket #10569).
498 // To work around this, let's have a look at adjacent positions to
499 // see whether we find closer matches.
500 if (rtl && newx < x) {
502 int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
503 if (abs(xm - x) < abs(newx - x)) {
509 } else if (rtl && newx > x) {
510 while (qpos < tline.textLength()) {
511 int const xp = static_cast<int>(tline.cursorToX(qpos + 1));
512 if (abs(xp - x) < abs(newx - x)) {
519 // correct x value to the actual cursor position.
522 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
523 * not be the same when there are high-plan unicode characters
526 #if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
527 int pos = qstring_to_ucs4(tl->text().left(qpos)).length();
528 // there may be a direction override character in front of the string.
529 return max(pos - BIDI_OFFSET, 0);
531 /* Due to QTBUG-25536 in 4.8.1 <= Qt < 5.1.0, the string returned
532 * by QString::toUcs4 (used by qstring_to_ucs4) may have wrong
533 * length. We work around the problem by trying all docstring
534 * positions until the right one is found. This is slow only if
535 * there are many high-plane Unicode characters. It might be
536 * worthwhile to implement a dichotomy search if this shows up
539 // there may be a direction override character in front of the string.
540 qpos = max(qpos - BIDI_OFFSET, 0);
541 int pos = min(qpos, static_cast<int>(s.length()));
542 while (pos >= 0 && toqstr(s.substr(0, pos)).length() != qpos)
544 LASSERT(pos > 0 || qpos == 0, /**/);
551 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
552 bool rtl, bool force) const
554 TextLayoutHelper const tlh(s, rtl);
558 /* Use undocumented flag to enforce drawing direction
559 * FIXME: This does not work with Qt 5.11 (ticket #11284).
561 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
563 tl.setText(tlh.qstr);
567 * Some Asian languages split lines anywhere (no notion of
568 * word). It seems that QTextLayout is not aware of this fact.
570 * https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
572 * FIXME: Something shall be done about characters which are
573 * not allowed at the beginning or end of line.
575 to.setWrapMode(force ? QTextOption::WrapAtWordBoundaryOrAnywhere
576 : QTextOption::WordWrap);
577 tl.setTextOption(to);
582 QTextLine line = tl.createLine();
585 line.setLineWidth(first ? first_wid : wid);
592 for (int i = 0 ; i < tl.lineCount() ; ++i) {
593 QTextLine const & line = tl.lineAt(i);
594 int const line_epos = line.textStart() + line.textLength();
595 int const epos = tlh.qpos2pos(line_epos);
596 #if QT_VERSION >= 0x050000
597 // This does not take trailing spaces into account, except for the last line.
598 int const wid = iround(line.naturalTextWidth());
599 // If the line is not the last one, trailing space is always omitted.
601 // For the last line, compute the width without trailing space
602 if (i + 1 == tl.lineCount()) {
603 // trim_pos points to the last character that is not a space
604 auto trim_pos = s.find_last_not_of(from_ascii(" "));
605 if (trim_pos == docstring::npos)
607 else if (trim_pos + 1 < s.length()) {
608 int const num_spaces = s.length() - trim_pos - 1;
609 // find the position on the line before trailing
610 // spaces. Remove 1 to account for the ending
611 // non-breaking space of qs.
612 nspc_wid = iround(line.cursorToX(line_epos - num_spaces - 1));
616 // With some monospace fonts, the value of horizontalAdvance()
617 // can be wrong with Qt4. One hypothesis is that the invisible
618 // characters that we use are given a non-null width.
619 // FIXME: this is slower than it could be but we'll get rid of Qt4 anyway
620 docstring const ss = s.substr(pos, epos - pos);
621 int const wid = width(ss);
622 int const nspc_wid = i + 1 < tl.lineCount() ? width(rtrim(ss)) : wid;
624 breaks.emplace_back(epos - pos, wid, nspc_wid);
627 // FIXME: should it be kept in some form?
628 if ((force && line.textLength() == brkStrOffset) || line_wid > x)
629 return {-1, line_wid};
637 uint qHash(BreakStringKey const & key)
639 // assume widths are less than 10000. This fits in 32 bits.
640 uint params = key.force + 2 * key.rtl + 4 * key.first_wid + 10000 * key.wid;
641 return std::qHash(key.s) ^ ::qHash(params);
645 FontMetrics::Breaks GuiFontMetrics::breakString(docstring const & s, int first_wid, int wid,
646 bool rtl, bool force) const
648 PROFILE_THIS_BLOCK(breakString);
652 BreakStringKey key{s, first_wid, wid, rtl, force};
654 if (auto * brks_ptr = breakstr_cache_.object_ptr(key))
657 PROFILE_CACHE_MISS(breakString);
658 brks = breakString_helper(s, first_wid, wid, rtl, force);
659 breakstr_cache_.insert(key, brks, sizeof(key) + s.size() * sizeof(char_type));
665 void GuiFontMetrics::rectText(docstring const & str,
666 int & w, int & ascent, int & descent) const
668 // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
669 int const offset = 4;
671 w = width(str) + offset;
672 ascent = metrics_.ascent() + offset / 2;
673 descent = metrics_.descent() + offset / 2;
677 void GuiFontMetrics::buttonText(docstring const & str, const int offset,
678 int & w, int & ascent, int & descent) const
680 rectText(str, w, ascent, descent);
685 Dimension const GuiFontMetrics::defaultDimension() const
687 return Dimension(0, maxAscent(), maxDescent());
691 Dimension const GuiFontMetrics::dimension(char_type c) const
693 return Dimension(width(c), ascent(c), descent(c));
697 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
702 r = metrics_.boundingRect(ucs4_to_qchar(c));
704 r = metrics_.boundingRect(toqstr(docstring(1, c)));
706 AscendDescend ad = { -r.top(), r.bottom() + 1};
707 // We could as well compute the width but this is not really
708 // needed for now as it is done directly in width() below.
709 metrics_cache_.insert(c, ad);
715 int GuiFontMetrics::width(char_type c) const
717 int value = width_cache_.value(c, outOfLimitMetric);
718 if (value != outOfLimitMetric)
721 #if QT_VERSION >= 0x050b00
723 value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
725 value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
728 value = metrics_.width(ucs4_to_qchar(c));
730 value = metrics_.width(toqstr(docstring(1, c)));
733 width_cache_.insert(c, value);
739 int GuiFontMetrics::ascent(char_type c) const
741 static AscendDescend const outOfLimitAD =
742 {outOfLimitMetric, outOfLimitMetric};
743 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
744 if (value.ascent != outOfLimitMetric)
747 value = fillMetricsCache(c);
752 int GuiFontMetrics::descent(char_type c) const
754 static AscendDescend const outOfLimitAD =
755 {outOfLimitMetric, outOfLimitMetric};
756 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
757 if (value.descent != outOfLimitMetric)
758 return value.descent;
760 value = fillMetricsCache(c);
761 return value.descent;
764 } // namespace frontend