#!/usr/bin/env python3 """fti2xpm.py -- raster the IRIX .fti vector icons into an XPM (32x32 default, or the size given). The format is the GL-style one of the Foundation icon files: color(N), bgnpolygon/endpolygon (filled), bgnoutlinepolygon/bgnclosedline/bgnline (stroked), vertex(x, y). The colours are IRIX colour indices; without the IRIX rgb table they are mapped to a small grey/blue palette that keeps the shape readable. """ import os import re import sys W = H = 32 # colour index -> XPM character; the reference's indices are not known # here, so everything negative (shadow) is dark, 0 black, 1 white, the # rest shades of grey/blue PALETTE = { 'k': '#000000', 'w': '#ffffff', 'd': '#202020', 'm': '#606060', 'l': '#a0a0a0', 'b': '#3f3f7f', 'B': '#707080', } def colour_char(n): try: n = int(n) except ValueError: return 'm' if n == 0: return 'k' if n == 1: return 'w' if n < 0: return 'b' if n % 3 == 0 else 'd' if n in (12, 14): return 'B' return 'm' def main(path, out, size=32): global W, H W = H = size txt = open(path, errors='replace').read() px = [['.'] * W for _ in range(H)] col = 'm' mode = None verts = [] def fill(verts, ch): if len(verts) < 3: return ys = [v[1] for v in verts] for y in range(max(0, int(min(ys))), min(H, int(max(ys)) + 1)): xs = [] n = len(verts) for i in range(n): x1, y1 = verts[i] x2, y2 = verts[(i + 1) % n] if (y1 <= y + 0.5 < y2) or (y2 <= y + 0.5 < y1): xs.append(x1 + (y + 0.5 - y1) * (x2 - x1) / (y2 - y1)) xs.sort() for i in range(0, len(xs) - 1, 2): for x in range(max(0, int(xs[i])), min(W, int(xs[i + 1]) + 1)): px[y][x] = ch def line(verts, ch): for i in range(len(verts) - 1): x1, y1 = verts[i] x2, y2 = verts[i + 1] steps = int(max(abs(x2 - x1), abs(y2 - y1)) * 2) + 1 for s in range(steps + 1): t = s / steps x = int(x1 + (x2 - x1) * t) y = int(y1 + (y2 - y1) * t) for dy in (0, 1): for dx in (0, 1): if 0 <= x + dx < W and 0 <= y + dy < H: px[y + dy][x + dx] = ch for line_txt in txt.splitlines(): s = line_txt.strip() m = re.match(r'color\((-?\d+)\)', s) if m: col = colour_char(m.group(1)) continue if s.startswith('bgnpolygon'): mode = 'poly' verts = [] continue if s.startswith('bgnoutlinepolygon') or s.startswith('bgnclosedline') or s.startswith('bgnline'): mode = 'line' verts = [] continue if s.startswith('endpolygon'): fill(verts, col) verts = [] mode = None continue if s.startswith('endoutlinepolygon') or s.startswith('endclosedline') or s.startswith('endline'): line(verts + ([verts[0]] if verts else []), col) verts = [] mode = None continue m = re.match(r'vertex\(([-\d.]+),\s*([-\d.]+)\)', s) if m and mode: verts.append((float(m.group(1)), float(m.group(2)))) lines = ['/* XPM */', '/* MCDE: %s, from the IRIX Foundation icon (fti2xpm.py) */' % os.path.basename(out), 'static char *%s[] = {' % re.sub(r'[^A-Za-z0-9_]', '_', os.path.basename(out)), '"%d %d 8 1 0 0",' % (W, H)] for ch, c in PALETTE.items(): lines.append('"%s c %s",' % (ch, c)) lines.append('". c None",') for y in range(H): lines.append('"' + ''.join(px[y]) + '",') lines.append('};') open(out, 'w').write('\n'.join(lines) + '\n') if __name__ == '__main__': main(sys.argv[1], sys.argv[2], int(sys.argv[3]) if len(sys.argv) > 3 else 32)