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 GuiFontMetrics::GuiFontMetrics(QFont const & font) : font_(font), metrics_(font, 0)
61 int GuiFontMetrics::maxAscent() const
63 return metrics_.ascent();
67 int GuiFontMetrics::maxDescent() const
69 // We add 1 as the value returned by QT is different than X
70 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
71 return metrics_.descent() + 1;
75 int GuiFontMetrics::em() const
77 return QFontInfo(font_).pixelSize();
81 int GuiFontMetrics::lineWidth() const
83 return metrics_.lineWidth();
87 int GuiFontMetrics::underlinePos() const
89 return metrics_.underlinePos();
93 int GuiFontMetrics::strikeoutPos() const
95 return metrics_.strikeOutPos();
99 int GuiFontMetrics::lbearing(char_type c) const
102 // FIXME: QFontMetrics::leftBearing does not support the
103 // full unicode range. Once it does, we could use:
104 //return metrics_.leftBearing(toqstr(docstring(1, c)));
107 return metrics_.leftBearing(ucs4_to_qchar(c));
112 int const outOfLimitMetric = -10000;
116 int GuiFontMetrics::rbearing(char_type c) const
118 int value = rbearing_cache_.value(c, outOfLimitMetric);
119 if (value != outOfLimitMetric)
122 // Qt rbearing is from the right edge of the char's width().
124 QChar sc = ucs4_to_qchar(c);
125 value = width(c) - metrics_.rightBearing(sc);
127 // FIXME: QFontMetrics::leftBearing does not support the
128 // full unicode range. Once it does, we could use:
129 // metrics_.rightBearing(toqstr(docstring(1, c)));
133 rbearing_cache_.insert(c, value);
139 int GuiFontMetrics::width(docstring const & s) const
142 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
143 if (it != strwidth_cache_.end()) {
146 w = metrics_.width(toqstr(s));
147 strwidth_cache_[s] = w;
153 int GuiFontMetrics::width(QString const & ucs2) const
155 return width(qstring_to_ucs4(ucs2));
159 int GuiFontMetrics::signedWidth(docstring const & s) const
165 return -width(s.substr(1, s.size() - 1));
171 void setTextLayout(QTextLayout & tl, docstring const & s, QFont font,
172 bool const rtl, double const wordspacing)
174 tl.setText(toqstr(s));
175 font.setWordSpacing(wordspacing);
177 // Note that both setFlags and the enums are undocumented
178 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
186 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
187 double const wordspacing) const
191 copy.setWordSpacing(wordspacing);
192 setTextLayout(tl, s, font_, rtl, wordspacing);
193 return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
197 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
198 double const wordspacing) const
201 setTextLayout(tl, s, font_, rtl, wordspacing);
202 int pos = tl.lineForTextPosition(0).xToCursor(x);
203 // correct x value to the actual cursor position.
204 x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
209 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
214 tl.setText(toqstr(s));
216 // Note that both setFlags and the enums are undocumented
217 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
219 to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
220 tl.setTextOption(to);
222 QTextLine line = tl.createLine();
223 line.setLineWidth(x);
226 if (int(line.naturalTextWidth()) > x)
228 x = int(line.naturalTextWidth());
229 s = s.substr(0, line.textLength());
234 void GuiFontMetrics::rectText(docstring const & str,
235 int & w, int & ascent, int & descent) const
237 static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
239 w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
240 ascent = metrics_.ascent() + d;
241 descent = metrics_.descent() + d;
246 void GuiFontMetrics::buttonText(docstring const & str,
247 int & w, int & ascent, int & descent) const
249 rectText(str, w, ascent, descent);
250 w += Inset::TEXT_TO_INSET_OFFSET;
254 Dimension const GuiFontMetrics::defaultDimension() const
256 return Dimension(0, maxAscent(), maxDescent());
260 Dimension const GuiFontMetrics::dimension(char_type c) const
262 return Dimension(width(c), ascent(c), descent(c));
266 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
271 r = metrics_.boundingRect(ucs4_to_qchar(c));
273 r = metrics_.boundingRect(toqstr(docstring(1, c)));
275 AscendDescend ad = { -r.top(), r.bottom() + 1};
276 // We could as well compute the width but this is not really
277 // needed for now as it is done directly in width() below.
278 metrics_cache_.insert(c, ad);
284 int GuiFontMetrics::width(char_type c) const
286 int value = width_cache_.value(c, outOfLimitMetric);
287 if (value != outOfLimitMetric)
291 value = metrics_.width(ucs4_to_qchar(c));
293 value = metrics_.width(toqstr(docstring(1, c)));
295 width_cache_.insert(c, value);
301 int GuiFontMetrics::ascent(char_type c) const
303 static AscendDescend const outOfLimitAD =
304 {outOfLimitMetric, outOfLimitMetric};
305 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
306 if (value.ascent != outOfLimitMetric)
309 value = fillMetricsCache(c);
314 int GuiFontMetrics::descent(char_type c) const
316 static AscendDescend const outOfLimitAD =
317 {outOfLimitMetric, outOfLimitMetric};
318 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
319 if (value.descent != outOfLimitMetric)
320 return value.descent;
322 value = fillMetricsCache(c);
323 return value.descent;
326 } // namespace frontend