Skip to content

Instantly share code, notes, and snippets.

@mjlagattuta
Last active February 12, 2019 01:35
Show Gist options
  • Save mjlagattuta/24e979b1d7c9c878e08133d7b78bd7a8 to your computer and use it in GitHub Desktop.
Save mjlagattuta/24e979b1d7c9c878e08133d7b78bd7a8 to your computer and use it in GitHub Desktop.
# Copyright 2019 Michael LaGattuta
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script to build brace layers
# NOTE: Does not modify HVAR
# NOTE: Fontmake (version 1.9.0 and up) now supports Brace layers in glyphs sources
## Slices a glyphs file into single glyph files and moves brace layers to master layers
## Using a bash script the generated glyf and gvar data can then be inserted into the final font file
import sys
import os
import re
import copy
import time
from glyphsLib import GSFont
from glyphsLib import GSGlyph
from glyphsLib import GSLayer
from glyphsLib import GSFontMaster
from glyphsLib import GSInstance
start = time.time()
filename = sys.argv[-1]
font = GSFont(filename)
nonExportGlyphs = []
baseIndex = 0
for glyph in font.glyphs:
if glyph.export == False:
nonExportGlyphs.append(baseIndex)
baseIndex -= 1
baseIndex += 1
for baseIndex in nonExportGlyphs:
del font._glyphs[baseIndex]
font.save(filename)
print "Build File Saved"
braceSources = []
def keepOneGlyph(font, glyphName):
delGlyphs = []
index = 0
for glyph in font.glyphs:
if glyph.name != glyphName:
delGlyphs.append(index)
index -= 1
index += 1
for glyphIndex in delGlyphs:
del font._glyphs[glyphIndex]
for glyphIndex, glyph in enumerate(font.glyphs):
hasBrace = False
for layer in glyph.layers:
if re.match('.*\d\}$', layer.name):
hasBrace = True
break
if hasBrace == True:
braceFont = GSFont(filename)
braceFilename = glyph.name + "-source" + str(glyphIndex) + ".glyphs"
print "Created file:" + glyph.name + "-source" + str(glyphIndex) + ".glyphs"
braceFont.familyName = glyph.name + "-source" + str(glyphIndex)
braceFont.features = []
braceFont.classes = ()
braceFont.kerning = {}
glyphName = glyph.name
keepOneGlyph(braceFont, glyphName)
addMasters = []
for layer in braceFont.glyphs[0].layers:
if re.match('.*\d\}$', layer.name):
value = re.sub('.*{|}', '', layer.name)
newMaster = copy.copy(braceFont.masters[0])
newMaster.weightValue = int(value)
newMaster.id = None
newMaster.name = value
addMasters.append([newMaster, layer.layerId])
for master in addMasters:
braceFont.masters.append(master[0])
masterId = braceFont.masters[-1].id
braceFont.glyphs[0].layers[masterId].paths = braceFont.glyphs[0].layers[master[1]].paths
braceFont.glyphs[0].layers[masterId].components = braceFont.glyphs[0].layers[master[1]].components
braceFont.glyphs[0].layers[masterId].anchors = braceFont.glyphs[0].layers[master[1]].anchors
braceFont.glyphs[0].layers[masterId].width = braceFont.glyphs[0].layers[master[1]].width
del braceFont.glyphs[0].layers[master[1]]
braceFont.save("brace-sources/" + braceFilename)
print "Saved file:" + braceFilename
end = time.time()
print end - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment