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"
26 #include <QTextLayout>
35 * Convert a UCS4 character into a QChar.
36 * This is a hack (it does only make sense for the common part of the UCS4
37 * and UTF16 encodings) and should not be used.
38 * This does only exist because of performance reasons (a real conversion
39 * using iconv is too slow on windows).
41 * This is no real conversion but a simple cast in reality. This is the reason
42 * why this works well for symbol fonts used in mathed too, even though
43 * these are not real ucs4 characters. These are codepoints in the
44 * computer modern fonts used, nothing unicode related.
45 * See comment in QLPainter::text() for more explanation.
47 inline QChar const ucs4_to_qchar(char_type const ucs4)
49 LATTEST(is_utf16(ucs4));
50 return QChar(static_cast<unsigned short>(ucs4));
55 GuiFontMetrics::GuiFontMetrics(QFont const & font) : font_(font), metrics_(font, 0)
60 int GuiFontMetrics::maxAscent() const
62 return metrics_.ascent();
66 int GuiFontMetrics::maxDescent() const
68 // We add 1 as the value returned by QT is different than X
69 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
70 return metrics_.descent() + 1;
74 int GuiFontMetrics::lbearing(char_type c) const
77 // FIXME: QFontMetrics::leftBearingdoes not support the
78 // full unicode range. Once it does, we could use:
79 //return metrics_.leftBearing(toqstr(docstring(1, c)));
82 return metrics_.leftBearing(ucs4_to_qchar(c));
87 int const outOfLimitMetric = -10000;
91 int GuiFontMetrics::rbearing(char_type c) const
93 int value = rbearing_cache_.value(c, outOfLimitMetric);
94 if (value != outOfLimitMetric)
97 // Qt rbearing is from the right edge of the char's width().
99 QChar sc = ucs4_to_qchar(c);
100 value = width(c) - metrics_.rightBearing(sc);
102 // FIXME: QFontMetrics::leftBearing does not support the
103 // full unicode range. Once it does, we could use:
104 // metrics_.rightBearing(toqstr(docstring(1, c)));
108 rbearing_cache_.insert(c, value);
114 int GuiFontMetrics::width(docstring const & s) const
117 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
118 if (it != strwidth_cache_.end()) {
121 w = metrics_.width(toqstr(s));
122 strwidth_cache_[s] = w;
128 int GuiFontMetrics::width(QString const & ucs2) const
130 return width(qstring_to_ucs4(ucs2));
134 int GuiFontMetrics::signedWidth(docstring const & s) const
140 return -width(s.substr(1, s.size() - 1));
146 void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
150 /* In LyX, the character direction is forced by the language.
151 * Therefore, we have to signal that fact to Qt.
152 * Source: http://www.iamcal.com/understanding-bidirectional-text/
154 // Left-to-right override: forces to draw text left-to-right
155 char_type const LRO = 0x202D;
156 // Right-to-left override: forces to draw text right-to-left
157 char_type const RLO = 0x202E;
158 // Pop directional formatting: return to previous state
159 char_type const PDF = 0x202C;
161 qs = toqstr(RLO + s + PDF);
163 qs = toqstr(LRO + s + PDF);
174 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl) const
177 setTextLayout(tl, s, font_, rtl);
178 return tl.lineForTextPosition(pos + 1).cursorToX(pos + 1);
182 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl) const
185 setTextLayout(tl, s, font_, rtl);
186 int pos = tl.lineForTextPosition(0).xToCursor(x);
187 // take into account the unicode formatting characters
190 if (pos > int(s.length()))
192 // correct x value to the actual cursor position.
193 x = tl.lineForTextPosition(0).cursorToX(pos + 1);
198 void GuiFontMetrics::rectText(docstring const & str,
199 int & w, int & ascent, int & descent) const
201 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
203 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
204 ascent = metrics_.ascent() + d;
205 descent = metrics_.descent() + d;
210 void GuiFontMetrics::buttonText(docstring const & str,
211 int & w, int & ascent, int & descent) const
213 rectText(str, w, ascent, descent);
214 w += Inset::TEXT_TO_INSET_OFFSET;
218 Dimension const GuiFontMetrics::defaultDimension() const
220 return Dimension(0, maxAscent(), maxDescent());
224 Dimension const GuiFontMetrics::dimension(char_type c) const
226 return Dimension(width(c), ascent(c), descent(c));
230 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
235 r = metrics_.boundingRect(ucs4_to_qchar(c));
237 r = metrics_.boundingRect(toqstr(docstring(1, c)));
239 AscendDescend ad = { -r.top(), r.bottom() + 1};
240 // We could as well compute the width but this is not really
241 // needed for now as it is done directly in width() below.
242 metrics_cache_.insert(c, ad);
248 int GuiFontMetrics::width(char_type c) const
250 int value = width_cache_.value(c, outOfLimitMetric);
251 if (value != outOfLimitMetric)
255 value = metrics_.width(ucs4_to_qchar(c));
257 value = metrics_.width(toqstr(docstring(1, c)));
259 width_cache_.insert(c, value);
265 int GuiFontMetrics::ascent(char_type c) const
267 static AscendDescend const outOfLimitAD =
268 {outOfLimitMetric, outOfLimitMetric};
269 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
270 if (value.ascent != outOfLimitMetric)
273 value = fillMetricsCache(c);
278 int GuiFontMetrics::descent(char_type c) const
280 static AscendDescend const outOfLimitAD =
281 {outOfLimitMetric, outOfLimitMetric};
282 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
283 if (value.descent != outOfLimitMetric)
284 return value.descent;
286 value = fillMetricsCache(c);
287 return value.descent;
290 } // namespace frontend