#!/usr/bin/env python3 """Removes the embedded bitmap strikes (EBDT/EBLC/EBSC/BDF) from Wine's Tahoma so FreeType renders outlines. Why: the OldEraMT2 client draws its UI text with its own FreeType code (EterLib FontManager, LCD rendering). With Wine's tahoma.ttf, FreeType returns the embedded 1-bit bitmaps for the small UI sizes and the client shows garbled slivers. Microsoft's Tahoma has no bitmap strikes. The outlines, metrics and names of the font stay untouched. Wine's fonts are LGPL 2.1+ (modified copy; source = Wine 11.0 fonts/tahoma.sfd). usage: strip-font-bitmaps.py IN.ttf OUT.ttf""" import struct, sys DROP = {b'EBDT', b'EBLC', b'EBSC', b'BDF '} def checksum(data): data += b'\0' * (-len(data) % 4) return sum(struct.unpack('>%dI' % (len(data) // 4), data)) & 0xFFFFFFFF def main(src, dst): b = open(src, 'rb').read() version, num = struct.unpack('>4sH', b[:6]) assert version in (b'\0\1\0\0', b'true'), 'TrueType outlines expected' tables = [] for i in range(num): tag, _, off, length = struct.unpack('>4sIII', b[12 + 16 * i:28 + 16 * i]) if tag not in DROP: tables.append((tag, b[off:off + length])) tables.sort() n = len(tables) es = n.bit_length() - 1 sr = (1 << es) * 16 out = bytearray(struct.pack('>4sHHHH', version, n, sr, es, n * 16 - sr)) offset = 12 + 16 * n body = bytearray() records = [] for tag, data in tables: if tag == b'head': data = data[:8] + b'\0\0\0\0' + data[12:] # checkSumAdjustment = 0 while summing records.append((tag, checksum(data), offset + len(body), len(data))) body += data + b'\0' * (-len(data) % 4) for rec in records: out += struct.pack('>4sIII', *rec) font = out + body head = next(r for r in records if r[0] == b'head') adj = (0xB1B0AFBA - checksum(bytes(font))) & 0xFFFFFFFF font[head[2] + 8:head[2] + 12] = struct.pack('>I', adj) open(dst, 'wb').write(font) print('%s: %d -> %d tables, dropped %s' % (dst, num, n, sorted(t.decode() for t in DROP))) if __name__ == '__main__': main(sys.argv[1], sys.argv[2])