Skip to content

Commit

Permalink
Lazy font loading
Browse files Browse the repository at this point in the history
Allows to delay loading the font file.
Font configuration is moved to standalone section.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Dec 25, 2023
1 parent 3199850 commit fbbf74e
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 183 deletions.
30 changes: 20 additions & 10 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
# 4. /etc/xdg/swayimg/config
# See `man swayimgrc` for details.

################################################################################
# General options
################################################################################
[general]

# Initial scale (optimal, fit, fill, or real)
scale = optimal

Expand All @@ -29,15 +34,6 @@ wndsize = parent
# Show image info: format, size, EXIF, and current scale (yes/no)
info = no

# Font used to show image info
font = monospace

# Font size (in pt)
font-size = 14

# Color for image info text
font-color = #cccccc

# Default order of the image list (none, alpha, or random)
order = alpha

Expand All @@ -62,7 +58,21 @@ antialiasing = no
# Enable/disable integration with Sway WM (window position managing)
sway = yes

# Section with key bindings (key -> action)
################################################################################
# Font configuration
################################################################################
[font]
# Font name
name = monospace
# Font size (in pt)
size = 14
# Font color
color = #cccccc

################################################################################
# Key binding section: key = action [parameters]
# Use the `xkbcli` tool to get key name: `xkbcli interactive-wayland`
################################################################################
[keys]
Home = first_file
g = first_file
Expand Down
9 changes: 6 additions & 3 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ Empty lines and comments are ignored.
\fIWIDTH,HEIGHT\fR: absolute size of the window.
.IP "\fBantialiasing\fR: enable/disable anti-aliasing, \fIyes\fR or [\fIno\fR];"
.IP "\fBinfo\fR: show image meta information: format, size, EXIF, and current scale, \fIyes\fR or [\fIno\fR];"
.IP "\fBfont\fR: font name used for printing image meta info, default is \fImonospace\fR;"
.IP "\fBfont-size\fR: font size (in pt) used to render image info text, default is \fI14\fR;"
.IP "\fBfont-color\fR: color used to render text, RGB hex format, default is \fI#cccccc\fR;"
.IP "\fBorder\fR: order of the image list:"
.nf
\fInone\fR: unsorted;
Expand All @@ -63,6 +60,12 @@ Empty lines and comments are ignored.
.IP "\fBslideshow\fR: slideshow image duration in seconds, default is \fI3\fR;"
.IP "\fBapp_id\fR: set a constant window class/app_id. Setting this may break the window layout;"
.IP "\fBsway\fR: enable or disable integration with sway WM (window position management), [\fIyes\fR] or \fIno\fR."
.SS Font configuration
.PP
The font configuration is described in the "font" section.
.IP "\fBname\fR: font name used for printing text, default is \fImonospace\fR;"
.IP "\fBsize\fR: font size (in pt), default is \fI14\fR;"
.IP "\fBcolor\fR: color in RGB hex format, default is \fI#cccccc\fR;"
.SS Key bindins
.PP
The key bindings are described in the "keys" section.
Expand Down
8 changes: 5 additions & 3 deletions src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool canvas_reset_window(size_t width, size_t height, size_t scale)
ctx.window.height = height;

ctx.wnd_scale = scale;
font_scale(scale);
font_set_scale(scale);

fix_viewport();

Expand Down Expand Up @@ -306,15 +306,17 @@ void canvas_print_line(argb_t* wnd, enum canvas_corner corner, const char* text)

switch (corner) {
case cc_top_right:
pos.x = ctx.window.width - font_text_width(text, 0) - TEXT_PADDING;
pos.x = ctx.window.width - font_print(NULL, NULL, NULL, text, 0) -
TEXT_PADDING;
pos.y = TEXT_PADDING;
break;
case cc_bottom_left:
pos.x = TEXT_PADDING;
pos.y = ctx.window.height - font_height() - TEXT_PADDING;
break;
case cc_bottom_right:
pos.x = ctx.window.width - font_text_width(text, 0) - TEXT_PADDING;
pos.x = ctx.window.width - font_print(NULL, NULL, NULL, text, 0) -
TEXT_PADDING;
pos.y = ctx.window.height - font_height() - TEXT_PADDING;
break;
}
Expand Down
92 changes: 33 additions & 59 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,6 @@ static bool set_boolean(const char* text, bool* value)
return rc;
}

/**
* Convert text value to RGB color.
* @param text text to convert
* @param value target variable
* @return false if value has invalid format
*/
static bool set_color(const char* text, uint32_t* value)
{
char* endptr;
unsigned long rgb;

if (*text == '#') {
++text;
}

rgb = strtoul(text, &endptr, 16);
if (*endptr || rgb > 0x00ffffff || errno == ERANGE ||
(rgb == 0 && errno == EINVAL)) {
return false;
}
*value = rgb;

return true;
}

