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