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