Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Farsi Support (Fix BIDI Right-to-left Problem) #320

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions trdg/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def generate(
cls,
index: int,
text: str,
language: str,
font: str,
out_dir: str,
size: int,
Expand Down Expand Up @@ -249,6 +250,15 @@ def generate(
# Generate name for resulting image #
#####################################
# We remove spaces if space_width == 0
if language in ("ar", "fa"):
from arabic_reshaper import ArabicReshaper
from bidi.algorithm import get_display

arabic_reshaper = ArabicReshaper()
text = " ".join(
[get_display(arabic_reshaper.reshape(w)) for w in text.split(" ")[::-1]]
)

if space_width == 0:
text = text.replace(" ", "")
if name_format == 0:
Expand Down
Binary file added trdg/fonts/fa/B Titr Bold_0.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/B-NAZANIN.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Sahel.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Samim.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Tahoma Regular font.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Tanha.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Times New Roman.ttf
Binary file not shown.
Binary file added trdg/fonts/fa/Vazirmatn-Regular.ttf
Binary file not shown.
10 changes: 9 additions & 1 deletion trdg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def main():
args.length, args.random, args.count, lang_dict
)

if args.language == "ar":
if args.language in ("ar", "fa"):
from arabic_reshaper import ArabicReshaper
from bidi.algorithm import get_display

Expand All @@ -441,6 +441,7 @@ def main():
zip(
[i for i in range(0, string_count)],
strings,
[args.language] * string_count,
[fonts[rnd.randrange(0, len(fonts))] for _ in range(0, string_count)],
[args.output_dir] * string_count,
[args.format] * string_count,
Expand Down Expand Up @@ -486,6 +487,13 @@ def main():
label = strings[i]
if args.space_width == 0:
label = label.replace(" ", "")
if args.language in ("ar", "fa"):
label = " ".join(
[
get_display(arabic_reshaper.reshape(w))
for w in strings[i].split(" ")[::-1]
]
)
f.write("{} {}\n".format(file_name, label))


Expand Down