2 * \file qfont_metrics.C
3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
14 #pragma implementation "frontends/font_metrics.h"
17 #include "support/lstrings.h"
18 #include "font_metrics.h"
19 #include "qfont_loader.h"
22 #include <qfontmetrics.h>
26 QFontMetrics const & metrics(LyXFont const & f) {
27 return fontloader.metrics(f);
32 namespace font_metrics {
34 int maxAscent(LyXFont const & f)
36 return metrics(f).ascent();
40 int maxDescent(LyXFont const & f)
42 return metrics(f).descent();
46 int ascent(char c, LyXFont const & f)
48 QRect r = metrics(f).boundingRect(c);
53 int descent(char c, LyXFont const & f)
55 QRect r = metrics(f).boundingRect(c);
56 return abs(r.bottom());
60 int lbearing(char c, LyXFont const & f)
62 lyxerr << "lb of " << c << " is " << metrics(f).leftBearing(c)
64 return metrics(f).leftBearing(c);
68 int rbearing(char c, LyXFont const & f)
70 QFontMetrics const & m(metrics(f));
72 // Qt rbearing is from the right edge of the char's width().
73 return (m.width(c) - m.rightBearing(c));
77 int width(char const * s, size_t ls, LyXFont const & f)
79 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
80 return metrics(f).width(s, ls);
83 // handle small caps ourselves ...
86 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
88 QFontMetrics qm = fontloader.metrics(f);
89 QFontMetrics qsmallm = fontloader.metrics(smallfont);
93 for (size_t i = 0; i < ls; ++i) {
94 char const c = uppercase(s[i]);
96 w += qsmallm.width(&c, 1);
104 int signedWidth(string const & s, LyXFont const & f)
107 return -width(s.substr(1, s.length() - 1), f);
113 void rectText(string const & str, LyXFont const & f,
118 QFontMetrics const & m(metrics(f));
120 static int const d = 2;
122 w = width(str, f) + d * 2 + 2;
123 ascent = m.ascent() + d;
124 descent = m.descent() + d;
129 void buttonText(string const & str, LyXFont const & f,
134 QFontMetrics const & m(metrics(f));
136 static int const d = 3;
138 w = width(str, f) + d * 2 + 2;
139 ascent = m.ascent() + d;
140 descent = m.descent() + d;
143 } // namespace font_metrics