aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-28 10:09:22 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-28 10:09:22 +0000
commitcb02c9cf416815c5d55254d3c81647b28ca0aa47 (patch)
tree8ae0eafee7802438b8a60199f55f167df19349b4
parentrender: poc: use freetype to render glyphs (diff)
font: load FreeType face, and set it's pixel sizes
-rw-r--r--PKGBUILD2
-rw-r--r--font.c63
-rw-r--r--font.h5
-rw-r--r--main.c12
-rw-r--r--render.c2
-rw-r--r--terminal.h4
6 files changed, 63 insertions, 25 deletions
diff --git a/PKGBUILD b/PKGBUILD
index 4dd9ce1b..b3b4352c 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -1,5 +1,5 @@
pkgname=foot
-pkgver=0.0.r2.g7379198
+pkgver=0.0.r136.g90d357b
pkgrel=1
pkgdesc="A wayland native terminal emulator"
arch=('x86_64')
diff --git a/font.c b/font.c
index 4d8bd65f..d16cc5ee 100644
--- a/font.c
+++ b/font.c
@@ -10,30 +10,37 @@
#define LOG_MODULE "font"
#include "log.h"
+static FT_Library ft_lib;
+
static void __attribute__((constructor))
init(void)
{
FcInit();
+ FT_Init_FreeType(&ft_lib);
}
static void __attribute__((destructor))
fini(void)
{
FcFini();
+ FT_Done_FreeType(ft_lib);
}
-cairo_scaled_font_t *
-font_from_name(const char *name)
+
+bool
+font_from_name(const char *name, struct font *font)
{
+ memset(font, 0, sizeof(*font));
+
FcPattern *pattern = FcNameParse((const unsigned char *)name);
if (pattern == NULL) {
LOG_ERR("%s: failed to lookup font", name);
- return NULL;
+ return false;
}
if (!FcConfigSubstitute(NULL, pattern, FcMatchPattern)) {
LOG_ERR("%s: failed to do config substitution", name);
FcPatternDestroy(pattern);
- return NULL;
+ return false;
}
cairo_font_options_t *options = cairo_font_options_create();
@@ -50,9 +57,40 @@ font_from_name(const char *name)
if (final_pattern == NULL) {
LOG_ERR("%s: failed to match font", name);
- return NULL;
+ return false;
}
+ FcChar8 *face_file = NULL;
+ if (FcPatternGetString(final_pattern, FC_FT_FACE, 0, &face_file) != FcResultMatch) {
+ if (FcPatternGetString(final_pattern, FC_FILE, 0, &face_file) != FcResultMatch) {
+ LOG_ERR("no font file name available");
+ FcPatternDestroy(final_pattern);
+ return false;
+ }
+ }
+
+ double dpi;
+ if (FcPatternGetDouble(final_pattern, FC_DPI, 0, &dpi) != FcResultMatch)
+ dpi = 96;
+
+ double size;
+ if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &size)) {
+ LOG_ERR("%s: failed to get size", name);
+ FcPatternDestroy(final_pattern);
+ return false;
+ }
+
+ LOG_DBG("loading: %s", face_file);
+
+ FT_Face ft_face;
+ FT_Error ft_err = FT_New_Face(ft_lib, (const char *)face_file, 0, &ft_face);
+ if (ft_err != 0)
+ LOG_ERR("%s: failed to create FreeType face", face_file);
+
+ /* TODO: use FT_Set_Char_Size() if FC_PIXEL_SIZE doesn't exist, and use size instead? */
+ if ((ft_err = FT_Set_Pixel_Sizes(ft_face, 0, size)) != 0)
+ LOG_WARN("failed to set FreeType pixel sizes");
+
FcBool fc_hinting, fc_antialias;
if (FcPatternGetBool(final_pattern, FC_HINTING,0, &fc_hinting) != FcResultMatch)
fc_hinting = FcTrue;
@@ -65,13 +103,6 @@ font_from_name(const char *name)
cairo_font_options_set_antialias(
options, fc_antialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
- double size;
- if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &size)) {
- LOG_ERR("%s: failed to get size", name);
- FcPatternDestroy(final_pattern);
- return NULL;
- }
-
cairo_font_face_t *face = cairo_ft_font_face_create_for_pattern(
final_pattern);
@@ -80,7 +111,7 @@ font_from_name(const char *name)
if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS) {
LOG_ERR("%s: failed to create cairo font face", name);
cairo_font_face_destroy(face);
- return NULL;
+ return false;
}
cairo_matrix_t matrix, ctm;
@@ -96,8 +127,10 @@ font_from_name(const char *name)
if (cairo_scaled_font_status(scaled_font) != CAIRO_STATUS_SUCCESS) {
LOG_ERR("%s: failed to create scaled font", name);
cairo_scaled_font_destroy(scaled_font);
- return NULL;
+ return false;
}
- return scaled_font;
+ font->face = ft_face;
+ font->font = scaled_font;
+ return true;
}
diff --git a/font.h b/font.h
index d2b8274c..90a1b6a7 100644
--- a/font.h
+++ b/font.h
@@ -1,5 +1,6 @@
#pragma once
-#include <cairo.h>
+#include <stdbool.h>
+#include "terminal.h"
-cairo_scaled_font_t *font_from_name(const char *name);
+bool font_from_name(const char *name, struct font *result);
diff --git a/main.c b/main.c
index 8248e28a..31de1b58 100644
--- a/main.c
+++ b/main.c
@@ -409,20 +409,19 @@ main(int argc, char *const *argv)
thrd_t keyboard_repeater_id;
thrd_create(&keyboard_repeater_id, &keyboard_repeater, &term);
- term.fonts[0].font = font_from_name(conf.font);
- if (term.fonts[0].font == NULL)
+ if (!font_from_name(conf.font, &term.fonts[0]))
goto out;
{
char fname[1024];
snprintf(fname, sizeof(fname), "%s:style=bold", conf.font);
- term.fonts[1].font = font_from_name(fname);
+ font_from_name(fname, &term.fonts[1]);
snprintf(fname, sizeof(fname), "%s:style=italic", conf.font);
- term.fonts[2].font = font_from_name(fname);
+ font_from_name(fname, &term.fonts[2]);
snprintf(fname, sizeof(fname), "%s:style=bold italic", conf.font);
- term.fonts[3].font = font_from_name(fname);
+ font_from_name(fname, &term.fonts[3]);
}
/* Underline position and size */
@@ -924,6 +923,9 @@ out:
for (size_t j = 0; j < 256; j++)
cairo_glyph_free(f->glyph_cache[j].glyphs);
+
+ if (f->face != NULL)
+ FT_Done_Face(f->face);
}
if (term.flash.fd != -1)
diff --git a/render.c b/render.c
index 0ffb0d18..a7948e60 100644
--- a/render.c
+++ b/render.c
@@ -5,8 +5,6 @@
#include <sys/time.h>
#include <sys/timerfd.h>
-#include <ft2build.h>
-#include FT_FREETYPE_H
#include <cairo-ft.h>
#include <wayland-cursor.h>
diff --git a/terminal.h b/terminal.h
index 1971d3f4..ed8adf0d 100644
--- a/terminal.h
+++ b/terminal.h
@@ -6,6 +6,9 @@
#include <threads.h>
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
#include <cairo.h>
#include <wayland-client.h>
#include <primary-selection-unstable-v1.h>
@@ -209,6 +212,7 @@ struct glyph_cache {
};
struct font {
+ FT_Face face;
cairo_scaled_font_t *font;
struct {
double position;