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 "font_metrics.h"
15 #include "frontends/lyx_gui.h"
16 #include "qfont_loader.h"
24 QFontMetrics const & metrics(LyXFont const & f)
26 return fontloader.metrics(f);
30 int charwidth(Uchar val, LyXFont const & f)
32 if (!lyx_gui::use_gui)
34 return fontloader.charwidth(f, val);
40 namespace font_metrics {
42 int maxAscent(LyXFont const & f)
44 if (!lyx_gui::use_gui)
46 return metrics(f).ascent();
50 int maxDescent(LyXFont const & f)
52 if (!lyx_gui::use_gui)
54 // We add 1 as the value returned by QT is different than X
55 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
56 return metrics(f).descent() + 1;
60 int ascent(char c, LyXFont const & f)
62 if (!lyx_gui::use_gui)
64 QRect const & r = metrics(f).boundingRect(c);
69 int descent(char c, LyXFont const & f)
71 if (!lyx_gui::use_gui)
73 QRect const & r = metrics(f).boundingRect(c);
78 int lbearing(char c, LyXFont const & f)
80 if (!lyx_gui::use_gui)
82 return metrics(f).leftBearing(c);
86 int rbearing(char c, LyXFont const & f)
88 if (!lyx_gui::use_gui)
90 QFontMetrics const & m(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 = fontloader.metrics(f);
116 QFontMetrics const & qsmallm = 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);
143 Encoding const * encoding(fontencoding(f));
146 return charwidth(encoding->ucs(s[0]), f);
151 for (size_t i = 0; i < ls; ++i) {
152 w += charwidth(encoding->ucs(s[i]), f);
159 int signedWidth(string const & s, LyXFont const & f)
162 return -width(s.substr(1, s.length() - 1), f);
168 void rectText(string const & str, LyXFont const & f,
173 QFontMetrics const & m(metrics(f));
175 static int const d = 2;
177 w = width(str, f) + d * 2 + 2;
178 ascent = m.ascent() + d;
179 descent = m.descent() + d;
184 void buttonText(string const & str, LyXFont const & f,
189 QFontMetrics const & m(metrics(f));
191 static int const d = 3;
193 w = width(str, f) + d * 2 + 2;
194 ascent = m.ascent() + d;
195 descent = m.descent() + d;
198 } // namespace font_metrics