From 4d2cadb1719675dc832b9e8cfdd00a27f9189612 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 8 Jan 2023 16:58:46 +0100 Subject: Draft implementations of libfonts_get_{default,output}_rendering_settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 1 + common.h | 15 ++++ libfonts.h | 27 +------ libfonts_get_default_font.c | 2 +- libfonts_get_default_rendering_settings.c | 108 ++++++++++++++++++++++++++++ libfonts_get_output_rendering_settings.c | 115 ++++++++++++++++++++++++++++++ 6 files changed, 242 insertions(+), 26 deletions(-) create mode 100644 libfonts_get_default_rendering_settings.c create mode 100644 libfonts_get_output_rendering_settings.c diff --git a/Makefile b/Makefile index 7790c0e..46df5b2 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ PUBLIC_OBJ =\ libfonts_encode_font_description.o\ libfonts_get_default_font.o\ libfonts_get_default_font_name.o\ + libfonts_get_default_rendering_settings.o\ libfonts_get_output_dpi.o\ libfonts_get_subpixel_order_class.o\ libfonts_unget_subpixel_order_class.o\ diff --git a/common.h b/common.h index 2451cb7..99298f1 100644 --- a/common.h +++ b/common.h @@ -13,6 +13,21 @@ #define DOUBLE_TOLERANCE 0.000001 +#define LIST_RENDERING_SETTINGS(X, _)\ + X("dpi-x", dpi_x, 96) _\ + X("dpi-y", dpi_y, 96) _\ + X("ref-width", reference_width, 0) _\ + X("ref-height", reference_height, 0) _\ + X("subpixel-order", subpixel_order, LIBFONTS_SUBPIXEL_ORDER_UNKNOWN) _\ + X("greyscale-min", min_dpsqi_for_greyscale, 0) _\ + X("subpixel-min", min_dpsqi_for_subpixel, 0) _\ + X("h-grey-text-aa", horizontal_grey_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) _\ + X("v-grey-text-aa", vertical_grey_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) _\ + X("d-grey-text-aa", diagonal_grey_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) _\ + X("h-coloured-text-aa", horizontal_colour_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) _\ + X("v-coloured-text-aa", vertical_colour_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) _\ + X("d-coloured-text-aa", diagonal_colour_text_antialiasing, LIBFONTS_ANTIALIASING_GREYSCALE) + static inline void transform(double *x_out, double *y_out, double x, double y, const struct libfonts_transformation *transformation) diff --git a/libfonts.h b/libfonts.h index 09cbb14..d4f948e 100644 --- a/libfonts.h +++ b/libfonts.h @@ -1722,24 +1722,6 @@ int libfonts_do_decoded_font_descriptions_match(const struct libfonts_font_descr * -1 on failure */ int libfonts_get_default_rendering_settings(struct libfonts_rendering_settings *, struct libfonts_context *); -/* TODO implement libfonts_get_default_rendering_settings - * - * /etc/libfonts/default-rendering.conf - * - * dpi-x = $dpi_x (default = 96) - * dpi-y = $dpi_y (default = 96) - * ref-width = $reference_width (default = 0) - * ref-height = $reference_height (default = 0) - * subpixel-order = $subpixel_order (default = LIBFONTS_SUBPIXEL_ORDER_UNKNOWN) - * greyscale-min = $min_dpsqi_for_greyscale (default = 0) - * subpixel-min = $min_dpsqi_for_subpixel (default = 0) - * h-grey-text-aa = $horizontal_grey_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - * v-grey-text-aa = $vertical_grey_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - * d-grey-text-aa = $diagonal_grey_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - * h-coloured-text-aa = $horizontal_colour_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - * v-coloured-text-aa = $vertical_colour_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - * d-coloured-text-aa = $diagonal_colour_text_antialiasing (default = LIBFONTS_ANTIALIASING_GREYSCALE) - */ /** * Get rendering settings specified for a specific output device @@ -1750,15 +1732,10 @@ int libfonts_get_default_rendering_settings(struct libfonts_rendering_settings * * @return 1 if rendering settings where found, * 0 otherwise (`*settings` will be unmodified), * -1 on failure - */ -int libfonts_get_output_rendering_settings(struct libfonts_rendering_settings *, const char *, struct libfonts_context *); -/* TODO implement libfonts_get_output_rendering_settings - * - * /etc/libfonts/output-rendering.conf * - * [$NAME] - * $SETTINGS + * @throws EINVAL `name` is `NULL` */ +int libfonts_get_output_rendering_settings(struct libfonts_rendering_settings *, const char *, struct libfonts_context *); /** diff --git a/libfonts_get_default_font.c b/libfonts_get_default_font.c index 7c21b1f..25b18e1 100644 --- a/libfonts_get_default_font.c +++ b/libfonts_get_default_font.c @@ -22,7 +22,7 @@ getn(const char *file_part1, size_t file_part1_len, const char *file_part2, cons if (!path) { enomem: errno = ENOMEM; - return NULL; + return NULL; /* TODO abort function */ } memcpy(path, file_part1, file_part1_len); diff --git a/libfonts_get_default_rendering_settings.c b/libfonts_get_default_rendering_settings.c new file mode 100644 index 0000000..0319897 --- /dev/null +++ b/libfonts_get_default_rendering_settings.c @@ -0,0 +1,108 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +static int +getn(const char *file_part1, size_t file_part1_len, const char *file_part2, struct libfonts_rendering_settings *settings) +{ + size_t file_part2_len = strlen(file_part2); + char *path; + + if (file_part1_len > SIZE_MAX - file_part2_len - 1) + goto enomem; + + path = malloc(file_part1_len + file_part2_len + 1); + if (!path) { + enomem: + errno = ENOMEM; + return 0; /* TODO abort function */ + } + + memcpy(path, file_part1, file_part1_len); + memcpy(&path[file_part1_len], file_part2, file_part2_len + 1); + + /* TODO (use LIST_RENDERING_SETTINGS) */ + + free(path); + return 1; +} + +static int +get(const char *file_part1, const char *file_part2, struct libfonts_rendering_settings *settings) +{ + return getn(file_part1, strlen(file_part1), file_part2, settings); +} + +int +libfonts_get_default_rendering_settings(struct libfonts_rendering_settings *settings, struct libfonts_context *ctx) +{ + + const char *env, *next; + char *home = NULL; + int saved_errno = errno; + int ret = 0; + size_t len; + + if (settings) { +#define X(CONFNAME, CNAME, DEFVAL) settings->CNAME = DEFVAL + LIST_RENDERING_SETTINGS(X, ;); +#undef X + } + + env = libfonts_getenv__("XDG_CONFIG_HOME", ctx); + if (env && *env) + if (get(env, "/libfonts/default-rendering.conf", settings)) + goto out; + + env = libfonts_getenv__("HOME", ctx); + if (env && *env) { + if (get(env, "/.config/libfonts/default-rendering.conf", settings)) + goto out; + if (get(env, "/.libfonts/default-rendering.conf", settings)) + goto out; + } + + home = libfonts_gethome__(ctx); + if (home && *home) { + if (get(home, "/.config/libfonts/default-rendering.conf", settings)) + goto out; + if (get(home, "/.libfonts/default-rendering.conf", settings)) + goto out; + } + + env = libfonts_getenv__("XDG_CONFIG_DIRS", ctx); + if (env && *env) { + do { + next = strchr(&env[1], ':'); + len = next ? (size_t)(next - env) : strlen(env); + if (len) + if (getn(env, len, "/libfonts/default-rendering.conf", settings)) + goto out; + env += len + 1; + } while (next); + } + + if (get("/etc", "/libfonts/default-rendering.conf", settings)) + goto out; + + ret = 0; + +out: + free(home); + errno = saved_errno; + return ret; +} + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif diff --git a/libfonts_get_output_rendering_settings.c b/libfonts_get_output_rendering_settings.c new file mode 100644 index 0000000..07a967c --- /dev/null +++ b/libfonts_get_output_rendering_settings.c @@ -0,0 +1,115 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +static int +getn(const char *file_part1, size_t file_part1_len, const char *file_part2, + struct libfonts_rendering_settings *settings, const char *name) +{ + size_t file_part2_len = strlen(file_part2); + char *path; + + if (file_part1_len > SIZE_MAX - file_part2_len - 1) + goto enomem; + + path = malloc(file_part1_len + file_part2_len + 1); + if (!path) { + enomem: + errno = ENOMEM; + return 0; /* TODO abort function */ + } + + memcpy(path, file_part1, file_part1_len); + memcpy(&path[file_part1_len], file_part2, file_part2_len + 1); + + /* TODO look for "[$name]" */ + /* TODO (use LIST_RENDERING_SETTINGS) */ + + free(path); + return 1; +} + +static int +get(const char *file_part1, const char *file_part2, struct libfonts_rendering_settings *settings, const char *name) +{ + return getn(file_part1, strlen(file_part1), file_part2, settings, name); +} + +int +libfonts_get_output_rendering_settings(struct libfonts_rendering_settings *settings, const char *name, struct libfonts_context *ctx) +{ + + const char *env, *next; + char *home = NULL; + int saved_errno = errno; + int ret = 0; + size_t len; + + if (!name) { + errno = EINVAL; + return -1; + } + + if (settings) { +#define X(CONFNAME, CNAME, DEFVAL) settings->CNAME = DEFVAL + LIST_RENDERING_SETTINGS(X, ;); +#undef X + } + + env = libfonts_getenv__("XDG_CONFIG_HOME", ctx); + if (env && *env) + if (get(env, "/libfonts/output-rendering.conf", settings, name)) + goto out; + + env = libfonts_getenv__("HOME", ctx); + if (env && *env) { + if (get(env, "/.config/libfonts/output-rendering.conf", settings, name)) + goto out; + if (get(env, "/.libfonts/output-rendering.conf", settings, name)) + goto out; + } + + home = libfonts_gethome__(ctx); + if (home && *home) { + if (get(home, "/.config/libfonts/output-rendering.conf", settings, name)) + goto out; + if (get(home, "/.libfonts/output-rendering.conf", settings, name)) + goto out; + } + + env = libfonts_getenv__("XDG_CONFIG_DIRS", ctx); + if (env && *env) { + do { + next = strchr(&env[1], ':'); + len = next ? (size_t)(next - env) : strlen(env); + if (len) + if (getn(env, len, "/libfonts/output-rendering.conf", settings, name)) + goto out; + env += len + 1; + } while (next); + } + + if (get("/etc", "/libfonts/output-rendering.conf", settings, name)) + goto out; + + ret = 0; + +out: + free(home); + errno = saved_errno; + return ret; +} + + +#else + + +int +main(void) +{ + return 0; /* TODO add test */ +} + + +#endif -- cgit v1.2.3-70-g09d2