| 1 |
from dataclasses import dataclass, field |
| 2 |
from functools import cached_property |
| 3 |
from typing import Dict, List, Optional |
| 4 |
|
| 5 |
from fontTools.ufoLib.pointPen import ( |
| 6 |
PointToSegmentPen, |
| 7 |
SegmentToPointPen, |
| 8 |
AbstractPointPen, |
| 9 |
) |
| 10 |
from fontTools.pens.boundsPen import BoundsPen |
| 11 |
from fontTools.pens.recordingPen import DecomposingRecordingPen |
| 12 |
|
| 13 |
from .BaseObject import BaseObject, Color |
| 14 |
from .Guide import Guide |
| 15 |
from .Anchor import Anchor |
| 16 |
from .Node import Node |
| 17 |
from .Shape import Shape |
| 18 |
import uuid |
| 19 |
|
| 20 |
|
| 21 |
@dataclass |
| 22 |
class _LayerFields: |
| 23 |
width: int = 0 |
| 24 |
height: int = 0 |
| 25 |
vertWidth: Optional[int] = None |
| 26 |
name: str = None |
| 27 |
_master: str = None |
| 28 |
id: str = field(default_factory=lambda: str(uuid.uuid1())) |
| 29 |
guides: List[Guide] = field(default_factory=list, repr=False) |
| 30 |
shapes: List[Shape] = field( |
| 31 |
default_factory=list, repr=False, metadata={"separate_items": True} |
| 32 |
) |
| 33 |
anchors: List[Anchor] = field(default_factory=list, repr=False) |
| 34 |
color: Color = None |
| 35 |
layerIndex: int = 0 |
| 36 |
# hints: List[Hint] |
| 37 |
background: Optional[str] = field(default=None, repr=False) |
| 38 |
isBackground: bool = field(default=False, repr=False) |
| 39 |
location: List[float] = None |
| 40 |
_font: Optional["Font"] = field( |
| 41 |
default=None, repr=False, metadata={"python_only": True} |
| 42 |
) |
| 43 |
_glyph: Optional["Glyph"] = field( |
| 44 |
default=None, repr=False, metadata={"python_only": True} |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
@dataclass |
| 49 |
class Layer(BaseObject, _LayerFields): |
| 50 |
@property |
| 51 |
def master(self): |
| 52 |
assert self._font |
| 53 |
if not self._master: |
| 54 |
return None |
| 55 |
return self._font.master(self._master) |
| 56 |
|
| 57 |
@property |
| 58 |
def paths(self) -> List[Shape]: |
| 59 |
return [x for x in self.shapes if x.is_path] |
| 60 |
|
| 61 |
@property |
| 62 |
def components(self) -> List[Shape]: |
| 63 |
return [x for x in self.shapes if x.is_component] |
| 64 |
|
| 65 |
def recursive_component_set(self): |
| 66 |
mine = set([x.ref for x in self.components]) |
| 67 |
theirs = set() |
| 68 |
for c in mine: |
| 69 |
other_layer = self.master.get_glyph_layer(c) |
| 70 |
theirs |= other_layer.recursive_component_set() |
| 71 |
return mine | theirs |
| 72 |
|
| 73 |
def _background_of(self) -> Optional["Layer"]: |
| 74 |
for layer in self._glyph.layers: |
| 75 |
if layer.background == self.id: |
| 76 |
return layer |
| 77 |
|
| 78 |
def _background_layer(self) -> Optional["Layer"]: |
| 79 |
if not self.background: |
| 80 |
return |
| 81 |
for layer in self._glyph.layers: |
| 82 |
if layer.id == self.background: |
| 83 |
return layer |
| 84 |
|
| 85 |
def _nested_component_dict(self) -> Dict[str, "Layer"]: |
| 86 |
result = {} |
| 87 |
todo = [x.ref for x in self.components] |
| 88 |
while todo: |
| 89 |
current = todo.pop() |
| 90 |
if current in result: |
| 91 |
continue |
| 92 |
if self.master: |
| 93 |
result[current] = self.master.get_glyph_layer(current) |
| 94 |
else: |
| 95 |
# Find a glyph with same layerid? |
| 96 |
for layer in self._font.glyphs[current].layers: |
| 97 |
if layer.id == self.id: |
| 98 |
result[current] = layer |
| 99 |
break |
| 100 |
if current not in result and self.isBackground: |
| 101 |
master_layer = self._background_of() |
| 102 |
if master_layer: |
| 103 |
master = master_layer._font.master(master_layer._master) |
| 104 |
result[current] = master.get_glyph_layer(current) |
| 105 |
if result[current] and result[current]._background_layer(): |
| 106 |
result[current] = result[current]._background_layer() |
| 107 |
|
| 108 |
if current not in result or not result[current]: |
| 109 |
raise ValueError("Could not find layer") |
| 110 |
todo.extend([x.ref for x in result[current].components]) |
| 111 |
return result |
| 112 |
|
| 113 |
@cached_property |
| 114 |
def bounds(self): |
| 115 |
glyphset = {} |
| 116 |
for c in list(self.recursive_component_set()): |
| 117 |
glyphset[c] = self.master.get_glyph_layer(c) |
| 118 |
pen = BoundsPen(glyphset) |
| 119 |
self.draw(pen) |
| 120 |
return pen.bounds |
| 121 |
|
| 122 |
@property |
| 123 |
def lsb(self): |
| 124 |
if not self.bounds: # Space glyph |
| 125 |
return 0 |
| 126 |
return self.bounds[0] |
| 127 |
|
| 128 |
@property |
| 129 |
def rsb(self): |
| 130 |
if not self.bounds: # Space glyph |
| 131 |
return 0 |
| 132 |
return self.width - self.bounds[2] |
| 133 |
|
| 134 |
@property |
| 135 |
def valid(self): |
| 136 |
if not self._font or not self._glyph: |
| 137 |
return False |
| 138 |
return True |
| 139 |
|
| 140 |
@property |
| 141 |
def anchors_dict(self): |
| 142 |
return {a.name: a for a in self.anchors} |
| 143 |
|
| 144 |
# Pen protocol support... |
| 145 |
|
| 146 |
def draw(self, pen): |
| 147 |
pen = PointToSegmentPen(pen) |
| 148 |
return self.drawPoints(pen) |
| 149 |
|
| 150 |
def drawPoints(self, pen): |
| 151 |
for path in self.paths: |
| 152 |
pen.beginPath() |
| 153 |
for node in path.nodes: |
| 154 |
pen.addPoint( |
| 155 |
pt=(node.x, node.y), |
| 156 |
segmentType=node.pen_type, |
| 157 |
smooth=node.is_smooth, |
| 158 |
) |
| 159 |
pen.endPath() |
| 160 |
for component in self.components: |
| 161 |
pen.addComponent(component.ref, component.transform) |
| 162 |
|
| 163 |
def clearContours(self): |
| 164 |
self.shapes = [] |
| 165 |
|
| 166 |
def getPen(self): |
| 167 |
return SegmentToPointPen(LayerPen(self)) |
| 168 |
|
| 169 |
def decompose(self): |
| 170 |
pen = DecomposingRecordingPen(self._nested_component_dict()) |
| 171 |
self.draw(pen) |
| 172 |
self.clearContours() |
| 173 |
pen.replay(self.getPen()) |
| 174 |
|
| 175 |
|
| 176 |
class LayerPen(AbstractPointPen): |
| 177 |
def __init__(self, target): |
| 178 |
self.target = target |
| 179 |
self.curPath = [] |
| 180 |
|
| 181 |
def beginPath(self, identifier=None, **kwargs): |
| 182 |
self.curPath = [] |
| 183 |
|
| 184 |
def endPath(self): |
| 185 |
"""End the current sub path.""" |
| 186 |
self.target.shapes.append(Shape(nodes=self.curPath)) |
| 187 |
|
| 188 |
def addPoint( |
| 189 |
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs |
| 190 |
): |
| 191 |
if segmentType == "move": |
| 192 |
return |
| 193 |
ourtype = Node._from_pen_type[segmentType] |
| 194 |
if smooth: |
| 195 |
ourtype = ourtype + "s" |
| 196 |
self.curPath.append(Node(pt[0], pt[1], ourtype)) |
| 197 |
|
| 198 |
def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs): |
| 199 |
self.target.shapes.append(Shape(ref=baseGlyphName, transform=transformation)) |