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 "Application.h"
18 #include "FontLoader.h"
22 #include "support/unicode.h"
32 int smallcapswidth(unsigned short const * s, size_t ls, LyXFont const & f)
34 if (!lyx_gui::use_gui)
36 // handle small caps ourselves ...
38 LyXFont smallfont = f;
39 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
41 QFontMetrics const & qm = theApp->fontLoader().metrics(f);
42 QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
46 for (size_t i = 0; i < ls; ++i) {
48 QChar const uc = c.upper();
50 w += qsmallm.width(uc);
61 int font_metrics::maxAscent(LyXFont const & f)
63 if (!lyx_gui::use_gui)
65 return theApp->fontLoader().metrics(f).ascent();
69 int font_metrics::maxDescent(LyXFont const & f)
71 if (!lyx_gui::use_gui)
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;
79 int font_metrics::ascent(char_type c, LyXFont const & f)
81 if (!lyx_gui::use_gui)
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();
95 int font_metrics::descent(char_type c, LyXFont const & f)
97 if (!lyx_gui::use_gui)
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;
106 return r.bottom() + 1;
111 int font_metrics::lbearing(char_type c, LyXFont const & f)
113 if (!lyx_gui::use_gui)
115 return theApp->fontLoader().metrics(f).leftBearing(ucs4_to_ucs2(c));
119 int font_metrics::rbearing(char_type c, LyXFont const & f)
121 if (!lyx_gui::use_gui)
123 QFontMetrics const & m = theApp->fontLoader().metrics(f);
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);
131 int font_metrics::width(char_type const * s, size_t ls, LyXFont const & f)
133 if (!lyx_gui::use_gui)
136 std::vector<unsigned short> ucs2 = ucs4_to_ucs2(s, ls);
139 if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
140 return smallcapswidth(&ucs2[0], ls, f);
142 QLFontInfo & fi = theApp->fontLoader().fontinfo(f);
145 return fi.width(ucs2[0]);
148 for (size_t i = 0; i < ls; ++i)
149 w += fi.width(ucs2[i]);
155 int font_metrics::signedWidth(docstring const & s, LyXFont const & f)
158 return -width(s.substr(1, s.length() - 1), f);
164 void font_metrics::rectText(docstring const & str, LyXFont const & f,
165 int & w, int & ascent, int & descent)
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;
176 void font_metrics::buttonText(docstring const & str, LyXFont const & f,
177 int & w, int & ascent, int & descent)
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;