2 * \file xftFontLoader.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "xftFontLoader.h"
17 #include "lyxrc.h" // lyxrc.font_*
18 #include "BufferView.h"
21 #include "frontends/LyXView.h"
22 #include "frontends/lyx_gui.h"
24 #include "support/systemcall.h"
25 #include "support/filetools.h"
27 #include <cmath> // fabs()
34 // The global fontLoader
35 xftFontLoader fontLoader;
38 // Initialize font loader
39 xftFontLoader::xftFontLoader()
44 // Destroy font loader
45 xftFontLoader::~xftFontLoader()
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()
61 void xftFontLoader::unload()
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;
76 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
80 case LyXFont::ROMAN_FAMILY:
81 ffamily = lyxrc.roman_font_name;
83 case LyXFont::SANS_FAMILY:
84 ffamily = lyxrc.sans_font_name;
86 case LyXFont::TYPEWRITER_FAMILY:
87 ffamily = lyxrc.typewriter_font_name;
89 case LyXFont::CMR_FAMILY:
92 case LyXFont::CMSY_FAMILY:
95 case LyXFont::CMM_FAMILY:
98 case LyXFont::CMEX_FAMILY:
101 case LyXFont::MSA_FAMILY:
104 case LyXFont::MSB_FAMILY:
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)
123 // Normal font. Let's search for an existing name that matches.
127 double fsize = lyxrc.font_sizes[size] * lyxrc.zoom / 100.0;
128 XftPattern *fpat = XftPatternCreate();
130 ffamily = familyString(family);
132 case LyXFont::MEDIUM_SERIES:
133 fweight = XFT_WEIGHT_MEDIUM;
135 case LyXFont::BOLD_SERIES:
136 fweight = XFT_WEIGHT_BOLD;
139 fweight = XFT_WEIGHT_MEDIUM;
144 case LyXFont::UP_SHAPE:
145 case LyXFont::SMALLCAPS_SHAPE:
146 fslant = XFT_SLANT_ROMAN;
148 case LyXFont::ITALIC_SHAPE:
149 fslant = XFT_SLANT_ITALIC;
151 case LyXFont::SLANTED_SHAPE:
152 fslant = XFT_SLANT_OBLIQUE;
155 fslant = XFT_SLANT_ROMAN;
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);
167 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
168 LyXFont::FONT_SERIES series,
169 LyXFont::FONT_SHAPE shape,
170 LyXFont::FONT_SIZE size)
172 XftPattern * fpat = getFontPattern(family, series, shape, size);
174 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
176 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
177 fonts_[family][series][shape][size] = font;
182 bool xftFontLoader::available(LyXFont const & f)
184 if (!lyx_gui::use_gui)
187 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
188 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
190 LyXFont::FONT_FAMILY family = f.family();
191 if (cache_set[family])
192 return cache[family];
193 cache_set[family] = true;
195 string const ffamily = familyString(family);
197 cache_set[family] = true;
198 XftPattern * fpat = XftPatternCreate();
199 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
201 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
203 XftPatternDestroy(fpat);
205 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
206 if (ffamily == familyM) {
207 cache[family] = true;
210 // We don't need to set cache[family] to false, as it
211 // is initialized to false;
214 // We don't care about non-symbol fonts