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