]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Fix positionning of cursor
[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 "Dimension.h"
19 #include "Language.h"
20 #include "LyXRC.h"
21
22 #include "insets/Inset.h"
23
24 #include "support/lassert.h"
25
26 #include <QTextLayout>
27
28 using namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 namespace {
34 /**
35  * Convert a UCS4 character into a QChar.
36  * This is a hack (it does only make sense for the common part of the UCS4
37  * and UTF16 encodings) and should not be used.
38  * This does only exist because of performance reasons (a real conversion
39  * using iconv is too slow on windows).
40  *
41  * This is no real conversion but a simple cast in reality. This is the reason
42  * why this works well for symbol fonts used in mathed too, even though
43  * these are not real ucs4 characters. These are codepoints in the
44  * computer modern fonts used, nothing unicode related.
45  * See comment in QLPainter::text() for more explanation.
46  **/
47 inline QChar const ucs4_to_qchar(char_type const ucs4)
48 {
49         LATTEST(is_utf16(ucs4));
50         return QChar(static_cast<unsigned short>(ucs4));
51 }
52 } // anon namespace
53
54
55 GuiFontMetrics::GuiFontMetrics(QFont const & font) : font_(font), metrics_(font, 0)
56 {
57 }
58
59
60 int GuiFontMetrics::maxAscent() const
61 {
62         return metrics_.ascent();
63 }
64
65
66 int GuiFontMetrics::maxDescent() const
67 {
68         // We add 1 as the value returned by QT is different than X
69         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
70         return metrics_.descent() + 1;
71 }
72
73
74 int GuiFontMetrics::lbearing(char_type c) const
75 {
76         if (!is_utf16(c))
77                 // FIXME: QFontMetrics::leftBearingdoes not support the
78                 //        full unicode range. Once it does, we could use:
79                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
80                 return 0;
81
82         return metrics_.leftBearing(ucs4_to_qchar(c));
83 }
84
85
86 namespace {
87 int const outOfLimitMetric = -10000;
88 }
89
90
91 int GuiFontMetrics::rbearing(char_type c) const
92 {
93         int value = rbearing_cache_.value(c, outOfLimitMetric);
94         if (value != outOfLimitMetric)
95                 return value;
96
97         // Qt rbearing is from the right edge of the char's width().
98         if (is_utf16(c)) {
99                 QChar sc = ucs4_to_qchar(c);
100                 value = width(c) - metrics_.rightBearing(sc);
101         } else {
102                 // FIXME: QFontMetrics::leftBearing does not support the
103                 //        full unicode range. Once it does, we could use:
104                 // metrics_.rightBearing(toqstr(docstring(1, c)));
105                 value = width(c);
106         }
107
108         rbearing_cache_.insert(c, value);
109
110         return value;
111 }
112
113
114 int GuiFontMetrics::width(docstring const & s) const
115 {
116         int w = 0;
117         map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
118         if (it != strwidth_cache_.end()) {
119                 w = it->second;
120         } else {
121                 w = metrics_.width(toqstr(s));
122                 strwidth_cache_[s] = w;
123         }
124         return w;
125 }
126
127
128 int GuiFontMetrics::width(QString const & ucs2) const
129 {
130         return width(qstring_to_ucs4(ucs2));
131 }
132
133
134 int GuiFontMetrics::signedWidth(docstring const & s) const
135 {
136         if (s.empty())
137                 return 0;
138
139         if (s[0] == '-')
140                 return -width(s.substr(1, s.size() - 1));
141         else
142                 return width(s);
143 }
144
145 namespace {
146 void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
147                              bool const rtl)
148 {
149         QString qs;
150         /* In LyX, the character direction is forced by the language.
151          * Therefore, we have to signal that fact to Qt.
152          * Source: http://www.iamcal.com/understanding-bidirectional-text/
153          */
154         // Left-to-right override: forces to draw text left-to-right
155         char_type const LRO = 0x202D;
156         // Right-to-left override: forces to draw text right-to-left
157         char_type const RLO = 0x202E;
158         // Pop directional formatting: return to previous state
159         char_type const PDF = 0x202C;
160         if (rtl)
161                 qs = toqstr(RLO + s + PDF);
162         else
163                 qs = toqstr(LRO + s + PDF);
164
165         tl.setText(qs);
166         tl.setFont(font);
167         tl.beginLayout();
168         tl.createLine();
169         tl.endLayout();
170 }
171 }
172
173
174 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl) const
175 {
176         QTextLayout tl;
177         setTextLayout(tl, s, font_, rtl);
178         return tl.lineForTextPosition(pos + 1).cursorToX(pos + 1);
179 }
180
181
182 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl) const
183 {
184         QTextLayout tl;
185         setTextLayout(tl, s, font_, rtl);
186         int pos = tl.lineForTextPosition(0).xToCursor(x);
187         // take into account the unicode formatting characters
188         if (pos > 0)
189                 --pos;
190         if (pos > int(s.length()))
191                 pos = s.length();
192         // correct x value to the actual cursor position.
193         x = tl.lineForTextPosition(0).cursorToX(pos + 1);
194         return pos;
195 }
196
197
198 void GuiFontMetrics::rectText(docstring const & str,
199         int & w, int & ascent, int & descent) const
200 {
201         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
202
203         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
204         ascent = metrics_.ascent() + d;
205         descent = metrics_.descent() + d;
206 }
207
208
209
210 void GuiFontMetrics::buttonText(docstring const & str,
211         int & w, int & ascent, int & descent) const
212 {
213         rectText(str, w, ascent, descent);
214         w += Inset::TEXT_TO_INSET_OFFSET;
215 }
216
217
218 Dimension const GuiFontMetrics::defaultDimension() const
219 {
220         return Dimension(0, maxAscent(), maxDescent());
221 }
222
223
224 Dimension const GuiFontMetrics::dimension(char_type c) const
225 {
226         return Dimension(width(c), ascent(c), descent(c));
227 }
228
229
230 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
231                 char_type c) const
232 {
233         QRect r;
234         if (is_utf16(c))
235                 r = metrics_.boundingRect(ucs4_to_qchar(c));
236         else
237                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
238
239         AscendDescend ad = { -r.top(), r.bottom() + 1};
240         // We could as well compute the width but this is not really
241         // needed for now as it is done directly in width() below.
242         metrics_cache_.insert(c, ad);
243
244         return ad;
245 }
246
247
248 int GuiFontMetrics::width(char_type c) const
249 {
250         int value = width_cache_.value(c, outOfLimitMetric);
251         if (value != outOfLimitMetric)
252                 return value;
253
254         if (is_utf16(c))
255                 value = metrics_.width(ucs4_to_qchar(c));
256         else
257                 value = metrics_.width(toqstr(docstring(1, c)));
258
259         width_cache_.insert(c, value);
260
261         return value;
262 }
263
264
265 int GuiFontMetrics::ascent(char_type c) const
266 {
267         static AscendDescend const outOfLimitAD =
268                 {outOfLimitMetric, outOfLimitMetric};
269         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
270         if (value.ascent != outOfLimitMetric)
271                 return value.ascent;
272
273         value = fillMetricsCache(c);
274         return value.ascent;
275 }
276
277
278 int GuiFontMetrics::descent(char_type c) const
279 {
280         static AscendDescend const outOfLimitAD =
281                 {outOfLimitMetric, outOfLimitMetric};
282         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
283         if (value.descent != outOfLimitMetric)
284                 return value.descent;
285
286         value = fillMetricsCache(c);
287         return value.descent;
288 }
289
290 } // namespace frontend
291 } // namespace lyx