]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/qfont_loader.C
the qtclean-2 patch with some very small changes
[lyx.git] / src / frontends / qt2 / qfont_loader.C
1 /**
2  * \file qfont_loader.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "qfont_loader.h"
19 #include "gettext.h"
20 #include "debug.h"
21 #include "lyxrc.h"
22 #include "BufferView.h"
23
24 #include <qglobal.h>
25 #if QT_VERSION < 0x030000
26 #include "support/lstrings.h"
27 #endif
28
29 using std::endl;
30
31
32 qfont_loader::qfont_loader()
33 {
34 }
35
36
37 qfont_loader::~qfont_loader()
38 {
39 }
40
41
42 void qfont_loader::update()
43 {
44         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1) {
45                 for (int i2 = 0; i1 < 2; ++i2) {
46                         for (int i3 = 0; i1 < 4; ++i3) {
47                                 for (int i4 = 0; i1 < 10; ++i4) {
48                                         fontinfo_[i1][i2][i3][i4].reset(0);
49                                 }
50                         }
51                 }
52         }
53 }
54
55
56 QFont const & qfont_loader::get(LyXFont const & f)
57 {
58         QFont const & ret(getfontinfo(f)->font);
59
60         if (lyxerr.debugging(Debug::FONT)) {
61                 lyxerr[Debug::FONT] << "Font '" << f.stateText(0)
62                         << "' matched by\n" << ret.rawName() << endl;
63         }
64
65         lyxerr[Debug::FONT] << "The font has size: "
66                             << ret.pointSizeFloat() << endl;
67
68         return ret;
69 }
70
71
72 qfont_loader::font_info::font_info(LyXFont const & f)
73         : metrics(font)
74 {
75         switch (f.family()) {
76                 case LyXFont::SYMBOL_FAMILY:
77                         font.setRawName("-*-symbol-*-*-*-*-*-*-*-*-*-*-adobe-fontspecific");
78                         break;
79                 case LyXFont::CMR_FAMILY:
80                         font.setRawName("-*-cmr10-medium-*-*-*-*-*-*-*-*-*-*-*");
81                         break;
82                 case LyXFont::CMSY_FAMILY:
83                         font.setRawName("-*-cmsy10-*-*-*-*-*-*-*-*-*-*-*-*");
84                         break;
85                 case LyXFont::CMM_FAMILY:
86                         font.setRawName("-*-cmmi10-medium-*-*-*-*-*-*-*-*-*-*-*");
87                         break;
88                 case LyXFont::CMEX_FAMILY:
89                         font.setRawName("-*-cmex10-*-*-*-*-*-*-*-*-*-*-*-*");
90                         break;
91                 case LyXFont::MSA_FAMILY:
92                         font.setRawName("-*-msam10-*-*-*-*-*-*-*-*-*-*-*-*");
93                         break;
94                 case LyXFont::MSB_FAMILY:
95                         font.setRawName("-*-msbm10-*-*-*-*-*-*-*-*-*-*-*-*");
96                         break;
97                 case LyXFont::EUFRAK_FAMILY:
98                         font.setRawName("-*-eufm10-medium-*-*-*-*-*-*-*-*-*-*-*");
99                         break;
100                 case LyXFont::ROMAN_FAMILY:
101                         font.setFamily("times");
102                         break;
103                 case LyXFont::SANS_FAMILY:
104                         font.setFamily("helvetica");
105                         break;
106                 case LyXFont::TYPEWRITER_FAMILY:
107                         font.setFamily("courier");
108                         break;
109         }
110
111         font.setPointSizeFloat(lyxrc.font_sizes[f.size()]
112                                * lyxrc.zoom / 100.0);
113
114         // FIXME: lyxrc, check for failure etc.
115
116         switch (f.series()) {
117                 case LyXFont::MEDIUM_SERIES:
118                         font.setWeight(QFont::Normal);
119                         break;
120                 case LyXFont::BOLD_SERIES:
121                         font.setWeight(QFont::Bold);
122                         break;
123         }
124
125         switch (f.realShape()) {
126                 case LyXFont::ITALIC_SHAPE:
127                 case LyXFont::SLANTED_SHAPE:
128                         font.setItalic(true);
129                         break;
130         }
131
132         // Is this an exact match?
133         if (font.exactMatch()) {
134                 lyxerr[Debug::FONT] << "This font is an exact match" << endl;
135         } else {
136                 lyxerr[Debug::FONT] << "This font is NOT an exact match"
137                                     << endl;
138         }
139
140         lyxerr[Debug::FONT] << "XFLD: " << font.rawName() << endl;
141
142         metrics = QFontMetrics(font);
143 }
144
145
146 qfont_loader::font_info const * qfont_loader::getfontinfo(LyXFont const & f)
147 {
148         if (!lyxrc.use_gui) {
149                 // FIXME
150         }
151
152         font_info * fi = fontinfo_[f.family()][f.series()][f.realShape()][f.size()].get();
153         if (!fi) {
154                 fi = new font_info(f);
155                 fontinfo_[f.family()][f.series()][f.realShape()][f.size()].reset(fi);
156         }
157
158         return fi;
159 }
160
161
162 bool qfont_loader::available(LyXFont const & f)
163 {
164         if (!lyxrc.use_gui)
165                 return false;
166
167 #if QT_VERSION >= 0x030000
168         return getfontinfo(f)->font.exactMatch();
169 #else
170         string tmp;
171         switch (f.family()) {
172         case LyXFont::SYMBOL_FAMILY:  tmp = "symbol"; break;
173         case LyXFont::CMR_FAMILY:     tmp = "cmr10"; break;
174         case LyXFont::CMSY_FAMILY:    tmp = "cmsy10"; break;
175         case LyXFont::CMM_FAMILY:     tmp = "cmmi10"; break;
176         case LyXFont::CMEX_FAMILY:    tmp = "cmex10"; break;
177         case LyXFont::MSA_FAMILY:     tmp = "msam10"; break;
178         case LyXFont::MSB_FAMILY:     tmp = "msbm10"; break;
179         default: break;
180         }
181         if (tmp.empty())
182                 return false;
183         else
184                 return token(getfontinfo(f)->font.rawName().latin1(), '-', 2)
185                         == tmp;
186 #endif
187 }