]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
52c31d904076fdca8af4abc5797e551ec4f32032
[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 "Language.h"
19 #include "Dimension.h"
20
21 #include "support/unicode.h"
22
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28 // Caution: When using ucs4_to_qchar() in these methods, this is no
29 // real conversion but a simple cast in reality. This is the reason
30 // why this works well for symbol fonts used in mathed too, even though
31 // these are not real ucs4 characters. These are codepoints in the
32 // modern fonts used, nothing unicode related.
33 // See comment in QLPainter::text() for more explanation.
34
35 GuiFontMetrics::GuiFontMetrics(QFont const & font)
36 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
37 {
38 }
39
40
41 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
42 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
43 {
44 }
45
46
47 int GuiFontMetrics::maxAscent() const
48 {
49         return metrics_.ascent();
50 }
51
52
53 int GuiFontMetrics::maxDescent() const
54 {
55         // We add 1 as the value returned by QT is different than X
56         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
57         return metrics_.descent() + 1;
58 }
59
60
61 int GuiFontMetrics::lbearing(char_type c) const
62 {
63         if (!is_utf16(c))
64                 // FIXME: QFontMetrics::leftBearingdoes not support the
65                 //        full unicode range. Once it does, we could use:
66                 //return metrics_.leftBearing(toqstr(docstring(1,c)));
67                 return 0;
68
69         return metrics_.leftBearing(ucs4_to_qchar(c));
70 }
71
72
73 namespace {
74 int const outOfLimitMetric = -10000;
75 }
76
77
78 int GuiFontMetrics::rbearing(char_type c) const
79 {
80         int value = rbearing_cache_.value(c, outOfLimitMetric);
81         if (value != outOfLimitMetric)
82                 return value;
83
84         // Qt rbearing is from the right edge of the char's width().
85         if (is_utf16(c)) {
86                 QChar sc = ucs4_to_qchar(c);
87                 value = width(c) - metrics_.rightBearing(sc);
88         } else
89                 // FIXME: QFontMetrics::leftBearingdoes not support the
90                 //        full unicode range. Once it does, we could use:
91                 // metrics_.rightBearing(toqstr(docstring(1,c)));
92                 value = width(c);
93
94         rbearing_cache_.insert(c, value);
95
96         return value;
97 }
98
99
100 int GuiFontMetrics::smallcapsWidth(char_type c) const
101 {
102         // FIXME: Optimisation probably needed: we don't use the width cache.
103         if (is_utf16(c)) {
104                 QChar const qc = ucs4_to_qchar(c);
105                 QChar const uc = qc.toUpper();
106                 if (qc != uc)
107                         return smallcaps_metrics_.width(uc);
108                 else
109                         return metrics_.width(qc);
110         } else {
111                 QString const s = toqstr(docstring(1,c));
112                 QString const us = s.toUpper();
113                 if (s != us)
114                         return smallcaps_metrics_.width(us);
115                 else
116                         return metrics_.width(s);
117         }
118 }
119
120
121 int GuiFontMetrics::width(docstring const & s) const
122 {
123         size_t ls = s.size();
124         int w = 0;
125         for (unsigned int i = 0; i < ls; ++i)
126                 w += width(s[i]);
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 = 2;
154         w = width(str) + d * 2 + 2;
155         ascent = metrics_.ascent() + d;
156         descent = metrics_.descent() + d;
157 }
158
159
160
161 void GuiFontMetrics::buttonText(docstring const & str,
162         int & w, int & ascent, int & descent) const
163 {
164         static int const d = 3;
165         w = width(str) + d * 2 + 2;
166         ascent = metrics_.ascent() + d;
167         descent = metrics_.descent() + d;
168 }
169
170
171 Dimension const GuiFontMetrics::defaultDimension() const
172 {
173         return Dimension(0, maxAscent(), maxDescent());
174 }
175
176
177 Dimension const GuiFontMetrics::dimension(char_type c) const
178 {
179         return Dimension(width(c), ascent(c), descent(c));
180 }
181
182
183 GuiFontMetrics::AscendDescend const GuiFontMetrics::fillMetricsCache(
184                 char_type c) const
185 {
186         QRect r;
187         if (is_utf16(c))
188                 r = metrics_.boundingRect(ucs4_to_qchar(c));
189         else
190                 r = metrics_.boundingRect(toqstr(docstring(1,c)));
191
192         AscendDescend ad = { -r.top(), r.bottom() + 1};
193         // We could as well compute the width but this is not really
194         // needed for now as it is done directly in width() below.
195         metrics_cache_.insert(c, ad);
196
197         return ad;
198 }
199
200
201 int GuiFontMetrics::width(char_type c) const
202 {
203         if (smallcaps_shape_)
204                 return smallcapsWidth(c);
205
206         int value = width_cache_.value(c, outOfLimitMetric);
207         if (value != outOfLimitMetric)
208                 return value;
209
210         if (is_utf16(c))
211                 value = metrics_.width(ucs4_to_qchar(c));
212         else
213                 value = metrics_.width(toqstr(docstring(1,c)));
214
215         width_cache_.insert(c, value);
216
217         return value;
218 }
219
220
221 int GuiFontMetrics::ascent(char_type c) const
222 {
223         static AscendDescend const outOfLimitAD = 
224                 {outOfLimitMetric, outOfLimitMetric};
225         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
226         if (value.ascent != outOfLimitMetric)
227                 return value.ascent;
228
229         value = fillMetricsCache(c);
230         return value.ascent;
231 }
232
233
234 int GuiFontMetrics::descent(char_type c) const
235 {
236         static AscendDescend const outOfLimitAD = 
237                 {outOfLimitMetric, outOfLimitMetric};
238         AscendDescend value = metrics_cache_.value(c, outOfLimitAD);
239         if (value.descent != outOfLimitMetric)
240                 return value.descent;
241
242         value = fillMetricsCache(c);
243         return value.descent;
244 }
245
246 } // frontend
247 } // lyx