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