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 23:59:24 +01:00
|
|
|
#include "support/Cache.h"
|
2006-10-07 16:15:06 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
|
2014-05-14 17:46:43 +02: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 16:33:34 +01:00
|
|
|
#include <QTextLayout>
|
2006-10-07 16:15:06 +00:00
|
|
|
|
2017-02-20 23:59:24 +01: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 14:06:22 +02:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2021-09-06 14:52:42 +02:00
|
|
|
struct BreakStringKey
|
2021-09-24 16:57:05 +02:00
|
|
|
{
|
2021-09-06 14:52:42 +02:00
|
|
|
bool operator==(BreakStringKey const & key) const {
|
|
|
|
return key.s == s && key.first_wid == first_wid && key.wid == wid
|
|
|
|
&& key.rtl == rtl && key.force == force;
|
2021-09-24 16:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
docstring s;
|
2021-09-06 14:52:42 +02:00
|
|
|
int first_wid;
|
|
|
|
int wid;
|
2021-09-24 16:57:05 +02:00
|
|
|
bool rtl;
|
|
|
|
bool force;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextLayoutKey
|
|
|
|
{
|
|
|
|
bool operator==(TextLayoutKey const & key) const {
|
|
|
|
return key.s == s && key.rtl == rtl && key.ws == ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring s;
|
|
|
|
bool rtl;
|
|
|
|
double ws;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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() {}
|
|
|
|
|
2020-10-01 10:42:11 +03:00
|
|
|
int maxAscent() const override;
|
|
|
|
int maxDescent() const override;
|
|
|
|
Dimension const defaultDimension() const override;
|
|
|
|
int em() const override;
|
|
|
|
int xHeight() const override;
|
|
|
|
int lineWidth() const override;
|
|
|
|
int underlinePos() const override;
|
|
|
|
int strikeoutPos() const override;
|
|
|
|
bool italic() const override;
|
|
|
|
double italicSlope() const override;
|
|
|
|
int width(char_type c) const override;
|
|
|
|
int ascent(char_type c) const override;
|
|
|
|
int descent(char_type c) const override;
|
|
|
|
int lbearing(char_type c) const override;
|
|
|
|
int rbearing(char_type c) const override;
|
|
|
|
int width(docstring const & s) const override;
|
|
|
|
int signedWidth(docstring const & s) const override;
|
|
|
|
int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
|
|
|
|
int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
|
2021-09-06 14:52:42 +02:00
|
|
|
Breaks breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const override;
|
2020-10-01 10:42:11 +03:00
|
|
|
Dimension const dimension(char_type c) const override;
|
2006-12-04 10:09:22 +00:00
|
|
|
|
2020-10-01 10:42:11 +03:00
|
|
|
void rectText(docstring const & str,
|
2006-10-07 16:15:06 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
2020-10-01 10:42:11 +03:00
|
|
|
int & descent) const override;
|
|
|
|
void buttonText(docstring const & str,
|
2017-06-15 15:30:23 +02:00
|
|
|
const int offset,
|
2006-10-07 16:15:06 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
2020-10-01 10:42:11 +03:00
|
|
|
int & descent) const override;
|
2016-08-13 19:03:02 +01:00
|
|
|
|
2020-10-01 10:42:11 +03:00
|
|
|
int countExpanders(docstring const & str) const override;
|
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 14:06:22 +02:00
|
|
|
/// Return a pointer to a cached QTextLayout object
|
2017-02-20 23:59:24 +01: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 14:06:22 +02:00
|
|
|
getTextLayout(docstring const & s, bool const rtl,
|
2017-02-20 23:59:24 +01: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 14:06:22 +02:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
private:
|
2015-12-11 16:33:34 +01:00
|
|
|
|
2021-09-06 14:52:42 +02:00
|
|
|
Breaks breakString_helper(docstring const & s, int first_wid, int wid,
|
|
|
|
bool rtl, bool force) const;
|
2015-12-11 16:33:34 +01:00
|
|
|
|
2014-05-14 17:46:43 +02:00
|
|
|
/// The font
|
|
|
|
QFont font_;
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
/// Metrics on the font
|
|
|
|
QFontMetrics metrics_;
|
|
|
|
|
2020-09-23 11:18:08 +03:00
|
|
|
/// Slope of italic font
|
|
|
|
double slope_;
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
/// Cache of char widths
|
2006-12-02 15:54:49 +00:00
|
|
|
mutable QHash<char_type, int> width_cache_;
|
2013-06-25 08:18:25 +02:00
|
|
|
/// Cache of string widths
|
2017-02-20 23:59:24 +01:00
|
|
|
mutable Cache<docstring, int> strwidth_cache_;
|
2021-09-06 14:52:42 +02:00
|
|
|
/// Cache for breakString
|
|
|
|
mutable Cache<BreakStringKey, Breaks> breakstr_cache_;
|
2017-02-20 23:59:24 +01:00
|
|
|
/// Cache for QTextLayout
|
2021-09-24 16:57:05 +02:00
|
|
|
mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
|
2013-06-25 08:18:25 +02: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
|
|
|
|
2019-03-21 18:45:28 +01:00
|
|
|
/// Cache of char left bearings
|
2019-03-20 10:56:16 +01:00
|
|
|
mutable QHash<char_type, int> lbearing_cache_;
|
2007-03-21 23:13:32 +00:00
|
|
|
/// Cache of char right bearings
|
|
|
|
mutable QHash<char_type, int> rbearing_cache_;
|
2015-12-11 16:33:34 +01: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
|