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/lyx_gui.h"
15 #include "support/lstrings.h"
16 #include "font_metrics.h"
17 #include "qfont_loader.h"
22 #include <qfontmetrics.h>
27 QFontMetrics const & metrics(LyXFont const & f)
29 return fontloader.metrics(f);
33 int charwidth(Uchar val, LyXFont const & f)
35 if (!lyx_gui::use_gui)
37 return fontloader.charwidth(f, val);
43 namespace font_metrics {
45 int maxAscent(LyXFont const & f)
47 if (!lyx_gui::use_gui)
49 return metrics(f).ascent();
53 int maxDescent(LyXFont const & f)
55 if (!lyx_gui::use_gui)
57 // We add 1 as the value returned by QT is different than X
58 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
59 return metrics(f).descent() + 1;
63 int ascent(char c, LyXFont const & f)
65 if (!lyx_gui::use_gui)
67 QRect const & r = metrics(f).boundingRect(c);
72 int descent(char c, LyXFont const & f)
74 if (!lyx_gui::use_gui)
76 QRect const & r = metrics(f).boundingRect(c);
81 int lbearing(char c, LyXFont const & f)
83 if (!lyx_gui::use_gui)
85 return metrics(f).leftBearing(c);
89 int rbearing(char c, LyXFont const & f)
91 if (!lyx_gui::use_gui)
93 QFontMetrics const & m(metrics(f));
95 // Qt rbearing is from the right edge of the char's width().
96 return (m.width(c) - m.rightBearing(c));
100 Encoding const * fontencoding(LyXFont const & f)
102 Encoding const * encoding = f.language()->encoding();
103 if (f.isSymbolFont())
104 encoding = encodings.symbol_encoding();
109 int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
111 if (!lyx_gui::use_gui)
113 // handle small caps ourselves ...
115 LyXFont smallfont(f);
116 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
118 QFontMetrics const & qm = fontloader.metrics(f);
119 QFontMetrics const & qsmallm = fontloader.metrics(smallfont);
121 Encoding const * encoding(fontencoding(f));
125 for (size_t i = 0; i < ls; ++i) {
126 QChar const c = QChar(encoding->ucs(s[i]));
127 QChar const uc = c.upper();
129 w += qsmallm.width(uc);
137 int width(char const * s, size_t ls, LyXFont const & f)
139 if (!lyx_gui::use_gui)
142 if (f.realShape() == LyXFont::SMALLCAPS_SHAPE) {
143 return smallcapswidth(s, ls, f);
146 Encoding const * encoding(fontencoding(f));
149 return charwidth(encoding->ucs(s[0]), f);
154 for (size_t i = 0; i < ls; ++i) {
155 w += charwidth(encoding->ucs(s[i]), f);
162 int signedWidth(string const & s, LyXFont const & f)
165 return -width(s.substr(1, s.length() - 1), f);
171 void rectText(string const & str, LyXFont const & f,
176 QFontMetrics const & m(metrics(f));
178 static int const d = 2;
180 w = width(str, f) + d * 2 + 2;
181 ascent = m.ascent() + d;
182 descent = m.descent() + d;
187 void buttonText(string const & str, LyXFont const & f,
192 QFontMetrics const & m(metrics(f));
194 static int const d = 3;
196 w = width(str, f) + d * 2 + 2;
197 ascent = m.ascent() + d;
198 descent = m.descent() + d;
201 } // namespace font_metrics