From 1fbe520d41d8a92082826b15dbd1cc54a067fa48 Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Mon, 15 Aug 2011 22:04:02 +0200 Subject: [PATCH] Trim spaces of font family strings This adds a trimming utility function that is used for trimming strings of font requests in the font sorter. --- libass/ass_fontselect.c | 5 +++-- libass/ass_utils.c | 20 +++++++++++++++++++- libass/ass_utils.h | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/libass/ass_fontselect.c b/libass/ass_fontselect.c index 481d4c9..2ca6f9d 100644 --- a/libass/ass_fontselect.c +++ b/libass/ass_fontselect.c @@ -263,8 +263,8 @@ static char *select_font(ASS_FontSelector *priv, ASS_Library *library, req.weight = bold; req.n_fullname = 1; req.fullnames = &req_fullname; - req.fullnames[0] = (char *)family; - req.family = strdup(family); + req.fullnames[0] = trim_space(strdup(family)); + req.family = trim_space(strdup(family)); char *p = strchr(req.family, ' '); if (p) *p = 0; @@ -281,6 +281,7 @@ static char *select_font(ASS_FontSelector *priv, ASS_Library *library, && font_infos[info_index].funcs.check_glyph(font_infos[info_index].priv, code) == 0) info_index++; + free(req.fullnames[0]); free(req.family); // return best match diff --git a/libass/ass_utils.c b/libass/ass_utils.c index a6a063b..0fc8b2a 100644 --- a/libass/ass_utils.c +++ b/libass/ass_utils.c @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include "ass_library.h" #include "ass.h" @@ -331,6 +331,24 @@ void ass_msg(ASS_Library *priv, int lvl, char *fmt, ...) va_end(va); } +char *trim_space(char *str) +{ + int i; + int left = 0; + int right = strlen(str) - 1; + + while (isspace(str[left])) left++; + while (isspace(str[right])) right--; + + if (left > 0) + for (i = 0; i <= right - left; i++) + str[i] = str[left+i]; + + str[right-left+1] = '\0'; + + return str; +} + unsigned ass_utf8_get_char(char **str) { uint8_t *strp = (uint8_t *) * str; diff --git a/libass/ass_utils.h b/libass/ass_utils.h index 45ebbb6..f249bc9 100644 --- a/libass/ass_utils.h +++ b/libass/ass_utils.h @@ -90,6 +90,7 @@ int mystrtoi32(char **p, int base, int32_t *res); int32_t parse_alpha_tag(char *str); uint32_t parse_color_tag(char *str); uint32_t parse_color_header(char *str); +char *trim_space(char *str); char parse_bool(char *str); int parse_ycbcr_matrix(char *str); unsigned ass_utf8_get_char(char **str); -- 2.40.0