]> git.lyx.org Git - features.git/blob - src/frontends/qt3/GuiFontMetrics.C
This commit saves the need to check for lyx::use_gui in a number of places.
[features.git] / src / frontends / qt3 / 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
28 namespace lyx {
29 namespace frontend {
30
31
32 GuiFontMetrics::GuiFontMetrics(QFont const & font)
33 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
34 {
35 }
36
37
38 GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
39 : metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
40 {
41 }
42
43
44 int GuiFontMetrics::maxAscent() const
45 {
46         return metrics_.ascent();
47 }
48
49
50 int GuiFontMetrics::maxDescent() const
51 {
52         // We add 1 as the value returned by QT is different than X
53         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
54         return metrics_.descent() + 1;
55 }
56
57
58 int GuiFontMetrics::ascent(char_type c) const
59 {
60         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
61         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
62         // value by the height: (x, -y-height, width, height).
63         // Other versions return: (x, -y, width, height)
64 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
65         return -r.top() - r.height();
66 #else
67         return -r.top();
68 #endif
69 }
70
71
72 int GuiFontMetrics::descent(char_type c) const
73 {
74         QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
75         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
76         // value by the height: (x, -y-height, width, height).
77         // Other versions return: (x, -y, width, height)
78 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
79         return r.bottom() + r.height() + 1;
80 #else
81         return r.bottom() + 1;
82 #endif
83 }
84
85
86 int GuiFontMetrics::lbearing(char_type c) const
87 {
88         return metrics_.leftBearing(ucs4_to_qchar(c));
89 }
90
91
92 int GuiFontMetrics::rbearing(char_type c) const
93 {
94         // Qt rbearing is from the right edge of the char's width().
95         QChar sc = ucs4_to_qchar(c);
96         return metrics_.width(sc) - metrics_.rightBearing(sc);
97 }
98
99
100 int GuiFontMetrics::smallcapsWidth(QString const & s) const
101 {
102         int w = 0;
103         int const ls = s.length();
104
105         for (int i = 0; i < ls; ++i) {
106                 QChar const & c = s[i];
107                 QChar const uc = c.upper();
108                 if (c != uc)
109                         w += smallcaps_metrics_.width(uc);
110                 else
111                         w += metrics_.width(c);
112         }
113         return w;
114 }
115
116
117 int GuiFontMetrics::width(char_type const * s, size_t ls) const
118 {
119         QString const ucs2 = toqstr(s, ls);
120
121         if (smallcaps_shape_)
122                 return smallcapsWidth(ucs2);
123
124         if (ls == 1)
125                 return width(ucs2[0].unicode());
126
127         int w = 0;
128         for (unsigned int i = 0; i < ls; ++i)
129                 w += width(ucs2[i].unicode());
130
131         return w;
132 }
133
134
135 int GuiFontMetrics::signedWidth(docstring const & s) const
136 {
137         if (s[0] == '-')
138                 return -FontMetrics::width(s.substr(1, s.length() - 1));
139         else
140                 return FontMetrics::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 = FontMetrics::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 = FontMetrics::width(str) + d * 2 + 2;
160         ascent = metrics_.ascent() + d;
161         descent = metrics_.descent() + d;
162 }
163
164 #ifdef USE_LYX_FONTCACHE
165 int GuiFontMetrics::width(unsigned short val) const
166 {
167         GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val);
168         if (cit != widthcache.end())
169                 return cit->second;
170
171         int const w = metrics_.width(QChar(val));
172         widthcache[val] = w;
173         return w;
174 }
175 #endif
176
177 }
178 }