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));
73 int GuiFontMetrics::rbearing(char_type c) const
75 if (!rbearing_cache_.contains(c)) {
76 // Qt rbearing is from the right edge of the char's width().
79 QChar sc = ucs4_to_qchar(c);
80 rb = width(c) - metrics_.rightBearing(sc);
82 // FIXME: QFontMetrics::leftBearingdoes not support the
83 // full unicode range. Once it does, we could use:
84 // metrics_.rightBearing(toqstr(docstring(1,c)));
87 rbearing_cache_.insert(c, rb);
89 return rbearing_cache_.value(c);
93 int GuiFontMetrics::smallcapsWidth(char_type c) const
95 // FIXME: Optimisation probably needed: we don't use the width cache.
97 QChar const qc = ucs4_to_qchar(c);
98 QChar const uc = qc.toUpper();
100 return smallcaps_metrics_.width(uc);
102 return metrics_.width(qc);
104 QString const s = toqstr(docstring(1,c));
105 QString const us = s.toUpper();
107 return smallcaps_metrics_.width(us);
109 return metrics_.width(s);
114 int GuiFontMetrics::width(docstring const & s) const
116 size_t ls = s.size();
118 for (unsigned int i = 0; i < ls; ++i)
125 int GuiFontMetrics::width(QString const & ucs2) const
127 return width(qstring_to_ucs4(ucs2));
131 int GuiFontMetrics::signedWidth(docstring const & s) const
137 return -width(s.substr(1, s.size() - 1));
143 void GuiFontMetrics::rectText(docstring const & str,
144 int & w, int & ascent, int & descent) const
146 static int const d = 2;
147 w = width(str) + d * 2 + 2;
148 ascent = metrics_.ascent() + d;
149 descent = metrics_.descent() + d;
154 void GuiFontMetrics::buttonText(docstring const & str,
155 int & w, int & ascent, int & descent) const
157 static int const d = 3;
158 w = width(str) + d * 2 + 2;
159 ascent = metrics_.ascent() + d;
160 descent = metrics_.descent() + d;
164 Dimension const GuiFontMetrics::defaultDimension() const
166 return Dimension(0, maxAscent(), maxDescent());
170 Dimension const GuiFontMetrics::dimension(char_type c) const
172 return Dimension(width(c), ascent(c), descent(c));
176 void GuiFontMetrics::fillMetricsCache(char_type c) const
180 r = metrics_.boundingRect(ucs4_to_qchar(c));
182 r = metrics_.boundingRect(toqstr(docstring(1,c)));
184 AscendDescend ad = { -r.top(), r.bottom() + 1};
185 // We could as well compute the width but this is not really
186 // needed for now as it is done directly in width() below.
187 metrics_cache_.insert(c, ad);
191 int GuiFontMetrics::width(char_type c) const
193 if (smallcaps_shape_)
194 return smallcapsWidth(c);
196 if (!width_cache_.contains(c)) {
198 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
200 width_cache_.insert(c, metrics_.width(toqstr(docstring(1,c))));
203 return width_cache_.value(c);
207 int GuiFontMetrics::ascent(char_type c) const
209 if (!metrics_cache_.contains(c))
212 return metrics_cache_.value(c).ascent;
216 int GuiFontMetrics::descent(char_type c) const
218 if (!metrics_cache_.contains(c))
221 return metrics_cache_.value(c).descent;