]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qfont_loader.h
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / qfont_loader.h
1 // -*- C++ -*-
2 /**
3  * \file qfont_loader.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 QFONTLOADER_H
13 #define QFONTLOADER_H
14
15 #include "encoding.h"
16 #include "lyxfont.h"
17
18 #include <QFont>
19 #include <QFontMetrics>
20
21 #if QT_VERSION < 0x030100
22 #define USE_LYX_FONTCACHE
23 #endif
24
25 #if defined(USE_LYX_FONTCACHE)
26 #include <map>
27 #endif
28
29 /**
30  * Qt font loader for LyX. Matches LyXFonts against
31  * actual QFont instances, and also caches metrics.
32  */
33 class QLFontInfo {
34 public:
35         QLFontInfo(LyXFont const & f);
36
37         /// Return pixel width for the given unicode char
38         int width(Uchar val) const;
39
40         /// The font instance
41         QFont font;
42         /// Metrics on the font
43         QFontMetrics metrics;
44
45 #if defined(USE_LYX_FONTCACHE)
46         typedef std::map<Uchar, int> WidthCache;
47         /// Cache of char widths
48         WidthCache widthcache;
49 #endif
50 };
51
52
53 /// Hold info about a particular font
54 class FontLoader {
55 public:
56         ///
57         FontLoader();
58
59         /// Update fonts after zoom, dpi, font names, or norm change
60         void update();
61
62         /// Do we have anything matching?
63         bool available(LyXFont const & f);
64
65         /// Get the QFont for this LyXFont
66         QFont const & get(LyXFont const & f) {
67                 return fontinfo(f).font;
68         }
69
70         /// Get the QFont metrics for this LyXFont
71         QFontMetrics const & metrics(LyXFont const & f) {
72                 return fontinfo(f).metrics;
73         }
74
75         /// Called before QApplication is initialized
76         static void initFontPath();
77
78         /// Called the first time when available() can't load a symbol font
79         static void addToFontPath();
80
81         /// Get font info (font + metrics) for the given LyX font.
82         QLFontInfo & fontinfo(LyXFont const & f) {
83                 QLFontInfo * & fi =
84                         fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
85                 if (!fi)
86                         fi = new QLFontInfo(f);
87                 return *fi;
88         }
89
90 private:
91         /// BUTT ugly !
92         QLFontInfo * fontinfo_[LyXFont::NUM_FAMILIES][2][4][10];
93 };
94
95 extern FontLoader fontloader;
96
97 #endif // QFONT_LOADER_H