-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathttf.py
65 lines (52 loc) · 2.09 KB
/
ttf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# pylint: skip-file
# Remove the above when TTF is actually supported.
try:
from typing import Tuple
from io import FileIO
from displayio import Bitmap
except ImportError:
pass
import struct
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6glyf.html
class TTF:
def __init__(self, f: FileIO, bitmap: Bitmap) -> None:
f.seek(0)
self.file = f
self.characters = {}
def read(format: str) -> Tuple:
s = struct.calcsize(format)
return struct.unpack_from(format, f.read(s))
scalar_type = read(">I")
numTables, searchRange, entrySelector, rangeShift = read(">HHHH")
print(numTables)
table_info = {}
for _ in range(numTables):
tag, checkSum, offset, length = read(">4sIII")
print(tag.decode("utf-8"), hex(checkSum), offset, length)
table_info[tag] = (offset, length)
head_offset, head_length = table_info[b"head"]
f.seek(head_offset)
version, fontRevision, checkSumAdjustment, magicNumber = read(">IIII")
flags, unitsPerEm, created, modified = read(">HHQQ")
xMin, yMin, xMax, yMax = read(">hhhh")
print(xMin, yMin, xMax, yMax)
macStyle, lowestRecPPEM, fontDirectionHint = read(">HHh")
indexToLocFormat, glyphDataFormat = read(">hh")
glyf_offset, glyf_length = table_info[b"glyf"]
f.seek(glyf_offset)
while f.tell() < glyf_offset + glyf_length:
numberOfContours, xMin, yMin, xMax, yMax = read(">hhhhh")
if numberOfContours > 0: # Simple
print(numberOfContours)
ends = []
for _ in range(numberOfContours):
ends.append(read(">H"))
instructionLength = read(">h")[0]
instructions = read(">{}s".format(instructionLength))[0]
print(instructions)
break
else:
raise RuntimeError("Unsupported font")