]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.C
* GuiFontMetrics: new metrics(QString) method
[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 }
35
36
37 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
38 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
39 {
40 }
41
42
43 int GuiFontMetrics::maxAscent() const
44 {
45         return metrics_.ascent();
46 }
47
48
49 int GuiFontMetrics::maxDescent() const
50 {
51         // We add 1 as the value returned by QT is different than X
52         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
53         return metrics_.descent() + 1;
54 }
55
56
57 int GuiFontMetrics::ascent(char_type c) const
58 {
59         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
60         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
61         // value by the height: (x, -y-height, width, height).
62         // Other versions return: (x, -y, width, height)
63 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
64         return -r.top() - r.height();
65 #else
66         return -r.top();
67 #endif
68 }
69
70
71 int GuiFontMetrics::descent(char_type c) const
72 {
73         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
74         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
75         // value by the height: (x, -y-height, width, height).
76         // Other versions return: (x, -y, width, height)
77 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
78         return r.bottom() + r.height() + 1;
79 #else
80         return r.bottom() + 1;
81 #endif
82 }
83
84
85 int GuiFontMetrics::lbearing(char_type c) const
86 {
87         return metrics_.leftBearing(ucs4_to_qchar(c));
88 }
89
90
91 int GuiFontMetrics::rbearing(char_type c) const
92 {
93         // Qt rbearing is from the right edge of the char's width().
94         QChar sc = ucs4_to_qchar(c);
95         return metrics_.width(sc) - metrics_.rightBearing(sc);
96 }
97
98
99 int GuiFontMetrics::smallcapsWidth(QString const & s) const
100 {
101         int w = 0;
102         int const ls = s.size();
103
104         for (int i = 0; i < ls; ++i) {
105                 QChar const & c = s[i];
106                 QChar const uc = c.toUpper();
107                 if (c != uc)
108                         w += smallcaps_metrics_.width(uc);
109                 else
110                         w += metrics_.width(c);
111         }
112         return w;
113 }
114
115
116 int GuiFontMetrics::width(char_type const * s, size_t ls) const
117 {
118         if (ls == 1 && !smallcaps_shape_) {
119                 QChar const c = ucs4_to_qchar(s[0]);
120                 return width(c.unicode());
121         }
122
123         QString ucs2;
124         ucs4_to_qstring(s, ls, ucs2);
125
126         if (smallcaps_shape_)
127                 return smallcapsWidth(ucs2);
128
129         int w = 0;
130         for (unsigned int i = 0; i < ls; ++i)
131                 w += width(ucs2[i].unicode());
132
133         return w;
134 }
135
136
137 int GuiFontMetrics::width(QString const & ucs2) const
138 {
139         int const ls = ucs2.size();
140         if (ls == 1 && !smallcaps_shape_) {
141                 return width(ucs2[0].unicode());
142         }
143
144         if (smallcaps_shape_)
145                 return smallcapsWidth(ucs2);
146
147         int w = 0;
148         for (int i = 0; i < ls; ++i)
149                 w += width(ucs2[i].unicode());
150
151         return w;
152 }
153
154
155 int GuiFontMetrics::signedWidth(docstring const & s) const
156 {
157         if (s[0] == '-')
158                 return -FontMetrics::width(s.substr(1, s.length() - 1));
159         else
160                 return FontMetrics::width(s);
161 }
162
163
164 void GuiFontMetrics::rectText(docstring const & str,
165         int & w, int & ascent, int & descent) const
166 {
167         static int const d = 2;
168         w = FontMetrics::width(str) + d * 2 + 2;
169         ascent = metrics_.ascent() + d;
170         descent = metrics_.descent() + d;
171 }
172
173
174
175 void GuiFontMetrics::buttonText(docstring const & str,
176         int & w, int & ascent, int & descent) const
177 {
178         static int const d = 3;
179         w = FontMetrics::width(str) + d * 2 + 2;
180         ascent = metrics_.ascent() + d;
181         descent = metrics_.descent() + d;
182 }
183
184 #ifdef USE_LYX_FONTCACHE
185 int GuiFontMetrics::width(unsigned short val) const
186 {
187         GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val);
188         if (cit != widthcache.end())
189                 return cit->second;
190
191         int const w = metrics_.width(QChar(val));
192         widthcache[val] = w;
193         return w;
194 }
195 #endif
196
197 }
198 }