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