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