"""Prepara os recursos do Halma para a edicao web (roda dentro do projeto extraido, em assets/): - gera FontLabel (Arimo), FontButton (EB Garamond) e FontTitle (Arvo Bold), todas SIL OFL 1.1, em BMFont de texto + PNG, no lugar das fontes bitmap que estavam dentro do atlas halma.png (HALMA, uma exportacao da Arial, e font-export / font-title-export, do skin Flat Earth, sem a fonte de origem nomeada); as tres regioes saem do atlas (halma.atlas) e seus pixels sao apagados de halma.png; - apaga o que o jogo nao usa e nao tem origem clara (imagem da Wikimedia, botao de outro jogo, skins antigos, arquivos .aseprite). Uso: fix-assets.py """ import os, re, shutil, sys from PIL import Image, ImageDraw, ImageFont assets, fontdir = sys.argv[1:3] BUTTON_SIZE, TITLE_SIZE = 27, 28 # 1. fontes novas (BMFont em texto + pagina PNG), no lugar das tres fontes bitmap do atlas: HALMA (Arial) e as duas do # skin Flat Earth (font-export e font-title-export), cujas fontes de origem o autor do skin nao nomeia def make_font(ttf, size, name, wght=None): font = ImageFont.truetype(ttf, size) ascent, descent = font.getmetrics() glyphs = [] for c in range(32, 127): ch = chr(c) l, t, r, b = font.getbbox(ch, anchor="la") w, h = max(r - l, 0), max(b - t, 0) img = Image.new("L", (max(w, 1), max(h, 1)), 0) if w and h: ImageDraw.Draw(img).text((-l, -t), ch, font=font, fill=255, anchor="la") glyphs.append((c, img, w, h, l, t, round(font.getlength(ch)))) PAD, W, H = 2, 512, 256 page = Image.new("RGBA", (W, H), (255, 255, 255, 0)) x = y = PAD row_h = 0 lines = [] for c, img, w, h, l, t, adv in glyphs: if x + w + PAD > W: x, y, row_h = PAD, y + row_h + PAD, 0 if w and h: rgba = Image.new("RGBA", img.size, (255, 255, 255, 0)) rgba.putalpha(img) page.paste(rgba, (x, y)) lines.append("char id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d page=0 chnl=0" % (c, x, y, w, h, l, t, adv)) x += w + PAD row_h = max(row_h, h) assert y + row_h + PAD <= H, "pagina da fonte pequena" page.save(os.path.join(assets, name + ".png"), optimize=False) with open(os.path.join(assets, name + ".fnt"), "w", newline="\n") as f: f.write('info face="%s" size=%d bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=2,2\n' % (name, size)) f.write("common lineHeight=%d base=%d scaleW=%d scaleH=%d pages=1 packed=0\n" % (ascent + descent, ascent, W, H)) f.write('page id=0 file="%s.png"\n' % name) f.write("chars count=%d\n" % len(lines)) f.write("\n".join(lines) + "\n") make_font(os.path.join(fontdir, "Arimo.ttf"), 32, "FontLabel") make_font(os.path.join(fontdir, "EBGaramond.ttf"), BUTTON_SIZE, "FontButton") make_font(os.path.join(fontdir, "Arvo-Bold.ttf"), TITLE_SIZE, "FontTitle") # 2. tira as tres regioes do atlas e apaga os pixels delas atlas = os.path.join(assets, "halma.atlas") png = os.path.join(assets, "halma.png") text = open(atlas, encoding="utf-8").read() atl = Image.open(png).convert("RGBA") for name in ["HALMA", "font-export", "font-title-export"]: m = re.search(r"^" + re.escape(name) + r"\n rotate: false\n xy: (\d+), (\d+)\n size: (\d+), (\d+)\n orig: [^\n]*\n offset: [^\n]*\n index: -1\n", text, re.M) assert m, "regiao %s nao achada" % name x0, y0, w0, h0 = map(int, m.groups()) text = text[:m.start()] + text[m.end():] atl.paste((0, 0, 0, 0), (x0, y0, x0 + w0, y0 + h0)) open(atlas, "w", encoding="utf-8", newline="\n").write(text) atl.save(png, optimize=False) for f in ["HALMA.fnt", "font-export.fnt", "font-title-export.fnt"]: os.remove(os.path.join(assets, f)) # 3. recursos sem uso e sem origem clara for p in ["800px-Halma4.svg.png", "b&w_DodgeGame_playButton.png", "skin", "Skin.json", "Skin.png", "Skin.atlas", "Roboto-Thin.fnt", "font-export.fnt.bak"]: p = os.path.join(assets, p) if os.path.isdir(p): shutil.rmtree(p) elif os.path.exists(p): os.remove(p) for f in os.listdir(assets): if re.fullmatch(r"strings_.*\.properties", f): # a edicao web so usa o ingles (Halma.java) os.remove(os.path.join(assets, f)) for f in os.listdir(os.path.join(assets, "redesign")): if f.endswith(".aseprite"): os.remove(os.path.join(assets, "redesign", f)) print("ok")