]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiFontMetrics.cpp
Use TextLayoutHelper in x2pos and pos2x
[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/debug.h"
22 #include "support/lassert.h"
23 #include "support/lyxlib.h"
24 #include "support/textutils.h"
25
26 #define DISABLE_PMPROF
27 #include "support/pmprof.h"
28
29 #include <QByteArray>
30 #include <QRawFont>
31 #include <QtEndian>
32
33 #if QT_VERSION >= 0x050100
34 #include <QtMath>
35 #else
36 #define qDegreesToRadians(degree) (degree * (M_PI / 180))
37 #endif
38
39 using namespace std;
40 using namespace lyx::support;
41
42 /* Define what mechanisms are used to enforce text direction. There
43  * are two methods that work with different Qt versions. Here we try
44  * to use both methods together.
45  */
46 // Define to use unicode override characters to force direction
47 #define BIDI_USE_OVERRIDE
48 // Define to use QTextLayout flag to force direction
49 #define BIDI_USE_FLAG
50
51 #if !defined(BIDI_USE_OVERRIDE) && !defined(BIDI_USE_FLAG)
52 #  error "Define at least one of BIDI_USE_OVERRIDE or BIDI_USE_FLAG"
53 #endif
54
55
56 #if QT_VERSION < 0x050000
57 inline uint qHash(double key)
58 {
59         return qHash(QByteArray(reinterpret_cast<char const *>(&key), sizeof(key)));
60 }
61 #endif
62
63
64 namespace std {
65
66 /*
67  * Argument-dependent lookup implies that this function shall be
68  * declared in the namespace of its argument. But this is std
69  * namespace, since lyx::docstring is just std::basic_string<wchar_t>.
70  */
71 uint qHash(lyx::docstring const & s)
72 {
73         return qHash(QByteArray(reinterpret_cast<char const *>(s.data()),
74                                 s.size() * sizeof(lyx::docstring::value_type)));
75 }
76
77 } // namespace std
78
79 namespace lyx {
80 namespace frontend {
81
82
83 namespace {
84 // Maximal size/cost for various caches. See QCache documentation to
85 // see what cost means.
86
87 // Limit strwidth_cache_ total cost to 1MB of string data.
88 int const strwidth_cache_max_cost = 1024 * 1024;
89 // Limit breakstr_cache_ total cost to 10MB of string data.
90 // This is useful for documents with very large insets.
91 int const breakstr_cache_max_cost = 10 * 1024 * 1024;
92 // Qt 5.x already has its own caching of QTextLayout objects
93 // but it does not seem to work well on MacOS X.
94 #if (QT_VERSION < 0x050000) || defined(Q_OS_MAC)
95 // Limit qtextlayout_cache_ size to 500 elements (we do not know the
96 // size of the QTextLayout objects anyway).
97 int const qtextlayout_cache_max_size = 500;
98 #else
99 // Disable the cache
100 int const qtextlayout_cache_max_size = 0;
101 #endif
102
103
104 /**
105  * Convert a UCS4 character into a QChar.
106  * This is a hack (it does only make sense for the common part of the UCS4
107  * and UTF16 encodings) and should not be used.
108  * This does only exist because of performance reasons (a real conversion
109  * using iconv is too slow on windows).
110  *
111  * This is no real conversion but a simple cast in reality. This is the reason
112  * why this works well for symbol fonts used in mathed too, even though
113  * these are not real ucs4 characters. These are codepoints in the
114  * computer modern fonts used, nothing unicode related.
115  * See comment in GuiPainter::text() for more explanation.
116  **/
117 inline QChar const ucs4_to_qchar(char_type const ucs4)
118 {
119         LATTEST(is_utf16(ucs4));
120         return QChar(static_cast<unsigned short>(ucs4));
121 }
122 } // namespace
123
124
125 GuiFontMetrics::GuiFontMetrics(QFont const & font)
126         : font_(font), metrics_(font, 0),
127           strwidth_cache_(strwidth_cache_max_cost),
128           breakstr_cache_(breakstr_cache_max_cost),
129           qtextlayout_cache_(qtextlayout_cache_max_size)
130 {
131         // Determine italic slope
132         double const defaultSlope = tan(qDegreesToRadians(19.0));
133         QRawFont raw = QRawFont::fromFont(font);
134         QByteArray post(raw.fontTable("post"));
135         if (post.length() == 0) {
136                 slope_ = defaultSlope;
137                 LYXERR(Debug::FONT, "Screen font doesn't have 'post' table.");
138         } else {
139                 // post table description:
140                 // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html
141                 int32_t italicAngle = qFromBigEndian(*reinterpret_cast<int32_t *>(post.data() + 4));
142                 double angle = italicAngle / 65536.0; // Fixed-point 16.16 to floating-point
143                 slope_ = -tan(qDegreesToRadians(angle));
144                 // Correct italic fonts with zero slope
145                 if (slope_ == 0.0 && font.italic())
146                         slope_ = defaultSlope;
147                 LYXERR(Debug::FONT, "Italic slope: " << slope_);
148         }
149 }
150
151
152 int GuiFontMetrics::maxAscent() const
153 {
154         return metrics_.ascent();
155 }
156
157
158 int GuiFontMetrics::maxDescent() const
159 {
160         // We add 1 as the value returned by QT is different than X
161         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
162         // FIXME: check this
163         return metrics_.descent() + 1;
164 }
165
166
167 int GuiFontMetrics::em() const
168 {
169         return QFontInfo(font_).pixelSize();
170 }
171
172
173 int GuiFontMetrics::xHeight() const
174 {
175 //      LATTEST(metrics_.xHeight() == ascent('x'));
176         return metrics_.xHeight();
177 }
178
179
180 int GuiFontMetrics::lineWidth() const
181 {
182         return metrics_.lineWidth();
183 }
184
185
186 int GuiFontMetrics::underlinePos() const
187 {
188         return metrics_.underlinePos();
189 }
190
191
192 int GuiFontMetrics::strikeoutPos() const
193 {
194         return metrics_.strikeOutPos();
195 }
196
197
198 bool GuiFontMetrics::italic() const
199 {
200         return font_.italic();
201 }
202
203
204 double GuiFontMetrics::italicSlope() const
205 {
206         return slope_;
207 }
208
209
210 namespace {
211 int const outOfLimitMetric = -10000;
212 }
213
214
215 int GuiFontMetrics::lbearing(char_type c) const
216 {
217         int value = lbearing_cache_.value(c, outOfLimitMetric);
218         if (value != outOfLimitMetric)
219                 return value;
220
221         if (is_utf16(c))
222                 value = metrics_.leftBearing(ucs4_to_qchar(c));
223         else {
224                 // FIXME: QFontMetrics::leftBearing does not support the
225                 //        full unicode range. Once it does, we could use:
226                 // metrics_.leftBearing(toqstr(docstring(1, c)));
227                 value = 0;
228         }
229
230         lbearing_cache_.insert(c, value);
231
232         return value;
233 }
234
235
236 int GuiFontMetrics::rbearing(char_type c) const
237 {
238         int value = rbearing_cache_.value(c, outOfLimitMetric);
239         if (value != outOfLimitMetric)
240                 return value;
241
242         // Qt rbearing is from the right edge of the char's width().
243         if (is_utf16(c)) {
244                 QChar sc = ucs4_to_qchar(c);
245                 value = width(c) - metrics_.rightBearing(sc);
246         } else {
247                 // FIXME: QFontMetrics::leftBearing does not support the
248                 //        full unicode range. Once it does, we could use:
249                 // metrics_.rightBearing(toqstr(docstring(1, c)));
250                 value = width(c);
251         }
252
253         rbearing_cache_.insert(c, value);
254
255         return value;
256 }
257
258
259 int GuiFontMetrics::width(docstring const & s) const
260 {
261         PROFILE_THIS_BLOCK(width);
262         if (int * wid_p = strwidth_cache_.object_ptr(s))
263                 return *wid_p;
264         PROFILE_CACHE_MISS(width);
265         /* Several problems have to be taken into account:
266          * * QFontMetrics::width does not returns a wrong value with Qt5 with
267          *   some arabic text, since the glyph-shaping operations are not
268          *   done (documented in Qt5).
269          * * QTextLayout is broken for single characters with null width
270          *   (like \not in mathed).
271          * * While QTextLine::horizontalAdvance is the right thing to use
272      *   for text strings, it does not give a good result with some
273      *   characters like the \int (gyph 4) of esint.
274
275          * The metrics of some of our math fonts (eg. esint) are such that
276          * QTextLine::horizontalAdvance leads, more or less, in the middle
277          * of a symbol. This is the horizontal position where a subscript
278          * should be drawn, so that the superscript has to be moved rightward.
279          * This is done when the kerning() method of the math insets returns
280          * a positive value. The problem with this choice is that navigating
281          * a formula becomes weird. For example, a selection extends only over
282          * about half of the symbol. In order to avoid this, with our math
283          * fonts we use QTextLine::naturalTextWidth, so that a superscript can
284          * be drawn right after the symbol, and move the subscript leftward by
285          * recording a negative value for the kerning.
286         */
287         int w = 0;
288         // is the string a single character from a math font ?
289 #if QT_VERSION >= 0x040800
290         bool const math_char = s.length() == 1 && font_.styleName() == "LyX";
291 #else
292         bool const math_char = s.length() == 1;
293 #endif
294         if (math_char) {
295                 QString const qs = toqstr(s);
296                 int br_width = metrics_.boundingRect(qs).width();
297 #if QT_VERSION >= 0x050b00
298                 int s_width = metrics_.horizontalAdvance(qs);
299 #else
300                 int s_width = metrics_.width(qs);
301 #endif
302                 // keep value 0 for math chars with width 0
303                 if (s_width != 0)
304                         w = max(br_width, s_width);
305         } else {
306                 QTextLayout tl;
307                 tl.setText(toqstr(s));
308                 tl.setFont(font_);
309                 tl.beginLayout();
310                 QTextLine line = tl.createLine();
311                 tl.endLayout();
312                 w = iround(line.horizontalAdvance());
313         }
314         strwidth_cache_.insert(s, w, s.size() * sizeof(char_type));
315         return w;
316 }
317
318
319 int GuiFontMetrics::width(QString const & ucs2) const
320 {
321         return width(qstring_to_ucs4(ucs2));
322 }
323
324
325 int GuiFontMetrics::signedWidth(docstring const & s) const
326 {
327         if (s.empty())
328                 return 0;
329
330         if (s[0] == '-')
331                 return -width(s.substr(1, s.size() - 1));
332         else
333                 return width(s);
334 }
335
336
337 uint qHash(TextLayoutKey const & key)
338 {
339         double params = (2 * key.rtl - 1) * key.ws;
340         return std::qHash(key.s) ^ ::qHash(params);
341 }
342
343
344 // This holds a translation table between the original string and the
345 // QString that we can use with QTextLayout.
346 struct TextLayoutHelper
347 {
348         /// Create the helper
349         /// \c s is the original string
350         /// \c isrtl is true if the string is right-to-left
351         /// \c naked is true to disable the insertion of zero width annotations
352         TextLayoutHelper(docstring const & s, bool isrtl, bool naked = false);
353
354         /// translate QString index to docstring index
355         docstring::size_type qpos2pos(int qpos) const
356         {
357                 return lower_bound(pos2qpos_.begin(), pos2qpos_.end(), qpos) - pos2qpos_.begin();
358         }
359
360         /// Translate docstring index to QString index
361         int pos2qpos(docstring::size_type pos) const { return pos2qpos_[pos]; }
362
363         // The original string
364         docstring docstr;
365         // The mirror string
366         QString qstr;
367         // is string right-to-left?
368         bool rtl;
369
370 private:
371         // This vector contains the QString pos for each string position
372         vector<int> pos2qpos_;
373 };
374
375
376 TextLayoutHelper::TextLayoutHelper(docstring const & s, bool isrtl, bool naked)
377         : docstr(s), rtl(isrtl)
378 {
379         // Reserve memory for performance purpose
380         pos2qpos_.reserve(s.size());
381         qstr.reserve(2 * s.size());
382
383         /* Qt will not break at a leading or trailing space, and we need
384          * that sometimes, see http://www.lyx.org/trac/ticket/9921.
385          *
386          * To work around the problem, we enclose the string between
387          * word joiner characters so that the QTextLayout algorithm will
388          * agree to break the text at these extremal spaces.
389          */
390         // Unicode character WORD JOINER
391         QChar const word_joiner(0x2060);
392         if (!naked)
393                 qstr += word_joiner;
394
395 #ifdef BIDI_USE_OVERRIDE
396         /* Unicode override characters enforce drawing direction
397          * Source: http://www.iamcal.com/understanding-bidirectional-text/
398          * Left-to-right override is 0x202d and right-to-left override is 0x202e.
399          */
400         if (!naked)
401                 qstr += QChar(rtl ? 0x202e : 0x202d);
402 #endif
403
404         // Now translate the string character-by-character.
405         bool was_space = false;
406         for (char_type const c : s) {
407                 // insert a word joiner character between consecutive spaces
408                 bool const is_space = isSpace(c);
409                 if (!naked && is_space && was_space)
410                         qstr += word_joiner;
411                 was_space = is_space;
412                 // Remember the QString index at this point
413                 pos2qpos_.push_back(qstr.size());
414                 // Performance: UTF-16 characters are easier
415                 if (is_utf16(c))
416                         qstr += ucs4_to_qchar(c);
417                 else
418                         qstr += toqstr(c);
419         }
420
421         // Final word joiner (see above)
422         if (!naked)
423                 qstr += word_joiner;
424
425         // Add virtual position at the end of the string
426         pos2qpos_.push_back(qstr.size());
427
428         //QString dump = qstr;
429         //LYXERR0("TLH: " << dump.replace(word_joiner, "|").toStdString());
430 }
431
432
433 namespace {
434
435 shared_ptr<QTextLayout>
436 getTextLayout_helper(TextLayoutHelper const & tlh, double const wordspacing,
437                      QFont font)
438 {
439         auto const ptl = make_shared<QTextLayout>();
440         ptl->setCacheEnabled(true);
441         font.setWordSpacing(wordspacing);
442         ptl->setFont(font);
443 #ifdef BIDI_USE_FLAG
444         /* Use undocumented flag to enforce drawing direction
445          * FIXME: This does not work with Qt 5.11 (ticket #11284).
446          */
447         ptl->setFlags(tlh.rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
448 #endif
449         ptl->setText(tlh.qstr);
450
451         ptl->beginLayout();
452         ptl->createLine();
453         ptl->endLayout();
454
455         return ptl;
456 }
457
458 }
459
460 shared_ptr<QTextLayout const>
461 GuiFontMetrics::getTextLayout(TextLayoutHelper const & tlh,
462                               double const wordspacing) const
463 {
464         PROFILE_THIS_BLOCK(getTextLayout_TLH);
465         TextLayoutKey key{tlh.docstr, tlh.rtl, wordspacing};
466         if (auto ptl = qtextlayout_cache_[key])
467                 return ptl;
468         PROFILE_CACHE_MISS(getTextLayout_TLH);
469         auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
470         qtextlayout_cache_.insert(key, ptl);
471         return ptl;
472 }
473
474
475 shared_ptr<QTextLayout const>
476 GuiFontMetrics::getTextLayout(docstring const & s, bool const rtl,
477                               double const wordspacing) const
478 {
479         PROFILE_THIS_BLOCK(getTextLayout);
480         TextLayoutKey key{s, rtl, wordspacing};
481         if (auto ptl = qtextlayout_cache_[key])
482                 return ptl;
483         PROFILE_CACHE_MISS(getTextLayout);
484         TextLayoutHelper tlh(s, rtl);
485         auto const ptl = getTextLayout_helper(tlh, wordspacing, font_);
486         qtextlayout_cache_.insert(key, ptl);
487         return ptl;
488 }
489
490
491 int GuiFontMetrics::pos2x(docstring const & s, int pos, bool const rtl,
492                           double const wordspacing) const
493 {
494         TextLayoutHelper tlh(s, rtl);
495         auto ptl = getTextLayout(tlh, wordspacing);
496         // pos can be negative, see #10506.
497         int const qpos = tlh.pos2qpos(max(pos, 0));
498         return static_cast<int>(ptl->lineForTextPosition(qpos).cursorToX(qpos));
499 }
500
501
502 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
503                           double const wordspacing) const
504 {
505         TextLayoutHelper tlh(s, rtl);
506         auto ptl = getTextLayout(tlh, wordspacing);
507         QTextLine const & tline = ptl->lineForTextPosition(0);
508         int qpos = tline.xToCursor(x);
509         int newx = static_cast<int>(tline.cursorToX(qpos));
510         // The value of qpos may be wrong in rtl text (see ticket #10569).
511         // To work around this, let's have a look at adjacent positions to
512         // see whether we find closer matches.
513         if (rtl && newx < x) {
514                 while (qpos > 0) {
515                         int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
516                         if (abs(xm - x) < abs(newx - x)) {
517                                 --qpos;
518                                 newx = xm;
519                         } else
520                                 break;
521                 }
522         } else if (rtl && newx > x) {
523                 while (qpos < tline.textLength()) {
524                         int const xp = static_cast<int>(tline.cursorToX(qpos + 1));
525                         if (abs(xp - x) < abs(newx - x)) {
526                                 ++qpos;
527                                 newx = xp;
528                         } else
529                                 break;
530                 }
531         }
532         // correct x value to the actual cursor position.
533         x = newx;
534
535         return tlh.qpos2pos(qpos);
536 }
537
538
539 FontMetrics::Breaks
540 GuiFontMetrics::breakString_helper(docstring const & s, int first_wid, int wid,
541                                    bool rtl, bool force) const
542 {
543         TextLayoutHelper const tlh(s, rtl);
544
545         QTextLayout tl;
546 #ifdef BIDI_USE_FLAG
547         /* Use undocumented flag to enforce drawing direction
548          * FIXME: This does not work with Qt 5.11 (ticket #11284).
549          */
550         tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
551 #endif
552         tl.setText(tlh.qstr);
553         tl.setFont(font_);
554         QTextOption to;
555         /*
556          * Some Asian languages split lines anywhere (no notion of
557          * word). It seems that QTextLayout is not aware of this fact.
558          * See for reference:
559          *    https://en.wikipedia.org/wiki/Line_breaking_rules_in_East_Asian_languages
560          *
561          * FIXME: Something shall be done about characters which are
562          * not allowed at the beginning or end of line.
563          */
564         to.setWrapMode(force ? QTextOption::WrapAtWordBoundaryOrAnywhere
565                              : QTextOption::WordWrap);
566         tl.setTextOption(to);
567
568         bool first = true;
569         tl.beginLayout();
570         while(true) {
571                 QTextLine line = tl.createLine();
572                 if (!line.isValid())
573                         break;
574                 line.setLineWidth(first ? first_wid : wid);
575                 first = false;
576         }
577         tl.endLayout();
578
579         Breaks breaks;
580         int pos = 0;
581         for (int i = 0 ; i < tl.lineCount() ; ++i) {
582                 QTextLine const & line = tl.lineAt(i);
583                 int const line_epos = line.textStart() + line.textLength();
584                 int const epos = tlh.qpos2pos(line_epos);
585 #if QT_VERSION >= 0x050000
586                 // This does not take trailing spaces into account, except for the last line.
587                 int const wid = iround(line.naturalTextWidth());
588                 // If the line is not the last one, trailing space is always omitted.
589                 int nspc_wid = wid;
590                 // For the last line, compute the width without trailing space
591                 if (i + 1 == tl.lineCount() && !s.empty() && isSpace(s.back())
592                     && line.textStart() <= tlh.pos2qpos(s.size() - 1))
593                         nspc_wid = iround(line.cursorToX(tlh.pos2qpos(s.size() - 1)));
594 #else
595                 // With some monospace fonts, the value of horizontalAdvance()
596                 // can be wrong with Qt4. One hypothesis is that the invisible
597                 // characters that we use are given a non-null width.
598                 // FIXME: this is slower than it could be but we'll get rid of Qt4 anyway
599                 docstring ss = s.substr(pos, epos - pos);
600                 int const wid = width(ss);
601                 if (!ss.empty() && isSpace(ss.back()))
602                         ss.pop_back();
603                 int const nspc_wid = i + 1 < tl.lineCount() ? width(ss) : wid;
604 #endif
605                 breaks.emplace_back(epos - pos, wid, nspc_wid);
606                 pos = epos;
607 #if 0
608                 // FIXME: should it be kept in some form?
609                 if ((force && line.textLength() == brkStrOffset) || line_wid > x)
610                         return {-1, line_wid};
611 #endif
612         }
613
614         return breaks;
615 }
616
617
618 uint qHash(BreakStringKey const & key)
619 {
620         // assume widths are less than 10000. This fits in 32 bits.
621         uint params = key.force + 2 * key.rtl + 4 * key.first_wid + 10000 * key.wid;
622         return std::qHash(key.s) ^ ::qHash(params);
623 }
624
625
626 FontMetrics::Breaks GuiFontMetrics::breakString(docstring const & s, int first_wid, int wid,
627                                                 bool rtl, bool force) const
628 {
629         PROFILE_THIS_BLOCK(breakString);
630         if (s.empty())
631                 return Breaks();
632
633         BreakStringKey key{s, first_wid, wid, rtl, force};
634         Breaks brks;
635         if (auto * brks_ptr = breakstr_cache_.object_ptr(key))
636                 brks = *brks_ptr;
637         else {
638                 PROFILE_CACHE_MISS(breakString);
639                 brks = breakString_helper(s, first_wid, wid, rtl, force);
640                 breakstr_cache_.insert(key, brks, sizeof(key) + s.size() * sizeof(char_type));
641         }
642         return brks;
643 }
644
645
646 void GuiFontMetrics::rectText(docstring const & str,
647         int & w, int & ascent, int & descent) const
648 {
649         // FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
650         int const offset = 4;
651
652         w = width(str) + offset;
653         ascent = metrics_.ascent() + offset / 2;
654         descent = metrics_.descent() + offset / 2;
655 }
656
657
658 void GuiFontMetrics::buttonText(docstring const & str, const int offset,
659         int & w, int & ascent, int & descent) const
660 {
661         rectText(str, w, ascent, descent);
662         w += offset;
663 }
664
665
666 Dimension const GuiFontMetrics::defaultDimension() const
667 {
668         return Dimension(0, maxAscent(), maxDescent());
669 }
670
671
672 Dimension const GuiFontMetrics::dimension(char_type c) const
673 {
674         return Dimension(width(c), ascent(c), descent(c));
675 }
676
677
678 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
679                 char_type c) const
680 {
681         QRect r;
682         if (is_utf16(c))
683                 r = metrics_.boundingRect(ucs4_to_qchar(c));
684         else
685                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
686
687         AscendDescend ad = { -r.top(), r.bottom() + 1};
688         // We could as well compute the width but this is not really
689         // needed for now as it is done directly in width() below.
690         metrics_cache_.insert(c, ad);
691
692         return ad;
693 }
694
695
696 int GuiFontMetrics::width(char_type c) const
697 {
698         int value = width_cache_.value(c, outOfLimitMetric);
699         if (value != outOfLimitMetric)
700                 return value;
701
702 #if QT_VERSION >= 0x050b00
703         if (is_utf16(c))
704                 value = metrics_.horizontalAdvance(ucs4_to_qchar(c));
705         else
706                 value = metrics_.horizontalAdvance(toqstr(docstring(1, c)));
707 #else
708         if (is_utf16(c))
709                 value = metrics_.width(ucs4_to_qchar(c));
710         else
711                 value = metrics_.width(toqstr(docstring(1, c)));
712 #endif
713
714         width_cache_.insert(c, value);
715
716         return value;
717 }
718
719
720 int GuiFontMetrics::ascent(char_type c) const
721 {
722         static AscendDescend const outOfLimitAD =
723                 {outOfLimitMetric, outOfLimitMetric};
724         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
725         if (value.ascent != outOfLimitMetric)
726                 return value.ascent;
727
728         value = fillMetricsCache(c);
729         return value.ascent;
730 }
731
732
733 int GuiFontMetrics::descent(char_type c) const
734 {
735         static AscendDescend const outOfLimitAD =
736                 {outOfLimitMetric, outOfLimitMetric};
737         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
738         if (value.descent != outOfLimitMetric)
739                 return value.descent;
740
741         value = fillMetricsCache(c);
742         return value.descent;
743 }
744
745 } // namespace frontend
746 } // namespace lyx