3 * \file GuiFontMetrics.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
12 #ifndef GUI_FONT_METRICS_H
13 #define GUI_FONT_METRICS_H
15 #include "frontends/FontMetrics.h"
17 #include "support/docstring.h"
20 #include <QFontMetrics>
22 #include <QTextLayout>
24 // Declare which font metrics elements have to be cached
26 #define CACHE_METRICS_WIDTH
27 #define CACHE_METRICS_BREAKAT
28 // Qt 5.x already has its own caching of QTextLayout objects
29 #if (QT_VERSION < 0x050000)
30 #define CACHE_METRICS_QTEXTLAYOUT
33 #if defined(CACHE_METRICS_WIDTH) || defined(CACHE_METRICS_BREAKAT) \
34 || defined(CACHE_METRICS_QTEXTLAYOUT)
35 #define CACHE_SOME_METRICS
38 #ifdef CACHE_SOME_METRICS
45 class GuiFontMetrics : public FontMetrics
48 GuiFontMetrics(QFont const & font);
50 virtual ~GuiFontMetrics() {}
52 virtual int maxAscent() const;
53 virtual int maxDescent() const;
54 virtual Dimension const defaultDimension() const;
55 virtual int em() const;
56 virtual int lineWidth() const;
57 virtual int underlinePos() const;
58 virtual int strikeoutPos() const;
59 virtual int width(char_type c) const;
60 virtual int ascent(char_type c) const;
61 virtual int descent(char_type c) const;
62 virtual int lbearing(char_type c) const;
63 virtual int rbearing(char_type c) const;
64 virtual int width(docstring const & s) const;
65 virtual int signedWidth(docstring const & s) const;
66 virtual int pos2x(docstring const & s, int pos, bool rtl, double ws) const;
67 virtual int x2pos(docstring const & s, int & x, bool rtl, double ws) const;
68 virtual bool breakAt(docstring & s, int & x, bool rtl, bool force) const;
69 virtual Dimension const dimension(char_type c) const;
71 virtual void rectText(docstring const & str,
75 virtual void buttonText(docstring const & str,
80 int countExpanders(docstring const & str) const;
82 int width(QString const & str) const;
84 /// Return a pointer to a cached QTextLayout object
86 getTextLayout(docstring const & s, bool const rtl,
87 double const wordspacing) const;
92 breakAt_helper(docstring const & s, int const x,
93 bool const rtl, bool const force) const;
98 /// Metrics on the font
99 QFontMetrics metrics_;
101 /// Cache of char widths
102 mutable QHash<char_type, int> width_cache_;
104 #ifdef CACHE_METRICS_WIDTH
105 /// Cache of string widths
106 mutable QCache<docstring, int> strwidth_cache_;
109 #ifdef CACHE_METRICS_BREAKAT
110 /// Cache for breakAt
111 mutable QCache<docstring, std::pair<int, int>> breakat_cache_;
114 #ifdef CACHE_METRICS_QTEXTLAYOUT
115 /// Cache for QTextLayout:s
116 mutable QCache<docstring, QTextLayout> qtextlayout_cache_;
119 struct AscendDescend {
123 /// Cache of char ascends and descends
124 mutable QHash<char_type, AscendDescend> metrics_cache_;
125 /// fill in \c metrics_cache_ at specified value.
126 AscendDescend const fillMetricsCache(char_type) const;
128 /// Cache of char right bearings
129 mutable QHash<char_type, int> rbearing_cache_;
133 } // namespace frontend
136 #endif // GUI_FONT_METRICS_H