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 "GuiApplication.h"
18 #include "FontLoader.h"
19 #include "qt_helpers.h"
23 #include "support/unicode.h"
33 int smallcapswidth(QString const & s, LyXFont const & f)
35 if (!lyx_gui::use_gui)
37 // handle small caps ourselves ...
39 LyXFont smallfont = f;
40 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
42 QFontMetrics const & qm = guiApp->guiFontLoader().metrics(f);
43 QFontMetrics const & qsmallm = guiApp->guiFontLoader().metrics(smallfont);
47 int const ls = s.size();
49 for (int i = 0; i < ls; ++i) {
50 QChar const & c = s[i];
51 QChar const uc = c.toUpper();
53 w += qsmallm.width(uc);
64 int font_metrics::maxAscent(LyXFont const & f)
66 if (!lyx_gui::use_gui)
68 return guiApp->guiFontLoader().metrics(f).ascent();
72 int font_metrics::maxDescent(LyXFont const & f)
74 if (!lyx_gui::use_gui)
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;
82 int font_metrics::ascent(char_type c, LyXFont const & f)
84 if (!lyx_gui::use_gui)
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();
98 int font_metrics::descent(char_type c, LyXFont const & f)
100 if (!lyx_gui::use_gui)
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;
109 return r.bottom() + 1;
114 int font_metrics::lbearing(char_type c, LyXFont const & f)
116 if (!lyx_gui::use_gui)
118 return guiApp->guiFontLoader().metrics(f).leftBearing(ucs4_to_qchar(c));
122 int font_metrics::rbearing(char_type c, LyXFont const & f)
124 if (!lyx_gui::use_gui)
126 QFontMetrics const & m = guiApp->guiFontLoader().metrics(f);
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);
134 int font_metrics::width(char_type const * s, size_t ls, LyXFont const & f)
136 if (!lyx_gui::use_gui)
139 QString ucs2 = ucs4_to_qstring(s, ls);
141 if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
142 return smallcapswidth(ucs2, f);
144 lyx::frontend::QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
147 return fi.width(ucs2[0].unicode());
150 for (unsigned int i = 0; i < ls; ++i)
151 w += fi.width(ucs2[i].unicode());
157 int font_metrics::signedWidth(docstring const & s, LyXFont const & f)
160 return -width(s.substr(1, s.length() - 1), f);
166 void font_metrics::rectText(docstring const & str, LyXFont const & f,
167 int & w, int & ascent, int & descent)
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;
178 void font_metrics::buttonText(docstring const & str, LyXFont const & f,
179 int & w, int & ascent, int & descent)
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;