]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.h
4ca34b92daa8d413ee4c2129632fabe6144769be
[lyx.git] / src / frontends / qt4 / GuiFontLoader.h
1 // -*- C++ -*-
2 /**
3  * \file GuiFontLoader.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef QT4_FONTLOADER_H
13 #define QT4_FONTLOADER_H
14
15 #include "frontends/FontLoader.h"
16
17 #include "FontInfo.h"
18
19 #include "GuiFontMetrics.h"
20
21 #include "Encoding.h"
22
23 #include <QFont>
24
25 #include <boost/assert.hpp>
26 #include <boost/scoped_ptr.hpp>
27
28 namespace lyx {
29 namespace frontend {
30
31 /**
32  * Qt font loader for LyX. Matches Fonts against
33  * actual QFont instances, and also caches metrics.
34  */
35 class GuiFontInfo
36 {
37 public:
38         GuiFontInfo(FontInfo const & f);
39
40         /// The font instance
41         QFont font;
42         /// Metrics on the font
43         boost::scoped_ptr<GuiFontMetrics> metrics;
44 };
45
46
47 /// Hold info about a particular font
48 class GuiFontLoader : public FontLoader
49 {
50 public:
51         ///
52         GuiFontLoader();
53
54         /// Destructor
55         virtual ~GuiFontLoader() {}
56
57         virtual void update();
58         virtual bool available(FontInfo const & f);
59         inline virtual FontMetrics const & metrics(FontInfo const & f) {
60                 return *fontinfo(f).metrics.get();
61         }
62
63         /// Get the QFont for this FontInfo
64         QFont const & get(FontInfo const & f) {
65                 return fontinfo(f).font;
66         }
67
68
69         /// Get font info (font + metrics) for the given LyX font.
70         GuiFontInfo & fontinfo(FontInfo const & f) {
71                 BOOST_ASSERT(f.family() < NUM_FAMILIES);
72                 BOOST_ASSERT(f.series() < 2);
73                 BOOST_ASSERT(f.realShape() < 4);
74                 BOOST_ASSERT(f.size() < 10);
75                 // fi is a reference to the pointer type (GuiFontInfo *) in the
76                 // fontinfo_ table.
77                 GuiFontInfo * & fi =
78                         fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
79                 if (!fi)
80                         fi = new GuiFontInfo(f);
81                 return *fi;
82         }
83
84 private:
85         /// BUTT ugly !
86         GuiFontInfo * fontinfo_[NUM_FAMILIES][2][4][10];
87 };
88
89
90 } // namespace frontend
91 } // namespace lyx
92
93 #endif // QT4_FONTLOADER_H