]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
Improve support for on screen length calculation
[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 <map>
20
21 #include <QFont>
22 #include <QFontMetrics>
23 #include <QHash>
24
25 namespace lyx {
26 namespace frontend {
27
28 class GuiFontMetrics : public FontMetrics
29 {
30 public:
31         GuiFontMetrics(QFont const & font);
32
33         virtual ~GuiFontMetrics() {}
34
35         virtual int maxAscent() const;
36         virtual int maxDescent() const;
37         virtual Dimension const defaultDimension() const;
38         virtual int em() const;
39         virtual int width(char_type c) const;
40         virtual int ascent(char_type c) const;
41         virtual int descent(char_type c) const;
42         virtual int lbearing(char_type c) const;
43         virtual int rbearing(char_type c) const;
44         virtual int width(docstring const & s) const;
45         virtual int signedWidth(docstring const & s) const;
46         virtual int pos2x(docstring const & s, int pos, bool rtl) const;
47         virtual int x2pos(docstring const & s, int & x, bool rtl) const;
48         virtual Dimension const dimension(char_type c) const;
49
50         virtual void rectText(docstring const & str,
51                 int & width,
52                 int & ascent,
53                 int & descent) const;
54         virtual void buttonText(docstring const & str,
55                 int & width,
56                 int & ascent,
57                 int & descent) const;
58         ///
59         int width(QString const & str) const;
60
61 private:
62         /// The font
63         QFont font_;
64
65         /// Metrics on the font
66         QFontMetrics metrics_;
67
68         /// Cache of char widths
69         mutable QHash<char_type, int> width_cache_;
70
71         /// Cache of string widths
72         /// FIXME Try to use a QHash (this requires to define qHash(docstring))
73         mutable std::map<docstring, int> strwidth_cache_;
74
75         struct AscendDescend {
76                 int ascent;
77                 int descent;
78         };
79         /// Cache of char ascends and descends
80         mutable QHash<char_type, AscendDescend> metrics_cache_;
81         /// fill in \c metrics_cache_ at specified value.
82         AscendDescend const fillMetricsCache(char_type) const;
83
84         /// Cache of char right bearings
85         mutable QHash<char_type, int> rbearing_cache_;
86 };
87
88 } // namespace frontend
89 } // namespace lyx
90
91 #endif // GUI_FONT_METRICS_H