Skip to content

Instantly share code, notes, and snippets.

@dperelman
Forked from RoelN/gist:8e3e7ecda723dfdfa590
Last active November 30, 2019 03:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dperelman/d78e4897743f32e88a4b to your computer and use it in GitHub Desktop.
Save dperelman/d78e4897743f32e88a4b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
import collections
import fileinput
import sys
plop = collections.defaultdict(list)
try:
import fontTools.agl
def map_character(c):
try:
return fontTools.agl.UV2AGL.get(ord(c), c)
except:
return c
except ImportError:
print('WARNING: Package fonttools not found.', file=sys.stderr)
# Don't have fonttools package.
# TODO: 5 ---> five etc.
character_mappings = {
' ': 'space',
}
def map_character(c):
return character_mappings.get(c, c)
for word in fileinput.input():
word = word.strip()
length = len(word)
string = ''
glue = ''
firstletter = ''
if length < 6:
yooniecode = 'uniE602'
elif length > 11:
yooniecode = 'uniE600'
else:
yooniecode = 'uniE601'
firstletter = word[0]
string = ','.join(map(map_character, word[1:]))
line = ' <Ligature components="' + string + '" glyph="' + yooniecode + '"/>'
plop[firstletter].append(line)
for ltr in plop:
print('<LigatureSet glyph="' + ltr + '">')
print("\n".join(plop[ltr]))
print('</LigatureSet>' + "\n")
@DonaldTsang
Copy link

Is it possible to use https://gist.github.com/karlding/c885b91051bd346d93ee 's import xml.dom.minidom to make it such that you can add ligatures to files without even needing to use ttx from Linux? Which can make it pure python?

@dperelman
Copy link
Author

dperelman commented Aug 6, 2019

@DonaldTsang It looks like that replaces my script ligature_xml_to_list.py which goes from a .ttx file to a word list (also linked from my blog post that links to this gist, for reference). You still need the ttx tool to convert .ttf files to/from .ttx. It looks like ttx is itself implemented in Python (and includes instructions for using it on Windows and OS X), so, yes, you could rework this to use its programmatic interface so more of the work happened inside Python.

(Apologies for the slow reply.)

@DonaldTsang
Copy link

@dperelman it is okay to be slow, slow and steady means the code would be of better quality, right?
Also if I am allowed to make my own proposal I would like to see a custom version of the software that...
replicates http://projectseen.com/ but instead censors the words that I would not like to see.

@dperelman
Copy link
Author

I have not looked into Project Seen, but, assuming it works the same way, you should be able to just follow the instructions in my blog post. I will probably look into making a pure Python version some time in the next week or so to make it a bit easier.

@DonaldTsang
Copy link

Please make this into a GitHub repos so people can see how it works, because it has its uses (I hope)

@dperelman
Copy link
Author

@DonaldTsang I got something messy but working at https://github.com/dperelman/ligature_replacer . All of the work happens inside fontTools in Python.

Basic usage:

./rewrite_ligatures.py SansBullshitSans.ttf --write-word-list words.txt --write-glyphs-json glyphs.json
# edit words.txt
./rewrite_ligatures.py SansBullshitSans.ttf --read-word-list words.txt --read-glyphs-json glyphs.json -o out.ttf

@DonaldTsang
Copy link

Will read this once I get home. Looks interesting.

@dperelman
Copy link
Author

Looking more carefully at Project Seen, it actually doesn't work quite the same way. It still uses ligatures, but it defines a separate ligature glyph for each word unlike SansBullshitSans which defines a small number of them and reuses them for words of the approximately the same length. For the "blackout" version of Project Seen it doesn't really matter (although my script will do a poor job of automatically identifying which ligature glyphs to use because it finds them by looking for repeats), but for the versions that leave the text visible, there's actual graphical manipulation involved which is much more complicated. I couldn't find any source code for Project Seen although I assume the author used a script of some sort to generate it from a word list.

@DonaldTsang
Copy link

DonaldTsang commented Nov 30, 2019

the blackout version sounds good enough for me, if the script can detect the length of the combined ligatures that would be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment