]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/qfont_loader.h
eadf2b46cc350e3929fa7b6cdd36fe674da4f049
[lyx.git] / src / frontends / qt3 / 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.h>
19 #include <qfontmetrics.h>
20
21 #if QT_VERSION < 0x030100 || defined(Q_WS_MACX)
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);
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 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                 // fi is a reference to the pointer type (QLFontInfo *) in the
84                 // fontinfo_ table.
85                 QLFontInfo * & fi =
86                         fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
87                 if (!fi)
88                         fi = new QLFontInfo(f);
89                 return *fi;
90         }
91
92 private:
93         /// BUTT ugly !
94         QLFontInfo * fontinfo_[LyXFont::NUM_FAMILIES][2][4][10];
95 };
96
97 extern FontLoader fontloader;
98
99 #endif // QFONT_LOADER_H