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 "insets/Inset.h"
23 #include "support/lassert.h"
32 * Convert a UCS4 character into a QChar.
33 * This is a hack (it does only make sense for the common part of the UCS4
34 * and UTF16 encodings) and should not be used.
35 * This does only exist because of performance reasons (a real conversion
36 * using iconv is too slow on windows).
38 * This is no real conversion but a simple cast in reality. This is the reason
39 * why this works well for symbol fonts used in mathed too, even though
40 * these are not real ucs4 characters. These are codepoints in the
41 * modern fonts used, nothing unicode related.
42 * See comment in QLPainter::text() for more explanation.
44 inline QChar const ucs4_to_qchar(char_type const ucs4)
46 LASSERT(is_utf16(ucs4), /**/);
47 return QChar(static_cast<unsigned short>(ucs4));
52 GuiFontMetrics::GuiFontMetrics(QFont const & font)
53 : metrics_(font, 0), smallcaps_metrics_(font), smallcaps_shape_(false)
58 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
59 : metrics_(font, 0), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
64 int GuiFontMetrics::maxAscent() const
66 return metrics_.ascent();
70 int GuiFontMetrics::maxDescent() const
72 // We add 1 as the value returned by QT is different than X
73 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
74 return metrics_.descent() + 1;
78 int GuiFontMetrics::lbearing(char_type c) const
81 // FIXME: QFontMetrics::leftBearingdoes not support the
82 // full unicode range. Once it does, we could use:
83 //return metrics_.leftBearing(toqstr(docstring(1, c)));
86 return metrics_.leftBearing(ucs4_to_qchar(c));
91 int const outOfLimitMetric = -10000;
95 int GuiFontMetrics::rbearing(char_type c) const
97 int value = rbearing_cache_.value(c, outOfLimitMetric);
98 if (value != outOfLimitMetric)
101 // Qt rbearing is from the right edge of the char's width().
103 QChar sc = ucs4_to_qchar(c);
104 value = width(c) - metrics_.rightBearing(sc);
106 // FIXME: QFontMetrics::leftBearing does not support the
107 // full unicode range. Once it does, we could use:
108 // metrics_.rightBearing(toqstr(docstring(1, c)));
112 rbearing_cache_.insert(c, value);
118 int GuiFontMetrics::smallcapsWidth(char_type c) const
120 // FIXME: Optimisation probably needed: we don't use the width cache.
122 QChar const qc = ucs4_to_qchar(c);
123 QChar const uc = qc.toUpper();
125 return smallcaps_metrics_.width(uc);
127 return metrics_.width(qc);
129 QString const s = toqstr(docstring(1, c));
130 QString const us = s.toUpper();
132 return smallcaps_metrics_.width(us);
134 return metrics_.width(s);
139 int GuiFontMetrics::width(docstring const & s) const
141 size_t ls = s.size();
143 for (unsigned int i = 0; i < ls; ++i) {
144 //FIXME: we need to detect surrogate pairs and act accordingly
146 if isSurrogateBase(s[i]) {
148 if (smallcaps_shape_)
149 w += metrics_.width(toqstr(c + s[i + 1]));
151 w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
163 int GuiFontMetrics::width(QString const & ucs2) const
165 return width(qstring_to_ucs4(ucs2));
169 int GuiFontMetrics::signedWidth(docstring const & s) const
175 return -width(s.substr(1, s.size() - 1));
181 void GuiFontMetrics::rectText(docstring const & str,
182 int & w, int & ascent, int & descent) const
184 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
186 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
187 ascent = metrics_.ascent() + d;
188 descent = metrics_.descent() + d;
193 void GuiFontMetrics::buttonText(docstring const & str,
194 int & w, int & ascent, int & descent) const
196 rectText(str, w, ascent, descent);
197 w += Inset::TEXT_TO_INSET_OFFSET;
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