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