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"
29 #include <X11/Xft/Xft.h>
31 #include <cmath> // fabs()
38 // The global fontLoader
39 xftFontLoader fontLoader;
42 // Initialize font loader
43 xftFontLoader::xftFontLoader()
48 // Destroy font loader
49 xftFontLoader::~xftFontLoader()
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()
65 void xftFontLoader::unload()
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;
80 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
84 case LyXFont::ROMAN_FAMILY:
85 ffamily = lyxrc.roman_font_name;
87 case LyXFont::SANS_FAMILY:
88 ffamily = lyxrc.sans_font_name;
90 case LyXFont::TYPEWRITER_FAMILY:
91 ffamily = lyxrc.typewriter_font_name;
93 case LyXFont::CMR_FAMILY:
96 case LyXFont::CMSY_FAMILY:
99 case LyXFont::CMM_FAMILY:
102 case LyXFont::CMEX_FAMILY:
105 case LyXFont::MSA_FAMILY:
108 case LyXFont::MSB_FAMILY:
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)
127 // Normal font. Let's search for an existing name that matches.
131 double fsize = lyxrc.font_sizes[size] * lyxrc.zoom / 100.0;
132 XftPattern *fpat = XftPatternCreate();
134 ffamily = familyString(family);
136 case LyXFont::MEDIUM_SERIES:
137 fweight = XFT_WEIGHT_MEDIUM;
139 case LyXFont::BOLD_SERIES:
140 fweight = XFT_WEIGHT_BOLD;
143 fweight = XFT_WEIGHT_MEDIUM;
148 case LyXFont::UP_SHAPE:
149 case LyXFont::SMALLCAPS_SHAPE:
150 fslant = XFT_SLANT_ROMAN;
152 case LyXFont::ITALIC_SHAPE:
153 fslant = XFT_SLANT_ITALIC;
155 case LyXFont::SLANTED_SHAPE:
156 fslant = XFT_SLANT_OBLIQUE;
159 fslant = XFT_SLANT_ROMAN;
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);
171 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
172 LyXFont::FONT_SERIES series,
173 LyXFont::FONT_SHAPE shape,
174 LyXFont::FONT_SIZE size)
176 XftPattern * fpat = getFontPattern(family, series, shape, size);
178 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
180 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
181 fonts_[family][series][shape][size] = font;
186 bool xftFontLoader::available(LyXFont const & f)
188 if (!lyx_gui::use_gui)
191 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
192 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
194 LyXFont::FONT_FAMILY family = f.family();
195 if (cache_set[family])
196 return cache[family];
197 cache_set[family] = true;
199 string const ffamily = familyString(family);
201 cache_set[family] = true;
202 XftPattern * fpat = XftPatternCreate();
203 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
205 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
207 XftPatternDestroy(fpat);
209 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
210 if (ffamily == familyM) {
211 cache[family] = true;
214 // We don't need to set cache[family] to false, as it
215 // is initialized to false;
218 // We don't care about non-symbol fonts