]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.C
replace the map based width cache with an array based one.
[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 lyx::char_type;
23 using lyx::docstring;
24
25 using std::string;
26
27 namespace lyx {
28 namespace frontend {
29
30
31 GuiFontMetrics::GuiFontMetrics(QFont const & font)
32 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
33 {
34 #ifdef USE_LYX_FONTCACHE
35   for (int i = 0; i != 65536; ++i)
36     widthcache_[i] = -1;
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::ascent(char_type c) const
62 {
63         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
64         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
65         // value by the height: (x, -y-height, width, height).
66         // Other versions return: (x, -y, width, height)
67 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
68         return -r.top() - r.height();
69 #else
70         return -r.top();
71 #endif
72 }
73
74
75 int GuiFontMetrics::descent(char_type c) const
76 {
77         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
78         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
79         // value by the height: (x, -y-height, width, height).
80         // Other versions return: (x, -y, width, height)
81 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
82         return r.bottom() + r.height() + 1;
83 #else
84         return r.bottom() + 1;
85 #endif
86 }
87
88
89 int GuiFontMetrics::lbearing(char_type c) const
90 {
91         return metrics_.leftBearing(ucs4_to_qchar(c));
92 }
93
94
95 int GuiFontMetrics::rbearing(char_type c) const
96 {
97         // Qt rbearing is from the right edge of the char's width().
98         QChar sc = ucs4_to_qchar(c);
99         return metrics_.width(sc) - metrics_.rightBearing(sc);
100 }
101
102
103 int GuiFontMetrics::smallcapsWidth(QString const & s) const
104 {
105         int w = 0;
106         int const ls = s.size();
107
108         for (int i = 0; i < ls; ++i) {
109                 QChar const & c = s[i];
110                 QChar const uc = c.toUpper();
111                 if (c != uc)
112                         w += smallcaps_metrics_.width(uc);
113                 else
114                         w += metrics_.width(c);
115         }
116         return w;
117 }
118
119
120 int GuiFontMetrics::width(char_type const * s, size_t ls) const
121 {
122         if (ls == 1 && !smallcaps_shape_) {
123                 QChar const c = ucs4_to_qchar(s[0]);
124                 return width(c.unicode());
125         }
126
127         QString ucs2;
128         ucs4_to_qstring(s, ls, ucs2);
129
130         if (smallcaps_shape_)
131                 return smallcapsWidth(ucs2);
132
133         int w = 0;
134         for (unsigned int i = 0; i < ls; ++i)
135                 w += width(ucs2[i].unicode());
136
137         return w;
138 }
139
140
141 int GuiFontMetrics::width(QString const & ucs2) const
142 {
143         int const ls = ucs2.size();
144         if (ls == 1 && !smallcaps_shape_) {
145                 return width(ucs2[0].unicode());
146         }
147
148         if (smallcaps_shape_)
149                 return smallcapsWidth(ucs2);
150
151         int w = 0;
152         for (int i = 0; i < ls; ++i)
153                 w += width(ucs2[i].unicode());
154
155         return w;
156 }
157
158
159 int GuiFontMetrics::signedWidth(docstring const & s) const
160 {
161         if (s[0] == '-')
162                 return -FontMetrics::width(s.substr(1, s.length() - 1));
163         else
164                 return FontMetrics::width(s);
165 }
166
167
168 void GuiFontMetrics::rectText(docstring const & str,
169         int & w, int & ascent, int & descent) const
170 {
171         static int const d = 2;
172         w = FontMetrics::width(str) + d * 2 + 2;
173         ascent = metrics_.ascent() + d;
174         descent = metrics_.descent() + d;
175 }
176
177
178
179 void GuiFontMetrics::buttonText(docstring const & str,
180         int & w, int & ascent, int & descent) const
181 {
182         static int const d = 3;
183         w = FontMetrics::width(str) + d * 2 + 2;
184         ascent = metrics_.ascent() + d;
185         descent = metrics_.descent() + d;
186 }
187
188 #ifdef USE_LYX_FONTCACHE
189 int GuiFontMetrics::width(unsigned short val) const
190 {
191         if (widthcache_[val] == -1)
192                 widthcache_[val] = metrics_.width(QChar(val));
193         return widthcache_[val];
194 }
195 #endif
196
197 }
198 }