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)
37 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
38 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
43 int GuiFontMetrics::maxAscent() const
45 return metrics_.ascent();
49 int GuiFontMetrics::maxDescent() const
51 // We add 1 as the value returned by QT is different than X
52 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
53 return metrics_.descent() + 1;
57 int GuiFontMetrics::ascent(char_type c) const
59 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
60 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
61 // value by the height: (x, -y-height, width, height).
62 // Other versions return: (x, -y, width, height)
63 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
64 return -r.top() - r.height();
71 int GuiFontMetrics::descent(char_type c) const
73 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
74 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
75 // value by the height: (x, -y-height, width, height).
76 // Other versions return: (x, -y, width, height)
77 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
78 return r.bottom() + r.height() + 1;
80 return r.bottom() + 1;
85 int GuiFontMetrics::lbearing(char_type c) const
87 return metrics_.leftBearing(ucs4_to_qchar(c));
91 int GuiFontMetrics::rbearing(char_type c) const
93 // Qt rbearing is from the right edge of the char's width().
94 QChar sc = ucs4_to_qchar(c);
95 return metrics_.width(sc) - metrics_.rightBearing(sc);
99 int GuiFontMetrics::smallcapsWidth(QString const & s) const
102 int const ls = s.size();
104 for (int i = 0; i < ls; ++i) {
105 QChar const & c = s[i];
106 QChar const uc = c.toUpper();
108 w += smallcaps_metrics_.width(uc);
110 w += metrics_.width(c);
116 int GuiFontMetrics::width(char_type const * s, size_t ls) const
119 ucs4_to_qstring(s, ls, ucs2);
121 if (smallcaps_shape_)
122 return smallcapsWidth(ucs2);
125 return width(ucs2[0].unicode());
128 for (unsigned int i = 0; i < ls; ++i)
129 w += width(ucs2[i].unicode());
135 int GuiFontMetrics::signedWidth(docstring const & s) const
138 return -FontMetrics::width(s.substr(1, s.length() - 1));
140 return FontMetrics::width(s);
144 void GuiFontMetrics::rectText(docstring const & str,
145 int & w, int & ascent, int & descent) const
147 static int const d = 2;
148 w = FontMetrics::width(str) + d * 2 + 2;
149 ascent = metrics_.ascent() + d;
150 descent = metrics_.descent() + d;
155 void GuiFontMetrics::buttonText(docstring const & str,
156 int & w, int & ascent, int & descent) const
158 static int const d = 3;
159 w = FontMetrics::width(str) + d * 2 + 2;
160 ascent = metrics_.ascent() + d;
161 descent = metrics_.descent() + d;
164 #ifdef USE_LYX_FONTCACHE
165 int GuiFontMetrics::width(unsigned short val) const
167 GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val);
168 if (cit != widthcache.end())
171 int const w = metrics_.width(QChar(val));