lt/font.c -rw-r--r-- 4.1 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "font.h"
#include "utils.h"

#include <math.h>

static bool font_load(x_window_t *x, FcPattern *pattern, font_t *f)
{
    FcPattern   *configured;
    FcPattern   *match;
    FcResult    result;
    XGlyphInfo  extents;
    int         want_attr;
    int         have_attr;

    configured = FcPatternDuplicate(pattern);
    if (configured == NULL)
        return false;

    FcConfigSubstitute(NULL, configured, FcMatchPattern);
    XftDefaultSubstitute(x->dsp, x->screen, configured);

    match = FcFontMatch(NULL, configured, &result);
    if (match == NULL)
    {
        FcPatternDestroy(configured);
        return false;
    }

    f->match = XftFontOpenPattern(x->dsp, match);
    if (f->match == NULL)
    {
        FcPatternDestroy(match);
        FcPatternDestroy(configured);
        return false;
    }

    if (XftPatternGetInteger(pattern, "slant", 0, &want_attr) == XftResultMatch)
    {
        if (XftPatternGetInteger(f->match->pattern, "slant", 0, &have_attr)
            != XftResultMatch || have_attr < want_attr)
        {
            f->bad_slant = 1;
            ERROR("Font slant does not match");
        }
    }

    if (XftPatternGetInteger(pattern, "weight", 0, &want_attr) == XftResultMatch)
    {
        if (XftPatternGetInteger(f->match->pattern, "weight", 0, &have_attr)
            != XftResultMatch || have_attr != want_attr)
        {
            f->bad_weight = 1;
            ERROR("Font weight does not match");
        }
    }

    XftTextExtentsUtf8(x->dsp, f->match,
        (const FcChar8 *)__ascii_printable, sizeof(__ascii_printable), &extents);
    f->set = NULL;
    f->pattern = configured;

    f->ascent = f->ascent;
    f->descent = f->descent;
    f->lbearing = 0;
    f->rbearing = f->match->max_advance_width;

    f->height = f->ascent + f->descent;
    f->width = DIVCEIL(extents.xOff, sizeof(__ascii_printable));

    return true;
}

bool fonts_load(x_window_t *x, const char *font_str, double size)
{
    FcPattern   *pattern;
    double      font_val;

    if (font_str[0] == '-')
        pattern = XftXlfdParse(font_str, False, False);
    else
        pattern = FcNameParse((FcChar8 *)font_str);

    if (pattern == NULL)
    {
        ERROR("Could not open font %s", font_str);
        return false;
    }

    if (size > 1)
    {
        FcPatternDel(pattern, FC_PIXEL_SIZE);
        FcPatternDel(pattern, FC_SIZE);
        FcPatternAddDouble(pattern, FC_PIXEL_SIZE, size);
        x->dc.used_font_size = size;
    }
    else
    {
        if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &font_val) == FcResultMatch)
            x->dc.used_font_size = font_val;
        else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &font_val) == FcResultMatch)
            x->dc.used_font_size = -1;
        else
        {
            FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
            x->dc.used_font_size = 12;
        }
    }

    if (!font_load(x, pattern, &x->dc.font))
        goto error;

    if (x->dc.used_font_size < 0)
    {
        FcPatternGetDouble(x->dc.font.match->pattern, FC_PIXEL_SIZE, 0, &font_val);
        x->dc.used_font_size = font_val;
    }

    x->term.cw = ceilf(x->dc.font.width);
    x->term.ch = ceilf(x->dc.font.height);

    FcPatternDel(pattern, FC_SLANT);
    FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
    if (!font_load(x, pattern, &x->dc.ifont))
        goto error;

    FcPatternDel(pattern, FC_WEIGHT);
    FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
    if (!font_load(x, pattern, &x->dc.ibfont))
        goto error;

    FcPatternDel(pattern, FC_SLANT);
    FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
    if (!font_load(x, pattern, &x->dc.bfont))
        goto error;

    FcPatternDestroy(pattern);

    return true;

error:
    ERROR("Can't open font %s", font_str);

    FcPatternDestroy(pattern);
    return false;
}

static void font_unload(x_window_t *x, font_t *f)
{
    XftFontClose(x->dsp, f->match);
    FcPatternDestroy(f->pattern);
    if (f->set)
        FcFontSetDestroy(f->set);
}

void fonts_unload(x_window_t *x)
{
    font_unload(x, &x->dc.font);
    font_unload(x, &x->dc.bfont);
    font_unload(x, &x->dc.ifont);
    font_unload(x, &x->dc.ibfont);
}