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));
64 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
65 // value by the height: (x, -y-height, width, height).
66 // Other versions return: (x, -y, width, height)
67 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
68 return -r.top() - r.height();
75 int GuiFontMetrics::descent(char_type c) const
77 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
78 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
79 // value by the height: (x, -y-height, width, height).
80 // Other versions return: (x, -y, width, height)
81 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
82 return r.bottom() + r.height() + 1;
84 return r.bottom() + 1;
89 int GuiFontMetrics::lbearing(char_type c) const
91 return metrics_.leftBearing(ucs4_to_qchar(c));
95 int GuiFontMetrics::rbearing(char_type c) const
97 // Qt rbearing is from the right edge of the char's width().
98 QChar sc = ucs4_to_qchar(c);
99 return metrics_.width(sc) - metrics_.rightBearing(sc);
103 int GuiFontMetrics::smallcapsWidth(QString const & s) const
106 int const ls = s.size();
108 for (int i = 0; i < ls; ++i) {
109 QChar const & c = s[i];
110 QChar const uc = c.toUpper();
112 w += smallcaps_metrics_.width(uc);
114 w += metrics_.width(c);
120 int GuiFontMetrics::width(char_type const * s, size_t ls) const
122 if (ls == 1 && !smallcaps_shape_) {
123 QChar const c = ucs4_to_qchar(s[0]);
124 return width(c.unicode());
128 ucs4_to_qstring(s, ls, ucs2);
130 if (smallcaps_shape_)
131 return smallcapsWidth(ucs2);
134 for (unsigned int i = 0; i < ls; ++i)
135 w += width(ucs2[i].unicode());
141 int GuiFontMetrics::width(QString const & ucs2) const
143 int const ls = ucs2.size();
144 if (ls == 1 && !smallcaps_shape_) {
145 return width(ucs2[0].unicode());
148 if (smallcaps_shape_)
149 return smallcapsWidth(ucs2);
152 for (int i = 0; i < ls; ++i)
153 w += width(ucs2[i].unicode());
159 int GuiFontMetrics::signedWidth(docstring const & s) const
162 return -FontMetrics::width(s.substr(1, s.length() - 1));
164 return FontMetrics::width(s);
168 void GuiFontMetrics::rectText(docstring const & str,
169 int & w, int & ascent, int & descent) const
171 static int const d = 2;
172 w = FontMetrics::width(str) + d * 2 + 2;
173 ascent = metrics_.ascent() + d;
174 descent = metrics_.descent() + d;
179 void GuiFontMetrics::buttonText(docstring const & str,
180 int & w, int & ascent, int & descent) const
182 static int const d = 3;
183 w = FontMetrics::width(str) + d * 2 + 2;
184 ascent = metrics_.ascent() + d;
185 descent = metrics_.descent() + d;
188 #ifdef USE_LYX_FONTCACHE
189 int GuiFontMetrics::width(unsigned short val) const
191 if (widthcache_[val] == -1)
192 widthcache_[val] = metrics_.width(QChar(val));
193 return widthcache_[val];