tInitialize font through fontconfig + freetype - xmenu - drop-down menu for X11
(HTM) git clone git://git.z3bra.org/xmenu.git
(DIR) Log
(DIR) Files
(DIR) Refs
---
(DIR) commit 769a43b6022669b39f11685961727945be3a016c
(DIR) parent 7ee5c0bf82d7491c1f62d381fcde373ec154d066
(HTM) Author: Willy Goiffon <dev@z3bra.org>
Date: Fri, 8 Nov 2019 23:05:57 +0100
Initialize font through fontconfig + freetype
Diffstat:
M config.def.h | 3 +++
M config.mk | 4 ++--
M xmenu.c | 52 +++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/config.def.h b/config.def.h
t@@ -5,3 +5,6 @@ int height = 480;
/* widget colors, given as RGB hex values */
uint32_t background = 0x666666;
uint32_t foreground = 0xffffff;
+
+/* font used for entries */
+char *font = "sans:pixelsize=14";
(DIR) diff --git a/config.mk b/config.mk
t@@ -6,7 +6,7 @@ LD = $(CC)
PREFIX = /usr/local
MANDIR = ${PREFIX}/man
-CPPFLAGS = -DVERSION=\"${VERSION}\"
+CPPFLAGS = -DVERSION=\"${VERSION}\" -I/usr/include/freetype2
CFLAGS = $(CPPFLAGS) -Wall -Wextra -pedantic
LDFLAGS =
-LDLIBS = -lxcb
+LDLIBS = -lxcb -lfontconfig -lfreetype
(DIR) diff --git a/xmenu.c b/xmenu.c
t@@ -4,6 +4,9 @@
#include <unistd.h>
#include <xcb/xcb.h>
+#include <fontconfig/fontconfig.h>
+#include <ft2build.h>
+#include FT_FREETYPE_H
#include "arg.h"
#include "config.h"
t@@ -20,11 +23,57 @@ usage(FILE *fd, char *name)
}
int
+setfont(char *fn, FT_Face *face)
+{
+ FcBool status;
+ FcResult result;
+ FcPattern *pattern, *match;
+ FcValue file, index;
+
+ FT_Library lib;
+
+ FcInit();
+ FT_Init_FreeType(&lib);
+
+ /* parse font name and fill default attributes */
+ pattern = FcNameParse((FcChar8 *)fn);
+ FcDefaultSubstitute(pattern);
+ status = FcConfigSubstitute(NULL, pattern, FcMatchPattern);
+ if (status == FcFalse) {
+ fprintf(stderr, "Bad font name '%s'\n", fn);
+ return -1;
+ }
+
+ /* load and actual font from the previously matched pattern */
+ match = FcFontMatch(NULL, pattern, &result);
+ if (result != FcResultMatch) {
+ fprintf(stderr, "Font not found: '%s'\n", fn);
+ return -1;
+ }
+
+ /* get fontconfig attributes from matched pattern like filename and index */
+ if (FcPatternGet(match, FC_FILE, 0, &file) != FcResultMatch) {
+ fprintf(stderr, "Font has no file: '%s'\n", fn);
+ return -1;
+ }
+ if (FcPatternGet(match, FC_INDEX, 0, &index) != FcResultMatch) {
+ fprintf(stderr, "Font has no file: '%s'\n", fn);
+ index.type = FcTypeInteger;
+ index.u.i = 0;
+ }
+
+ /* allocate freetype face according from fontconfig result */
+ return FT_New_Face(lib, (const char *)file.u.s, index.u.i, face);
+}
+
+int
main(int argc, char *argv[])
{
int mask, val[4];
char *argv0;
+ FT_Face face;
+
ARGBEGIN {
case 'h':
usage(stdout, argv0);
t@@ -43,6 +92,9 @@ main(int argc, char *argv[])
if (xcb_connection_has_error(dpy))
return -1;
+ if (setfont(font, &face) < 0)
+ return -1;
+
screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
if (!screen)
return -1;