]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.C
This commit fixes the following bug:
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.C
1 /**
2  * \file GuiFontMetrics.C
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
20 #include "support/unicode.h"
21
22 using std::string;
23
24 namespace lyx {
25 namespace frontend {
26
27
28 GuiFontMetrics::GuiFontMetrics(QFont const & font)
29 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
30 {
31 #ifdef USE_LYX_FONTCACHE
32  for (int i = 0; i != 65536; ++i) {
33          metrics_cache_[i].width = -1000;
34          metrics_cache_[i].ascent = -1000;
35          metrics_cache_[i].descent = -1000;
36  }
37 #endif
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         return metrics_.leftBearing(ucs4_to_qchar(c));
64 }
65
66
67 int GuiFontMetrics::rbearing(char_type c) const
68 {
69         // Qt rbearing is from the right edge of the char's width().
70         QChar sc = ucs4_to_qchar(c);
71         return metrics_.width(sc) - metrics_.rightBearing(sc);
72 }
73
74
75 int GuiFontMetrics::smallcapsWidth(QString const & s) const
76 {
77         int w = 0;
78         int const ls = s.size();
79
80         for (int i = 0; i < ls; ++i) {
81                 QChar const & c = s[i];
82                 QChar const uc = c.toUpper();
83                 if (c != uc)
84                         w += smallcaps_metrics_.width(uc);
85                 else
86                         w += metrics_.width(c);
87         }
88         return w;
89 }
90
91
92 int GuiFontMetrics::width(char_type const * s, size_t ls) const
93 {
94         // Caution: The following ucs4_to_something conversions work for
95         // symbol fonts only because they are no real conversions but simple
96         // casts in reality.
97
98         if (ls == 1 && !smallcaps_shape_) {
99                 return width(s[0]);
100         }
101
102         if (smallcaps_shape_) {
103                 QString ucs2;
104                 ucs4_to_qstring(s, ls, ucs2);
105                 return smallcapsWidth(ucs2);
106         }
107
108         int w = 0;
109         for (unsigned int i = 0; i < ls; ++i)
110                 w += width(s[i]);
111
112         return w;
113 }
114
115
116 int GuiFontMetrics::width(QString const & ucs2) const
117 {
118         int const ls = ucs2.size();
119         if (ls == 1 && !smallcaps_shape_) {
120                 return width(ucs2[0].unicode());
121         }
122
123         if (smallcaps_shape_)
124                 return smallcapsWidth(ucs2);
125
126         int w = 0;
127         for (int i = 0; i < ls; ++i)
128                 w += width(ucs2[i].unicode());
129
130         return w;
131 }
132
133
134 int GuiFontMetrics::signedWidth(docstring const & s) const
135 {
136         if (s.empty())
137                 return 0;
138
139         if (s[0] == '-')
140                 return -width(&(s[1]), s.length() - 1);
141         else
142                 return FontMetrics::width(s);
143 }
144
145
146 void GuiFontMetrics::rectText(docstring const & str,
147         int & w, int & ascent, int & descent) const
148 {
149         static int const d = 2;
150         w = FontMetrics::width(str) + d * 2 + 2;
151         ascent = metrics_.ascent() + d;
152         descent = metrics_.descent() + d;
153 }
154
155
156
157 void GuiFontMetrics::buttonText(docstring const & str,
158         int & w, int & ascent, int & descent) const
159 {
160         static int const d = 3;
161         w = FontMetrics::width(str) + d * 2 + 2;
162         ascent = metrics_.ascent() + d;
163         descent = metrics_.descent() + d;
164 }
165
166 #ifndef USE_LYX_FONTCACHE
167
168 int GuiFontMetrics::ascent(char_type c) const
169 {
170         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
171         return -r.top();
172 }
173
174
175 int GuiFontMetrics::descent(char_type c) const
176 {
177         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
178         return r.bottom() + 1;
179 }
180
181 #else
182
183 void GuiFontMetrics::fillCache(unsigned short val) const
184 {
185         QRect const & r = metrics_.boundingRect(QChar(val));
186         metrics_cache_[val].descent = r.bottom() + 1;
187         metrics_cache_[val].ascent = -r.top();
188         // We could as well compute the width but this is not really
189         // needed for now as it is done directly in width() below.
190         //metrics_cache_[val].width = metrics_.width(QChar(val));
191 }
192
193
194 int GuiFontMetrics::width(char_type c) const
195 {
196         unsigned short val = static_cast<unsigned short>(c);
197         if (metrics_cache_[val].width == -1000)
198                 metrics_cache_[val].width = metrics_.width(QChar(val));
199
200         return metrics_cache_[val].width;
201 }
202
203
204 int GuiFontMetrics::ascent(char_type c) const
205 {
206         unsigned short val = static_cast<unsigned short>(c);
207         if (metrics_cache_[val].ascent == -1000)
208                 fillCache(val);
209
210         return metrics_cache_[val].ascent;
211 }
212
213
214 int GuiFontMetrics::descent(char_type c) const
215 {
216         unsigned short val = static_cast<unsigned short>(c);
217         if (metrics_cache_[val].descent == -1000)
218                 fillCache(val);
219
220         return metrics_cache_[val].descent;
221 }
222
223 #endif
224
225 } // frontend
226 } // lyx