-
-
Save typesupply/5a9c5cfa4724a20529528bcdc3b3863c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.path.insert(0, '/Users/frederik/Downloads/fonttools-ufoLib/Lib') | |
import os | |
import shutil | |
import ufoLib | |
from fontTools.pens.basePen import NullPen | |
class Lib(dict): | |
pass | |
class Glyph(object): | |
def __init__(self): | |
self.name = None | |
self.width = 0 | |
self.unicodes = [] | |
self.lib = Lib() | |
self.contours = [] | |
self.components = [] | |
self.anchors = [] | |
self.guidelines = [] | |
@property | |
def unicode(self): | |
if self.unicodes: | |
return self.unicodes[0] | |
return None | |
def draw(self, pen): | |
pointPen = ufoLib.pointPen.PointToSegmentPen(pen) | |
self.drawPoints(pointPen) | |
def drawPoints(self, pointPen): | |
for contourIdentfier, contourData in self.contours: | |
pointPen.beginPath(identifier=contourIdentfier) | |
for point, segmentType, smooth, name, kwargs in contourData: | |
pointPen.addPoint(point, segmentType, smooth, name, **kwargs) | |
pointPen.endPath() | |
for baseGlyph, transformation, identifier in self.components: | |
pointPen.addComponent(baseGlyph, transformation, identifier=identifier) | |
def getPen(self): | |
pen = ufoLib.pointPen.SegmentToPointPen(self.getPointPen()) | |
return pen | |
def getPointPen(self): | |
return self | |
def beginPath(self, identifier=None): | |
self.contours.append([identifier, []]) | |
def addPoint(self, point, segmentType, smooth, name, **kwargs): | |
self.contours[-1][-1].append((point, segmentType, smooth, name, kwargs)) | |
def addComponent(self, baseGlyph, transformation, identifier=None): | |
self.components.append((baseGlyph, transformation, identifier)) | |
def endPath(self): | |
pass | |
class Info(object): | |
pass | |
class Groups(dict): | |
pass | |
class Kerning(dict): | |
pass | |
class Layer(dict): | |
def __iter__(self): | |
for key in self.keys(): | |
yield self[key] | |
def save(self, glyphSet): | |
for glyphName, glyph in sorted(self.items()): | |
glyphSet.writeGlyph(glyph.name, glyph, glyph.drawPoints) | |
class Layers(dict): | |
def save(self, writer, defaultLayerName): | |
for layerName, layer in self.items(): | |
isDefaultLayer = layerName == defaultLayerName | |
glyphSet = writer.getGlyphSet(layerName=layerName, defaultLayer=isDefaultLayer) | |
layer.save(glyphSet) | |
glyphSet.writeLayerInfo(layer) | |
writer.writeLayerContents(self.keys()) | |
class Font(object): | |
UFOReader = ufoLib.UFOReader | |
UFOWriter = ufoLib.UFOWriter | |
def __init__(self, path=None): | |
self.lib = Lib() | |
self.groups = Groups() | |
self.kerning = Kerning() | |
self.info = Info() | |
self.defaultLayerName = None | |
self.layers = Layers() | |
if path: | |
self._read(path) | |
def _read(self, path): | |
reader = self.UFOReader(path) | |
reader.readInfo(self.info) | |
self.kerning.update(reader.readKerning()) | |
self.groups.update(reader.readGroups()) | |
self.lib.update(reader.readLib()) | |
self.defaultLayerName = reader.getDefaultLayerName() | |
for layerName in reader.getLayerNames(): | |
layer = Layer() | |
glyphSet = reader.getGlyphSet(layerName) | |
for glyphName in glyphSet.keys(): | |
glyph = Glyph() | |
glyphSet.readGlyph(glyphName, glyph, glyph.getPointPen()) | |
layer[glyphName] = glyph | |
self.layers[layerName] = layer | |
def save(self, path=None): | |
writer = self.UFOWriter(path) | |
writer.writeInfo(self.info) | |
writer.writeGroups(self.groups) | |
writer.writeKerning(self.kerning) | |
writer.writeLib(self.lib) | |
# no images...yet | |
# no data...yet | |
self.layers.save(writer, defaultLayerName=self.defaultLayerName) | |
writer.setModificationTime() | |
def keys(self): | |
return self.layers[self.defaultLayerName].keys() | |
def __iter__(self): | |
return iter(self.layers[self.defaultLayerName]) | |
def __len__(self): | |
return len(self.layers[self.defaultLayerName]) | |
def __getitem__(self, glyphName): | |
return self.layers[self.defaultLayerName][glyphName] | |
### | |
# the git version is UFO2 fontTools.ufoLib does not support ufo3... so convert it first | |
# path = '/Users/frederik/Downloads/roboto-master/src/v2/Roboto-Regular.ufo' | |
path = '/Users/frederik/Downloads/roboto-master/src/v2/Roboto-Regular_3.ufo' | |
savePath = '/Users/frederik/Downloads/roboto-master/src/v2/Roboto-Regular_3_%s.ufo' | |
import timeit | |
NUMBER = 1 | |
print("test loading into font object") | |
def testLoad(fontClass): | |
def run(): | |
f = fontClass(path) | |
for g in f: | |
g.draw(NullPen()) | |
destPath = savePath % fontClass.__module__ | |
if os.path.exists(destPath): | |
shutil.rmtree(destPath) | |
f.save(destPath) | |
r = timeit.Timer(run).timeit(number=NUMBER) | |
print(fontClass.__module__, r) | |
testLoad(Font) | |
import fontTools.ufoLib | |
testLoad(fontTools.ufoLib.Font) | |
import defcon | |
testLoad(defcon.Font) | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment