Skip to content

Commit

Permalink
Add background color option for text
Browse files Browse the repository at this point in the history
Allows to specify background color for rendered text, disabled by
default.

Resolves #228.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Dec 2, 2024
1 parent bb31a18 commit 1214ec8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ size = 14
# Font color (RGBA)
color = #ccccccff
# Shadow color (RGBA)
shadow = #000000a0
shadow = #000000d0
# Background color (RGBA)
shadow = #00000000

This comment has been minimized.

Copy link
@charbelnicolas

charbelnicolas Dec 2, 2024

Shouldn't this be name background?

This comment has been minimized.

Copy link
@artemsen

artemsen Dec 2, 2024

Author Owner

Oops...
Thank you!


################################################################################
# Image meta info scheme (format, size, EXIF, etc)
Expand Down
5 changes: 4 additions & 1 deletion extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ Set the font size (in pt), default is \fI14\fR.
.IP "\fBcolor\fR = \fI#COLOR\fR"
Set text color in RGBA format, default is \fI#ccccccff\fR.
.\" ----------------------------------------------------------------------------
.IP "\fBbackground\fR = \fI#COLOR\fR"
Text background color, default is \fI#00000000\fR (none).
.\" ----------------------------------------------------------------------------
.IP "\fBshadow\fR = \fI#COLOR\fR"
Draw text shadow with specified color, default is \fI#000000a0\fR.
Draw text shadow with specified color, default is \fI#000000d0\fR.
To disable shadow use fully transparent color \fI#00000000\fR.
.\" ****************************************************************************
.\" Text info config section
Expand Down
3 changes: 2 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static const struct config_default defaults[] = {
{ "font", "name", "monospace" },
{ "font", "size", "14" },
{ "font", "color", "#ccccccff" },
{ "font", "shadow", "#000000a0" },
{ "font", "shadow", "#000000d0" },
{ "font", "background", "#00000000" },

{ "info", "show", "yes" },
{ "info", "info_timeout", "5" },
Expand Down
20 changes: 14 additions & 6 deletions src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
#define CFG_SIZE_DEF 14
#define CFG_COLOR "color"
#define CFG_COLOR_DEF ARGB(0xff, 0xcc, 0xcc, 0xcc)
#define CFG_BKG "background"
#define CFG_BKG_DEF ARGB(0, 0, 0, 0)
#define CFG_SHADOW "shadow"
#define CFG_SHADOW_DEF ARGB(0x80, 0, 0, 0)
#define CFG_SHADOW_DEF ARGB(0xd0, 0, 0, 0)

#define POINT_FACTOR 64.0 // default points per pixel for 26.6 format
#define SPACE_WH_REL 2.0

/** Font context. */
struct font {
FT_Library lib; ///< Font lib instance
FT_Face face; ///< Font face instance
argb_t color; ///< Font color
argb_t shadow; ///< Font shadow color
FT_Library lib; ///< Font lib instance
FT_Face face; ///< Font face instance
argb_t color; ///< Font color
argb_t shadow; ///< Font shadow color
argb_t background; ///< Font background
};

/** Global font context instance. */
Expand Down Expand Up @@ -148,8 +151,9 @@ void font_init(struct config* cfg)
config_get_num(cfg, CFG_SECTION, CFG_SIZE, 1, 256, CFG_SIZE_DEF);
FT_Set_Char_Size(ctx.face, font_size * POINT_FACTOR, 0, 96, 0);

// color and shdow parameters
// color/background/shadow parameters
ctx.color = config_get_color(cfg, CFG_SECTION, CFG_COLOR, CFG_COLOR_DEF);
ctx.background = config_get_color(cfg, CFG_SECTION, CFG_BKG, CFG_BKG_DEF);
ctx.shadow = config_get_color(cfg, CFG_SECTION, CFG_SHADOW, CFG_SHADOW_DEF);
}

Expand Down Expand Up @@ -226,6 +230,10 @@ bool font_render(const char* text, struct text_surface* surface)
void font_print(struct pixmap* wnd, ssize_t x, ssize_t y,
const struct text_surface* text)
{
if (ARGB_GET_A(ctx.background)) {
pixmap_blend(wnd, x, y, text->width, text->height, ctx.background);
}

if (ARGB_GET_A(ctx.shadow)) {
ssize_t shadow_offset = text->height / 16;
if (shadow_offset < 1) {
Expand Down
5 changes: 0 additions & 5 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,6 @@ static void print_help(struct pixmap* window)
top = window->height / 2 - (rows * line_height) / 2;
}

// darken text block background
pixmap_blend(window, left - TEXT_PADDING, top - TEXT_PADDING,
total_width + TEXT_PADDING, rows * line_height + TEXT_PADDING,
ARGB(0xa0, 0, 0, 0));

// put text on window
for (size_t col = 0; col < columns; ++col) {
size_t y = top;
Expand Down

1 comment on commit 1214ec8

@charbelnicolas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Some left and right padding for the background would be nice though :)

Please sign in to comment.