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>
29 using namespace lyx::support;
36 * Convert a UCS4 character into a QChar.
37 * This is a hack (it does only make sense for the common part of the UCS4
38 * and UTF16 encodings) and should not be used.
39 * This does only exist because of performance reasons (a real conversion
40 * using iconv is too slow on windows).
42 * This is no real conversion but a simple cast in reality. This is the reason
43 * why this works well for symbol fonts used in mathed too, even though
44 * these are not real ucs4 characters. These are codepoints in the
45 * computer modern fonts used, nothing unicode related.
46 * See comment in GuiPainter::text() for more explanation.
48 inline QChar const ucs4_to_qchar(char_type const ucs4)
50 LATTEST(is_utf16(ucs4));
51 return QChar(static_cast<unsigned short>(ucs4));
56 // Limit strwidth_cache_ size to 512kB of string data
57 GuiFontMetrics::GuiFontMetrics(QFont const & font)
58 : font_(font), metrics_(font, 0),
59 strwidth_cache_(1 << 19)
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::em() const
80 return QFontInfo(font_).pixelSize();
84 int GuiFontMetrics::lineWidth() const
86 return metrics_.lineWidth();
90 int GuiFontMetrics::underlinePos() const
92 return metrics_.underlinePos();
96 int GuiFontMetrics::strikeoutPos() const
98 return metrics_.strikeOutPos();
102 int GuiFontMetrics::lbearing(char_type c) const
105 // FIXME: QFontMetrics::leftBearing does not support the
106 // full unicode range. Once it does, we could use:
107 //return metrics_.leftBearing(toqstr(docstring(1, c)));
110 return metrics_.leftBearing(ucs4_to_qchar(c));
115 int const outOfLimitMetric = -10000;
119 int GuiFontMetrics::rbearing(char_type c) const
121 int value = rbearing_cache_.value(c, outOfLimitMetric);
122 if (value != outOfLimitMetric)
125 // Qt rbearing is from the right edge of the char's width().
127 QChar sc = ucs4_to_qchar(c);
128 value = width(c) - metrics_.rightBearing(sc);
130 // FIXME: QFontMetrics::leftBearing does not support the
131 // full unicode range. Once it does, we could use:
132 // metrics_.rightBearing(toqstr(docstring(1, c)));
136 rbearing_cache_.insert(c, value);
142 int GuiFontMetrics::width(docstring const & s) const
145 QByteArray(reinterpret_cast<char const *>(s.data()),
146 s.size() * sizeof(docstring::value_type));
147 int * pw = strwidth_cache_[qba];
150 int w = metrics_.width(toqstr(s));
151 strwidth_cache_.insert(qba, new int(w), qba.size());
156 int GuiFontMetrics::width(QString const & ucs2) const
158 return width(qstring_to_ucs4(ucs2));
162 int GuiFontMetrics::signedWidth(docstring const & s) const
168 return -width(s.substr(1, s.size() - 1));
175 QTextLayout const & getTextLayout(docstring const & s, QFont font,
176 bool const rtl, double const wordspacing)
178 static docstring old_s;
179 static QFont old_font;
180 static bool old_rtl = false;
181 // this invalid value is to make sure that it is reset on first try.
182 static double old_wordspacing = -1.0;
183 // This one is our trivial cache
184 static QTextLayout tl;
185 if (s != old_s || font != old_font || rtl != old_rtl
186 || wordspacing != old_wordspacing) {
187 tl.setText(toqstr(s));
188 font.setWordSpacing(wordspacing);
190 // Note that both setFlags and the enums are undocumented
191 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
198 old_wordspacing = wordspacing;
206 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
207 double const wordspacing) const
210 copy.setWordSpacing(wordspacing);
211 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
212 return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
216 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
217 double const wordspacing) const
219 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
220 int pos = tl.lineForTextPosition(0).xToCursor(x);
221 // correct x value to the actual cursor position.
222 x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
227 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
232 tl.setText(toqstr(s));
234 // Note that both setFlags and the enums are undocumented
235 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
237 to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
238 tl.setTextOption(to);
240 QTextLine line = tl.createLine();
241 line.setLineWidth(x);
244 if (int(line.naturalTextWidth()) > x)
246 x = int(line.naturalTextWidth());
247 s = s.substr(0, line.textLength());
252 void GuiFontMetrics::rectText(docstring const & str,
253 int & w, int & ascent, int & descent) const
255 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
257 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
258 ascent = metrics_.ascent() + d;
259 descent = metrics_.descent() + d;
264 void GuiFontMetrics::buttonText(docstring const & str,
265 int & w, int & ascent, int & descent) const
267 rectText(str, w, ascent, descent);
268 w += Inset::TEXT_TO_INSET_OFFSET;
272 Dimension const GuiFontMetrics::defaultDimension() const
274 return Dimension(0, maxAscent(), maxDescent());
278 Dimension const GuiFontMetrics::dimension(char_type c) const
280 return Dimension(width(c), ascent(c), descent(c));
284 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
289 r = metrics_.boundingRect(ucs4_to_qchar(c));
291 r = metrics_.boundingRect(toqstr(docstring(1, c)));
293 AscendDescend ad = { -r.top(), r.bottom() + 1};
294 // We could as well compute the width but this is not really
295 // needed for now as it is done directly in width() below.
296 metrics_cache_.insert(c, ad);
302 int GuiFontMetrics::width(char_type c) const
304 int value = width_cache_.value(c, outOfLimitMetric);
305 if (value != outOfLimitMetric)
309 value = metrics_.width(ucs4_to_qchar(c));
311 value = metrics_.width(toqstr(docstring(1, c)));
313 width_cache_.insert(c, value);
319 int GuiFontMetrics::ascent(char_type c) const
321 static AscendDescend const outOfLimitAD =
322 {outOfLimitMetric, outOfLimitMetric};
323 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
324 if (value.ascent != outOfLimitMetric)
327 value = fillMetricsCache(c);
332 int GuiFontMetrics::descent(char_type c) const
334 static AscendDescend const outOfLimitAD =
335 {outOfLimitMetric, outOfLimitMetric};
336 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
337 if (value.descent != outOfLimitMetric)
338 return value.descent;
340 value = fillMetricsCache(c);
341 return value.descent;
344 } // namespace frontend