From 5f3995acb1c72a1f5ff5eda6cb8842625b7f5128 Mon Sep 17 00:00:00 2001 From: Seb-sti1 <65665540+seb-sti1@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:46:38 +0100 Subject: [PATCH] feat: add paper_size in SceneInfo Closes #39 --- README.md | 4 ++++ src/rmscene/scene_stream.py | 7 ++++++- src/rmscene/tagged_block_reader.py | 7 +++++++ src/rmscene/tagged_block_writer.py | 6 ++++++ tests/test_scene_stream.py | 6 ++++-- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92fbb19..57de571 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ To convert rm files to other formats, you can use [rmc](https://github.com/rickl ### Unreleased +New feature: + +- Add support for `paper_size` field on some SceneInfo + ### v0.6.1 Fixes: diff --git a/src/rmscene/scene_stream.py b/src/rmscene/scene_stream.py index 16e15b2..7915ff6 100644 --- a/src/rmscene/scene_stream.py +++ b/src/rmscene/scene_stream.py @@ -143,21 +143,26 @@ def version_info(self, _) -> tuple[int, int]: current_layer: LwwValue[CrdtId] background_visible: LwwValue[bool] root_document_visible: LwwValue[bool] + paper_size: tp.Optional[tuple[int, int]] @classmethod def from_stream(cls, stream: TaggedBlockReader) -> SceneInfo: current_layer = stream.read_lww_id(1) background_visible = stream.read_lww_bool(2) root_document_visible = stream.read_lww_bool(3) + paper_size = stream.read_int_pair(5) if stream.bytes_remaining_in_block() > 0 else None return SceneInfo(current_layer=current_layer, background_visible=background_visible, - root_document_visible=root_document_visible) + root_document_visible=root_document_visible, + paper_size=paper_size) def to_stream(self, writer: TaggedBlockWriter): writer.write_lww_id(1, self.current_layer) writer.write_lww_bool(2, self.background_visible) writer.write_lww_bool(3, self.root_document_visible) + if self.paper_size: + writer.write_int_pair(5, self.paper_size) @dataclass diff --git a/src/rmscene/tagged_block_reader.py b/src/rmscene/tagged_block_reader.py index c7931af..a26dbf4 100644 --- a/src/rmscene/tagged_block_reader.py +++ b/src/rmscene/tagged_block_reader.py @@ -347,3 +347,10 @@ def read_string_with_format(self, index: int) -> tuple[str, tp.Optional[int]]: fmt = None return string, fmt + + def read_int_pair(self, index: int) -> tp.Optional[tuple[int, int]]: + """Read a sub block containing two uint32""" + with self.read_subblock(index): + first = self.data.read_uint32() + second = self.data.read_uint32() + return first, second diff --git a/src/rmscene/tagged_block_writer.py b/src/rmscene/tagged_block_writer.py index d110788..c078e90 100644 --- a/src/rmscene/tagged_block_writer.py +++ b/src/rmscene/tagged_block_writer.py @@ -186,3 +186,9 @@ def write_string_with_format(self, index: int, text: str, fmt: int): self.data.write_bool(is_ascii) self.data.write_bytes(b) self.write_int(2, fmt) + + def write_int_pair(self, index: int, value: tuple[int, int]): + """Read a sub block containing two uint32""" + with self.write_subblock(index): + self.data.write_uint32(value[0]) + self.data.write_uint32(value[1]) diff --git a/tests/test_scene_stream.py b/tests/test_scene_stream.py index e1b9b58..95381cc 100644 --- a/tests/test_scene_stream.py +++ b/tests/test_scene_stream.py @@ -280,11 +280,11 @@ def test_write_blocks(): def test_blocks_keep_unknown_data_in_main_block(): - # The "E1 FF" is represents new, unknown data -- note that this might need + # The "E1 FF" represents new, unknown data -- note that this might need # to be changed in future if the next id starts to actually be used in a # future update! data_hex = """ - 21000000 0000010D + 2E000000 0000010D 1C 06000000 1F 0000 2F 0000 @@ -292,6 +292,8 @@ def test_blocks_keep_unknown_data_in_main_block(): 1F 0000 21 01 3C 05000000 1F 0000 21 01 + 5C 08000000 + 7C050000 50070000 E1 FF """ buf = BytesIO(HEADER_V6 + bytes.fromhex(data_hex))