]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
Rename .C ==> .cpp for files in src/frontends/qt4, part two
[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 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         if (!rbearing_cache_.contains(c)) {
63                 // Qt rbearing is from the right edge of the char's width().
64                 QChar sc = ucs4_to_qchar(c);
65                 int rb = metrics_.width(sc) - metrics_.rightBearing(sc);
66                 rbearing_cache_.insert(c, rb);
67         }
68         return rbearing_cache_.value(c);
69 }
70
71
72 int GuiFontMetrics::smallcapsWidth(QString const & s) const
73 {
74         int w = 0;
75         int const ls = s.size();
76
77         for (int i = 0; i < ls; ++i) {
78                 QChar const & c = s[i];
79                 QChar const uc = c.toUpper();
80                 if (c != uc)
81                         w += smallcaps_metrics_.width(uc);
82                 else
83                         w += metrics_.width(c);
84         }
85         return w;
86 }
87
88
89 int GuiFontMetrics::width(docstring const & s) const
90 {
91         size_t ls = s.size();
92         if (ls == 0)
93                 return 0;
94
95         if (ls == 1 && !smallcaps_shape_) {
96                 return width(s[0]);
97         }
98
99         if (smallcaps_shape_)
100                 // Caution: The following ucs4 to QString conversions work
101                 // for symbol fonts only because they are no real conversions
102                 // but simple casts in reality. See comment in QLPainter::text()
103                 // for more explanation.
104                 return smallcapsWidth(toqstr(s));
105
106         int w = 0;
107         for (unsigned int i = 0; i < ls; ++i)
108                 w += width(s[i]);
109
110         return w;
111 }
112
113
114 int GuiFontMetrics::width(QString const & ucs2) const
115 {
116         int const ls = ucs2.size();
117         if (ls == 1 && !smallcaps_shape_) {
118                 return width(ucs2[0].unicode());
119         }
120
121         if (smallcaps_shape_)
122                 return smallcapsWidth(ucs2);
123
124         int w = 0;
125         for (int i = 0; i < ls; ++i)
126                 w += width(ucs2[i].unicode());
127
128         return w;
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 = 2;
148         w = width(str) + d * 2 + 2;
149         ascent = metrics_.ascent() + d;
150         descent = metrics_.descent() + d;
151 }
152
153
154
155 void GuiFontMetrics::buttonText(docstring const & str,
156         int & w, int & ascent, int & descent) const
157 {
158         static int const d = 3;
159         w = width(str) + d * 2 + 2;
160         ascent = metrics_.ascent() + d;
161         descent = metrics_.descent() + d;
162 }
163
164
165 Dimension const GuiFontMetrics::defaultDimension() const
166 {
167         return Dimension(0, maxAscent(), maxDescent());
168 }
169
170
171 Dimension const GuiFontMetrics::dimension(char_type c) const
172 {
173         return Dimension(width(c), ascent(c), descent(c));
174 }
175
176
177 void GuiFontMetrics::fillMetricsCache(char_type c) const
178 {
179         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
180         AscendDescend ad = { -r.top(), r.bottom() + 1};
181         // We could as well compute the width but this is not really
182         // needed for now as it is done directly in width() below.
183         metrics_cache_.insert(c, ad);
184 }
185
186
187 int GuiFontMetrics::width(char_type c) const
188 {
189         if (smallcaps_shape_)
190                 return smallcapsWidth(ucs4_to_qchar(c));
191
192         if (!width_cache_.contains(c)) {
193                 width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
194         }
195
196         return width_cache_.value(c);
197 }
198
199
200 int GuiFontMetrics::ascent(char_type c) const
201 {
202         if (!metrics_cache_.contains(c))
203                 fillMetricsCache(c);
204
205         return metrics_cache_.value(c).ascent;
206 }
207
208
209 int GuiFontMetrics::descent(char_type c) const
210 {
211         if (!metrics_cache_.contains(c))
212                 fillMetricsCache(c);
213
214         return metrics_cache_.value(c).descent;
215 }
216
217 } // frontend
218 } // lyx