/ adafruit_bitmap_font / ttf.py
ttf.py
1 # pylint: skip-file 2 # Remove the above when TTF is actually supported. 3 4 import struct 5 6 7 # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html 8 9 10 class TTF: 11 def __init__(self, f): 12 f.seek(0) 13 self.file = f 14 15 self.characters = {} 16 17 def read(format): 18 s = struct.calcsize(format) 19 return struct.unpack_from(format, f.read(s)) 20 21 scalar_type = read(">I") 22 numTables, searchRange, entrySelector, rangeShift = read(">HHHH") 23 24 print(numTables) 25 table_info = {} 26 for _ in range(numTables): 27 tag, checkSum, offset, length = read(">4sIII") 28 print(tag.decode("utf-8"), hex(checkSum), offset, length) 29 table_info[tag] = (offset, length) 30 31 head_offset, head_length = table_info[b"head"] 32 f.seek(head_offset) 33 version, fontRevision, checkSumAdjustment, magicNumber = read(">IIII") 34 flags, unitsPerEm, created, modified = read(">HHQQ") 35 xMin, yMin, xMax, yMax = read(">hhhh") 36 print(xMin, yMin, xMax, yMax) 37 macStyle, lowestRecPPEM, fontDirectionHint = read(">HHh") 38 indexToLocFormat, glyphDataFormat = read(">hh") 39 40 glyf_offset, glyf_length = table_info[b"glyf"] 41 f.seek(glyf_offset) 42 while f.tell() < glyf_offset + glyf_length: 43 numberOfContours, xMin, yMin, xMax, yMax = read(">hhhhh") 44 45 if numberOfContours > 0: # Simple 46 print(numberOfContours) 47 ends = [] 48 for _ in range(numberOfContours): 49 ends.append(read(">H")) 50 instructionLength = read(">h")[0] 51 instructions = read(">{}s".format(instructionLength))[0] 52 print(instructions) 53 break 54 else: 55 raise RuntimeError("Unsupported font")