Skip to content

Instantly share code, notes, and snippets.

@medicalwei
Created June 26, 2016 14:35
Show Gist options
  • Save medicalwei/3e7a98b1864db6fe26400d2537e861e0 to your computer and use it in GitHub Desktop.
Save medicalwei/3e7a98b1864db6fe26400d2537e861e0 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import sys
import urllib
import fontforge
import psMat
BASE_FONT = "TW-Kai-98_1.ttf"
IDS_URL = "https://tools.wmflabs.org/idsgen/%s.svg?字體=%s"
IDS_FONT = "楷體"
IDS_SVG_OUTPUT = "ids_svg/%s.svg"
def main():
base_font = fontforge.open(BASE_FONT)
font = fontforge.font()
font.encoding = "UnicodeBmp"
font.addLookup('ligatures','gsub_ligature', (),[['liga',[['DFLT', ['dflt']], ['latn', ['dflt']]]]])
font.addLookupSubtable('ligatures', 'ids_lookup')
code = 0
for ids_code in sys.stdin:
add_ids_glyph(font, base_font, ids_code.strip(), code)
code += 1
font.generate("ids.otf")
def add_ids_glyph(font, base_font, ids_code, code):
print ids_code
glyph_code = "_".join(c.encode("unicode_escape").strip("\\") for c in ids_code.decode("utf-8"))
ligature_tuple = tuple("uni%s" % (c.encode("unicode_escape").strip("\\u").upper()) for c in ids_code.decode("utf-8"))
urllib.urlretrieve(IDS_URL % (ids_code,IDS_FONT), IDS_SVG_OUTPUT % (glyph_code))
for c in ids_code.decode("utf-8"):
base_font.selection.select(("unicode", None), ord(c))
base_font.copy()
font.selection.select(("unicode", None), ord(c))
font.paste()
glyph = font.createChar(-1, "ids%04x" % (code))
glyph.importOutlines(IDS_SVG_OUTPUT % (glyph_code))
glyph.transform(psMat.translate(0, -630))
glyph.transform(psMat.scale(5))
glyph.width = 1000
glyph.addPosSub('ids_lookup', ligature_tuple)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment