]> git.lyx.org Git - lyx.git/blob - src/FontLoader.C
add bindings for latin charsets, use self-insert as intended from the beginning
[lyx.git] / src / FontLoader.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1997 Asger Alstrup
8  *           and the LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13 #include <cmath>        // fabs()
14
15 #ifdef __GNUG__
16 #pragma implementation "FontLoader.h"
17 #endif
18
19 #include "gettext.h"
20 #include "FontLoader.h"
21 #include "FontInfo.h"
22 #include "debug.h"
23 #include "lyxrc.h"      // lyxrc.font_*
24 #include "BufferView.h"
25 #include "LyXView.h"
26
27 using std::endl;
28
29 extern BufferView * current_view;
30
31
32 // The global fontloader
33 FontLoader fontloader;
34
35
36 // Initialize font loader
37 FontLoader::FontLoader()
38 {
39         reset();
40 }
41
42
43 // Destroy font loader
44 FontLoader::~FontLoader()
45 {
46         unload();
47 }
48
49
50 // Update fonts after zoom, dpi, font names, or norm change
51 // For now, we just ditch all fonts we have. Later, we should
52 // reuse the ones that are already loaded.
53 void FontLoader::update()
54 {
55         unload();
56 }
57
58
59 // Reset font loader
60 void FontLoader::reset()
61 {
62         // Clear font infos, font structs and font metrics
63         for (int i1 = 0; i1 < 4; ++i1)
64                 for (int i2 = 0; i2 < 2; ++i2)
65                         for (int i3 = 0; i3 < 4; ++i3) {
66                                 fontinfo[i1][i2][i3] = 0;
67                                 for (int i4 = 0; i4<10; ++i4) {
68                                         fontstruct[i1][i2][i3][i4] = 0;
69                                 }
70                         }
71 }
72
73
74 // Unload all fonts
75 void FontLoader::unload() 
76 {
77         // Unload all fonts
78         for (int i1 = 0; i1 < 4; ++i1)
79                 for (int i2 = 0; i2 < 2; ++i2)
80                         for (int i3 = 0; i3 < 4; ++i3) {
81                                 if (fontinfo[i1][i2][i3]) {
82                                         delete fontinfo[i1][i2][i3];
83                                         fontinfo[i1][i2][i3] = 0;
84                                 }
85                                 for (int i4 = 0; i4 < 10; ++i4) {
86                                         if (fontstruct[i1][i2][i3][i4]) {
87                                                 XFreeFont(fl_get_display(), fontstruct[i1][i2][i3][i4]);
88                                                 fontstruct[i1][i2][i3][i4] = 0;
89                                         }
90                                 }
91                         }
92 }
93
94
95 // Get font info
96 /* Takes care of finding which font that can match the given request. Tries
97 different alternatives. */
98 void FontLoader::getFontinfo(LyXFont::FONT_FAMILY family, 
99                              LyXFont::FONT_SERIES series, 
100                              LyXFont::FONT_SHAPE shape)
101 {
102         // Do we have the font info already?
103         if (fontinfo[family][series][shape] != 0)
104                 return;
105
106         // Special code for the symbol family
107         if (family == LyXFont::SYMBOL_FAMILY){
108                 fontinfo[family][series][shape] = new FontInfo("-*-symbol-*");
109                 return;
110         }
111
112         // Normal font. Let's search for an existing name that matches.
113         string ffamily;
114         string fseries;
115         string fshape;
116         string norm = lyxrc.font_norm;
117         string fontname;
118
119         FontInfo * fi = new FontInfo();
120         fontinfo[family][series][shape] = fi;
121
122         for (int cfam = 0; cfam < 2; ++cfam) {
123                 // Determine family name
124                 switch (family) {
125                 case LyXFont::ROMAN_FAMILY:
126                         switch (cfam) {
127                         case 0: ffamily = lyxrc.roman_font_name; break;
128                         case 1: ffamily = "-*-times";
129                         default: cfam = 100;
130                         }
131                         break;
132                 case LyXFont::SANS_FAMILY:
133                         switch (cfam) {
134                         case 0: ffamily = lyxrc.sans_font_name; break;
135                         case 1: ffamily = "-*-helvetica";
136                         default: cfam = 100;
137                         }
138                         break;
139                 case LyXFont::TYPEWRITER_FAMILY:
140                         switch (cfam) {
141                         case 0: ffamily = lyxrc.typewriter_font_name; break;
142                         case 1: ffamily = "-*-courier";
143                         default: cfam = 100;
144                         }
145                         break;
146                 default: ;
147                 }
148
149                 for (int cser = 0; cser < 4; ++cser) {
150                         // Determine series name
151                         switch (series) {
152                         case LyXFont::MEDIUM_SERIES:
153                                 switch (cser) {
154                                 case 0: fseries = "-medium"; break;
155                                 case 1: fseries = "-book"; break;
156                                 case 2: fseries = "-light";
157                                 default: cser = 100;
158                                 }
159                                 break;
160                         case LyXFont::BOLD_SERIES:
161                                 switch (cser) {
162                                 case 0: fseries = "-bold"; break;
163                                 case 1: fseries = "-black"; break;
164                                 case 2: fseries = "-demi"; break;
165                                 case 3: fseries = "-demibold";
166                                 default: cser = 100;
167                                 }
168                                 break;
169                         default: ;
170                         }
171
172                         for (int csha = 0; csha < 2; ++csha) {
173                                 // Determine shape name
174                                 switch (shape) {
175                                 case LyXFont::UP_SHAPE:
176                                 case LyXFont::SMALLCAPS_SHAPE:
177                                         switch (csha) {
178                                         case 0: fshape = "-r";
179                                         default: csha = 100;
180                                         }
181                                         break;
182                                 case LyXFont::ITALIC_SHAPE:
183                                         switch (csha) {
184                                         case 0: fshape = "-i"; break;
185                                         case 1: fshape = "-o";
186                                         default: csha = 100;
187                                         }
188                                         break;
189                                 case LyXFont::SLANTED_SHAPE:
190                                         switch (csha) {
191                                         case 0: fshape = "-o"; break;
192                                         case 1: fshape = "-i";
193                                         default: csha = 100;
194                                         }
195                                         break;
196                                 default: ;
197                                 }
198                                 //
199                                 fontname = ffamily + fseries + fshape +
200                                            "-normal-*-*-*-*-*-*-*-" + norm;
201                                 fi->setPattern(fontname);
202                                 if (fi->exist()) {
203                                         return;
204                                 }
205                         }
206                 }
207         }
208 }
209
210
211 // A dummy fontstruct used when there is no gui.
212 namespace {
213
214 XFontStruct dummyXFontStruct;
215 bool dummyXFontStructisGood = false;
216
217 } // namespace anon
218
219 /// Do load font
220 XFontStruct * FontLoader::doLoad(LyXFont::FONT_FAMILY family, 
221                                 LyXFont::FONT_SERIES series, 
222                                 LyXFont::FONT_SHAPE shape, 
223                                 LyXFont::FONT_SIZE size)
224 {
225         if (!lyxrc.use_gui) {
226                 if (!dummyXFontStructisGood) {
227                         // no character specific info
228                         dummyXFontStruct.per_char = 0; 
229                         // unit ascent on character displays
230                         dummyXFontStruct.ascent = 1; 
231                         // no descent on character displays
232                         dummyXFontStruct.descent = 0; 
233                         dummyXFontStructisGood = true;
234                 }
235
236                 return &dummyXFontStruct;
237         }
238
239         getFontinfo(family, series, shape);
240         int fsize = int( (lyxrc.font_sizes[size] * lyxrc.dpi * 
241                           (lyxrc.zoom/100.0) ) / 72.27 + 0.5 );
242
243         string font = fontinfo[family][series][shape]->getFontname(fsize);
244
245         if (font.empty()) {
246                 lyxerr << "No font matches request. Using 'fixed'." << endl;
247                 lyxerr << "Start LyX as 'lyx -dbg 515' to get more information." << endl;
248                 font = "fixed";
249         }
250
251         XFontStruct * fs = 0;
252
253         current_view->owner()->messagePush(_("Loading font into X-Server..."));
254
255         fs = XLoadQueryFont(fl_get_display(), font.c_str());
256         
257         if (fs == 0) {
258                 if (font == "fixed") {
259                         lyxerr << "We're doomed. Can't get 'fixed' font." << endl;
260                 } else {
261                         lyxerr << "Could not get font. Using 'fixed'." << endl;
262                         fs = XLoadQueryFont(fl_get_display(), "fixed");
263                 }
264         } else if (lyxerr.debugging(Debug::FONT)) {
265                 // Tell user the font matching
266                 LyXFont f;
267                 f.setFamily(family);
268                 f.setSeries(series);
269                 f.setShape(shape);
270                 f.setSize(size);
271                 // The rest of the attributes are not interesting
272                 f.setEmph(LyXFont::INHERIT);
273                 f.setUnderbar(LyXFont::INHERIT);
274                 f.setNoun(LyXFont::INHERIT);
275                 f.setLatex(LyXFont::INHERIT);
276                 f.setColor(LColor::inherit);
277                 lyxerr << "Font '" << f.stateText(0) 
278                        << "' matched by\n" << font << endl;
279         }
280
281         current_view->owner()->messagePop();
282
283         fontstruct[family][series][shape][size] = fs;
284         return fs;
285 }