Skip to content

Instantly share code, notes, and snippets.

@midworld
Last active January 29, 2023 09:34
Show Gist options
  • Save midworld/4e43921a018270b103f8c4a83f4a900f to your computer and use it in GitHub Desktop.
Save midworld/4e43921a018270b103f8c4a83f4a900f to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
# https://www.academia.edu/6647563/The_C90_encoding_for_Thai
# https://github.com/SaladLab/Unity3D.ThaiFontAdjuster/blob/master/docs/UnderTheHood.md
# https://www.thaitux.info/book/export/html/275
# https://unicodey.com/emoji-data/build/google/noto-emoji/add_svg_glyphs.py
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables import _g_l_y_f
from fontTools.ttLib.tables import _v_m_t_x
from fontTools.misc.xmlWriter import XMLWriter
from pprint import pprint
import os
source = 'NotoSansThaiUI-SemiCondensedSemiBold.ttf'
font = TTFont(source)
glyph_order = font.getGlyphOrder()
cmap = font['cmap'].tables[0].cmap
hmtx = font['hmtx'].metrics
glyphs = font['glyf'].glyphs
c90 = [
# replace top.low -> top
(0x0E48, 'uni0E48', 'uni0E48.small'),
(0x0E49, 'uni0E49', 'uni0E49.small'),
(0x0E4A, 'uni0E4A', 'uni0E4A.small'),
(0x0E4B, 'uni0E4B', 'uni0E4B.small'),
(0x0E4C, 'uni0E4C', 'uni0E4C.small'),
# Code Description C90 Category
# U+F700 uni0E10.descless base.descless
(0xF700, 'uni0E10.descless', 'thoThan-thai.less'),
# U+F701-04 uni0E34-37.left upper.left
(0xF701, 'uni0E34.left', 'uni0E34.narrow'),
(0xF702, 'uni0E35.left', 'uni0E35.narrow'),
(0xF703, 'uni0E36.left', 'uni0E36.narrow'),
(0xF704, 'uni0E37.left', 'uni0E37.narrow'),
# U+F705-09 uni0E48-4C.lowleft top.lowleft
(0xF705, 'uni0E48.lowleft', 'uni0E48.narrow'),
(0xF706, 'uni0E49.lowleft', 'uni0E49.narrow'),
(0xF707, 'uni0E4A.lowleft', 'uni0E4A.narrow'),
(0xF708, 'uni0E4B.lowleft', 'maiChattawa-thai.narrow'),
(0xF709, 'uni0E4C.lowleft', 'uni0E4C.narrow'),
# U+F70A-0E uni0E48-4C.low top.low
(0xF70A, 'uni0E48.low', 'uni0E48'),
(0xF70B, 'uni0E49.low', 'uni0E49'),
(0xF70C, 'uni0E4A.low', 'uni0E4A'),
(0xF70D, 'uni0E4B.low', 'uni0E4B'),
(0xF70E, 'uni0E4C.low', 'uni0E4C'),
# U+F70F uni0E0D.descless base.descless
(0xF70F, 'uni0E0D.descless', 'yoYing-thai.less'),
# U+F710-12 uni0E31,4D,47.left upper.left
(0xF710, 'uni0E31.left', 'uni0E31.narrow'),
(0xF711, 'uni0E4D.left', 'nikhahit-thai.narrow'),
(0xF712, 'uni0E47.left', 'uni0E47.narrow'),
# U+F713-17 uni0E48-4C.left top.left
(0xF713, 'uni0E48.left', 'uni0E48.small', True), #'nikhahit_maiEk-thai.narrow', True),
(0xF714, 'uni0E49.left', 'uni0E49.small', True), #'nikhahit_maiTho-thai.narrow', True),
(0xF715, 'uni0E4A.left', 'uni0E4A.small', True), #'nikhahit_maiTri-thai.narrow', True),
(0xF716, 'uni0E4B.left', 'uni0E4B.small', True), #'nikhahit_maiChattawa-thai.narrow', True),
(0xF717, 'uni0E4C.left', 'uni0E4C.small', True),
# U+F718-1A uni0E38-3A.low lower.low
(0xF718, 'uni0E38.low', 'uni0E38.small'),
(0xF719, 'uni0E39.low', 'uni0E39.small'),
(0xF71A, 'uni0E3A.low', 'uni0E3A.small'),
]
# pprint(glyphs)
for item in c90:
if (len(item) == 3):
cmap[item[0]] = item[2]
else:
cmap[item[0]] = item[1]
hmtx[item[1]] = hmtx[item[2]]
glyph_order.append(item[1])
# glyphs[item[1]] = glyphs[item[2]]
glyphs[item[1]] = _g_l_y_f.Glyph()
glyphs[item[1]].fromXML('component', {'glyphName': item[2], 'x': '-190', 'y': '0', 'flags': '0x4' }, {}, font)
# cmap[item[0]] = item[1]
# hmtx[item[1]] = [0, 0]
# glyph_order.append(item[1])
# glyphs[item[1]] = _g_l_y_f.Glyph()
# pprint(glyphs)
# x = XMLWriter('a.xml')
# g = glyphs['nikhahit_maiEk-thai.narrow']
# g.expand(font['glyf'])
# g.toXML(x, font)
# pprint(hmtx['nikhahit_maiEk-thai'])
# pprint(hmtx['nikhahit_maiEk-thai.narrow'])
font.save(os.path.splitext(source)[0] + '-C90' + os.path.splitext(source)[1], False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment