]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
7cf841f87384b5eb28238e68fa5d5b754de04011
[features.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 // Limit strwidth_cache_ size to 512kB of string data
57 GuiFontMetrics::GuiFontMetrics(QFont const & font)
58         : font_(font), metrics_(font, 0),
59           strwidth_cache_(1 << 19)
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::em() const
79 {
80         return QFontInfo(font_).pixelSize();
81 }
82
83
84 int GuiFontMetrics::lineWidth() const
85 {
86         return metrics_.lineWidth();
87 }
88
89
90 int GuiFontMetrics::underlinePos() const
91 {
92         return metrics_.underlinePos();
93 }
94
95
96 int GuiFontMetrics::strikeoutPos() const
97 {
98         return metrics_.strikeOutPos();
99 }
100
101
102 int GuiFontMetrics::lbearing(char_type c) const
103 {
104         if (!is_utf16(c))
105                 // FIXME: QFontMetrics::leftBearing does not support the
106                 //        full unicode range. Once it does, we could use:
107                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
108                 return 0;
109
110         return metrics_.leftBearing(ucs4_to_qchar(c));
111 }
112
113
114 namespace {
115 int const outOfLimitMetric = -10000;
116 }
117
118
119 int GuiFontMetrics::rbearing(char_type c) const
120 {
121         int value = rbearing_cache_.value(c, outOfLimitMetric);
122         if (value != outOfLimitMetric)
123                 return value;
124
125         // Qt rbearing is from the right edge of the char's width().
126         if (is_utf16(c)) {
127                 QChar sc = ucs4_to_qchar(c);
128                 value = width(c) - metrics_.rightBearing(sc);
129         } else {
130                 // FIXME: QFontMetrics::leftBearing does not support the
131                 //        full unicode range. Once it does, we could use:
132                 // metrics_.rightBearing(toqstr(docstring(1, c)));
133                 value = width(c);
134         }
135
136         rbearing_cache_.insert(c, value);
137
138         return value;
139 }
140
141
142 int GuiFontMetrics::width(docstring const & s) const
143 {
144         QByteArray qba =
145                 QByteArray(reinterpret_cast<char const *>(s.data()),
146                            s.size() * sizeof(docstring::value_type));
147         int * pw = strwidth_cache_[qba];
148         if (pw)
149                 return *pw;
150         int w = metrics_.width(toqstr(s));
151         strwidth_cache_.insert(qba, new int(w), qba.size());
152         return w;
153 }
154
155
156 int GuiFontMetrics::width(QString const & ucs2) const
157 {
158         return width(qstring_to_ucs4(ucs2));
159 }
160
161
162 int GuiFontMetrics::signedWidth(docstring const & s) const
163 {
164         if (s.empty())
165                 return 0;
166
167         if (s[0] == '-')
168                 return -width(s.substr(1, s.size() - 1));
169         else
170                 return width(s);
171 }
172
173 namespace {
174
175 QTextLayout const & getTextLayout(docstring const & s, QFont font,
176                                   bool const rtl, double const wordspacing)
177 {
178         static docstring old_s;
179         static QFont old_font;
180         static bool old_rtl = false;
181         // this invalid value is to make sure that it is reset on first try.
182         static double old_wordspacing = -1.0;
183         // This one is our trivial cache
184         static QTextLayout tl;
185         if (s != old_s || font != old_font || rtl != old_rtl
186             || wordspacing != old_wordspacing) {
187                 tl.setText(toqstr(s));
188                 font.setWordSpacing(wordspacing);
189                 tl.setFont(font);
190                 // Note that both setFlags and the enums are undocumented
191                 tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
192                 tl.beginLayout();
193                 tl.createLine();
194                 tl.endLayout();
195                 old_s = s;
196                 old_font = font;
197                 old_rtl = rtl;
198                 old_wordspacing = wordspacing;
199         }
200         return tl;
201 }
202
203 }
204
205
206 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
207                           double const wordspacing) const
208 {
209         QFont copy = font_;
210         copy.setWordSpacing(wordspacing);
211         QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
212         return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
213 }
214
215
216 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
217                           double const wordspacing) const
218 {
219         QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
220         int pos = tl.lineForTextPosition(0).xToCursor(x);
221         // correct x value to the actual cursor position.
222         x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
223         return pos;
224 }
225
226
227 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
228 {
229         if (s.empty())
230                 return false;
231         QTextLayout tl;
232         tl.setText(toqstr(s));
233         tl.setFont(font_);
234         // Note that both setFlags and the enums are undocumented
235         tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
236         QTextOption to;
237         to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
238         tl.setTextOption(to);
239         tl.beginLayout();
240         QTextLine line = tl.createLine();
241         line.setLineWidth(x);
242         tl.createLine();
243         tl.endLayout();
244         if (int(line.naturalTextWidth()) > x)
245                 return false;
246         x = int(line.naturalTextWidth());
247         s = s.substr(0, line.textLength());
248         return true;
249 }
250
251
252 void GuiFontMetrics::rectText(docstring const & str,
253         int & w, int & ascent, int & descent) const
254 {
255         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
256
257         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
258         ascent = metrics_.ascent() + d;
259         descent = metrics_.descent() + d;
260 }
261
262
263
264 void GuiFontMetrics::buttonText(docstring const & str,
265         int & w, int & ascent, int & descent) const
266 {
267         rectText(str, w, ascent, descent);
268         w += Inset::TEXT_TO_INSET_OFFSET;
269 }
270
271
272 Dimension const GuiFontMetrics::defaultDimension() const
273 {
274         return Dimension(0, maxAscent(), maxDescent());
275 }
276
277
278 Dimension const GuiFontMetrics::dimension(char_type c) const
279 {
280         return Dimension(width(c), ascent(c), descent(c));
281 }
282
283
284 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
285                 char_type c) const
286 {
287         QRect r;
288         if (is_utf16(c))
289                 r = metrics_.boundingRect(ucs4_to_qchar(c));
290         else
291                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
292
293         AscendDescend ad = { -r.top(), r.bottom() + 1};
294         // We could as well compute the width but this is not really
295         // needed for now as it is done directly in width() below.
296         metrics_cache_.insert(c, ad);
297
298         return ad;
299 }
300
301
302 int GuiFontMetrics::width(char_type c) const
303 {
304         int value = width_cache_.value(c, outOfLimitMetric);
305         if (value != outOfLimitMetric)
306                 return value;
307
308         if (is_utf16(c))
309                 value = metrics_.width(ucs4_to_qchar(c));
310         else
311                 value = metrics_.width(toqstr(docstring(1, c)));
312
313         width_cache_.insert(c, value);
314
315         return value;
316 }
317
318
319 int GuiFontMetrics::ascent(char_type c) const
320 {
321         static AscendDescend const outOfLimitAD =
322                 {outOfLimitMetric, outOfLimitMetric};
323         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
324         if (value.ascent != outOfLimitMetric)
325                 return value.ascent;
326
327         value = fillMetricsCache(c);
328         return value.ascent;
329 }
330
331
332 int GuiFontMetrics::descent(char_type c) const
333 {
334         static AscendDescend const outOfLimitAD =
335                 {outOfLimitMetric, outOfLimitMetric};
336         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
337         if (value.descent != outOfLimitMetric)
338                 return value.descent;
339
340         value = fillMetricsCache(c);
341         return value.descent;
342 }
343
344 } // namespace frontend
345 } // namespace lyx