diff options
author | wm4 <wm4@nowhere> | 2013-04-26 19:48:13 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-04-26 20:44:18 +0200 |
commit | 7bc4b18cee071d57195147c89519f1025635fbd7 (patch) | |
tree | d788af0c15850984b73151f6ed48d6523d97d73f /sub | |
parent | 56efcc7b7f609f3dfd5a0060d0b8b700cde75890 (diff) | |
download | mpv-7bc4b18cee071d57195147c89519f1025635fbd7.tar.bz2 mpv-7bc4b18cee071d57195147c89519f1025635fbd7.tar.xz |
subassconvert: do not escape likely ASS override tags
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.)
Diffstat (limited to 'sub')
-rw-r--r-- | sub/subassconvert.c | 20 |
1 files changed, 19 insertions, 1 deletions
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 { {"<b>", "{\\b1}"}, {"</b>", "{\\b0}"}, {"<u>", "{\\u1}"}, {"</u>", "{\\u0}"}, {"<s>", "{\\s1}"}, {"</s>", "{\\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 */ |