Skip to content

Commit

Permalink
Update pnghex
Browse files Browse the repository at this point in the history
  • Loading branch information
lakejason0 committed Sep 15, 2024
1 parent 41a6efc commit 1468090
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions tools/pnghex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ def png_to_hex(png_file):
byte = ''
return hex_string


def png_to_hex_mc(png_file):
img = Image.open(png_file)
img = Image.open(png_file)
width, height = img.size
data = list(img.getdata())
hex_string = ''
for y in range(height):
row = data[y*width:(y+1)*width]
byte = ''
for x in range(width):
byte += '1' if row[x] == (255,255,255,255) else '0'
byte += '1' if row[x] == (255, 255, 255, 255) else '0'
if (x+1) % 8 == 0:
hex_string += f'{int(byte, 2):02X}'
byte = ''
Expand Down Expand Up @@ -103,3 +104,69 @@ def remove_identical_glyphs(partial_hex_file, unifont_hex_file, final_hex_file):
if partial_hex_file_glyphs[k] != unifont_hex_file_glyphs.get(k):
final_hex_file.write(k+":"+partial_hex_file_glyphs[k]+"\n")

# create_hk_patch_from_tw_hex(r'D:\farstar\\',r'C:\Users\Lakeus\Downloads\unifont-16.0.01\unifont-16.0.01\font\precompiled\unifont_all-16.0.01.hex',r'D:\farstar\hk=#tw.txt',r'D:\farstar\glyph_82_tw.hex',r'D:\farstar\glyph_82_hk.hex',0x8200)
def create_hk_patch_from_tw_hex(path, unifont_hex_file, hk_ref_tw_list, tw_hex_file, final_hex_file, cp_start=0x4E00):
unifont_glyphs = read_hex_file_glyphs(unifont_hex_file)
tw_glyphs = read_hex_file_glyphs(tw_hex_file)
with open(hk_ref_tw_list,'r',encoding='utf-8') as f:
hk_ref_tw_str = f.read()
glyphs = {}
for i in range(256):
cp = cp_start + i
cp_str = hex(cp)[2:].upper()
if chr(cp) in hk_ref_tw_str:
glyphs[cp_str] = tw_glyphs[cp_str]
else:
glyphs[cp_str] = unifont_glyphs[cp_str]

for filename in os.listdir(path):
if filename.endswith("-hk.png"):
png_file = os.path.join(path, filename)
unicode_value = filename.split('u')[1].split('-hk.')[0]
glyphs[unicode_value.upper()] = png_to_hex_mc(png_file)
with open(final_hex_file,'w',newline='\n') as final_hex_file:
for k in glyphs:
final_hex_file.write(k+':'+glyphs[k]+'\n')

def unihex_to_unicode_page(hex_file, output_image_file, cp_start=0x4E00, img_size=256, sub_img_size=16, glyph_size=(16, 16)):
# 创建256x256的图像,背景为白色
img = Image.new('1', (img_size, img_size), color=1)

# 读取hex文件中的字形数据
glyphs = read_hex_file_glyphs(hex_file)

# 初始化图块位置
x = 0
y = 0

# 从指定的码位开始处理
glyph_items = list(glyphs.items())
for unicode_val, hex_string in glyph_items:
# 如果当前码位小于开始码位,跳过
if int(unicode_val, 16) < cp_start:
continue

# 将hex字形转换为像素数据
glyph_data = []
for i in range(0, len(hex_string), 2):
byte = format(int(hex_string[i:i+2], 16), '08b')
glyph_data.extend([0 if b == '0' else 255 for b in byte])

# 创建一个16x16的图块
glyph_img = Image.new('1', glyph_size)
glyph_img.putdata(glyph_data)

# 将图块粘贴到大图上
img.paste(glyph_img, (x, y))

# 更新图块位置,每16个图块换一行
x += sub_img_size
if x >= img_size:
x = 0
y += sub_img_size
if y >= img_size:
break

# 保存为PNG文件
img.save(output_image_file)

0 comments on commit 1468090

Please sign in to comment.