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/lstrings.h"
30 #include "support/systemcall.h"
31 #include "support/filetools.h"
33 #include <cmath> // fabs()
36 using lyx::support::strToDbl;
41 // The global fontLoader
42 xftFontLoader fontLoader;
45 // Initialize font loader
46 xftFontLoader::xftFontLoader()
51 // Destroy font loader
52 xftFontLoader::~xftFontLoader()
58 // Update fonts after zoom, dpi, font names, or norm change
59 // For now, we just ditch all fonts we have. Later, we should
60 // reuse the ones that are already loaded.
61 void xftFontLoader::update()
68 void xftFontLoader::unload()
71 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
72 for (int i2 = 0; i2 < 2; ++i2)
73 for (int i3 = 0; i3 < 4; ++i3)
74 for (int i4 = 0; i4 < 10; ++i4) {
75 if (fonts_[i1][i2][i3][i4]){
76 XftFontClose(getDisplay(), fonts_[i1][i2][i3][i4]);
77 fonts_[i1][i2][i3][i4] = 0;
83 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
87 case LyXFont::ROMAN_FAMILY:
88 ffamily = lyxrc.roman_font_name;
90 case LyXFont::SANS_FAMILY:
91 ffamily = lyxrc.sans_font_name;
93 case LyXFont::TYPEWRITER_FAMILY:
94 ffamily = lyxrc.typewriter_font_name;
96 case LyXFont::CMR_FAMILY:
99 case LyXFont::CMSY_FAMILY:
102 case LyXFont::CMM_FAMILY:
105 case LyXFont::CMEX_FAMILY:
108 case LyXFont::MSA_FAMILY:
111 case LyXFont::MSB_FAMILY:
123 /* Takes care of finding which font that can match the given request. Tries
124 different alternatives. */
125 XftPattern * xftFontLoader::getFontPattern(LyXFont::FONT_FAMILY family,
126 LyXFont::FONT_SERIES series,
127 LyXFont::FONT_SHAPE shape,
128 LyXFont::FONT_SIZE size)
130 // Normal font. Let's search for an existing name that matches.
134 double fsize = strToDbl(lyxrc.font_sizes[size]) * lyxrc.zoom / 100.0;
135 XftPattern *fpat = XftPatternCreate();
137 ffamily = familyString(family);
139 case LyXFont::MEDIUM_SERIES:
140 fweight = XFT_WEIGHT_MEDIUM;
142 case LyXFont::BOLD_SERIES:
143 fweight = XFT_WEIGHT_BOLD;
146 fweight = XFT_WEIGHT_MEDIUM;
151 case LyXFont::UP_SHAPE:
152 case LyXFont::SMALLCAPS_SHAPE:
153 fslant = XFT_SLANT_ROMAN;
155 case LyXFont::ITALIC_SHAPE:
156 fslant = XFT_SLANT_ITALIC;
158 case LyXFont::SLANTED_SHAPE:
159 fslant = XFT_SLANT_OBLIQUE;
162 fslant = XFT_SLANT_ROMAN;
165 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
166 XftPatternAddInteger(fpat, XFT_WEIGHT, fweight);
167 XftPatternAddInteger(fpat, XFT_SLANT, fslant);
168 XftPatternAddDouble(fpat, XFT_SIZE, fsize);
174 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
175 LyXFont::FONT_SERIES series,
176 LyXFont::FONT_SHAPE shape,
177 LyXFont::FONT_SIZE size)
179 XftPattern * fpat = getFontPattern(family, series, shape, size);
181 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
183 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
184 fonts_[family][series][shape][size] = font;
189 bool xftFontLoader::available(LyXFont const & f)
191 if (!lyx_gui::use_gui)
194 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
195 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
197 LyXFont::FONT_FAMILY family = f.family();
198 if (cache_set[family])
199 return cache[family];
200 cache_set[family] = true;
202 string const ffamily = familyString(family);
204 cache_set[family] = true;
205 XftPattern * fpat = XftPatternCreate();
206 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
208 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
210 XftPatternDestroy(fpat);
212 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
213 if (ffamily == familyM) {
214 cache[family] = true;
217 // We don't need to set cache[family] to false, as it
218 // is initialized to false;
221 // We don't care about non-symbol fonts