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"
35 GuiFontMetrics::GuiFontMetrics(QFont const & font)
36 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
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
51 return metrics_.ascent();
55 int GuiFontMetrics::maxDescent() const
59 // We add 1 as the value returned by QT is different than X
60 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
61 return metrics_.descent() + 1;
65 int GuiFontMetrics::ascent(char_type c) const
69 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
70 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
71 // value by the height: (x, -y-height, width, height).
72 // Other versions return: (x, -y, width, height)
73 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
74 return -r.top() - r.height();
81 int GuiFontMetrics::descent(char_type c) const
85 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
86 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
87 // value by the height: (x, -y-height, width, height).
88 // Other versions return: (x, -y, width, height)
89 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
90 return r.bottom() + r.height() + 1;
92 return r.bottom() + 1;
97 int GuiFontMetrics::lbearing(char_type c) const
101 return metrics_.leftBearing(ucs4_to_qchar(c));
105 int GuiFontMetrics::rbearing(char_type c) const
110 // Qt rbearing is from the right edge of the char's width().
111 QChar sc = ucs4_to_qchar(c);
112 return metrics_.width(sc) - metrics_.rightBearing(sc);
116 int GuiFontMetrics::smallcapsWidth(QString const & s) const
122 int const ls = s.length();
124 for (int i = 0; i < ls; ++i) {
125 QChar const & c = s[i];
126 QChar const uc = c.upper();
128 w += smallcaps_metrics_.width(uc);
130 w += metrics_.width(c);
136 int GuiFontMetrics::width(char_type const * s, size_t ls) const
141 QString const ucs2 = toqstr(s, ls);
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));