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