aboutsummaryrefslogtreecommitdiffstats
path: root/sway
diff options
context:
space:
mode:
authorLibravatar Hugo Osvaldo Barrera <hugo@barrera.io>2022-06-29 21:38:24 +0200
committerLibravatar Simon Ser <contact@emersion.fr>2022-07-01 13:05:58 +0200
commit75605491a54f8647740fdba75dd2ad7bae9e0ca7 (patch)
treef1d7476fbc893517ea01ed0f92841219f8b4808a /sway
parentAvoid unecessary string copy (diff)
downloadsway-75605491a54f8647740fdba75dd2ad7bae9e0ca7.tar.gz
sway-75605491a54f8647740fdba75dd2ad7bae9e0ca7.tar.zst
sway-75605491a54f8647740fdba75dd2ad7bae9e0ca7.zip
Reject font values that are invalid for pango
Use pango to parse font configuration early, and reject the command as invalid if the value is invalid for pango. Since we're already parsing the font into a `PangoFontDescription`, keep that instance around and avoid re-parsing the font each time we render text. Fixes: https://github.com/swaywm/sway/issues/6805
Diffstat (limited to 'sway')
-rw-r--r--sway/commands/font.c23
-rw-r--r--sway/config.c3
2 files changed, 25 insertions, 1 deletions
diff --git a/sway/commands/font.c b/sway/commands/font.c
index 3eda0a9c..74bb6b9f 100644
--- a/sway/commands/font.c
+++ b/sway/commands/font.c
@@ -4,6 +4,7 @@
4#include "sway/config.h" 4#include "sway/config.h"
5#include "log.h" 5#include "log.h"
6#include "stringop.h" 6#include "stringop.h"
7#include <pango/pangocairo.h>
7 8
8struct cmd_results *cmd_font(int argc, char **argv) { 9struct cmd_results *cmd_font(int argc, char **argv) {
9 struct cmd_results *error = NULL; 10 struct cmd_results *error = NULL;
@@ -22,6 +23,28 @@ struct cmd_results *cmd_font(int argc, char **argv) {
22 config->font = font; 23 config->font = font;
23 } 24 }
24 25
26 // Parse the font early so we can reject it if it's not valid for pango.
27 // Also avoids re-parsing each time we render text.
28 PangoFontDescription *font_description = pango_font_description_from_string(config->font);
29
30 const char *family = pango_font_description_get_family(font_description);
31 if (family == NULL) {
32 pango_font_description_free(font_description);
33 return cmd_results_new(CMD_FAILURE, "Invalid font family.");
34 }
35
36 const gint size = pango_font_description_get_size(font_description);
37 if (size == 0) {
38 pango_font_description_free(font_description);
39 return cmd_results_new(CMD_FAILURE, "Invalid font size.");
40 }
41
42 if (config->font_description != NULL) {
43 pango_font_description_free(config->font_description);
44 }
45
46 config->font_description = font_description;
25 config_update_font_height(); 47 config_update_font_height();
48
26 return cmd_results_new(CMD_SUCCESS, NULL); 49 return cmd_results_new(CMD_SUCCESS, NULL);
27} 50}
diff --git a/sway/config.c b/sway/config.c
index 8220ece0..b41dd871 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -243,6 +243,7 @@ static void config_defaults(struct sway_config *config) {
243 config->default_layout = L_NONE; 243 config->default_layout = L_NONE;
244 config->default_orientation = L_NONE; 244 config->default_orientation = L_NONE;
245 if (!(config->font = strdup("monospace 10"))) goto cleanup; 245 if (!(config->font = strdup("monospace 10"))) goto cleanup;
246 config->font_description = pango_font_description_from_string(config->font);
246 config->urgent_timeout = 500; 247 config->urgent_timeout = 500;
247 config->focus_on_window_activation = FOWA_URGENT; 248 config->focus_on_window_activation = FOWA_URGENT;
248 config->popup_during_fullscreen = POPUP_SMART; 249 config->popup_during_fullscreen = POPUP_SMART;
@@ -1006,7 +1007,7 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
1006void config_update_font_height(void) { 1007void config_update_font_height(void) {
1007 int prev_max_height = config->font_height; 1008 int prev_max_height = config->font_height;
1008 1009
1009 get_text_metrics(config->font, &config->font_height, &config->font_baseline); 1010 get_text_metrics(config->font_description, &config->font_height, &config->font_baseline);
1010 1011
1011 if (config->font_height != prev_max_height) { 1012 if (config->font_height != prev_max_height) {
1012 arrange_root(); 1013 arrange_root();