Aegisub/aegisub/automation/demos/auto3/4-text_extents.lua
Amar Takhar 91d73ec8ea SVN Transition Step 3/7
1. cd aegisub/
  2. svn mv *cpp *h src/
  3. svn mv Makefile.am MatroskaParser.c auto4_perldata.inc bitmaps boost \
     changelog.txt config gl include libosxutil libresrc md5.c msvc mythes.cxx \
     mythes.hxx res.rc src/
  4. cd ..
  5. svn mv FFmpegSource2/ INSTALL Makefile.am README  acinclude.m4 \
     autogen.sh automation/ bin build configure.in desktop dummy.txt lib \
     libass/ m4macros/ packages/ po/ scripts/ universalchardet/ aegisub/
  6. mkdir -p docs/wiki_convert
  7. svn add docs/wiki_convert
  8. cd docs
  9. svn mv aegisub_convert_docs.pl convert.bat output wiki_convert/

* See r2749 for full description.

Originally committed to SVN as r2752.
2009-03-08 08:30:39 +00:00

57 lines
2.3 KiB
Lua

-- Aegisub Automation demonstration script
-- Original written by Niels Martin Hansen
-- Given into the public domain
include("utils.lua")
name = "Text placement demo"
description = "Demonstration of the text_extents function, to do per-syllable placement of text."
version, kind, configuration = 3, 'basic_ass', {}
function process_lines(meta, styles, lines, config)
-- Prepare local variables
local output = { n=0 }
-- Loop through every line
for i = 0, lines.n-1 do
aegisub.report_progress(i/lines.n*100)
-- Only process dialogue lines
if lines[i].kind ~= "dialogue" then
output.n = output.n + 1
output[output.n] = lines[i]
else
-- This is just for making the code a bit easier to read
local line = lines[i]
-- Get the rendered size of the entire line. (Won't work if there's line breaks in it.)
local totalx, totaly = aegisub.text_extents(styles[line.style], line.text_stripped)
-- Calculate where the first syllable should be positioned, if the line is to appear centered on screen
local curx, cury = (meta.res_x - totalx) / 2, meta.res_y / 2
-- And more preparations for per-syllable placement
local startx = curx
local tstart, tend = 0, 0
-- Now process each stllable
for j = 1, line.karaoke.n-1 do
-- A shortcut variable, and, most important: a copy of the original line
local syl, syllin = line.karaoke[j], copy_line(line)
-- Calculate the ending time of this syllable
tend = tstart + syl.duration*10
-- Get the rendered size of this syllable
local extx, exty, extd, extl = aegisub.text_extents(styles[line.style], syl.text_stripped)
-- Some debug stuff...
aegisub.output_debug(string.format("text_extents returned: %d, %d, %d, %d", extx, exty, extd, extl));
-- Replace the text of the copy of the line with this syllable, moving around
syllin.text = string.format("{\\an4\\move(%d,%d,%d,%d,%d,%d)\\kf%d\\kf%d}%s", curx, cury, curx, cury-exty, tstart, tend, tstart/10, syl.duration, syl.text)
-- Add the line to the output
output.n = output.n + 1
output[output.n] = syllin
-- And prepare for next iteration
curx = curx + extx
tstart = tend
end
-- More debug stuff
aegisub.output_debug(string.format("after syllable loop: totalx=%d curx-startx=%d", totalx, curx-startx))
end
end
-- And remember to return something :)
return output
end