Skip to content

Instantly share code, notes, and snippets.

@LettError
Last active January 22, 2024 08:33
Show Gist options
  • Save LettError/d9288964edae31f65a43c78df7a62abc to your computer and use it in GitHub Desktop.
Save LettError/d9288964edae31f65a43c78df7a62abc to your computer and use it in GitHub Desktop.
Controls for sorting the content of a RoboFont spacecenter.
# coding: utf-8
# menuTitle : Sort Space Center
import ezui
from random import shuffle
from glyphNameFormatter.reader import u2c
from mojo.UI import CurrentSpaceCenter, splitText, OpenSpaceCenter
"""
Take the glyphs from the current spacecenter
and sort them in different ways
evb 240122 v2
"""
class SortSpaceCenter(ezui.WindowController):
def build(self):
content = """
( Category / Unicode / Pseudo ) @unicodeButton
( Glyph Order ) @glyphOrderButton
( Sort by width ) @narrowToWideButton
( Sort by Area ) @areaButton
( Randomise all the glyphs ) @shuffleButton
"""
descriptionData = dict()
self.w = ezui.EZWindow(
title="Sort Space Center",
content=content,
descriptionData=descriptionData,
size=(180, 150),
controller=self
)
def started(self):
self.w.open()
def shuffleButtonCallback(self, sender):
names = self.getNames()
shuffle(names)
self.setNames(names)
def areaButtonCallback(self, sender):
names = self.getNames()
font = CurrentFont()
areas = {}
new = []
for g in font:
a = g.area
if not a in areas:
areas[a] = []
areas[a].append(g.name)
for a in sorted(areas.keys()):
new.extend(areas[a])
self.setNames(new)
def getPseudoUnicode(self, glyph):
return glyph.font.asDefcon().unicodeData.pseudoUnicodeForGlyphName(glyph.name)
def unicodeButtonCallback(self, sender):
names = self.getNames()
font = CurrentFont()
unicodes = {}
nocodes = []
for n in names:
g = font[n]
uni = g.unicode
if uni is None:
uni = self.getPseudoUnicode(g)
if uni is None:
nocodes.append(n)
continue
cat = u2c(uni)
if cat == None:
cat = "None"
key = (cat, uni)
if not key in unicodes:
unicodes[key] = []
unicodes[key].append(n)
unicodes[key].sort()
k = list(unicodes.keys())
k.sort()
new = []
for nn in k:
for name in unicodes[nn]:
new.append(name)
new.extend(nocodes)
self.setNames(new)
def glyphOrderButtonCallback(self, sender):
names = self.getNames()
font = CurrentFont()
new = []
unicodes = {}
nocodes = []
for n in font.glyphOrder:
if not n in font: continue
if not n in names: continue
new.append(n)
self.setNames(new)
def narrowToWideButtonCallback(self, sender):
names = self.getNames()
font = CurrentFont()
widths = {}
for n in names:
g = font[n]
if g.width not in widths:
widths[g.width] = []
widths[g.width].append(n)
widths[g.width].sort()
k = list(widths.keys())
k.sort()
new = []
for nn in k:
for name in widths[nn]:
new.append(name)
self.setNames(new)
def setNames(self, names):
sp = CurrentSpaceCenter()
sp.set(names)
def getNames(self):
font = CurrentFont()
if font == None:
return
sp = CurrentSpaceCenter()
if sp == None:
sp = OpenSpaceCenter(font)
names = list(font.keys())
else:
raw = sp.getRaw()
names = splitText(raw, cmap=font.getCharacterMapping())
return names
SortSpaceCenter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment