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
15 #pragma implementation "frontends/font_metrics.h"
18 #include "support/lstrings.h"
19 #include "font_metrics.h"
20 #include "qfont_loader.h"
25 #include <qfontmetrics.h>
31 QFontMetrics const & metrics(LyXFont const & f)
33 return fontloader.metrics(f);
39 namespace font_metrics {
41 int maxAscent(LyXFont const & f)
43 return metrics(f).ascent();
47 int maxDescent(LyXFont const & f)
49 return metrics(f).descent()+1;
50 // We add 1 as the value returned by QT is different than X
51 // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
55 int ascent(char c, LyXFont const & f)
57 QRect r = metrics(f).boundingRect(c);
62 int descent(char c, LyXFont const & f)
64 QRect r = metrics(f).boundingRect(c);
69 int lbearing(char c, LyXFont const & f)
71 return metrics(f).leftBearing(c);
75 int rbearing(char c, LyXFont const & f)
77 QFontMetrics const & m(metrics(f));
79 // Qt rbearing is from the right edge of the char's width().
80 return (m.width(c) - m.rightBearing(c));
84 int width(char const * s, size_t ls, LyXFont const & f)
86 Encoding const * encoding = f.language()->encoding();
88 encoding = encodings.symbol_encoding();
93 for (size_t i = 0; i < ls; ++i)
94 str[i] = QChar(encoding->ucs(s[i]));
96 for (size_t i = 0; i < ls; ++i)
97 str += QChar(encoding->ucs(s[i]));
100 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
101 return metrics(f).width(str);
104 // handle small caps ourselves ...
106 LyXFont smallfont(f);
107 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
109 QFontMetrics qm = fontloader.metrics(f);
110 QFontMetrics qsmallm = fontloader.metrics(smallfont);
114 for (size_t i = 0; i < ls; ++i) {
115 QChar const c = str[i].upper();
117 w += qsmallm.width(c);
125 int signedWidth(string const & s, LyXFont const & f)
128 return -width(s.substr(1, s.length() - 1), f);
134 void rectText(string const & str, LyXFont const & f,
139 QFontMetrics const & m(metrics(f));
141 static int const d = 2;
143 w = width(str, f) + d * 2 + 2;
144 ascent = m.ascent() + d;
145 descent = m.descent() + d;
150 void buttonText(string const & str, LyXFont const & f,
155 QFontMetrics const & m(metrics(f));
157 static int const d = 3;
159 w = width(str, f) + d * 2 + 2;
160 ascent = m.ascent() + d;
161 descent = m.descent() + d;
164 } // namespace font_metrics