Compile fix. This was easy, since the code is alsmost identical to qt4,

but I am not going to do gtk.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15271 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-10-08 07:39:02 +00:00
parent 3f0d2901c2
commit 94c10102c0
10 changed files with 315 additions and 231 deletions

View File

@ -735,6 +735,7 @@ src_frontends_qt3_header_files = Split('''
FileDialog_private.h
GuiApplication.h
GuiClipboard.h
GuiFontMetrics.h
GuiImplementation.h
GuiSelection.h
GuiWorkArea.h
@ -856,6 +857,7 @@ src_frontends_qt3_files = Split('''
FileDialog_private.C
GuiApplication.C
GuiClipboard.C
GuiFontMetrics.C
GuiSelection.C
LyXKeySymFactory.C
QAbout.C
@ -956,7 +958,6 @@ src_frontends_qt3_files = Split('''
panelstack.C
qcoloritem.C
qfont_loader.C
qfont_metrics.C
qfontexample.C
qscreen.C
qsetborder.C

View File

@ -0,0 +1,199 @@
/**
* \file GuiFontMetrics.C
* 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"
#include "language.h"
#include "frontends/lyx_gui.h"
#include "support/unicode.h"
using lyx::char_type;
using lyx::docstring;
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
{
if (!lyx_gui::use_gui)
return 1;
return metrics_.ascent();
}
int GuiFontMetrics::maxDescent() const
{
if (!lyx_gui::use_gui)
return 1;
// 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::ascent(char_type c) const
{
if (!lyx_gui::use_gui)
return 1;
QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
return -r.top() - r.height();
#else
return -r.top();
#endif
}
int GuiFontMetrics::descent(char_type c) const
{
if (!lyx_gui::use_gui)
return 1;
QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
return r.bottom() + r.height() + 1;
#else
return r.bottom() + 1;
#endif
}
int GuiFontMetrics::lbearing(char_type c) const
{
if (!lyx_gui::use_gui)
return 1;
return metrics_.leftBearing(ucs4_to_qchar(c));
}
int GuiFontMetrics::rbearing(char_type c) const
{
if (!lyx_gui::use_gui)
return 1;
// Qt rbearing is from the right edge of the char's width().
QChar sc = ucs4_to_qchar(c);
return metrics_.width(sc) - metrics_.rightBearing(sc);
}
int GuiFontMetrics::smallcapsWidth(QString const & s) const
{
if (!lyx_gui::use_gui)
return 1;
int w = 0;
int const ls = s.length();
for (int i = 0; i < ls; ++i) {
QChar const & c = s[i];
QChar const uc = c.upper();
if (c != uc)
w += smallcaps_metrics_.width(uc);
else
w += metrics_.width(c);
}
return w;
}
int GuiFontMetrics::width(char_type const * s, size_t ls) const
{
if (!lyx_gui::use_gui)
return ls;
QString const ucs2 = toqstr(s, ls);
if (smallcaps_shape_)
return smallcapsWidth(ucs2);
if (ls == 1)
return width(ucs2[0].unicode());
int w = 0;
for (unsigned int i = 0; i < ls; ++i)
w += width(ucs2[i].unicode());
return w;
}
int GuiFontMetrics::signedWidth(docstring const & s) const
{
if (s[0] == '-')
return -FontMetrics::width(s.substr(1, s.length() - 1));
else
return FontMetrics::width(s);
}
void GuiFontMetrics::rectText(docstring const & str,
int & w, int & ascent, int & descent) const
{
static int const d = 2;
w = FontMetrics::width(str) + d * 2 + 2;
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;
w = FontMetrics::width(str) + d * 2 + 2;
ascent = metrics_.ascent() + d;
descent = metrics_.descent() + d;
}
#ifdef USE_LYX_FONTCACHE
int GuiFontMetrics::width(unsigned short val) const
{
GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val);
if (cit != widthcache.end())
return cit->second;
int const w = metrics_.width(QChar(val));
widthcache[val] = w;
return w;
}
#endif
}
}

View File

@ -0,0 +1,83 @@
// -*- C++ -*-
/**
* \file FontMetrics.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef QT3_FONT_METRICS_H
#define QT3_FONT_METRICS_H
#include "frontends/FontMetrics.h"
#include "support/docstring.h"
#include <qfontmetrics.h>
// Starting with version 3.1.0, Qt/X11 does its own caching of
// character width, so it is not necessary to provide ours.
#if defined(Q_WS_MACX) || defined(Q_WS_WIN32)
#define USE_LYX_FONTCACHE
#include <map>
#endif
namespace lyx {
namespace frontend {
class GuiFontMetrics: public FontMetrics
{
public:
GuiFontMetrics(QFont const & font);
GuiFontMetrics(QFont const & font, QFont const & smallcaps_font);
virtual ~GuiFontMetrics() {}
virtual int maxAscent() const;
virtual int maxDescent() const;
virtual int ascent(lyx::char_type c) const;
int descent(lyx::char_type c) const;
virtual int lbearing(lyx::char_type c) const;
virtual int rbearing(lyx::char_type c) const;
virtual int width(lyx::char_type const * s, size_t n) const;
virtual int signedWidth(lyx::docstring const & s) const;
virtual void rectText(lyx::docstring const & str,
int & width,
int & ascent,
int & descent) const;
virtual void buttonText(lyx::docstring const & str,
int & width,
int & ascent,
int & descent) const;
private:
int smallcapsWidth(QString const & s) const;
/// Metrics on the font
QFontMetrics metrics_;
QFontMetrics smallcaps_metrics_;
bool smallcaps_shape_;
#ifndef USE_LYX_FONTCACHE
/// Return pixel width for the given unicode char
int width(unsigned short val) const { return metrics_.width(QChar(val)); }
#else
/// Return pixel width for the given unicode char
int width(unsigned short val) const;
typedef std::map<unsigned short, int> WidthCache;
/// Cache of char widths
mutable WidthCache widthcache;
#endif // USE_LYX_FONTCACHE
};
} // namespace frontend
} // namespace lyx
#endif // QT3_FONT_METRICS_H

View File

@ -36,6 +36,7 @@ libqt3_la_SOURCES = \
FileDialog.C \
GuiApplication.C GuiApplication.h \
GuiClipboard.C GuiClipboard.h \
GuiFontMetrics.C GuiFontMetrics.h \
GuiImplementation.h \
GuiSelection.C GuiSelection.h \
GuiWorkArea.h \
@ -91,7 +92,6 @@ libqt3_la_SOURCES = \
qcoloritem.h qcoloritem.C \
qfontexample.h qfontexample.C \
qfont_loader.h qfont_loader.C \
qfont_metrics.C \
qlkey.h \
qscreen.h qscreen.C \
qt_helpers.h qt_helpers.C \

View File

@ -23,7 +23,7 @@
#include "support/unicode.h"
#include "frontends/font_metrics.h"
#include "frontends/FontMetrics.h"
#include <qpainter.h>
@ -245,7 +245,7 @@ void QLPainter::text(int x, int y, lyx::char_type const * s, size_t ls,
}
if (f.underbar() == LyXFont::ON) {
underline(f, x, y, font_metrics::width(s, ls, f));
underline(f, x, y, qp_->fontMetrics().width(str));
}
}

View File

@ -219,7 +219,6 @@ void GuiFontLoader::update()
QLFontInfo::QLFontInfo(LyXFont const & f)
: metrics(font)
{
string const pat = symbolFamily(f.family());
@ -285,25 +284,7 @@ QLFontInfo::QLFontInfo(LyXFont const & f)
lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
metrics = QFontMetrics(font);
}
int QLFontInfo::width(Uchar val)
{
// Starting with version 3.1.0, Qt/X11 does its own caching of
// character width, so it is not necessary to provide ours.
#if defined (USE_LYX_FONTCACHE)
QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val);
if (cit != widthcache.end())
return cit->second;
int const w = metrics.width(QChar(val));
widthcache[val] = w;
return w;
#else
return metrics.width(QChar(val));
#endif
metrics.reset(new lyx::frontend::GuiFontMetrics(font));
}

View File

@ -14,19 +14,13 @@
#include "frontends/FontLoader.h"
#include "GuiFontMetrics.h"
#include "encoding.h"
#include "lyxfont.h"
#include <qfont.h>
#include <qfontmetrics.h>
#if QT_VERSION < 0x030100 || defined(Q_WS_MACX)
#define USE_LYX_FONTCACHE
#endif
#if defined(USE_LYX_FONTCACHE)
#include <map>
#endif
/**
* Qt font loader for LyX. Matches LyXFonts against
@ -36,19 +30,10 @@ class QLFontInfo {
public:
QLFontInfo(LyXFont const & f);
/// Return pixel width for the given unicode char
int width(Uchar val);
/// The font instance
QFont font;
/// Metrics on the font
QFontMetrics metrics;
#if defined(USE_LYX_FONTCACHE)
typedef std::map<Uchar, int> WidthCache;
/// Cache of char widths
WidthCache widthcache;
#endif
boost::scoped_ptr<lyx::frontend::GuiFontMetrics> metrics;
};
@ -73,8 +58,8 @@ public:
}
/// Get the QFont metrics for this LyXFont
QFontMetrics const & metrics(LyXFont const & f) {
return fontinfo(f).metrics;
lyx::frontend::FontMetrics const & metrics(LyXFont const & f) {
return *(fontinfo(f).metrics);
}
/// Called the first time when available() can't load a symbol font

View File

@ -1,186 +0,0 @@
/**
* \file qfont_metrics.C
* 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 "frontends/font_metrics.h"
#include "frontends/lyx_gui.h"
#include "qfont_loader.h"
#include "language.h"
#include "support/unicode.h"
using lyx::char_type;
using lyx::docstring;
using std::string;
namespace {
int smallcapswidth(unsigned short const * s, size_t ls, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
// handle small caps ourselves ...
LyXFont smallfont = f;
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
QFontMetrics const & qm = fontloader.metrics(f);
QFontMetrics const & qsmallm = fontloader.metrics(smallfont);
int w = 0;
for (size_t i = 0; i < ls; ++i) {
QChar const c = s[i];
QChar const uc = c.upper();
if (c != uc)
w += qsmallm.width(uc);
else
w += qm.width(c);
}
return w;
}
} // anon namespace
int font_metrics::maxAscent(LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
return fontloader.metrics(f).ascent();
}
int font_metrics::maxDescent(LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
// We add 1 as the value returned by QT is different than X
// See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
return fontloader.metrics(f).descent() + 1;
}
int font_metrics::ascent(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
QRect const & r = fontloader.metrics(f).boundingRect(ucs4_to_ucs2(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
return -r.top() - r.height();
#else
return -r.top();
#endif
}
int font_metrics::descent(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
QRect const & r = fontloader.metrics(f).boundingRect(ucs4_to_ucs2(c));
// Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
// value by the height: (x, -y-height, width, height).
// Other versions return: (x, -y, width, height)
#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201)
return r.bottom() + r.height() + 1;
#else
return r.bottom() + 1;
#endif
}
int font_metrics::lbearing(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
return fontloader.metrics(f).leftBearing(ucs4_to_ucs2(c));
}
int font_metrics::rbearing(char_type c, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return 1;
QFontMetrics const & m = fontloader.metrics(f);
// Qt rbearing is from the right edge of the char's width().
unsigned short sc = ucs4_to_ucs2(c);
return m.width(sc) - m.rightBearing(sc);
}
int font_metrics::width(lyx::char_type const * str, size_t const ls, LyXFont const & f)
{
if (!lyx_gui::use_gui)
return ls;
std::vector<unsigned short> ucs2 = ucs4_to_ucs2(str, ls);
ucs2.push_back(0);
if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
return smallcapswidth(&ucs2[0], ls, f);
QLFontInfo & fi = fontloader.fontinfo(f);
if (ls == 1)
return fi.width(ucs2[0]);
int w = 0;
for (size_t i = 0; i < ls; ++i)
w += fi.width(ucs2[i]);
return w;
}
void font_metrics::rectText(docstring const & str, LyXFont const & f,
int & w, int & ascent, int & descent)
{
QFontMetrics const & m = fontloader.metrics(f);
static int const d = 2;
w = font_metrics::width(str, f) + d * 2 + 2;
ascent = m.ascent() + d;
descent = m.descent() + d;
}
void font_metrics::buttonText(docstring const & str, LyXFont const & f,
int & w, int & ascent, int & descent)
{
QFontMetrics const & m = fontloader.metrics(f);
static int const d = 3;
w = font_metrics::width(str, f) + d * 2 + 2;
ascent = m.ascent() + d;
descent = m.descent() + d;
}
int font_metrics::signedWidth(docstring const & s, LyXFont const & f)
{
if (s[0] == '-')
return -font_metrics::width(s.substr(1, s.length() - 1), f);
else
return font_metrics::width(s, f);
}

View File

@ -126,6 +126,15 @@ QString const toqstr(docstring const & str)
}
QString const toqstr(lyx::char_type const * str, size_t ls)
{
std::vector<unsigned short> ucs2 =
ucs4_to_ucs2(str, ls);
ucs2.push_back('\0');
return QString::fromUcs2(&ucs2[0]);
}
QString const qt_(char const * str)
{
return toqstr(_(str));

View File

@ -17,10 +17,11 @@
#include "lyxlength.h"
#include "support/docstring.h"
#include <qstring.h>
class LengthCombo;
class QComboBox;
class QLineEdit;
class QString;
std::string makeFontName(std::string const & family, std::string const & foundry);
@ -62,6 +63,17 @@ QString const toqstr(std::string const & str);
QString const toqstr(lyx::docstring const & str);
/**
* toqstr - convert UCS4 encoded docstring to QString
*/
QString const toqstr(lyx::char_type const * str, size_t ls);
inline QChar const ucs4_to_qchar(lyx::char_type const ucs4) {
return QChar(static_cast<unsigned short>(ucs4));
}
/**
* qt_ - i18nize string and convert to unicode
*