]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_metrics.C
The initial merge of the Qt frontend, and the necessary compile fixes.
[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 // FIXME ?
26 extern qfont_loader * global_fontloader;
27  
28 namespace {
29         QFontMetrics const & metrics(LyXFont const & f) {
30                 return global_fontloader->metrics(f);
31         }
32 };
33
34  
35 namespace font_metrics {
36  
37 int maxAscent(LyXFont const & f)
38 {
39         return metrics(f).ascent();
40 }
41
42
43 int maxDescent(LyXFont const & f)
44 {
45         return metrics(f).descent();
46 }
47
48
49 int ascent(char c, LyXFont const & f)
50 {
51         // FIXME - must do ascent for char not maxascent
52         //QRect r = metrics(f).boundingRect(c);
53         //lyxerr << r.x() << "," << r.y() <<
54         //      " : " << r.width() << "," << r.height() << endl;
55         return metrics(f).ascent();
56 }
57
58
59 int descent(char, LyXFont const & f)
60 {
61         // FIXME
62         return metrics(f).descent();
63 }
64
65
66 int lbearing(char c, LyXFont const & f)
67 {
68         return metrics(f).leftBearing(c);
69 }
70
71
72 int rbearing(char c, LyXFont const & f)
73 {
74         return metrics(f).rightBearing(c);
75 }
76
77
78 int width(char const * s, size_t ls, LyXFont const & f)
79 {
80         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
81                 return metrics(f).width(s, ls);
82         }
83
84         // handle small caps ourselves ...
85  
86         LyXFont smallfont(f);
87         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
88
89         QFontMetrics qm = global_fontloader->metrics(f);
90         QFontMetrics qsmallm = global_fontloader->metrics(smallfont);
91
92         int w = 0;
93
94         for (size_t i = 0; i < ls; ++i) {
95                 char const c = uppercase(s[i]);
96                 if (c != s[i])
97                         w += qsmallm.width(&c, 1);
98                 else
99                         w += qm.width(&c, 1);
100         }
101         return w;
102 }
103
104
105 int signedWidth(string const & s, LyXFont const & f)
106 {
107         if (s[0] == '-')
108                 return -width(s.substr(1, s.length() - 1), f);
109         else
110                 return width(s, f);
111 }
112
113
114 void rectText(string const & str, LyXFont const & f,
115         int & w,
116         int & ascent, 
117         int & descent)
118 {
119         QFontMetrics const & metrics(metrics(f));
120  
121         w = width(str, f);
122         ascent = metrics.ascent();
123         descent = metrics.descent();
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 & metrics(metrics(f));
134  
135         static int const d = 3;
136         
137         w = width(str, f) + d * 2 + 2;
138         ascent = metrics.ascent() + d;
139         descent = metrics.descent() + d;
140 }
141
142 } // namespace font_metrics