only import on demand
This commit is contained in:
parent
e23369df94
commit
25ef83878a
4 changed files with 722 additions and 107 deletions
|
@ -91,10 +91,24 @@ class FontFace:
|
|||
|
||||
:param glyphs: dictionary of glyphs, defaults to ``{}``
|
||||
:type glyphs: dict, optional
|
||||
:param loaded_glyphs: glyphs currently loaded
|
||||
:type loaded_glyphs: List[str], optional
|
||||
:param missing_glyphs: glyphs not present in the fontfile
|
||||
:type missing_glyphs: List[str], optional
|
||||
:param filenames: from which file is this face
|
||||
:type filenames: List[str]
|
||||
"""
|
||||
def __init__(self, glyphs = {}):
|
||||
def __init__(self,
|
||||
glyphs = {}):
|
||||
self.glyphs = glyphs
|
||||
|
||||
# lists have to be initialized in __init__
|
||||
# to be attributes per instance.
|
||||
# otherwise they are static class attributes
|
||||
self.loaded_glyphs = []
|
||||
self.missing_glyphs = []
|
||||
self.glyphs_in_fontfile = []
|
||||
self.filepaths = []
|
||||
self.unit_factor = 1.0
|
||||
|
||||
class Font:
|
||||
"""Font holds the faces and various metadata for a font
|
||||
|
@ -108,6 +122,19 @@ class Font:
|
|||
|
||||
# TODO: better class structure?
|
||||
# TODO: get fonts and faces directly
|
||||
|
||||
def register_font(font_name, face_name, glyphs_in_fontfile, filepath):
|
||||
if not fonts.keys().__contains__(font_name):
|
||||
fonts[font_name] = Font({})
|
||||
if fonts[font_name].faces.get(face_name) == None:
|
||||
fonts[font_name].faces[face_name] = FontFace({})
|
||||
fonts[font_name].faces[face_name].glyphs_in_fontfile = glyphs_in_fontfile
|
||||
else:
|
||||
fonts[font_name].faces[face_name].glyphs_in_fontfile = \
|
||||
list(set(fonts[font_name].faces[face_name].glyphs_in_fontfile + glyphs_in_fontfile))
|
||||
if filepath not in fonts[font_name].faces[face_name].filepaths:
|
||||
fonts[font_name].faces[face_name].filepaths.append(filepath)
|
||||
|
||||
|
||||
def add_glyph(font_name, face_name, glyph_id, glyph_object):
|
||||
""" add_glyph adds a glyph to a FontFace
|
||||
|
@ -125,15 +152,14 @@ def add_glyph(font_name, face_name, glyph_id, glyph_object):
|
|||
|
||||
if not fonts.keys().__contains__(font_name):
|
||||
fonts[font_name] = Font({})
|
||||
# print("is it has been added", fonts.keys())
|
||||
if fonts[font_name].faces.get(face_name) == None:
|
||||
fonts[font_name].faces[face_name] = FontFace({})
|
||||
# print("is it has been added faces", fonts[font_name].faces[face_name])
|
||||
if fonts[font_name].faces[face_name].glyphs.get(glyph_id) == None:
|
||||
fonts[font_name].faces[face_name].glyphs[glyph_id] = []
|
||||
# print("is it has been added glyph", fonts[font_name].faces[face_name].glyphs[glyph_id])
|
||||
fonts[font_name].faces[face_name].glyphs.get(glyph_id).append(glyph_object)
|
||||
|
||||
if glyph_id not in fonts[font_name].faces[face_name].loaded_glyphs:
|
||||
fonts[font_name].faces[face_name].loaded_glyphs.append(glyph_id)
|
||||
|
||||
def get_glyph(font_name, face_name, glyph_id, alternate=0):
|
||||
""" add_glyph adds a glyph to a FontFace
|
||||
|
@ -149,7 +175,7 @@ def get_glyph(font_name, face_name, glyph_id, alternate=0):
|
|||
:return: returns the glyph object, or ``None`` if it does not exist
|
||||
:rtype: `Object`
|
||||
"""
|
||||
# print(fonts)
|
||||
|
||||
if not fonts.keys().__contains__(font_name):
|
||||
print(f"ABC3D::get_glyph: font name({font_name}) not found")
|
||||
print(fonts.keys())
|
||||
|
@ -164,10 +190,32 @@ def get_glyph(font_name, face_name, glyph_id, alternate=0):
|
|||
glyphs_for_id = face.glyphs.get(glyph_id)
|
||||
if glyphs_for_id == None or len(glyphs_for_id) <= alternate:
|
||||
print(f"ABC3D::get_glyph: font({font_name}) face({face_name}) glyph({glyph_id})[{alternate}] not found")
|
||||
if glyph_id not in fonts[font_name].faces[face_name].missing_glyphs:
|
||||
fonts[font_name].faces[face_name].missing_glyphs.append(glyph_id)
|
||||
return None
|
||||
|
||||
return fonts[font_name].faces[face_name].glyphs.get(glyph_id)[alternate]
|
||||
|
||||
def test_glyphs_availability(font_name, face_name, text):
|
||||
# maybe there is NOTHING yet
|
||||
if not fonts.keys().__contains__(font_name) or \
|
||||
fonts[font_name].faces.get(face_name) == None:
|
||||
return "", "", text # <loaded>, <missing>, <maybe>
|
||||
|
||||
loaded = []
|
||||
missing = []
|
||||
maybe = []
|
||||
for c in text:
|
||||
if c in fonts[font_name].faces[face_name].loaded_glyphs:
|
||||
loaded.append(c)
|
||||
elif c in fonts[font_name].faces[face_name].glyphs_in_fontfile:
|
||||
maybe.append(c)
|
||||
else:
|
||||
if c not in fonts[font_name].faces[face_name].missing_glyphs:
|
||||
fonts[font_name].faces[face_name].missing_glyphs.append(c)
|
||||
missing.append(c)
|
||||
return ''.join(loaded), ''.join(missing), ''.join(maybe), fonts[font_name].faces[face_name].filepaths
|
||||
|
||||
def get_loaded_fonts():
|
||||
return fonts.keys()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue