Created
February 13, 2019 12:34
-
-
Save mathieureguer/14d7bc57234f198d5a8b2c97db455f82 to your computer and use it in GitHub Desktop.
A glyph set picker for Robofont 3
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
# menuTitle: Assign Glyph Set | |
# Mathieu Reguer | |
""" | |
version 0.4 | |
- switched to vanilla.dialogs.getFile | |
version 0.3 | |
- Added import charset option | |
version 0.2 | |
- Charset List is sorted | |
- optional text input for trailing glyphs | |
- minor optimisations | |
- persistent dialog | |
""" | |
########## | |
# importing stuff | |
########## | |
from mojo.UI import getCharacterSets | |
from vanilla import * | |
from collections import OrderedDict | |
from vanilla.dialogs import getFile | |
########## | |
# classes | |
########## | |
class charSetPicker(object): | |
def __init__(self): | |
self.myCharsets = getCharacterSets() | |
self.addTrailGlyphs = False | |
self.font_has_ui = True | |
self.panel() | |
def panel(self): | |
# set up the margins | |
xMargin = 15 | |
yMargin = 15 | |
itemHeight = 22 | |
windowsWidth = 340 | |
tab_1 = 140 | |
width_0 = tab_1 - xMargin * 2 | |
# the main window | |
self.w = FloatingWindow((windowsWidth, 0), "Assign Character Set") | |
currentHeight = yMargin | |
self.w.lengthText = TextBox( | |
(xMargin, currentHeight, width_0, itemHeight), text="Character Set") | |
self.w.charset = PopUpButton( | |
(tab_1, currentHeight, -xMargin, itemHeight), sorted(self.myCharsets.keys())) | |
currentHeight += itemHeight + yMargin / 2 | |
self.w.targetText = TextBox( | |
(xMargin, currentHeight, width_0, itemHeight), text="Target:") | |
self.w.target = PopUpButton( | |
(tab_1, currentHeight, -xMargin, itemHeight), ["Current Font", "All Fonts", "Load Fonts"]) | |
currentHeight += itemHeight + yMargin | |
self.w.addTrailGlyphs = CheckBox((xMargin, currentHeight, -xMargin, itemHeight), | |
"Add trailing glyphs on the fly", value=self.addTrailGlyphs, callback=self.enableTrailGlyph) | |
currentHeight += itemHeight + yMargin / 2 | |
self.w.trailGlyphs = EditText((xMargin, currentHeight, -xMargin, itemHeight * 10), | |
continuous=False, placeholder="glyphs names as a space separated list") | |
self.w.trailGlyphs.enable(False) | |
currentHeight += itemHeight * 10 + yMargin | |
# button | |
self.w.assignCharset = Button( | |
(xMargin, currentHeight, -xMargin, itemHeight), "Go", callback=self.assignCharset_callback) | |
currentHeight += itemHeight + yMargin / 2 | |
self.w.importGlyphList = Button( | |
(xMargin, currentHeight, -xMargin, itemHeight), "Import Glyph List", callback=self.importList_callback) | |
currentHeight += itemHeight + yMargin | |
self.w.setPosSize((200, 100, windowsWidth, currentHeight)) | |
self.w.setDefaultButton(self.w.assignCharset) | |
self.w.open() | |
self.w.makeKey() | |
def importGlyphOrder(self, lines): | |
out = [] | |
for l in lines: | |
l = l.strip() | |
if l.startswith("#") or l.startswith("%"): | |
pass | |
else: | |
parts = l.split() | |
if parts != []: | |
out += parts | |
return out | |
def parseInput(self, string): | |
string = str(string) | |
return string.split() | |
def mergeGlyphOrder(self, glyphOrder, glyphOrderAdd): | |
return list(OrderedDict.fromkeys(glyphOrder + glyphOrderAdd)) | |
def assignCharset(self, target, glyphOrder): | |
for f in target: | |
f.templateGlyphOrder = glyphOrder | |
f.update() | |
print("Assigned Glyph Set to %s %s" % | |
(f.info.familyName, f.info.styleName)) | |
if not self.font_has_ui: | |
f.save() | |
f.close() | |
def assignCharset_callback(self, sender): | |
target = self.getTarget() | |
myCharset = self.w.charset.getItems()[self.w.charset.get()] | |
myCharset = self.myCharsets[myCharset] | |
if self.addTrailGlyphs: | |
myCharsetAdd = self.parseInput(self.w.trailGlyphs.get()) | |
myCharset = self.mergeGlyphOrder(myCharset, myCharsetAdd) | |
self.assignCharset(target, myCharset) | |
self.w.close() | |
def importList_callback(self, sender): | |
path = getFile(messageText="Select your glyph set") | |
target = self.getTarget() | |
with open(path[0], "r") as file_: | |
lines = file_.readlines() | |
myCharset = self.importGlyphOrder(lines) | |
self.assignCharset(target, myCharset) | |
def enableTrailGlyph(self, sender): | |
self.addTrailGlyphs = sender.get() | |
self.w.trailGlyphs.enable(sender.get()) | |
def getTarget(self): | |
target = [] | |
if self.w.target.get() == 0: # current font | |
target = CurrentFont() | |
self.font_has_ui = True | |
elif self.w.target.get() == 1: | |
target = AllFonts() | |
self.font_has_ui = True | |
elif self.w.target.get() == 2: # load fonts | |
target = OpenFont(showUI=False) | |
self.font_has_ui = False | |
if not isinstance(target, list): | |
target = [target] | |
return target | |
charSetPicker() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment