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 <cmath> // fabs()
15 #include <X11/Xft/Xft.h>
16 #include "xftFontLoader.h"
20 #include "lyxrc.h" // lyxrc.font_*
21 #include "BufferView.h"
22 #include "frontends/LyXView.h"
23 #include "support/systemcall.h"
24 #include "support/filetools.h"
27 #include "frontends/lyx_gui.h"
31 // The global fontLoader
32 xftFontLoader fontLoader;
35 // Initialize font loader
36 xftFontLoader::xftFontLoader()
41 // Destroy font loader
42 xftFontLoader::~xftFontLoader()
48 // Update fonts after zoom, dpi, font names, or norm change
49 // For now, we just ditch all fonts we have. Later, we should
50 // reuse the ones that are already loaded.
51 void xftFontLoader::update()
58 void xftFontLoader::unload()
61 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
62 for (int i2 = 0; i2 < 2; ++i2)
63 for (int i3 = 0; i3 < 4; ++i3)
64 for (int i4 = 0; i4 < 10; ++i4) {
65 if (fonts_[i1][i2][i3][i4]){
66 XftFontClose(getDisplay(), fonts_[i1][i2][i3][i4]);
67 fonts_[i1][i2][i3][i4] = 0;
73 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
77 case LyXFont::ROMAN_FAMILY:
78 ffamily = lyxrc.roman_font_name;
80 case LyXFont::SANS_FAMILY:
81 ffamily = lyxrc.sans_font_name;
83 case LyXFont::TYPEWRITER_FAMILY:
84 ffamily = lyxrc.typewriter_font_name;
86 case LyXFont::CMR_FAMILY:
89 case LyXFont::CMSY_FAMILY:
92 case LyXFont::CMM_FAMILY:
95 case LyXFont::CMEX_FAMILY:
98 case LyXFont::MSA_FAMILY:
101 case LyXFont::MSB_FAMILY:
113 /* Takes care of finding which font that can match the given request. Tries
114 different alternatives. */
115 XftPattern * xftFontLoader::getFontPattern(LyXFont::FONT_FAMILY family,
116 LyXFont::FONT_SERIES series,
117 LyXFont::FONT_SHAPE shape,
118 LyXFont::FONT_SIZE size)
120 // Normal font. Let's search for an existing name that matches.
124 double fsize = lyxrc.font_sizes[size] * lyxrc.zoom / 100.0;
125 XftPattern *fpat = XftPatternCreate();
127 ffamily = familyString(family);
129 case LyXFont::MEDIUM_SERIES:
130 fweight = XFT_WEIGHT_MEDIUM;
132 case LyXFont::BOLD_SERIES:
133 fweight = XFT_WEIGHT_BOLD;
136 fweight = XFT_WEIGHT_MEDIUM;
141 case LyXFont::UP_SHAPE:
142 case LyXFont::SMALLCAPS_SHAPE:
143 fslant = XFT_SLANT_ROMAN;
145 case LyXFont::ITALIC_SHAPE:
146 fslant = XFT_SLANT_ITALIC;
148 case LyXFont::SLANTED_SHAPE:
149 fslant = XFT_SLANT_OBLIQUE;
152 fslant = XFT_SLANT_ROMAN;
155 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
156 XftPatternAddInteger(fpat, XFT_WEIGHT, fweight);
157 XftPatternAddInteger(fpat, XFT_SLANT, fslant);
158 XftPatternAddDouble(fpat, XFT_SIZE, fsize);
164 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
165 LyXFont::FONT_SERIES series,
166 LyXFont::FONT_SHAPE shape,
167 LyXFont::FONT_SIZE size)
169 XftPattern *fpat = getFontPattern(family, series, shape, size);
171 XftPattern *fpat2 = XftFontMatch(getDisplay(), getScreen(),
173 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
174 fonts_[family][series][shape][size] = font;
179 bool xftFontLoader::available(LyXFont const & f)
181 if (!lyx_gui::use_gui)
184 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
185 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
187 LyXFont::FONT_FAMILY family = f.family();
188 if (cache_set[family])
189 return cache[family];
190 cache_set[family] = true;
192 string const ffamily = familyString(family);
194 cache_set[family] = true;
195 XftPattern *fpat = XftPatternCreate();
196 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
198 XftPattern *fpat2 = XftFontMatch(getDisplay(), getScreen(),
200 XftPatternDestroy(fpat);
202 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
203 if (ffamily == familyM) {
204 cache[family] = true;
207 // We don't need to set cache[family] to false, as it
208 // is initialized to false;
211 // We don't care about non-symbol fonts