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 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
116 if (it != strwidth_cache_.end()) {
119 w = metrics_.width(toqstr(s));
120 strwidth_cache_[s] = w;
126 int GuiFontMetrics::width(QString const & ucs2) const
128 return width(qstring_to_ucs4(ucs2));
132 int GuiFontMetrics::signedWidth(docstring const & s) const
138 return -width(s.substr(1, s.size() - 1));
144 void GuiFontMetrics::rectText(docstring const & str,
145 int & w, int & ascent, int & descent) const
147 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
149 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
150 ascent = metrics_.ascent() + d;
151 descent = metrics_.descent() + d;
156 void GuiFontMetrics::buttonText(docstring const & str,
157 int & w, int & ascent, int & descent) const
159 rectText(str, w, ascent, descent);
160 w += Inset::TEXT_TO_INSET_OFFSET;
164 Dimension const GuiFontMetrics::defaultDimension() const
166 return Dimension(0, maxAscent(), maxDescent());
170 Dimension const GuiFontMetrics::dimension(char_type c) const
172 return Dimension(width(c), ascent(c), descent(c));
176 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
181 r = metrics_.boundingRect(ucs4_to_qchar(c));
183 r = metrics_.boundingRect(toqstr(docstring(1, c)));
185 AscendDescend ad = { -r.top(), r.bottom() + 1};
186 // We could as well compute the width but this is not really
187 // needed for now as it is done directly in width() below.
188 metrics_cache_.insert(c, ad);
194 int GuiFontMetrics::width(char_type c) const
196 int value = width_cache_.value(c, outOfLimitMetric);
197 if (value != outOfLimitMetric)
201 value = metrics_.width(ucs4_to_qchar(c));
203 value = metrics_.width(toqstr(docstring(1, c)));
205 width_cache_.insert(c, value);
211 int GuiFontMetrics::ascent(char_type c) const
213 static AscendDescend const outOfLimitAD =
214 {outOfLimitMetric, outOfLimitMetric};
215 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
216 if (value.ascent != outOfLimitMetric)
219 value = fillMetricsCache(c);
224 int GuiFontMetrics::descent(char_type c) const
226 static AscendDescend const outOfLimitAD =
227 {outOfLimitMetric, outOfLimitMetric};
228 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
229 if (value.descent != outOfLimitMetric)
230 return value.descent;
232 value = fillMetricsCache(c);
233 return value.descent;
236 } // namespace frontend