]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qfont_loader.h
2e782267a451516c223862acb6e889f0629609a0
[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         /// Destructor
60         ~FontLoader();
61
62         /// Update fonts after zoom, dpi, font names, or norm change
63         void update();
64
65         /// Do we have anything matching?
66         bool available(LyXFont const & f);
67
68         /// Get the QFont for this LyXFont
69         QFont const & get(LyXFont const & f) {
70                 return fontinfo(f).font;
71         }
72
73         /// Get the QFont metrics for this LyXFont
74         QFontMetrics const & metrics(LyXFont const & f) {
75                 return fontinfo(f).metrics;
76         }
77
78         /// Called before QApplication is initialized
79         static void initFontPath();
80
81         /// Called the first time when available() can't load a symbol font
82         static void addToFontPath();
83
84         /// Get font info (font + metrics) for the given LyX font.
85         QLFontInfo & fontinfo(LyXFont const & f) {
86                 QLFontInfo * & fi =
87                         fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
88                 if (!fi)
89                         fi = new QLFontInfo(f);
90                 return *fi;
91         }
92
93 private:
94         /// BUTT ugly !
95         QLFontInfo * fontinfo_[LyXFont::NUM_FAMILIES][2][4][10];
96 };
97
98 extern FontLoader fontloader;
99
100 #endif // QFONT_LOADER_H