Skip to content

Commit

Permalink
πŸ—œοΈ Support zstd compression algorithm (#129)
Browse files Browse the repository at this point in the history
* πŸ—œοΈ Support `zstd` compression algorithm
* πŸ“¦ Update `fig-kiwi` to version supporting `zstd`
* πŸ“ Update max supported fig format version
  • Loading branch information
tmdvs authored Sep 11, 2024
1 parent bd5845c commit 4336fda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ name = "fig2sketch"
readme = "README.md"
requires-python = ">=3.10"
dynamic = ["version"]
dependencies = ["Pillow==10.3.0", "fonttools==4.43.0", "appdirs==1.4.4"]
dependencies = [
"Pillow==10.3.0",
"fonttools==4.43.0",
"appdirs==1.4.4",
"zstd==1.5.5.1",
]

[project.scripts]
fig2sketch = "fig2sketch:main"
Expand All @@ -16,8 +21,8 @@ build-backend = "setuptools.build_meta"
enabled = true

[project.optional-dependencies]
fast = ["fig-kiwi==0.1.0"]
dev = ["black==24.2.0", "mypy==0.991", "pytest==8.2.0", "fig-kiwi==0.1.0"]
fast = ["fig-kiwi==0.1.1"]
dev = ["black==24.2.0", "mypy==0.991", "pytest==8.2.0", "fig-kiwi==0.1.1"]

[tool.black]
line-length = 99
Expand Down
32 changes: 28 additions & 4 deletions src/figformat/kiwi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ctypes
from collections import OrderedDict
import zlib
import zstd
import struct
import io
import logging
Expand Down Expand Up @@ -144,7 +145,11 @@ def _decode_type_inner(self, kw, type_id, array):

def decode(reader, type_converters):
MIN_SUPPORTED_VERSIONS = 15
MAX_SUPPORTED_VERSION = 25
MAX_SUPPORTED_VERSION = 70

# `zstd` data will start with a magic number frame
# the value of which is `0xfd2fb528`
ZSTD_MAGIC_NUMBER = b"\x28\xb5\x2f\xfd"

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

segment_header = reader.read(4)
size = struct.unpack("<I", segment_header)[0]
data = io.BytesIO(zlib.decompress(reader.read(size), wbits=-15))
schema = KiwiSchema(data)
compressedSchema = reader.read(size)

schemaData = b""
# Check to see if the first four bytes are the zstd magic number.
# If so we need to use zstd to decompress, not zlib
if compressedSchema.startswith(ZSTD_MAGIC_NUMBER):
schemaData = io.BytesIO(zstd.decompress(compressedSchema))
else:
schemaData = io.BytesIO(zlib.decompress(compressedSchema, wbits=-15))

schema = KiwiSchema(schemaData)

segment_header = reader.read(4)
size = struct.unpack("<I", segment_header)[0]
data = io.BytesIO(zlib.decompress(reader.read(size), wbits=-15))
compressedData = reader.read(size)

data = b""
# Check to see if the first four bytes are the zstd magic number.
# If so we need to use zstd to decompress, not zlib

if compressedData.startswith(ZSTD_MAGIC_NUMBER):
data = io.BytesIO(zstd.decompress(compressedData))
else:
data = io.BytesIO(zlib.decompress(compressedData, wbits=-15))

return KiwiDecoder(schema, type_converters).decode(data, "Message")

0 comments on commit 4336fda

Please sign in to comment.