Merge main into #1594

This commit is contained in:
bflatastic committed 2026-10-30 08:25:58 +07:00
commit 619d642eb1
37 files changed
+204 -41

No files matched your search

+10 -2
View File
@@ -53,7 +53,7 @@ use std::collections::BTreeMap;
use std::io::Read;
use std::sync::Arc;
use figcraft_doc::Document;
use figcraft_doc::{Document, FileKind};
pub use export::export;
@@ -83,6 +83,9 @@ pub(crate) fn layer_budget(packed: usize) -> usize {
(packed / PACKED_BYTES_PER_LAYER).clamp(crate::rest::MAX_NODES, MAX_FIG_NODES)
}
/// The canvas header of a FigJam whiteboard (a `.jam`); a `.fig` has `fig-kiwi`.
pub const JAM_HEADER: &[u8] = b"fig-jam.";
/// Whether `bytes` look like a `.fig` file: a zip holding `canvas.fig`, or a bare canvas.
pub fn is_fig(bytes: &[u8]) -> bool {
bytes.starts_with(b"fig-") || (bytes.starts_with(b"PK") && zip_has_canvas(bytes))
@@ -104,6 +107,8 @@ pub fn import(bytes: &[u8], name: &str) -> Result<(Document, Vec<String>), Forma
// The budgets follow the compressed canvas, which the file itself bounds (its zip member may
// inflate too).
let packed = archive.canvas.len().min(bytes.len());
// FigJam writes its whiteboards with this header: they open as boards.
let board = archive.canvas.starts_with(JAM_HEADER);
// Each stage's input is dropped as soon as the next one is built: in a large file every one
// of them takes gigabytes, and holding them all at once runs out of memory.
let (schema, message) = decode_canvas(&std::mem::take(&mut archive.canvas))?;
@@ -117,7 +122,10 @@ pub fn import(bytes: &[u8], name: &str) -> Result<(Document, Vec<String>), Forma
let embedded = embedded_copies(&msg);
drop(msg);
let images = |r: &str| archive.image(r).or_else(|| embedded.get(r).map(|b| b.to_vec()));
let (doc, more) = crate::rest::import_with_networks(&json, &images, networks, max_nodes)?;
let (mut doc, more) = crate::rest::import_with_networks(&json, &images, networks, max_nodes)?;
if board {
doc.kind = FileKind::Board;
}
// Both stages stop at the same cap and say so in the same words: one note (#981).
for w in more.into_iter().filter_map(map::adapt_warning) {
let base = w.rsplit_once(" (×").map_or(w.as_str(), |(b, _)| b);
+37 -1
View File
@@ -4,7 +4,12 @@ use super::*;
/// The synthetic FigJam schema adds generated layout and text alignment records.
fn board_schema() -> Schema {
Schema::parse(&SCHEMA.replace(
board_schema_of(SCHEMA)
}
/// [`board_schema`] over another base schema.
fn board_schema_of(base: &str) -> Schema {
Schema::parse(&base.replace(
"\nmessage Blob ",
", GeneratedData nodeGenerationData, GeneratedData derivedImmutableFrameData, string textAlignHorizontal, string textAlignVertical\nmessage GeneratedData NodeChange[] overrides, uint version\nmessage Blob ",
))
@@ -97,6 +102,7 @@ fn imported(layer: Kv<'static>) -> (Document, Vec<String>) {
fn generated_stickies_and_shapes_keep_their_background_text_and_author() {
for ty in ["STICKY", "SHAPE_WITH_TEXT"] {
let (doc, warnings) = imported(board_node(ty));
assert_eq!(doc.kind, figcraft_doc::FileKind::Design, "the same layers in a .fig stay a design file");
let frame = named(&doc, "Board object");
assert!(matches!(frame.kind, NodeKind::Frame));
assert_eq!(frame.opacity, 0.5);
@@ -417,3 +423,33 @@ fn outer_sticky_body_survives_alongside_the_generated_author() {
}
}
}
/// A FigJam `.jam` file (#1342) is the same archive as a `.fig` with a `fig-jam.` header on its
/// canvas (synthetic here, never a real file): it imports the same way, as a board, notes and
/// shapes with text as their frames, a table as its stand-in, and a layer type the importer doesn't know is
/// left out with a note instead of failing the file.
#[test]
fn a_jam_file_imports_like_a_fig() {
let schema = board_schema_of(&SCHEMA.replace("CONNECTOR TABLE", "CONNECTOR TABLE SYNTHETIC_JAM_ONLY"));
let layers = vec![
node(0, None, "DOCUMENT", "Synthetic board", vec![]),
node(1, Some((0, "!")), "CANVAS", "Page", vec![]),
board_node("STICKY"),
node(3, Some((1, "\"")), "TABLE", "Synthetic table", vec![("size", xy(200.0, 100.0)), ("transform", at(400.0, 0.0))]),
node(4, Some((1, "#")), "SYNTHETIC_JAM_ONLY", "Unknown object", vec![("size", xy(50.0, 50.0)), ("transform", at(0.0, 400.0))]),
];
let msg = o(vec![("nodeChanges", arr(layers)), ("blobs", arr(vec![o(vec![("bytes", Kv::Bytes(triangle_blob()))])]))]);
let mut jam = canvas(&schema, &msg);
jam.splice(..8, *b"fig-jam.");
for bytes in [jam.clone(), zip_file(&jam)] {
assert!(is_fig(&bytes), "read as the same archive");
let (doc, warnings) = import(&bytes, "Synthetic board").unwrap();
assert_eq!(doc.kind, figcraft_doc::FileKind::Board, "a FigJam whiteboard opens as a board");
let note = named(&doc, "Board object");
assert!(matches!(note.kind, NodeKind::Frame) && note.children.len() == 3, "the sticky with its background, note and author");
assert!(matches!(named(&doc, "Synthetic table").kind, NodeKind::Frame));
assert!(doc.nodes().all(|n| n.name != "Unknown object"), "left out");
assert!(warnings.iter().any(|w| w.contains("table layers import as")), "{warnings:?}");
assert!(warnings.iter().any(|w| w.contains("SYNTHETIC_JAM_ONLY")), "a note says what was left out: {warnings:?}");
}
}
+2 -1
View File
@@ -82,7 +82,8 @@ impl FileKind {
let ext = path.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default();
match ext.as_str() {
"figcraft" => FileKind::FigCraft,
"fig" => FileKind::Fig,
// A FigJam `.jam` is the same archive.
"fig" | "jam" => FileKind::Fig,
"sketch" => FileKind::Sketch,
"svg" => FileKind::Svg,
"png" | "jpg" | "jpeg" | "webp" | "gif" | "bmp" | "tif" | "tiff" => FileKind::Image,