]> granicus.if.org Git - libass/commitdiff
Trim spaces of font family strings
authorGrigori Goronzy <greg@chown.ath.cx>
Mon, 15 Aug 2011 20:04:02 +0000 (22:04 +0200)
committerGrigori Goronzy <greg@chown.ath.cx>
Fri, 10 Jul 2015 08:42:40 +0000 (10:42 +0200)
This adds a trimming utility function that is used for trimming strings
of font requests in the font sorter.

libass/ass_fontselect.c
libass/ass_utils.c
libass/ass_utils.h

index 481d4c99f02835cd130da682c2af7f9d5fab0ed7..2ca6f9dcbb6810fb5155a5bf8b2f8ddb009b280f 100644 (file)
@@ -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
index a6a063b1d0613d2f622e00029eaec7836ad495a1..0fc8b2a5865fe87bd03637ab7834cf8329612bb3 100644 (file)
@@ -24,7 +24,7 @@
 #include <stdint.h>
 #include <inttypes.h>
 #include <strings.h>
-#include <limits.h>
+#include <ctype.h>
 
 #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;
index 45ebbb62d73454b1bcc3a2c842dfb6061212f182..f249bc9a78d4a31371f48a01ae3a1c2bd457c8b7 100644 (file)
@@ -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);