]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.C
document docstring abuse for symbol font code points
[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         return -r.top();
65 }
66
67
68 int GuiFontMetrics::descent(char_type c) const
69 {
70         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
71         return r.bottom() + 1;
72 }
73
74
75 int GuiFontMetrics::lbearing(char_type c) const
76 {
77         return metrics_.leftBearing(ucs4_to_qchar(c));
78 }
79
80
81 int GuiFontMetrics::rbearing(char_type c) const
82 {
83         // Qt rbearing is from the right edge of the char's width().
84         QChar sc = ucs4_to_qchar(c);
85         return metrics_.width(sc) - metrics_.rightBearing(sc);
86 }
87
88
89 int GuiFontMetrics::smallcapsWidth(QString const & s) const
90 {
91         int w = 0;
92         int const ls = s.size();
93
94         for (int i = 0; i < ls; ++i) {
95                 QChar const & c = s[i];
96                 QChar const uc = c.toUpper();
97                 if (c != uc)
98                         w += smallcaps_metrics_.width(uc);
99                 else
100                         w += metrics_.width(c);
101         }
102         return w;
103 }
104
105
106 int GuiFontMetrics::width(char_type const * s, size_t ls) const
107 {
108         // Caution: The following ucs4_to_something conversions work for
109         // symbol fonts only because they are no real conversions but simple
110         // casts in reality.
111
112         if (ls == 1 && !smallcaps_shape_) {
113                 QChar const c = ucs4_to_qchar(s[0]);
114                 return width(c.unicode());
115         }
116
117         QString ucs2;
118         ucs4_to_qstring(s, ls, ucs2);
119
120         if (smallcaps_shape_)
121                 return smallcapsWidth(ucs2);
122
123         int w = 0;
124         for (unsigned int i = 0; i < ls; ++i)
125                 w += width(ucs2[i].unicode());
126
127         return w;
128 }
129
130
131 int GuiFontMetrics::width(QString const & ucs2) const
132 {
133         int const ls = ucs2.size();
134         if (ls == 1 && !smallcaps_shape_) {
135                 return width(ucs2[0].unicode());
136         }
137
138         if (smallcaps_shape_)
139                 return smallcapsWidth(ucs2);
140
141         int w = 0;
142         for (int i = 0; i < ls; ++i)
143                 w += width(ucs2[i].unicode());
144
145         return w;
146 }
147
148
149 int GuiFontMetrics::signedWidth(docstring const & s) const
150 {
151         if (s[0] == '-')
152                 return -width(&(s[1]), s.length() - 1);
153         else
154                 return FontMetrics::width(s);
155 }
156
157
158 void GuiFontMetrics::rectText(docstring const & str,
159         int & w, int & ascent, int & descent) const
160 {
161         static int const d = 2;
162         w = FontMetrics::width(str) + d * 2 + 2;
163         ascent = metrics_.ascent() + d;
164         descent = metrics_.descent() + d;
165 }
166
167
168
169 void GuiFontMetrics::buttonText(docstring const & str,
170         int & w, int & ascent, int & descent) const
171 {
172         static int const d = 3;
173         w = FontMetrics::width(str) + d * 2 + 2;
174         ascent = metrics_.ascent() + d;
175         descent = metrics_.descent() + d;
176 }
177
178 #ifdef USE_LYX_FONTCACHE
179 int GuiFontMetrics::width(unsigned short val) const
180 {
181         if (widthcache_[val] == -1)
182                 widthcache_[val] = metrics_.width(QChar(val));
183         return widthcache_[val];
184 }
185 #endif
186
187 }
188 }