aboutsummaryrefslogtreecommitdiffstats
path: root/macros/garret.inverse-glow.lua
blob: ec3edd1e4bd3f273a2896cafae732487cb3ebbf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
script_name = "Inverse Glow"
script_description = "glow but it goes inside the letter"
script_author = "garret"
script_version = "1.0.1"
script_namespace = "garret.inverse-glow"

local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl")
local util
if haveDepCtrl then
    depctrl = DependencyControl({
        --feed="TODO",
        { "aegisub.util" },
    })
    util = depctrl:requireModules()
else
    util = require("aegisub.util")
end

local patterns = { tags_text = "(%b{})(.*)", bord = "\\bord(%d+)", dark = "\\c(&?H*%x+&?)", light = "\\3c(&?H*%x+&?)" }

local function main(subs, sel)
    for i = #sel, 1, -1 do
        local line = subs[sel[i]]
        local tags, text = line.text:match(patterns.tags_text)
        local blur = tags:match(patterns.bord) -- \bord = value of \blur
        local dark = tags:match(patterns.dark) -- \c = colour of dark layer
        local light = tags:match(patterns.light) -- \3c = colour of light layer

        if tags and blur and dark and light then -- skip lines that don't have everything needed
            tags = tags:gsub(patterns.bord, "") -- remove \bord
            tags = tags:gsub(patterns.dark, "") -- and both colours
            tags = tags:gsub(patterns.light, "")

            -- light layer
            local light_line = util.copy(line)
            light_line.layer = line.layer + 1
            local light_tags = tags:gsub("}", "\\bord0\\shad0\\c" .. light .. "}") -- } (end of tag block) --> \cLightColour}
            light_line.text = light_tags .. text
            subs[sel[i]] = light_line

            local dark_line = util.copy(line)
            dark_line.layer = line.layer + 2
            local dark_tags = tags:gsub("}", "\\bord0\\shad0\\c" .. dark .. "\\blur" .. blur .. "}") -- } -> \cDarkColour\blurBord}
            dark_line.text = dark_tags .. text
            subs.insert(sel[i] + 1, dark_line)
        end
    end
    aegisub.set_undo_point(script_name)
end

if haveDepCtrl then
    depctrl:registerMacro(main)
else
    aegisub.register_macro(script_name, script_description, main)
end