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