make-theme.py (5280B)
1 #!/usr/bin/env python3 2 # makes the chamesle boot menu theme in files/usr/share/grub/themes/chamesle: the pictures, and the fonts as grub's 3 # pf2 (void's grub comes without grub-mkfont, so they're drawn here with pillow) 4 # make-theme.py INTER_DIR the folder with Inter-Regular.ttf and Inter-Bold.ttf 5 import os 6 import struct 7 import sys 8 9 from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont 10 11 HERE = os.path.dirname(os.path.abspath(__file__)) 12 OUT = os.path.join(HERE, 'files/usr/share/grub/themes/chamesle') 13 ART = os.path.join(HERE, '../chamesle-artwork/files/usr/share/backgrounds/chamesle/grub.png') 14 # ascii, latin-1 for names in other languages, and the arrows and dot of the hints. grub wants them in order 15 CHARS = sorted([c for c in range(0x20, 0x7f)] + [c for c in range(0xa0, 0x100)] + [0x2191, 0x2193, 0x2022, 0x2026, 0x2013, 0x2014]) 16 17 18 def pf2(ttf, size, family, weight, path): 19 font = ImageFont.truetype(ttf, size) 20 ascent, descent = font.getmetrics() 21 glyphs = [] 22 for cp in CHARS: 23 ch = chr(cp) 24 if font.getmask(ch).getbbox() is None and cp != 0x20 and cp != 0xa0: 25 continue 26 adv = round(font.getlength(ch)) 27 l, t, r, b = font.getbbox(ch, anchor='ls') 28 w, h = max(r - l, 0), max(b - t, 0) 29 bits = b'' 30 if w and h: 31 img = Image.new('L', (w, h), 0) 32 d = ImageDraw.Draw(img) 33 d.fontmode = '1' 34 d.text((-l, -t), ch, font=font, fill=255, anchor='ls') 35 px = img.load() 36 # the rows one after another, no padding between them, most significant bit first 37 acc, n, out = 0, 0, bytearray() 38 for y in range(h): 39 for x in range(w): 40 acc = acc << 1 | (px[x, y] > 127) 41 n += 1 42 if n == 8: 43 out.append(acc); acc, n = 0, 0 44 if n: 45 out.append(acc << (8 - n)) 46 bits = bytes(out) 47 # yoffset: from the baseline up to the bitmap's bottom row 48 glyphs.append((cp, struct.pack('>HHhhh', w, h, l, -b, adv) + bits)) 49 name = '%s %s %d' % (family, weight.capitalize(), size) 50 51 def sec(tag, data): 52 return tag + struct.pack('>I', len(data)) + data 53 54 head = (sec(b'FILE', b'PFF2') + sec(b'NAME', name.encode() + b'\0') + sec(b'FAMI', family.encode() + b'\0') + 55 sec(b'WEIG', weight.encode() + b'\0') + sec(b'SLAN', b'normal\0') + sec(b'PTSZ', struct.pack('>H', size)) + 56 sec(b'MAXW', struct.pack('>H', max(struct.unpack('>H', g[1][:2])[0] for g in glyphs))) + 57 sec(b'MAXH', struct.pack('>H', max(struct.unpack('>H', g[1][2:4])[0] for g in glyphs))) + 58 sec(b'ASCE', struct.pack('>H', ascent)) + sec(b'DESC', struct.pack('>H', descent))) 59 # the index points at each glyph by its place in the file 60 pos = len(head) + 8 + 9 * len(glyphs) + 8 61 index, data = b'', b'' 62 for cp, g in glyphs: 63 index += struct.pack('>IBI', cp, 0, pos + len(data)) 64 data += g 65 with open(path, 'wb') as f: 66 f.write(head + b'CHIX' + struct.pack('>I', len(index)) + index + b'DATA' + b'\xff\xff\xff\xff' + data) 67 return name 68 69 70 def box(prefix, fill, radius, border=None): 71 # a rounded box in grub's nine pieces: corners, edges and the middle, which grub stretches 72 s = radius * 2 + 1 73 big = Image.new('RGBA', (s * 4, s * 4), (0, 0, 0, 0)) 74 ImageDraw.Draw(big).rounded_rectangle((0, 0, s * 4 - 1, s * 4 - 1), radius * 4, fill=fill, 75 outline=border, width=4 if border else 0) 76 img = big.resize((s, s), Image.LANCZOS) 77 r = radius 78 parts = {'nw': (0, 0, r, r), 'n': (r, 0, r + 1, r), 'ne': (r + 1, 0, s, r), 79 'w': (0, r, r, r + 1), 'c': (r, r, r + 1, r + 1), 'e': (r + 1, r, s, r + 1), 80 'sw': (0, r + 1, r, s), 's': (r, r + 1, r + 1, s), 'se': (r + 1, r + 1, s, s)} 81 for k, b in parts.items(): 82 img.crop(b).save(os.path.join(OUT, '%s_%s.png' % (prefix, k))) 83 84 85 def main(): 86 inter = sys.argv[1] 87 os.makedirs(OUT, exist_ok=True) 88 # what this makes, theme.txt is written by hand 89 for f in os.listdir(OUT): 90 if f.endswith(('.png', '.pf2')): 91 os.remove(os.path.join(OUT, f)) 92 names = [pf2(os.path.join(inter, 'Inter-Regular.ttf'), 16, 'Inter', 'regular', os.path.join(OUT, 'inter-16.pf2')), 93 pf2(os.path.join(inter, 'Inter-Regular.ttf'), 20, 'Inter', 'regular', os.path.join(OUT, 'inter-20.pf2')), 94 pf2(os.path.join(inter, 'Inter-Bold.ttf'), 32, 'Inter', 'bold', os.path.join(OUT, 'inter-bold-32.pf2'))] 95 # the wallpaper a little darker and softer, so the menu reads well on it. small: grub decodes it slowly, blurred 96 # it loses nothing when stretched 97 bg = Image.open(ART).convert('RGB').resize((1280, 720), Image.LANCZOS).filter(ImageFilter.GaussianBlur(2)) 98 ImageEnhance.Brightness(bg).enhance(0.8).save(os.path.join(OUT, 'background.png'), optimize=True) 99 box('menu', (12, 20, 40, 170), 14, border=(255, 255, 255, 40)) 100 box('select', (255, 255, 255, 235), 10) 101 # the same size, invisible: grub moves an item by its box's border, this way every item moves the same 102 box('item', (0, 0, 0, 0), 10) 103 print('\n'.join(names)) 104 105 106 main()