]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
Revert "Fix crash"
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file GuiFontMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef GUI_FONT_METRICS_H
13 #define GUI_FONT_METRICS_H
14
15 #include "frontends/FontMetrics.h"
16
17 #include "support/docstring.h"
18
19 #include <QFont>
20 #include <QFontMetrics>
21 #include <QHash>
22 #include <QTextLayout>
23
24 // Declare which font metrics elements have to be cached
25
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
31 #endif
32
33 #if defined(CACHE_METRICS_WIDTH) || defined(CACHE_METRICS_BREAKAT) \
34   || defined(CACHE_METRICS_QTEXTLAYOUT)
35 #define CACHE_SOME_METRICS
36 #endif
37
38 #ifdef CACHE_SOME_METRICS
39 #include <QCache>
40 #endif
41
42 namespace lyx {
43 namespace frontend {
44
45 class GuiFontMetrics : public FontMetrics
46 {
47 public:
48         GuiFontMetrics(QFont const & font);
49
50         virtual ~GuiFontMetrics() {}
51
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;
70
71         virtual void rectText(docstring const & str,
72                 int & width,
73                 int & ascent,
74                 int & descent) const;
75         virtual void buttonText(docstring const & str,
76                 int & width,
77                 int & ascent,
78                 int & descent) const;
79
80         int countExpanders(docstring const & str) const;
81         ///
82         int width(QString const & str) const;
83
84         /// Return a pointer to a cached QTextLayout object
85         QTextLayout const *
86         getTextLayout(docstring const & s, bool const rtl,
87                   double const wordspacing) const;
88
89 private:
90
91         std::pair<int, int> *
92         breakAt_helper(docstring const & s, int const x,
93                        bool const rtl, bool const force) const;
94
95         /// The font
96         QFont font_;
97
98         /// Metrics on the font
99         QFontMetrics metrics_;
100
101         /// Cache of char widths
102         mutable QHash<char_type, int> width_cache_;
103
104 #ifdef CACHE_METRICS_WIDTH
105         /// Cache of string widths
106         mutable QCache<docstring, int> strwidth_cache_;
107 #endif
108
109 #ifdef CACHE_METRICS_BREAKAT
110         /// Cache for breakAt
111         mutable QCache<docstring, std::pair<int, int>> breakat_cache_;
112 #endif
113
114 #ifdef CACHE_METRICS_QTEXTLAYOUT
115         /// Cache for QTextLayout:s
116         mutable QCache<docstring, QTextLayout> qtextlayout_cache_;
117 #endif
118
119         struct AscendDescend {
120                 int ascent;
121                 int descent;
122         };
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;
127
128         /// Cache of char right bearings
129         mutable QHash<char_type, int> rbearing_cache_;
130
131 };
132
133 } // namespace frontend
134 } // namespace lyx
135
136 #endif // GUI_FONT_METRICS_H