2 * \file GuiFontMetrics.cpp
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"
19 #include "Dimension.h"
21 #include "support/unicode.h"
28 GuiFontMetrics::GuiFontMetrics(QFont const & font)
29 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
34 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
35 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
40 int GuiFontMetrics::maxAscent() const
42 return metrics_.ascent();
46 int GuiFontMetrics::maxDescent() const
48 // We add 1 as the value returned by QT is different than X
49 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
50 return metrics_.descent() + 1;
54 int GuiFontMetrics::lbearing(char_type c) const
56 return metrics_.leftBearing(ucs4_to_qchar(c));
60 int GuiFontMetrics::rbearing(char_type c) const
62 if (!rbearing_cache_.contains(c)) {
63 // Qt rbearing is from the right edge of the char's width().
64 QChar sc = ucs4_to_qchar(c);
65 int rb = metrics_.width(sc) - metrics_.rightBearing(sc);
66 rbearing_cache_.insert(c, rb);
68 return rbearing_cache_.value(c);
72 int GuiFontMetrics::smallcapsWidth(QString const & s) const
75 int const ls = s.size();
77 for (int i = 0; i < ls; ++i) {
78 QChar const & c = s[i];
79 QChar const uc = c.toUpper();
81 w += smallcaps_metrics_.width(uc);
83 w += metrics_.width(c);
89 int GuiFontMetrics::width(docstring const & s) const
95 if (ls == 1 && !smallcaps_shape_) {
100 // Caution: The following ucs4 to QString conversions work
101 // for symbol fonts only because they are no real conversions
102 // but simple casts in reality. See comment in QLPainter::text()
103 // for more explanation.
104 return smallcapsWidth(toqstr(s));
107 for (unsigned int i = 0; i < ls; ++i)
114 int GuiFontMetrics::width(QString const & ucs2) const
116 int const ls = ucs2.size();
117 if (ls == 1 && !smallcaps_shape_) {
118 return width(ucs2[0].unicode());
121 if (smallcaps_shape_)
122 return smallcapsWidth(ucs2);
125 for (int i = 0; i < ls; ++i)
126 w += width(ucs2[i].unicode());
132 int GuiFontMetrics::signedWidth(docstring const & s) const
138 return -width(s.substr(1, s.size() - 1));
144 void GuiFontMetrics::rectText(docstring const & str,
145 int & w, int & ascent, int & descent) const
147 static int const d = 2;
148 w = 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 = width(str) + d * 2 + 2;
160 ascent = metrics_.ascent() + d;
161 descent = metrics_.descent() + d;
165 Dimension const GuiFontMetrics::defaultDimension() const
167 return Dimension(0, maxAscent(), maxDescent());
171 Dimension const GuiFontMetrics::dimension(char_type c) const
173 return Dimension(width(c), ascent(c), descent(c));
177 void GuiFontMetrics::fillMetricsCache(char_type c) const
179 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
180 AscendDescend ad = { -r.top(), r.bottom() + 1};
181 // We could as well compute the width but this is not really
182 // needed for now as it is done directly in width() below.
183 metrics_cache_.insert(c, ad);
187 int GuiFontMetrics::width(char_type c) const
189 if (smallcaps_shape_)
190 return smallcapsWidth(ucs4_to_qchar(c));
192 if (!width_cache_.contains(c)) {
193 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
196 return width_cache_.value(c);
200 int GuiFontMetrics::ascent(char_type c) const
202 if (!metrics_cache_.contains(c))
205 return metrics_cache_.value(c).ascent;
209 int GuiFontMetrics::descent(char_type c) const
211 if (!metrics_cache_.contains(c))
214 return metrics_cache_.value(c).descent;