Last active
January 29, 2023 06:20
-
-
Save hamidb80/da2cc766e8cf80ae94b35203af221258 to your computer and use it in GitHub Desktop.
customizing subtitle font [for `.SRT`] - made for Arabic/Persian subtitles in `Kdenlive` software which uses html-4 for font settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/[strformat, strutils] | |
import sbttl | |
# ------------------------ | |
type | |
Lang = enum | |
fa, en | |
Dir = enum | |
ltr | |
rtl | |
# ------------------------ | |
iterator countdir(s, e: int, dir: Dir): int = | |
case dir | |
of ltr: | |
for i in countup(s, e): yield i | |
of rtl: | |
for i in countdown(e, s): yield i | |
func haveLang(word: string, lang: Lang): bool = | |
for ch in word: | |
case lang | |
of en: | |
if ch.toLowerAscii in 'a'..'z': | |
return true | |
of fa: | |
raise newException(ValueError, "not implmented") | |
false | |
func findFirst(sentence: seq[string], l: Lang, d: Dir): int = | |
for i in countdir(0, sentence.high, d): | |
if sentence[i].haveLang l: | |
return i | |
-1 | |
func arabicNumbers(d: char): string = | |
case d | |
of '0': "۰" | |
of '1': "۱" | |
of '2': "۲" | |
of '3': "۳" | |
of '4': "۴" | |
of '5': "۵" | |
of '6': "۶" | |
of '7': "۷" | |
of '8': "۸" | |
of '9': "۹" | |
else: raise newException(ValueError, "not a digit") | |
func arabicNumbers(s: string): string = | |
for ch in s: | |
result.add: | |
if ch.isDigit: arabicNumbers ch | |
else: $ch | |
func multiLang(text: string): string = | |
let | |
sentence = text.splitWhitespace() | |
li = sentence.findFirst(en, ltr) | |
ri = sentence.findFirst(en, rtl) | |
if li == -1: | |
text | |
else: | |
(sentence[ri+1..sentence.high] & sentence[li..ri] & sentence[0..<li]) | |
.join " " | |
# ------------------------ | |
const | |
fontName {.strdefine.} = "Vazir" | |
fontSize {.intdefine.} = 31 | |
# ------------------------ | |
when isMainModule: | |
var captions = parseSRT readfile "./subtitle.srt" | |
for c in captions.mitems: | |
c.content = | |
fmt """ | |
<font face="{fontName}" size="{fontSize}" dir="rtl"> | |
{multiLang c.content} | |
</font> | |
""" | |
writefile "final.srt", genSRT captions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment