]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontMetrics.h
08e6975c6cbb181509c106c90a63888060ec8a15
[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         int width(QString const & str) const;
58
59 private:
60         int smallcapsWidth(QString const & s) const;
61
62         /// Metrics on the font
63         QFontMetrics metrics_;
64         QFontMetrics smallcaps_metrics_;
65
66         bool smallcaps_shape_;
67
68 #ifndef USE_LYX_FONTCACHE
69         /// Return pixel width for the given unicode char
70         int width(unsigned short val) const { return metrics_.width(QChar(val)); }
71
72 #else
73         /// Return pixel width for the given unicode char
74         int width(unsigned short val) const;
75
76         typedef std::map<unsigned short, int> WidthCache;
77         /// Cache of char widths
78         mutable WidthCache widthcache;
79 #endif // USE_LYX_FONTCACHE
80 };
81
82 } // namespace frontend
83 } // namespace lyx
84
85 #endif // QT4_FONT_METRICS_H