aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/misc/sstruct.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/fontTools/misc/sstruct.py')
-rw-r--r--Lib/fontTools/misc/sstruct.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/fontTools/misc/sstruct.py b/Lib/fontTools/misc/sstruct.py
index ba1f8788..6db8b515 100644
--- a/Lib/fontTools/misc/sstruct.py
+++ b/Lib/fontTools/misc/sstruct.py
@@ -46,8 +46,8 @@ calcsize(fmt)
it returns the size of the data in bytes.
"""
-from fontTools.misc.py23 import tobytes, tostr
from fontTools.misc.fixedTools import fixedToFloat as fi2fl, floatToFixed as fl2fi
+from fontTools.misc.textTools import tobytes, tostr
import struct
import re
@@ -59,7 +59,7 @@ class Error(Exception):
pass
def pack(fmt, obj):
- formatstring, names, fixes = getformat(fmt)
+ formatstring, names, fixes = getformat(fmt, keep_pad_byte=True)
elements = []
if not isinstance(obj, dict):
obj = obj.__dict__
@@ -112,7 +112,8 @@ _elementRE = re.compile(
r"\s*" # whitespace
r"([A-Za-z_][A-Za-z_0-9]*)" # name (python identifier)
r"\s*:\s*" # whitespace : whitespace
- r"([cbBhHiIlLqQfd]|[0-9]+[ps]|" # formatchar...
+ r"([xcbB?hHiIlLqQfd]|" # formatchar...
+ r"[0-9]+[ps]|" # ...formatchar...
r"([0-9]+)\.([0-9]+)(F))" # ...formatchar
r"\s*" # whitespace
r"(#.*)?$" # [comment] + end of string
@@ -131,7 +132,7 @@ _fixedpointmappings = {
_formatcache = {}
-def getformat(fmt):
+def getformat(fmt, keep_pad_byte=False):
fmt = tostr(fmt, encoding="ascii")
try:
formatstring, names, fixes = _formatcache[fmt]
@@ -153,8 +154,9 @@ def getformat(fmt):
if not m:
raise Error("syntax error in fmt: '%s'" % line)
name = m.group(1)
- names.append(name)
formatchar = m.group(2)
+ if keep_pad_byte or formatchar != "x":
+ names.append(name)
if m.group(3):
# fixed point
before = int(m.group(3))
@@ -182,6 +184,8 @@ def _test():
astr: 5s
afloat: f; adouble: d # multiple "statements" are allowed
afixed: 16.16F
+ abool: ?
+ apad: x
"""
print('size:', calcsize(fmt))
@@ -199,6 +203,7 @@ def _test():
i.afloat = 0.5
i.adouble = 0.5
i.afixed = 1.5
+ i.abool = True
data = pack(fmt, i)
print('data:', repr(data))