| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #!/usr/bin/env python3
- """Copy an Xcursor theme, keeping only the images of one nominal size.
- Usage: tools/xcursor_trim.py SRC_DIR DST_DIR [SIZE]
- Used once to import assets/cursors/adwaita/ from the host's adwaita-cursors
- package (/usr/share/icons/Adwaita/cursors). Symlinks (legacy X11 aliases)
- are skipped; every regular file is rewritten as a valid Xcursor file that
- holds only the SIZE (default 24) image chunks, so the tree does not carry
- the 30..96 px images (12 MB for the whole theme, mostly the animated
- wait/progress spinners). Comment chunks are dropped.
- """
- import os
- import struct
- import sys
- XCUR_IMAGE = 0xFFFD0002
- def trim(data, size):
- magic, hdr, version, ntoc = struct.unpack_from('<4sIII', data, 0)
- if magic != b'Xcur':
- raise ValueError('not an Xcursor file')
- chunks = []
- for i in range(ntoc):
- typ, sub, pos = struct.unpack_from('<III', data, hdr + i * 12)
- if typ != XCUR_IMAGE or sub != size:
- continue
- chdr, ctyp, csub, cver, w, h, xh, yh, delay = \
- struct.unpack_from('<IIIIIIIII', data, pos)
- end = pos + chdr + w * h * 4
- chunks.append((typ, sub, data[pos:end]))
- if not chunks:
- raise ValueError('no %d px images' % size)
- out = bytearray(struct.pack('<4sIII', b'Xcur', 16, version, len(chunks)))
- pos = 16 + 12 * len(chunks)
- toc = bytearray()
- body = bytearray()
- for typ, sub, raw in chunks:
- toc += struct.pack('<III', typ, sub, pos + len(body))
- body += raw
- return bytes(out + toc + body)
- def main():
- if len(sys.argv) < 3:
- sys.exit(__doc__)
- src, dst = sys.argv[1], sys.argv[2]
- size = int(sys.argv[3]) if len(sys.argv) > 3 else 24
- os.makedirs(dst, exist_ok=True)
- for name in sorted(os.listdir(src)):
- path = os.path.join(src, name)
- if os.path.islink(path) or not os.path.isfile(path):
- continue
- with open(path, 'rb') as f:
- data = f.read()
- try:
- out = trim(data, size)
- except ValueError as e:
- print('skip %s: %s' % (name, e))
- continue
- with open(os.path.join(dst, name), 'wb') as f:
- f.write(out)
- if __name__ == '__main__':
- main()
|