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