Advertisement
Guest User

ttc2ttf (beer)

a guest
Feb 13th, 2011
1,198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. #First released as C++ program by Hiroyuki Tsutsumi as part of the free software suite “Beer”
  5. #I thought porting it to Python could be both a challenge and useful
  6.  
  7. from sys import argv, exit, getsizeof
  8. from struct import pack_into, unpack_from
  9.  
  10. def ceil4(n):
  11.     """returns the next integer which is a multiple of 4"""
  12.     return (n + 3) & ~3
  13.  
  14. if len(argv)!=2:
  15.     print("Usage: %s FontCollection.ttc" % argv)
  16.     exit(2)
  17.  
  18. filename = argv[1]
  19. in_file = open(filename, "rb")
  20. buf     = in_file.read()
  21. in_file.close()
  22.  
  23. if filename.lower().endswith(".ttc"):
  24.     filename = filename[:-4]
  25.  
  26.  
  27. if buf[:4] != b"ttcf":
  28.     out_filename = "%s.ttf" % filename
  29.     out_file = open(out_filename, "wb")
  30.     out_file.write(buf)
  31.     #end, so we don’t have to close the files or call exit() here
  32. else:
  33.     ttf_count        = unpack_from("!L", buf, 0x08)[0]
  34.     print("Anzahl enthaltener TTF-Dateien: %s" % ttf_count)
  35.     ttf_offset_array = unpack_from("!"+ttf_count*"L", buf, 0x0C)
  36.     for i in range(ttf_count):
  37.         print("Extrahiere TTF #%s:" % (i+1))
  38.         table_header_offset = ttf_offset_array[i]
  39.         print("\tHeader beginnt bei Byte %s" % table_header_offset)
  40.         table_count =  unpack_from("!H", buf, table_header_offset+0x04)[0]
  41.         header_length = 0x0C + table_count * 0x10
  42.         print("\tHeaderlänge: %s Byte" % header_length)
  43.        
  44.         table_length = 0
  45.         for j in range(table_count):
  46.             length = unpack_from("!L", buf, table_header_offset+0x0C+0x0C+j*0x10)[0]
  47.             table_length += ceil4(length)
  48.        
  49.         total_length = header_length + table_length
  50.         new_buf = bytearray(total_length)
  51.         header = unpack_from(header_length*"c", buf, table_header_offset)
  52.         pack_into(header_length*"c", new_buf, 0, *header)
  53.         current_offset = header_length
  54.        
  55.         for j in range(table_count):
  56.             offset = unpack_from("!L", buf, table_header_offset+0x0C+0x08+j*0x10)[0]
  57.             length = unpack_from("!L", buf, table_header_offset+0x0C+0x0C+j*0x10)[0]
  58.             pack_into("!L", new_buf, 0x0C+0x08+j*0x10, current_offset)
  59.             current_table = unpack_from(length*"c", buf, offset)
  60.             pack_into(length*"c", new_buf, current_offset, *current_table)
  61.            
  62.             #table_checksum = sum(unpack_from("!"+("L"*length), new_buf, current_offset))
  63.             #pack_into("!L", new_buf, 0x0C+0x04+j*0x10, table_checksum)
  64.            
  65.             current_offset += ceil4(length)
  66.        
  67.         out_file = open("%s%d.ttf"%(filename, i), "wb")
  68.         out_file.write(new_buf)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement