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 // For some reason QMetrics::width returns a wrong value with Qt5
150 // int w = metrics_.width(toqstr(s));
152 tl.setText(toqstr(s));
155 QTextLine line = tl.createLine();
157 int w = int(line.naturalTextWidth());
159 strwidth_cache_.insert(qba, new int(w), qba.size());
164 int GuiFontMetrics::width(QString const & ucs2) const
166 return width(qstring_to_ucs4(ucs2));
170 int GuiFontMetrics::signedWidth(docstring const & s) const
176 return -width(s.substr(1, s.size() - 1));
183 GuiFontMetrics::getTextLayout(docstring const & s, QFont font,
184 bool const rtl, double const wordspacing) const
186 if (s != tl_cache_s_ || font != tl_cache_font_ || rtl != tl_cache_rtl_
187 || wordspacing != tl_cache_wordspacing_) {
188 tl_cache_.setText(toqstr(s));
189 font.setWordSpacing(wordspacing);
190 tl_cache_.setFont(font);
191 // Note that both setFlags and the enums are undocumented
192 tl_cache_.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
193 tl_cache_.beginLayout();
194 tl_cache_.createLine();
195 tl_cache_.endLayout();
197 tl_cache_font_ = font;
199 tl_cache_wordspacing_ = wordspacing;
205 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
206 double const wordspacing) const
208 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
209 return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
213 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
214 double const wordspacing) const
216 QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
217 int pos = tl.lineForTextPosition(0).xToCursor(x);
218 // correct x value to the actual cursor position.
219 x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
224 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
229 /* Qt will not break at a leading or trailing space, and we need
230 * that sometimes, see http://www.lyx.org/trac/ticket/9921.
232 * To work around the problem, we enclose the string between
233 * zero-width characters so that the QTextLayout algorithm will
234 * agree to break the text at these extremal spaces.
236 // Unicode character ZERO WIDTH NO-BREAK SPACE
237 QChar const zerow_nbsp(0xfeff);
238 QString str = zerow_nbsp + toqstr(s) + zerow_nbsp;
240 /* Use unicode override characters to enforce drawing direction
241 * Source: http://www.iamcal.com/understanding-bidirectional-text/
244 // Right-to-left override: forces to draw text right-to-left
245 str = QChar(0x202E) + str;
247 // Left-to-right override: forces to draw text left-to-right
248 str = QChar(0x202D) + str;
249 int const offset = 2;
251 // Alternative version that breaks with Qt5 and arabic text (#10436)
252 // Note that both setFlags and the enums are undocumented
253 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
254 int const offset = 1;
260 to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
261 tl.setTextOption(to);
263 QTextLine line = tl.createLine();
264 line.setLineWidth(x);
267 if ((force && line.textLength() == offset) || int(line.naturalTextWidth()) > x)
269 x = int(line.naturalTextWidth());
270 // The offset is here to account for the extra leading characters.
271 s = s.substr(0, line.textLength() - offset);
276 void GuiFontMetrics::rectText(docstring const & str,
277 int & w, int & ascent, int & descent) const
279 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
281 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
282 ascent = metrics_.ascent() + d;
283 descent = metrics_.descent() + d;
288 void GuiFontMetrics::buttonText(docstring const & str,
289 int & w, int & ascent, int & descent) const
291 rectText(str, w, ascent, descent);
292 w += Inset::TEXT_TO_INSET_OFFSET;
296 Dimension const GuiFontMetrics::defaultDimension() const
298 return Dimension(0, maxAscent(), maxDescent());
302 Dimension const GuiFontMetrics::dimension(char_type c) const
304 return Dimension(width(c), ascent(c), descent(c));
308 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
313 r = metrics_.boundingRect(ucs4_to_qchar(c));
315 r = metrics_.boundingRect(toqstr(docstring(1, c)));
317 AscendDescend ad = { -r.top(), r.bottom() + 1};
318 // We could as well compute the width but this is not really
319 // needed for now as it is done directly in width() below.
320 metrics_cache_.insert(c, ad);
326 int GuiFontMetrics::width(char_type c) const
328 int value = width_cache_.value(c, outOfLimitMetric);
329 if (value != outOfLimitMetric)
333 value = metrics_.width(ucs4_to_qchar(c));
335 value = metrics_.width(toqstr(docstring(1, c)));
337 width_cache_.insert(c, value);
343 int GuiFontMetrics::ascent(char_type c) const
345 static AscendDescend const outOfLimitAD =
346 {outOfLimitMetric, outOfLimitMetric};
347 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
348 if (value.ascent != outOfLimitMetric)
351 value = fillMetricsCache(c);
356 int GuiFontMetrics::descent(char_type c) const
358 static AscendDescend const outOfLimitAD =
359 {outOfLimitMetric, outOfLimitMetric};
360 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
361 if (value.descent != outOfLimitMetric)
362 return value.descent;
364 value = fillMetricsCache(c);
365 return value.descent;
368 } // namespace frontend