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