]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qfont_metrics.C
* MSVC Compilation fix: #warning is not supported.
[lyx.git] / src / frontends / qt4 / 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 #include "frontends/font_metrics.h"
15 #include "frontends/lyx_gui.h"
16
17 #include "FontLoader.h"
18 #include "Application.h"
19
20 #include "language.h"
21
22
23 using std::string;
24
25
26 namespace font_metrics {
27
28 int maxAscent(LyXFont const & f)
29 {
30         if (!lyx_gui::use_gui)
31                 return 1;
32         return theApp->fontLoader().metrics(f).ascent();
33 }
34
35
36 int maxDescent(LyXFont const & f)
37 {
38         if (!lyx_gui::use_gui)
39                 return 1;
40         // We add 1 as the value returned by QT is different than X
41         // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
42         return theApp->fontLoader().metrics(f).descent() + 1;
43 }
44
45
46 int ascent(char c, LyXFont const & f)
47 {
48         if (!lyx_gui::use_gui)
49                 return 1;
50         QRect const & r = theApp->fontLoader().metrics(f).boundingRect(c);
51         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
52         // value by the height: (x, -y-height, width, height).
53         // Other versions return: (x, -y, width, height)
54 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
55         return -r.top() - r.height();
56 #else
57         return -r.top();
58 #endif
59 }
60
61
62 int descent(char c, LyXFont const & f)
63 {
64         if (!lyx_gui::use_gui)
65                 return 1;
66         QRect const & r = theApp->fontLoader().metrics(f).boundingRect(c);
67         // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
68         // value by the height: (x, -y-height, width, height).
69         // Other versions return: (x, -y, width, height)
70 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
71         return r.bottom() + r.height() + 1;
72 #else
73         return r.bottom() + 1;
74 #endif
75 }
76
77
78 int lbearing(char c, LyXFont const & f)
79 {
80         if (!lyx_gui::use_gui)
81                 return 1;
82         return theApp->fontLoader().metrics(f).leftBearing(c);
83 }
84
85
86 int rbearing(char c, LyXFont const & f)
87 {
88         if (!lyx_gui::use_gui)
89                 return 1;
90         QFontMetrics const & m = theApp->fontLoader().metrics(f);
91
92         // Qt rbearing is from the right edge of the char's width().
93         return m.width(c) - m.rightBearing(c);
94 }
95
96
97 Encoding const * fontencoding(LyXFont const & f)
98 {
99         Encoding const * encoding = f.language()->encoding();
100         if (f.isSymbolFont())
101                 encoding = encodings.symbol_encoding();
102         return encoding;
103 }
104
105
106 int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
107 {
108         if (!lyx_gui::use_gui)
109                 return 1;
110         // handle small caps ourselves ...
111
112         LyXFont smallfont = f;
113         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
114
115         QFontMetrics const & qm = theApp->fontLoader().metrics(f);
116         QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
117
118         Encoding const * encoding = fontencoding(f);
119
120         int w = 0;
121
122         for (size_t i = 0; i < ls; ++i) {
123                 QChar const c = QChar(encoding->ucs(s[i]));
124                 QChar const uc = c.upper();
125                 if (c != uc)
126                         w += qsmallm.width(uc);
127                 else
128                         w += qm.width(c);
129         }
130         return w;
131 }
132
133
134 int width(char const * s, size_t ls, LyXFont const & f)
135 {
136         if (!lyx_gui::use_gui)
137                 return ls;
138
139         if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
140                 return smallcapswidth(s, ls, f);
141
142         Encoding const * encoding = fontencoding(f);
143         QLFontInfo & fi = theApp->fontLoader().fontinfo(f);
144
145         if (ls == 1)
146                 return fi.width(encoding->ucs(s[0]));
147
148         int w = 0;
149         for (size_t i = 0; i < ls; ++i)
150                 w += fi.width(encoding->ucs(s[i]));
151
152         return w;
153 }
154
155
156 int signedWidth(string const & s, LyXFont const & f)
157 {
158         if (s[0] == '-')
159                 return -width(s.substr(1, s.length() - 1), f);
160         else
161                 return width(s, f);
162 }
163
164
165 void rectText(string const & str, LyXFont const & f,
166         int & w, int & ascent, int & descent)
167 {
168         QFontMetrics const & m = theApp->fontLoader().metrics(f);
169         static int const d = 2;
170         w = width(str, f) + d * 2 + 2;
171         ascent = m.ascent() + d;
172         descent = m.descent() + d;
173 }
174
175
176
177 void buttonText(string const & str, LyXFont const & f,
178         int & w, int & ascent, int & descent)
179 {
180         QFontMetrics const & m = theApp->fontLoader().metrics(f);
181         static int const d = 3;
182         w = width(str, f) + d * 2 + 2;
183         ascent = m.ascent() + d;
184         descent = m.descent() + d;
185 }
186
187 } // namespace font_metrics