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