]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_metrics.C
Minipage is no more (long live the box inset)
[lyx.git] / src / frontends / qt2 / qfont_metrics.C
1 /**
2  * \file qfont_metrics.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 "font_metrics.h"
15 #include "frontends/lyx_gui.h"
16 #include "qfont_loader.h"
17 #include "language.h"
18
19 using std::string;
20
21
22 namespace {
23
24 QFontMetrics const & metrics(LyXFont const & f)
25 {
26         return fontloader.metrics(f);
27 }
28
29
30 int charwidth(Uchar val, LyXFont const & f)
31 {
32         if (!lyx_gui::use_gui)
33                 return 1;
34         return fontloader.charwidth(f, val);
35 }
36
37 } // namespace anon
38
39
40 namespace font_metrics {
41
42 int maxAscent(LyXFont const & f)
43 {
44         if (!lyx_gui::use_gui)
45                 return 1;
46         return metrics(f).ascent();
47 }
48
49
50 int maxDescent(LyXFont const & f)
51 {
52         if (!lyx_gui::use_gui)
53                 return 1;
54         // We add 1 as the value returned by QT is different than X
55         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
56         return metrics(f).descent() + 1;
57 }
58
59
60 int ascent(char c, LyXFont const & f)
61 {
62         if (!lyx_gui::use_gui)
63                 return 1;
64         QRect const & r = metrics(f).boundingRect(c);
65         return -r.top();
66 }
67
68
69 int descent(char c, LyXFont const & f)
70 {
71         if (!lyx_gui::use_gui)
72                 return 1;
73         QRect const & r = metrics(f).boundingRect(c);
74         return r.bottom()+1;
75 }
76
77
78 int lbearing(char c, LyXFont const & f)
79 {
80         if (!lyx_gui::use_gui)
81                 return 1;
82         return metrics(f).leftBearing(c);
83 }
84
85
86 int rbearing(char c, LyXFont const & f)
87 {
88         if (!lyx_gui::use_gui)
89                 return 1;
90         QFontMetrics const & m(metrics(f));
91
92         // Qt rbearing is from the right edge of the char's width().
93         return (m.width(c) - m.rightBearing(c));
94 }
95
96
97 Encoding const * fontencoding(LyXFont const & f)
98 {
99         Encoding const * encoding = f.language()->encoding();
100         if (f.isSymbolFont())
101                 encoding = encodings.symbol_encoding();
102         return encoding;
103 }
104
105
106 int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
107 {
108         if (!lyx_gui::use_gui)
109                 return 1;
110         // handle small caps ourselves ...
111
112         LyXFont smallfont(f);
113         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
114
115         QFontMetrics const & qm = fontloader.metrics(f);
116         QFontMetrics const & qsmallm = fontloader.metrics(smallfont);
117
118         Encoding const * encoding(fontencoding(f));
119
120         int w = 0;
121
122         for (size_t i = 0; i < ls; ++i) {
123                 QChar const c = QChar(encoding->ucs(s[i]));
124                 QChar const uc = c.upper();
125                 if (c != uc)
126                         w += qsmallm.width(uc);
127                 else
128                         w += qm.width(c);
129         }
130         return w;
131 }
132
133
134 int width(char const * s, size_t ls, LyXFont const & f)
135 {
136         if (!lyx_gui::use_gui)
137                 return ls;
138
139         if (f.realShape() == LyXFont::SMALLCAPS_SHAPE) {
140                 return smallcapswidth(s, ls, f);
141         }
142
143         Encoding const * encoding(fontencoding(f));
144
145         if (ls == 1) {
146                 return charwidth(encoding->ucs(s[0]), f);
147         }
148
149         int w = 0;
150
151         for (size_t i = 0; i < ls; ++i) {
152                 w += charwidth(encoding->ucs(s[i]), f);
153         }
154
155         return w;
156 }
157
158
159 int signedWidth(string const & s, LyXFont const & f)
160 {
161         if (s[0] == '-')
162                 return -width(s.substr(1, s.length() - 1), f);
163         else
164                 return width(s, f);
165 }
166
167
168 void rectText(string const & str, LyXFont const & f,
169         int & w,
170         int & ascent,
171         int & descent)
172 {
173         QFontMetrics const & m(metrics(f));
174
175         static int const d = 2;
176
177         w = width(str, f) + d * 2 + 2;
178         ascent = m.ascent() + d;
179         descent = m.descent() + d;
180 }
181
182
183
184 void buttonText(string const & str, LyXFont const & f,
185         int & w,
186         int & ascent,
187         int & descent)
188 {
189         QFontMetrics const & m(metrics(f));
190
191         static int const d = 3;
192
193         w = width(str, f) + d * 2 + 2;
194         ascent = m.ascent() + d;
195         descent = m.descent() + d;
196 }
197
198 } // namespace font_metrics