-
-
Save southwolf/5271031 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
# coding=utf-8 | |
import Image, ImageDraw, ImageFont | |
import struct | |
font_width = 16 | |
font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", font_width) | |
def get_font_data(char): | |
(width, height) = font.getsize(char) | |
image = Image.new('1', (width, height), 255) | |
draw = ImageDraw.Draw(image) | |
draw.text((0,0), char, font=font) | |
(left, top) = (0, 0) | |
(right, bottom) = (0, 0) | |
data = image.getdata() | |
find_top = False | |
for y in range(height): | |
for x in range(width): | |
if data[x + y*width] == 0: | |
find_top = True | |
top = y | |
break | |
if find_top: | |
break | |
find_bottom = False | |
for y in reversed(range(height)): | |
for x in range(width): | |
if data[x + y*width] == 0: | |
find_bottom = True | |
bottom = y + 1 | |
break | |
if find_bottom: | |
break | |
find_left = False | |
for x in range(width): | |
for y in range(height): | |
if data[x + y*width] == 0: | |
find_left = True | |
left = x | |
break | |
if find_left: | |
break | |
find_right = False | |
for x in reversed(range(width)): | |
for y in range(height): | |
if data[x + y*width] == 0: | |
find_right = True | |
right = x + 1 | |
break | |
if find_right: | |
break | |
(real_width, real_height) = (right-left, bottom-top) | |
real_data = [] | |
count = 0 | |
byte = 0 | |
for y in range(top, bottom): | |
for x in range(left, right): | |
write_end = False | |
if data[x + y*width] == 0: | |
byte = byte + (1 << (count%8)) | |
count = count + 1 | |
if count % 8 == 0: | |
write_end = True | |
real_data.append(byte) | |
byte = 0 | |
if not write_end: | |
real_data.append(byte) | |
return (real_data, (real_width, real_height), (left, top), (width, height)) | |
fonts = {} | |
lookup_table = [] | |
lookup_offset = 1 | |
gb2312_offset = 160 | |
max_height = 0 | |
for i in range(160+16,160+87): | |
for j in range(160+1,160+95): | |
char = (chr(i) + chr(j)).decode('gb18030') | |
(data, (width, height), (left_margin, top_margin), (whole_width, whole_height)) = get_font_data(char) | |
lookup_table.append({ | |
'char': char, | |
'offset': lookup_offset, | |
'width': width, | |
'height': height, | |
'left': left_margin, | |
'top': top_margin, | |
'whole_width': whole_width, | |
'data': data, | |
}) | |
if whole_height > max_height: | |
max_height = whole_height | |
lookup_offset = lookup_offset + (8 + (width * height)/8 + 3)/4 | |
res = open('myres', 'wb') | |
res.write('\x01') | |
res.write(struct.pack('B', max_height)) | |
res.write(struct.pack('H', len(lookup_table))) | |
res.write('\x56\x34') | |
for entry in lookup_table: | |
res.write(struct.pack('H', ord(entry['char']))) | |
res.write(struct.pack('H', entry['offset'])) | |
fonts[entry['char']] = entry | |
count = 0 | |
bitmap = '' | |
for byte in entry['data']: | |
mask = 0x01 | |
while mask <= 0x80: | |
if byte & mask: | |
bitmap = bitmap + '#' | |
else: | |
bitmap = bitmap + ' ' | |
count = count + 1 | |
if count % entry['width'] == 0: | |
bitmap = bitmap + '\n' | |
mask = mask << 1 | |
entry['bitmap'] = bitmap | |
end = 0x6 + len(lookup_table) * 4 | |
for entry in lookup_table: | |
res.seek(end + entry['offset']*4) | |
res.write(struct.pack("B", entry['width'])) | |
res.write(struct.pack("B", entry['height'])) | |
res.write(struct.pack("b", entry['left'])) | |
res.write(struct.pack("b", entry['top'])) | |
res.write('\x00') | |
res.write('\x00') | |
res.write('\x00') | |
res.write(struct.pack("B", entry['whole_width'])) | |
for byte in entry['data']: | |
res.write(struct.pack("B", byte)) | |
res.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment