]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
On Linux show in crash message box the backtrace
[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  * computer 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         LATTEST(is_utf16(ucs4));
47         return QChar(static_cast<unsigned short>(ucs4));
48 }
49 } // anon namespace
50
51
52 GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font, 0)
53 {
54 }
55
56
57 int GuiFontMetrics::maxAscent() const
58 {
59         return metrics_.ascent();
60 }
61
62
63 int GuiFontMetrics::maxDescent() const
64 {
65         // We add 1 as the value returned by QT is different than X
66         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
67         return metrics_.descent() + 1;
68 }
69
70
71 int GuiFontMetrics::lbearing(char_type c) const
72 {
73         if (!is_utf16(c))
74                 // FIXME: QFontMetrics::leftBearingdoes not support the
75                 //        full unicode range. Once it does, we could use:
76                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
77                 return 0;
78
79         return metrics_.leftBearing(ucs4_to_qchar(c));
80 }
81
82
83 namespace {
84 int const outOfLimitMetric = -10000;
85 }
86
87
88 int GuiFontMetrics::rbearing(char_type c) const
89 {
90         int value = rbearing_cache_.value(c, outOfLimitMetric);
91         if (value != outOfLimitMetric)
92                 return value;
93
94         // Qt rbearing is from the right edge of the char's width().
95         if (is_utf16(c)) {
96                 QChar sc = ucs4_to_qchar(c);
97                 value = width(c) - metrics_.rightBearing(sc);
98         } else {
99                 // FIXME: QFontMetrics::leftBearing does not support the
100                 //        full unicode range. Once it does, we could use:
101                 // metrics_.rightBearing(toqstr(docstring(1, c)));
102                 value = width(c);
103         }
104
105         rbearing_cache_.insert(c, value);
106
107         return value;
108 }
109
110
111 int GuiFontMetrics::width(docstring const & s) const
112 {
113         size_t ls = s.size();
114         int w = 0;
115         for (unsigned int i = 0; i < ls; ++i) {
116                 //FIXME: we need to detect surrogate pairs and act accordingly
117                 /**
118                 if isSurrogateBase(s[i]) {
119                         docstring c = s[i];
120                         w += metrics_.width(toqstr(c + s[i + 1]));
121                         ++i;
122                 }
123                 else
124                 */
125                 w += width(s[i]);
126         }
127
128         return w;
129 }
130
131
132 int GuiFontMetrics::width(QString const & ucs2) const
133 {
134         return width(qstring_to_ucs4(ucs2));
135 }
136
137
138 int GuiFontMetrics::signedWidth(docstring const & s) const
139 {
140         if (s.empty())
141                 return 0;
142
143         if (s[0] == '-')
144                 return -width(s.substr(1, s.size() - 1));
145         else
146                 return width(s);
147 }
148
149
150 void GuiFontMetrics::rectText(docstring const & str,
151         int & w, int & ascent, int & descent) const
152 {
153         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
154
155         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
156         ascent = metrics_.ascent() + d;
157         descent = metrics_.descent() + d;
158 }
159
160
161
162 void GuiFontMetrics::buttonText(docstring const & str,
163         int & w, int & ascent, int & descent) const
164 {
165         rectText(str, w, ascent, descent);
166         w += Inset::TEXT_TO_INSET_OFFSET;
167 }
168
169
170 Dimension const GuiFontMetrics::defaultDimension() const
171 {
172         return Dimension(0, maxAscent(), maxDescent());
173 }
174
175
176 Dimension const GuiFontMetrics::dimension(char_type c) const
177 {
178         return Dimension(width(c), ascent(c), descent(c));
179 }
180
181
182 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
183                 char_type c) const
184 {
185         QRect r;
186         if (is_utf16(c))
187                 r = metrics_.boundingRect(ucs4_to_qchar(c));
188         else
189                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
190
191         AscendDescend ad = { -r.top(), r.bottom() + 1};
192         // We could as well compute the width but this is not really
193         // needed for now as it is done directly in width() below.
194         metrics_cache_.insert(c, ad);
195
196         return ad;
197 }
198
199
200 int GuiFontMetrics::width(char_type c) const
201 {
202         int value = width_cache_.value(c, outOfLimitMetric);
203         if (value != outOfLimitMetric)
204                 return value;
205
206         if (is_utf16(c))
207                 value = metrics_.width(ucs4_to_qchar(c));
208         else
209                 value = metrics_.width(toqstr(docstring(1, c)));
210
211         width_cache_.insert(c, value);
212
213         return value;
214 }
215
216
217 int GuiFontMetrics::ascent(char_type c) const
218 {
219         static AscendDescend const outOfLimitAD =
220                 {outOfLimitMetric, outOfLimitMetric};
221         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
222         if (value.ascent != outOfLimitMetric)
223                 return value.ascent;
224
225         value = fillMetricsCache(c);
226         return value.ascent;
227 }
228
229
230 int GuiFontMetrics::descent(char_type c) const
231 {
232         static AscendDescend const outOfLimitAD =
233                 {outOfLimitMetric, outOfLimitMetric};
234         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
235         if (value.descent != outOfLimitMetric)
236                 return value.descent;
237
238         value = fillMetricsCache(c);
239         return value.descent;
240 }
241
242 } // namespace frontend
243 } // namespace lyx