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"
34 GuiFontMetrics::GuiFontMetrics(QFont const & font)
35 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
40 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
41 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
46 int GuiFontMetrics::maxAscent() const
50 return metrics_.ascent();
54 int GuiFontMetrics::maxDescent() const
58 // We add 1 as the value returned by QT is different than X
59 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
60 return metrics_.descent() + 1;
64 int GuiFontMetrics::ascent(char_type c) const
68 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
69 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
70 // value by the height: (x, -y-height, width, height).
71 // Other versions return: (x, -y, width, height)
72 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
73 return -r.top() - r.height();
80 int GuiFontMetrics::descent(char_type c) const
84 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
85 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
86 // value by the height: (x, -y-height, width, height).
87 // Other versions return: (x, -y, width, height)
88 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
89 return r.bottom() + r.height() + 1;
91 return r.bottom() + 1;
96 int GuiFontMetrics::lbearing(char_type c) const
100 return metrics_.leftBearing(ucs4_to_qchar(c));
104 int GuiFontMetrics::rbearing(char_type c) const
109 // Qt rbearing is from the right edge of the char's width().
110 QChar sc = ucs4_to_qchar(c);
111 return metrics_.width(sc) - metrics_.rightBearing(sc);
115 int GuiFontMetrics::smallcapsWidth(QString const & s) const
121 int const ls = s.size();
123 for (int i = 0; i < ls; ++i) {
124 QChar const & c = s[i];
125 QChar const uc = c.toUpper();
127 w += smallcaps_metrics_.width(uc);
129 w += metrics_.width(c);
135 int GuiFontMetrics::width(char_type const * s, size_t ls) const
141 ucs4_to_qstring(s, ls, ucs2);
143 if (smallcaps_shape_)
144 return smallcapsWidth(ucs2);
147 return width(ucs2[0].unicode());
150 for (unsigned int i = 0; i < ls; ++i)
151 w += width(ucs2[i].unicode());
157 int GuiFontMetrics::signedWidth(docstring const & s) const
160 return -FontMetrics::width(s.substr(1, s.length() - 1));
162 return FontMetrics::width(s);
166 void GuiFontMetrics::rectText(docstring const & str,
167 int & w, int & ascent, int & descent) const
169 static int const d = 2;
170 w = FontMetrics::width(str) + d * 2 + 2;
171 ascent = metrics_.ascent() + d;
172 descent = metrics_.descent() + d;
177 void GuiFontMetrics::buttonText(docstring const & str,
178 int & w, int & ascent, int & descent) const
180 static int const d = 3;
181 w = FontMetrics::width(str) + d * 2 + 2;
182 ascent = metrics_.ascent() + d;
183 descent = metrics_.descent() + d;
186 #ifdef USE_LYX_FONTCACHE
187 int GuiFontMetrics::width(unsigned short val) const
189 GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val);
190 if (cit != widthcache.end())
193 int const w = metrics_.width(QChar(val));