From 09cd27b6889d2a34acecd0026c10857ea772edbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 26 Dec 2018 15:59:16 +0100 Subject: [PATCH] decoration/underline: draws a line at the bottom of the particle --- CMakeLists.txt | 1 + config.c | 22 +++++++++++++++++----- decorations/underline.c | 41 +++++++++++++++++++++++++++++++++++++++++ decorations/underline.h | 6 ++++++ 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 decorations/underline.c create mode 100644 decorations/underline.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c216bc..3e6b28f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ add_executable(f00bar yml.c yml.h decorations/background.c decorations/background.h + decorations/underline.c decorations/underline.h particles/list.c particles/list.h particles/map.c particles/map.h diff --git a/config.c b/config.c index 12cc6da..5e30473 100644 --- a/config.c +++ b/config.c @@ -9,6 +9,7 @@ #include "decoration.h" #include "decorations/background.h" +#include "decorations/underline.h" #include "particle.h" #include "particles/list.h" @@ -100,6 +101,20 @@ deco_background_from_config(const struct yml_node *node) return deco_background(color_from_hexstr(yml_value_as_string(color))); } +static struct deco * +deco_underline_from_config(const struct yml_node *node) +{ + assert(yml_is_dict(node)); + + const struct yml_node *size = yml_get_value(node, "size"); + const struct yml_node *color = yml_get_value(node, "color"); + assert(yml_is_scalar(size)); + assert(yml_is_scalar(color)); + + return deco_underline( + yml_value_as_int(size), color_from_hexstr(yml_value_as_string(color))); +} + static struct deco * deco_from_config(const struct yml_node *node) { @@ -117,6 +132,8 @@ deco_from_config(const struct yml_node *node) if (strcmp(type, "background") == 0) return deco_background_from_config(deco_data); + else if (strcmp(type, "underline") == 0) + return deco_underline_from_config(deco_data); else assert(false); } @@ -268,11 +285,6 @@ particle_ramp_from_config(const struct yml_node *node, const struct font *parent static struct particle * particle_from_config(const struct yml_node *node, const struct font *parent_font) { -#if 0 - const struct yml_node *deco_node = yml_get_value(node, "deco"); - - assert(deco_node == NULL || yml_is_dict(deco_node)); -#endif assert(yml_is_dict(node)); assert(yml_dict_length(node) == 1); diff --git a/decorations/underline.c b/decorations/underline.c new file mode 100644 index 0000000..0b7ce27 --- /dev/null +++ b/decorations/underline.c @@ -0,0 +1,41 @@ +#include "underline.h" + +#include + +struct private { + int size; + struct rgba color; +}; + +static void +destroy(struct deco *deco) +{ + struct private *d = deco->private; + free(d); + free(deco); +} + +static void +expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height) +{ + const struct private *d = deco->private; + cairo_set_source_rgba( + cr, d->color.red, d->color.green, d->color.blue, d->color.alpha); + cairo_rectangle(cr, x, y + height - d->size, width, d->size); + cairo_fill(cr); +} + +struct deco * +deco_underline(int size, struct rgba color) +{ + struct private *priv = malloc(sizeof(*priv)); + priv->size = size; + priv->color = color; + + struct deco *deco = malloc(sizeof(*deco)); + deco->private = priv; + deco->expose = &expose; + deco->destroy = &destroy; + + return deco; +} diff --git a/decorations/underline.h b/decorations/underline.h new file mode 100644 index 0000000..7b69ef6 --- /dev/null +++ b/decorations/underline.h @@ -0,0 +1,6 @@ +#pragma once + +#include "../color.h" +#include "../decoration.h" + +struct deco *deco_underline(int size, struct rgba color);