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"
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 // Qt rbearing is from the right edge of the char's width().
63 QChar sc = ucs4_to_qchar(c);
64 return metrics_.width(sc) - metrics_.rightBearing(sc);
68 int GuiFontMetrics::smallcapsWidth(QString const & s) const
71 int const ls = s.size();
73 for (int i = 0; i < ls; ++i) {
74 QChar const & c = s[i];
75 QChar const uc = c.toUpper();
77 w += smallcaps_metrics_.width(uc);
79 w += metrics_.width(c);
85 int GuiFontMetrics::width(docstring const & s) const
91 if (ls == 1 && !smallcaps_shape_) {
96 // Caution: The following ucs4 to QString conversions work
97 // for symbol fonts only because they are no real conversions
98 // but simple casts in reality. See comment in QLPainter::text()
99 // for more explanation.
100 return smallcapsWidth(toqstr(s));
103 for (unsigned int i = 0; i < ls; ++i)
110 int GuiFontMetrics::width(QString const & ucs2) const
112 int const ls = ucs2.size();
113 if (ls == 1 && !smallcaps_shape_) {
114 return width(ucs2[0].unicode());
117 if (smallcaps_shape_)
118 return smallcapsWidth(ucs2);
121 for (int i = 0; i < ls; ++i)
122 w += width(ucs2[i].unicode());
128 int GuiFontMetrics::signedWidth(docstring const & s) const
134 return -width(s.substr(1, s.size() - 1));
140 void GuiFontMetrics::rectText(docstring const & str,
141 int & w, int & ascent, int & descent) const
143 static int const d = 2;
144 w = width(str) + d * 2 + 2;
145 ascent = metrics_.ascent() + d;
146 descent = metrics_.descent() + d;
151 void GuiFontMetrics::buttonText(docstring const & str,
152 int & w, int & ascent, int & descent) const
154 static int const d = 3;
155 w = width(str) + d * 2 + 2;
156 ascent = metrics_.ascent() + d;
157 descent = metrics_.descent() + d;
161 Dimension const GuiFontMetrics::defaultDimension() const
163 return Dimension(0, maxAscent(), maxDescent());
167 Dimension const GuiFontMetrics::dimension(char_type c) const
169 return Dimension(width(c), ascent(c), descent(c));
173 void GuiFontMetrics::fillMetricsCache(char_type c) const
175 QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
176 AscendDescend ad = { -r.top(), r.bottom() + 1};
177 // We could as well compute the width but this is not really
178 // needed for now as it is done directly in width() below.
179 metrics_cache_.insert(c, ad);
183 int GuiFontMetrics::width(char_type c) const
185 if (smallcaps_shape_)
186 return smallcapsWidth(ucs4_to_qchar(c));
188 if (!width_cache_.contains(c)) {
189 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
192 return width_cache_.value(c);
196 int GuiFontMetrics::ascent(char_type c) const
198 if (!metrics_cache_.contains(c))
201 return metrics_cache_.value(c).ascent;
205 int GuiFontMetrics::descent(char_type c) const
207 if (!metrics_cache_.contains(c))
210 return metrics_cache_.value(c).descent;