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