Backspace sends DEL instead of ^H.
[spft.git] / FontSet.cpp
blob9343eb609fb0f95b4ed9146bd15ee3340214c240
1 #include "FontSet.h"
2 #include <stdexcept>
3 #include <stdio.h>
6 FontSet::FontSet(
7 std::string spec,
8 Display* display_in, int screen,
9 double font_size_override, bool skip_italics)
10 : display(display_in)
12 FcResult result;
14 // Regular.
15 FcPattern* pattern = FcNameParse((const FcChar8*) spec.c_str());
16 if (font_size_override > 0) {
17 FcPatternDel(pattern, FC_PIXEL_SIZE);
18 FcPatternDel(pattern, FC_SIZE);
19 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, font_size_override);
21 XftDefaultSubstitute(display, screen, pattern);
22 FcPattern* match = XftFontMatch(display, screen, pattern, &result);
23 #ifdef SHOW_REAL_FONT
24 FcChar8* format_name = FcPatternFormat(match, (FcChar8*) "%{=fcmatch} size: %{size} pixelsize: %{pixelsize}");
25 printf("Real font: \"%s\".\n", format_name);
26 free(format_name);
27 #endif
28 xft_fonts[0] = XftFontOpenPattern(display, match);
29 if (xft_fonts[0] == nullptr)
30 throw std::runtime_error("Couldn't open the font.");
32 // Get the font size used.
33 used_font_size = 0;
34 result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &used_font_size);
36 // Italics.
37 if (skip_italics) {
38 xft_fonts[2] = xft_fonts[0];
39 // We'll make bold-italic be the same as bold, below.
41 else {
42 // Italic.
43 // (We do this first because we'll be clobbering the weight later.)
44 FcPatternDel(pattern, FC_SLANT);
45 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
46 match = XftFontMatch(display, screen, pattern, &result);
47 xft_fonts[2] = XftFontOpenPattern(display, match);
48 if (xft_fonts[2] == nullptr) {
49 fprintf(stderr, "Couldn't open italic font.");
50 xft_fonts[2] = xft_fonts[0];
53 // Bold italic.
54 FcPatternDel(pattern, FC_WEIGHT);
55 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
56 match = XftFontMatch(display, screen, pattern, &result);
57 xft_fonts[3] = XftFontOpenPattern(display, match);
58 if (xft_fonts[3] == nullptr) {
59 fprintf(stderr, "Couldn't open bold italic font.");
60 xft_fonts[3] = xft_fonts[0];
64 // Bold.
65 if (skip_italics) {
66 FcPatternDel(pattern, FC_WEIGHT);
67 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
69 else {
70 FcPatternDel(pattern, FC_SLANT);
71 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
73 match = XftFontMatch(display, screen, pattern, &result);
74 xft_fonts[1] = XftFontOpenPattern(display, match);
75 if (xft_fonts[1] == nullptr) {
76 fprintf(stderr, "Couldn't open bold font.");
77 xft_fonts[1] = xft_fonts[0];
79 if (skip_italics)
80 xft_fonts[3] = xft_fonts[1];
82 FcPatternDestroy(pattern);
86 FontSet::~FontSet()
88 for (int i = 3; i >= 0; --i) {
89 bool is_copy = false;
90 for (int j = i - 1; j >= 0; --j) {
91 if (xft_fonts[i] == xft_fonts[j]) {
92 is_copy = true;
93 break;
96 if (!is_copy)
97 XftFontClose(display, xft_fonts[i]);
98 xft_fonts[i] = nullptr;