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.)
This commit is contained in:
wm4 2013-04-26 19:48:13 +02:00
parent 56efcc7b7f
commit 7bc4b18cee
1 changed files with 19 additions and 1 deletions

View File

@ -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 */