]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
44ee9c38205a06fc89c837f80a89ce44b6c90047
[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 #include <map>
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         virtual int ascent(lyx::char_type c) const;
43         int descent(lyx::char_type c) const;
44         virtual int lbearing(lyx::char_type c) const;
45         virtual int rbearing(lyx::char_type c) const;
46         virtual int width(lyx::char_type const * s, size_t n) const;
47         virtual int signedWidth(lyx::docstring const & s) const;
48         virtual void rectText(lyx::docstring const & str,
49                 int & width,
50                 int & ascent,
51                 int & descent) const;
52         virtual void buttonText(lyx::docstring const & str,
53                 int & width,
54                 int & ascent,
55                 int & descent) const;
56
57 private:
58         int smallcapsWidth(QString const & s) const;
59
60         /// Metrics on the font
61         QFontMetrics metrics_;
62         QFontMetrics smallcaps_metrics_;
63
64         bool smallcaps_shape_;
65
66 #ifndef USE_LYX_FONTCACHE
67         /// Return pixel width for the given unicode char
68         int width(unsigned short val) const { return metrics_.width(QChar(val)); }
69
70 #else
71         /// Return pixel width for the given unicode char
72         int width(unsigned short val) const;
73
74         typedef std::map<unsigned short, int> WidthCache;
75         /// Cache of char widths
76         mutable WidthCache widthcache;
77 #endif // USE_LYX_FONTCACHE
78 };
79
80 } // namespace frontend
81 } // namespace lyx
82
83 #endif // QT4_FONT_METRICS_H