AviSynth private function

文字を表示させるのに便利そげな関数を書いたので晒す。まず改行のある文字列の行数を数える関数。簡単のために Subtitle 版と SubtitleEx 版で分けてます。文字列操作関数の豊富な scripting language に慣れてると AviSynth でこういうことするときにちょっと大変。あー escape してる場合を考えてなかった。まぁいいか。

# for Subtitle
function CountLine(string text, int "count") {
    count = default(count, 1)

    pos = FindStr(text, "\n")
    return (pos == 0)
    \   ? count
    \   : CountLine(MidStr(text, pos + 2), count + 1)
}
# for SubtitleEx
function CountLineEx(string text, int "count") {
    count = default(count, 1)

    pos = FindStr(text, "|")
    return (pos == 0)
    \   ? count
    \   : CountLineEx(MidStr(text, pos + 1), count + 1)
}

text 挿入用関数群。 SubtitleEx を wrap しただけなんだけど last frame を指定するのはやりづらいと思ったので duration ( 表示時間 ) を指定するようにしたのと、改行を含んだ場合に縦方向に centering してくれる処理をつっこんでます。文字がつらつら並ぶ際に地味に便利。あと引数を default -> Assert で処理する流れは鉄板だね。関数定義の仕方わかってきたよ。

# print text baseline (vertical center) in centeringed big characters
function Title(clip c, string text, int "start", int "duration", int "color", int "halo", float "fontsize", string "fontfamily") {
    start      = default(start, 0)
    duration   = default(duration, FrameCount(c))
    color      = default(color, $00ffffff)
    halo       = default(halo, $00000000)
    fontsize   = default(fontsize, 48)
    fontfamily = default(fontfamily, "HG創英角ゴシックUB")

    # assertions
    Assert(start >= 0, "start must be 0 or positive.")
    Assert(duration > 0, "duration must be positive.")
    Assert(fontsize > 0, "fontsize must be positive.")

    # to fit SubtitleEx
    end = start + duration - 1

    # calculate coordinates
    delta = ((CountLineEx(text) - 1) * fontsize) / 2
    x = Width(c) / 2
    y = Height(c) / 2 - delta

    SubtitleEx(c, text, x, y, start, end, fontfamily, "c", fontsize, color, halo)
}

# print text baseline (vertical center)
function Explanation(clip c, string text, int "start", int "duration", int "color", int "halo", float "fontsize", string "fontfamily") {
    # defaults
    start      = default(start, 0)
    duration   = default(duration, FrameCount(c))
    color      = default(color, $00ffffff)
    halo       = default(halo, $00000000)
    fontsize   = default(fontsize, 24)
    fontfamily = default(fontfamily, "HG創英角ゴシックUB")

    # assertions
    Assert(start >= 0, "start must be 0 or positive.")
    Assert(duration > 0, "duration must be positive.")
    Assert(fontsize > 0, "fontsize must be positive.")

    # to fit SubtitleEx
    end = start + duration - 1

    # calculate coordinates
    delta = ((CountLineEx(text) - 1) * fontsize) / 2
    x = fontsize
    y = Height(c) / 2 - delta

    SubtitleEx(c, text, x, y, start, end, fontfamily, "", fontsize, color, halo)
}

# print centeringed text at the bottom of screen
function Caption(clip c, string text, int "start", int "duration", int "color", int "halo", float "fontsize", string "fontfamily") {
    start      = default(start, 0)
    duration   = default(duration, FrameCount(c))
    color      = default(color, $00ffffff)
    halo       = default(halo, $00000000)
    fontsize   = default(fontsize, 24)
    fontfamily = default(fontfamily, "HG創英角ゴシックUB")

    # assertions
    Assert(start >= 0, "start must be 0 or positive.")
    Assert(duration > 0, "duration must be positive.")
    Assert(fontsize > 0, "fontsize must be positive.")

    # to fit SubtitleEx
    end = start + duration - 1

    # calculate coordinates
    delta = ((CountLineEx(text) - 1) * FontSize) / 2
    x = Width(c) / 2
    y = Height(c) - FontSize - delta

    SubtitleEx(c, text, x, y, start, end, fontfamily, "c", fontsize, color, halo)
}