diff --git a/trdg/data_generator.py b/trdg/data_generator.py index 46a60df78b..2022ec4931 100644 --- a/trdg/data_generator.py +++ b/trdg/data_generator.py @@ -25,6 +25,7 @@ def generate( cls, index, text, + language, font, out_dir, size, @@ -244,9 +245,17 @@ def generate( ##################################### # Generate name for resulting image # ##################################### + if language == "ar" or language == "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]]) + # We remove spaces if space_width == 0 if space_width == 0: text = text.replace(" ", "") + if name_format == 0: image_name = "{}_{}.{}".format(text, str(index), extension) mask_name = "{}_{}_mask.png".format(text, str(index)) diff --git a/trdg/fonts/fa/BNAZANIN.TTF b/trdg/fonts/fa/BNAZANIN.TTF new file mode 100644 index 0000000000..72bbfc3803 Binary files /dev/null and b/trdg/fonts/fa/BNAZANIN.TTF differ diff --git a/trdg/run.py b/trdg/run.py index b761e24a54..bf1ef75bb1 100755 --- a/trdg/run.py +++ b/trdg/run.py @@ -405,7 +405,7 @@ def main(): args.length, args.random, args.count, lang_dict ) - if args.language == "ar": + if args.language == "ar" or args.language == "fa": from arabic_reshaper import ArabicReshaper from bidi.algorithm import get_display @@ -428,6 +428,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, @@ -471,7 +472,13 @@ def main(): file_name = str(i) + "." + args.extension if args.space_width == 0: file_name = file_name.replace(" ", "") - f.write("{} {}\n".format(file_name, strings[i])) + + if args.language == "ar" or args.language == "fa": + sentence = " ".join([get_display(arabic_reshaper.reshape(w)) for w in strings[i].split(" ")[::-1]]) + else: + sentence = strings[i] + + f.write("{} {}\n".format(file_name, sentence)) if __name__ == "__main__":