Skip to content

Commit 7cf75c1

Browse files
authored
Add --font_dir argument
1 parent 6913757 commit 7cf75c1

5 files changed

Lines changed: 42 additions & 5 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: python
22
python:
3-
- "3.5"
3+
- "3.6"
44
install:
55
- pip install -r requirements-hw.txt
66
- pip install codecov

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ docker run -v /output/path/:/app/out/ -t belval/trdg:latest trdg [args]
3131
The path (`/output/path/`) must be absolute.
3232

3333
## New
34+
- Add `--font_dir` argument to specify the fonts to use
3435
- Add `--output_mask` to output character-level mask for each image
3536
- Add `--character_spacing` to control space between characters (in pixels)
3637
- Add python module

tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,33 @@ def test_personalfont_unlocated(self):
803803
self.assertTrue(len(os.listdir("tests/out/")) == 0)
804804
empty_directory("tests/out/")
805805

806+
def test_personalfont_directory(self):
807+
args = [
808+
"python3", "run.py",
809+
"--font_dir",
810+
"fonts/latin/",
811+
"-c",
812+
"1",
813+
"--output_dir",
814+
"../tests/out/",
815+
]
816+
subprocess.Popen(args, cwd="trdg/").wait()
817+
self.assertTrue(len(os.listdir("tests/out/")) == 1)
818+
empty_directory("tests/out/")
819+
820+
def test_personalfont_directory_unlocated(self):
821+
args = [
822+
"python3", "run.py",
823+
"--font_dir",
824+
"fonts/void/",
825+
"-c",
826+
"1",
827+
"--output_dir",
828+
"../tests/out/",
829+
]
830+
subprocess.Popen(args, cwd="trdg/").wait()
831+
self.assertTrue(len(os.listdir("tests/out/")) == 0)
832+
empty_directory("tests/out/")
806833

807834
# def test_word_count(self):
808835
# args = ['python3', 'run.py', '-c', '1', '-w', '5']

trdg/generators/from_strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ def next(self):
9494
self.output_mask,
9595
),
9696
self.strings[(self.generated_count - 1) % len(self.strings)],
97-
)
97+
)

trdg/run.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ def parse_arguments():
275275
parser.add_argument(
276276
"-ft", "--font", type=str, nargs="?", help="Define font to be used"
277277
)
278+
parser.add_argument(
279+
"-fd", "--font_dir", type=str, nargs="?", help="Define a font directory to be used"
280+
)
278281
parser.add_argument(
279282
"-ca",
280283
"--case",
@@ -304,13 +307,19 @@ def main():
304307
lang_dict = load_dict(args.language)
305308

306309
# Create font (path) list
307-
if not args.font:
308-
fonts = load_fonts(args.language)
309-
else:
310+
if args.font_dir:
311+
fonts = [
312+
os.path.join(args.font_dir, p)
313+
for p in os.listdir(args.font_dir)
314+
if os.path.splitext(p)[1] == ".ttf"
315+
]
316+
elif args.font:
310317
if os.path.isfile(args.font):
311318
fonts = [args.font]
312319
else:
313320
sys.exit("Cannot open font")
321+
else:
322+
fonts = load_fonts(args.language)
314323

315324
# Creating synthetic sentences (or word)
316325
strings = []

0 commit comments

Comments
 (0)