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 "support/unicode.h"
23 #include <boost/assert.hpp>
31 * Convert a UCS4 character into a QChar.
32 * This is a hack (it does only make sense for the common part of the UCS4
33 * and UTF16 encodings) and should not be used.
34 * This does only exist because of performance reasons (a real conversion
35 * using iconv is too slow on windows).
37 static inline QChar const ucs4_to_qchar(char_type const ucs4)
39 BOOST_ASSERT(is_utf16(ucs4));
40 return QChar(static_cast<unsigned short>(ucs4));
44 // Caution: When using ucs4_to_qchar() in these methods, this is no
45 // real conversion but a simple cast in reality. This is the reason
46 // why this works well for symbol fonts used in mathed too, even though
47 // these are not real ucs4 characters. These are codepoints in the
48 // modern fonts used, nothing unicode related.
49 // See comment in QLPainter::text() for more explanation.
51 GuiFontMetrics::GuiFontMetrics(QFont const & font)
52 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
57 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
58 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
63 int GuiFontMetrics::maxAscent() const
65 return metrics_.ascent();
69 int GuiFontMetrics::maxDescent() const
71 // We add 1 as the value returned by QT is different than X
72 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
73 return metrics_.descent() + 1;
77 int GuiFontMetrics::lbearing(char_type c) const
80 // FIXME: QFontMetrics::leftBearingdoes not support the
81 // full unicode range. Once it does, we could use:
82 //return metrics_.leftBearing(toqstr(docstring(1,c)));
85 return metrics_.leftBearing(ucs4_to_qchar(c));
90 int const outOfLimitMetric = -10000;
94 int GuiFontMetrics::rbearing(char_type c) const
96 int value = rbearing_cache_.value(c, outOfLimitMetric);
97 if (value != outOfLimitMetric)
100 // Qt rbearing is from the right edge of the char's width().
102 QChar sc = ucs4_to_qchar(c);
103 value = width(c) - metrics_.rightBearing(sc);
105 // FIXME: QFontMetrics::leftBearing does not support the
106 // full unicode range. Once it does, we could use:
107 // metrics_.rightBearing(toqstr(docstring(1,c)));
111 rbearing_cache_.insert(c, value);
117 int GuiFontMetrics::smallcapsWidth(char_type c) const
119 // FIXME: Optimisation probably needed: we don't use the width cache.
121 QChar const qc = ucs4_to_qchar(c);
122 QChar const uc = qc.toUpper();
124 return smallcaps_metrics_.width(uc);
126 return metrics_.width(qc);
128 QString const s = toqstr(docstring(1,c));
129 QString const us = s.toUpper();
131 return smallcaps_metrics_.width(us);
133 return metrics_.width(s);
138 int GuiFontMetrics::width(docstring const & s) const
140 size_t ls = s.size();
142 for (unsigned int i = 0; i < ls; ++i) {
143 //FIXME: we need to detect surrogate pairs and act accordingly
145 if isSurrogateBase(s[i]) {
147 if (smallcaps_shape_)
148 w += metrics_.width(toqstr(c + s[i + 1]));
150 w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
162 int GuiFontMetrics::width(QString const & ucs2) const
164 return width(qstring_to_ucs4(ucs2));
168 int GuiFontMetrics::signedWidth(docstring const & s) const
174 return -width(s.substr(1, s.size() - 1));
180 void GuiFontMetrics::rectText(docstring const & str,
181 int & w, int & ascent, int & descent) const
183 static int const d = 2;
184 w = width(str) + d * 2 + 2;
185 ascent = metrics_.ascent() + d;
186 descent = metrics_.descent() + d;
191 void GuiFontMetrics::buttonText(docstring const & str,
192 int & w, int & ascent, int & descent) const
194 static int const d = 3;
195 w = width(str) + d * 2 + 2;
196 ascent = metrics_.ascent() + d;
197 descent = metrics_.descent() + d;
201 Dimension const GuiFontMetrics::defaultDimension() const
203 return Dimension(0, maxAscent(), maxDescent());
207 Dimension const GuiFontMetrics::dimension(char_type c) const
209 return Dimension(width(c), ascent(c), descent(c));
213 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
218 r = metrics_.boundingRect(ucs4_to_qchar(c));
220 r = metrics_.boundingRect(toqstr(docstring(1,c)));
222 AscendDescend ad = { -r.top(), r.bottom() + 1};
223 // We could as well compute the width but this is not really
224 // needed for now as it is done directly in width() below.
225 metrics_cache_.insert(c, ad);
231 int GuiFontMetrics::width(char_type c) const
233 if (smallcaps_shape_)
234 return smallcapsWidth(c);
236 int value = width_cache_.value(c, outOfLimitMetric);
237 if (value != outOfLimitMetric)
241 value = metrics_.width(ucs4_to_qchar(c));
243 value = metrics_.width(toqstr(docstring(1, c)));
245 width_cache_.insert(c, value);
251 int GuiFontMetrics::ascent(char_type c) const
253 static AscendDescend const outOfLimitAD =
254 {outOfLimitMetric, outOfLimitMetric};
255 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
256 if (value.ascent != outOfLimitMetric)
259 value = fillMetricsCache(c);
264 int GuiFontMetrics::descent(char_type c) const
266 static AscendDescend const outOfLimitAD =
267 {outOfLimitMetric, outOfLimitMetric};
268 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
269 if (value.descent != outOfLimitMetric)
270 return value.descent;
272 value = fillMetricsCache(c);
273 return value.descent;
276 } // namespace frontend