]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
1 /**
2  * \file GuiFontMetrics.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiFontMetrics.h"
15
16 #include "qt_helpers.h"
17
18 #include "Language.h"
19 #include "Dimension.h"
20
21 #include "insets/Inset.h"
22
23 #include "support/lassert.h"
24
25 using namespace std;
26
27 namespace lyx {
28 namespace frontend {
29
30 /**
31  * Convert a UCS4 character into a QChar.
32  * This is a hack (it does only make sense for the common part of the UCS4
33  * and UTF16 encodings) and should not be used.
34  * This does only exist because of performance reasons (a real conversion
35  * using iconv is too slow on windows).
36  */
37 static inline QChar const ucs4_to_qchar(char_type const ucs4)
38 {
39         LASSERT(is_utf16(ucs4), /**/);
40         return QChar(static_cast<unsigned short>(ucs4));
41 }
42
43
44 // Caution: When using ucs4_to_qchar() in these methods, this is no
45 // real conversion but a simple cast in reality. This is the reason
46 // why this works well for symbol fonts used in mathed too, even though
47 // these are not real ucs4 characters. These are codepoints in the
48 // modern fonts used, nothing unicode related.
49 // See comment in QLPainter::text() for more explanation.
50
51 GuiFontMetrics::GuiFontMetrics(QFont const & font)
52 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
53 {
54 }
55
56
57 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
58 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
59 {
60 }
61
62
63 int GuiFontMetrics::maxAscent() const
64 {
65         return metrics_.ascent();
66 }
67
68
69 int GuiFontMetrics::maxDescent() const
70 {
71         // We add 1 as the value returned by QT is different than X
72         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
73         return metrics_.descent() + 1;
74 }
75
76
77 int GuiFontMetrics::lbearing(char_type c) const
78 {
79         if (!is_utf16(c))
80                 // FIXME: QFontMetrics::leftBearingdoes not support the
81                 //        full unicode range. Once it does, we could use:
82                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
83                 return 0;
84
85         return metrics_.leftBearing(ucs4_to_qchar(c));
86 }
87
88
89 namespace {
90 int const outOfLimitMetric = -10000;
91 }
92
93
94 int GuiFontMetrics::rbearing(char_type c) const
95 {
96         int value = rbearing_cache_.value(c, outOfLimitMetric);
97         if (value != outOfLimitMetric)
98                 return value;
99
100         // Qt rbearing is from the right edge of the char's width().
101         if (is_utf16(c)) {
102                 QChar sc = ucs4_to_qchar(c);
103                 value = width(c) - metrics_.rightBearing(sc);
104         } else {
105                 // FIXME: QFontMetrics::leftBearing does not support the
106                 //        full unicode range. Once it does, we could use:
107                 // metrics_.rightBearing(toqstr(docstring(1, c)));
108                 value = width(c);
109         }
110
111         rbearing_cache_.insert(c, value);
112
113         return value;
114 }
115
116
117 int GuiFontMetrics::smallcapsWidth(char_type c) const
118 {
119         // FIXME: Optimisation probably needed: we don't use the width cache.
120         if (is_utf16(c)) {
121                 QChar const qc = ucs4_to_qchar(c);
122                 QChar const uc = qc.toUpper();
123                 if (qc != uc)
124                         return smallcaps_metrics_.width(uc);
125                 else
126                         return metrics_.width(qc);
127         } else {
128                 QString const s = toqstr(docstring(1, c));
129                 QString const us = s.toUpper();
130                 if (s != us)
131                         return smallcaps_metrics_.width(us);
132                 else
133                         return metrics_.width(s);
134         }
135 }
136
137
138 int GuiFontMetrics::width(docstring const & s) const
139 {
140         size_t ls = s.size();
141         int w = 0;
142         for (unsigned int i = 0; i < ls; ++i) {
143                 //FIXME: we need to detect surrogate pairs and act accordingly
144                 /**
145                 if isSurrogateBase(s[i]) {
146                         docstring c = s[i];
147                         if (smallcaps_shape_)
148                                 w += metrics_.width(toqstr(c + s[i + 1]));
149                         else
150                                 w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
151                         ++i;
152                 }
153                 else
154                 */
155                 w += width(s[i]);
156         }
157
158         return w;
159 }
160
161
162 int GuiFontMetrics::width(QString const & ucs2) const
163 {
164         return width(qstring_to_ucs4(ucs2));
165 }
166
167
168 int GuiFontMetrics::signedWidth(docstring const & s) const
169 {
170         if (s.empty())
171                 return 0;
172
173         if (s[0] == '-')
174                 return -width(s.substr(1, s.size() - 1));
175         else
176                 return width(s);
177 }
178
179
180 void GuiFontMetrics::rectText(docstring const & str,
181         int & w, int & ascent, int & descent) const
182 {
183         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
184
185         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
186         ascent = metrics_.ascent() + d;
187         descent = metrics_.descent() + d;
188 }
189
190
191
192 void GuiFontMetrics::buttonText(docstring const & str,
193         int & w, int & ascent, int & descent) const
194 {
195         rectText(str, w, ascent, descent);
196         w += Inset::TEXT_TO_INSET_OFFSET;
197 }
198
199
200 Dimension const GuiFontMetrics::defaultDimension() const
201 {
202         return Dimension(0, maxAscent(), maxDescent());
203 }
204
205
206 Dimension const GuiFontMetrics::dimension(char_type c) const
207 {
208         return Dimension(width(c), ascent(c), descent(c));
209 }
210
211
212 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
213                 char_type c) const
214 {
215         QRect r;
216         if (is_utf16(c))
217                 r = metrics_.boundingRect(ucs4_to_qchar(c));
218         else
219                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
220
221         AscendDescend ad = { -r.top(), r.bottom() + 1};
222         // We could as well compute the width but this is not really
223         // needed for now as it is done directly in width() below.
224         metrics_cache_.insert(c, ad);
225
226         return ad;
227 }
228
229
230 int GuiFontMetrics::width(char_type c) const
231 {
232         if (smallcaps_shape_)
233                 return smallcapsWidth(c);
234
235         int value = width_cache_.value(c, outOfLimitMetric);
236         if (value != outOfLimitMetric)
237                 return value;
238
239         if (is_utf16(c))
240                 value = metrics_.width(ucs4_to_qchar(c));
241         else
242                 value = metrics_.width(toqstr(docstring(1, c)));
243
244         width_cache_.insert(c, value);
245
246         return value;
247 }
248
249
250 int GuiFontMetrics::ascent(char_type c) const
251 {
252         static AscendDescend const outOfLimitAD = 
253                 {outOfLimitMetric, outOfLimitMetric};
254         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
255         if (value.ascent != outOfLimitMetric)
256                 return value.ascent;
257
258         value = fillMetricsCache(c);
259         return value.ascent;
260 }
261
262
263 int GuiFontMetrics::descent(char_type c) const
264 {
265         static AscendDescend const outOfLimitAD = 
266                 {outOfLimitMetric, outOfLimitMetric};
267         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
268         if (value.descent != outOfLimitMetric)
269                 return value.descent;
270
271         value = fillMetricsCache(c);
272         return value.descent;
273 }
274
275 } // namespace frontend
276 } // namespace lyx