Skip to content

Commit 4336fda

Browse files
authored
πŸ—œοΈ Support zstd compression algorithm (#129)
* πŸ—œοΈ Support `zstd` compression algorithm * πŸ“¦ Update `fig-kiwi` to version supporting `zstd` * πŸ“ Update max supported fig format version
1 parent bd5845c commit 4336fda

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

β€Žpyproject.tomlβ€Ž

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ name = "fig2sketch"
33
readme = "README.md"
44
requires-python = ">=3.10"
55
dynamic = ["version"]
6-
dependencies = ["Pillow==10.3.0", "fonttools==4.43.0", "appdirs==1.4.4"]
6+
dependencies = [
7+
"Pillow==10.3.0",
8+
"fonttools==4.43.0",
9+
"appdirs==1.4.4",
10+
"zstd==1.5.5.1",
11+
]
712

813
[project.scripts]
914
fig2sketch = "fig2sketch:main"
@@ -16,8 +21,8 @@ build-backend = "setuptools.build_meta"
1621
enabled = true
1722

1823
[project.optional-dependencies]
19-
fast = ["fig-kiwi==0.1.0"]
20-
dev = ["black==24.2.0", "mypy==0.991", "pytest==8.2.0", "fig-kiwi==0.1.0"]
24+
fast = ["fig-kiwi==0.1.1"]
25+
dev = ["black==24.2.0", "mypy==0.991", "pytest==8.2.0", "fig-kiwi==0.1.1"]
2126

2227
[tool.black]
2328
line-length = 99

β€Žsrc/figformat/kiwi.pyβ€Ž

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import ctypes
33
from collections import OrderedDict
44
import zlib
5+
import zstd
56
import struct
67
import io
78
import logging
@@ -144,7 +145,11 @@ def _decode_type_inner(self, kw, type_id, array):
144145

145146
def decode(reader, type_converters):
146147
MIN_SUPPORTED_VERSIONS = 15
147-
MAX_SUPPORTED_VERSION = 25
148+
MAX_SUPPORTED_VERSION = 70
149+
150+
# `zstd` data will start with a magic number frame
151+
# the value of which is `0xfd2fb528`
152+
ZSTD_MAGIC_NUMBER = b"\x28\xb5\x2f\xfd"
148153

149154
header = reader.read(12)
150155
fig_version = struct.unpack("<I", header[8:12])[0]
@@ -159,10 +164,29 @@ def decode(reader, type_converters):
159164

160165
segment_header = reader.read(4)
161166
size = struct.unpack("<I", segment_header)[0]
162-
data = io.BytesIO(zlib.decompress(reader.read(size), wbits=-15))
163-
schema = KiwiSchema(data)
167+
compressedSchema = reader.read(size)
168+
169+
schemaData = b""
170+
# Check to see if the first four bytes are the zstd magic number.
171+
# If so we need to use zstd to decompress, not zlib
172+
if compressedSchema.startswith(ZSTD_MAGIC_NUMBER):
173+
schemaData = io.BytesIO(zstd.decompress(compressedSchema))
174+
else:
175+
schemaData = io.BytesIO(zlib.decompress(compressedSchema, wbits=-15))
176+
177+
schema = KiwiSchema(schemaData)
164178

165179
segment_header = reader.read(4)
166180
size = struct.unpack("<I", segment_header)[0]
167-
data = io.BytesIO(zlib.decompress(reader.read(size), wbits=-15))
181+
compressedData = reader.read(size)
182+
183+
data = b""
184+
# Check to see if the first four bytes are the zstd magic number.
185+
# If so we need to use zstd to decompress, not zlib
186+
187+
if compressedData.startswith(ZSTD_MAGIC_NUMBER):
188+
data = io.BytesIO(zstd.decompress(compressedData))
189+
else:
190+
data = io.BytesIO(zlib.decompress(compressedData, wbits=-15))
191+
168192
return KiwiDecoder(schema, type_converters).decode(data, "Message")

0 commit comments

Comments
Β (0)