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 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
18 #include "xftFontLoader.h"
22 #include "lyxrc.h" // lyxrc.font_*
23 #include "BufferView.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/lyx_gui.h"
29 #include "support/convert.h"
30 #include "support/lstrings.h"
31 #include "support/systemcall.h"
32 #include "support/filetools.h"
34 #include <cmath> // fabs()
40 // The global fontLoader
41 xftFontLoader fontLoader;
44 // Initialize font loader
45 xftFontLoader::xftFontLoader()
50 // Destroy font loader
51 xftFontLoader::~xftFontLoader()
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()
67 void xftFontLoader::unload()
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;
82 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
86 case LyXFont::ROMAN_FAMILY:
87 ffamily = lyxrc.roman_font_name;
89 case LyXFont::SANS_FAMILY:
90 ffamily = lyxrc.sans_font_name;
92 case LyXFont::TYPEWRITER_FAMILY:
93 ffamily = lyxrc.typewriter_font_name;
95 case LyXFont::CMR_FAMILY:
98 case LyXFont::CMSY_FAMILY:
101 case LyXFont::CMM_FAMILY:
104 case LyXFont::CMEX_FAMILY:
107 case LyXFont::MSA_FAMILY:
110 case LyXFont::MSB_FAMILY:
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)
129 // Normal font. Let's search for an existing name that matches.
133 double fsize = convert<double>(lyxrc.font_sizes[size]) * lyxrc.zoom / 100.0;
134 XftPattern *fpat = XftPatternCreate();
136 ffamily = familyString(family);
138 case LyXFont::MEDIUM_SERIES:
139 fweight = XFT_WEIGHT_MEDIUM;
141 case LyXFont::BOLD_SERIES:
142 fweight = XFT_WEIGHT_BOLD;
145 fweight = XFT_WEIGHT_MEDIUM;
150 case LyXFont::UP_SHAPE:
151 case LyXFont::SMALLCAPS_SHAPE:
152 fslant = XFT_SLANT_ROMAN;
154 case LyXFont::ITALIC_SHAPE:
155 fslant = XFT_SLANT_ITALIC;
157 case LyXFont::SLANTED_SHAPE:
158 fslant = XFT_SLANT_OBLIQUE;
161 fslant = XFT_SLANT_ROMAN;
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);
173 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
174 LyXFont::FONT_SERIES series,
175 LyXFont::FONT_SHAPE shape,
176 LyXFont::FONT_SIZE size)
178 XftPattern * fpat = getFontPattern(family, series, shape, size);
180 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
182 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
183 fonts_[family][series][shape][size] = font;
188 bool xftFontLoader::available(LyXFont const & f)
190 if (!lyx_gui::use_gui)
193 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
194 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
196 LyXFont::FONT_FAMILY family = f.family();
197 if (cache_set[family])
198 return cache[family];
199 cache_set[family] = true;
201 string const ffamily = familyString(family);
203 cache_set[family] = true;
204 XftPattern * fpat = XftPatternCreate();
205 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
207 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
209 XftPatternDestroy(fpat);
211 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
212 if (ffamily == familyM) {
213 cache[family] = true;
216 // We don't need to set cache[family] to false, as it
217 // is initialized to false;
220 // We don't care about non-symbol fonts