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 if (ls == 1 && !smallcaps_shape_) {
109 QChar const c = ucs4_to_qchar(s[0]);
110 return width(c.unicode());
114 ucs4_to_qstring(s, ls, ucs2);
116 if (smallcaps_shape_)
117 return smallcapsWidth(ucs2);
120 for (unsigned int i = 0; i < ls; ++i)
121 w += width(ucs2[i].unicode());
127 int GuiFontMetrics::width(QString const & ucs2) const
129 int const ls = ucs2.size();
130 if (ls == 1 && !smallcaps_shape_) {
131 return width(ucs2[0].unicode());
134 if (smallcaps_shape_)
135 return smallcapsWidth(ucs2);
138 for (int i = 0; i < ls; ++i)
139 w += width(ucs2[i].unicode());
145 int GuiFontMetrics::signedWidth(docstring const & s) const
148 return -width(&(s[1]), s.length() - 1);
150 return FontMetrics::width(s);
154 void GuiFontMetrics::rectText(docstring const & str,
155 int & w, int & ascent, int & descent) const
157 static int const d = 2;
158 w = FontMetrics::width(str) + d * 2 + 2;
159 ascent = metrics_.ascent() + d;
160 descent = metrics_.descent() + d;
165 void GuiFontMetrics::buttonText(docstring const & str,
166 int & w, int & ascent, int & descent) const
168 static int const d = 3;
169 w = FontMetrics::width(str) + d * 2 + 2;
170 ascent = metrics_.ascent() + d;
171 descent = metrics_.descent() + d;
174 #ifdef USE_LYX_FONTCACHE
175 int GuiFontMetrics::width(unsigned short val) const
177 if (widthcache_[val] == -1)
178 widthcache_[val] = metrics_.width(QChar(val));
179 return widthcache_[val];