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