]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
* qt_helpers.h:
[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 // Starting with version 3.1.0, Qt/X11 does its own caching of
23 // character width, so it is not necessary to provide ours.
24 #if defined(Q_WS_MACX) || defined(Q_WS_WIN32)
25 #define USE_LYX_FONTCACHE
26 #endif
27
28 namespace lyx {
29 namespace frontend {
30
31 class GuiFontMetrics: public FontMetrics
32 {
33 public:
34
35         GuiFontMetrics(QFont const & font);
36         GuiFontMetrics(QFont const & font, QFont const & smallcaps_font);
37
38         virtual ~GuiFontMetrics() {}
39
40         virtual int maxAscent() const;
41         virtual int maxDescent() const;
42 #ifndef USE_LYX_FONTCACHE
43         virtual int width(char_type c) const {
44                 return metrics_.width(QChar(static_cast<short int>(c)));
45         }
46 #else
47         virtual int width(char_type c) const;
48 #endif
49         virtual int ascent(char_type c) const;
50         virtual int descent(char_type c) const;
51         virtual int lbearing(char_type c) const;
52         virtual int rbearing(char_type c) const;
53         virtual int width(char_type const * s, size_t n) const;
54         virtual int signedWidth(docstring const & s) const;
55         virtual void rectText(docstring const & str,
56                 int & width,
57                 int & ascent,
58                 int & descent) const;
59         virtual void buttonText(docstring const & str,
60                 int & width,
61                 int & ascent,
62                 int & descent) const;
63         ///
64         int width(QString const & str) const;
65
66 private:
67         int smallcapsWidth(QString const & s) const;
68
69         /// Metrics on the font
70         QFontMetrics metrics_;
71         QFontMetrics smallcaps_metrics_;
72
73         bool smallcaps_shape_;
74
75 #ifdef USE_LYX_FONTCACHE
76
77         /// Cache of char widths
78         /** This cache adds 20Mo of memory to the LyX executable when
79         * loading UserGuide.lyx which contains a good number of fonts. If
80         * this turns out to be too much, we can switch to a \c QHash based
81         * solution.
82         **/
83         mutable QHash<char_type, int> width_cache_;
84
85         struct AscendDescend {
86                 short int ascent;
87                 short int descent;
88         };
89         mutable QHash<char_type, AscendDescend> metrics_cache_;
90         /// fill in \c metrics_cache_ at specified value.
91         void fillMetricsCache(char_type) const;
92
93 #endif // USE_LYX_FONTCACHE
94 };
95
96 } // namespace frontend
97 } // namespace lyx
98
99 #endif // QT4_FONT_METRICS_H