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"
22 #include "insets/Inset.h"
24 #include "support/lassert.h"
33 * Convert a UCS4 character into a QChar.
34 * This is a hack (it does only make sense for the common part of the UCS4
35 * and UTF16 encodings) and should not be used.
36 * This does only exist because of performance reasons (a real conversion
37 * using iconv is too slow on windows).
39 * This is no real conversion but a simple cast in reality. This is the reason
40 * why this works well for symbol fonts used in mathed too, even though
41 * these are not real ucs4 characters. These are codepoints in the
42 * computer modern fonts used, nothing unicode related.
43 * See comment in QLPainter::text() for more explanation.
45 inline QChar const ucs4_to_qchar(char_type const ucs4)
47 LATTEST(is_utf16(ucs4));
48 return QChar(static_cast<unsigned short>(ucs4));
53 GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font, 0)
58 int GuiFontMetrics::maxAscent() const
60 return metrics_.ascent();
64 int GuiFontMetrics::maxDescent() const
66 // We add 1 as the value returned by QT is different than X
67 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
68 return metrics_.descent() + 1;
72 int GuiFontMetrics::lbearing(char_type c) const
75 // FIXME: QFontMetrics::leftBearingdoes not support the
76 // full unicode range. Once it does, we could use:
77 //return metrics_.leftBearing(toqstr(docstring(1, c)));
80 return metrics_.leftBearing(ucs4_to_qchar(c));
85 int const outOfLimitMetric = -10000;
89 int GuiFontMetrics::rbearing(char_type c) const
91 int value = rbearing_cache_.value(c, outOfLimitMetric);
92 if (value != outOfLimitMetric)
95 // Qt rbearing is from the right edge of the char's width().
97 QChar sc = ucs4_to_qchar(c);
98 value = width(c) - metrics_.rightBearing(sc);
100 // FIXME: QFontMetrics::leftBearing does not support the
101 // full unicode range. Once it does, we could use:
102 // metrics_.rightBearing(toqstr(docstring(1, c)));
106 rbearing_cache_.insert(c, value);
112 int GuiFontMetrics::width(docstring const & s) const
115 if (lyxrc.force_paint_single_char) {
116 size_t const ls = s.size();
117 for (size_t i = 0; i < ls; ++i)
120 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
121 if (it != strwidth_cache_.end()) {
124 w = metrics_.width(toqstr(s));
125 strwidth_cache_[s] = w;
132 int GuiFontMetrics::width(QString const & ucs2) const
134 return width(qstring_to_ucs4(ucs2));
138 int GuiFontMetrics::signedWidth(docstring const & s) const
144 return -width(s.substr(1, s.size() - 1));
150 void GuiFontMetrics::rectText(docstring const & str,
151 int & w, int & ascent, int & descent) const
153 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
155 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
156 ascent = metrics_.ascent() + d;
157 descent = metrics_.descent() + d;
162 void GuiFontMetrics::buttonText(docstring const & str,
163 int & w, int & ascent, int & descent) const
165 rectText(str, w, ascent, descent);
166 w += Inset::TEXT_TO_INSET_OFFSET;
170 Dimension const GuiFontMetrics::defaultDimension() const
172 return Dimension(0, maxAscent(), maxDescent());
176 Dimension const GuiFontMetrics::dimension(char_type c) const
178 return Dimension(width(c), ascent(c), descent(c));
182 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
187 r = metrics_.boundingRect(ucs4_to_qchar(c));
189 r = metrics_.boundingRect(toqstr(docstring(1, c)));
191 AscendDescend ad = { -r.top(), r.bottom() + 1};
192 // We could as well compute the width but this is not really
193 // needed for now as it is done directly in width() below.
194 metrics_cache_.insert(c, ad);
200 int GuiFontMetrics::width(char_type c) const
202 int value = width_cache_.value(c, outOfLimitMetric);
203 if (value != outOfLimitMetric)
207 value = metrics_.width(ucs4_to_qchar(c));
209 value = metrics_.width(toqstr(docstring(1, c)));
211 width_cache_.insert(c, value);
217 int GuiFontMetrics::ascent(char_type c) const
219 static AscendDescend const outOfLimitAD =
220 {outOfLimitMetric, outOfLimitMetric};
221 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
222 if (value.ascent != outOfLimitMetric)
225 value = fillMetricsCache(c);
230 int GuiFontMetrics::descent(char_type c) const
232 static AscendDescend const outOfLimitAD =
233 {outOfLimitMetric, outOfLimitMetric};
234 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
235 if (value.descent != outOfLimitMetric)
236 return value.descent;
238 value = fillMetricsCache(c);
239 return value.descent;
242 } // namespace frontend