Skip to content

Instantly share code, notes, and snippets.

@b3z
Created July 30, 2020 10:20
Show Gist options
  • Save b3z/7b5117c1c455e1a3f9fba2a6e07f2d52 to your computer and use it in GitHub Desktop.
Save b3z/7b5117c1c455e1a3f9fba2a6e07f2d52 to your computer and use it in GitHub Desktop.
Me trying to generate word lines for Discoed categories. I didn't succeed.
import sys
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
font = TTFont('whitney.ttf')
cmap = font['cmap']
t = cmap.getcmap(3,1).cmap
s = font.getGlyphSet()
units_per_em = font['head'].unitsPerEm
# get width in points
def getTextWidth(text,pointSize):
total = 0
for c in text:
if ord(c) in t and t[ord(c)] in s:
total += s[t[ord(c)]].width
else:
total += s['.notdef'].width
total = total*float(pointSize)/units_per_em;
return total
FONT_SIZE= 12
BAR = '─' #the bar char (ASCII 196)
BAR_SIZE = getTextWidth(BAR, FONT_SIZE)
HAIR_SPACE = ' '
HAIR_SPACE_SIZE = getTextWidth(HAIR_SPACE, FONT_SIZE)
THIN_SPACE = ' '
out = f'{THIN_SPACE}{sys.argv[1]}{THIN_SPACE}'
DISCORD_MAX_LENGTH = 18 # The max display length for categories in Discord
DISCORD_MAX_POINTS = BAR_SIZE*DISCORD_MAX_LENGTH
def build(s):
print(s)
global out_width
out_width = getTextWidth(s, FONT_SIZE)
diff = DISCORD_MAX_POINTS-out_width
if diff > BAR_SIZE*2:
s = f'{BAR}{s}{BAR}'
elif diff < BAR_SIZE*2:
arr = s.split(THIN_SPACE)
s = f'{arr[0]}{HAIR_SPACE}{THIN_SPACE}{arr[1]}{THIN_SPACE}{HAIR_SPACE}{arr[2]}'
elif diff < HAIR_SPACE_SIZE*2:
arr = s.split(THIN_SPACE)
s = f'{arr[0]}{THIN_SPACE}{arr[1]}{THIN_SPACE}{HAIR_SPACE}{arr[2]}'
if diff < 0:
return s
print(diff)
build(s)
out = build(out)
print(f'Tolerance: {DISCORD_MAX_POINTS-out_width} zero is perfect')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment