2006-10-07 16:15:06 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file GuiFontMetrics.cpp
|
2006-10-07 16:15:06 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author unknown
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "GuiFontMetrics.h"
|
|
|
|
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Language.h"
|
|
|
|
#include "Dimension.h"
|
2006-10-07 16:15:06 +00:00
|
|
|
|
|
|
|
#include "support/unicode.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
GuiFontMetrics::GuiFontMetrics(QFont const & font)
|
|
|
|
: metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font)
|
|
|
|
: metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::maxAscent() const
|
|
|
|
{
|
|
|
|
return metrics_.ascent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::maxDescent() const
|
|
|
|
{
|
|
|
|
// We add 1 as the value returned by QT is different than X
|
|
|
|
// See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
|
|
|
|
return metrics_.descent() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::lbearing(char_type c) const
|
|
|
|
{
|
|
|
|
return metrics_.leftBearing(ucs4_to_qchar(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::rbearing(char_type c) const
|
|
|
|
{
|
2007-03-21 23:13:32 +00:00
|
|
|
if (!rbearing_cache_.contains(c)) {
|
|
|
|
// Qt rbearing is from the right edge of the char's width().
|
|
|
|
QChar sc = ucs4_to_qchar(c);
|
|
|
|
int rb = metrics_.width(sc) - metrics_.rightBearing(sc);
|
|
|
|
rbearing_cache_.insert(c, rb);
|
|
|
|
}
|
|
|
|
return rbearing_cache_.value(c);
|
2006-10-07 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::smallcapsWidth(QString const & s) const
|
|
|
|
{
|
|
|
|
int w = 0;
|
|
|
|
int const ls = s.size();
|
|
|
|
|
|
|
|
for (int i = 0; i < ls; ++i) {
|
|
|
|
QChar const & c = s[i];
|
|
|
|
QChar const uc = c.toUpper();
|
|
|
|
if (c != uc)
|
|
|
|
w += smallcaps_metrics_.width(uc);
|
|
|
|
else
|
|
|
|
w += metrics_.width(c);
|
|
|
|
}
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-26 15:13:08 +00:00
|
|
|
int GuiFontMetrics::width(docstring const & s) const
|
2006-10-07 16:15:06 +00:00
|
|
|
{
|
2007-02-26 15:13:08 +00:00
|
|
|
size_t ls = s.size();
|
|
|
|
if (ls == 0)
|
|
|
|
return 0;
|
2006-11-07 20:46:38 +00:00
|
|
|
|
2006-10-22 18:49:18 +00:00
|
|
|
if (ls == 1 && !smallcaps_shape_) {
|
2006-12-01 16:12:24 +00:00
|
|
|
return width(s[0]);
|
2006-10-22 18:49:18 +00:00
|
|
|
}
|
2006-10-23 08:46:09 +00:00
|
|
|
|
2007-02-26 15:13:08 +00:00
|
|
|
if (smallcaps_shape_)
|
2007-02-26 16:22:54 +00:00
|
|
|
// Caution: The following ucs4 to QString conversions work
|
|
|
|
// for symbol fonts only because they are no real conversions
|
|
|
|
// but simple casts in reality. See comment in QLPainter::text()
|
|
|
|
// for more explanation.
|
2007-02-26 15:13:08 +00:00
|
|
|
return smallcapsWidth(toqstr(s));
|
2006-10-07 16:15:06 +00:00
|
|
|
|
|
|
|
int w = 0;
|
|
|
|
for (unsigned int i = 0; i < ls; ++i)
|
2006-12-01 16:12:24 +00:00
|
|
|
w += width(s[i]);
|
2006-10-27 21:27:03 +00:00
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::width(QString const & ucs2) const
|
|
|
|
{
|
|
|
|
int const ls = ucs2.size();
|
|
|
|
if (ls == 1 && !smallcaps_shape_) {
|
|
|
|
return width(ucs2[0].unicode());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smallcaps_shape_)
|
|
|
|
return smallcapsWidth(ucs2);
|
|
|
|
|
|
|
|
int w = 0;
|
|
|
|
for (int i = 0; i < ls; ++i)
|
2006-10-07 16:15:06 +00:00
|
|
|
w += width(ucs2[i].unicode());
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::signedWidth(docstring const & s) const
|
|
|
|
{
|
2006-11-08 11:32:33 +00:00
|
|
|
if (s.empty())
|
|
|
|
return 0;
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
if (s[0] == '-')
|
2007-02-26 15:13:08 +00:00
|
|
|
return -width(s.substr(1, s.size() - 1));
|
2006-10-07 16:15:06 +00:00
|
|
|
else
|
2007-02-26 15:13:08 +00:00
|
|
|
return width(s);
|
2006-10-07 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiFontMetrics::rectText(docstring const & str,
|
|
|
|
int & w, int & ascent, int & descent) const
|
|
|
|
{
|
|
|
|
static int const d = 2;
|
2007-02-26 15:13:08 +00:00
|
|
|
w = width(str) + d * 2 + 2;
|
2006-10-07 16:15:06 +00:00
|
|
|
ascent = metrics_.ascent() + d;
|
|
|
|
descent = metrics_.descent() + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GuiFontMetrics::buttonText(docstring const & str,
|
|
|
|
int & w, int & ascent, int & descent) const
|
|
|
|
{
|
|
|
|
static int const d = 3;
|
2007-02-26 15:13:08 +00:00
|
|
|
w = width(str) + d * 2 + 2;
|
2006-10-07 16:15:06 +00:00
|
|
|
ascent = metrics_.ascent() + d;
|
|
|
|
descent = metrics_.descent() + d;
|
|
|
|
}
|
|
|
|
|
2006-12-01 16:12:24 +00:00
|
|
|
|
2006-12-04 10:09:22 +00:00
|
|
|
Dimension const GuiFontMetrics::defaultDimension() const
|
2006-10-07 16:15:06 +00:00
|
|
|
{
|
2006-12-04 10:45:43 +00:00
|
|
|
return Dimension(0, maxAscent(), maxDescent());
|
2006-10-07 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2006-12-01 16:12:24 +00:00
|
|
|
|
2006-12-04 10:09:22 +00:00
|
|
|
Dimension const GuiFontMetrics::dimension(char_type c) const
|
2006-12-01 16:12:24 +00:00
|
|
|
{
|
2006-12-04 10:45:43 +00:00
|
|
|
return Dimension(width(c), ascent(c), descent(c));
|
2006-12-01 16:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
void GuiFontMetrics::fillMetricsCache(char_type c) const
|
2006-12-01 16:12:24 +00:00
|
|
|
{
|
2006-12-02 15:54:49 +00:00
|
|
|
QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
|
|
|
|
AscendDescend ad = { -r.top(), r.bottom() + 1};
|
2006-12-01 16:12:24 +00:00
|
|
|
// We could as well compute the width but this is not really
|
|
|
|
// needed for now as it is done directly in width() below.
|
2006-12-02 15:54:49 +00:00
|
|
|
metrics_cache_.insert(c, ad);
|
2006-12-01 16:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::width(char_type c) const
|
|
|
|
{
|
2006-12-09 07:07:00 +00:00
|
|
|
if (smallcaps_shape_)
|
|
|
|
return smallcapsWidth(ucs4_to_qchar(c));
|
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
if (!width_cache_.contains(c)) {
|
|
|
|
width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c)));
|
2006-12-01 23:45:06 +00:00
|
|
|
}
|
2006-12-01 16:12:24 +00:00
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
return width_cache_.value(c);
|
2006-12-01 16:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::ascent(char_type c) const
|
|
|
|
{
|
2006-12-02 15:54:49 +00:00
|
|
|
if (!metrics_cache_.contains(c))
|
|
|
|
fillMetricsCache(c);
|
2006-12-01 16:12:24 +00:00
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
return metrics_cache_.value(c).ascent;
|
2006-10-07 16:15:06 +00:00
|
|
|
}
|
2006-12-01 16:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
int GuiFontMetrics::descent(char_type c) const
|
|
|
|
{
|
2006-12-02 15:54:49 +00:00
|
|
|
if (!metrics_cache_.contains(c))
|
|
|
|
fillMetricsCache(c);
|
2006-12-01 16:12:24 +00:00
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
return metrics_cache_.value(c).descent;
|
2006-10-07 16:15:06 +00:00
|
|
|
}
|
2006-12-01 16:12:24 +00:00
|
|
|
|
|
|
|
} // frontend
|
|
|
|
} // lyx
|