Skip to content

Commit ecede7d

Browse files
committed
Add Split Text
1 parent 30766ab commit ecede7d

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

DependencyControl.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,39 @@
304304
"name": "ILL - Change Alignment",
305305
"description": "Changes the alignment of a text or shape without changing its original position",
306306
"author": "ILLTeam"
307+
},
308+
"ILL.SplitText": {
309+
"url": "@{baseUrl}#@{namespace}",
310+
"fileBaseUrl": "@{fileBaseUrl}macros/@{namespace}",
311+
"channels": {
312+
"main": {
313+
"default": true,
314+
"version": "2.0.0",
315+
"released": "2025-05-03",
316+
"files": [
317+
{
318+
"sha1": "6c1a5debebe1f72227e548fe126c259b6c3354d4",
319+
"url": "@{fileBaseUrl}@{fileName}",
320+
"name": ".moon"
321+
}
322+
],
323+
"requiredModules": [
324+
{
325+
"moduleName": "ILL.ILL",
326+
"feed": "@{feed:ILL}",
327+
"version": "1.6.1"
328+
}
329+
]
330+
}
331+
},
332+
"changelog": {
333+
"2.0.0": [
334+
"Initial depctrl release"
335+
]
336+
},
337+
"name": "ILL - Split Text",
338+
"description": "Splits the text in several ways",
339+
"author": "ILLTeam"
307340
}
308341
},
309342
"modules": {

macros/ILL.SplitText.moon

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export script_name = "ILL - Split Text"
2+
export script_description = "Splits the text in several ways"
3+
export script_version = "2.0.0"
4+
export script_author = "ILLTeam"
5+
export script_namespace = "ILL.SplitText"
6+
7+
haveDepCtrl, DependencyControl = pcall require, "l0.DependencyControl"
8+
9+
local depctrl, ILL, Ass, Line
10+
if haveDepCtrl
11+
depctrl = DependencyControl {
12+
feed: "https://raw.githubusercontent.com/TypesettingTools/ILL-Aegisub-Scripts/main/DependencyControl.json",
13+
{
14+
{
15+
"ILL.ILL"
16+
version: "1.6.1"
17+
url: "https://github.com/TypesettingTools/ILL-Aegisub-Scripts/"
18+
feed: "https://raw.githubusercontent.com/TypesettingTools/ILL-Aegisub-Scripts/main/DependencyControl.json"
19+
}
20+
}
21+
}
22+
ILL = depctrl\requireModules!
23+
else
24+
ILL = require "ILL.ILL"
25+
26+
{:Ass, :Line} = ILL
27+
28+
main = (mode) ->
29+
(sub, sel, activeLine) ->
30+
ass = Ass sub, sel, activeLine
31+
for l, s, i, n in ass\iterSel!
32+
ass\progressLine s, i, n
33+
ass\removeLine l, s
34+
Line.extend ass, l
35+
unless l.isShape
36+
for line in *(mode == "chars" and Line.chars(ass, l, true) or Line.words(ass, l, true))
37+
fr = line.data.angle != 0
38+
if fr or line.text\existsTagOr "frx", "fry", "frz"
39+
line.tags\insert {{"org", line.data.org}, true}
40+
line.tags\insert {{"pos", Line.reallocate l, line}, true}
41+
line.text\modifyBlock line.tags
42+
ass\insertLine line, s
43+
else
44+
ass\warning s, "Only divite text not shapes"
45+
return ass\getNewSelection!
46+
47+
if haveDepCtrl
48+
depctrl\registerMacros {
49+
{"By Chars", "", main "chars"}
50+
{"By Words", "", main "words"}
51+
}
52+
else
53+
aegisub.register_macro "#{script_name}/By Chars", "", main "chars"
54+
aegisub.register_macro "#{script_name}/By Words", "", main "words"

modules/ILL/ILL.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module_version = "1.6.0"
1+
module_version = "1.6.1"
22

33
haveDepCtrl, DependencyControl = pcall require, "l0.DependencyControl"
44

modules/ILL/ILL/Ass/Line.moon

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,107 @@ class Line
314314
Line.process ass, l
315315
unless l.isShape
316316
l.lines = Line.lineBreaks ass, l, noblank
317+
l.extended = true
318+
319+
-- splits the text word by word
320+
words: (ass, l) ->
321+
if l.extended
322+
words = {n: 0}
323+
for i = 1, l.lines.n
324+
lineBreak = l.lines[i]
325+
left = switch l.data.an
326+
when 1, 4, 7 then l.eff_margin_l
327+
when 2, 5, 8 then (ass.meta.res_x - l.eff_margin_l - l.eff_margin_r - lineBreak.width) / 2 + l.eff_margin_l
328+
when 3, 6, 9 then ass.meta.res_x - l.eff_margin_r - lineBreak.width
329+
for j = 1, lineBreak.n
330+
lineTags = lineBreak[j]
331+
lineTagsText = lineTags.text_stripped
332+
lineTagsTags = lineTags.tags\get!
333+
for prevspace, wordText, postspace in lineTagsText\gmatch "(%s*)(%S+)(%s*)"
334+
word = Table.copy lineTags
335+
word.tags = Tags lineTagsTags
336+
word.text = Text wordText
337+
word.text.tagsBlocks[1] = Tags lineTagsTags
338+
word.text_stripped = wordText
339+
font = Font word.data
340+
textExtents = font\getTextExtents wordText
341+
prevspace = prevspace\len!
342+
postspace = postspace\len!
343+
spacewidth = font\getTextExtents(" ").width
344+
left += prevspace * spacewidth
345+
word.width = textExtents.width * ass.meta.video_x_correct_factor
346+
word.left = left
347+
word.center = left + word.width * 0.5
348+
word.right = left + word.width
349+
word.top = lineTags.top
350+
word.middle = lineTags.middle
351+
word.bottom = lineTags.bottom
352+
word.x = switch l.data.an
353+
when 1, 4, 7 then word.left
354+
when 2, 5, 8 then word.center
355+
when 3, 6, 9 then word.right
356+
word.y = lineTags.y
357+
left += word.width + postspace * spacewidth
358+
words.n += 1
359+
words[words.n] = word
360+
return words
361+
else
362+
error "You have to extend the line before you get the words", 2
363+
364+
-- splits the text character by character
365+
chars: (ass, l, noblank) ->
366+
if l.extended
367+
chars = {n: 0}
368+
for i = 1, l.lines.n
369+
lineBreak = l.lines[i]
370+
left = switch l.data.an
371+
when 1, 4, 7 then l.eff_margin_l
372+
when 2, 5, 8 then (ass.meta.res_x - l.eff_margin_l - l.eff_margin_r - lineBreak.width) / 2 + l.eff_margin_l
373+
when 3, 6, 9 then ass.meta.res_x - l.eff_margin_r - lineBreak.width
374+
for j = 1, lineBreak.n
375+
lineTags = lineBreak[j]
376+
lineTagsText = lineTags.text_stripped
377+
lineTagsTags = lineTags.tags\get!
378+
for ci, charText in UTF8(lineTagsText)\chars!
379+
char = Table.copy lineTags
380+
char.tags = Tags lineTagsTags
381+
char.text = Text charText
382+
char.text.tagsBlocks[1] = Tags lineTagsTags
383+
char.text_stripped = charText
384+
font = Font char.data
385+
textExtents = font\getTextExtents charText
386+
char.width = textExtents.width * ass.meta.video_x_correct_factor
387+
char.left = left
388+
char.center = left + char.width * 0.5
389+
char.right = left + char.width
390+
char.top = lineTags.top
391+
char.middle = lineTags.middle
392+
char.bottom = lineTags.bottom
393+
char.x = switch l.data.an
394+
when 1, 4, 7 then char.left
395+
when 2, 5, 8 then char.center
396+
when 3, 6, 9 then char.right
397+
char.y = lineTags.y
398+
left += char.width
399+
chars.n += 1
400+
chars[chars.n] = char
401+
if noblank
402+
charsNoblank = {n: 0}
403+
for char in *chars
404+
unless Util.isBlank char.text_stripped
405+
charsNoblank.n += 1
406+
charsNoblank[charsNoblank.n] = char
407+
return charsNoblank
408+
return chars
409+
else
410+
error "You have to extend the line before you get the characters", 2
317411

318412
-- updates line information
319413
update: (ass, l, noblank) ->
320414
l.lines = nil
321415
l.data = nil
322416
l.styleref = nil
417+
l.extended = false
323418
l.text = l.text\__tostring!
324419
Line.extend ass, l, noblank
325420

0 commit comments

Comments
 (0)