2 * \file GuiFontMetrics.C
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"
20 #include "support/unicode.h"
27 GuiFontMetrics::GuiFontMetrics(QFont const & font)
28 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
33 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
34 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
39 int GuiFontMetrics::maxAscent() const
41 return metrics_.ascent();
45 int GuiFontMetrics::maxDescent() const
47 // We add 1 as the value returned by QT is different than X
48 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
49 return metrics_.descent() + 1;
53 int GuiFontMetrics::lbearing(char_type c) const
55 return metrics_.leftBearing(ucs4_to_qchar(c));
59 int GuiFontMetrics::rbearing(char_type c) const
61 // Qt rbearing is from the right edge of the char's width().
62 QChar sc = ucs4_to_qchar(c);
63 return metrics_.width(sc) - metrics_.rightBearing(sc);
67 int GuiFontMetrics::smallcapsWidth(QString const & s) const
70 int const ls = s.size();
72 for (int i = 0; i < ls; ++i) {
73 QChar const & c = s[i];
74 QChar const uc = c.toUpper();
76 w += smallcaps_metrics_.width(uc);
78 w += metrics_.width(c);
84 int GuiFontMetrics::width(char_type const * s, size_t ls) const
86 // Caution: The following ucs4_to_something conversions work for
87 // symbol fonts only because they are no real conversions but simple
90 if (ls == 1 && !smallcaps_shape_) {
94 if (smallcaps_shape_) {
96 ucs4_to_qstring(s, ls, ucs2);
97 return smallcapsWidth(ucs2);
101 for (unsigned int i = 0; i < ls; ++i)
108 int GuiFontMetrics::width(QString const & ucs2) const
110 int const ls = ucs2.size();
111 if (ls == 1 && !smallcaps_shape_) {
112 return width(ucs2[0].unicode());
115 if (smallcaps_shape_)
116 return smallcapsWidth(ucs2);
119 for (int i = 0; i < ls; ++i)
120 w += width(ucs2[i].unicode());
126 int GuiFontMetrics::signedWidth(docstring const & s) const
132 return -width(&(s[1]), s.length() - 1);
134 return FontMetrics::width(s);
138 void GuiFontMetrics::rectText(docstring const & str,
139 int & w, int & ascent, int & descent) const
141 static int const d = 2;
142 w = FontMetrics::width(str) + d * 2 + 2;
143 ascent = metrics_.ascent() + d;
144 descent = metrics_.descent() + d;
149 void GuiFontMetrics::buttonText(docstring const & str,
150 int & w, int & ascent, int & descent) const
152 static int const d = 3;
153 w = FontMetrics::width(str) + d * 2 + 2;
154 ascent = metrics_.ascent() + d;
155 descent = metrics_.descent() + d;
158 #ifndef USE_LYX_FONTCACHE
160 int GuiFontMetrics::ascent(char_type c) const
162 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
167 int GuiFontMetrics::descent(char_type c) const
169 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
170 return r.bottom() + 1;
175 void GuiFontMetrics::fillMetricsCache(char_type c) const
177 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
178 AscendDescend ad = { -r.top(), r.bottom() + 1};
179 // We could as well compute the width but this is not really
180 // needed for now as it is done directly in width() below.
181 metrics_cache_.insert(c, ad);
185 int GuiFontMetrics::width(char_type c) const
187 if (!width_cache_.contains(c)) {
188 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
191 return width_cache_.value(c);
195 int GuiFontMetrics::ascent(char_type c) const
197 if (!metrics_cache_.contains(c))
200 return metrics_cache_.value(c).ascent;
204 int GuiFontMetrics::descent(char_type c) const
206 if (!metrics_cache_.contains(c))
209 return metrics_cache_.value(c).descent;