diff options
author | Guido Cella <guido@guidocella.xyz> | 2022-05-01 07:29:32 +0200 |
---|---|---|
committer | Kacper Michajłow <kasper93@gmail.com> | 2024-03-21 03:20:14 +0100 |
commit | d6610a5b2f34e398e2ceba2a4da2b79e556b1c9e (patch) | |
tree | cb5d6e8777179a4383067e05c52467d198096912 /sub/osd_libass.c | |
parent | 520849dd48e34e68be09b5f4849fea1d5212fb44 (diff) | |
download | mpv-d6610a5b2f34e398e2ceba2a4da2b79e556b1c9e.tar.bz2 mpv-d6610a5b2f34e398e2ceba2a4da2b79e556b1c9e.tar.xz |
command: add escape-ass
This adds a command to escape ASS tags to remove code duplication
between sub/osd_libass.c, console.lua, osc.lua, stats.lua and any user
script that calls mp.create_osd_overlay().
A command is used instead of scripting functions so that all clients can
use this and not just use Lua and JS ones.
osd_mangle_ass() also interprets osd-sym-cc and osd-ass-cc/{0,1}, but
since they use invalid UTF-8 characters there is no risk of escape-ass
users using them by accident, like with any OSD message.
Always replacing \n with \\N in mangle_ass() even when it is not called
by escape-ass doesn't seem to cause any issue, but I made it conditional
anyway to avoid changing how all OSD messages are treated unnecessarily.
Diffstat (limited to 'sub/osd_libass.c')
-rw-r--r-- | sub/osd_libass.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/sub/osd_libass.c b/sub/osd_libass.c index 16d94b3cbf..f2de27bd64 100644 --- a/sub/osd_libass.c +++ b/sub/osd_libass.c @@ -193,7 +193,7 @@ void osd_get_function_sym(char *buffer, size_t buffer_size, int osd_function) snprintf(buffer, buffer_size, "\xFF%c", osd_function); } -static void mangle_ass(bstr *dst, const char *in) +void osd_mangle_ass(bstr *dst, const char *in, bool replace_newlines) { const char *start = in; bool escape_ass = true; @@ -213,6 +213,14 @@ static void mangle_ass(bstr *dst, const char *in) } if (escape_ass && *in == '{') bstr_xappend(NULL, dst, bstr0("\\")); + // Replace newlines with \N for escape-ass. This is necessary to apply + // ASS tags past newlines and to preserve consecutive newlines with + // osd-overlay because update_external() adds a ASS event per line. + if (replace_newlines && *in == '\n') { + bstr_xappend(NULL, dst, bstr0("\\N")); + in += 1; + continue; + } // Libass will strip leading whitespace if (in[0] == ' ' && (in == start || in[-1] == '\n')) { bstr_xappend(NULL, dst, bstr0("\\h")); @@ -231,7 +239,7 @@ static ASS_Event *add_osd_ass_event_escaped(ASS_Track *track, const char *style, const char *text) { bstr buf = {0}; - mangle_ass(&buf, text); + osd_mangle_ass(&buf, text, false); ASS_Event *e = add_osd_ass_event(track, style, buf.start); talloc_free(buf.start); return e; |