Last active
February 2, 2023 20:04
-
-
Save kojiishi/1c6b2f616a88d251b2f749102a51ae1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import shlex | |
import subprocess | |
import uharfbuzz as hb | |
font_dir = 'fonts/g/mac/10.15.7/' | |
font_path = f'{font_dir}Hiragino Kaku Gothic W4.ttc' | |
font_path = f'{font_dir}Hiragino MaruGothic ProN W4.ttc' | |
font_path = f'{font_dir}Hiragino Mincho ProN.ttc' | |
text = '\uFF11' | |
with open(font_path, 'rb') as fontfile: | |
fontdata = fontfile.read() | |
face = hb.Face(fontdata) | |
font = hb.Font(face) | |
buf = hb.Buffer() | |
buf.set_message_func(lambda message: print(message)) | |
buf.add_str(text) | |
# buf.language = 'x-hbotJAN' | |
# buf.script = 'hani' | |
buf.guess_segment_properties() | |
features = {'palt': True} | |
hb.shape(font, buf, features) | |
infos = buf.glyph_infos | |
positions = buf.glyph_positions | |
for info, pos in zip(infos, positions): | |
gid = info.codepoint | |
cluster = info.cluster | |
x_advance = pos.x_advance | |
x_offset = pos.x_offset | |
y_offset = pos.y_offset | |
print(f"gid{gid}={cluster}@{x_advance},{x_offset}+{y_offset}") | |
args = ['hb-shape', | |
f'--font-file={font_path}', | |
# '--face-index=0', | |
# '--language=x-hbotJAN', | |
# '--script=hani', | |
'--trace', | |
f'--text={text}'] | |
# Without the feature. | |
# print(shlex.join(args)) | |
# subprocess.run(args) | |
# With the feature. | |
args.append(f'--features={",".join(features.keys())}') | |
print(shlex.join(args)) | |
subprocess.run(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment