From 8f015537e7c016722dbd7fffee520461fe394f28 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 22 Jul 2022 19:52:07 +0200 Subject: Add default fonts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 1 + libfonts.h | 88 ++++++++++++++++++++++++++++++++++++++++ libfonts_get_default_font_name.c | 49 ++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 libfonts_get_default_font_name.c diff --git a/Makefile b/Makefile index 97a3499..74bb9e1 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ OBJ =\ libfonts_calculate_subpixel_order.o\ libfonts_decode_font_description.o\ libfonts_encode_font_description.o\ + libfonts_get_default_font_name.o\ libfonts_get_output_dpi.o HDR =\ diff --git a/libfonts.h b/libfonts.h index 2d8e94d..5cf2c83 100644 --- a/libfonts.h +++ b/libfonts.h @@ -3,6 +3,29 @@ #define LIBFONTS_H #include +#include + + +/** + * Style-based default fonts + */ +enum libfonts_default_font { + /** + * A proportional font without serifs + */ + LIBFONTS_DEFAULT_SANS_SERIF, + + /** + * A proportional font with serifs + */ + LIBFONTS_DEFAULT_SERIF, + + /** + * A monospace font, which may escape + * the character cell + */ + LIBFONTS_DEFAULT_MONOSPACE +}; /** @@ -505,6 +528,71 @@ struct libfonts_font_description { }; +/** + * Get the font a default font name aliases to + * + * @param font The default font + * @param name Output buffer for the font the default font is an alias + * for; will always be NUL-terminated (unless `size` is 0) + * @param size Buffer size of `name` + * @return The minimum value required on `size` for a + * complete output to `name`, or -1 on failure + * + * @throws EINVAL Unrecognised value on `font` + */ +ssize_t libfonts_get_default_font(enum libfonts_default_font, char *, size_t); +/* TODO implement libfonts_get_default_font + * + * /etc/libfonts/default-fonts.conf + * + * sans-serif = $FONTNAME + * serif = $FONTNAME + * monospace = $FONTNAME + * + * fallback, look in /etc/libfonts/default-fonts/sans-serif/ + * /etc/libfonts/default-fonts/serif/ + * /etc/libfonts/default-fonts/monospace/ + * + * as a last resort look around for some + * font that matches the specification as + * well as possible + */ + +/** + * Get the alias for a default font + * + * @param font The default font + * @param name Output buffer for the font name; will always + * be NUL-terminated (unless `size` is 0) + * @param size Buffer size of `name` + * @param index The index of the alias (some default fonts + * have multiple aliases) + * @return The minimum value required on `size` for a + * complete output to `name`, 0 if `index` is + * equal to or greater than the number of + * aliases, or -1 on failure + * + * @throws EINVAL Unrecognised value on `font` + */ +ssize_t libfonts_get_default_font_name(enum libfonts_default_font, char *, size_t, size_t); + +/** + * Get a default font a font name represents + * + * @param font Output parameter for the default font + * @param name The alias for the default font, optionally + * with style and size information + * @param style Output parameter for the offset in `name` + * where the style and size information begins; + * will be set to 0 if this information is + * excluded from `name` + * @return 1 on successful completion, 0 if `name` is not + * an alias for a default font + */ +int libfonts_get_default_font_by_name(enum libfonts_default_font *, const char *, size_t *); +/* TODO implement libfonts_get_default_font_by_name */ + + /** * Encode an X Logical Font Description (XLFD) string * diff --git a/libfonts_get_default_font_name.c b/libfonts_get_default_font_name.c new file mode 100644 index 0000000..5fe7704 --- /dev/null +++ b/libfonts_get_default_font_name.c @@ -0,0 +1,49 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +ssize_t +libfonts_get_default_font_name(enum libfonts_default_font font, char *name, size_t size, size_t index) +{ + const char *alias; + size_t len; + + if (font == LIBFONTS_DEFAULT_SANS_SERIF) { + if (index == 0) + alias = "sans-serif"; + else if (index == 1) + alias = "sans serif"; + else if (index == 2) + alias = "sans"; + else + return 0; + + } else if (font == LIBFONTS_DEFAULT_SERIF) { + if (index == 0) + alias = "serif"; + else + return 0; + + } else if (font == LIBFONTS_DEFAULT_MONOSPACE) { + if (index == 0) + alias = "monospace"; + else if (index == 1) + alias = "monospaced"; + else + return 0; + + } else { + errno = EINVAL; + return -1; + } + + len = strlen(alias); + if (size) { + size -= 1; + if (size > len) + size = len; + memcpy(name, alias, size); + name[size] = '\0'; + } + return (ssize_t)len; +} -- cgit v1.2.3-70-g09d2