/**
* Parse pair of numbers.
* @param text text to parse
Expand Down Expand Up @@ -201,12 +176,6 @@ static bool apply_conf(const char* key, const char* value)
return config_set_wndsize(value);
} else if (strcmp(key, "info") == 0) {
return set_boolean(value, &config.show_info);
} else if (strcmp(key, "font") == 0) {
return config_set_font_name(value);
} else if (strcmp(key, "font-size") == 0) {
return config_set_font_size(value);
} else if (strcmp(key, "font-color") == 0) {
return set_color(value, &config.font_color);
} else if (strcmp(key, "order") == 0) {
return config_set_order(value);
} else if (strcmp(key, "loop") == 0) {
Expand Down Expand Up @@ -349,9 +318,6 @@ void config_init(void)
config.geometry.y = SAME_AS_PARENT;
config.geometry.width = SAME_AS_PARENT;
config.geometry.height = SAME_AS_PARENT;
config_set_font_name("monospace");
config.font_color = 0xcccccc;
config.font_size = 14;
config.slideshow = false;
config.slideshow_sec = 3;
config.order = cfgord_alpha;
Expand Down Expand Up @@ -385,7 +351,6 @@ void config_init(void)

void config_free(void)
{
free((void*)config.font_face);
free((void*)config.app_id);
}

Expand All @@ -403,6 +368,37 @@ void config_add_section(const char* name, config_loader loader)
section->loader = loader;
}

bool config_parse_num(const char* text, ssize_t* value, int base)
{
char* endptr;
long long num;

errno = 0;
num = strtoll(text, &endptr, base);
if (!*endptr && errno == 0) {
*value = num;
return true;
}

return false;
}

bool config_parse_color(const char* text, argb_t* color)
{
ssize_t num;

if (*text == '#') {
++text;
}

if (config_parse_num(text, &num, 16) && num >= 0 && num <= 0xffffffff) {
*color = num;
return true;
}

return false;
}

bool config_set_scale(const char* val)
{
if (strcmp(val, "optimal") == 0) {
Expand All @@ -427,7 +423,7 @@ bool config_set_background(const char* val)
config.background = BACKGROUND_GRID;
} else if (strcmp(val, "none") == 0) {
config.background = COLOR_TRANSPARENT;
} else if (!set_color(val, &config.background)) {
} else if (!config_parse_color(val, &config.background)) {
fprintf(stderr, "Invalid image background: %s\n", val);
fprintf(stderr, "Expected 'none', 'grid', or RGB hex value.\n");
return false;
Expand All @@ -439,7 +435,7 @@ bool config_set_wndbkg(const char* val)
{
if (strcmp(val, "none") == 0) {
config.window = COLOR_TRANSPARENT;
} else if (!set_color(val, &config.window)) {
} else if (!config_parse_color(val, &config.window)) {
fprintf(stderr, "Invalid window background: %s\n", val);
fprintf(stderr, "Expected 'none' or RGB hex value.\n");
return false;
Expand Down Expand Up @@ -490,28 +486,6 @@ bool config_set_wndsize(const char* val)
return false;
}

bool config_set_font_name(const char* val)
{
const size_t len = strlen(val);
if (len == 0) {
fprintf(stderr, "Invalid font name\n");
return false;
}
return set_string(val, (char**)&config.font_face);
}

bool config_set_font_size(const char* val)
{
char* endptr;
const unsigned long sz = strtoul(val, &endptr, 10);
if (*endptr || sz == 0) {
fprintf(stderr, "Invalid font size\n");
return false;
}
config.font_size = sz;
return true;
}

bool config_set_order(const char* val)
{
if (strcmp(val, "none") == 0) {
Expand Down
22 changes: 17 additions & 5 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ struct config {
argb_t window; ///< Window background mode/color
enum config_scale scale; ///< Initial scale
bool show_info; ///< Show image info
const char* font_face; ///< Font name
size_t font_size; ///< Font size
argb_t font_color; ///< Font color
bool slideshow; ///< Slide show mode
size_t slideshow_sec; ///< Slide show mode timing
enum config_order order; ///< File list order
Expand All @@ -77,14 +74,29 @@ void config_free(void);
*/
void config_add_section(const char* name, config_loader loader);

/**
* Parse text to number.
* @param text text to convert
* @param color output variable
* @param base numeric base
* @return false if text has invalid format
*/
bool config_parse_num(const char* text, ssize_t* value, int base);

/**
* Parse text value to ARGB color.
* @param text text to convert
* @param color output variable
* @return false if text has invalid format
*/
bool config_parse_color(const char* text, argb_t* color);

// Configuration setters
bool config_set_scale(const char* val);
bool config_set_background(const char* val);
bool config_set_wndbkg(const char* val);
bool config_set_wndpos(const char* val);
bool config_set_wndsize(const char* val);
bool config_set_font_name(const char* val);
bool config_set_font_size(const char* val);
bool config_set_order(const char* val);
bool config_set_slideshow_sec(const char* val);
bool config_set_appid(const char* val);
Loading

0 comments on commit fbbf74e

Please sign in to comment.