]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_metrics.C
fix rbearing
[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         QRect r = metrics(f).boundingRect(c);
49         return abs(r.top());
50 }
51
52
53 int descent(char c, LyXFont const & f)
54 {
55         QRect r = metrics(f).boundingRect(c);
56         return abs(r.bottom());
57 }
58
59
60 int lbearing(char c, LyXFont const & f)
61 {
62         lyxerr << "lb of " << c << " is " << metrics(f).leftBearing(c) << endl; 
63         return metrics(f).leftBearing(c);
64 }
65
66
67 int rbearing(char c, LyXFont const & f)
68 {
69         QFontMetrics const & m(metrics(f));
70  
71         // Qt rbearing is from the right edge of the char's width(). 
72         return (m.width(c) - m.rightBearing(c));
73 }
74
75
76 int width(char const * s, size_t ls, LyXFont const & f)
77 {
78         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
79                 return metrics(f).width(s, ls);
80         }
81
82         // handle small caps ourselves ...
83  
84         LyXFont smallfont(f);
85         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
86
87         QFontMetrics qm = fontloader.metrics(f);
88         QFontMetrics qsmallm = fontloader.metrics(smallfont);
89
90         int w = 0;
91
92         for (size_t i = 0; i < ls; ++i) {
93                 char const c = uppercase(s[i]);
94                 if (c != s[i])
95                         w += qsmallm.width(&c, 1);
96                 else
97                         w += qm.width(&c, 1);
98         }
99         return w;
100 }
101
102
103 int signedWidth(string const & s, LyXFont const & f)
104 {
105         if (s[0] == '-')
106                 return -width(s.substr(1, s.length() - 1), f);
107         else
108                 return width(s, f);
109 }
110
111
112 void rectText(string const & str, LyXFont const & f,
113         int & w,
114         int & ascent, 
115         int & descent)
116 {
117         QFontMetrics const & m(metrics(f));
118  
119         static int const d = 2;
120  
121         w = width(str, f) + d * 2 + 2;
122         ascent = m.ascent() + d;
123         descent = m.descent() + d;
124 }
125
126
127
128 void buttonText(string const & str, LyXFont const & f,
129         int & w, 
130         int & ascent, 
131         int & descent)
132 {
133         QFontMetrics const & m(metrics(f));
134  
135         static int const d = 3;
136         
137         w = width(str, f) + d * 2 + 2;
138         ascent = m.ascent() + d;
139         descent = m.descent() + d;
140 }
141
142 } // namespace font_metrics