diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | font.c | 64 | ||||
| -rw-r--r-- | font.h | 8 | ||||
| -rw-r--r-- | keyboard.c | 1 | ||||
| -rw-r--r-- | main.c | 8 | ||||
| -rw-r--r-- | state.h | 7 |
6 files changed, 88 insertions, 2 deletions
@@ -1,7 +1,7 @@ BIN=swenu CC=gcc OBJDIR=objs -LIBS=egl wayland-client gl wayland-egl xkbcommon +LIBS=egl wayland-client gl wayland-egl xkbcommon freetype2 fontconfig CFLAGS=-Wall -g $(shell pkg-config --cflags $(LIBS)) -I$(OBJDIR) LDFLAGS=$(shell pkg-config --libs $(LIBS)) VPATH=/usr/share/wayland-protocols/staging/cursor-shape/:/usr/share/wayland-protocols/stable/tablet/:/usr/share/wayland-protocols/stable/xdg-shell/:$(OBJDIR) @@ -0,0 +1,64 @@ +#include <fontconfig/fontconfig.h> +#include "font.h" + +char* get_font(const char* font_name) { + FcPattern* pattern = FcNameParse((const FcChar8*)font_name); + if (!pattern) { + fprintf(stderr, "Failed to create font pattern\n"); + return NULL; + } + + FcConfigSubstitute(0, pattern, FcMatchPattern); + FcDefaultSubstitute(pattern); + + FcResult result = 0; + FcFontSet* font_patterns = FcFontSort(0, pattern, FcTrue, 0, &result); + if (!font_patterns || !font_patterns->nfont) { + fprintf(stderr, "No fonts installed on system\n"); + return NULL; + } + + FcFontSet* fs = FcFontSetCreate(); + FcPattern* font_pattern = FcFontRenderPrepare(0, pattern, font_patterns->fonts[0]); + if (!font_pattern) { + fprintf(stderr, "Could not prepare matched font for loading\n"); + return NULL; + } + FcFontSetAdd(fs, font_pattern); + + FcFontSetSortDestroy(font_patterns); + FcPatternDestroy(pattern); + + if (!fs || !font_patterns->nfont) { + fprintf(stderr, "Could not prepare font set\n"); + } + FcObjectSet* os = FcObjectSetBuild(FC_FILE, (char*)0); + FcValue v = { 0 }; + + FcPattern* fontp = FcPatternFilter(fs->fonts[0], os); + FcPatternGet(fontp, FC_FILE, 0, &v); + char* font = strdup((char*)v.u.f); + + FcPatternDestroy(fontp); + FcFontSetDestroy(fs); + FcObjectSetDestroy(os); + + return font; +} + +bool init_freetype(client_state* state, const char* file) { + if (FT_Init_FreeType(&state->ft_library)) { + fprintf(stderr, "Failed to intalize FreeType Library\n"); + return false; + } + if (FT_New_Face(state->ft_library, file, 0, &state->ft_face)) { + fprintf(stderr, "Failed to create font\n"); + return false; + } + if (FT_Set_Char_Size(state->ft_face, 0, 12 << 6, 0, 0)) { + fprintf(stderr, "Failed to set font size\n"); + return false; + } + + return true; +} @@ -0,0 +1,8 @@ +#ifndef FONT_H_ +#define FONT_H_ +#include "state.h" + +char* get_font(const char* font_name); +bool init_freetype(client_state* state, const char* font); + +#endif // FONT_H_ @@ -117,7 +117,6 @@ void wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, client_state* state = data; state->key_repeat_rate = rate; state->key_repeat_delay = delay; - printf("repeat info\n"); } struct wl_keyboard_listener keyboard_listener = { @@ -6,6 +6,7 @@ #include "wayland.h" #include "graphics.h" #include "input_field.h" +#include "font.h" void poll_events(client_state* state); @@ -34,6 +35,13 @@ int main() { return 1; } + char* font = get_font("Monospace"); + printf("font: %s\n", font); + if (!init_freetype(&state, font)) { + fprintf(stderr, "Failed to Initalize FreeType\n"); + } + free(font); + // event loop while (state.running) { poll_events(&state); @@ -11,6 +11,9 @@ #include <cursor-shape-v1.h> #include <sys/timerfd.h> +#include <ft2build.h> +#include FT_FREETYPE_H + #include "./glad/glad.h" typedef struct { @@ -46,6 +49,10 @@ typedef struct { // state char* input_buffer; uint32_t width, height; + + FT_Library ft_library; + FT_Face ft_face; + bool running; } client_state; |
