]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
1 /**
2  * \file GuiFontMetrics.cpp
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 #include "Dimension.h"
20
21 #include "support/unicode.h"
22
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28 // Caution: When using ucs4_to_qchar() in these methods, this is no
29 // real conversion but a simple cast in reality. This is the reason
30 // why this works well for symbol fonts used in mathed too, even though
31 // these are not real ucs4 characters. These are codepoints in the
32 // modern fonts used, nothing unicode related.
33 // See comment in QLPainter::text() for more explanation.
34
35 GuiFontMetrics::GuiFontMetrics(QFont const & font)
36 : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
37 {
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::lbearing(char_type c) const
62 {
63         if (!is_utf16(c))
64                 // FIXME: QFontMetrics::leftBearingdoes not support the
65                 //        full unicode range. Once it does, we could use:
66                 //return metrics_.leftBearing(toqstr(docstring(1,c)));
67                 return 0;
68
69         return metrics_.leftBearing(ucs4_to_qchar(c));
70 }
71
72
73 int GuiFontMetrics::rbearing(char_type c) const
74 {
75         if (!rbearing_cache_.contains(c)) {
76                 // Qt rbearing is from the right edge of the char's width().
77                 int rb;
78                 if (is_utf16(c)) {
79                         QChar sc = ucs4_to_qchar(c);
80                         rb = width(c) - metrics_.rightBearing(sc);
81                 } else
82                         // FIXME: QFontMetrics::leftBearingdoes not support the
83                         //        full unicode range. Once it does, we could use:
84                         // metrics_.rightBearing(toqstr(docstring(1,c)));
85                         rb = width(c);
86
87                 rbearing_cache_.insert(c, rb);
88         }
89         return rbearing_cache_.value(c);
90 }
91
92
93 int GuiFontMetrics::smallcapsWidth(char_type c) const
94 {
95         // FIXME: Optimisation probably needed: we don't use the width cache.
96         if (is_utf16(c)) {
97                 QChar const qc = ucs4_to_qchar(c);
98                 QChar const uc = qc.toUpper();
99                 if (qc != uc)
100                         return smallcaps_metrics_.width(uc);
101                 else
102                         return metrics_.width(qc);
103         } else {
104                 QString const s = toqstr(docstring(1,c));
105                 QString const us = s.toUpper();
106                 if (s != us)
107                         return smallcaps_metrics_.width(us);
108                 else
109                         return metrics_.width(s);
110         }
111 }
112
113
114 int GuiFontMetrics::width(docstring const & s) const
115 {
116         size_t ls = s.size();
117         int w = 0;
118         for (unsigned int i = 0; i < ls; ++i)
119                 w += width(s[i]);
120
121         return w;
122 }
123
124
125 int GuiFontMetrics::width(QString const & ucs2) const
126 {
127         return width(qstring_to_ucs4(ucs2));
128 }
129
130
131 int GuiFontMetrics::signedWidth(docstring const & s) const
132 {
133         if (s.empty())
134                 return 0;
135
136         if (s[0] == '-')
137                 return -width(s.substr(1, s.size() - 1));
138         else
139                 return width(s);
140 }
141
142
143 void GuiFontMetrics::rectText(docstring const & str,
144         int & w, int & ascent, int & descent) const
145 {
146         static int const d = 2;
147         w = width(str) + d * 2 + 2;
148         ascent = metrics_.ascent() + d;
149         descent = metrics_.descent() + d;
150 }
151
152
153
154 void GuiFontMetrics::buttonText(docstring const & str,
155         int & w, int & ascent, int & descent) const
156 {
157         static int const d = 3;
158         w = width(str) + d * 2 + 2;
159         ascent = metrics_.ascent() + d;
160         descent = metrics_.descent() + d;
161 }
162
163
164 Dimension const GuiFontMetrics::defaultDimension() const
165 {
166         return Dimension(0, maxAscent(), maxDescent());
167 }
168
169
170 Dimension const GuiFontMetrics::dimension(char_type c) const
171 {
172         return Dimension(width(c), ascent(c), descent(c));
173 }
174
175
176 void GuiFontMetrics::fillMetricsCache(char_type c) const
177 {
178         QRect r;
179         if (is_utf16(c))
180                 r = metrics_.boundingRect(ucs4_to_qchar(c));
181         else
182                 r = metrics_.boundingRect(toqstr(docstring(1,c)));
183
184         AscendDescend ad = { -r.top(), r.bottom() + 1};
185         // We could as well compute the width but this is not really
186         // needed for now as it is done directly in width() below.
187         metrics_cache_.insert(c, ad);
188 }
189
190
191 int GuiFontMetrics::width(char_type c) const
192 {
193         if (smallcaps_shape_)
194                 return smallcapsWidth(c);
195
196         if (!width_cache_.contains(c)) {
197                 if (is_utf16(c))
198                         width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
199                 else
200                         width_cache_.insert(c, metrics_.width(toqstr(docstring(1,c))));
201         }
202
203         return width_cache_.value(c);
204 }
205
206
207 int GuiFontMetrics::ascent(char_type c) const
208 {
209         if (!metrics_cache_.contains(c))
210                 fillMetricsCache(c);
211
212         return metrics_cache_.value(c).ascent;
213 }
214
215
216 int GuiFontMetrics::descent(char_type c) const
217 {
218         if (!metrics_cache_.contains(c))
219                 fillMetricsCache(c);
220
221         return metrics_cache_.value(c).descent;
222 }
223
224 } // frontend
225 } // lyx