]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
a3b680d66166c25f848a36f3e4578b89e24302fd
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file FontMetrics.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 QT4_FONT_METRICS_H
13 #define QT4_FONT_METRICS_H
14
15 #include "frontends/FontMetrics.h"
16
17 #include "support/docstring.h"
18
19 #include <QFontMetrics>
20 #include <QHash>
21
22 #if defined(Q_WS_MACX) || defined(Q_WS_WIN32)
23 #define USE_LYX_FONTCACHE
24 #endif
25
26 namespace lyx {
27 namespace frontend {
28
29 class GuiFontMetrics: public FontMetrics
30 {
31 public:
32
33         GuiFontMetrics(QFont const & font);
34         GuiFontMetrics(QFont const & font, QFont const & smallcaps_font);
35
36         virtual ~GuiFontMetrics() {}
37
38         virtual int maxAscent() const;
39         virtual int maxDescent() const;
40 #ifndef USE_LYX_FONTCACHE
41         virtual int width(char_type c) const {
42                 return metrics_.width(QChar(static_cast<short int>(c)));
43         }
44 #else
45         virtual int width(char_type c) const;
46 #endif
47         virtual int ascent(char_type c) const;
48         virtual int descent(char_type c) const;
49         virtual int lbearing(char_type c) const;
50         virtual int rbearing(char_type c) const;
51         virtual int width(char_type const * s, size_t n) const;
52         virtual int signedWidth(docstring const & s) const;
53         virtual void rectText(docstring const & str,
54                 int & width,
55                 int & ascent,
56                 int & descent) const;
57         virtual void buttonText(docstring const & str,
58                 int & width,
59                 int & ascent,
60                 int & descent) const;
61         ///
62         int width(QString const & str) const;
63
64 private:
65         int smallcapsWidth(QString const & s) const;
66
67         /// Metrics on the font
68         QFontMetrics metrics_;
69         QFontMetrics smallcaps_metrics_;
70
71         bool smallcaps_shape_;
72
73 #ifdef USE_LYX_FONTCACHE
74
75         /// Cache of char widths
76         mutable QHash<char_type, int> width_cache_;
77
78         struct AscendDescend {
79                 short int ascent;
80                 short int descent;
81         };
82         /// Cache of char ascends and descends
83         mutable QHash<char_type, AscendDescend> metrics_cache_;
84         /// fill in \c metrics_cache_ at specified value.
85         void fillMetricsCache(char_type) const;
86
87 #endif // USE_LYX_FONTCACHE
88 };
89
90 } // namespace frontend
91 } // namespace lyx
92
93 #endif // QT4_FONT_METRICS_H