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