-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.c
143 lines (115 loc) · 3.44 KB
/
font.c
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
/*
* mxswm - MaXed Stacks Window Manager for X11
* Copyright (c) 2021, Tommi Leino <namhas@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* font.c: Implements Xft(3) font and text drawing support. */
#include "mxswm.h"
#include <X11/Xft/Xft.h>
#include <limits.h>
#include <assert.h>
#include <err.h>
#include "fontnames.c"
static XftColor ftcolor;
static XftFont *ftfont[NUM_FONT];
static XftFont *current_font;
static XftDraw *ftdraw;
static XftFont *load_font(int);
/*
* Sets font color by reusing named/enum-defined colors from color.c so
* that we can use same color defines in font and non-font stuff.
*/
void
set_font_color(int color)
{
XColor xcolor;
xcolor = query_color(color);
ftcolor.pixel = xcolor.pixel;
ftcolor.color.red = xcolor.red;
ftcolor.color.green = xcolor.green;
ftcolor.color.blue = xcolor.blue;
ftcolor.color.alpha = USHRT_MAX;
}
int
get_font_height()
{
assert(current_font != NULL);
return current_font->height;
}
void
set_font(int id)
{
assert (id < NUM_FONT);
if (ftfont[id] == NULL)
ftfont[id] = load_font(id);
current_font = ftfont[id];
}
void
font_extents(const char *text, size_t len, XGlyphInfo *extents)
{
XftTextExtentsUtf8(display(), current_font,
(const FcChar8 *) text, len, extents);
}
int
draw_font(Window window, int x, int y, int bgcolor, const char *text)
{
XGlyphInfo extents;
size_t len;
XftColor ftbg;
XColor xcolor;
if (ftdraw == NULL)
if ((ftdraw = XftDrawCreate(display(), window,
DefaultVisual(display(), DefaultScreen(display())),
DefaultColormap(display(), DefaultScreen(display())))) ==
NULL)
errx(1, "XftDrawCreate failed");
if (XftDrawDrawable(ftdraw) != window)
XftDrawChange(ftdraw, window);
len = strlen(text);
font_extents(text, len, &extents);
if (bgcolor != -1) {
xcolor = query_color(bgcolor);
ftbg.pixel = xcolor.pixel;
ftbg.color.red = xcolor.red;
ftbg.color.green = xcolor.green;
ftbg.color.blue = xcolor.blue;
ftbg.color.alpha = USHRT_MAX;
XftDrawRect(ftdraw, &ftbg, x, y, extents.xOff,
current_font->height);
}
XftDrawStringUtf8(ftdraw, &ftcolor, current_font, x,
y + current_font->ascent, (const FcChar8 *) text, len);
return extents.xOff;
}
static XftFont *
load_font(int id)
{
/*
* First try Xlfd form, then Xft font name form.
*/
ftfont[id] = XftFontOpenXlfd(display(),
DefaultScreen(display()), fontname[id]);
if (ftfont[id] == NULL)
ftfont[id] = XftFontOpenName(display(),
DefaultScreen(display()), fontname[id]);
/*
* No success? Try the fallback font.
*/
if (ftfont[id] == NULL && id != FONT_FALLBACK) {
warnx("couldn't load font: %s", fontname[id]);
return load_font(FONT_FALLBACK);
} else if (ftfont[id] == NULL)
errx(1, "couldn't load fallback font: %s", fontname[id]);
return ftfont[id];
}