]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Amend f441590c
[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 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30 namespace frontend {
31
32 namespace {
33 /**
34  * Convert a UCS4 character into a QChar.
35  * This is a hack (it does only make sense for the common part of the UCS4
36  * and UTF16 encodings) and should not be used.
37  * This does only exist because of performance reasons (a real conversion
38  * using iconv is too slow on windows).
39  *
40  * This is no real conversion but a simple cast in reality. This is the reason
41  * why this works well for symbol fonts used in mathed too, even though
42  * these are not real ucs4 characters. These are codepoints in the
43  * computer modern fonts used, nothing unicode related.
44  * See comment in GuiPainter::text() for more explanation.
45  **/
46 inline QChar const ucs4_to_qchar(char_type const ucs4)
47 {
48         LATTEST(is_utf16(ucs4));
49         return QChar(static_cast<unsigned short>(ucs4));
50 }
51 } // anon namespace
52
53
54 // Limit strwidth_cache_ size to 512kB of string data
55 GuiFontMetrics::GuiFontMetrics(QFont const & font)
56         : font_(font), metrics_(font, 0),
57           strwidth_cache_(1 << 19),
58           tl_cache_rtl_(false), tl_cache_wordspacing_(-1.0)
59 {
60 }
61
62
63 int GuiFontMetrics::maxAscent() const
64 {
65         return metrics_.ascent();
66 }
67
68
69 int GuiFontMetrics::maxDescent() const
70 {
71         // We add 1 as the value returned by QT is different than X
72         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
73         return metrics_.descent() + 1;
74 }
75
76
77 int GuiFontMetrics::em() const
78 {
79         return QFontInfo(font_).pixelSize();
80 }
81
82
83 int GuiFontMetrics::lineWidth() const
84 {
85         return metrics_.lineWidth();
86 }
87
88
89 int GuiFontMetrics::underlinePos() const
90 {
91         return metrics_.underlinePos();
92 }
93
94
95 int GuiFontMetrics::strikeoutPos() const
96 {
97         return metrics_.strikeOutPos();
98 }
99
100
101 int GuiFontMetrics::lbearing(char_type c) const
102 {
103         if (!is_utf16(c))
104                 // FIXME: QFontMetrics::leftBearing does not support the
105                 //        full unicode range. Once it does, we could use:
106                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
107                 return 0;
108
109         return metrics_.leftBearing(ucs4_to_qchar(c));
110 }
111
112
113 namespace {
114 int const outOfLimitMetric = -10000;
115 }
116
117
118 int GuiFontMetrics::rbearing(char_type c) const
119 {
120         int value = rbearing_cache_.value(c, outOfLimitMetric);
121         if (value != outOfLimitMetric)
122                 return value;
123
124         // Qt rbearing is from the right edge of the char's width().
125         if (is_utf16(c)) {
126                 QChar sc = ucs4_to_qchar(c);
127                 value = width(c) - metrics_.rightBearing(sc);
128         } else {
129                 // FIXME: QFontMetrics::leftBearing does not support the
130                 //        full unicode range. Once it does, we could use:
131                 // metrics_.rightBearing(toqstr(docstring(1, c)));
132                 value = width(c);
133         }
134
135         rbearing_cache_.insert(c, value);
136
137         return value;
138 }
139
140
141 int GuiFontMetrics::width(docstring const & s) const
142 {
143         QByteArray qba =
144                 QByteArray(reinterpret_cast<char const *>(s.data()),
145                            s.size() * sizeof(docstring::value_type));
146         int * pw = strwidth_cache_[qba];
147         if (pw)
148                 return *pw;
149         int w = metrics_.width(toqstr(s));
150         strwidth_cache_.insert(qba, new int(w), qba.size());
151         return w;
152 }
153
154
155 int GuiFontMetrics::width(QString const & ucs2) const
156 {
157         return width(qstring_to_ucs4(ucs2));
158 }
159
160
161 int GuiFontMetrics::signedWidth(docstring const & s) const
162 {
163         if (s.empty())
164                 return 0;
165
166         if (s[0] == '-')
167                 return -width(s.substr(1, s.size() - 1));
168         else
169                 return width(s);
170 }
171
172
173 QTextLayout const &
174 GuiFontMetrics::getTextLayout(docstring const & s, QFont font,
175                               bool const rtl, double const wordspacing) const
176 {
177         if (s != tl_cache_s_ || font != tl_cache_font_ || rtl != tl_cache_rtl_
178             || wordspacing != tl_cache_wordspacing_) {
179                 tl_cache_.setText(toqstr(s));
180                 font.setWordSpacing(wordspacing);
181                 tl_cache_.setFont(font);
182                 // Note that both setFlags and the enums are undocumented
183                 tl_cache_.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
184                 tl_cache_.beginLayout();
185                 tl_cache_.createLine();
186                 tl_cache_.endLayout();
187                 tl_cache_s_ = s;
188                 tl_cache_font_ = font;
189                 tl_cache_rtl_ = rtl;
190                 tl_cache_wordspacing_ = wordspacing;
191         }
192         return tl_cache_;
193 }
194
195
196 int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
197                           double const wordspacing) const
198 {
199         QFont copy = font_;
200         copy.setWordSpacing(wordspacing);
201         QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
202         return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
203 }
204
205
206 int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
207                           double const wordspacing) const
208 {
209         QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
210         int pos = tl.lineForTextPosition(0).xToCursor(x);
211         // correct x value to the actual cursor position.
212         x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
213         return pos;
214 }
215
216
217 bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
218 {
219         if (s.empty())
220                 return false;
221         QTextLayout tl;
222         tl.setText(toqstr(s));
223         tl.setFont(font_);
224         // Note that both setFlags and the enums are undocumented
225         tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
226         QTextOption to;
227         to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
228         tl.setTextOption(to);
229         tl.beginLayout();
230         QTextLine line = tl.createLine();
231         line.setLineWidth(x);
232         tl.createLine();
233         tl.endLayout();
234         if (int(line.naturalTextWidth()) > x)
235                 return false;
236         x = int(line.naturalTextWidth());
237         s = s.substr(0, line.textLength());
238         return true;
239 }
240
241
242 void GuiFontMetrics::rectText(docstring const & str,
243         int & w, int & ascent, int & descent) const
244 {
245         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
246
247         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
248         ascent = metrics_.ascent() + d;
249         descent = metrics_.descent() + d;
250 }
251
252
253
254 void GuiFontMetrics::buttonText(docstring const & str,
255         int & w, int & ascent, int & descent) const
256 {
257         rectText(str, w, ascent, descent);
258         w += Inset::TEXT_TO_INSET_OFFSET;
259 }
260
261
262 Dimension const GuiFontMetrics::defaultDimension() const
263 {
264         return Dimension(0, maxAscent(), maxDescent());
265 }
266
267
268 Dimension const GuiFontMetrics::dimension(char_type c) const
269 {
270         return Dimension(width(c), ascent(c), descent(c));
271 }
272
273
274 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
275                 char_type c) const
276 {
277         QRect r;
278         if (is_utf16(c))
279                 r = metrics_.boundingRect(ucs4_to_qchar(c));
280         else
281                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
282
283         AscendDescend ad = { -r.top(), r.bottom() + 1};
284         // We could as well compute the width but this is not really
285         // needed for now as it is done directly in width() below.
286         metrics_cache_.insert(c, ad);
287
288         return ad;
289 }
290
291
292 int GuiFontMetrics::width(char_type c) const
293 {
294         int value = width_cache_.value(c, outOfLimitMetric);
295         if (value != outOfLimitMetric)
296                 return value;
297
298         if (is_utf16(c))
299                 value = metrics_.width(ucs4_to_qchar(c));
300         else
301                 value = metrics_.width(toqstr(docstring(1, c)));
302
303         width_cache_.insert(c, value);
304
305         return value;
306 }
307
308
309 int GuiFontMetrics::ascent(char_type c) const
310 {
311         static AscendDescend const outOfLimitAD =
312                 {outOfLimitMetric, outOfLimitMetric};
313         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
314         if (value.ascent != outOfLimitMetric)
315                 return value.ascent;
316
317         value = fillMetricsCache(c);
318         return value.ascent;
319 }
320
321
322 int GuiFontMetrics::descent(char_type c) const
323 {
324         static AscendDescend const outOfLimitAD =
325                 {outOfLimitMetric, outOfLimitMetric};
326         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
327         if (value.descent != outOfLimitMetric)
328                 return value.descent;
329
330         value = fillMetricsCache(c);
331         return value.descent;
332 }
333
334 } // namespace frontend
335 } // namespace lyx