mirror of
https://gitea.tendokyu.moe/moegrid/sega-mount.git
synced 2026-10-13 01:28:43 +03:00
87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
import os
|
|
import re
|
|
import json
|
|
|
|
# 获取当前脚本文件所在的文件夹路径
|
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
# 拼接出绝对路径,确保一定能找到同目录下的 icons.json
|
|
JSON_FILE = os.path.join(SCRIPT_DIR, 'info.json')
|
|
HEADER_FILE = os.path.join(SCRIPT_DIR, 'lucide.h')
|
|
|
|
|
|
def hex_to_utf8_escape(hex_str):
|
|
"""
|
|
将 JSON 中的 '\\e585' 或 'e585' 转化为 C++ 可用的 UTF-8 转义序列,如 "\\xEE\\x96\\x85"
|
|
"""
|
|
# 提取纯十六进制字符串
|
|
clean_hex = hex_str.replace('\\', '').replace('e', 'E', 1) if hex_str.startswith('\\') else hex_str
|
|
code_point = int(clean_hex, 16)
|
|
|
|
# 编码为 UTF-8 字节
|
|
utf8_bytes = chr(code_point).encode('utf-8')
|
|
|
|
# 转为 C++ 转义字符串格式 "\\xEE\\x96\\x85"
|
|
return ''.join([f'\\x{b:02X}' for b in utf8_bytes])
|
|
|
|
|
|
def sanitize_macro_name(key):
|
|
"""
|
|
将图标 key (如 'a-arrow-down') 转换为合法的 C++ 宏名称 (如 'ICON_A_ARROW_DOWN')
|
|
"""
|
|
# 将连字符/空格等替换为下划线,转大写
|
|
clean_key = re.sub(r'[^a-zA-Z0-9_]', '_', key)
|
|
return f"ICON_{clean_key.upper()}"
|
|
|
|
|
|
def generate_header():
|
|
with open(JSON_FILE, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
|
|
macros = []
|
|
min_code = 0xFFFF
|
|
max_code = 0x0000
|
|
|
|
for key, info in data.items():
|
|
encoded_code = info.get('encodedCode', '')
|
|
if not encoded_code:
|
|
continue
|
|
|
|
# 解析 Unicode 码位,用于计算范围
|
|
clean_hex = encoded_code.replace('\\', '')
|
|
code_point = int(clean_hex, 16)
|
|
|
|
if code_point < min_code:
|
|
min_code = code_point
|
|
if code_point > max_code:
|
|
max_code = code_point
|
|
|
|
macro_name = sanitize_macro_name(key)
|
|
utf8_str = hex_to_utf8_escape(encoded_code)
|
|
|
|
macros.append((macro_name, utf8_str, key, encoded_code))
|
|
|
|
# 生成 C++ 头文件内容
|
|
header_content = []
|
|
header_content.append("// Auto-generated by generate_header.py")
|
|
header_content.append("#pragma once\n")
|
|
|
|
# 记录码位范围(ImGui 加载字体时需要用到)
|
|
header_content.append(f"#define ICON_MIN 0x{min_code:04X}")
|
|
header_content.append(f"#define ICON_MAX 0x{max_code:04X}\n")
|
|
|
|
# 生成宏定义
|
|
for macro_name, utf8_str, original_key, raw_hex in macros:
|
|
# 对齐排版
|
|
header_content.append(f'#define {macro_name:<30} "{utf8_str}" // {raw_hex} ({original_key})')
|
|
|
|
with open(HEADER_FILE, 'w', encoding='utf-8') as f:
|
|
f.write('\n'.join(header_content))
|
|
|
|
print(f"✅ 成功生成头文件 {HEADER_FILE}!共处理了 {len(macros)} 个图标。")
|
|
print(f"📊 码位范围: 0x{min_code:04X} - 0x{max_code:04X}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
generate_header()
|