]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Implement real string width computation
[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
28 namespace lyx {
29 namespace frontend {
30
31 namespace {
32 /**
33  * Convert a UCS4 character into a QChar.
34  * This is a hack (it does only make sense for the common part of the UCS4
35  * and UTF16 encodings) and should not be used.
36  * This does only exist because of performance reasons (a real conversion
37  * using iconv is too slow on windows).
38  *
39  * This is no real conversion but a simple cast in reality. This is the reason
40  * why this works well for symbol fonts used in mathed too, even though
41  * these are not real ucs4 characters. These are codepoints in the
42  * computer modern fonts used, nothing unicode related.
43  * See comment in QLPainter::text() for more explanation.
44  **/
45 inline QChar const ucs4_to_qchar(char_type const ucs4)
46 {
47         LATTEST(is_utf16(ucs4));
48         return QChar(static_cast<unsigned short>(ucs4));
49 }
50 } // anon namespace
51
52
53 GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font, 0)
54 {
55 }
56
57
58 int GuiFontMetrics::maxAscent() const
59 {
60         return metrics_.ascent();
61 }
62
63
64 int GuiFontMetrics::maxDescent() const
65 {
66         // We add 1 as the value returned by QT is different than X
67         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
68         return metrics_.descent() + 1;
69 }
70
71
72 int GuiFontMetrics::lbearing(char_type c) const
73 {
74         if (!is_utf16(c))
75                 // FIXME: QFontMetrics::leftBearingdoes not support the
76                 //        full unicode range. Once it does, we could use:
77                 //return metrics_.leftBearing(toqstr(docstring(1, c)));
78                 return 0;
79
80         return metrics_.leftBearing(ucs4_to_qchar(c));
81 }
82
83
84 namespace {
85 int const outOfLimitMetric = -10000;
86 }
87
88
89 int GuiFontMetrics::rbearing(char_type c) const
90 {
91         int value = rbearing_cache_.value(c, outOfLimitMetric);
92         if (value != outOfLimitMetric)
93                 return value;
94
95         // Qt rbearing is from the right edge of the char's width().
96         if (is_utf16(c)) {
97                 QChar sc = ucs4_to_qchar(c);
98                 value = width(c) - metrics_.rightBearing(sc);
99         } else {
100                 // FIXME: QFontMetrics::leftBearing does not support the
101                 //        full unicode range. Once it does, we could use:
102                 // metrics_.rightBearing(toqstr(docstring(1, c)));
103                 value = width(c);
104         }
105
106         rbearing_cache_.insert(c, value);
107
108         return value;
109 }
110
111
112 int GuiFontMetrics::width(docstring const & s) const
113 {
114         int w = 0;
115         if (lyxrc.force_paint_single_char) {
116                 size_t const ls = s.size();
117                 for (size_t i = 0; i < ls; ++i)
118                         w += width(s[i]);
119         } else {
120                 map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
121                 if (it != strwidth_cache_.end()) {
122                         w = it->second;
123                 } else {
124                         w = metrics_.width(toqstr(s));
125                         strwidth_cache_[s] = w;
126                 }
127         }
128         return w;
129 }
130
131
132 int GuiFontMetrics::width(QString const & ucs2) const
133 {
134         return width(qstring_to_ucs4(ucs2));
135 }
136
137
138 int GuiFontMetrics::signedWidth(docstring const & s) const
139 {
140         if (s.empty())
141                 return 0;
142
143         if (s[0] == '-')
144                 return -width(s.substr(1, s.size() - 1));
145         else
146                 return width(s);
147 }
148
149
150 void GuiFontMetrics::rectText(docstring const & str,
151         int & w, int & ascent, int & descent) const
152 {
153         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
154
155         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
156         ascent = metrics_.ascent() + d;
157         descent = metrics_.descent() + d;
158 }
159
160
161
162 void GuiFontMetrics::buttonText(docstring const & str,
163         int & w, int & ascent, int & descent) const
164 {
165         rectText(str, w, ascent, descent);
166         w += Inset::TEXT_TO_INSET_OFFSET;
167 }
168
169
170 Dimension const GuiFontMetrics::defaultDimension() const
171 {
172         return Dimension(0, maxAscent(), maxDescent());
173 }
174
175
176 Dimension const GuiFontMetrics::dimension(char_type c) const
177 {
178         return Dimension(width(c), ascent(c), descent(c));
179 }
180
181
182 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
183                 char_type c) const
184 {
185         QRect r;
186         if (is_utf16(c))
187                 r = metrics_.boundingRect(ucs4_to_qchar(c));
188         else
189                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
190
191         AscendDescend ad = { -r.top(), r.bottom() + 1};
192         // We could as well compute the width but this is not really
193         // needed for now as it is done directly in width() below.
194         metrics_cache_.insert(c, ad);
195
196         return ad;
197 }
198
199
200 int GuiFontMetrics::width(char_type c) const
201 {
202         int value = width_cache_.value(c, outOfLimitMetric);
203         if (value != outOfLimitMetric)
204                 return value;
205
206         if (is_utf16(c))
207                 value = metrics_.width(ucs4_to_qchar(c));
208         else
209                 value = metrics_.width(toqstr(docstring(1, c)));
210
211         width_cache_.insert(c, value);
212
213         return value;
214 }
215
216
217 int GuiFontMetrics::ascent(char_type c) const
218 {
219         static AscendDescend const outOfLimitAD =
220                 {outOfLimitMetric, outOfLimitMetric};
221         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
222         if (value.ascent != outOfLimitMetric)
223                 return value.ascent;
224
225         value = fillMetricsCache(c);
226         return value.ascent;
227 }
228
229
230 int GuiFontMetrics::descent(char_type c) const
231 {
232         static AscendDescend const outOfLimitAD =
233                 {outOfLimitMetric, outOfLimitMetric};
234         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
235         if (value.descent != outOfLimitMetric)
236                 return value.descent;
237
238         value = fillMetricsCache(c);
239         return value.descent;
240 }
241
242 } // namespace frontend
243 } // namespace lyx