Skip to content

Commit

Permalink
SplitSections: Add script
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1t3cht committed Sep 27, 2023
1 parent 3d26379 commit ee9e775
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
35 changes: 35 additions & 0 deletions DependencyControl.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,41 @@
]
}
},
"arch.SplitSections": {
"fileBaseUrl": "@{fileBaseUrl}/macros/@{namespace}",
"url": "@{baseUrl}#@{namespace}",
"author": "arch1t3cht",
"name": "Split Tag Sections",
"description": "Split subtitle lines at tags, creating a separate event for each section",
"channels": {
"release": {
"version": "0.1.0",
"released": "2023-09-27",
"default": true,
"files": [
{
"name": ".moon",
"url": "@{fileBaseUrl}@{fileName}",
"sha1": ""
}
],
"requiredModules": [
{
"moduleName": "l0.ASSFoundation",
"name": "ASSFoundation",
"url": "https://github.com/TypesettingTools/ASSFoundation",
"version": "0.5.0",
"feed": "https://raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json"
}
]
}
},
"changelog": {
"0.1.0": [
"Initial Release. Doesn't account for newlines yet."
]
}
},
"arch.Resample": {
"fileBaseUrl": "@{fileBaseUrl}/macros/@{namespace}",
"url": "@{baseUrl}#@{namespace}",
Expand Down
99 changes: 99 additions & 0 deletions macros/arch.SplitSections.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
export script_name = "Split Tag Sections"
export script_description = "Split subtitle lines at tags, creating a separate event for each section"
export script_author = "arch1t3cht"
export script_namespace = "arch.SplitSections"
export script_version = "0.1.0"

DependencyControl = require "l0.DependencyControl"
dep = DependencyControl{
feed: "https://raw.githubusercontent.com/TypesettingTools/arch1t3cht-Aegisub-Scripts/main/DependencyControl.json",
{
{"a-mo.Line", version: "1.5.3", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"a-mo.LineCollection", version: "1.3.0", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"l0.ASSFoundation", version: "0.5.0", url: "https://github.com/TypesettingTools/ASSFoundation",
feed: "https://raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json"},
}
}
Line, LineCollection, ASS = dep\requireModules!

an_xshift = { 0, 0.5, 1, 0, 0.5, 1, 0, 0.5, 1 }
an_yshift = { 1, 1, 1, 0.5, 0.5, 0.5, 0, 0, 0 }

logger = dep\getLogger!

split = (subs, sel) ->
lines = LineCollection subs, sel, () -> true

toDelete = {}

lines\runCallback (lines, line) ->
data = ASS\parse line

efftags = data\getEffectiveTags(-1, true, true, true).tags
pos = data\getPosition()
if pos.class == ASS.Tag.Move
aegisub.log("Warning: Line #{line.humanizedNumber} has \\move. Skipping.")
return

table.insert toDelete, line

an = efftags.align.value
hasorg = #data\getTags({"origin"}) != 0

x = 0
y = 0

lineheight = 0
linedescent = 0

splitLines = {}

data\callback (section, _, i, j) ->
return unless section.class == ASS.Section.Text or section.class == ASS.Section.Drawing

-- TODO handle newlines

splitLine = Line line, lines, {ASS: {}}
splitSections = data\get ASS.Section.Tag, 1, i
splitSections[#splitSections+1] = section
splitLine.ASS = ASS.LineContents splitLine, splitSections

lines\addLine splitLine
table.insert splitLines, splitLine

if section.class == ASS.Section.Text
splitLine.width, splitLine.height, splitLine.descent = section\getTextExtents!
else
bounds = section\getBounds!
splitLine.width, splitLine.height, splitLine.descent = bounds.w, bounds.h, 0

splitLine.x = x

x += splitLine.width
lineheight = math.max(lineheight, splitLine.height)
linedescent = math.max(linedescent, splitLine.descent)


for splitLine in *splitLines
xshift = splitLine.x + an_xshift[an] * (splitLine.width - x)
yshift = (1 - an_yshift[an]) * (lineheight - splitLine.height) - (linedescent - splitLine.descent)

-- We ensured above that this is not a move tag
splitpos = splitLine.ASS\getPosition()
splitpos.x += xshift
splitpos.y += yshift

if not hasorg
splitLine.ASS\insertTags {
ASS\createTag "origin", pos.x, pos.y
}

splitLine.ASS\cleanTags 4
splitLine.ASS\commit!

lines\insertLines!
lines\deleteLines toDelete

dep\registerMacro split

0 comments on commit ee9e775

Please sign in to comment.