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
16 #pragma implementation "frontends/font_metrics.h"
19 #include "support/lstrings.h"
20 #include "font_metrics.h"
21 #include "qfont_loader.h"
26 #include <qfontmetrics.h>
30 QFontMetrics const & metrics(LyXFont const & f) {
31 return fontloader.metrics(f);
36 namespace font_metrics {
38 int maxAscent(LyXFont const & f)
40 return metrics(f).ascent();
44 int maxDescent(LyXFont const & f)
46 return metrics(f).descent();
50 int ascent(char c, LyXFont const & f)
52 QRect r = metrics(f).boundingRect(c);
57 int descent(char c, LyXFont const & f)
59 QRect r = metrics(f).boundingRect(c);
60 return abs(r.bottom());
64 int lbearing(char c, LyXFont const & f)
66 lyxerr << "lb of " << c << " is " << metrics(f).leftBearing(c)
68 return metrics(f).leftBearing(c);
72 int rbearing(char c, LyXFont const & f)
74 QFontMetrics const & m(metrics(f));
76 // Qt rbearing is from the right edge of the char's width().
77 return (m.width(c) - m.rightBearing(c));
81 int width(char const * s, size_t ls, LyXFont const & f)
83 Encoding const * encoding = f.language()->encoding();
85 encoding = encodings.symbol_encoding();
88 #if QT_VERSION >= 0x030000
90 for (size_t i = 0; i < ls; ++i)
91 str[i] = QChar(encoding->ucs(s[i]));
93 for (size_t i = 0; i < ls; ++i)
94 str += QChar(encoding->ucs(s[i]));
97 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
98 return metrics(f).width(str);
101 // handle small caps ourselves ...
103 LyXFont smallfont(f);
104 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
106 QFontMetrics qm = fontloader.metrics(f);
107 QFontMetrics qsmallm = fontloader.metrics(smallfont);
111 for (size_t i = 0; i < ls; ++i) {
112 QChar const c = str[i].upper();
114 w += qsmallm.width(c);
122 int signedWidth(string const & s, LyXFont const & f)
125 return -width(s.substr(1, s.length() - 1), f);
131 void rectText(string const & str, LyXFont const & f,
136 QFontMetrics const & m(metrics(f));
138 static int const d = 2;
140 w = width(str, f) + d * 2 + 2;
141 ascent = m.ascent() + d;
142 descent = m.descent() + d;
147 void buttonText(string const & str, LyXFont const & f,
152 QFontMetrics const & m(metrics(f));
154 static int const d = 3;
156 w = width(str, f) + d * 2 + 2;
157 ascent = m.ascent() + d;
158 descent = m.descent() + d;
161 } // namespace font_metrics