]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/xftFontLoader.C
Display accelerator (binding) labels in menus.
[lyx.git] / src / frontends / gtk / xftFontLoader.C
1 /**
2  * \file xftFontLoader.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "xftFontLoader.h"
14 #include "FontInfo.h"
15 #include "gettext.h"
16 #include "debug.h"
17 #include "lyxrc.h"      // lyxrc.font_*
18 #include "BufferView.h"
19 #include "GtkmmX.h"
20
21 #include "frontends/LyXView.h"
22 #include "frontends/lyx_gui.h"
23
24 #include "support/systemcall.h"
25 #include "support/filetools.h"
26
27 #include <cmath>        // fabs()
28 #include <vector>
29
30
31 using std::endl;
32 using std::string;
33
34 // The global fontLoader
35 xftFontLoader fontLoader;
36
37
38 // Initialize font loader
39 xftFontLoader::xftFontLoader()
40 {
41 }
42
43
44 // Destroy font loader
45 xftFontLoader::~xftFontLoader()
46 {
47         unload();
48 }
49
50
51 // Update fonts after zoom, dpi, font names, or norm change
52 // For now, we just ditch all fonts we have. Later, we should
53 // reuse the ones that are already loaded.
54 void xftFontLoader::update()
55 {
56         unload();
57 }
58
59
60 // Unload all fonts
61 void xftFontLoader::unload()
62 {
63         // Unload all fonts
64         for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
65                 for (int i2 = 0; i2 < 2; ++i2)
66                         for (int i3 = 0; i3 < 4; ++i3)
67                                 for (int i4 = 0; i4 < 10; ++i4) {
68                                         if (fonts_[i1][i2][i3][i4]){
69                                                 XftFontClose(getDisplay(), fonts_[i1][i2][i3][i4]);
70                                                 fonts_[i1][i2][i3][i4] = 0;
71                                         }
72                                 }
73 }
74
75
76 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
77 {
78         string ffamily;
79         switch (family) {
80         case LyXFont::ROMAN_FAMILY:
81                 ffamily = lyxrc.roman_font_name;
82                 break;
83         case LyXFont::SANS_FAMILY:
84                 ffamily = lyxrc.sans_font_name;
85                 break;
86         case LyXFont::TYPEWRITER_FAMILY:
87                 ffamily = lyxrc.typewriter_font_name;
88                 break;
89         case LyXFont::CMR_FAMILY:
90                 ffamily = "cmr10";
91                 break;
92         case LyXFont::CMSY_FAMILY:
93                 ffamily = "cmsy10";
94                 break;
95         case LyXFont::CMM_FAMILY:
96                 ffamily = "cmmi10";
97                 break;
98         case LyXFont::CMEX_FAMILY:
99                 ffamily = "cmex10";
100                 break;
101         case LyXFont::MSA_FAMILY:
102                 ffamily = "msam10";
103                 break;
104         case LyXFont::MSB_FAMILY:
105                 ffamily = "msbm10";
106                 break;
107         default:
108                 ffamily = "Sans";
109                 break;
110         }
111         return ffamily;
112 }
113
114
115 // Get font pattern
116 /* Takes care of finding which font that can match the given request. Tries
117 different alternatives. */
118 XftPattern * xftFontLoader::getFontPattern(LyXFont::FONT_FAMILY family,
119                                           LyXFont::FONT_SERIES series,
120                                           LyXFont::FONT_SHAPE shape,
121                                           LyXFont::FONT_SIZE size)
122 {
123         // Normal font. Let's search for an existing name that matches.
124         string ffamily;
125         int fweight;
126         int fslant;
127         double fsize = lyxrc.font_sizes[size] * lyxrc.zoom / 100.0;
128         XftPattern *fpat = XftPatternCreate();
129
130         ffamily = familyString(family);
131         switch (series) {
132         case LyXFont::MEDIUM_SERIES:
133                 fweight = XFT_WEIGHT_MEDIUM;
134                 break;
135         case LyXFont::BOLD_SERIES:
136                 fweight = XFT_WEIGHT_BOLD;
137                 break;
138         default:
139                 fweight = XFT_WEIGHT_MEDIUM;
140                 break;
141         }
142
143         switch (shape) {
144         case LyXFont::UP_SHAPE:
145         case LyXFont::SMALLCAPS_SHAPE:
146                 fslant = XFT_SLANT_ROMAN;
147                 break;
148         case LyXFont::ITALIC_SHAPE:
149                 fslant = XFT_SLANT_ITALIC;
150                 break;
151         case LyXFont::SLANTED_SHAPE:
152                 fslant = XFT_SLANT_OBLIQUE;
153                 break;
154         default:
155                 fslant = XFT_SLANT_ROMAN;
156                 break;
157         }
158         XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
159         XftPatternAddInteger(fpat, XFT_WEIGHT, fweight);
160         XftPatternAddInteger(fpat, XFT_SLANT, fslant);
161         XftPatternAddDouble(fpat, XFT_SIZE, fsize);
162         return fpat;
163 }
164
165
166 /// Do load font
167 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
168                                LyXFont::FONT_SERIES series,
169                                LyXFont::FONT_SHAPE shape,
170                                LyXFont::FONT_SIZE size)
171 {
172         XftPattern * fpat = getFontPattern(family, series, shape, size);
173         XftResult result;
174         XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
175                                           fpat, &result);
176         XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
177         fonts_[family][series][shape][size] = font;
178         return font;
179 }
180
181
182 bool xftFontLoader::available(LyXFont const & f)
183 {
184         if (!lyx_gui::use_gui)
185                 return false;
186
187         static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
188         static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
189
190         LyXFont::FONT_FAMILY family = f.family();
191         if (cache_set[family])
192                 return cache[family];
193         cache_set[family] = true;
194
195         string const ffamily = familyString(family);
196         if (isSpecial(f)) {
197                 cache_set[family] = true;
198                 XftPattern * fpat = XftPatternCreate();
199                 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
200                 XftResult result;
201                 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
202                                                   fpat, &result);
203                 XftPatternDestroy(fpat);
204                 char * familyM;
205                 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
206                 if (ffamily == familyM) {
207                         cache[family] = true;
208                         return true;
209                 }
210                 // We don't need to set cache[family] to false, as it
211                 // is initialized to false;
212                 return false;
213         }
214         // We don't care about non-symbol fonts
215         return false;
216 }