]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiFontMetrics.h
Change FontMetrics::breakAt to return a position
[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 BreakAtKey
31 {
32         bool operator==(BreakAtKey const & key) const {
33                 return key.s == s && key.x == x && key.rtl == rtl && key.force == force;
34         }
35
36         docstring s;
37         int x;
38         bool rtl;
39         bool force;
40 };
41
42 struct TextLayoutKey
43 {
44         bool operator==(TextLayoutKey const & key) const {
45                 return key.s == s && key.rtl == rtl && key.ws == ws;
46         }
47
48         docstring s;
49         bool rtl;
50         double ws;
51 };
52
53
54 class GuiFontMetrics : public FontMetrics
55 {
56 public:
57         GuiFontMetrics(QFont const & font);
58
59         virtual ~GuiFontMetrics() {}
60
61         int maxAscent() const override;
62         int maxDescent() const override;
63         Dimension const defaultDimension() const override;
64         int em() const override;
65         int xHeight() const override;
66         int lineWidth() const override;
67         int underlinePos() const override;
68         int strikeoutPos() const override;
69         bool italic() const override;
70         double italicSlope() const override;
71         int width(char_type c) const override;
72         int ascent(char_type c) const override;
73         int descent(char_type c) const override;
74         int lbearing(char_type c) const override;
75         int rbearing(char_type c) const override;
76         int width(docstring const & s) const override;
77         int signedWidth(docstring const & s) const override;
78         int pos2x(docstring const & s, int pos, bool rtl, double ws) const override;
79         int x2pos(docstring const & s, int & x, bool rtl, double ws) const override;
80         int breakAt(docstring const & s, int & x, bool rtl, bool force) const override;
81         Dimension const dimension(char_type c) const override;
82
83         void rectText(docstring const & str,
84                 int & width,
85                 int & ascent,
86                 int & descent) const override;
87         void buttonText(docstring const & str,
88                 const int offset,
89                 int & width,
90                 int & ascent,
91                 int & descent) const override;
92
93         int countExpanders(docstring const & str) const override;
94         ///
95         int width(QString const & str) const;
96
97         /// Return a pointer to a cached QTextLayout object
98         std::shared_ptr<QTextLayout const>
99         getTextLayout(docstring const & s, bool const rtl,
100                       double const wordspacing) const;
101
102 private:
103
104         std::pair<int, int> breakAt_helper(docstring const & s, int const x,
105                                            bool const rtl, bool const force) const;
106
107         /// The font
108         QFont font_;
109
110         /// Metrics on the font
111         QFontMetrics metrics_;
112
113         /// Slope of italic font
114         double slope_;
115
116         /// Cache of char widths
117         mutable QHash<char_type, int> width_cache_;
118         /// Cache of string widths
119         mutable Cache<docstring, int> strwidth_cache_;
120         /// Cache for breakAt
121         mutable Cache<BreakAtKey, std::pair<int, int>> breakat_cache_;
122         /// Cache for QTextLayout
123         mutable Cache<TextLayoutKey, std::shared_ptr<QTextLayout>> qtextlayout_cache_;
124
125         struct AscendDescend {
126                 int ascent;
127                 int descent;
128         };
129         /// Cache of char ascends and descends
130         mutable QHash<char_type, AscendDescend> metrics_cache_;
131         /// fill in \c metrics_cache_ at specified value.
132         AscendDescend const fillMetricsCache(char_type) const;
133
134         /// Cache of char left bearings
135         mutable QHash<char_type, int> lbearing_cache_;
136         /// Cache of char right bearings
137         mutable QHash<char_type, int> rbearing_cache_;
138
139 };
140
141 } // namespace frontend
142 } // namespace lyx
143
144 #endif // GUI_FONT_METRICS_H