Skip to content

Fixed addfont, added tests #2085

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

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Packages/vcs/vcs/Canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5452,41 +5452,51 @@ def getcolormap(self, Cp_name_src='default'):
return vcs.getcolormap(Cp_name_src)
getcolormap.__doc__ = vcs.manageElements.getcolormap.__doc__

def addfont(self, path, name=""):
def addfont(self, path, name=None, recursive=False):
"""
Add a font to VCS.

:param path: Path to the font file you wish to add (must be .ttf)
:param path: Path to the font file you wish to add (must be .ttf) or directory you want to search through.
:type path: str

:param name: Name to use to represent the font.
:type name: str

:param recursive: If path is a directory, whether to search it recursively or not.
:type recursive: bool
"""
if not os.path.exists(path):
raise ValueError('Error - The font path does not exists')
if os.path.isdir(path):
if name == 'r':
warnings.warn("Recursive font addition via the name property has been deprecated. Please use the recursive keyword argument instead.")
recursive = True
name = None
dir_files = []
files = []
if name == "":
if not recursive:
subfiles = os.listdir(path)
for file in subfiles:
dir_files.append(os.path.join(path, file))
elif name == 'r':
elif recursive:
for root, dirs, subfiles in os.walk(path):
for file in subfiles:
dir_files.append(os.path.join(root, file))
for f in dir_files:
if f.lower()[-3:]in ['ttf', 'pfa', 'pfb']:
files.append([f, ""])
files.append([f, None])
else:
files = [[path, name], ]

nms = []
for f in files:
fnm, name = f
if name is None:
name = os.path.splitext(os.path.basename(fnm))[0]
i = max(vcs.elements["fontNumber"].keys()) + 1
vcs.elements["font"][name] = fnm
vcs.elements["fontNumber"][i] = name
nms.append(name)
if len(nms) == 0:
raise vcsError('No font Loaded')
elif len(nms) > 1:
Expand Down
10 changes: 10 additions & 0 deletions testing/vcs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,16 @@ cdat_add_test(test_vcs_colorpicker_selection
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_colorpicker_selection.py
)

cdat_add_test(test_vcs_addfont
"${PYTHON_EXECUTABLE}"
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_addfont.py
)

cdat_add_test(test_vcs_addfont_directory
"${PYTHON_EXECUTABLE}"
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_addfont_directory.py
)

cdat_add_test(test_vcs_configurator_click_text
"${PYTHON_EXECUTABLE}"
${cdat_SOURCE_DIR}/testing/vcs/test_vcs_configurator_click_text.py
Expand Down
14 changes: 14 additions & 0 deletions testing/vcs/test_vcs_addfont.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import vcs, os

x = vcs.init()
font_dir = os.path.dirname(vcs.elements["font"]["Adelon"])
default_fonts = 0

for font in vcs.elements["font"].values():
if font.startswith(font_dir):
default_fonts += 1

# Test adding a single font.
font_path = os.path.join(font_dir, "Adelon_Regular.ttf")
new_font_name = x.addfont(font_path)
assert new_font_name == "Adelon_Regular"
14 changes: 14 additions & 0 deletions testing/vcs/test_vcs_addfont_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import vcs, os

x = vcs.init()
font_dir = os.path.dirname(vcs.elements["font"]["Adelon"])

orig_fonts = set()

for font in vcs.elements["font"].values():
if font.startswith(font_dir):
orig_fonts.add(font)

# Test adding a directory of fonts.
all_the_new_fonts = set(x.addfont(font_dir))
assert len(all_the_new_fonts) == len(orig_fonts)