From 7bc4b18cee071d57195147c89519f1025635fbd7 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 26 Apr 2013 19:48:13 +0200 Subject: [PATCH] subassconvert: do not escape likely ASS override tags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Usually SubRip files are not expected to contain ASS override tags, but unfortunately these files seem to become more common. Example from a real file: 1 00:00:00,800 --> 00:00:15,000 {\an8}本字幕由 {\c&H26F4FF&}ShinY {\c&HFFAE1A&}深影字幕组{\c&HFFFFFF&} 原创翻译制作 subassconvert.c escaped '{', so that libass displayed the above line literally. Try to apply a simple heuristic to detect whether '{' is likely to start an ASS tag: if the string starts with '{\', and there is a closing '}', assume it's an ASS tag, otherwise escape the '{' properly. If it's a likely ASS tag, it's passed through to libass. The end result is that the above script is displayed in color, while at the same time legitimate uses of '{' and '}' should work fine. We assume that nobody uses {...} for commenting text in SubRip files. (This kind of comment is popular and legal in ASS files, though.) --- sub/subassconvert.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sub/subassconvert.c b/sub/subassconvert.c index c1eea9a01b..f292414604 100644 --- a/sub/subassconvert.c +++ b/sub/subassconvert.c @@ -55,6 +55,11 @@ static void append_text(struct line *dst, char *fmt, ...) va_end(va); } +static void append_text_n(struct line *dst, char *start, size_t length) +{ + append_text(dst, "%.*s", length, start); +} + static int indexof(const char *s, int c) { char *f = strchr(s, c); @@ -88,7 +93,7 @@ static const struct tag_conv { {"", "{\\b1}"}, {"", "{\\b0}"}, {"", "{\\u1}"}, {"", "{\\u0}"}, {"", "{\\s1}"}, {"", "{\\s0}"}, - {"{", "\\{"}, {"}", "\\}"}, + {"}", "\\}"}, {"\r\n", "\\N"}, {"\n", "\\N"}, {"\r", "\\N"}, }; @@ -417,6 +422,19 @@ void subassconvert_subrip(const char *orig, char *dest, int dest_buffer_size) sp++; line++; } + } else if (*line == '{') { + char *end = strchr(line, '}'); + if (line[1] == '\\' && end) { + // Likely ASS tag, pass them through + // Note that ASS tags like {something\an8} are legal too (i.e. + // the first character after '{' doesn't have to be '\'), but + // consider these fringe cases not worth supporting. + append_text_n(&new_line, line, end - line + 1); + line = end + 1; + } else { + append_text(&new_line, "\\{"); + line++; + } } /* Tag conversion code didn't match */