]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiFontMetrics.C
b90535690170ff640e129d2ea619ded6ee4153f2
[features.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 #include "dimension.h"
20
21 #include "support/unicode.h"
22
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28 GuiFontMetrics::GuiFontMetrics(QFont const & font)
29 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
30 {
31 }
32
33
34 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
35 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
36 {
37 }
38
39
40 int GuiFontMetrics::maxAscent() const
41 {
42         return metrics_.ascent();
43 }
44
45
46 int GuiFontMetrics::maxDescent() const
47 {
48         // We add 1 as the value returned by QT is different than X
49         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
50         return metrics_.descent() + 1;
51 }
52
53
54 int GuiFontMetrics::lbearing(char_type c) const
55 {
56         return metrics_.leftBearing(ucs4_to_qchar(c));
57 }
58
59
60 int GuiFontMetrics::rbearing(char_type c) const
61 {
62         // Qt rbearing is from the right edge of the char's width().
63         QChar sc = ucs4_to_qchar(c);
64         return metrics_.width(sc) - metrics_.rightBearing(sc);
65 }
66
67
68 int GuiFontMetrics::smallcapsWidth(QString const & s) const
69 {
70         int w = 0;
71         int const ls = s.size();
72
73         for (int i = 0; i < ls; ++i) {
74                 QChar const & c = s[i];
75                 QChar const uc = c.toUpper();
76                 if (c != uc)
77                         w += smallcaps_metrics_.width(uc);
78                 else
79                         w += metrics_.width(c);
80         }
81         return w;
82 }
83
84
85 int GuiFontMetrics::width(char_type const * s, size_t ls) const
86 {
87         // Caution: The following ucs4_to_something conversions work for
88         // symbol fonts only because they are no real conversions but simple
89         // casts in reality.
90
91         if (ls == 1 && !smallcaps_shape_) {
92                 return width(s[0]);
93         }
94
95         if (smallcaps_shape_) {
96                 QString ucs2;
97                 ucs4_to_qstring(s, ls, ucs2);
98                 return smallcapsWidth(ucs2);
99         }
100
101         int w = 0;
102         for (unsigned int i = 0; i < ls; ++i)
103                 w += width(s[i]);
104
105         return w;
106 }
107
108
109 int GuiFontMetrics::width(QString const & ucs2) const
110 {
111         int const ls = ucs2.size();
112         if (ls == 1 && !smallcaps_shape_) {
113                 return width(ucs2[0].unicode());
114         }
115
116         if (smallcaps_shape_)
117                 return smallcapsWidth(ucs2);
118
119         int w = 0;
120         for (int i = 0; i < ls; ++i)
121                 w += width(ucs2[i].unicode());
122
123         return w;
124 }
125
126
127 int GuiFontMetrics::signedWidth(docstring const & s) const
128 {
129         if (s.empty())
130                 return 0;
131
132         if (s[0] == '-')
133                 return -width(&(s[1]), s.length() - 1);
134         else
135                 return FontMetrics::width(s);
136 }
137
138
139 void GuiFontMetrics::rectText(docstring const & str,
140         int & w, int & ascent, int & descent) const
141 {
142         static int const d = 2;
143         w = FontMetrics::width(str) + d * 2 + 2;
144         ascent = metrics_.ascent() + d;
145         descent = metrics_.descent() + d;
146 }
147
148
149
150 void GuiFontMetrics::buttonText(docstring const & str,
151         int & w, int & ascent, int & descent) const
152 {
153         static int const d = 3;
154         w = FontMetrics::width(str) + d * 2 + 2;
155         ascent = metrics_.ascent() + d;
156         descent = metrics_.descent() + d;
157 }
158
159
160 Dimension const GuiFontMetrics::defaultDimension() const
161 {
162         return Dimension(0, maxAscent(), maxDescent());
163 }
164
165
166 Dimension const GuiFontMetrics::dimension(char_type c) const
167 {
168         return Dimension(width(c), ascent(c), descent(c));
169 }
170
171
172 void GuiFontMetrics::fillMetricsCache(char_type c) const
173 {
174         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
175         AscendDescend ad = { -r.top(), r.bottom() + 1};
176         // We could as well compute the width but this is not really
177         // needed for now as it is done directly in width() below.
178         metrics_cache_.insert(c, ad);
179 }
180
181
182 int GuiFontMetrics::width(char_type c) const
183 {
184         if (smallcaps_shape_)
185                 return smallcapsWidth(ucs4_to_qchar(c));
186
187         if (!width_cache_.contains(c)) {
188                 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
189         }
190
191         return width_cache_.value(c);
192 }
193
194
195 int GuiFontMetrics::ascent(char_type c) const
196 {
197         if (!metrics_cache_.contains(c))
198                 fillMetricsCache(c);
199
200         return metrics_cache_.value(c).ascent;
201 }
202
203
204 int GuiFontMetrics::descent(char_type c) const
205 {
206         if (!metrics_cache_.contains(c))
207                 fillMetricsCache(c);
208
209         return metrics_cache_.value(c).descent;
210 }
211
212 } // frontend
213 } // lyx