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 "FontLoader.h"
18 #include "Application.h"
26 namespace font_metrics {
28 int maxAscent(LyXFont const & f)
30 if (!lyx_gui::use_gui)
32 return theApp->fontLoader().metrics(f).ascent();
36 int maxDescent(LyXFont const & f)
38 if (!lyx_gui::use_gui)
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;
46 int ascent(char c, LyXFont const & f)
48 if (!lyx_gui::use_gui)
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();
62 int descent(char c, LyXFont const & f)
64 if (!lyx_gui::use_gui)
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;
73 return r.bottom() + 1;
78 int lbearing(char c, LyXFont const & f)
80 if (!lyx_gui::use_gui)
82 return theApp->fontLoader().metrics(f).leftBearing(c);
86 int rbearing(char c, LyXFont const & f)
88 if (!lyx_gui::use_gui)
90 QFontMetrics const & m = theApp->fontLoader().metrics(f);
92 // Qt rbearing is from the right edge of the char's width().
93 return m.width(c) - m.rightBearing(c);
97 Encoding const * fontencoding(LyXFont const & f)
99 Encoding const * encoding = f.language()->encoding();
100 if (f.isSymbolFont())
101 encoding = encodings.symbol_encoding();
106 int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
108 if (!lyx_gui::use_gui)
110 // handle small caps ourselves ...
112 LyXFont smallfont = f;
113 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
115 QFontMetrics const & qm = theApp->fontLoader().metrics(f);
116 QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
118 Encoding const * encoding = fontencoding(f);
122 for (size_t i = 0; i < ls; ++i) {
123 QChar const c = QChar(encoding->ucs(s[i]));
124 QChar const uc = c.upper();
126 w += qsmallm.width(uc);
134 int width(char const * s, size_t ls, LyXFont const & f)
136 if (!lyx_gui::use_gui)
139 if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
140 return smallcapswidth(s, ls, f);
142 Encoding const * encoding = fontencoding(f);
143 QLFontInfo & fi = theApp->fontLoader().fontinfo(f);
146 return fi.width(encoding->ucs(s[0]));
149 for (size_t i = 0; i < ls; ++i)
150 w += fi.width(encoding->ucs(s[i]));
156 int signedWidth(string const & s, LyXFont const & f)
159 return -width(s.substr(1, s.length() - 1), f);
165 void rectText(string const & str, LyXFont const & f,
166 int & w, int & ascent, int & descent)
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;
177 void buttonText(string const & str, LyXFont const & f,
178 int & w, int & ascent, int & descent)
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;
187 } // namespace font_metrics