Use the cache for getting variable data for faces

This commit is contained in:
Kovid Goyal
2024-05-10 22:26:50 +05:30
parent 1763cbde46
commit 076ddd2998
2 changed files with 13 additions and 1 deletions

View File

@@ -423,6 +423,7 @@ def fc_match_postscript_name(
class Face:
path: Optional[str]
def __init__(self, descriptor: Optional[FontConfigPattern] = None, path: str = '', index: int = 0): ...
def get_variable_data(self) -> VariableData: ...
def identify_for_debug(self) -> str: ...
@@ -456,6 +457,7 @@ class CoreTextFont(TypedDict):
class CTFace:
path: Optional[str]
def __init__(self, descriptor: Optional[CoreTextFont] = None, path: str = ''): ...
def get_variable_data(self) -> VariableData: ...
def identify_for_debug(self) -> str: ...

View File

@@ -68,6 +68,16 @@ def get_variable_data_for_descriptor(d: Descriptor) -> VariableData:
return ans
def get_variable_data_for_face(d: Face) -> VariableData:
path = d.path
if not path:
return d.get_variable_data()
ans = cache_for_variable_data_by_path.get(path)
if ans is None:
ans = cache_for_variable_data_by_path[path] = d.get_variable_data()
return ans
def find_best_match_in_candidates(
candidates: Sequence[Descriptor], scorer: Scorer, is_medium_face: bool, ignore_face: Optional[Descriptor] = None
) -> Optional[Descriptor]:
@@ -293,7 +303,7 @@ def get_named_style(face: Face) -> Optional[NamedStyle]:
axis_map = face.get_variation()
if axis_map is None:
return None
vd = face.get_variable_data()
vd = get_variable_data_for_face(face)
for ns in vd['named_styles']:
if ns['axis_values'] == axis_map:
return ns