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/lyxlib.h"
24 #include "support/textutils.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);
355 // This holds a translation table between the original string and the
356 // QString that we can use with QTextLayout.
357 struct TextLayoutHelper
359 /// Create the helper
360 /// \c s is the original string
361 /// \c isrtl is true if the string is right-to-left
362 /// \c naked is true to disable the insertion of zero width annotations
363 TextLayoutHelper(docstring const & s, bool isrtl, bool naked = false);
365 /// translate QString index to docstring index
366 docstring::size_type qpos2pos(int qpos) const
368 return lower_bound(pos2qpos_.begin(), pos2qpos_.end(), qpos) - pos2qpos_.begin();
371 /// Translate docstring index to QString index
372 int pos2qpos(docstring::size_type pos) const { return pos2qpos_[pos]; }
374 // The original string
378 // is string right-to-left?
382 // This vector contains the QString pos for each string position
383 vector<int> pos2qpos_;
387 TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
388 : docstr(s), rtl(isrtl)
390 // Reserve memory for performance purpose
391 pos2qpos_.reserve(s.size());
392 qstr.reserve(2 * s.size());
394 /* Qt will not break at a leading or trailing space, and we need
395 * that sometimes, see http://www.lyx.org/trac/ticket/9921.
397 * To work around the problem, we enclose the string between
398 * word joiner characters so that the QTextLayout algorithm will
399 * agree to break the text at these extremal spaces.
401 // Unicode character WORD JOINER
402 QChar const word_joiner(0x2060);
406 #ifdef BIDI_USE_OVERRIDE
408 qstr += bidi_override[rtl];
411 // Now translate the string character-by-character.
412 bool was_space = false;
413 for (char_type const c : s) {
414 // insert a word joiner character between consecutive spaces
415 bool const is_space = isSpace(c);
416 if (!naked && is_space && was_space)
418 was_space = is_space;
419 // Remember the QString index at this point
420 pos2qpos_.push_back(qstr.size());
421 // Performance: UTF-16 characters are easier
423 qstr += ucs4_to_qchar(c);
428 // Final word joiner (see above)
432 // Add virtual position at the end of the string
433 pos2qpos_.push_back(qstr.size());
435 //QString dump = qstr;
436 //LYXERR0("TLH: " << dump.replace(word_joiner, "|").toStdString());
442 shared_ptr<QTextLayout>
443 getTextLayout_helper(TextLayoutHelper const & tlh, double const wordspacing,
446 auto const ptl = make_shared<QTextLayout>();
447 ptl->setCacheEnabled(true);
448 font.setWordSpacing(wordspacing);
451 /* Use undocumented flag to enforce drawing direction
452 * FIXME: This does not work with Qt 5.11 (ticket #11284).
454 ptl->setFlags(tlh.rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
456 ptl->setText(tlh.qstr);
467 shared_ptr<QTextLayout const>
468 GuiFontMetrics::getTextLayout(TextLayoutHelper const & tlh,
469 double const wordspacing) const
471 PROFILE_THIS_BLOCK(getTextLayout_TLH);
472 TextLayoutKey key{tlh.docstr, tlh.rtl, wordspacing};
473 if (auto ptl = qtextlayout_cache_[key])
475 PROFILE_CACHE_MISS(getTextLayout_TLH);
476 auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
477 qtextlayout_cache_.insert(key, ptl);
482 shared_ptr<QTextLayout const>
483 GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
484 double const wordspacing) const
486 PROFILE_THIS_BLOCK(getTextLayout);
487 TextLayoutKey key{s, rtl, wordspacing};
488 if (auto ptl = qtextlayout_cache_[key])
490 PROFILE_CACHE_MISS(getTextLayout);
491 TextLayoutHelper tlh(s, rtl);
492 auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
493 qtextlayout_cache_.insert(key, ptl);
498 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
499 double const wordspacing) const
503 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
504 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
505 * not be the same when there are high-plan unicode characters
508 // BIDI_OFFSET accounts for a possible direction override
509 // character in front of the string.
510 int const qpos = toqstr(s.substr(0, pos)).length() + BIDI_OFFSET;
511 return static_cast<int>(tl->lineForTextPosition(qpos).cursorToX(qpos));
515 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
516 double const wordspacing) const
518 shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
519 QTextLine const & tline = tl->lineForTextPosition(0);
520 int qpos = tline.xToCursor(x);
521 int newx = static_cast<int>(tline.cursorToX(qpos));
522 // The value of qpos may be wrong in rtl text (see ticket #10569).
523 // To work around this, let's have a look at adjacent positions to
524 // see whether we find closer matches.
525 if (rtl && newx < x) {
527 int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
528 if (abs(xm - x) < abs(newx - x)) {
534 } else if (rtl && newx > x) {
535 while (qpos < tline.textLength()) {
536 int const xp = static_cast<int>(tline.cursorToX(qpos + 1));
537 if (abs(xp - x) < abs(newx - x)) {
544 // correct x value to the actual cursor position.
547 /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
548 * not be the same when there are high-plan unicode characters
551 #if QT_VERSION < 0x040801 || QT_VERSION >= 0x050100
552 int pos = qstring_to_ucs4(tl->text().left(qpos)).length();
553 // there may be a direction override character in front of the string.
554 return max(pos - BIDI_OFFSET, 0);
556 /* Due to QTBUG-25536 in 4.8.1 <= Qt < 5.1.0, the string returned
557 * by QString::toUcs4 (used by qstring_to_ucs4) may have wrong
558 * length. We work around the problem by trying all docstring
559 * positions until the right one is found. This is slow only if
560 * there are many high-plane Unicode characters. It might be
561 * worthwhile to implement a dichotomy search if this shows up
564 // there may be a direction override character in front of the string.
565 qpos = max(qpos - BIDI_OFFSET, 0);
566 int pos = min(qpos, static_cast<int>(s.length()));
567 while (pos >= 0 && toqstr(s.substr(0, pos)).length() != qpos)
569 LASSERT(pos > 0 || qpos == 0, /**/);
576 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
577 bool rtl, bool force) const
579 TextLayoutHelper const tlh(s, rtl);
583 /* Use undocumented flag to enforce drawing direction
584 * FIXME: This does not work with Qt 5.11 (ticket #11284).
586 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
588 tl.setText(tlh.qstr);
592 * Some Asian languages split lines anywhere (no notion of
593 * word). It seems that QTextLayout is not aware of this fact.
595 * https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
597 * FIXME: Something shall be done about characters which are
598 * not allowed at the beginning or end of line.
600 to.setWrapMode(force ? QTextOption::WrapAtWordBoundaryOrAnywhere
601 : QTextOption::WordWrap);
602 tl.setTextOption(to);
607 QTextLine line = tl.createLine();
610 line.setLineWidth(first ? first_wid : wid);
617 for (int i = 0 ; i < tl.lineCount() ; ++i) {
618 QTextLine const & line = tl.lineAt(i);
619 int const line_epos = line.textStart() + line.textLength();
620 int const epos = tlh.qpos2pos(line_epos);
621 #if QT_VERSION >= 0x050000
622 // This does not take trailing spaces into account, except for the last line.
623 int const wid = iround(line.naturalTextWidth());
624 // If the line is not the last one, trailing space is always omitted.
626 // For the last line, compute the width without trailing space
627 if (i + 1 == tl.lineCount() && !s.empty() && isSpace(s.back())
628 && line.textStart() <= tlh.pos2qpos(s.size() - 1))
629 nspc_wid = iround(line.cursorToX(tlh.pos2qpos(s.size() - 1)));
631 // With some monospace fonts, the value of horizontalAdvance()
632 // can be wrong with Qt4. One hypothesis is that the invisible
633 // characters that we use are given a non-null width.
634 // FIXME: this is slower than it could be but we'll get rid of Qt4 anyway
635 docstring ss = s.substr(pos, epos - pos);
636 int const wid = width(ss);
637 if (!ss.empty() && isSpace(ss.back()))
639 int const nspc_wid = i + 1 < tl.lineCount() ? width(ss) : wid;
641 breaks.emplace_back(epos - pos, wid, nspc_wid);
644 // FIXME: should it be kept in some form?
645 if ((force && line.textLength() == brkStrOffset) || line_wid > x)
646 return {-1, line_wid};
654 uint qHash(BreakStringKey const & key)
656 // assume widths are less than 10000. This fits in 32 bits.
657 uint params = key.force + 2 * key.rtl + 4 * key.first_wid + 10000 * key.wid;
658 return std::qHash(key.s) ^ ::qHash(params);
662 FontMetrics::Breaks GuiFontMetrics::breakString(docstring const & s, int first_wid, int wid,
663 bool rtl, bool force) const
665 PROFILE_THIS_BLOCK(breakString);
669 BreakStringKey key{s, first_wid, wid, rtl, force};
671 if (auto * brks_ptr = breakstr_cache_.object_ptr(key))
674 PROFILE_CACHE_MISS(breakString);
675 brks = breakString_helper(s, first_wid, wid, rtl, force);
676 breakstr_cache_.insert(key, brks, sizeof(key) + s.size() * sizeof(char_type));
682 void GuiFontMetrics::rectText(docstring const & str,
683 int & w, int & ascent, int & descent) const
685 // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
686 int const offset = 4;
688 w = width(str) + offset;
689 ascent = metrics_.ascent() + offset / 2;
690 descent = metrics_.descent() + offset / 2;
694 void GuiFontMetrics::buttonText(docstring const & str, const int offset,
695 int & w, int & ascent, int & descent) const
697 rectText(str, w, ascent, descent);
702 Dimension const GuiFontMetrics::defaultDimension() const
704 return Dimension(0, maxAscent(), maxDescent());
708 Dimension const GuiFontMetrics::dimension(char_type c) const
710 return Dimension(width(c), ascent(c), descent(c));
714 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
719 r = metrics_.boundingRect(ucs4_to_qchar(c));
721 r = metrics_.boundingRect(toqstr(docstring(1, c)));
723 AscendDescend ad = { -r.top(), r.bottom() + 1};
724 // We could as well compute the width but this is not really
725 // needed for now as it is done directly in width() below.
726 metrics_cache_.insert(c, ad);
732 int GuiFontMetrics::width(char_type c) const
734 int value = width_cache_.value(c, outOfLimitMetric);
735 if (value != outOfLimitMetric)
738 #if QT_VERSION >= 0x050b00
740 value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
742 value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
745 value = metrics_.width(ucs4_to_qchar(c));
747 value = metrics_.width(toqstr(docstring(1, c)));
750 width_cache_.insert(c, value);
756 int GuiFontMetrics::ascent(char_type c) const
758 static AscendDescend const outOfLimitAD =
759 {outOfLimitMetric, outOfLimitMetric};
760 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
761 if (value.ascent != outOfLimitMetric)
764 value = fillMetricsCache(c);
769 int GuiFontMetrics::descent(char_type c) const
771 static AscendDescend const outOfLimitAD =
772 {outOfLimitMetric, outOfLimitMetric};
773 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
774 if (value.descent != outOfLimitMetric)
775 return value.descent;
777 value = fillMetricsCache(c);
778 return value.descent;
781 } // namespace frontend