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"
31 GuiFontMetrics::GuiFontMetrics(QFont const & font)
32 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
34 #ifdef USE_LYX_FONTCACHE
35 for (int i = 0; i != 65536; ++i)
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::ascent(char_type c) const
63 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
68 int GuiFontMetrics::descent(char_type c) const
70 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
71 return r.bottom() + 1;
75 int GuiFontMetrics::lbearing(char_type c) const
77 return metrics_.leftBearing(ucs4_to_qchar(c));
81 int GuiFontMetrics::rbearing(char_type c) const
83 // Qt rbearing is from the right edge of the char's width().
84 QChar sc = ucs4_to_qchar(c);
85 return metrics_.width(sc) - metrics_.rightBearing(sc);
89 int GuiFontMetrics::smallcapsWidth(QString const & s) const
92 int const ls = s.size();
94 for (int i = 0; i < ls; ++i) {
95 QChar const & c = s[i];
96 QChar const uc = c.toUpper();
98 w += smallcaps_metrics_.width(uc);
100 w += metrics_.width(c);
106 int GuiFontMetrics::width(char_type const * s, size_t ls) const
108 // Caution: The following ucs4_to_something conversions work for
109 // symbol fonts only because they are no real conversions but simple
112 if (ls == 1 && !smallcaps_shape_) {
113 QChar const c = ucs4_to_qchar(s[0]);
114 return width(c.unicode());
118 ucs4_to_qstring(s, ls, ucs2);
120 if (smallcaps_shape_)
121 return smallcapsWidth(ucs2);
124 for (unsigned int i = 0; i < ls; ++i)
125 w += width(ucs2[i].unicode());
131 int GuiFontMetrics::width(QString const & ucs2) const
133 int const ls = ucs2.size();
134 if (ls == 1 && !smallcaps_shape_) {
135 return width(ucs2[0].unicode());
138 if (smallcaps_shape_)
139 return smallcapsWidth(ucs2);
142 for (int i = 0; i < ls; ++i)
143 w += width(ucs2[i].unicode());
149 int GuiFontMetrics::signedWidth(docstring const & s) const
155 return -width(&(s[1]), s.length() - 1);
157 return FontMetrics::width(s);
161 void GuiFontMetrics::rectText(docstring const & str,
162 int & w, int & ascent, int & descent) const
164 static int const d = 2;
165 w = FontMetrics::width(str) + d * 2 + 2;
166 ascent = metrics_.ascent() + d;
167 descent = metrics_.descent() + d;
172 void GuiFontMetrics::buttonText(docstring const & str,
173 int & w, int & ascent, int & descent) const
175 static int const d = 3;
176 w = FontMetrics::width(str) + d * 2 + 2;
177 ascent = metrics_.ascent() + d;
178 descent = metrics_.descent() + d;
181 #ifdef USE_LYX_FONTCACHE
182 int GuiFontMetrics::width(unsigned short val) const
184 if (widthcache_[val] == -1)
185 widthcache_[val] = metrics_.width(QChar(val));
186 return widthcache_[val];