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/systemcall.h"
30 #include "support/filetools.h"
32 #include <cmath> // fabs()
39 // The global fontLoader
40 xftFontLoader fontLoader;
43 // Initialize font loader
44 xftFontLoader::xftFontLoader()
49 // Destroy font loader
50 xftFontLoader::~xftFontLoader()
56 // Update fonts after zoom, dpi, font names, or norm change
57 // For now, we just ditch all fonts we have. Later, we should
58 // reuse the ones that are already loaded.
59 void xftFontLoader::update()
66 void xftFontLoader::unload()
69 for (int i1 = 0; i1 < LyXFont::NUM_FAMILIES; ++i1)
70 for (int i2 = 0; i2 < 2; ++i2)
71 for (int i3 = 0; i3 < 4; ++i3)
72 for (int i4 = 0; i4 < 10; ++i4) {
73 if (fonts_[i1][i2][i3][i4]){
74 XftFontClose(getDisplay(), fonts_[i1][i2][i3][i4]);
75 fonts_[i1][i2][i3][i4] = 0;
81 string xftFontLoader::familyString(LyXFont::FONT_FAMILY family)
85 case LyXFont::ROMAN_FAMILY:
86 ffamily = lyxrc.roman_font_name;
88 case LyXFont::SANS_FAMILY:
89 ffamily = lyxrc.sans_font_name;
91 case LyXFont::TYPEWRITER_FAMILY:
92 ffamily = lyxrc.typewriter_font_name;
94 case LyXFont::CMR_FAMILY:
97 case LyXFont::CMSY_FAMILY:
100 case LyXFont::CMM_FAMILY:
103 case LyXFont::CMEX_FAMILY:
106 case LyXFont::MSA_FAMILY:
109 case LyXFont::MSB_FAMILY:
121 /* Takes care of finding which font that can match the given request. Tries
122 different alternatives. */
123 XftPattern * xftFontLoader::getFontPattern(LyXFont::FONT_FAMILY family,
124 LyXFont::FONT_SERIES series,
125 LyXFont::FONT_SHAPE shape,
126 LyXFont::FONT_SIZE size)
128 // Normal font. Let's search for an existing name that matches.
132 double fsize = lyxrc.font_sizes[size] * lyxrc.zoom / 100.0;
133 XftPattern *fpat = XftPatternCreate();
135 ffamily = familyString(family);
137 case LyXFont::MEDIUM_SERIES:
138 fweight = XFT_WEIGHT_MEDIUM;
140 case LyXFont::BOLD_SERIES:
141 fweight = XFT_WEIGHT_BOLD;
144 fweight = XFT_WEIGHT_MEDIUM;
149 case LyXFont::UP_SHAPE:
150 case LyXFont::SMALLCAPS_SHAPE:
151 fslant = XFT_SLANT_ROMAN;
153 case LyXFont::ITALIC_SHAPE:
154 fslant = XFT_SLANT_ITALIC;
156 case LyXFont::SLANTED_SHAPE:
157 fslant = XFT_SLANT_OBLIQUE;
160 fslant = XFT_SLANT_ROMAN;
163 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
164 XftPatternAddInteger(fpat, XFT_WEIGHT, fweight);
165 XftPatternAddInteger(fpat, XFT_SLANT, fslant);
166 XftPatternAddDouble(fpat, XFT_SIZE, fsize);
172 XftFont * xftFontLoader::doLoad(LyXFont::FONT_FAMILY family,
173 LyXFont::FONT_SERIES series,
174 LyXFont::FONT_SHAPE shape,
175 LyXFont::FONT_SIZE size)
177 XftPattern * fpat = getFontPattern(family, series, shape, size);
179 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
181 XftFont * font = XftFontOpenPattern(getDisplay(), fpat2);
182 fonts_[family][series][shape][size] = font;
187 bool xftFontLoader::available(LyXFont const & f)
189 if (!lyx_gui::use_gui)
192 static std::vector<bool> cache_set(LyXFont::NUM_FAMILIES, false);
193 static std::vector<bool> cache(LyXFont::NUM_FAMILIES, false);
195 LyXFont::FONT_FAMILY family = f.family();
196 if (cache_set[family])
197 return cache[family];
198 cache_set[family] = true;
200 string const ffamily = familyString(family);
202 cache_set[family] = true;
203 XftPattern * fpat = XftPatternCreate();
204 XftPatternAddString(fpat, XFT_FAMILY, ffamily.c_str());
206 XftPattern * fpat2 = XftFontMatch(getDisplay(), getScreen(),
208 XftPatternDestroy(fpat);
210 XftPatternGetString(fpat2, XFT_FAMILY, 0, &familyM);
211 if (ffamily == familyM) {
212 cache[family] = true;
215 // We don't need to set cache[family] to false, as it
216 // is initialized to false;
219 // We don't care about non-symbol fonts