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