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"
27 using namespace lyx::support;
34 * Convert a UCS4 character into a QChar.
35 * This is a hack (it does only make sense for the common part of the UCS4
36 * and UTF16 encodings) and should not be used.
37 * This does only exist because of performance reasons (a real conversion
38 * using iconv is too slow on windows).
40 * This is no real conversion but a simple cast in reality. This is the reason
41 * why this works well for symbol fonts used in mathed too, even though
42 * these are not real ucs4 characters. These are codepoints in the
43 * computer modern fonts used, nothing unicode related.
44 * See comment in GuiPainter::text() for more explanation.
46 inline QChar const ucs4_to_qchar(char_type const ucs4)
48 LATTEST(is_utf16(ucs4));
49 return QChar(static_cast<unsigned short>(ucs4));
54 // Limit strwidth_cache_ size to 512kB of string data
55 GuiFontMetrics::GuiFontMetrics(QFont const & font)
56 : font_(font), metrics_(font, 0),
57 strwidth_cache_(1 << 19),
58 tl_cache_rtl_(false), tl_cache_wordspacing_(-1.0)
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::em() const
79 return QFontInfo(font_).pixelSize();
83 int GuiFontMetrics::lineWidth() const
85 return metrics_.lineWidth();
89 int GuiFontMetrics::underlinePos() const
91 return metrics_.underlinePos();
95 int GuiFontMetrics::strikeoutPos() const
97 return metrics_.strikeOutPos();
101 int GuiFontMetrics::lbearing(char_type c) const
104 // FIXME: QFontMetrics::leftBearing does not support the
105 // full unicode range. Once it does, we could use:
106 //return metrics_.leftBearing(toqstr(docstring(1, c)));
109 return metrics_.leftBearing(ucs4_to_qchar(c));
114 int const outOfLimitMetric = -10000;
118 int GuiFontMetrics::rbearing(char_type c) const
120 int value = rbearing_cache_.value(c, outOfLimitMetric);
121 if (value != outOfLimitMetric)
124 // Qt rbearing is from the right edge of the char's width().
126 QChar sc = ucs4_to_qchar(c);
127 value = width(c) - metrics_.rightBearing(sc);
129 // FIXME: QFontMetrics::leftBearing does not support the
130 // full unicode range. Once it does, we could use:
131 // metrics_.rightBearing(toqstr(docstring(1, c)));
135 rbearing_cache_.insert(c, value);
141 int GuiFontMetrics::width(docstring const & s) const
144 QByteArray(reinterpret_cast<char const *>(s.data()),
145 s.size() * sizeof(docstring::value_type));
146 int * pw = strwidth_cache_[qba];
149 int w = metrics_.width(toqstr(s));
150 strwidth_cache_.insert(qba, new int(w), qba.size());
155 int GuiFontMetrics::width(QString const & ucs2) const
157 return width(qstring_to_ucs4(ucs2));
161 int GuiFontMetrics::signedWidth(docstring const & s) const
167 return -width(s.substr(1, s.size() - 1));
174 GuiFontMetrics::getTextLayout(docstring const & s, QFont font,
175 bool const rtl, double const wordspacing) const
177 if (s != tl_cache_s_ || font != tl_cache_font_ || rtl != tl_cache_rtl_
178 || wordspacing != tl_cache_wordspacing_) {
179 tl_cache_.setText(toqstr(s));
180 font.setWordSpacing(wordspacing);
181 tl_cache_.setFont(font);
182 // Note that both setFlags and the enums are undocumented
183 tl_cache_.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
184 tl_cache_.beginLayout();
185 tl_cache_.createLine();
186 tl_cache_.endLayout();
188 tl_cache_font_ = font;
190 tl_cache_wordspacing_ = wordspacing;
196 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
197 double const wordspacing) const
200 copy.setWordSpacing(wordspacing);
201 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
202 return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
206 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
207 double const wordspacing) const
209 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
210 int pos = tl.lineForTextPosition(0).xToCursor(x);
211 // correct x value to the actual cursor position.
212 x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
217 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
222 /* Qt will not break at a leading or trailing space, and we need
223 * that sometimes, see http://www.lyx.org/trac/ticket/9921.
225 * To work around the problem, we enclose the string between
226 * zero-width characters so that the QTextLayout algorithm will
227 * agree to break the text at these extremal spaces.
229 // Unicode character ZERO WIDTH NO-BREAK SPACE
230 QChar const zerow_nbsp(0xfeff);
231 tl.setText(zerow_nbsp + toqstr(s) + zerow_nbsp);
233 // Note that both setFlags and the enums are undocumented
234 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
236 to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
237 tl.setTextOption(to);
239 QTextLine line = tl.createLine();
240 line.setLineWidth(x);
243 if (int(line.naturalTextWidth()) > x)
245 x = int(line.naturalTextWidth());
246 // The -1 is here to account for the leading zerow_nbsp.
247 s = s.substr(0, line.textLength() - 1);
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