2006-10-07 16:15:06 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
2007-08-31 22:16:11 +00:00
|
|
|
* \file GuiFontMetrics.h
|
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 Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
#ifndef GUI_FONT_METRICS_H
|
|
|
|
#define GUI_FONT_METRICS_H
|
2006-10-07 16:15:06 +00:00
|
|
|
|
|
|
|
#include "frontends/FontMetrics.h"
|
|
|
|
|
2017-02-20 22:59:24 +00:00
|
|
|
#include "support/Cache.h"
|
2006-10-07 16:15:06 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
|
2014-05-14 15:46:43 +00:00
|
|
|
#include <QFont>
|
2006-10-07 16:15:06 +00:00
|
|
|
#include <QFontMetrics>
|
2006-12-02 15:54:49 +00:00
|
|
|
#include <QHash>
|
2015-12-11 15:33:34 +00:00
|
|
|
#include <QTextLayout>
|
2006-10-07 16:15:06 +00:00
|
|
|
|
2017-02-20 22:59:24 +00:00
|
|
|
#include <memory>
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
class GuiFontMetrics : public FontMetrics
|
2006-10-07 16:15:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GuiFontMetrics(QFont const & font);
|
|
|
|
|
|
|
|
virtual ~GuiFontMetrics() {}
|
|
|
|
|
|
|
|
virtual int maxAscent() const;
|
|
|
|
virtual int maxDescent() const;
|
2006-12-04 10:09:22 +00:00
|
|
|
virtual Dimension const defaultDimension() const;
|
2015-03-26 15:55:19 +00:00
|
|
|
virtual int em() const;
|
2018-07-19 20:16:40 +00:00
|
|
|
virtual int xHeight() const;
|
2015-04-14 13:22:11 +00:00
|
|
|
virtual int lineWidth() const;
|
|
|
|
virtual int underlinePos() const;
|
|
|
|
virtual int strikeoutPos() const;
|
2006-12-01 16:12:24 +00:00
|
|
|
virtual int width(char_type c) const;
|
|
|
|
virtual int ascent(char_type c) const;
|
|
|
|
virtual int descent(char_type c) const;
|
|
|
|
virtual int lbearing(char_type c) const;
|
|
|
|
virtual int rbearing(char_type c) const;
|
2007-02-26 15:13:08 +00:00
|
|
|
virtual int width(docstring const & s) const;
|
2006-12-01 16:12:24 +00:00
|
|
|
virtual int signedWidth(docstring const & s) const;
|
2015-07-18 23:22:10 +00:00
|
|
|
virtual int pos2x(docstring const & s, int pos, bool rtl, double ws) const;
|
|
|
|
virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const;
|
2015-07-18 23:22:10 +00:00
|
|
|
virtual bool breakAt(docstring & s, int & x, bool rtl, bool force) const;
|
2006-12-04 10:09:22 +00:00
|
|
|
virtual Dimension const dimension(char_type c) const;
|
|
|
|
|
2006-12-01 16:12:24 +00:00
|
|
|
virtual void rectText(docstring const & str,
|
2006-10-07 16:15:06 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
|
|
|
int & descent) const;
|
2006-12-01 16:12:24 +00:00
|
|
|
virtual void buttonText(docstring const & str,
|
2017-06-15 13:30:23 +00:00
|
|
|
const int offset,
|
2006-10-07 16:15:06 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
|
|
|
int & descent) const;
|
2016-08-13 18:03:02 +00:00
|
|
|
|
|
|
|
int countExpanders(docstring const & str) const;
|
2006-10-27 21:27:03 +00:00
|
|
|
///
|
|
|
|
int width(QString const & str) const;
|
2006-10-07 16:15:06 +00:00
|
|
|
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
/// Return a pointer to a cached QTextLayout object
|
2017-02-20 22:59:24 +00:00
|
|
|
std::shared_ptr<QTextLayout const>
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
getTextLayout(docstring const & s, bool const rtl,
|
2017-02-20 22:59:24 +00:00
|
|
|
double const wordspacing) const;
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
private:
|
2015-12-11 15:33:34 +00:00
|
|
|
|
2017-02-20 22:59:24 +00:00
|
|
|
std::pair<int, int> breakAt_helper(docstring const & s, int const x,
|
|
|
|
bool const rtl, bool const force) const;
|
2015-12-11 15:33:34 +00:00
|
|
|
|
2014-05-14 15:46:43 +00:00
|
|
|
/// The font
|
|
|
|
QFont font_;
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
/// Metrics on the font
|
|
|
|
QFontMetrics metrics_;
|
|
|
|
|
|
|
|
/// Cache of char widths
|
2006-12-02 15:54:49 +00:00
|
|
|
mutable QHash<char_type, int> width_cache_;
|
2013-06-25 06:18:25 +00:00
|
|
|
/// Cache of string widths
|
2017-02-20 22:59:24 +00:00
|
|
|
mutable Cache<docstring, int> strwidth_cache_;
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
/// Cache for breakAt
|
2017-02-20 22:59:24 +00:00
|
|
|
mutable Cache<docstring, std::pair<int, int>> breakat_cache_;
|
|
|
|
/// Cache for QTextLayout
|
|
|
|
mutable Cache<docstring, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
|
2013-06-25 06:18:25 +00:00
|
|
|
|
2006-12-02 15:54:49 +00:00
|
|
|
struct AscendDescend {
|
2012-01-07 18:29:07 +00:00
|
|
|
int ascent;
|
|
|
|
int descent;
|
2006-12-02 15:54:49 +00:00
|
|
|
};
|
2006-12-02 21:55:28 +00:00
|
|
|
/// Cache of char ascends and descends
|
2006-12-02 15:54:49 +00:00
|
|
|
mutable QHash<char_type, AscendDescend> metrics_cache_;
|
|
|
|
/// fill in \c metrics_cache_ at specified value.
|
2007-08-13 13:56:54 +00:00
|
|
|
AscendDescend const fillMetricsCache(char_type) const;
|
2007-03-21 23:13:32 +00:00
|
|
|
|
|
|
|
/// Cache of char right bearings
|
|
|
|
mutable QHash<char_type, int> rbearing_cache_;
|
2015-12-11 15:33:34 +00:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
2007-08-31 22:16:11 +00:00
|
|
|
#endif // GUI_FONT_METRICS_H
|