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