Skip to content

Instantly share code, notes, and snippets.

@balthild
Created November 7, 2022 02:49
Show Gist options
  • Save balthild/6962854434e9d98b970a243346401ae1 to your computer and use it in GitHub Desktop.
Save balthild/6962854434e9d98b970a243346401ae1 to your computer and use it in GitHub Desktop.
A FontForge script that fixes position of CJK parentheses in FangZheng fonts.
move_chars = [
("([{", 150),
(")]}", -150),
("〈《「『【〔〖", 150),
("〉》」』】〕〗", -150),
]
keep_puncts_only = True
puncts = [
# 这个列表来自 xeCJK 文档
# \c__xeCJK_FullLeft_chars_clist
## OP
0x2018, 0x201C,
0x3008, 0x300A, 0x300C, 0x300E, 0x3010, 0x3014, 0x3016, 0x3018, 0x301A, 0x301D,
0xFE17, 0xFE35, 0xFE37, 0xFE39, 0xFE3B, 0xFE3D, 0xFE3F, 0xFE41, 0xFE43, 0xFE47,
0xFE59, 0xFE5B, 0xFE5D, 0xFF08, 0xFF3B, 0xFF5B, 0xFF5F, 0xFF62,
## PR
0xFE69, 0xFF04, 0xFFE1, 0xFFE5, 0xFFE6,
# \c__xeCJK_FullRight_chars_clist
## CL
0x00B7, 0x2019, 0x201D, 0x2013, 0x2014, 0x2025, 0x2026, 0x2027, 0x2E3A,
0x3001, 0x3002, 0x3009, 0x300B, 0x300D, 0x300F, 0x3011, 0x3015, 0x3017, 0x3019,
0x301B, 0x301E, 0x301F, 0xFE11, 0xFE12, 0xFE18, 0xFE36, 0xFE38, 0xFE3A, 0xFE3C,
0xFE3E, 0xFE40, 0xFE42, 0xFE44, 0xFE48, 0xFE50, 0xFE52, 0xFE5A, 0xFE5C, 0xFE5E,
0xFF09, 0xFF0C, 0xFF0E, 0xFF3D, 0xFF5D, 0xFF60, 0xFF61, 0xFF63, 0xFF64,
## NS
0x30FB, 0xFE54, 0xFE55, 0xFF1A, 0xFF1B, 0xFF65, 0x16FE0,
## EX
0xFE15, 0xFE16, 0xFE56, 0xFE57, 0xFF01, 0xFF1F,
## IS
0xFE10, 0xFE13, 0xFE14,
## PO
0xFE6A, 0xFF05, 0xFFE0,
]
font = fontforge.activeFont()
if keep_puncts_only:
font.selection.all()
for glyph in font.selection.byGlyphs:
is_punct = False
if glyph.unicode in puncts:
is_punct = True
if glyph.altuni is not None:
for unicode, *rest in glyph.altuni:
if unicode in puncts:
is_punct = True
if not is_punct:
glyph.unlinkThisGlyph()
font.removeGlyph(glyph)
for (chars, dx) in move_chars:
for ch in chars:
font.selection.select(ord(ch))
glyph = list(font.selection.byGlyphs)[0]
layer = glyph.foreground.transform(psMat.translate(dx, 0))
glyph.foreground = layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment