]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Remove support for LyXRC::force_paint_single_char
[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         map<docstring, int>::const_iterator it = strwidth_cache_.find(s);
116         if (it != strwidth_cache_.end()) {
117                 w = it->second;
118         } else {
119                 w = metrics_.width(toqstr(s));
120                 strwidth_cache_[s] = w;
121         }
122         return w;
123 }
124
125
126 int GuiFontMetrics::width(QString const & ucs2) const
127 {
128         return width(qstring_to_ucs4(ucs2));
129 }
130
131
132 int GuiFontMetrics::signedWidth(docstring const & s) const
133 {
134         if (s.empty())
135                 return 0;
136
137         if (s[0] == '-')
138                 return -width(s.substr(1, s.size() - 1));
139         else
140                 return width(s);
141 }
142
143
144 void GuiFontMetrics::rectText(docstring const & str,
145         int & w, int & ascent, int & descent) const
146 {
147         static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
148
149         w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
150         ascent = metrics_.ascent() + d;
151         descent = metrics_.descent() + d;
152 }
153
154
155
156 void GuiFontMetrics::buttonText(docstring const & str,
157         int & w, int & ascent, int & descent) const
158 {
159         rectText(str, w, ascent, descent);
160         w += Inset::TEXT_TO_INSET_OFFSET;
161 }
162
163
164 Dimension const GuiFontMetrics::defaultDimension() const
165 {
166         return Dimension(0, maxAscent(), maxDescent());
167 }
168
169
170 Dimension const GuiFontMetrics::dimension(char_type c) const
171 {
172         return Dimension(width(c), ascent(c), descent(c));
173 }
174
175
176 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
177                 char_type c) const
178 {
179         QRect r;
180         if (is_utf16(c))
181                 r = metrics_.boundingRect(ucs4_to_qchar(c));
182         else
183                 r = metrics_.boundingRect(toqstr(docstring(1, c)));
184
185         AscendDescend ad = { -r.top(), r.bottom() + 1};
186         // We could as well compute the width but this is not really
187         // needed for now as it is done directly in width() below.
188         metrics_cache_.insert(c, ad);
189
190         return ad;
191 }
192
193
194 int GuiFontMetrics::width(char_type c) const
195 {
196         int value = width_cache_.value(c, outOfLimitMetric);
197         if (value != outOfLimitMetric)
198                 return value;
199
200         if (is_utf16(c))
201                 value = metrics_.width(ucs4_to_qchar(c));
202         else
203                 value = metrics_.width(toqstr(docstring(1, c)));
204
205         width_cache_.insert(c, value);
206
207         return value;
208 }
209
210
211 int GuiFontMetrics::ascent(char_type c) const
212 {
213         static AscendDescend const outOfLimitAD =
214                 {outOfLimitMetric, outOfLimitMetric};
215         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
216         if (value.ascent != outOfLimitMetric)
217                 return value.ascent;
218
219         value = fillMetricsCache(c);
220         return value.ascent;
221 }
222
223
224 int GuiFontMetrics::descent(char_type c) const
225 {
226         static AscendDescend const outOfLimitAD =
227                 {outOfLimitMetric, outOfLimitMetric};
228         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
229         if (value.descent != outOfLimitMetric)
230                 return value.descent;
231
232         value = fillMetricsCache(c);
233         return value.descent;
234 }
235
236 } // namespace frontend
237 } // namespace lyx