Skip to content

Commit 36851a9

Browse files
author
Sam Fries
committed
Fixed addfont, added tests
1 parent eb89153 commit 36851a9

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

Packages/vcs/vcs/Canvas.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5369,41 +5369,51 @@ def getcolormap(self, Cp_name_src='default'):
53695369
return vcs.getcolormap(Cp_name_src)
53705370
getcolormap.__doc__ = vcs.manageElements.getcolormap.__doc__
53715371

5372-
def addfont(self, path, name=""):
5372+
def addfont(self, path, name=None, recursive=False):
53735373
"""
53745374
Add a font to VCS.
53755375
5376-
:param path: Path to the font file you wish to add (must be .ttf)
5376+
:param path: Path to the font file you wish to add (must be .ttf) or directory you want to search through.
53775377
:type path: str
53785378
53795379
:param name: Name to use to represent the font.
53805380
:type name: str
5381+
5382+
:param recursive: If path is a directory, whether to search it recursively or not.
5383+
:type recursive: bool
53815384
"""
53825385
if not os.path.exists(path):
53835386
raise ValueError('Error - The font path does not exists')
53845387
if os.path.isdir(path):
5388+
if name == 'r':
5389+
warnings.warn("Recursive font addition via the name property has been deprecated. Please use the recursive keyword argument instead.")
5390+
recursive = True
5391+
name = None
53855392
dir_files = []
53865393
files = []
5387-
if name == "":
5394+
if not recursive:
53885395
subfiles = os.listdir(path)
53895396
for file in subfiles:
53905397
dir_files.append(os.path.join(path, file))
5391-
elif name == 'r':
5398+
elif recursive:
53925399
for root, dirs, subfiles in os.walk(path):
53935400
for file in subfiles:
53945401
dir_files.append(os.path.join(root, file))
53955402
for f in dir_files:
53965403
if f.lower()[-3:]in ['ttf', 'pfa', 'pfb']:
5397-
files.append([f, ""])
5404+
files.append([f, None])
53985405
else:
53995406
files = [[path, name], ]
54005407

54015408
nms = []
54025409
for f in files:
54035410
fnm, name = f
5411+
if name is None:
5412+
name = os.path.splitext(os.path.basename(fnm))[0]
54045413
i = max(vcs.elements["fontNumber"].keys()) + 1
54055414
vcs.elements["font"][name] = fnm
54065415
vcs.elements["fontNumber"][i] = name
5416+
nms.append(name)
54075417
if len(nms) == 0:
54085418
raise vcsError('No font Loaded')
54095419
elif len(nms) > 1:

testing/vcs/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,16 @@ cdat_add_test(test_vcs_colorpicker_selection
983983
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_colorpicker_selection.py
984984
)
985985

986+
cdat_add_test(test_vcs_addfont
987+
"${PYTHON_EXECUTABLE}"
988+
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_addfont.py
989+
)
990+
991+
cdat_add_test(test_vcs_addfont_directory
992+
"${PYTHON_EXECUTABLE}"
993+
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_addfont_directory.py
994+
)
995+
986996
cdat_add_test(test_vcs_configurator_click_text
987997
"${PYTHON_EXECUTABLE}"
988998
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_configurator_click_text.py

testing/vcs/test_vcs_addfont.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import vcs, os
2+
3+
x = vcs.init()
4+
font_dir = os.path.dirname(vcs.elements["font"]["Adelon"])
5+
default_fonts = 0
6+
7+
for font in vcs.elements["font"].values():
8+
if font.startswith(font_dir):
9+
default_fonts += 1
10+
11+
# Test adding a single font.
12+
font_path = os.path.join(font_dir, "Adelon_Regular.ttf")
13+
new_font_name = x.addfont(font_path)
14+
assert new_font_name == "Adelon_Regular"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import vcs, os
2+
3+
x = vcs.init()
4+
font_dir = os.path.dirname(vcs.elements["font"]["Adelon"])
5+
6+
orig_fonts = set()
7+
8+
for font in vcs.elements["font"].values():
9+
if font.startswith(font_dir):
10+
orig_fonts.add(font)
11+
12+
# Test adding a directory of fonts.
13+
all_the_new_fonts = set(x.addfont(font_dir))
14+
assert len(all_the_new_fonts) == len(orig_fonts)

0 commit comments

Comments
 (0)