]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiFontMetrics.cpp
Better handling of trailing spaces in rows.
[lyx.git] / src / frontends / qt / GuiFontMetrics.cpp
1 /**
2  * \file GuiFontMetrics.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiFontMetrics.h"
15
16 #include "qt_helpers.h"
17
18 #include "Dimension.h"
19
20 #include "support/convert.h"
21 #include "support/lassert.h"
22 #include "support/lyxlib.h"
23 #include "support/debug.h"
24
25 #define DISABLE_PMPROF
26 #include "support/pmprof.h"
27
28 #include <QByteArray>
29 #include <QRawFont>
30 #include <QtEndian>
31
32 #if QT_VERSION >= 0x050100
33 #include <QtMath>
34 #else
35 #define qDegreesToRadians(degree) (degree * (M_PI / 180))
36 #endif
37
38 using namespace std;
39 using namespace lyx::support;
40
41 /* Define what mechanism is used to enforce text direction. Different
42  * methods work with different Qt versions. Here we try to use both
43  * methods together.
44  */
45 // Define to use unicode override characters to force direction
46 #define BIDI_USE_OVERRIDE
47 // Define to use flag to force direction
48 #define BIDI_USE_FLAG
49
50 #ifdef BIDI_USE_OVERRIDE
51 # define BIDI_OFFSET 1
52 #else
53 # define BIDI_OFFSET 0
54 #endif
55
56 #if !defined(BIDI_USE_OVERRIDE) && !defined(BIDI_USE_FLAG)
57 #  error "Define at least one of BIDI_USE_OVERRIDE or BIDI_USE_FLAG"
58 #endif
59
60
61 #if QT_VERSION < 0x050000
62 inline uint qHash(double key)
63 {
64         return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
65 }
66 #endif
67
68
69 namespace std {
70
71 /*
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>.
75  */
76 uint qHash(lyx::docstring const & s)
77 {
78         return qHash(QByteArray(reinterpret_cast<char const *>(s.data()),
79                                 s.size() * sizeof(lyx::docstring::value_type)));
80 }
81
82 } // namespace std
83
84 namespace lyx {
85 namespace frontend {
86
87
88 namespace {
89 // Maximal size/cost for various caches. See QCache documentation to
90 // see what cost means.
91
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;
103 #else
104 // Disable the cache
105 int const qtextlayout_cache_max_size = 0;
106 #endif
107
108
109 /**
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).
115  *
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.
121  **/
122 inline QChar const ucs4_to_qchar(char_type const ucs4)
123 {
124         LATTEST(is_utf16(ucs4));
125         return QChar(static_cast<unsigned short>(ucs4));
126 }
127 } // namespace
128
129
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)
135 {
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.");
143         } else {
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_);
153         }
154 }
155
156
157 int GuiFontMetrics::maxAscent() const
158 {
159         return metrics_.ascent();
160 }
161
162
163 int GuiFontMetrics::maxDescent() const
164 {
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
167         // FIXME: check this
168         return metrics_.descent() + 1;
169 }
170
171
172 int GuiFontMetrics::em() const
173 {
174         return QFontInfo(font_).pixelSize();
175 }
176
177
178 int GuiFontMetrics::xHeight() const
179 {
180 //      LATTEST(metrics_.xHeight() == ascent('x'));
181         return metrics_.xHeight();
182 }
183
184
185 int GuiFontMetrics::lineWidth() const
186 {
187         return metrics_.lineWidth();
188 }
189
190
191 int GuiFontMetrics::underlinePos() const
192 {
193         return metrics_.underlinePos();
194 }
195
196
197 int GuiFontMetrics::strikeoutPos() const
198 {
199         return metrics_.strikeOutPos();
200 }
201
202
203 bool GuiFontMetrics::italic() const
204 {
205         return font_.italic();
206 }
207
208
209 double GuiFontMetrics::italicSlope() const
210 {
211         return slope_;
212 }
213
214
215 namespace {
216 int const outOfLimitMetric = -10000;
217 }
218
219
220 int GuiFontMetrics::lbearing(char_type c) const
221 {
222         int value = lbearing_cache_.value(c, outOfLimitMetric);
223         if (value != outOfLimitMetric)
224                 return value;
225
226         if (is_utf16(c))
227                 value = metrics_.leftBearing(ucs4_to_qchar(c));
228         else {
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)));
232                 value = 0;
233         }
234
235         lbearing_cache_.insert(c, value);
236
237         return value;
238 }
239
240
241 int GuiFontMetrics::rbearing(char_type c) const
242 {
243         int value = rbearing_cache_.value(c, outOfLimitMetric);
244         if (value != outOfLimitMetric)
245                 return value;
246
247         // Qt rbearing is from the right edge of the char's width().
248         if (is_utf16(c)) {
249                 QChar sc = ucs4_to_qchar(c);
250                 value = width(c) - metrics_.rightBearing(sc);
251         } else {
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)));
255                 value = width(c);
256         }
257
258         rbearing_cache_.insert(c, value);
259
260         return value;
261 }
262
263
264 int GuiFontMetrics::width(docstring const & s) const
265 {
266         PROFILE_THIS_BLOCK(width);
267         if (int * wid_p = strwidth_cache_.object_ptr(s))
268                 return *wid_p;
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.
279
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.
291         */
292         int w = 0;
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";
296 #else
297         bool const math_char = s.length() == 1;
298 #endif
299         if (math_char) {
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);
304 #else
305                 int s_width = metrics_.width(qs);
306 #endif
307                 // keep value 0 for math chars with width 0
308                 if (s_width != 0)
309                         w = max(br_width, s_width);
310         } else {
311                 QTextLayout tl;
312                 tl.setText(toqstr(s));
313                 tl.setFont(font_);
314                 tl.beginLayout();
315                 QTextLine line = tl.createLine();
316                 tl.endLayout();
317                 w = iround(line.horizontalAdvance());
318         }
319         strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
320         return w;
321 }
322
323
324 int GuiFontMetrics::width(QString const & ucs2) const
325 {
326         return width(qstring_to_ucs4(ucs2));
327 }
328
329
330 int GuiFontMetrics::signedWidth(docstring const & s) const
331 {
332         if (s.empty())
333                 return 0;
334
335         if (s[0] == '-')
336                 return -width(s.substr(1, s.size() - 1));
337         else
338                 return width(s);
339 }
340
341
342 uint qHash(TextLayoutKey const & key)
343 {
344         double params = (2 * key.rtl - 1) * key.ws;
345         return std::qHash(key.s) ^ ::qHash(params);
346 }
347
348 shared_ptr<QTextLayout const>
349 GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
350                               double const wordspacing) const
351 {
352         PROFILE_THIS_BLOCK(getTextLayout);
353         TextLayoutKey key{s, rtl, wordspacing};
354         if (auto ptl = qtextlayout_cache_[key])
355                 return ptl;
356         PROFILE_CACHE_MISS(getTextLayout);
357         auto const ptl = make_shared<QTextLayout>();
358         ptl->setCacheEnabled(true);
359         QFont copy = font_;
360         copy.setWordSpacing(wordspacing);
361         ptl->setFont(copy);
362
363 #ifdef BIDI_USE_FLAG
364         /* Use undocumented flag to enforce drawing direction
365          * FIXME: This does not work with Qt 5.11 (ticket #11284).
366          */
367         ptl->setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
368 #endif
369
370 #ifdef BIDI_USE_OVERRIDE
371         /* Use unicode override characters to enforce drawing direction
372          * Source: http://www.iamcal.com/understanding-bidirectional-text/
373          */
374         if (rtl)
375                 // Right-to-left override: forces to draw text right-to-left
376                 ptl->setText(QChar(0x202E) + toqstr(s));
377         else
378                 // Left-to-right override: forces to draw text left-to-right
379                 ptl->setText(QChar(0x202D) + toqstr(s));
380 #else
381         ptl->setText(toqstr(s));
382 #endif
383
384         ptl->beginLayout();
385         ptl->createLine();
386         ptl->endLayout();
387         qtextlayout_cache_.insert(key, ptl);
388         return ptl;
389 }
390
391
392 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
393                           double const wordspacing) const
394 {
395         if (pos <= 0)
396                 pos = 0;
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
400          * (bug #10443).
401          */
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));
406 }
407
408
409 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
410                           double const wordspacing) const
411 {
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) {
420                 while (qpos > 0) {
421                         int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
422                         if (abs(xm - x) < abs(newx - x)) {
423                                 --qpos;
424                                 newx = xm;
425                         } else
426                                 break;
427                 }
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)) {
432                                 ++qpos;
433                                 newx = xp;
434                         } else
435                                 break;
436                 }
437         }
438         // correct x value to the actual cursor position.
439         x = newx;
440
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
443          * (bug #10443).
444          */
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);
449 #else
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
456          * under a profiler.
457          */
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)
462                 --pos;
463         LASSERT(pos > 0 || qpos == 0, /**/);
464         return pos;
465 #endif
466 }
467
468
469 int GuiFontMetrics::countExpanders(docstring const & str) const
470 {
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;
477         int nexp = 0;
478         for (char_type c : str)
479                 if (c > 0x0d && QChar(c).isSpace()) {
480                         if (!wasspace) {
481                                 ++nexp;
482                                 wasspace = true;
483                         }
484                 } else
485                         wasspace = false;
486         return nexp;
487 }
488
489
490 namespace {
491
492 const int brkStrOffset = 1 + BIDI_OFFSET;
493
494
495 QString createBreakableString(docstring const & s, bool rtl, QTextLayout & tl)
496 {
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.
499          *
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.
503          */
504         // Unicode character ZERO WIDTH NO-BREAK SPACE
505         QChar const zerow_nbsp(0xfeff);
506         QString qs = zerow_nbsp + toqstr(s) + zerow_nbsp;
507 #ifdef BIDI_USE_FLAG
508         /* Use undocumented flag to enforce drawing direction
509          * FIXME: This does not work with Qt 5.11 (ticket #11284).
510          */
511         tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
512 #endif
513
514 #ifdef BIDI_USE_OVERRIDE
515         /* Use unicode override characters to enforce drawing direction
516          * Source: http://www.iamcal.com/understanding-bidirectional-text/
517          */
518         if (rtl)
519                 // Right-to-left override: forces to draw text right-to-left
520                 qs = QChar(0x202E) + qs;
521         else
522                 // Left-to-right override: forces to draw text left-to-right
523                 qs =  QChar(0x202D) + qs;
524 #endif
525         return qs;
526 }
527
528
529 docstring::size_type brkstr2str_pos(QString brkstr, docstring const & str, int pos)
530 {
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
533          * (bug #10443).
534          */
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();
540         // Avoid warning
541         (void)str;
542 #else
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
549          * under a profiler.
550          */
551         int len = min(qlen, static_cast<int>(str.length()));
552         while (len >= 0 && toqstr(str.substr(0, len)).length() != qlen)
553                 --len;
554         LASSERT(len > 0 || qlen == 0, /**/);
555 #endif
556         return len;
557 }
558
559 }
560
561 FontMetrics::Breaks
562 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
563                                    bool rtl, bool force) const
564 {
565         QTextLayout tl;
566         QString qs = createBreakableString(s, rtl, tl);
567         tl.setText(qs);
568         tl.setFont(font_);
569         QTextOption to;
570         /*
571          * Some Asian languages split lines anywhere (no notion of
572          * word). It seems that QTextLayout is not aware of this fact.
573          * See for reference:
574          *    https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
575          *
576          * FIXME: Something shall be done about characters which are
577          * not allowed at the beginning or end of line.
578          */
579         to.setWrapMode(force ? QTextOption::WrapAtWordBoundaryOrAnywhere
580                              : QTextOption::WordWrap);
581         tl.setTextOption(to);
582
583         bool first = true;
584         tl.beginLayout();
585         while(true) {
586                 QTextLine line = tl.createLine();
587                 if (!line.isValid())
588                         break;
589                 line.setLineWidth(first ? first_wid : wid);
590                 tl.createLine();
591                 first = false;
592         }
593         tl.endLayout();
594
595         Breaks breaks;
596         int pos = 0;
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.
605                 int nspc_wid = wid;
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)
611                                 nspc_wid = 0;
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));
618                         }
619                 }
620 #else
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));
628 #endif
629                 breaks.emplace_back(epos - pos, wid, nspc_wid);
630                 pos = epos;
631 #if 0
632                 // FIXME: should it be kept in some form?
633                 if ((force && line.textLength() == brkStrOffset) || line_wid > x)
634                         return {-1, line_wid};
635 #endif
636         }
637
638         return breaks;
639 }
640
641
642 uint qHash(BreakStringKey const & key)
643 {
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);
647 }
648
649
650 FontMetrics::Breaks GuiFontMetrics::breakString(docstring const & s, int first_wid, int wid,
651                                                 bool rtl, bool force) const
652 {
653         PROFILE_THIS_BLOCK(breakString);
654         if (s.empty())
655                 return Breaks();
656
657         BreakStringKey key{s, first_wid, wid, rtl, force};
658         Breaks brks;
659         if (auto * brks_ptr = breakstr_cache_.object_ptr(key))
660                 brks = *brks_ptr;
661         else {
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));
665         }
666         return brks;
667 }
668
669
670 void GuiFontMetrics::rectText(docstring const & str,
671         int & w, int & ascent, int & descent) const
672 {
673         // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
674         int const offset = 4;
675
676         w = width(str) + offset;
677         ascent = metrics_.ascent() + offset / 2;
678         descent = metrics_.descent() + offset / 2;
679 }
680
681
682 void GuiFontMetrics::buttonText(docstring const & str, const int offset,
683         int & w, int & ascent, int & descent) const
684 {
685         rectText(str, w, ascent, descent);
686         w += offset;
687 }
688
689
690 Dimension const GuiFontMetrics::defaultDimension() const
691 {
692         return Dimension(0, maxAscent(), maxDescent());
693 }
694
695
696 Dimension const GuiFontMetrics::dimension(char_type c) const
697 {
698         return Dimension(width(c), ascent(c), descent(c));
699 }
700
701
702 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
703                 char_type c) const
704 {
705         QRect r;
706         if (is_utf16(c))
707                 r = metrics_.boundingRect(ucs4_to_qchar(c));
708         else
709                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
710
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);
715
716         return ad;
717 }
718
719
720 int GuiFontMetrics::width(char_type c) const
721 {
722         int value = width_cache_.value(c, outOfLimitMetric);
723         if (value != outOfLimitMetric)
724                 return value;
725
726 #if QT_VERSION >= 0x050b00
727         if (is_utf16(c))
728                 value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
729         else
730                 value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
731 #else
732         if (is_utf16(c))
733                 value = metrics_.width(ucs4_to_qchar(c));
734         else
735                 value = metrics_.width(toqstr(docstring(1, c)));
736 #endif
737
738         width_cache_.insert(c, value);
739
740         return value;
741 }
742
743
744 int GuiFontMetrics::ascent(char_type c) const
745 {
746         static AscendDescend const outOfLimitAD =
747                 {outOfLimitMetric, outOfLimitMetric};
748         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
749         if (value.ascent != outOfLimitMetric)
750                 return value.ascent;
751
752         value = fillMetricsCache(c);
753         return value.ascent;
754 }
755
756
757 int GuiFontMetrics::descent(char_type c) const
758 {
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;
764
765         value = fillMetricsCache(c);
766         return value.descent;
767 }
768
769 } // namespace frontend
770 } // namespace lyx