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"
28 GuiFontMetrics::GuiFontMetrics(QFont const & font)
29 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
31 #ifdef USE_LYX_FONTCACHE
32 for (int i = 0; i != 65536; ++i) {
33 metrics_cache_[i].width = -1000;
34 metrics_cache_[i].ascent = -1000;
35 metrics_cache_[i].descent = -1000;
41 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
42 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
47 int GuiFontMetrics::maxAscent() const
49 return metrics_.ascent();
53 int GuiFontMetrics::maxDescent() const
55 // We add 1 as the value returned by QT is different than X
56 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
57 return metrics_.descent() + 1;
61 int GuiFontMetrics::lbearing(char_type c) const
63 return metrics_.leftBearing(ucs4_to_qchar(c));
67 int GuiFontMetrics::rbearing(char_type c) const
69 // Qt rbearing is from the right edge of the char's width().
70 QChar sc = ucs4_to_qchar(c);
71 return metrics_.width(sc) - metrics_.rightBearing(sc);
75 int GuiFontMetrics::smallcapsWidth(QString const & s) const
78 int const ls = s.size();
80 for (int i = 0; i < ls; ++i) {
81 QChar const & c = s[i];
82 QChar const uc = c.toUpper();
84 w += smallcaps_metrics_.width(uc);
86 w += metrics_.width(c);
92 int GuiFontMetrics::width(char_type const * s, size_t ls) const
94 // Caution: The following ucs4_to_something conversions work for
95 // symbol fonts only because they are no real conversions but simple
98 if (ls == 1 && !smallcaps_shape_) {
102 if (smallcaps_shape_) {
104 ucs4_to_qstring(s, ls, ucs2);
105 return smallcapsWidth(ucs2);
109 for (unsigned int i = 0; i < ls; ++i)
116 int GuiFontMetrics::width(QString const & ucs2) const
118 int const ls = ucs2.size();
119 if (ls == 1 && !smallcaps_shape_) {
120 return width(ucs2[0].unicode());
123 if (smallcaps_shape_)
124 return smallcapsWidth(ucs2);
127 for (int i = 0; i < ls; ++i)
128 w += width(ucs2[i].unicode());
134 int GuiFontMetrics::signedWidth(docstring const & s) const
140 return -width(&(s[1]), s.length() - 1);
142 return FontMetrics::width(s);
146 void GuiFontMetrics::rectText(docstring const & str,
147 int & w, int & ascent, int & descent) const
149 static int const d = 2;
150 w = FontMetrics::width(str) + d * 2 + 2;
151 ascent = metrics_.ascent() + d;
152 descent = metrics_.descent() + d;
157 void GuiFontMetrics::buttonText(docstring const & str,
158 int & w, int & ascent, int & descent) const
160 static int const d = 3;
161 w = FontMetrics::width(str) + d * 2 + 2;
162 ascent = metrics_.ascent() + d;
163 descent = metrics_.descent() + d;
166 #ifndef USE_LYX_FONTCACHE
168 int GuiFontMetrics::ascent(char_type c) const
170 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
175 int GuiFontMetrics::descent(char_type c) const
177 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
178 return r.bottom() + 1;
183 void GuiFontMetrics::fillCache(unsigned short val) const
185 QRect const & r = metrics_.boundingRect(QChar(val));
186 metrics_cache_[val].descent = r.bottom() + 1;
187 metrics_cache_[val].ascent = -r.top();
188 // We could as well compute the width but this is not really
189 // needed for now as it is done directly in width() below.
190 //metrics_cache_[val].width = metrics_.width(QChar(val));
194 int GuiFontMetrics::width(char_type c) const
196 unsigned short val = static_cast<unsigned short>(c);
197 if (metrics_cache_[val].width == -1000)
198 metrics_cache_[val].width = metrics_.width(QChar(val));
200 return metrics_cache_[val].width;
204 int GuiFontMetrics::ascent(char_type c) const
206 unsigned short val = static_cast<unsigned short>(c);
207 if (metrics_cache_[val].ascent == -1000)
210 return metrics_cache_[val].ascent;
214 int GuiFontMetrics::descent(char_type c) const
216 unsigned short val = static_cast<unsigned short>(c);
217 if (metrics_cache_[val].descent == -1000)
220 return metrics_cache_[val].descent;