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"
28 // Used for checking initialisation state of the C-ish metrics table.
29 const int BadMetrics = -1000;
33 GuiFontMetrics::GuiFontMetrics(QFont const & font)
34 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
36 #ifdef USE_LYX_FONTCACHE
37 for (int i = 0; i != MaxCharType; ++i) {
38 metrics_cache_[i].width = BadMetrics;
39 metrics_cache_[i].ascent = BadMetrics;
40 metrics_cache_[i].descent = BadMetrics;
46 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
47 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
52 int GuiFontMetrics::maxAscent() const
54 return metrics_.ascent();
58 int GuiFontMetrics::maxDescent() const
60 // We add 1 as the value returned by QT is different than X
61 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
62 return metrics_.descent() + 1;
66 int GuiFontMetrics::lbearing(char_type c) const
68 return metrics_.leftBearing(ucs4_to_qchar(c));
72 int GuiFontMetrics::rbearing(char_type c) const
74 // Qt rbearing is from the right edge of the char's width().
75 QChar sc = ucs4_to_qchar(c);
76 return metrics_.width(sc) - metrics_.rightBearing(sc);
80 int GuiFontMetrics::smallcapsWidth(QString const & s) const
83 int const ls = s.size();
85 for (int i = 0; i < ls; ++i) {
86 QChar const & c = s[i];
87 QChar const uc = c.toUpper();
89 w += smallcaps_metrics_.width(uc);
91 w += metrics_.width(c);
97 int GuiFontMetrics::width(char_type const * s, size_t ls) const
99 // Caution: The following ucs4_to_something conversions work for
100 // symbol fonts only because they are no real conversions but simple
103 if (ls == 1 && !smallcaps_shape_) {
107 if (smallcaps_shape_) {
109 ucs4_to_qstring(s, ls, ucs2);
110 return smallcapsWidth(ucs2);
114 for (unsigned int i = 0; i < ls; ++i)
121 int GuiFontMetrics::width(QString const & ucs2) const
123 int const ls = ucs2.size();
124 if (ls == 1 && !smallcaps_shape_) {
125 return width(ucs2[0].unicode());
128 if (smallcaps_shape_)
129 return smallcapsWidth(ucs2);
132 for (int i = 0; i < ls; ++i)
133 w += width(ucs2[i].unicode());
139 int GuiFontMetrics::signedWidth(docstring const & s) const
145 return -width(&(s[1]), s.length() - 1);
147 return FontMetrics::width(s);
151 void GuiFontMetrics::rectText(docstring const & str,
152 int & w, int & ascent, int & descent) const
154 static int const d = 2;
155 w = FontMetrics::width(str) + d * 2 + 2;
156 ascent = metrics_.ascent() + d;
157 descent = metrics_.descent() + d;
162 void GuiFontMetrics::buttonText(docstring const & str,
163 int & w, int & ascent, int & descent) const
165 static int const d = 3;
166 w = FontMetrics::width(str) + d * 2 + 2;
167 ascent = metrics_.ascent() + d;
168 descent = metrics_.descent() + d;
171 #ifndef USE_LYX_FONTCACHE
173 int GuiFontMetrics::ascent(char_type c) const
175 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
180 int GuiFontMetrics::descent(char_type c) const
182 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
183 return r.bottom() + 1;
188 void GuiFontMetrics::fillCache(unsigned short val) const
190 QRect const & r = metrics_.boundingRect(QChar(val));
191 metrics_cache_[val].descent = r.bottom() + 1;
192 metrics_cache_[val].ascent = -r.top();
193 // We could as well compute the width but this is not really
194 // needed for now as it is done directly in width() below.
195 //metrics_cache_[val].width = metrics_.width(QChar(val));
199 int GuiFontMetrics::width(char_type c) const
201 // FIXME: The following cast is not a real conversion but it work
202 // for the ucs2 subrange of unicode. Instead of an assertion we should
203 // give the metrics of some special characters that indicates that
204 // its display is not supported.
205 BOOST_ASSERT(c < MaxCharType);
206 unsigned short val = static_cast<unsigned short>(c);
207 if (metrics_cache_[val].width == BadMetrics)
208 metrics_cache_[val].width = metrics_.width(QChar(val));
210 return metrics_cache_[val].width;
214 int GuiFontMetrics::ascent(char_type c) const
216 // FIXME: The following cast is not a real conversion but it work
217 // for the ucs2 subrange of unicode. Instead of an assertion we should
218 // give the metrics of some special characters that indicates that
219 // its display is not supported.
220 BOOST_ASSERT(c < MaxCharType);
221 unsigned short val = static_cast<unsigned short>(c);
222 if (metrics_cache_[val].ascent == BadMetrics)
225 return metrics_cache_[val].ascent;
229 int GuiFontMetrics::descent(char_type c) const
231 // FIXME: The following cast is not a real conversion but it work
232 // for the ucs2 subrange of unicode. Instead of an assertion we should
233 // give the metrics of some special characters that indicates that
234 // its display is not supported.
235 BOOST_ASSERT(c < MaxCharType);
236 unsigned short val = static_cast<unsigned short>(c);
237 if (metrics_cache_[val].descent == BadMetrics)
240 return metrics_cache_[val].descent;