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