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