]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_metrics.C
Lots and lots of little trivial bits.
[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
24 #include <qfontmetrics.h>
25 #include <qfont.h>
26
27 namespace {
28         QFontMetrics const & metrics(LyXFont const & f) {
29                 return fontloader.metrics(f);
30         }
31 }
32
33
34 namespace font_metrics {
35
36 int maxAscent(LyXFont const & f)
37 {
38         return metrics(f).ascent();
39 }
40
41
42 int maxDescent(LyXFont const & f)
43 {
44         return metrics(f).descent();
45 }
46
47
48 int ascent(char c, LyXFont const & f)
49 {
50         QRect r = metrics(f).boundingRect(c);
51         return abs(r.top());
52 }
53
54
55 int descent(char c, LyXFont const & f)
56 {
57         QRect r = metrics(f).boundingRect(c);
58         return abs(r.bottom());
59 }
60
61
62 int lbearing(char c, LyXFont const & f)
63 {
64         lyxerr << "lb of " << c << " is " << metrics(f).leftBearing(c)
65                << std::endl;
66         return metrics(f).leftBearing(c);
67 }
68
69
70 int rbearing(char c, LyXFont const & f)
71 {
72         QFontMetrics const & m(metrics(f));
73
74         // Qt rbearing is from the right edge of the char's width().
75         return (m.width(c) - m.rightBearing(c));
76 }
77
78
79 int width(char const * s, size_t ls, LyXFont const & f)
80 {
81         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
82                 return metrics(f).width(s, ls);
83         }
84
85         // handle small caps ourselves ...
86
87         LyXFont smallfont(f);
88         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
89
90         QFontMetrics qm = fontloader.metrics(f);
91         QFontMetrics qsmallm = fontloader.metrics(smallfont);
92
93         int w = 0;
94
95         for (size_t i = 0; i < ls; ++i) {
96                 char const c = uppercase(s[i]);
97                 if (c != s[i])
98                         w += qsmallm.width(&c, 1);
99                 else
100                         w += qm.width(&c, 1);
101         }
102         return w;
103 }
104
105
106 int signedWidth(string const & s, LyXFont const & f)
107 {
108         if (s[0] == '-')
109                 return -width(s.substr(1, s.length() - 1), f);
110         else
111                 return width(s, f);
112 }
113
114
115 void rectText(string const & str, LyXFont const & f,
116         int & w,
117         int & ascent,
118         int & descent)
119 {
120         QFontMetrics const & m(metrics(f));
121
122         static int const d = 2;
123
124         w = width(str, f) + d * 2 + 2;
125         ascent = m.ascent() + d;
126         descent = m.descent() + d;
127 }
128
129
130
131 void buttonText(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 = 3;
139
140         w = width(str, f) + d * 2 + 2;
141         ascent = m.ascent() + d;
142         descent = m.descent() + d;
143 }
144
145 } // namespace font_metrics