]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Better algorithm for forcing bidi drawing
[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 using namespace lyx::support;
30
31 namespace lyx {
32 namespace frontend {
33
34 namespace {
35 /**
36  * Convert a UCS4 character into a QChar.
37  * This is a hack (it does only make sense for the common part of the UCS4
38  * and UTF16 encodings) and should not be used.
39  * This does only exist because of performance reasons (a real conversion
40  * using iconv is too slow on windows).
41  *
42  * This is no real conversion but a simple cast in reality. This is the reason
43  * why this works well for symbol fonts used in mathed too, even though
44  * these are not real ucs4 characters. These are codepoints in the
45  * computer modern fonts used, nothing unicode related.
46  * See comment in GuiPainter::text() for more explanation.
47  **/
48 inline QChar const ucs4_to_qchar(char_type const ucs4)
49 {
50         LATTEST(is_utf16(ucs4));
51         return QChar(static_cast<unsigned short>(ucs4));
52 }
53 } // anon namespace
54
55
56 GuiFontMetrics::GuiFontMetrics(QFont const & font) : font_(font), metrics_(font, 0)
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::width(docstring const & s) const
116 {
117         int w = 0;
118         map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
119         if (it != strwidth_cache_.end()) {
120                 w = it->second;
121         } else {
122                 w = metrics_.width(toqstr(s));
123                 strwidth_cache_[s] = w;
124         }
125         return w;
126 }
127
128
129 int GuiFontMetrics::width(QString const & ucs2) const
130 {
131         return width(qstring_to_ucs4(ucs2));
132 }
133
134
135 int GuiFontMetrics::signedWidth(docstring const & s) const
136 {
137         if (s.empty())
138                 return 0;
139
140         if (s[0] == '-')
141                 return -width(s.substr(1, s.size() - 1));
142         else
143                 return width(s);
144 }
145
146 namespace {
147 void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
148                              bool const rtl)
149 {
150         tl.setText(toqstr(s));
151         tl.setFont(font);
152         // Note that both setFlags and the enums are undocumented
153         tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
154         tl.beginLayout();
155         tl.createLine();
156         tl.endLayout();
157 }
158 }
159
160
161 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl) const
162 {
163         QTextLayout tl;
164         setTextLayout(tl, s, font_, rtl);
165         return tl.lineForTextPosition(pos).cursorToX(pos);
166 }
167
168
169 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl) const
170 {
171         QTextLayout tl;
172         setTextLayout(tl, s, font_, rtl);
173         int pos = tl.lineForTextPosition(0).xToCursor(x);
174         // correct x value to the actual cursor position.
175         x = tl.lineForTextPosition(0).cursorToX(pos);
176         return pos;
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         int value = width_cache_.value(c, outOfLimitMetric);
233         if (value != outOfLimitMetric)
234                 return value;
235
236         if (is_utf16(c))
237                 value = metrics_.width(ucs4_to_qchar(c));
238         else
239                 value = metrics_.width(toqstr(docstring(1, c)));
240
241         width_cache_.insert(c, value);
242
243         return value;
244 }
245
246
247 int GuiFontMetrics::ascent(char_type c) const
248 {
249         static AscendDescend const outOfLimitAD =
250                 {outOfLimitMetric, outOfLimitMetric};
251         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
252         if (value.ascent != outOfLimitMetric)
253                 return value.ascent;
254
255         value = fillMetricsCache(c);
256         return value.ascent;
257 }
258
259
260 int GuiFontMetrics::descent(char_type c) const
261 {
262         static AscendDescend const outOfLimitAD =
263                 {outOfLimitMetric, outOfLimitMetric};
264         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
265         if (value.descent != outOfLimitMetric)
266                 return value.descent;
267
268         value = fillMetricsCache(c);
269         return value.descent;
270 }
271
272 } // namespace frontend
273 } // namespace lyx