From cb02c9cf416815c5d55254d3c81647b28ca0aa47 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jul 2019 12:09:22 +0200 Subject: font: load FreeType face, and set it's pixel sizes --- font.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 15 deletions(-) (limited to 'font.c') 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; } -- cgit v1.3 From 175dc9cf9483e389c2440a68b9472b1befc4d459 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jul 2019 12:45:01 +0200 Subject: cairo-ft: remove all usages of cairo-ft --- font.c | 38 -------------------------------------- main.c | 34 ---------------------------------- meson.build | 3 +-- render.c | 4 +++- terminal.h | 3 --- 5 files changed, 4 insertions(+), 78 deletions(-) (limited to 'font.c') diff --git a/font.c b/font.c index d16cc5ee..5686c12a 100644 --- a/font.c +++ b/font.c @@ -5,7 +5,6 @@ #include #include -#include #define LOG_MODULE "font" #include "log.h" @@ -43,12 +42,6 @@ font_from_name(const char *name, struct font *font) return false; } - cairo_font_options_t *options = cairo_font_options_create(); - cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_DEFAULT); - cairo_font_options_set_antialias(options, CAIRO_ANTIALIAS_DEFAULT); - cairo_font_options_set_subpixel_order(options, CAIRO_SUBPIXEL_ORDER_DEFAULT); - cairo_ft_font_options_substitute(options, pattern); - FcDefaultSubstitute(pattern); FcResult result; @@ -98,39 +91,8 @@ font_from_name(const char *name, struct font *font) if (FcPatternGetBool(final_pattern, FC_ANTIALIAS, 0, &fc_antialias) != FcResultMatch) fc_antialias = FcTrue; - cairo_font_options_set_hint_style( - options, fc_hinting ? CAIRO_HINT_STYLE_DEFAULT : CAIRO_HINT_STYLE_NONE); - cairo_font_options_set_antialias( - options, fc_antialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE); - - cairo_font_face_t *face = cairo_ft_font_face_create_for_pattern( - final_pattern); - FcPatternDestroy(final_pattern); - 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 false; - } - - cairo_matrix_t matrix, ctm; - cairo_matrix_init_identity(&ctm); - cairo_matrix_init_scale(&matrix, size, size); - - cairo_scaled_font_t *scaled_font = cairo_scaled_font_create( - face, &matrix, &ctm, options); - - cairo_font_options_destroy(options); - cairo_font_face_destroy(face); - - 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 false; - } - font->face = ft_face; - font->font = scaled_font; return true; } diff --git a/main.c b/main.c index 80ec053e..dfcc28ab 100644 --- a/main.c +++ b/main.c @@ -485,34 +485,6 @@ main(int argc, char *const *argv) term.cell_width = (int)ceil(term.fextents.max_x_advance); term.cell_height = (int)ceil(term.fextents.height); - /* Glyph cache */ - for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) { - struct font *f = &term.fonts[i]; - - for (int j = 0; j < 256; j++) { - cairo_glyph_t *glyphs = NULL; - int count = 0; - - char c = j; - cairo_status_t status = cairo_scaled_font_text_to_glyphs( - f->font, 0, 0 + term.fextents.ascent, - &c, 1, &glyphs, &count, - NULL, NULL, NULL); - - if (status != CAIRO_STATUS_SUCCESS) - continue; - - if (count == 0) - continue; - - assert(glyphs != NULL); - assert(count == 1); - - f->glyph_cache[j].glyphs = glyphs; - f->glyph_cache[j].count = count; - } - } - term.wl.display = wl_display_connect(NULL); if (term.wl.display == NULL) { LOG_ERR("failed to connect to wayland; no compositor running?"); @@ -927,12 +899,6 @@ out: for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) { struct font *f = &term.fonts[i]; - if (f->font != NULL) - cairo_scaled_font_destroy(f->font); - - 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); } diff --git a/meson.build b/meson.build index a6da9aba..95a139d7 100644 --- a/meson.build +++ b/meson.build @@ -23,7 +23,6 @@ math = cc.find_library('m') threads = dependency('threads') fontconfig = dependency('fontconfig') cairo = dependency('cairo') -cairo_ft = dependency('cairo-ft') wayland_protocols = dependency('wayland-protocols') wayland_client = dependency('wayland-client') wayland_cursor = dependency('wayland-cursor') @@ -78,7 +77,7 @@ executable( 'tllist.h', 'vt.c', 'vt.h', wl_proto_src + wl_proto_headers, - dependencies: [threads, math, cairo, cairo_ft, fontconfig, wayland_client, wayland_cursor, xkb], + dependencies: [threads, math, cairo, fontconfig, wayland_client, wayland_cursor, xkb], install: true) custom_target( diff --git a/render.c b/render.c index 5d16689d..e1340a86 100644 --- a/render.c +++ b/render.c @@ -61,15 +61,17 @@ gseq_flush(struct terminal *term, struct buffer *buf) if (gseq.count == 0) return; + assert(NULL); struct rgb fg = color_hex_to_rgb(gseq.foreground); if (gseq.attrs.dim) color_dim(&fg); +#if 0 cairo_set_scaled_font(buf->cairo, attrs_to_font(term, &gseq.attrs)->font); cairo_set_source_rgb(buf->cairo, fg.r, fg.g, fg.b); cairo_show_glyphs(buf->cairo, gseq.glyphs, gseq.count); - +#endif gseq.g = gseq.glyphs; gseq.count = 0; } diff --git a/terminal.h b/terminal.h index 6c0b81f1..b5fea057 100644 --- a/terminal.h +++ b/terminal.h @@ -213,7 +213,6 @@ struct glyph_cache { struct font { FT_Face face; - cairo_scaled_font_t *font; struct { double position; double thickness; @@ -222,8 +221,6 @@ struct font { double position; double thickness; } strikeout; - - struct glyph_cache glyph_cache[256]; }; enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; -- cgit v1.3 From 9e57ba21086abb15772ae7efda06e584d618cd19 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jul 2019 20:37:59 +0200 Subject: font: add font_glyph_for_utf8() This function fills in a struct glyph with glyph bitmap data for the provided utf-8 character (essentially a FT_Bitmap wrapped in a cairo surface). --- font.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ font.h | 2 ++ render.c | 84 ++++++++------------------------------------------------------ terminal.h | 8 +++--- 4 files changed, 94 insertions(+), 76 deletions(-) (limited to 'font.c') diff --git a/font.c b/font.c index 5686c12a..b22e4457 100644 --- a/font.c +++ b/font.c @@ -96,3 +96,79 @@ font_from_name(const char *name, struct font *font) font->face = ft_face; return true; } + +bool +font_glyph_for_utf8(const struct font *font, const char *utf8, + struct glyph *glyph) +{ + wchar_t wc; + if (mbstowcs(&wc, utf8, 1) < 0) + return false; + + FT_UInt idx = FT_Get_Char_Index(font->face, wc); + FT_Error err = FT_Load_Glyph(font->face, idx, FT_LOAD_DEFAULT); + if (err != 0) + return false; + + err = FT_Render_Glyph(font->face->glyph, FT_RENDER_MODE_NORMAL); + if (err != 0) + return false; + + FT_Bitmap *bitmap = &font->face->glyph->bitmap; + assert(bitmap->pixel_mode == FT_PIXEL_MODE_GRAY || + bitmap->pixel_mode == FT_PIXEL_MODE_MONO); + + cairo_format_t cr_format = bitmap->pixel_mode == FT_PIXEL_MODE_GRAY + ? CAIRO_FORMAT_A8 : CAIRO_FORMAT_A1; + + int stride = cairo_format_stride_for_width(cr_format, bitmap->width); + assert(stride >= bitmap->pitch); + + uint8_t *data = malloc(bitmap->rows * stride); + + switch (bitmap->pixel_mode) { + case FT_PIXEL_MODE_MONO: + for (size_t r = 0; r < bitmap->rows; r++) { + for (size_t c = 0; c < bitmap->width; c++) { + uint8_t v = bitmap->buffer[r * bitmap->pitch + c]; + uint8_t reversed = 0; + for (size_t i = 0; i < 8; i++) { + reversed |= (v & 1) << (7 - i); + v >>= 1; + } + data[r * stride + c] = reversed; + } + } + break; + + case FT_PIXEL_MODE_GRAY: + for (size_t r = 0; r < bitmap->rows; r++) { + for (size_t c = 0; c < bitmap->width; c++) + data[r * stride + c] = bitmap->buffer[r * bitmap->pitch + c]; + } + break; + + default: + LOG_ERR("unimplemented FreeType bitmap pixel mode: %d", + bitmap->pixel_mode); + free(data); + return false; + } + + cairo_surface_t *surf = cairo_image_surface_create_for_data( + data, cr_format, bitmap->width, bitmap->rows, stride); + + if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) { + free(data); + cairo_surface_destroy(surf); + return false; + } + + *glyph = (struct glyph){ + .data = data, + .surf = surf, + .left = font->face->glyph->bitmap_left, + .top = font->face->glyph->bitmap_top, + }; + return true; +} diff --git a/font.h b/font.h index 90a1b6a7..d8efd509 100644 --- a/font.h +++ b/font.h @@ -4,3 +4,5 @@ #include "terminal.h" bool font_from_name(const char *name, struct font *result); +bool font_glyph_for_utf8( + const struct font *font, const char *utf8, struct glyph *glyph); diff --git a/render.c b/render.c index e1340a86..7bebbbce 100644 --- a/render.c +++ b/render.c @@ -5,8 +5,6 @@ #include #include -#include - #include #include @@ -15,6 +13,7 @@ #include "log.h" #include "shm.h" #include "grid.h" +#include "font.h" #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) @@ -312,81 +311,20 @@ render_cell(struct terminal *term, struct buffer *buf, const struct cell *cell, : NULL; if (e == NULL || e->data == NULL) { - wchar_t wc; - int res __attribute__((unused)) = mbstowcs(&wc, cell->c, 1); - if (res != 1) - return; - - FT_Face ft_face = font->face; - - FT_UInt glyph_idx = FT_Get_Char_Index(ft_face, wc); - FT_Error ft_err = FT_Load_Glyph(ft_face, glyph_idx, FT_LOAD_DEFAULT); - if (ft_err != 0) { - LOG_ERR("FT_Load_Glyph"); - goto done; - } - - ft_err = FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL); - if (ft_err != 0) { - LOG_ERR("FT_Render_Glyph"); + struct glyph g; + if (!font_glyph_for_utf8(font, cell->c, &g)) goto done; - } - - FT_Bitmap *bitmap = &ft_face->glyph->bitmap; - assert(bitmap->pixel_mode == FT_PIXEL_MODE_GRAY || - bitmap->pixel_mode == FT_PIXEL_MODE_MONO); - - cairo_format_t cr_format = bitmap->pixel_mode == FT_PIXEL_MODE_GRAY - ? CAIRO_FORMAT_A8 : CAIRO_FORMAT_A1; - - int stride = cairo_format_stride_for_width(cr_format, buf->width); - assert(stride >= bitmap->pitch); - - uint8_t *copy = malloc(bitmap->rows * stride); - - switch (bitmap->pixel_mode) { - case FT_PIXEL_MODE_MONO: { - for (size_t r = 0; r < bitmap->rows; r++) { - for (size_t c = 0; c < bitmap->width; c++) { - uint8_t v = bitmap->buffer[r * bitmap->pitch + c]; - uint8_t reversed = 0; - for (size_t i = 0; i < 8; i++) { - reversed |= (v & 1) << (7 - i); - v >>= 1; - } - copy[r * stride + c] = reversed; - } - } - break; - } - - case FT_PIXEL_MODE_GRAY: - for (size_t r = 0; r < bitmap->rows; r++) { - for (size_t c = 0; c < bitmap->width; c++) - copy[r * stride + c] = bitmap->buffer[r * bitmap->pitch + c]; - } - break; - - default: - LOG_ERR("unimplemented FT bitmap pixel mode: %d", bitmap->pixel_mode); - abort(); - break; - } - - glyph = cairo_image_surface_create_for_data( - copy, cr_format, bitmap->width, bitmap->rows, stride); - - left = ft_face->glyph->bitmap_left; - top = ft_face->glyph->bitmap_top; - - assert(cairo_surface_status(glyph) == CAIRO_STATUS_SUCCESS); if (e != NULL) { - e->data = copy; - e->surf = glyph; - e->left = left; - e->top = top; + e->data = g.data; + e->surf = g.surf; + e->left = g.left; + e->top = g.top; } + + glyph = g.surf; + left = g.left; + top = g.top; } else { glyph = e->surf; left = e->left; diff --git a/terminal.h b/terminal.h index b5fea057..83efe69a 100644 --- a/terminal.h +++ b/terminal.h @@ -206,9 +206,11 @@ struct primary { uint32_t serial; }; -struct glyph_cache { - cairo_glyph_t *glyphs; - int count; +struct glyph { + void *data; + cairo_surface_t *surf; + int left; + int top; }; struct font { -- cgit v1.3 From fe882bddbab2b3c75220976ba01cc998773fdfa5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jul 2019 21:03:38 +0200 Subject: font: populate glyph cache (ASCII characters only) when instantiating font --- font.c | 25 ++++++++++++++++++ font.h | 1 + main.c | 9 +++---- render.c | 89 +++++++++----------------------------------------------------- terminal.h | 2 ++ 5 files changed, 44 insertions(+), 82 deletions(-) (limited to 'font.c') diff --git a/font.c b/font.c index b22e4457..49fd8130 100644 --- a/font.c +++ b/font.c @@ -25,6 +25,14 @@ fini(void) FT_Done_FreeType(ft_lib); } +static void +font_populate_glyph_cache(struct font *font) +{ + memset(font->cache, 0, sizeof(font->cache)); + for (size_t i = 0; i < 256; i++) + font_glyph_for_utf8(font, &(char){i}, &font->cache[i]); +} + bool font_from_name(const char *name, struct font *font) { @@ -94,6 +102,7 @@ font_from_name(const char *name, struct font *font) FcPatternDestroy(final_pattern); font->face = ft_face; + font_populate_glyph_cache(font); return true; } @@ -114,6 +123,8 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, if (err != 0) return false; + assert(font->face->glyph->format == FT_GLYPH_FORMAT_BITMAP); + FT_Bitmap *bitmap = &font->face->glyph->bitmap; assert(bitmap->pixel_mode == FT_PIXEL_MODE_GRAY || bitmap->pixel_mode == FT_PIXEL_MODE_MONO); @@ -172,3 +183,17 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, }; return true; } + +void +font_destroy(struct font *font) +{ + if (font->face != NULL) + FT_Done_Face(font->face); + + for (size_t i = 0; i < 256; i++) { + if (font->cache[i].surf != NULL) + cairo_surface_destroy(font->cache[i].surf); + if (font->cache[i].data != NULL) + free(font->cache[i].data); + } +} diff --git a/font.h b/font.h index d8efd509..c02e4ac1 100644 --- a/font.h +++ b/font.h @@ -6,3 +6,4 @@ bool font_from_name(const char *name, struct font *result); bool font_glyph_for_utf8( const struct font *font, const char *utf8, struct glyph *glyph); +void font_destroy(struct font *font); diff --git a/main.c b/main.c index dfcc28ab..7d0fa355 100644 --- a/main.c +++ b/main.c @@ -896,12 +896,8 @@ out: free(term.window_title); tll_free_and_free(term.window_title_stack, free); - for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) { - struct font *f = &term.fonts[i]; - - if (f->face != NULL) - FT_Done_Face(f->face); - } + for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++) + font_destroy(&term.fonts[i]); if (term.flash.fd != -1) close(term.flash.fd); @@ -921,4 +917,5 @@ out: cairo_debug_reset_static_data(); return ret; + } diff --git a/render.c b/render.c index 7bebbbce..cf6c013e 100644 --- a/render.c +++ b/render.c @@ -258,93 +258,30 @@ render_cell(struct terminal *term, struct buffer *buf, const struct cell *cell, gseq.foreground = _fg; } -#if 0 - int new_glyphs - = sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - gseq.count; -#endif - struct font *font = attrs_to_font(term, &cell->attrs); -#if 0 - struct glyph_cache *entry = cell->c[1] == '\0' - ? &font->glyph_cache[(unsigned char)cell->c[0]] - : NULL; - - if (likely(entry != NULL && entry->glyphs != NULL)) { - /* Copy cached glyph(s) and upate position */ - memcpy(gseq.g, entry->glyphs, entry->count * sizeof(gseq.g[0])); - for (size_t i = 0; i < entry->count; i++) { - gseq.g[i].x += x; - gseq.g[i].y += y; - } - - new_glyphs = entry->count; - } else { - /* Must generate new glyph(s) */ - cairo_status_t status = cairo_scaled_font_text_to_glyphs( - font->font, x, y + term->fextents.ascent, - cell->c, strnlen(cell->c, 4), &gseq.g, &new_glyphs, - NULL, NULL, NULL); - - if (status != CAIRO_STATUS_SUCCESS) - return; + struct glyph *glyph = NULL; + if (strnlen(cell->c, 4) == 1) { + if (font->cache[(unsigned char)cell->c[0]].surf != NULL) + glyph = &font->cache[(unsigned char)cell->c[0]]; } - gseq.g += new_glyphs; - gseq.count += new_glyphs; - assert(gseq.count <= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0])); -#else - - struct foo_cache { - uint8_t *data; - cairo_surface_t *surf; - int left; - int top; - }; - static struct foo_cache foo_cache[4][256] = {0}; - - cairo_surface_t *glyph = NULL; - double left, top; - - struct foo_cache *e = strnlen(cell->c, 4) == 1 - ? &foo_cache[cell->attrs.italic << 1 | cell->attrs.bold][(unsigned char)cell->c[0]] - : NULL; - - if (e == NULL || e->data == NULL) { - struct glyph g; - if (!font_glyph_for_utf8(font, cell->c, &g)) - goto done; - - if (e != NULL) { - e->data = g.data; - e->surf = g.surf; - e->left = g.left; - e->top = g.top; - } - - glyph = g.surf; - left = g.left; - top = g.top; - } else { - glyph = e->surf; - left = e->left; - top = e->top; + struct glyph _glyph; + if (glyph == NULL) { + if (!font_glyph_for_utf8(font, cell->c, &_glyph)) + return; + glyph = &_glyph; } assert(glyph != NULL); cairo_set_source_rgb(buf->cairo, fg.r, fg.g, fg.b); cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER); - cairo_mask_surface(buf->cairo, glyph, x + left, y + term->fextents.ascent - top); + cairo_mask_surface(buf->cairo, glyph->surf, x + glyph->left, y + term->fextents.ascent - glyph->top); - if (e == NULL) { - void *raw = cairo_image_surface_get_data(glyph); - cairo_surface_destroy(glyph); - free(raw); + if (glyph == &_glyph) { + cairo_surface_destroy(_glyph.surf); + free(_glyph.data); } - -done: - return; -#endif } static void diff --git a/terminal.h b/terminal.h index 83efe69a..89f3dc39 100644 --- a/terminal.h +++ b/terminal.h @@ -223,6 +223,8 @@ struct font { double position; double thickness; } strikeout; + + struct glyph cache[256]; }; enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; -- cgit v1.3 From 4302d3eb680e1bd88724b8d67a6659f44bcdf789 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 29 Jul 2019 20:10:55 +0200 Subject: font: set FT load and render flags from FontConfig properties --- font.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- font.h | 4 +- meson.build | 2 +- terminal.h | 5 ++- 4 files changed, 129 insertions(+), 21 deletions(-) (limited to 'font.c') diff --git a/font.c b/font.c index 49fd8130..c6a118c9 100644 --- a/font.c +++ b/font.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -9,6 +10,8 @@ #define LOG_MODULE "font" #include "log.h" +#define min(x, y) ((x) < (y) ? (x) : (y)) + static FT_Library ft_lib; static void __attribute__((constructor)) @@ -88,40 +91,128 @@ font_from_name(const char *name, struct font *font) 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"); + if ((ft_err = FT_Set_Char_Size(ft_face, size * 64, 0, 0, 0)) != 0) + LOG_WARN("failed to set character size"); - FcBool fc_hinting, fc_antialias; + FcBool fc_hinting; if (FcPatternGetBool(final_pattern, FC_HINTING,0, &fc_hinting) != FcResultMatch) fc_hinting = FcTrue; + FcBool fc_antialias; if (FcPatternGetBool(final_pattern, FC_ANTIALIAS, 0, &fc_antialias) != FcResultMatch) fc_antialias = FcTrue; + int fc_hintstyle; + if (FcPatternGetInteger(final_pattern, FC_HINT_STYLE, 0, &fc_hintstyle) != FcResultMatch) + fc_hintstyle = FC_HINT_SLIGHT; + + int fc_rgba; + if (FcPatternGetInteger(final_pattern, FC_RGBA, 0, &fc_rgba) != FcResultMatch) + fc_rgba = FC_RGBA_UNKNOWN; + + int load_flags = 0; + if (!fc_antialias) { + if (!fc_hinting || fc_hintstyle == FC_HINT_NONE) + load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL; + else + load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO; + } else { + if (!fc_hinting || fc_hintstyle == FC_HINT_NONE) + load_flags |= FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL; + else if (fc_hinting && fc_hintstyle == FC_HINT_SLIGHT) + load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LIGHT; + else if (fc_rgba == FC_RGBA_RGB) { + LOG_WARN("unimplemented: subpixel antialiasing"); + // load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD; + load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_NORMAL; + } else if (fc_rgba == FC_RGBA_VRGB) { + LOG_WARN("unimplemented: subpixel antialiasing"); + //load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD_V; + load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_NORMAL; + } else + load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_NORMAL; + } + + FcBool fc_embeddedbitmap; + if (FcPatternGetBool(final_pattern, FC_EMBEDDED_BITMAP, 0, &fc_embeddedbitmap) != FcResultMatch) + fc_embeddedbitmap = FcTrue; + + if (!fc_embeddedbitmap) + load_flags |= FT_LOAD_NO_BITMAP; + + int render_flags = 0; + if (!fc_antialias) + render_flags |= FT_RENDER_MODE_MONO; + else { + if (false) + ; +#if 0 + if (fc_rgba == FC_RGBA_RGB) + render_flags |= FT_RENDER_MODE_LCD; + else if (fc_rgba == FC_RGBA_VRGB) + render_flags |= FT_RENDER_MODE_LCD_V; +#endif + else + render_flags |= FT_RENDER_MODE_NORMAL; + } + + int fc_lcdfilter; + if (FcPatternGetInteger(final_pattern, FC_LCD_FILTER, 0, &fc_lcdfilter) != FcResultMatch) + fc_lcdfilter = FC_LCD_DEFAULT; + + switch (fc_lcdfilter) { + case FC_LCD_NONE: font->lcd_filter = FT_LCD_FILTER_NONE; break; + case FC_LCD_DEFAULT: font->lcd_filter = FT_LCD_FILTER_DEFAULT; break; + case FC_LCD_LIGHT: font->lcd_filter = FT_LCD_FILTER_LIGHT; break; + case FC_LCD_LEGACY: font->lcd_filter = FT_LCD_FILTER_LEGACY; break; + } + FcPatternDestroy(final_pattern); + mtx_init(&font->lock, mtx_plain); font->face = ft_face; + font->load_flags = load_flags; + font->render_flags = render_flags; font_populate_glyph_cache(font); return true; } bool -font_glyph_for_utf8(const struct font *font, const char *utf8, +font_glyph_for_utf8(struct font *font, const char *utf8, struct glyph *glyph) { + mbstate_t ps = {0}; wchar_t wc; - if (mbstowcs(&wc, utf8, 1) < 0) + if (mbrtowc(&wc, utf8, 4, &ps) < 0) { + LOG_ERR("FAILED: %.4s", utf8); return false; + } + + wprintf(L"CONVERTED: %.1s\n", &wc); + + mtx_lock(&font->lock); + + /* + * LCD filter is per library instance. Thus we need to re-set it + * every time... + * + * Also note that many freetype builds lack this feature + * (FT_CONFIG_OPTION_SUBPIXEL_RENDERING must be defined, and isn't + * by default) */ + FT_Error err = FT_Library_SetLcdFilter(ft_lib, font->lcd_filter); + if (err != 0 && err != FT_Err_Unimplemented_Feature) + goto err; FT_UInt idx = FT_Get_Char_Index(font->face, wc); - FT_Error err = FT_Load_Glyph(font->face, idx, FT_LOAD_DEFAULT); - if (err != 0) - return false; + err = FT_Load_Glyph(font->face, idx, font->load_flags); + if (err != 0) { + LOG_ERR("load failed"); + goto err; + } - err = FT_Render_Glyph(font->face->glyph, FT_RENDER_MODE_NORMAL); + err = FT_Render_Glyph(font->face->glyph, font->render_flags); if (err != 0) - return false; + goto err; assert(font->face->glyph->format == FT_GLYPH_FORMAT_BITMAP); @@ -136,17 +227,17 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, assert(stride >= bitmap->pitch); uint8_t *data = malloc(bitmap->rows * stride); + assert(bitmap->pitch >= 0); switch (bitmap->pixel_mode) { case FT_PIXEL_MODE_MONO: for (size_t r = 0; r < bitmap->rows; r++) { - for (size_t c = 0; c < bitmap->width; c++) { + for (size_t c = 0; c < (bitmap->width + 7) / 8; c++) { uint8_t v = bitmap->buffer[r * bitmap->pitch + c]; uint8_t reversed = 0; - for (size_t i = 0; i < 8; i++) { - reversed |= (v & 1) << (7 - i); - v >>= 1; - } + for (size_t i = 0; i < min(8, bitmap->width - c * 8); i++) + reversed |= ((v >> (7 - i)) & 1) << i; + data[r * stride + c] = reversed; } } @@ -163,7 +254,7 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, LOG_ERR("unimplemented FreeType bitmap pixel mode: %d", bitmap->pixel_mode); free(data); - return false; + goto err; } cairo_surface_t *surf = cairo_image_surface_create_for_data( @@ -172,7 +263,7 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) { free(data); cairo_surface_destroy(surf); - return false; + goto err; } *glyph = (struct glyph){ @@ -180,8 +271,18 @@ font_glyph_for_utf8(const struct font *font, const char *utf8, .surf = surf, .left = font->face->glyph->bitmap_left, .top = font->face->glyph->bitmap_top, + + .format = cr_format, + .width = bitmap->width, + .height = bitmap->rows, + .stride = stride, }; + mtx_unlock(&font->lock); return true; + +err: + mtx_unlock(&font->lock); + return false; } void @@ -196,4 +297,6 @@ font_destroy(struct font *font) if (font->cache[i].data != NULL) free(font->cache[i].data); } + + mtx_destroy(&font->lock); } diff --git a/font.h b/font.h index c02e4ac1..192640a9 100644 --- a/font.h +++ b/font.h @@ -1,9 +1,11 @@ #pragma once #include +#include + #include "terminal.h" bool font_from_name(const char *name, struct font *result); bool font_glyph_for_utf8( - const struct font *font, const char *utf8, struct glyph *glyph); + struct font *font, const char *utf8, struct glyph *glyph); void font_destroy(struct font *font); diff --git a/meson.build b/meson.build index 95a139d7..84fb288d 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ project('foot', 'c', is_debug_build = get_option('buildtype').startswith('debug') add_project_arguments( - ['-D_GNU_SOURCE', + ['-D_GNU_SOURCE=200809L', #'-DF00SEL_VERSION=@0@'.format(version)] + ] + (is_debug_build ? ['-D_DEBUG'] : []), diff --git a/terminal.h b/terminal.h index 89f3dc39..86b92d17 100644 --- a/terminal.h +++ b/terminal.h @@ -215,6 +215,9 @@ struct glyph { struct font { FT_Face face; + int load_flags; + int render_flags; + FT_LcdFilter lcd_filter; struct { double position; double thickness; @@ -225,6 +228,7 @@ struct font { } strikeout; struct glyph cache[256]; + mtx_t lock; }; enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; @@ -326,7 +330,6 @@ struct terminal { struct grid *grid; struct font fonts[4]; - //cairo_font_extents_t fextents; struct { int height; int descent; -- cgit v1.3