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/lassert.h"
22 #include "support/lyxlib.h"
23 #include "support/debug.h"
25 #define DISABLE_PMPROF
26 #include "support/pmprof.h"
32 #if QT_VERSION >= 0x050100
35 #define qDegreesToRadians(degree) (degree * (M_PI / 180))
39 using namespace lyx::support;
41 /* Define what mechanism is used to enforce text direction. Different
42 * methods work with different Qt versions. Here we try to use both
45 // Define to use unicode override characters to force direction
46 #define BIDI_USE_OVERRIDE
47 // Define to use flag to force direction
50 #ifdef BIDI_USE_OVERRIDE
51 # define BIDI_OFFSET 1
53 # define BIDI_OFFSET 0
56 #if !defined(BIDI_USE_OVERRIDE) && !defined(BIDI_USE_FLAG)
57 # error "Define at least one of BIDI_USE_OVERRIDE or BIDI_USE_FLAG"
61 #if QT_VERSION < 0x050000
62 inline uint qHash(double key)
64 return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
72 * Argument-dependent lookup implies that this function shall be
73 * declared in the namespace of its argument. But this is std
74 * namespace, since lyx::docstring is just std::basic_string<wchar_t>.
76 uint qHash(lyx::docstring const & s)
78 return qHash(QByteArray(reinterpret_cast<char const *>(s.data()),
79 s.size() * sizeof(lyx::docstring::value_type)));
89 // Maximal size/cost for various caches. See QCache documentation to
90 // see what cost means.
92 // Limit strwidth_cache_ total cost to 1MB of string data.
93 int const strwidth_cache_max_cost = 1024 * 1024;
94 // Limit breakstr_cache_ total cost to 10MB of string data.
95 // This is useful for documents with very large insets.
96 int const breakstr_cache_max_cost = 10 * 1024 * 1024;
97 // Qt 5.x already has its own caching of QTextLayout objects
98 // but it does not seem to work well on MacOS X.
99 #if (QT_VERSION < 0x050000) || defined(Q_OS_MAC)
100 // Limit qtextlayout_cache_ size to 500 elements (we do not know the
101 // size of the QTextLayout objects anyway).
102 int const qtextlayout_cache_max_size = 500;
105 int const qtextlayout_cache_max_size = 0;
110 * Convert a UCS4 character into a QChar.
111 * This is a hack (it does only make sense for the common part of the UCS4
112 * and UTF16 encodings) and should not be used.
113 * This does only exist because of performance reasons (a real conversion
114 * using iconv is too slow on windows).
116 * This is no real conversion but a simple cast in reality. This is the reason
117 * why this works well for symbol fonts used in mathed too, even though
118 * these are not real ucs4 characters. These are codepoints in the
119 * computer modern fonts used, nothing unicode related.
120 * See comment in GuiPainter::text() for more explanation.
122 inline QChar const ucs4_to_qchar(char_type const ucs4)
124 LATTEST(is_utf16(ucs4));
125 return QChar(static_cast<unsigned short>(ucs4));
130 GuiFontMetrics::GuiFontMetrics(QFont const & font)
131 : font_(font), metrics_(font, 0),
132 strwidth_cache_(strwidth_cache_max_cost),
133 breakstr_cache_(breakstr_cache_max_cost),
134 qtextlayout_cache_(qtextlayout_cache_max_size)
136 // Determine italic slope
137 double const defaultSlope = tan(qDegreesToRadians(19.0));
138 QRawFont raw = QRawFont::fromFont(font);
139 QByteArray post(raw.fontTable("post"));
140 if (post.length() == 0) {
141 slope_ = defaultSlope;
142 LYXERR(Debug::FONT, "Screen font doesn't have 'post' table.");
144 // post table description:
145 // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html
146 int32_t italicAngle = qFromBigEndian(*reinterpret_cast<int32_t *>(post.data() + 4));
147 double angle = italicAngle / 65536.0; // Fixed-point 16.16 to floating-point
148 slope_ = -tan(qDegreesToRadians(angle));
149 // Correct italic fonts with zero slope
150 if (slope_ == 0.0 && font.italic())
151 slope_ = defaultSlope;
152 LYXERR(Debug::FONT, "Italic slope: " << slope_);
157 int GuiFontMetrics::maxAscent() const
159 return metrics_.ascent();
163 int GuiFontMetrics::maxDescent() const
165 // We add 1 as the value returned by QT is different than X
166 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
168 return metrics_.descent() + 1;
172 int GuiFontMetrics::em() const
174 return QFontInfo(font_).pixelSize();
178 int GuiFontMetrics::xHeight() const
180 // LATTEST(metrics_.xHeight() == ascent('x'));
181 return metrics_.xHeight();
185 int GuiFontMetrics::lineWidth() const
187 return metrics_.lineWidth();
191 int GuiFontMetrics::underlinePos() const
193 return metrics_.underlinePos();
197 int GuiFontMetrics::strikeoutPos() const
199 return metrics_.strikeOutPos();
203 bool GuiFontMetrics::italic() const
205 return font_.italic();
209 double GuiFontMetrics::italicSlope() const
216 int const outOfLimitMetric = -10000;
220 int GuiFontMetrics::lbearing(char_type c) const
222 int value = lbearing_cache_.value(c, outOfLimitMetric);
223 if (value != outOfLimitMetric)
227 value = metrics_.leftBearing(ucs4_to_qchar(c));
229 // FIXME: QFontMetrics::leftBearing does not support the
230 // full unicode range. Once it does, we could use:
231 // metrics_.leftBearing(toqstr(docstring(1, c)));
235 lbearing_cache_.insert(c, value);
241 int GuiFontMetrics::rbearing(char_type c) const
243 int value = rbearing_cache_.value(c, outOfLimitMetric);
244 if (value != outOfLimitMetric)
247 // Qt rbearing is from the right edge of the char's width().
249 QChar sc = ucs4_to_qchar(c);
250 value = width(c) - metrics_.rightBearing(sc);
252 // FIXME: QFontMetrics::leftBearing does not support the
253 // full unicode range. Once it does, we could use:
254 // metrics_.rightBearing(toqstr(docstring(1, c)));
258 rbearing_cache_.insert(c, value);
264 int GuiFontMetrics::width(docstring const & s) const
266 PROFILE_THIS_BLOCK(width);
267 if (int * wid_p = strwidth_cache_.object_ptr(s))
269 PROFILE_CACHE_MISS(width);
270 /* Several problems have to be taken into account:
271 * * QFontMetrics::width does not returns a wrong value with Qt5 with
272 * some arabic text, since the glyph-shaping operations are not
273 * done (documented in Qt5).
274 * * QTextLayout is broken for single characters with null width
275 * (like \not in mathed).
276 * * While QTextLine::horizontalAdvance is the right thing to use
277 * for text strings, it does not give a good result with some
278 * characters like the \int (gyph 4) of esint.
280 * The metrics of some of our math fonts (eg. esint) are such that
281 * QTextLine::horizontalAdvance leads, more or less, in the middle
282 * of a symbol. This is the horizontal position where a subscript
283 * should be drawn, so that the superscript has to be moved rightward.
284 * This is done when the kerning() method of the math insets returns
285 * a positive value. The problem with this choice is that navigating
286 * a formula becomes weird. For example, a selection extends only over
287 * about half of the symbol. In order to avoid this, with our math
288 * fonts we use QTextLine::naturalTextWidth, so that a superscript can
289 * be drawn right after the symbol, and move the subscript leftward by
290 * recording a negative value for the kerning.
293 // is the string a single character from a math font ?
294 #if QT_VERSION >= 0x040800
295 bool const math_char = s.length() == 1 && font_.styleName() == "LyX";
297 bool const math_char = s.length() == 1;
300 QString const qs = toqstr(s);
301 int br_width = metrics_.boundingRect(qs).width();
302 #if QT_VERSION >= 0x050b00
303 int s_width = metrics_.horizontalAdvance(qs);
305 int s_width = metrics_.width(qs);
307 // keep value 0 for math chars with width 0
309 w = max(br_width, s_width);
312 tl.setText(toqstr(s));
315 QTextLine line = tl.createLine();
317 w = iround(line.horizontalAdvance());
319 strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
324 int GuiFontMetrics::width(QString const & ucs2) const
326 return width(qstring_to_ucs4(ucs2));
330 int GuiFontMetrics::signedWidth(docstring const & s) const
336 return -width(s.substr(1, s.size() - 1));
342 uint qHash(TextLayoutKey const & key)
344 double params = (2 * key.rtl - 1) * key.ws;
345 return std::qHash(key.s) ^ ::qHash(params);
348 shared_ptr<QTextLayout const>
349 GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
350 double const wordspacing) const
352 PROFILE_THIS_BLOCK(getTextLayout);
353 TextLayoutKey key{s, rtl, wordspacing};
354 if (auto ptl = qtextlayout_cache_[key])
356 PROFILE_CACHE_MISS(getTextLayout);
357 auto const ptl = make_shared<QTextLayout>();
358 ptl->setCacheEnabled(true);
360 copy.setWordSpacing(wordspacing);
364 /* Use undocumented flag to enforce drawing direction
365 * FIXME: This does not work with Qt 5.11 (ticket #11284).
367 ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
370 #ifdef BIDI_USE_OVERRIDE
371 /* Use unicode override characters to enforce drawing direction
372 * Source: http://www.iamcal.com/understanding-bidirectional-text/
375 // Right-to-left override: forces to draw text right-to-left
376 ptl->setText(QChar(0x202E) + toqstr(s));
378 // Left-to-right override: forces to draw text left-to-right
379 ptl->setText(QChar(0x202D) + toqstr(s));
381 ptl->setText(toqstr(s));
387 qtextlayout_cache_.insert(key, ptl);
392 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
393 double const wordspacing) const
397 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
398 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
399 * not be the same when there are high-plan unicode characters
402 // BIDI_OFFSET accounts for a possible direction override
403 // character in front of the string.
404 int const qpos = toqstr(s.substr(0, pos)).length() + BIDI_OFFSET;
405 return static_cast<int>(tl->lineForTextPosition(qpos).cursorToX(qpos));
409 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
410 double const wordspacing) const
412 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
413 QTextLine const & tline = tl->lineForTextPosition(0);
414 int qpos = tline.xToCursor(x);
415 int newx = static_cast<int>(tline.cursorToX(qpos));
416 // The value of qpos may be wrong in rtl text (see ticket #10569).
417 // To work around this, let's have a look at adjacent positions to
418 // see whether we find closer matches.
419 if (rtl && newx < x) {
421 int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
422 if (abs(xm - x) < abs(newx - x)) {
428 } else if (rtl && newx > x) {
429 while (qpos < tline.textLength()) {
430 int const xp = static_cast<int>(tline.cursorToX(qpos + 1));
431 if (abs(xp - x) < abs(newx - x)) {
438 // correct x value to the actual cursor position.
441 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
442 * not be the same when there are high-plan unicode characters
445 #if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
446 int pos = qstring_to_ucs4(tl->text().left(qpos)).length();
447 // there may be a direction override character in front of the string.
448 return max(pos - BIDI_OFFSET, 0);
450 /* Due to QTBUG-25536 in 4.8.1 <= Qt < 5.1.0, the string returned
451 * by QString::toUcs4 (used by qstring_to_ucs4) may have wrong
452 * length. We work around the problem by trying all docstring
453 * positions until the right one is found. This is slow only if
454 * there are many high-plane Unicode characters. It might be
455 * worthwhile to implement a dichotomy search if this shows up
458 // there may be a direction override character in front of the string.
459 qpos = max(qpos - BIDI_OFFSET, 0);
460 int pos = min(qpos, static_cast<int>(s.length()));
461 while (pos >= 0 && toqstr(s.substr(0, pos)).length() != qpos)
463 LASSERT(pos > 0 || qpos == 0, /**/);
469 int GuiFontMetrics::countExpanders(docstring const & str) const
471 // Numbers of characters that are expanded by inter-word spacing. These
472 // characters are spaces, except for characters 09-0D which are treated
473 // specially. (From a combination of testing with the notepad found in qt's
474 // examples, and reading the source code.) In addition, consecutive spaces
475 // only count as one expander.
476 bool wasspace = false;
478 for (char_type c : str)
479 if (c > 0x0d && QChar(c).isSpace()) {
492 const int brkStrOffset = 1 + BIDI_OFFSET;
495 QString createBreakableString(docstring const & s, bool rtl, QTextLayout & tl)
497 /* Qt will not break at a leading or trailing space, and we need
498 * that sometimes, see http://www.lyx.org/trac/ticket/9921.
500 * To work around the problem, we enclose the string between
501 * zero-width characters so that the QTextLayout algorithm will
502 * agree to break the text at these extremal spaces.
504 // Unicode character ZERO WIDTH NO-BREAK SPACE
505 QChar const zerow_nbsp(0xfeff);
506 QString qs = zerow_nbsp + toqstr(s) + zerow_nbsp;
508 /* Use undocumented flag to enforce drawing direction
509 * FIXME: This does not work with Qt 5.11 (ticket #11284).
511 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
514 #ifdef BIDI_USE_OVERRIDE
515 /* Use unicode override characters to enforce drawing direction
516 * Source: http://www.iamcal.com/understanding-bidirectional-text/
519 // Right-to-left override: forces to draw text right-to-left
520 qs = QChar(0x202E) + qs;
522 // Left-to-right override: forces to draw text left-to-right
523 qs = QChar(0x202D) + qs;
529 docstring::size_type brkstr2str_pos(QString brkstr, docstring const & str, int pos)
531 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
532 * not be the same when there are high-plan unicode characters
535 // The variable `brkStrOffset' is here to account for the extra leading characters.
536 // The ending character zerow_nbsp has to be ignored if the line is complete.
537 int const qlen = pos - brkStrOffset - (pos == brkstr.length());
538 #if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
539 auto const len = qstring_to_ucs4(brkstr.mid(brkStrOffset, qlen)).length();
543 /* Due to QTBUG-25536 in 4.8.1 <= Qt < 5.1.0, the string returned
544 * by QString::toUcs4 (used by qstring_to_ucs4) may have wrong
545 * length. We work around the problem by trying all docstring
546 * positions until the right one is found. This is slow only if
547 * there are many high-plane Unicode characters. It might be
548 * worthwhile to implement a dichotomy search if this shows up
551 int len = min(qlen, static_cast<int>(str.length()));
552 while (len >= 0 && toqstr(str.substr(0, len)).length() != qlen)
554 LASSERT(len > 0 || qlen == 0, /**/);
562 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
563 bool rtl, bool force) const
566 QString qs = createBreakableString(s, rtl, tl);
571 * Some Asian languages split lines anywhere (no notion of
572 * word). It seems that QTextLayout is not aware of this fact.
574 * https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
576 * FIXME: Something shall be done about characters which are
577 * not allowed at the beginning or end of line.
579 to.setWrapMode(force ? QTextOption::WrapAtWordBoundaryOrAnywhere
580 : QTextOption::WordWrap);
581 tl.setTextOption(to);
586 QTextLine line = tl.createLine();
589 line.setLineWidth(first ? first_wid : wid);
597 for (int i = 0 ; i < tl.lineCount() ; ++i) {
598 QTextLine const & line = tl.lineAt(i);
599 int const line_epos = line.textStart() + line.textLength();
600 int const epos = brkstr2str_pos(qs, s, line_epos);
601 #if QT_VERSION >= 0x050000
602 // This does not take trailing spaces into account, except for the last line.
603 int const wid = iround(line.naturalTextWidth());
604 // If the line is not the last one, trailing space is always omitted.
606 // For the last line, compute the width without trailing space
607 if (i + 1 == tl.lineCount()) {
608 // trim_pos points to the last character that is not a space
609 auto trim_pos = s.find_last_not_of(from_ascii(" "));
610 if (trim_pos == docstring::npos)
612 else if (trim_pos + 1 < s.length()) {
613 int const num_spaces = s.length() - trim_pos - 1;
614 // find the position on the line before trailing
615 // spaces. Remove 1 to account for the ending
616 // non-breaking space of qs.
617 nspc_wid = iround(line.cursorToX(line_epos - num_spaces - 1));
621 // With some monospace fonts, the value of horizontalAdvance()
622 // can be wrong with Qt4. One hypothesis is that the invisible
623 // characters that we use are given a non-null width.
624 // FIXME: this is slower than it could be but we'll get rid of Qt4 anyway
625 docstring const ss = s.substr(pos, epos - pos);
626 int const wid = width(ss);
627 int const nspc_wid = i + 1 < tl.lineCount() ? wid : width(trim(ss));
629 breaks.emplace_back(epos - pos, wid, nspc_wid);
632 // FIXME: should it be kept in some form?
633 if ((force && line.textLength() == brkStrOffset) || line_wid > x)
634 return {-1, line_wid};
642 uint qHash(BreakStringKey const & key)
644 // assume widths are less than 10000. This fits in 32 bits.
645 uint params = key.force + 2 * key.rtl + 4 * key.first_wid + 10000 * key.wid;
646 return std::qHash(key.s) ^ ::qHash(params);
650 FontMetrics::Breaks GuiFontMetrics::breakString(docstring const & s, int first_wid, int wid,
651 bool rtl, bool force) const
653 PROFILE_THIS_BLOCK(breakString);
657 BreakStringKey key{s, first_wid, wid, rtl, force};
659 if (auto * brks_ptr = breakstr_cache_.object_ptr(key))
662 PROFILE_CACHE_MISS(breakString);
663 brks = breakString_helper(s, first_wid, wid, rtl, force);
664 breakstr_cache_.insert(key, brks, sizeof(key) + s.size() * sizeof(char_type));
670 void GuiFontMetrics::rectText(docstring const & str,
671 int & w, int & ascent, int & descent) const
673 // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
674 int const offset = 4;
676 w = width(str) + offset;
677 ascent = metrics_.ascent() + offset / 2;
678 descent = metrics_.descent() + offset / 2;
682 void GuiFontMetrics::buttonText(docstring const & str, const int offset,
683 int & w, int & ascent, int & descent) const
685 rectText(str, w, ascent, descent);
690 Dimension const GuiFontMetrics::defaultDimension() const
692 return Dimension(0, maxAscent(), maxDescent());
696 Dimension const GuiFontMetrics::dimension(char_type c) const
698 return Dimension(width(c), ascent(c), descent(c));
702 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
707 r = metrics_.boundingRect(ucs4_to_qchar(c));
709 r = metrics_.boundingRect(toqstr(docstring(1, c)));
711 AscendDescend ad = { -r.top(), r.bottom() + 1};
712 // We could as well compute the width but this is not really
713 // needed for now as it is done directly in width() below.
714 metrics_cache_.insert(c, ad);
720 int GuiFontMetrics::width(char_type c) const
722 int value = width_cache_.value(c, outOfLimitMetric);
723 if (value != outOfLimitMetric)
726 #if QT_VERSION >= 0x050b00
728 value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
730 value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
733 value = metrics_.width(ucs4_to_qchar(c));
735 value = metrics_.width(toqstr(docstring(1, c)));
738 width_cache_.insert(c, value);
744 int GuiFontMetrics::ascent(char_type c) const
746 static AscendDescend const outOfLimitAD =
747 {outOfLimitMetric, outOfLimitMetric};
748 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
749 if (value.ascent != outOfLimitMetric)
752 value = fillMetricsCache(c);
757 int GuiFontMetrics::descent(char_type c) const
759 static AscendDescend const outOfLimitAD =
760 {outOfLimitMetric, outOfLimitMetric};
761 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
762 if (value.descent != outOfLimitMetric)
763 return value.descent;
765 value = fillMetricsCache(c);
766 return value.descent;
769 } // namespace frontend