From 58c6729df40c303e429986e312472c1999c3dfbd Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Mon, 7 Jul 2025 10:52:20 +0200 Subject: [PATCH] feat: generate human readable table cell IDs in almost all real-world cases these IDs require less memory than the binary IDs used before, and they are also require less storage in PDF files, since binary data is encoded in hex escape sequences, taking up 4 bytes per byte of data. --- Cargo.lock | 5 +++-- crates/typst-pdf/Cargo.toml | 1 + crates/typst-pdf/src/tags/table.rs | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a0daeb6b..96ae7eec0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1367,7 +1367,7 @@ dependencies = [ [[package]] name = "krilla" version = "0.4.0" -source = "git+https://github.com/saecki/krilla?branch=tag-attributes#a13326781d3959896a468b79dd80741bd3ab2398" +source = "git+https://github.com/saecki/krilla?branch=tag-attributes#2556c404d19746f9385b4a4e26e910d9e625c1db" dependencies = [ "base64", "bumpalo", @@ -1396,7 +1396,7 @@ dependencies = [ [[package]] name = "krilla-svg" version = "0.1.0" -source = "git+https://github.com/saecki/krilla?branch=tag-attributes#a13326781d3959896a468b79dd80741bd3ab2398" +source = "git+https://github.com/saecki/krilla?branch=tag-attributes#2556c404d19746f9385b4a4e26e910d9e625c1db" dependencies = [ "flate2", "fontdb", @@ -3139,6 +3139,7 @@ dependencies = [ "krilla", "krilla-svg", "serde", + "smallvec", "typst-assets", "typst-library", "typst-macros", diff --git a/crates/typst-pdf/Cargo.toml b/crates/typst-pdf/Cargo.toml index 5745d0530..b96a28029 100644 --- a/crates/typst-pdf/Cargo.toml +++ b/crates/typst-pdf/Cargo.toml @@ -27,6 +27,7 @@ infer = { workspace = true } krilla = { workspace = true } krilla-svg = { workspace = true } serde = { workspace = true } +smallvec = { workspace = true } [lints] workspace = true diff --git a/crates/typst-pdf/src/tags/table.rs b/crates/typst-pdf/src/tags/table.rs index eb13a1391..c27f95a12 100644 --- a/crates/typst-pdf/src/tags/table.rs +++ b/crates/typst-pdf/src/tags/table.rs @@ -1,8 +1,10 @@ +use std::io::Write as _; use std::num::{NonZeroU32, NonZeroUsize}; use krilla::tagging::{ TableCellSpan, TableDataCell, TableHeaderCell, TagBuilder, TagId, TagIdRefs, TagKind, }; +use smallvec::SmallVec; use typst_library::foundations::{Packed, Smart, StyleChain}; use typst_library::model::{TableCell, TableCellKind, TableElem, TableHeaderScope}; @@ -313,11 +315,9 @@ fn should_group_rows(a: TableCellKind, b: TableCellKind) -> bool { } fn table_cell_id(table_id: TableId, x: u32, y: u32) -> TagId { - let mut bytes = [0; 12]; - bytes[0..4].copy_from_slice(&table_id.0.to_ne_bytes()); - bytes[4..8].copy_from_slice(&x.to_ne_bytes()); - bytes[8..12].copy_from_slice(&y.to_ne_bytes()); - TagId::from_slice(&bytes) + let mut buf = SmallVec::new(); + _ = write!(&mut buf, "{}x{x}y{y}", table_id.0); + TagId::from_smallvec(buf) } fn table_header_scope(scope: TableHeaderScope) -> krilla::tagging::TableHeaderScope {