]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiFontLoader.h
enable Font cache only for MacOSX and inline width() for other platform.
[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 "encoding.h"
18 #include "lyxfont.h"
19
20 #include <QFont>
21 #include <QFontMetrics>
22
23 // Starting with version 3.1.0, Qt/X11 does its own caching of
24 // character width, so it is not necessary to provide ours.
25 #if defined(Q_WS_MACX)
26 #define USE_LYX_FONTCACHE
27 #include <map>
28 #endif
29
30 namespace lyx {
31 namespace frontend {
32
33 /**
34  * Qt font loader for LyX. Matches LyXFonts against
35  * actual QFont instances, and also caches metrics.
36  */
37 class QLFontInfo {
38 public:
39         QLFontInfo(LyXFont const & f);
40
41         /// The font instance
42         QFont font;
43         /// Metrics on the font
44         QFontMetrics metrics;
45
46 #ifndef USE_LYX_FONTCACHE
47         /// Return pixel width for the given unicode char
48         int width(Uchar val) { return metrics.width(QChar(val)); }
49
50 #else
51         /// Return pixel width for the given unicode char
52         int width(Uchar val);
53
54 private:
55         typedef std::map<Uchar, int> WidthCache;
56         /// Cache of char widths
57         WidthCache widthcache;
58 #endif // USE_LYX_FONTCACHE
59 };
60
61
62 /// Hold info about a particular font
63 class GuiFontLoader: public FontLoader 
64 {
65 public:
66         ///
67         GuiFontLoader();
68         
69         /// Destructor
70         virtual ~GuiFontLoader();
71
72         /// Update fonts after zoom, dpi, font names, or norm change
73         virtual void update();
74
75         /// Do we have anything matching?
76         virtual bool available(LyXFont const & f);
77
78         /// Get the QFont for this LyXFont
79         QFont const & get(LyXFont const & f) {
80                 return fontinfo(f).font;
81         }
82
83         /// Get the QFont metrics for this LyXFont
84         QFontMetrics const & metrics(LyXFont const & f) {
85                 return fontinfo(f).metrics;
86         }
87
88         /// Get font info (font + metrics) for the given LyX font.
89         QLFontInfo & fontinfo(LyXFont const & f) {
90                 // fi is a reference to the pointer type (QLFontInfo *) in the
91                 // fontinfo_ table.
92                 QLFontInfo * & fi =
93                         fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
94                 if (!fi)
95                         fi = new QLFontInfo(f);
96                 return *fi;
97         }
98
99 private:
100         /// BUTT ugly !
101         QLFontInfo * fontinfo_[LyXFont::NUM_FAMILIES][2][4][10];
102 };
103
104
105 } // namespace frontend
106 } // namespace lyx
107
108 #endif // QT4_FONTLOADER_H