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