-
Notifications
You must be signed in to change notification settings - Fork 13
/
font.py
605 lines (487 loc) · 21.6 KB
/
font.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from __future__ import annotations
import os
import shutil
from typing import (
Any,
Dict,
Iterable,
Iterator,
KeysView,
List,
Mapping,
MutableMapping,
Optional,
Sequence,
cast,
)
import fs.base
import fs.tempfs
from attrs import define, field
from fontTools.ufoLib import UFOFileStructure, UFOReader, UFOWriter
from ufoLib2.constants import DEFAULT_LAYER_NAME
from ufoLib2.objects.dataSet import DataSet
from ufoLib2.objects.features import Features
from ufoLib2.objects.glyph import Glyph
from ufoLib2.objects.guideline import Guideline
from ufoLib2.objects.imageSet import ImageSet
from ufoLib2.objects.info import Info
from ufoLib2.objects.kerning import Kerning, KerningPair
from ufoLib2.objects.layer import Layer
from ufoLib2.objects.layerSet import LayerSet
from ufoLib2.objects.lib import Lib, _convert_Lib, _get_lib, _set_lib
from ufoLib2.objects.misc import (
BoundingBox,
_deepcopy_unlazify_attrs,
_getstate_unlazify_attrs,
_object_lib,
_prune_object_libs,
_setstate_attrs,
)
from ufoLib2.serde import serde
from ufoLib2.typing import HasIdentifier, PathLike, T
def _convert_LayerSet(value: LayerSet | Iterable[Layer]) -> LayerSet:
return value if isinstance(value, LayerSet) else LayerSet.from_iterable(value)
def _convert_Info(value: Info | Mapping[str, Any]) -> Info:
return value if isinstance(value, Info) else Info(**value)
def _convert_DataSet(value: DataSet | MutableMapping[str, bytes]) -> DataSet:
return value if isinstance(value, DataSet) else DataSet(value) # type: ignore
def _convert_ImageSet(value: ImageSet | MutableMapping[str, bytes]) -> ImageSet:
return value if isinstance(value, ImageSet) else ImageSet(value) # type: ignore
def _convert_Features(value: Features | str) -> Features:
return value if isinstance(value, Features) else Features(value)
def _convert_Kerning(value: Mapping[KerningPair, float]) -> Kerning:
return value if isinstance(value, Kerning) else Kerning(value)
def _get_kerning(self: Font) -> Kerning:
return self._kerning
def _set_kerning(self: Font, value: Mapping[KerningPair, float]) -> None:
self._kerning = _convert_Kerning(value)
@serde
@define(kw_only=True)
class Font:
"""A data class representing a single Unified Font Object (UFO).
Font uses :class:`fontTools.ufoLib.UFOReader` and
:class:`fontTools.ufoLib.UFOWriter` to read and write UFO data from and to disk.
It will default to reading lazily, loading glyphs, data and images as they are
accessed. Copying a font implicitly loads everything eagerly before.
The data model is formally specified at
http://unifiedfontobject.org/versions/ufo3/index.html.
Parameters:
layers (LayerSet): A mapping of layer names to Layer objects.
info (Info): The font Info object.
features (Features): The font Features object.
groups (Dict[str, List[str]]): A mapping of group names to a list of glyph
names.
kerning (Dict[Tuple[str, str], float]): A mapping of a tuple of first and
second kerning pair to a kerning value.
lib (Dict[str, Any]): A mapping of keys to arbitrary values.
data (DataSet): A mapping of data file paths to arbitrary data.
images (ImageSet): A mapping of image file paths to arbitrary image data.
Behavior:
The Font object has some dict-like behavior for quick access to glyphs on the
the default layer. For example, to get a glyph by name from the default layer::
glyph = font["aGlyphName"]
To iterate over all glyphs in the default layer::
for glyph in font:
pass
To get the number of glyphs in the default layer::
glyphCount = len(font)
To find out if a font contains a particular glyph in the default layer by name::
exists = "aGlyphName" in font
To remove a glyph from the default layer by name::
del font["aGlyphName"]
To replace a glyph in the default layer with another :class:`.Glyph` object::
font["aGlyphName"] = otherGlyph
To copy a font::
import copy
fontCopy = copy.deepcopy(font)
Layers behave the same, for when you're working on something other than the
default layer.
"""
layers: LayerSet = field(
factory=LayerSet.default,
converter=_convert_LayerSet,
metadata={"omit_if_default": False},
)
"""LayerSet: A mapping of layer names to Layer objects."""
info: Info = field(factory=Info, converter=_convert_Info)
"""Info: The font Info object."""
features: Features = field(factory=Features, converter=_convert_Features)
"""Features: The font Features object."""
groups: Dict[str, List[str]] = field(factory=dict)
"""Dict[str, List[str]]: A mapping of group names to a list of glyph names."""
_kerning: Kerning = field(factory=Kerning, converter=_convert_Kerning)
"""Dict[Tuple[str, str], float]: A mapping of a tuple of first and second kerning
pair to a kerning value."""
_lib: Lib = field(factory=Lib, converter=_convert_Lib)
"""Dict[str, PlistEncodable]: A mapping of keys to arbitrary plist values."""
data: DataSet = field(factory=DataSet, converter=_convert_DataSet)
"""DataSet: A mapping of data file paths to arbitrary data."""
images: ImageSet = field(factory=ImageSet, converter=_convert_ImageSet)
"""ImageSet: A mapping of image file paths to arbitrary image data."""
# init=False args, set by alternate open/read classmethod constructors
_path: Optional[PathLike] = field(default=None, eq=False, init=False)
_lazy: Optional[bool] = field(default=None, init=False, eq=False)
_reader: Optional[UFOReader] = field(default=None, init=False, eq=False)
_fileStructure: Optional[UFOFileStructure] = field(
default=None, init=False, eq=False
)
@classmethod
def open(cls, path: PathLike, lazy: bool = True, validate: bool = True) -> Font:
"""Instantiates a new Font object from a path to a UFO.
Args:
path: The path to the UFO to load.
lazy: If True, load glyphs, data files and images as they are accessed. If
False, load everything up front.
validate: If True, enable UFO data model validation during loading. If
False, load whatever is deserializable.
"""
reader = UFOReader(path, validate=validate)
self = cls.read(reader, lazy=lazy)
self._path = path
if not lazy:
reader.close()
return self
@classmethod
def read(cls, reader: UFOReader, lazy: bool = True) -> Font:
"""Instantiates a Font object from a :class:`fontTools.ufoLib.UFOReader`.
Args:
path: The path to the UFO to load.
lazy: If True, load glyphs, data files and images as they are accessed. If
False, load everything up front.
"""
self = cls(
layers=LayerSet.read(reader, lazy=lazy),
data=DataSet.read(reader, lazy=lazy),
images=ImageSet.read(reader, lazy=lazy),
info=Info.read(reader),
features=Features(reader.readFeatures()),
groups=reader.readGroups(),
kerning=Kerning(reader.readKerning()),
lib=Lib(reader.readLib()),
)
self._lazy = lazy
self._fileStructure = reader.fileStructure
if lazy:
# keep the reader around so we can close it when done
self._reader = reader
return self
def __contains__(self, name: object) -> bool:
return name in self.layers._defaultLayer
def __delitem__(self, name: str) -> None:
del self.layers._defaultLayer[name]
def __getitem__(self, name: str) -> Glyph:
return self.layers._defaultLayer[name]
def __setitem__(self, name: str, glyph: Glyph) -> None:
self.layers._defaultLayer[name] = glyph
def __iter__(self) -> Iterator[Glyph]:
return iter(self.layers._defaultLayer)
def __len__(self) -> int:
return len(self.layers._defaultLayer)
def get(self, name: str, default: T | None = None) -> T | Glyph | None:
"""Return the :class:`.Glyph` object for name if it is present in the
default layer, otherwise return ``default``."""
return self.layers._defaultLayer.get(name, default)
def keys(self) -> KeysView[str]:
"""Return a list of glyph names in the default layer."""
return self.layers._defaultLayer.keys()
def close(self) -> None:
"""Closes the UFOReader if it still exists to finalize any outstanding
file operations."""
if self._reader is not None:
self._reader.close()
def __enter__(self) -> Font:
# TODO: Document an example for this.
return self
def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None:
self.close()
def __repr__(self) -> str:
names = list(filter(None, [self.info.familyName, self.info.styleName]))
fontName = " '{}'".format(" ".join(names)) if names else ""
return "<{}.{}{} at {}>".format(
self.__class__.__module__, self.__class__.__name__, fontName, hex(id(self))
)
def __eq__(self, other: object) -> bool:
# same as attrs-defined __eq__ method, only that it un-lazifies fonts if needed
# NOTE: Avoid isinstance check that mypy recognizes because we don't want to
# test possible Font subclasses for equality.
if other.__class__ is not self.__class__:
return NotImplemented
other = cast(Font, other)
for font in (self, other):
if font._lazy:
font.unlazify()
return (
self.layers,
self.info,
self.features,
self.groups,
self.kerning,
self.lib,
self.data,
self.images,
) == (
other.layers,
other.info,
other.features,
other.groups,
other.kerning,
other.lib,
other.data,
other.images,
)
def __ne__(self, other: object) -> bool:
result = self.__eq__(other)
if result is NotImplemented:
return NotImplemented
return not result
@property
def reader(self) -> UFOReader | None:
"""Returns the underlying :class:`fontTools.ufoLib.UFOReader`."""
return self._reader
def unlazify(self) -> None:
"""Load all glyphs, data files and images if the Font object loaded
them lazily previously."""
if self._lazy:
assert self._reader is not None
self.layers.unlazify()
self.data.unlazify()
self.images.unlazify()
self._lazy = False
__deepcopy__ = _deepcopy_unlazify_attrs
__getstate__ = _getstate_unlazify_attrs
__setstate__ = _setstate_attrs
@property
def glyphOrder(self) -> list[str]:
"""The font's glyph order.
See http://unifiedfontobject.org/versions/ufo3/lib.plist/#publicglyphorder for
semantics.
Getter:
Return the font's glyph order, if it is set in lib, or an empty list.
Note:
The getter always returns a new list, modifications to it do not change
the lib content.
Setter:
Sets the font's glyph order. If ``value`` is None or an empty list, the
glyph order key will be deleted from the lib if it exists.
"""
return list(cast(Sequence[str], self.lib.get("public.glyphOrder", [])))
@glyphOrder.setter
def glyphOrder(self, value: list[str] | None) -> None:
if value is None or len(value) == 0:
if "public.glyphOrder" in self.lib:
del self.lib["public.glyphOrder"]
else:
self.lib["public.glyphOrder"] = value
@property
def guidelines(self) -> list[Guideline]:
"""The font's global guidelines.
Getter:
Returns the font's global guidelines or an empty list.
Setter:
Appends the list of Guidelines to the global Guidelines.
XXX Should it replace them?
"""
if self.info.guidelines is None:
return []
return self.info.guidelines
@guidelines.setter
def guidelines(self, value: Iterable[Guideline | Mapping[str, Any]]) -> None:
self.info.guidelines = []
for guideline in value:
self.appendGuideline(guideline)
kerning = property(_get_kerning, _set_kerning)
lib = property(_get_lib, _set_lib)
def objectLib(self, object: HasIdentifier) -> dict[str, Any]:
"""Return the lib for an object with an identifier, as stored in a font's lib.
If the object does not yet have an identifier, a new one is assigned to it. If
the font lib does not yet contain the object's lib, a new one is inserted and
returned.
.. doctest::
>>> from ufoLib2.objects import Font, Guideline
>>> font = Font()
>>> font.guidelines = [Guideline(x=100)]
>>> guideline_lib = font.objectLib(font.guidelines[0])
>>> guideline_lib["com.test.foo"] = 1234
>>> guideline_id = font.guidelines[0].identifier
>>> assert guideline_id is not None
>>> assert font.lib["public.objectLibs"][guideline_id] is guideline_lib
"""
return _object_lib(self.lib, object)
@property
def path(self) -> PathLike | None:
"""Return the path of the UFO, if it was set, or None."""
return self._path
@property
def bounds(self) -> BoundingBox | None:
"""Returns the (xMin, yMin, xMax, yMax) bounding box of the default
layer, taking the actual contours into account.
|defcon_compat|
"""
return self.layers._defaultLayer.bounds
@property
def controlPointBounds(self) -> BoundingBox | None:
"""Returns the (xMin, yMin, xMax, yMax) bounding box of the layer,
taking only the control points into account.
|defcon_compat|
"""
return self.layers._defaultLayer.controlPointBounds
def addGlyph(self, glyph: Glyph) -> None:
"""Appends glyph object to the default layer unless its name is already
taken.
Note:
Call the method on the layer directly if you want to overwrite entries
with the same name or append copies of the glyph.
"""
self.layers._defaultLayer.addGlyph(glyph)
def newGlyph(self, name: str) -> Glyph:
"""Creates and returns new :class:`.Glyph` object in default layer with
name."""
return self.layers._defaultLayer.newGlyph(name)
def newLayer(self, name: str, **kwargs: Any) -> Layer:
"""Creates and returns a new :class:`.Layer`.
Args:
name: The name of the new layer.
kwargs: The arguments passed to the Layer object constructor.
"""
return self.layers.newLayer(name, **kwargs)
def renameGlyph(self, name: str, newName: str, overwrite: bool = False) -> None:
"""Renames a :class:`.Glyph` object in the default layer.
Args:
name: The old name.
newName: The new name.
overwrite: If False, raises exception if newName is already taken.
If True, overwrites (read: deletes) the old :class:`.Glyph` object.
"""
self.layers._defaultLayer.renameGlyph(name, newName, overwrite)
def renameLayer(self, name: str, newName: str, overwrite: bool = False) -> None:
"""Renames a :class:`.Layer`.
Args:
name: The old name.
newName: The new name.
overwrite: If False, raises exception if newName is already taken.
If True, overwrites (read: deletes) the old :class:`.Layer` object.
"""
self.layers.renameLayer(name, newName, overwrite)
def appendGuideline(self, guideline: Guideline | Mapping[str, Any]) -> None:
"""Appends a guideline to the list of the font's global guidelines.
Creates the global guideline list unless it already exists.
Args:
guideline: A :class:`.Guideline` object or a mapping for the Guideline
constructor.
"""
if not isinstance(guideline, Guideline):
if not isinstance(guideline, Mapping):
raise TypeError(
"Expected Guideline object or a Mapping for the ",
f"Guideline constructor, found {type(guideline).__name__}",
)
guideline = Guideline(**guideline)
if self.info.guidelines is None:
self.info.guidelines = []
self.info.guidelines.append(guideline)
def write(self, writer: UFOWriter, saveAs: bool | None = None) -> None:
"""Writes this Font to a :class:`fontTools.ufoLib.UFOWriter`.
Args:
writer: The :class:`fontTools.ufoLib.UFOWriter` to write to.
saveAs: If True, tells the writer to save out-of-place. If False, tells the
writer to save in-place. This affects how resources are cleaned before
writing.
"""
if saveAs is None:
saveAs = self._reader is not writer
# TODO move this check to fontTools UFOWriter
if self.layers._defaultLayer.name != DEFAULT_LAYER_NAME:
assert DEFAULT_LAYER_NAME not in self.layers.layerOrder
# save font attrs
writer.writeFeatures(self.features.text)
writer.writeGroups(self.groups)
writer.writeInfo(self.info)
writer.writeKerning(self.kerning)
_prune_object_libs(
self.lib,
{g.identifier for g in self.guidelines if g.identifier is not None},
)
writer.writeLib(self.lib)
# save the layers
self.layers.write(writer, saveAs=saveAs)
# save bin parts
self.data.write(writer, saveAs=saveAs)
self.images.write(writer, saveAs=saveAs)
def save(
self,
path: PathLike | fs.base.FS | None = None,
formatVersion: int = 3,
structure: UFOFileStructure | None = None,
overwrite: bool = False,
validate: bool = True,
) -> None:
"""Saves the font to ``path``.
Args:
path: The target path. If it is None, the path from the last save (except
when that was a ``fs.base.FS``) or when the font was first opened will
be used.
formatVersion: The version to save the UFO as. Only version 3 is supported
currently.
structure (fontTools.ufoLib.UFOFileStructure): How to store the UFO.
Can be either None, "zip" or "package". If None, it tries to use the
same structure as the original UFO at the output path. If "zip", the
UFO will be saved as compressed archive. If "package", it is saved as
a regular folder or "package".
overwrite: If False, raises OSError when the target path exists. If True,
overwrites the target path.
validate: If True, will validate the data in Font before writing it out. If
False, will write out whatever is serializable.
"""
if formatVersion != 3:
raise NotImplementedError(f"unsupported format version: {formatVersion}")
# validate 'structure' argument
if structure is not None:
structure = UFOFileStructure(structure)
elif self._fileStructure is not None:
# if structure is None, fall back to the same as when first loaded
structure = self._fileStructure
# Normalize path unless we're given a fs.base.FS, which we pass to UFOWriter.
if path is not None and not isinstance(path, fs.base.FS):
path = os.path.normpath(os.fspath(path))
overwritePath = tmp = None
saveAs = path is not None
if saveAs:
if isinstance(path, str) and os.path.exists(path):
if overwrite:
overwritePath = path
tmp = fs.tempfs.TempFS()
path = tmp.getsyspath(os.path.basename(path))
else:
import errno
raise OSError(errno.EEXIST, "path %r already exists" % path)
elif self.path is None:
raise TypeError("'path' is required when saving a new Font")
else:
path = self.path
try:
with UFOWriter(path, structure=structure, validate=validate) as writer:
self.write(writer, saveAs=saveAs)
writer.setModificationTime()
except Exception:
raise
else:
if overwritePath is not None:
assert isinstance(path, str)
# remove existing then move file to destination
if os.path.isdir(overwritePath):
shutil.rmtree(overwritePath)
elif os.path.isfile(overwritePath):
os.remove(overwritePath)
shutil.move(path, overwritePath)
path = overwritePath
finally:
# clean up the temporary directory
if tmp is not None:
tmp.close()
# Only remember path if it isn't a fs.base.FS because not all FS objects are
# OsFS with a corresponding filesystem path. E.g. think about MemoryFS.
# If you want, you can call getsyspath("") method of OsFS object and set that to
# self._path. But you then have to catch the fs.errors.NoSysPath and skip if
# the FS object does not implement a filesystem path.
if not isinstance(path, fs.base.FS):
self._path = path