]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_metrics.C
Missing files, get keyboard partly working
[lyx.git] / src / frontends / qt2 / qfont_metrics.C
1 /**
2  * \file qfont_metrics.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12
13 #ifdef __GNUG__
14 #pragma implementation "frontends/font_metrics.h"
15 #endif
16
17 #include "support/lstrings.h"
18 #include "font_metrics.h"
19 #include "qfont_loader.h"
20 #include "debug.h"
21
22 #include <qfontmetrics.h>
23 #include <qfont.h>
24
25 namespace {
26         QFontMetrics const & metrics(LyXFont const & f) {
27                 return fontloader.metrics(f);
28         }
29 };
30
31  
32 namespace font_metrics {
33  
34 int maxAscent(LyXFont const & f)
35 {
36         return metrics(f).ascent();
37 }
38
39
40 int maxDescent(LyXFont const & f)
41 {
42         return metrics(f).descent();
43 }
44
45
46 int ascent(char c, LyXFont const & f)
47 {
48         // FIXME - must do ascent for char not maxascent
49         //QRect r = metrics(f).boundingRect(c);
50         //lyxerr << r.x() << "," << r.y() <<
51         //      " : " << r.width() << "," << r.height() << endl;
52         return metrics(f).ascent();
53 }
54
55
56 int descent(char, LyXFont const & f)
57 {
58         // FIXME
59         return metrics(f).descent();
60 }
61
62
63 int lbearing(char c, LyXFont const & f)
64 {
65         return metrics(f).leftBearing(c);
66 }
67
68
69 int rbearing(char c, LyXFont const & f)
70 {
71         return metrics(f).rightBearing(c);
72 }
73
74
75 int width(char const * s, size_t ls, LyXFont const & f)
76 {
77         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
78                 return metrics(f).width(s, ls);
79         }
80
81         // handle small caps ourselves ...
82  
83         LyXFont smallfont(f);
84         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
85
86         QFontMetrics qm = fontloader.metrics(f);
87         QFontMetrics qsmallm = fontloader.metrics(smallfont);
88
89         int w = 0;
90
91         for (size_t i = 0; i < ls; ++i) {
92                 char const c = uppercase(s[i]);
93                 if (c != s[i])
94                         w += qsmallm.width(&c, 1);
95                 else
96                         w += qm.width(&c, 1);
97         }
98         return w;
99 }
100
101
102 int signedWidth(string const & s, LyXFont const & f)
103 {
104         if (s[0] == '-')
105                 return -width(s.substr(1, s.length() - 1), f);
106         else
107                 return width(s, f);
108 }
109
110
111 void rectText(string const & str, LyXFont const & f,
112         int & w,
113         int & ascent, 
114         int & descent)
115 {
116         QFontMetrics const & metrics(metrics(f));
117  
118         w = width(str, f);
119         ascent = metrics.ascent();
120         descent = metrics.descent();
121 }
122
123
124
125 void buttonText(string const & str, LyXFont const & f,
126         int & w, 
127         int & ascent, 
128         int & descent)
129 {
130         QFontMetrics const & metrics(metrics(f));
131  
132         static int const d = 3;
133         
134         w = width(str, f) + d * 2 + 2;
135         ascent = metrics.ascent() + d;
136         descent = metrics.descent() + d;
137 }
138
139 } // namespace font_metrics