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 // Caution: When using ucs4_to_qchar() in these methods, this is no
29 // real conversion but a simple cast in reality. This is the reason
30 // why this works well for symbol fonts used in mathed too, even though
31 // these are not real ucs4 characters. These are codepoints in the
32 // modern fonts used, nothing unicode related.
33 // See comment in QLPainter::text() for more explanation.
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
49 return metrics_.ascent();
53 int GuiFontMetrics::maxDescent() const
55 // We add 1 as the value returned by QT is different than X
56 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
57 return metrics_.descent() + 1;
61 int GuiFontMetrics::lbearing(char_type c) const
64 // FIXME: QFontMetrics::leftBearingdoes not support the
65 // full unicode range. Once it does, we could use:
66 //return metrics_.leftBearing(toqstr(docstring(1,c)));
69 return metrics_.leftBearing(ucs4_to_qchar(c));
74 int const outOfLimitMetric = -10000;
78 int GuiFontMetrics::rbearing(char_type c) const
80 int value = rbearing_cache_.value(c, outOfLimitMetric);
81 if (value != outOfLimitMetric)
84 // Qt rbearing is from the right edge of the char's width().
86 QChar sc = ucs4_to_qchar(c);
87 value = width(c) - metrics_.rightBearing(sc);
89 // FIXME: QFontMetrics::leftBearingdoes not support the
90 // full unicode range. Once it does, we could use:
91 // metrics_.rightBearing(toqstr(docstring(1,c)));
94 rbearing_cache_.insert(c, value);
100 int GuiFontMetrics::smallcapsWidth(char_type c) const
102 // FIXME: Optimisation probably needed: we don't use the width cache.
104 QChar const qc = ucs4_to_qchar(c);
105 QChar const uc = qc.toUpper();
107 return smallcaps_metrics_.width(uc);
109 return metrics_.width(qc);
111 QString const s = toqstr(docstring(1,c));
112 QString const us = s.toUpper();
114 return smallcaps_metrics_.width(us);
116 return metrics_.width(s);
121 int GuiFontMetrics::width(docstring const & s) const
123 size_t ls = s.size();
125 for (unsigned int i = 0; i < ls; ++i) {
126 //FIXME: we need to detect surrogate pairs and act accordingly
128 if isSurrogateBase(s[i]) {
130 if (smallcaps_shape_)
131 w += metrics_.width(toqstr(c + s[i + 1]));
133 w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
145 int GuiFontMetrics::width(QString const & ucs2) const
147 return width(qstring_to_ucs4(ucs2));
151 int GuiFontMetrics::signedWidth(docstring const & s) const
157 return -width(s.substr(1, s.size() - 1));
163 void GuiFontMetrics::rectText(docstring const & str,
164 int & w, int & ascent, int & descent) const
166 static int const d = 2;
167 w = width(str) + d * 2 + 2;
168 ascent = metrics_.ascent() + d;
169 descent = metrics_.descent() + d;
174 void GuiFontMetrics::buttonText(docstring const & str,
175 int & w, int & ascent, int & descent) const
177 static int const d = 3;
178 w = width(str) + d * 2 + 2;
179 ascent = metrics_.ascent() + d;
180 descent = metrics_.descent() + d;
184 Dimension const GuiFontMetrics::defaultDimension() const
186 return Dimension(0, maxAscent(), maxDescent());
190 Dimension const GuiFontMetrics::dimension(char_type c) const
192 return Dimension(width(c), ascent(c), descent(c));
196 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
201 r = metrics_.boundingRect(ucs4_to_qchar(c));
203 r = metrics_.boundingRect(toqstr(docstring(1,c)));
205 AscendDescend ad = { -r.top(), r.bottom() + 1};
206 // We could as well compute the width but this is not really
207 // needed for now as it is done directly in width() below.
208 metrics_cache_.insert(c, ad);
214 int GuiFontMetrics::width(char_type c) const
216 if (smallcaps_shape_)
217 return smallcapsWidth(c);
219 int value = width_cache_.value(c, outOfLimitMetric);
220 if (value != outOfLimitMetric)
224 value = metrics_.width(ucs4_to_qchar(c));
226 value = metrics_.width(toqstr(docstring(1,c)));
228 width_cache_.insert(c, value);
234 int GuiFontMetrics::ascent(char_type c) const
236 static AscendDescend const outOfLimitAD =
237 {outOfLimitMetric, outOfLimitMetric};
238 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
239 if (value.ascent != outOfLimitMetric)
242 value = fillMetricsCache(c);
247 int GuiFontMetrics::descent(char_type c) const
249 static AscendDescend const outOfLimitAD =
250 {outOfLimitMetric, outOfLimitMetric};
251 AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
252 if (value.descent != outOfLimitMetric)
253 return value.descent;
255 value = fillMetricsCache(c);
256 return value.descent;