Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Xzonn
Created August 4, 2020 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xzonn/4385c11a388d57158e7bbf4661d8bbcf to your computer and use it in GitHub Desktop.
Save Xzonn/4385c11a388d57158e7bbf4661d8bbcf to your computer and use it in GitHub Desktop.
import struct
import re
import random
import json
import os
import time
from PIL import Image, ImageDraw, ImageFont
from pngToBin import getPointSequence
with open("../main.gzf", "rb") as f:
data = list(f.read());
NOW = time.localtime();
# 改用 EOJ 的 gzf
with open("main.EOJ.gzf.bak", "rb") as f:
data = list(f.read());
def getEntry(data):
letter, unk1, height, imgId, width, left, top = struct.unpack("<HHHHHBB", bytes(data));
letter = chr(letter);
return {
"top": top,
"left": left,
"width": width,
"height": height,
"imgId": imgId,
"letter": letter
}
headerData = data[0x00: 0x38];
version, imgInfoOffset, unk1, entryLength = struct.unpack("<IHHI", bytes(data[0x04: 0x10]));
imgCount, entryCount, unk2, unk3 = struct.unpack("<IIII", bytes(data[0x10: 0x20]));
format, pad1, unk4, unk5, unk6, unk7 = struct.unpack("<BBHHHI", bytes(data[0x20: 0x2C]));
imgOffset, imgWidth, imgHeight = struct.unpack("<IHH", bytes(data[0x30: 0x38]));
entryData = [data[0x38 + i * 0x0C: 0x38 + (i + 1) * 0x0C] for i in range(entryCount)];
print(entryCount)
# headerData[0x14: 0x16] = [0xF0, 0x01];
# headerData[0x30: 0x34] = [0x80, 0x17, 0x00, 0x00]
'''
headerData[0x14: 0x16] = [0x44, 0x08]; # 46 * 46 = 2116 = 0x0844;
headerData[0x30: 0x34] = [0x00, 0x64, 0x00, 0x00]; # new offset = 0x6400;
headerData[0x37] = 0x04; # 1024 = 0x0400;
'''
entries = {};
oldEntries = {getEntry(i)["letter"]: getEntry(i) for i in entryData}
with open("C:/Codes/LuigiMansion/Translated/main.md", encoding="utf-8") as f:
newCharTable = sorted(set("  ©" + re.sub(r'\s', '', f.read())));
with open("GB2312.txt", encoding="utf-8") as f:
gb2312 = sorted(set(f.read()));
for i in range(len(newCharTable)):
if len(entries) >= entryCount:
print("字符数量超过最大值。");
print("".join(newCharTable[i:]));
break;
char = newCharTable[i];
if char not in entries:
if (ord(char) < 0x4E00 or ord(char) >= 0xE000) and char in oldEntries:
entries[char] = oldEntries[char];
if char in "“”":
entries[char].update({
"width": 2,
"height": 15
})
else:
entries[char] = {
"top": 0,
"left": 0,
"width": 2,
"height": 15,
"imgId": 0,
"letter": char
}
while len(entries) < entryCount:
char = random.choice(gb2312);
if char not in entries:
entries[char] = {
"top": 0,
"left": 0,
"width": 2,
"height": 15,
"imgId": 0,
"letter": char
}
keys = sorted(entries.keys());
newEntryData = [];
baseImg = Image.new("RGBA", (imgWidth, imgHeight));
for i in range(len(keys)):
char = keys[i];
entry = entries[char];
top, left = i // 51, i % 51;
width, height = entry["width"], entry["height"];
imgId, unk1 = 0, 0;
newEntryData += list(struct.pack("<HHHHHBB", ord(char), unk1, height, imgId, width, left, top));
if os.path.exists(("main - EOJ - ori/%04x" % ord(char)) + ".png"):
imPath = ("main - EOJ - ori/%04x" % ord(char)) + ".png";
elif os.path.exists(("../FontsTest/png/%04x" % ord(char)) + ".png"):
imPath = ("../FontsTest/png/%04x" % ord(char)) + ".png";
elif os.path.exists(("main - EOJ - edi/%04x" % ord(char)) + ".png"):
imPath = ("main - EOJ - edi/%04x" % ord(char)) + ".png";
if imPath:
im = Image.open(imPath);
baseImg.paste(im, (20 * left, 22 * top));
baseImg.save("main - EOJ.png");
points = getPointSequence(imgWidth, imgHeight);
nibbles = [];
for (x, y) in points:
nibbles.append(baseImg.getpixel((x, y))[3] // 16);
imgData = [];
for i in range(0, len(nibbles), 2):
imgData.append(nibbles[i] + nibbles[i + 1] * 16);
allData = headerData + newEntryData + [0] * (imgOffset - len(headerData) - len(newEntryData)) + imgData;
with open("main.gzf", "wb") as f:
f.write(bytes(allData));
with open("main.json", "w") as f:
json.dump(entries, f);
SW, SH = 20, 22;
ST, SL = 0, 0;
for x in range(baseImg.size[0]):
for y in range(baseImg.size[1]):
if baseImg.getpixel((x, y))[3] < 10:
sx, sy = (x - SL) // SW, (y - ST) // SH;
baseImg.putpixel((x, y), (127, 255, 0, 255) if (sx + sy) % 2 else (255, 0, 127, 255));
baseImg.save("main - EOJ.paint.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment