Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Last active February 10, 2021 13:47
Show Gist options
  • Save frankrolf/a4fe38d51a05fe8a56743d1472779136 to your computer and use it in GitHub Desktop.
Save frankrolf/a4fe38d51a05fe8a56743d1472779136 to your computer and use it in GitHub Desktop.
Select Unicode categories in Robofont 3
from mojo.UI import CurrentFontWindow
import unicodedata
import AppKit
from vanilla import CheckBox, FloatingWindow, List
category_to_codepoints = {}
for code_point in range(0, 0xFFFF + 1):
category = unicodedata.category(chr(code_point))
category_to_codepoints.setdefault(category, []).append(code_point)
all_categories = {
'Cc': 'Other, Control',
'Cf': 'Other, Format',
'Cn': 'Other, Not Assigned',
'Co': 'Other, Private Use',
'Cs': 'Other, Surrogate',
'LC': 'Letter, Cased',
'Ll': 'Letter, Lowercase',
'Lm': 'Letter, Modifier',
'Lo': 'Letter, Other',
'Lt': 'Letter, Titlecase',
'Lu': 'Letter, Uppercase',
'Mc': 'Mark, Spacing Combining',
'Me': 'Mark, Enclosing',
'Mn': 'Mark, Nonspacing',
'Nd': 'Number, Decimal Digit',
'Nl': 'Number, Letter',
'No': 'Number, Other',
'Pc': 'Punctuation, Connector',
'Pd': 'Punctuation, Dash',
'Pe': 'Punctuation, Close',
'Pf': 'Punctuation, Final quote',
'Pi': 'Punctuation, Initial quote',
'Po': 'Punctuation, Other',
'Ps': 'Punctuation, Open',
'Sc': 'Symbol, Currency',
'Sk': 'Symbol, Modifier',
'Sm': 'Symbol, Math',
'So': 'Symbol, Other',
'Zl': 'Separator, Line',
'Zp': 'Separator, Paragraph',
'Zs': 'Separator, Space',
}
class CatList(object):
def __init__(self, font):
self.f = font
self.select_suffix = False
font_code_point_lists = [g.unicodes for g in self.f if g.unicodes]
font_code_points = sum(font_code_point_lists, ())
self.font_cats = self.font_cat_names(font_code_points)
self.cat_list = []
self.display_cat_list = [] # use in window
self.selected_cats = []
for cat_name in self.font_cats:
description = all_categories.get(cat_name, '')
self.cat_list.append(cat_name)
self.display_cat_list.append({
'cat name': cat_name,
'description': description,
})
list_height = 24 + 20 * len(self.display_cat_list)
window_height = list_height + 24
self.w = FloatingWindow(
(350, window_height),
title='uni😸😸😸',
)
self.w.bind('close', self.close_callback)
self.w.myList = List(
(0, 0, -0, list_height),
self.display_cat_list,
columnDescriptions=[
{'title': 'cat name'},
{'title': 'description'}
],
selectionCallback=self.sel_callback,
allowsMultipleSelection=True
)
self.w.myCheckBox = CheckBox(
(10, -23, -10, 20),
"Show Suffixed",
callback=self.check_callback,
value=False)
self.w.open()
# select the first item in the list and filter the glyph set
# according to that selection
self.w.myList.setSelection([0])
self.sel_callback(self.w.myList)
def font_cat_names(self, font_cpoints):
'''
Find Unicode categories found in the font at hand
'''
cat_names = []
for cat_name, cpoint_list in category_to_codepoints.items():
if set(font_cpoints) & set(cpoint_list):
cat_names.append(cat_name)
return sorted(cat_names)
def sel_callback(self, sender):
cat_selections = sender.getSelection()
cat_names = []
for sel_index in cat_selections:
cat_name = self.cat_list[sel_index]
cat_names.append(cat_name)
self.selected_cats = cat_names
self.select_cats(cat_names)
def close_callback(self, sender):
CurrentFontWindow().getGlyphCollection().setQuery(None)
def check_callback(self, sender):
self.select_suffix = sender.get()
if self.selected_cats:
self.select_cats(self.selected_cats)
else:
self.select_cats(['Ll'])
def select_cats(self, cat_names):
g_name_list = []
for cat_name in cat_names:
g_name_list.extend(
g.name for g in self.f if
set(g.unicodes) & set(category_to_codepoints[cat_name]))
if self.select_suffix:
sel_list = [
g.name for g in self.f if g.name.split('.')[0] in g_name_list]
else:
sel_list = g_name_list
query = 'Name in {"%s"}' % '", "'.join(sel_list)
query = AppKit.NSPredicate.predicateWithFormat_(query)
CurrentFontWindow().getGlyphCollection().setQuery(query)
f = CurrentFont()
CatList(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment