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"
25 #include "support/lstrings.h"
27 #include <QTextLayout>
30 using namespace lyx::support;
37 * Convert a UCS4 character into a QChar.
38 * This is a hack (it does only make sense for the common part of the UCS4
39 * and UTF16 encodings) and should not be used.
40 * This does only exist because of performance reasons (a real conversion
41 * using iconv is too slow on windows).
43 * This is no 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 * computer modern fonts used, nothing unicode related.
47 * See comment in GuiPainter::text() for more explanation.
49 inline QChar const ucs4_to_qchar(char_type const ucs4)
51 LATTEST(is_utf16(ucs4));
52 return QChar(static_cast<unsigned short>(ucs4));
57 GuiFontMetrics::GuiFontMetrics(QFont const & font) : font_(font), metrics_(font, 0)
62 int GuiFontMetrics::maxAscent() const
64 return metrics_.ascent();
68 int GuiFontMetrics::maxDescent() const
70 // We add 1 as the value returned by QT is different than X
71 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
72 return metrics_.descent() + 1;
76 int GuiFontMetrics::lbearing(char_type c) const
79 // FIXME: QFontMetrics::leftBearingdoes not support the
80 // full unicode range. Once it does, we could use:
81 //return metrics_.leftBearing(toqstr(docstring(1, c)));
84 return metrics_.leftBearing(ucs4_to_qchar(c));
89 int const outOfLimitMetric = -10000;
93 int GuiFontMetrics::rbearing(char_type c) const
95 int value = rbearing_cache_.value(c, outOfLimitMetric);
96 if (value != outOfLimitMetric)
99 // Qt rbearing is from the right edge of the char's width().
101 QChar sc = ucs4_to_qchar(c);
102 value = width(c) - metrics_.rightBearing(sc);
104 // FIXME: QFontMetrics::leftBearing does not support the
105 // full unicode range. Once it does, we could use:
106 // metrics_.rightBearing(toqstr(docstring(1, c)));
110 rbearing_cache_.insert(c, value);
116 int GuiFontMetrics::width(docstring const & s) const
119 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
120 if (it != strwidth_cache_.end()) {
123 w = metrics_.width(toqstr(s));
124 strwidth_cache_[s] = w;
130 int GuiFontMetrics::width(QString const & ucs2) const
132 return width(qstring_to_ucs4(ucs2));
136 int GuiFontMetrics::signedWidth(docstring const & s) const
142 return -width(s.substr(1, s.size() - 1));
148 void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
151 QString const qs = toqstr(directedString(s, rtl));
161 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl) const
164 setTextLayout(tl, s, font_, rtl);
165 // we take into account the unicode formatting characters
166 return tl.lineForTextPosition(pos + 1).cursorToX(pos + 1);
170 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl) const
173 setTextLayout(tl, s, font_, rtl);
174 int pos = tl.lineForTextPosition(0).xToCursor(x);
175 // take into account the unicode formatting characters
178 if (pos > int(s.length()))
180 // correct x value to the actual cursor position.
181 x = tl.lineForTextPosition(0).cursorToX(pos + 1);
186 void GuiFontMetrics::rectText(docstring const & str,
187 int & w, int & ascent, int & descent) const
189 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
191 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
192 ascent = metrics_.ascent() + d;
193 descent = metrics_.descent() + d;
198 void GuiFontMetrics::buttonText(docstring const & str,
199 int & w, int & ascent, int & descent) const
201 rectText(str, w, ascent, descent);
202 w += Inset::TEXT_TO_INSET_OFFSET;
206 Dimension const GuiFontMetrics::defaultDimension() const
208 return Dimension(0, maxAscent(), maxDescent());
212 Dimension const GuiFontMetrics::dimension(char_type c) const
214 return Dimension(width(c), ascent(c), descent(c));
218 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
223 r = metrics_.boundingRect(ucs4_to_qchar(c));
225 r = metrics_.boundingRect(toqstr(docstring(1, c)));
227 AscendDescend ad = { -r.top(), r.bottom() + 1};
228 // We could as well compute the width but this is not really
229 // needed for now as it is done directly in width() below.
230 metrics_cache_.insert(c, ad);
236 int GuiFontMetrics::width(char_type c) const
238 int value = width_cache_.value(c, outOfLimitMetric);
239 if (value != outOfLimitMetric)
243 value = metrics_.width(ucs4_to_qchar(c));
245 value = metrics_.width(toqstr(docstring(1, c)));
247 width_cache_.insert(c, value);
253 int GuiFontMetrics::ascent(char_type c) const
255 static AscendDescend const outOfLimitAD =
256 {outOfLimitMetric, outOfLimitMetric};
257 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
258 if (value.ascent != outOfLimitMetric)
261 value = fillMetricsCache(c);
266 int GuiFontMetrics::descent(char_type c) const
268 static AscendDescend const outOfLimitAD =
269 {outOfLimitMetric, outOfLimitMetric};
270 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
271 if (value.descent != outOfLimitMetric)
272 return value.descent;
274 value = fillMetricsCache(c);
275 return value.descent;
278 } // namespace frontend