| 1 |
from dataclasses import dataclass, field |
| 2 |
from typing import Optional, Union |
| 3 |
|
| 4 |
from .Layer import Layer |
| 5 |
from .BaseObject import BaseObject, I18NDictionary |
| 6 |
from .Guide import Guide |
| 7 |
|
| 8 |
# Anything which can be varied in MVAR is a master-specific metric |
| 9 |
CORE_METRICS = [ |
| 10 |
"xHeight", |
| 11 |
"capHeight", |
| 12 |
"ascender", |
| 13 |
"descender", |
| 14 |
"italicAngle", |
| 15 |
"hheaAscender", |
| 16 |
"hheaDescender", |
| 17 |
"hheaLineGap", |
| 18 |
"winAscent", |
| 19 |
"winDescent", |
| 20 |
"typoAscender", |
| 21 |
"typoDescender", |
| 22 |
"typoLineGap", |
| 23 |
"subscriptXSize", |
| 24 |
"subscriptYSize", |
| 25 |
"subscriptXOffset", |
| 26 |
"subscriptYOffset", |
| 27 |
"superscriptXSize", |
| 28 |
"superscriptYSize", |
| 29 |
"superscriptXOffset", |
| 30 |
"superscriptYOffset", |
| 31 |
"strikeoutSize", |
| 32 |
"strikeoutPosition", |
| 33 |
"underlinePosition", |
| 34 |
"underlineThickness", |
| 35 |
"hheaCaretSlopeRise", |
| 36 |
"hheaCaretSlopeRun", |
| 37 |
"hheaCaretOffset", |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
@dataclass |
| 42 |
class _MasterFields: |
| 43 |
name: I18NDictionary |
| 44 |
id: str = field( |
| 45 |
repr=False, |
| 46 |
metadata={ |
| 47 |
"description": """An ID used to refer to this master in the |
| 48 |
`Layer._master` field. (This is allows the user to change the master name |
| 49 |
without the layers becoming lost.)""" |
| 50 |
}, |
| 51 |
) |
| 52 |
location: dict = field( |
| 53 |
default=None, |
| 54 |
metadata={ |
| 55 |
"description": """A dictionary mapping axis tags to coordinates |
| 56 |
in order to locate this master in the design space. The coordinates are in designspace units.""" |
| 57 |
}, |
| 58 |
) |
| 59 |
sparse: bool = field( |
| 60 |
default=False, |
| 61 |
repr=False, |
| 62 |
metadata={ |
| 63 |
"description": """If true, this master is sparse and may not have all glyphs""" |
| 64 |
}, |
| 65 |
) |
| 66 |
guides: [Guide] = field( |
| 67 |
default_factory=list, |
| 68 |
repr=False, |
| 69 |
metadata={"separate_items": True, "description": "A list of guides."}, |
| 70 |
) |
| 71 |
metrics: dict = field( |
| 72 |
default_factory=dict, |
| 73 |
repr=False, |
| 74 |
metadata={ |
| 75 |
"description": """A dictionary mapping metric names (string) to metric value (integer). The following |
| 76 |
metric names are reserved: `%s`. Other metrics may be added to this dictionary |
| 77 |
as needed by font clients, but their interpretation is not guaranteed to be |
| 78 |
compatible between clients.""" |
| 79 |
% (",".join(CORE_METRICS)) |
| 80 |
}, |
| 81 |
) |
| 82 |
kerning: dict = field( # I think I want this to be UFO-style (l,r) -> value |
| 83 |
default_factory=dict, |
| 84 |
repr=False, |
| 85 |
metadata={ |
| 86 |
"separate_items": True, |
| 87 |
"description": "I'll be honest, I haven't worked out how this is meant to work.", |
| 88 |
}, |
| 89 |
) |
| 90 |
font: "Font" = field( |
| 91 |
default=None, |
| 92 |
repr=False, |
| 93 |
metadata={ |
| 94 |
"python_only": True, |
| 95 |
"description": "Within the Python object, provides a reference to the font object containing this master.", |
| 96 |
}, |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
@dataclass |
| 101 |
class Master(BaseObject, _MasterFields): |
| 102 |
"""A font master.""" |
| 103 |
|
| 104 |
CORE_METRICS = CORE_METRICS |
| 105 |
|
| 106 |
def __post_init__(self): |
| 107 |
super().__post_init__() |
| 108 |
# If they smacked my name with a bare string, replace with I18NDict |
| 109 |
if isinstance(self.name, str): |
| 110 |
self.name = I18NDictionary.with_default(self.name) |
| 111 |
|
| 112 |
def get_glyph_layer(self, glyphname: str) -> Optional[Layer]: |
| 113 |
g = self.font.glyphs[glyphname] |
| 114 |
for layer in g.layers: |
| 115 |
if layer._master == self.id: |
| 116 |
return layer |
| 117 |
|
| 118 |
@property |
| 119 |
def normalized_location(self) -> dict[str, float]: |
| 120 |
return {a.tag: a.normalize_value(self.location[a.tag]) for a in self.font.axes} |
| 121 |
|
| 122 |
@property |
| 123 |
def xHeight(self) -> Union[int, float]: |
| 124 |
return self.metrics.get("xHeight", 0) |
| 125 |
|
| 126 |
@property |
| 127 |
def capHeight(self) -> Union[int, float]: |
| 128 |
return self.metrics.get("capHeight", 0) |
| 129 |
|
| 130 |
@property |
| 131 |
def ascender(self) -> Union[int, float]: |
| 132 |
return self.metrics.get("ascender", 0) |
| 133 |
|
| 134 |
@property |
| 135 |
def descender(self) -> Union[int, float]: |
| 136 |
return self.metrics.get("descender", 0) |
| 137 |
|
| 138 |
@property |
| 139 |
def valid(self) -> bool: |
| 140 |
if not self.font: |
| 141 |
return False |
| 142 |
if self.location and list(self.location.keys()) != [ |
| 143 |
n.tag for n in self.font.axes |
| 144 |
]: |
| 145 |
return False |
| 146 |
return True |