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.
9 * Full author contact details are available in file CREDITS.
14 #include "frontends/font_metrics.h"
15 #include "frontends/lyx_gui.h"
17 #include "qfont_loader.h"
25 namespace font_metrics {
27 int maxAscent(LyXFont const & f)
29 if (!lyx_gui::use_gui)
31 return fontloader.metrics(f).ascent();
35 int maxDescent(LyXFont const & f)
37 if (!lyx_gui::use_gui)
39 // We add 1 as the value returned by QT is different than X
40 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
41 return fontloader.metrics(f).descent() + 1;
45 int ascent(char c, LyXFont const & f)
47 if (!lyx_gui::use_gui)
49 QRect const & r = fontloader.metrics(f).boundingRect(c);
50 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
51 // value by the height: (x, -y-height, width, height).
52 // Other versions return: (x, -y, width, height)
53 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
54 return -r.top() - r.height();
61 int descent(char c, LyXFont const & f)
63 if (!lyx_gui::use_gui)
65 QRect const & r = fontloader.metrics(f).boundingRect(c);
66 // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
67 // value by the height: (x, -y-height, width, height).
68 // Other versions return: (x, -y, width, height)
69 #if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
70 return r.bottom() + r.height() + 1;
72 return r.bottom() + 1;
77 int lbearing(char c, LyXFont const & f)
79 if (!lyx_gui::use_gui)
81 return fontloader.metrics(f).leftBearing(c);
85 int rbearing(char c, LyXFont const & f)
87 if (!lyx_gui::use_gui)
89 QFontMetrics const & m = fontloader.metrics(f);
91 // Qt rbearing is from the right edge of the char's width().
92 return m.width(c) - m.rightBearing(c);
96 Encoding const * fontencoding(LyXFont const & f)
98 Encoding const * encoding = f.language()->encoding();
100 encoding = encodings.symbol_encoding();
105 int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
107 if (!lyx_gui::use_gui)
109 // handle small caps ourselves ...
111 LyXFont smallfont = f;
112 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
114 QFontMetrics const & qm = fontloader.metrics(f);
115 QFontMetrics const & qsmallm = fontloader.metrics(smallfont);
117 Encoding const * encoding = fontencoding(f);
121 for (size_t i = 0; i < ls; ++i) {
122 QChar const c = QChar(encoding->ucs(s[i]));
123 QChar const uc = c.upper();
125 w += qsmallm.width(uc);
133 int width(char const * s, size_t ls, LyXFont const & f)
135 if (!lyx_gui::use_gui)
138 if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
139 return smallcapswidth(s, ls, f);
141 Encoding const * encoding = fontencoding(f);
142 QLFontInfo & fi = fontloader.fontinfo(f);
145 return fi.width(encoding->ucs(s[0]));
148 for (size_t i = 0; i < ls; ++i)
149 w += fi.width(encoding->ucs(s[i]));
155 int signedWidth(string const & s, LyXFont const & f)
158 return -width(s.substr(1, s.length() - 1), f);
164 void rectText(string const & str, LyXFont const & f,
165 int & w, int & ascent, int & descent)
167 QFontMetrics const & m = 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;
176 void buttonText(string const & str, LyXFont const & f,
177 int & w, int & ascent, int & descent)
179 QFontMetrics const & m = 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;
186 } // namespace font_metrics