]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiFontMetrics.h
Fix up 'Reduce metrics updates from 4 to 1 when loading file'
[lyx.git] / src / frontends / qt / 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/Cache.h"
18 #include "support/docstring.h"
19
20 #include <QFont>
21 #include <QFontMetrics>
22 #include <QHash>
23 #include <QTextLayout>
24
25 #include <memory>
26
27 namespace lyx {
28 namespace frontend {
29
30 struct TextLayoutHelper;
31
32 struct BreakStringKey
33 {
34         bool operator==(BreakStringKey const & key) const {
35                 return key.s == s && key.first_wid == first_wid && key.wid == wid
36                         && key.rtl == rtl && key.force == force;
37         }
38
39         docstring s;
40         int first_wid;
41         int wid;
42         bool rtl;
43         bool force;
44 };
45
46 struct TextLayoutKey
47 {
48         bool operator==(TextLayoutKey const & key) const {
49                 return key.s == s && key.rtl == rtl && key.ws == ws;
50         }
51
52         docstring s;
53         bool rtl;
54         double ws;
55 };
56
57
58 class GuiFontMetrics : public FontMetrics
59 {
60 public:
61         GuiFontMetrics(QFont const & font);
62
63         virtual ~GuiFontMetrics() {}
64
65         int maxAscent() const override;
66         int maxDescent() const override;
67         Dimension const defaultDimension() const override;
68         int em() const override;
69         int xHeight() const override;
70         int lineWidth() const override;
71         int underlinePos() const override;
72         int strikeoutPos() const override;
73         bool italic() const override;
74         double italicSlope() const override;
75         int width(char_type c) const override;
76         int ascent(char_type c) const override;
77         int descent(char_type c) const override;
78         int lbearing(char_type c) const override;
79         int rbearing(char_type c) const override;
80         int width(docstring const & s) const override;
81         int signedWidth(docstring const & s) const override;
82         int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
83         int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
84         Breaks breakString(docstring const & s, int first_wid, int wid, bool rtl, bool force) const override;
85         Dimension const dimension(char_type c) const override;
86
87         void rectText(docstring const & str,
88                 int & width,
89                 int & ascent,
90                 int & descent) const override;
91         void buttonText(docstring const & str,
92                 const int offset,
93                 int & width,
94                 int & ascent,
95                 int & descent) const override;
96
97         ///
98         int width(QString const & str) const;
99
100         /// Return a pointer to a cached QTextLayout object
101         std::shared_ptr<QTextLayout const>
102         getTextLayout(docstring const & s, bool const rtl, double const wordspacing) const;
103
104 private:
105
106         Breaks breakString_helper(docstring const & s, int first_wid, int wid,
107                                   bool rtl, bool force) const;
108
109         /// A different version of getTextLayout for internal use
110         std::shared_ptr<QTextLayout const>
111         getTextLayout(TextLayoutHelper const & tlh, double const wordspacing) const;
112
113         /// The font
114         QFont font_;
115
116         /// Metrics on the font
117         QFontMetrics metrics_;
118
119         /// Height of character "x"
120         int xheight_;
121
122         /// Slope of italic font
123         double slope_;
124
125         /// Cache of char widths
126         mutable QHash<char_type, int> width_cache_;
127         /// Cache of string widths
128         mutable Cache<docstring, int> strwidth_cache_;
129         /// Cache for breakString
130         mutable Cache<BreakStringKey, Breaks> breakstr_cache_;
131         /// Cache for QTextLayout
132         mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
133
134         struct AscendDescend {
135                 int ascent;
136                 int descent;
137         };
138         /// Cache of char ascends and descends
139         mutable QHash<char_type, AscendDescend> metrics_cache_;
140         /// fill in \c metrics_cache_ at specified value.
141         AscendDescend const fillMetricsCache(char_type) const;
142
143         /// Cache of char left bearings
144         mutable QHash<char_type, int> lbearing_cache_;
145         /// Cache of char right bearings
146         mutable QHash<char_type, int> rbearing_cache_;
147
148 };
149
150 } // namespace frontend
151 } // namespace lyx
152
153 #endif // GUI_FONT_METRICS_H