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"
19 #include "Dimension.h"
21 #include <boost/assert.hpp>
29 * Convert a UCS4 character into a QChar.
30 * This is a hack (it does only make sense for the common part of the UCS4
31 * and UTF16 encodings) and should not be used.
32 * This does only exist because of performance reasons (a real conversion
33 * using iconv is too slow on windows).
35 static inline QChar const ucs4_to_qchar(char_type const ucs4)
37 BOOST_ASSERT(is_utf16(ucs4));
38 return QChar(static_cast<unsigned short>(ucs4));
42 // Caution: When using ucs4_to_qchar() in these methods, this is no
43 // real conversion but a simple cast in reality. This is the reason
44 // why this works well for symbol fonts used in mathed too, even though
45 // these are not real ucs4 characters. These are codepoints in the
46 // modern fonts used, nothing unicode related.
47 // See comment in QLPainter::text() for more explanation.
49 GuiFontMetrics::GuiFontMetrics(QFont const & font)
50 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
55 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
56 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
61 int GuiFontMetrics::maxAscent() const
63 return metrics_.ascent();
67 int GuiFontMetrics::maxDescent() const
69 // We add 1 as the value returned by QT is different than X
70 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
71 return metrics_.descent() + 1;
75 int GuiFontMetrics::lbearing(char_type c) const
78 // FIXME: QFontMetrics::leftBearingdoes not support the
79 // full unicode range. Once it does, we could use:
80 //return metrics_.leftBearing(toqstr(docstring(1,c)));
83 return metrics_.leftBearing(ucs4_to_qchar(c));
88 int const outOfLimitMetric = -10000;
92 int GuiFontMetrics::rbearing(char_type c) const
94 int value = rbearing_cache_.value(c, outOfLimitMetric);
95 if (value != outOfLimitMetric)
98 // Qt rbearing is from the right edge of the char's width().
100 QChar sc = ucs4_to_qchar(c);
101 value = width(c) - metrics_.rightBearing(sc);
103 // FIXME: QFontMetrics::leftBearing does not support the
104 // full unicode range. Once it does, we could use:
105 // metrics_.rightBearing(toqstr(docstring(1,c)));
109 rbearing_cache_.insert(c, value);
115 int GuiFontMetrics::smallcapsWidth(char_type c) const
117 // FIXME: Optimisation probably needed: we don't use the width cache.
119 QChar const qc = ucs4_to_qchar(c);
120 QChar const uc = qc.toUpper();
122 return smallcaps_metrics_.width(uc);
124 return metrics_.width(qc);
126 QString const s = toqstr(docstring(1,c));
127 QString const us = s.toUpper();
129 return smallcaps_metrics_.width(us);
131 return metrics_.width(s);
136 int GuiFontMetrics::width(docstring const & s) const
138 size_t ls = s.size();
140 for (unsigned int i = 0; i < ls; ++i) {
141 //FIXME: we need to detect surrogate pairs and act accordingly
143 if isSurrogateBase(s[i]) {
145 if (smallcaps_shape_)
146 w += metrics_.width(toqstr(c + s[i + 1]));
148 w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
160 int GuiFontMetrics::width(QString const & ucs2) const
162 return width(qstring_to_ucs4(ucs2));
166 int GuiFontMetrics::signedWidth(docstring const & s) const
172 return -width(s.substr(1, s.size() - 1));
178 void GuiFontMetrics::rectText(docstring const & str,
179 int & w, int & ascent, int & descent) const
181 static int const d = 2;
182 w = width(str) + d * 2 + 2;
183 ascent = metrics_.ascent() + d;
184 descent = metrics_.descent() + d;
189 void GuiFontMetrics::buttonText(docstring const & str,
190 int & w, int & ascent, int & descent) const
192 static int const d = 3;
193 w = width(str) + d * 2 + 2;
194 ascent = metrics_.ascent() + d;
195 descent = metrics_.descent() + d;
199 Dimension const GuiFontMetrics::defaultDimension() const
201 return Dimension(0, maxAscent(), maxDescent());
205 Dimension const GuiFontMetrics::dimension(char_type c) const
207 return Dimension(width(c), ascent(c), descent(c));
211 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
216 r = metrics_.boundingRect(ucs4_to_qchar(c));
218 r = metrics_.boundingRect(toqstr(docstring(1,c)));
220 AscendDescend ad = { -r.top(), r.bottom() + 1};
221 // We could as well compute the width but this is not really
222 // needed for now as it is done directly in width() below.
223 metrics_cache_.insert(c, ad);
229 int GuiFontMetrics::width(char_type c) const
231 if (smallcaps_shape_)
232 return smallcapsWidth(c);
234 int value = width_cache_.value(c, outOfLimitMetric);
235 if (value != outOfLimitMetric)
239 value = metrics_.width(ucs4_to_qchar(c));
241 value = metrics_.width(toqstr(docstring(1, c)));
243 width_cache_.insert(c, value);
249 int GuiFontMetrics::ascent(char_type c) const
251 static AscendDescend const outOfLimitAD =
252 {outOfLimitMetric, outOfLimitMetric};
253 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
254 if (value.ascent != outOfLimitMetric)
257 value = fillMetricsCache(c);
262 int GuiFontMetrics::descent(char_type c) const
264 static AscendDescend const outOfLimitAD =
265 {outOfLimitMetric, outOfLimitMetric};
266 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
267 if (value.descent != outOfLimitMetric)
268 return value.descent;
270 value = fillMetricsCache(c);
271 return value.descent;
274 } // namespace frontend