Files
lishanpi/tools/patch_font_weight.py
T
hm 97f7d200a2 refactor: 将工具类文件迁移到 tools/ 目录
- 移动 10 个 Python 字体工具脚本到 tools/
- 移动 5 个 C 字体测试文件到 tools/
- 更新脚本路径引用适配新目录结构
- 清理 vue/vue3 前端模板文件
2026-05-07 17:10:39 +08:00

66 lines
2.4 KiB
Python

import struct
import os
ttf_path = "vue/vue3/src/assets/fonts/NotoSansSC-VariableFont_wght_2.ttf"
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ttf_path = os.path.join(project_root, ttf_path)
with open(ttf_path, "rb") as f:
data = bytearray(f.read())
sfVersion = data[0:4]
numTables = struct.unpack(">H", data[4:6])[0]
print(f"sfVersion: {sfVersion}")
print(f"numTables: {numTables}")
table_dir = {}
for i in range(numTables):
pos = 12 + i * 16
tag = data[pos:pos+4].decode("ascii")
checksum, offset, length = struct.unpack_from(">III", data, pos + 4)
table_dir[tag] = {"offset": offset, "length": length, "checksum": checksum}
print(f" Table {tag}: offset={offset}, length={length}, checksum=0x{checksum:08X}")
if "fvar" not in table_dir:
print("ERROR: No fvar table found!")
exit(1)
fvar = table_dir["fvar"]
fvar_data = data[fvar["offset"]:fvar["offset"]+fvar["length"]]
version_major, version_minor, axesArrayOffset, reserved, axisCount, axisSize, instanceCount, instanceSize = \
struct.unpack_from(">HHHHHHHH", fvar_data, 0)
print(f"\nfvar table at offset {fvar['offset']}:")
print(f" version: {version_major}.{version_minor}")
print(f" axesArrayOffset: {axesArrayOffset}")
print(f" axisCount: {axisCount}, axisSize: {axisSize}")
print(f" instanceCount: {instanceCount}, instanceSize: {instanceSize}")
axis_records_start = fvar["offset"] + axesArrayOffset
for i in range(axisCount):
pos = axis_records_start + i * axisSize
tag = data[pos:pos+4].decode("ascii")
minValue = struct.unpack_from(">i", data, pos + 4)[0]
defaultValue = struct.unpack_from(">i", data, pos + 8)[0]
maxValue = struct.unpack_from(">i", data, pos + 12)[0]
minWeight = minValue / 65536.0
defaultWeight = defaultValue / 65536.0
maxWeight = maxValue / 65536.0
print(f"\n Axis {i}: {tag}")
print(f" min={minValue} ({minWeight}), default={defaultValue} ({defaultWeight}), max={maxValue} ({maxWeight})")
if tag == "wght" and defaultWeight == 100.0:
new_default = int(500 * 65536)
struct.pack_into(">i", data, pos + 8, new_default)
print(f" *** PATCHED default from {defaultWeight} to 500 (0x{new_default:08X})")
new_ttf_path = ttf_path.replace(".ttf", "_medium.ttf")
with open(new_ttf_path, "wb") as f:
f.write(data)
print(f"\nSaved patched font to: {new_ttf_path}")
print("Done!")