2002-06-19 03:38:44 +00:00
|
|
|
/**
|
|
|
|
* \file qfont_metrics.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-06-19 03:38:44 +00:00
|
|
|
*
|
|
|
|
* \author unknown
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-06-19 03:38:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "font_metrics.h"
|
2003-09-05 16:18:57 +00:00
|
|
|
#include "frontends/lyx_gui.h"
|
2002-06-19 03:38:44 +00:00
|
|
|
#include "qfont_loader.h"
|
2002-10-15 15:13:49 +00:00
|
|
|
#include "language.h"
|
2002-06-19 03:38:44 +00:00
|
|
|
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
namespace {
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
QFontMetrics const & metrics(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return fontloader.metrics(f);
|
2002-06-21 16:40:54 +00:00
|
|
|
}
|
2002-06-19 03:38:44 +00:00
|
|
|
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
int charwidth(Uchar val, LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2003-02-25 13:35:26 +00:00
|
|
|
return fontloader.charwidth(f, val);
|
2002-12-11 00:22:37 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
namespace font_metrics {
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
int maxAscent(LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-06-19 03:38:44 +00:00
|
|
|
return metrics(f).ascent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int maxDescent(LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-10-21 15:51:06 +00:00
|
|
|
// We add 1 as the value returned by QT is different than X
|
|
|
|
// See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
|
2002-12-11 00:22:37 +00:00
|
|
|
return metrics(f).descent() + 1;
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-07 22:25:03 +00:00
|
|
|
int ascent(char c, LyXFont const & f)
|
2002-06-19 03:38:44 +00:00
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-12-11 00:22:37 +00:00
|
|
|
QRect const & r = metrics(f).boundingRect(c);
|
2002-10-21 15:51:06 +00:00
|
|
|
return -r.top();
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-07 22:25:03 +00:00
|
|
|
int descent(char c, LyXFont const & f)
|
2002-06-19 03:38:44 +00:00
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-12-11 00:22:37 +00:00
|
|
|
QRect const & r = metrics(f).boundingRect(c);
|
2002-10-21 15:51:06 +00:00
|
|
|
return r.bottom()+1;
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lbearing(char c, LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-06-19 03:38:44 +00:00
|
|
|
return metrics(f).leftBearing(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int rbearing(char c, LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-09-14 04:50:38 +00:00
|
|
|
QFontMetrics const & m(metrics(f));
|
2002-09-23 16:16:08 +00:00
|
|
|
|
|
|
|
// Qt rbearing is from the right edge of the char's width().
|
2002-09-14 04:50:38 +00:00
|
|
|
return (m.width(c) - m.rightBearing(c));
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
Encoding const * fontencoding(LyXFont const & f)
|
2002-06-19 03:38:44 +00:00
|
|
|
{
|
2002-10-15 15:13:49 +00:00
|
|
|
Encoding const * encoding = f.language()->encoding();
|
|
|
|
if (f.isSymbolFont())
|
|
|
|
encoding = encodings.symbol_encoding();
|
2002-12-11 00:22:37 +00:00
|
|
|
return encoding;
|
|
|
|
}
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-10-15 15:13:49 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
int smallcapswidth(char const * s, size_t ls, LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return 1;
|
2002-06-19 03:38:44 +00:00
|
|
|
// handle small caps ourselves ...
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
QFontMetrics const & qm = fontloader.metrics(f);
|
|
|
|
QFontMetrics const & qsmallm = fontloader.metrics(smallfont);
|
2002-06-19 03:38:44 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
Encoding const * encoding(fontencoding(f));
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
int w = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ls; ++i) {
|
2002-12-11 00:22:37 +00:00
|
|
|
QChar const c = QChar(encoding->ucs(s[i]));
|
|
|
|
QChar const uc = c.upper();
|
|
|
|
if (c != uc)
|
|
|
|
w += qsmallm.width(uc);
|
2002-06-19 03:38:44 +00:00
|
|
|
else
|
2002-10-15 15:13:49 +00:00
|
|
|
w += qm.width(c);
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
return w;
|
|
|
|
}
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
|
|
|
|
int width(char const * s, size_t ls, LyXFont const & f)
|
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2003-02-12 08:50:16 +00:00
|
|
|
return ls;
|
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
if (f.realShape() == LyXFont::SMALLCAPS_SHAPE) {
|
|
|
|
return smallcapswidth(s, ls, f);
|
|
|
|
}
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
Encoding const * encoding(fontencoding(f));
|
|
|
|
|
|
|
|
if (ls == 1) {
|
|
|
|
return charwidth(encoding->ucs(s[0]), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int w = 0;
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
for (size_t i = 0; i < ls; ++i) {
|
|
|
|
w += charwidth(encoding->ucs(s[i]), f);
|
|
|
|
}
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2002-12-11 00:22:37 +00:00
|
|
|
return w;
|
|
|
|
}
|
2002-06-19 03:38:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
int signedWidth(string const & s, LyXFont const & f)
|
|
|
|
{
|
|
|
|
if (s[0] == '-')
|
|
|
|
return -width(s.substr(1, s.length() - 1), f);
|
|
|
|
else
|
|
|
|
return width(s, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void rectText(string const & str, LyXFont const & f,
|
|
|
|
int & w,
|
2002-09-23 16:16:08 +00:00
|
|
|
int & ascent,
|
2002-06-19 03:38:44 +00:00
|
|
|
int & descent)
|
|
|
|
{
|
2002-06-21 13:50:57 +00:00
|
|
|
QFontMetrics const & m(metrics(f));
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-09-11 03:05:19 +00:00
|
|
|
static int const d = 2;
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-09-11 03:05:19 +00:00
|
|
|
w = width(str, f) + d * 2 + 2;
|
|
|
|
ascent = m.ascent() + d;
|
|
|
|
descent = m.descent() + d;
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void buttonText(string const & str, LyXFont const & f,
|
2002-09-23 16:16:08 +00:00
|
|
|
int & w,
|
|
|
|
int & ascent,
|
2002-06-19 03:38:44 +00:00
|
|
|
int & descent)
|
|
|
|
{
|
2002-06-21 13:50:57 +00:00
|
|
|
QFontMetrics const & m(metrics(f));
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
static int const d = 3;
|
2002-09-23 16:16:08 +00:00
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
w = width(str, f) + d * 2 + 2;
|
2002-06-21 13:50:57 +00:00
|
|
|
ascent = m.ascent() + d;
|
|
|
|
descent = m.descent() + d;
|
2002-06-19 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace font_metrics
|