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 tl.setText(toqstr(s));
224 // Note that both setFlags and the enums are undocumented
225 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
227 to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
228 tl.setTextOption(to);
230 QTextLine line = tl.createLine();
231 line.setLineWidth(x);
234 if (int(line.naturalTextWidth()) > x)
236 x = int(line.naturalTextWidth());
237 s = s.substr(0, line.textLength());
242 void GuiFontMetrics::rectText(docstring const & str,
243 int & w, int & ascent, int & descent) const
245 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
247 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
248 ascent = metrics_.ascent() + d;
249 descent = metrics_.descent() + d;
254 void GuiFontMetrics::buttonText(docstring const & str,
255 int & w, int & ascent, int & descent) const
257 rectText(str, w, ascent, descent);
258 w += Inset::TEXT_TO_INSET_OFFSET;
262 Dimension const GuiFontMetrics::defaultDimension() const
264 return Dimension(0, maxAscent(), maxDescent());
268 Dimension const GuiFontMetrics::dimension(char_type c) const
270 return Dimension(width(c), ascent(c), descent(c));
274 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
279 r = metrics_.boundingRect(ucs4_to_qchar(c));
281 r = metrics_.boundingRect(toqstr(docstring(1, c)));
283 AscendDescend ad = { -r.top(), r.bottom() + 1};
284 // We could as well compute the width but this is not really
285 // needed for now as it is done directly in width() below.
286 metrics_cache_.insert(c, ad);
292 int GuiFontMetrics::width(char_type c) const
294 int value = width_cache_.value(c, outOfLimitMetric);
295 if (value != outOfLimitMetric)
299 value = metrics_.width(ucs4_to_qchar(c));
301 value = metrics_.width(toqstr(docstring(1, c)));
303 width_cache_.insert(c, value);
309 int GuiFontMetrics::ascent(char_type c) const
311 static AscendDescend const outOfLimitAD =
312 {outOfLimitMetric, outOfLimitMetric};
313 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
314 if (value.ascent != outOfLimitMetric)
317 value = fillMetricsCache(c);
322 int GuiFontMetrics::descent(char_type c) const
324 static AscendDescend const outOfLimitAD =
325 {outOfLimitMetric, outOfLimitMetric};
326 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
327 if (value.descent != outOfLimitMetric)
328 return value.descent;
330 value = fillMetricsCache(c);
331 return value.descent;
334 } // namespace frontend