M6: .fig import and export through craft-fig and craft-figma-rest

The kiwi codec, the .fig container, the canvas chunks, the vector network and
geometry blobs and the mapping of a .fig message onto the REST file JSON now
come from craft-libs (craft-fig, craft-figma-rest, at a4b57d47). FigCraft keeps
its REST importer, its export mapping and a small rest::Geometry over its own
board routing and text measure.

Deleted: fig/kiwi.rs, fig/network.rs, fig/text_path.rs, fig/map/** and the
archive and canvas code in fig.rs. Warnings and style_name come from
craft-figma-rest, the _fig* field names from craft_fig::rest.

The routing note is now worded 'are routed afresh'; the catalogues keep their
translations under the new key. The catalogue test reads craft-fig's mapping
notes as well.
This commit is contained in:
Dylan M. Taylor committed 2026-11-12 05:43:07 +00:00
1 parent f2135dda4f
commit d1bc300802
34 files changed
+225 -5988

No files matched your search

Generated
+42
View File
@@ -1011,6 +1011,31 @@ name = "craft-docpath"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545#e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545"
[[package]]
name = "craft-fig"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26#e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26"
dependencies = [
"craft-figma-rest",
"craft-kiwi",
"craft-zip",
"flate2",
"kurbo",
"ruzstd",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-figma-rest"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26#e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26"
dependencies = [
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-font-catalog"
version = "0.1.0"
@@ -1076,6 +1101,11 @@ dependencies = [
"serde_json",
]
[[package]]
name = "craft-kiwi"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26#e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26"
[[package]]
name = "craft-menu"
version = "0.1.0"
@@ -1186,6 +1216,16 @@ dependencies = [
"wayland-client",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26#e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip 9.1.1",
]
[[package]]
name = "crc32fast"
version = "1.5.3"
@@ -1935,6 +1975,8 @@ dependencies = [
name = "figcraft-format"
version = "0.18.1"
dependencies = [
"craft-fig",
"craft-figma-rest",
"figcraft-doc",
"figcraft-layout",
"figcraft-pathops",
+6
View File
@@ -56,6 +56,12 @@ craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9ae
# Where the settings live: the suite's folder rules, portable mode included (craft-libs).
craft-settings = { git = "https://github.com/storytold/craft-libs", rev = "e11f9d9aebea5f6b8e74643c1ea1ed172ba5f545" }
# The design-file formats (craft-libs): the .fig container and its kiwi encoding, the mapping of
# a .fig message onto the REST file JSON, and that JSON's shared helpers. These crates share
# types, so they stay on one rev; they share none with the crates above.
craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26" }
craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "e9d2af9ecdba63ced233e16fe4e3cfa5b3e5bf26" }
# foundation
kurbo = { version = "0.13", features = ["serde"] }
thiserror = "2"
+2
View File
@@ -20,3 +20,5 @@ ruzstd = { workspace = true }
sha1 = { workspace = true }
sha2 = { workspace = true }
image = { workspace = true }
craft-fig = { workspace = true }
craft-figma-rest = { workspace = true }
+19 -276
View File
@@ -26,13 +26,11 @@ mod export;
mod export_tests;
#[cfg(test)]
mod interop_tests;
pub mod kiwi;
#[cfg(test)]
mod layer_cap_tests;
mod map;
#[cfg(test)]
mod missing_image_tests;
mod network;
#[cfg(test)]
mod paste_style_tests;
#[cfg(test)]
@@ -47,7 +45,6 @@ mod source_identity_tests;
mod tests;
#[cfg(test)]
mod text_paragraph_tests;
mod text_path;
#[cfg(test)]
mod text_path_tests;
#[cfg(test)]
@@ -61,27 +58,22 @@ mod value_budget_tests;
#[cfg(test)]
mod vector_scale_tests;
use std::cell::Cell;
use std::collections::BTreeMap;
use std::io::Read;
use std::sync::Arc;
use figcraft_doc::{Document, FileKind};
pub use export::export;
use crate::FormatError;
use kiwi::{Kv, Schema};
#[cfg(test)]
use craft_fig::{MAX_CANVAS, chunks, network};
use craft_fig::{Archive, Limits, decode_canvas, embedded_aliases, embedded_copies, kiwi};
pub(crate) use craft_fig::rest::layer_budget;
#[cfg(test)]
use craft_fig::rest::MAX_FIG_NODES;
pub use craft_fig::{JAM_HEADER, is_fig};
pub(crate) use craft_figma_rest::style_name;
/// Largest `canvas.fig` member read, and largest decompressed chunk.
const MAX_CANVAS: u64 = 256 * 1024 * 1024;
const MAX_CHUNK: u64 = 512 * 1024 * 1024;
/// Largest decompressed schema chunk: Figma's schema unpacks to well under 1 MB, so a schema that
/// inflates past this is refused before its definitions are decoded.
const MAX_SCHEMA: u64 = 16 * 1024 * 1024;
/// Longest image hash (bytes) an image is found by: `images/` names and embedded images both
/// stop at 64 bytes (128 hex digits).
pub(crate) const MAX_IMAGE_HASH: usize = 64;
/// Largest image member read.
const MAX_IMAGE: u64 = figcraft_doc::ImageData::MAX_BYTES;
/// Total decompressed ZIP image bytes read per import: the native image budget (`IMAGE_BUDGET`) on
@@ -91,35 +83,6 @@ const MAX_IMAGE: u64 = figcraft_doc::ImageData::MAX_BYTES;
const MAX_IMAGE_BYTES: u64 = crate::IMAGE_BUDGET.bytes;
#[cfg(target_pointer_width = "32")]
const MAX_IMAGE_BYTES: u64 = 1024 * 1024 * 1024;
/// Most chunks read from `canvas.fig`.
const MAX_CHUNKS: usize = 16;
/// Most layers imported from a `.fig` file: one per [`PACKED_BYTES_PER_LAYER`] bytes of its
/// compressed canvas, at least the REST import's cap and at most [`MAX_FIG_NODES`]. Instances
/// expand into a layer per layer of their component, and chunks inflate up to [`MAX_CHUNK`], so the
/// budget follows what the file takes, not what it unpacks to: a small file can't claim a large
/// count, however it's built; a large one gets room. Design systems are mostly instances, which
/// pack tightly: a 7.8 MB canvas stopped at 244 248 layers at 32 bytes a layer (#981). At 8, files
/// under about 1.6 MB keep the floor and the ceiling comes at a 16 MB canvas (about 11 GB and 40 s
/// to import 2 million synthetic layers; memory grows with the count, about 5.4 KB a layer).
const PACKED_BYTES_PER_LAYER: usize = 8;
const MAX_FIG_NODES: usize = 2_000_000;
/// Most layers imported from a `.fig` file whose compressed canvas is `packed` bytes.
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))
}
fn zip_has_canvas(bytes: &[u8]) -> bool {
zip::ZipArchive::new(std::io::Cursor::new(bytes)).is_ok_and(|z| z.file_names().any(|n| n == "canvas.fig"))
}
/// Import `bytes` when they are a `.fig` file; `None` when they are something else.
pub fn try_import(bytes: &[u8], name: &str) -> Option<Result<(Document, Vec<String>), FormatError>> {
@@ -133,24 +96,23 @@ pub fn import(bytes: &[u8], name: &str) -> Result<(Document, Vec<String>), Forma
}
fn import_with_image_limit(bytes: &[u8], name: &str, image_limit: u64) -> Result<(Document, Vec<String>), FormatError> {
let mut archive = Archive::open(bytes)?;
archive.image_budget.remaining.set(image_limit);
let mut archive = Archive::open_with(bytes, Limits { max_image: MAX_IMAGE, max_image_total: image_limit }).map_err(fig_err)?;
// 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);
let board = archive.is_board();
// 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))?;
let (schema, message) = decode_canvas(&std::mem::take(&mut archive.canvas)).map_err(fig_err)?;
let root = schema.find("Message").ok_or_else(|| corrupt("the schema has no Message definition"))?;
let msg = schema.decode_message_within(root, &message, kiwi::value_budget(packed)).map_err(|e| corrupt(e.to_string()))?;
let msg = schema.decode_message_within(root, &message, craft_fig::value_budget(packed)).map_err(|e| corrupt(e.to_string()))?;
let max_nodes = layer_budget(packed);
drop(message);
let file_name = archive.file_name.as_deref().filter(|n| !n.trim().is_empty()).unwrap_or(name);
let (mut json, mut warnings, networks) = map::to_rest_for(&msg, file_name, max_nodes, board);
// The message goes before the layers are built, so the images it carries are copied out first.
let embedded = embedded_copies(&msg);
let embedded = embedded_copies(&msg, MAX_IMAGE);
drop(msg);
let aliases = embedded_aliases(&embedded, |hash| archive.has_image(hash));
if !aliases.is_empty() {
@@ -158,7 +120,7 @@ fn import_with_image_limit(bytes: &[u8], name: &str, image_limit: u64) -> Result
}
let images = |r: &str| archive.image(r).or_else(|| embedded.get(r).map(|b| b.to_vec()));
let (mut doc, more) = crate::rest::import_with_networks(&json, &images, networks, max_nodes)?;
let skipped = archive.image_budget.skipped.get();
let skipped = archive.images_skipped();
if skipped > 0 {
warnings.push(format!("{skipped} ZIP image(s) skipped by the total ZIP image byte budget ({image_limit} bytes)"));
}
@@ -175,87 +137,16 @@ fn import_with_image_limit(bytes: &[u8], name: &str, image_limit: u64) -> Result
Ok((doc, warnings))
}
/// A font style under the name the format uses: the standard weight names as words ("SemiBold"
/// → "Semi Bold", "ExtraBoldItalic" → "Extra Bold Italic", "Regular Italic" → "Italic"). Other
/// names stay as they are.
pub(crate) fn style_name(style: &str) -> String {
const WEIGHTS: [&str; 12] =
["Thin", "Extra Light", "Ultra Light", "Light", "Regular", "Medium", "Semi Bold", "Demi Bold", "Bold", "Extra Bold", "Ultra Bold", "Black"];
let compact = |s: &str| s.chars().filter(|c| !matches!(c, ' ' | '-' | '_')).flat_map(char::to_lowercase).collect::<String>();
let key = compact(style);
let (base, italic) = match key.strip_suffix("italic") {
Some(b) => (b, true),
None => (key.as_str(), false),
};
let weight = if base.is_empty() && italic { Some("Regular") } else { WEIGHTS.into_iter().find(|w| compact(w) == base) };
match (weight, italic) {
(None, _) => style.to_string(),
(Some("Regular"), true) => "Italic".into(),
(Some(w), true) => format!("{w} Italic"),
(Some(w), false) => w.into(),
}
}
fn corrupt(msg: impl Into<String>) -> FormatError {
FormatError::Corrupt(format!(".fig file: {}", msg.into()))
}
/// Image bytes that travel inside the message, by image hash (lowercase hex): a paint's
/// `image.dataBlob` indexes the message's `blobs`. Copies written by plugins and other tools embed
/// their images this way, and a bare canvas (what the clipboard carries) has no `images/`. The
/// walk is iterative (no recursion on nested data) and skips blobs larger than [`MAX_IMAGE`].
/// Returns the index of each hash's blob.
fn embedded_images(msg: &Kv) -> BTreeMap<String, usize> {
let blobs = msg.get("blobs").map(Kv::list).unwrap_or_default();
let mut found = BTreeMap::new();
let mut pending = vec![msg];
while let Some(value) = pending.pop() {
match value {
Kv::Object(fields) => {
if let Some((hash, index)) = value.get("image").and_then(|image| embedded_image(image, blobs)) {
found.entry(hash).or_insert(index);
}
pending.extend(fields.iter().filter(|(name, _)| *name != "blobs").map(|(_, field)| field));
}
Kv::Array(items) => pending.extend(items),
_ => {}
}
/// A `.fig` read or write failure as the app reports it (`Corrupt` already says `.fig file:`).
pub(crate) fn fig_err(e: craft_fig::Error) -> FormatError {
match e {
craft_fig::Error::Corrupt(_) => FormatError::Corrupt(e.to_string()),
craft_fig::Error::TooLarge | craft_fig::Error::Io(_) => FormatError::Io(e.to_string()),
}
found
}
/// The images that travel inside the message ([`embedded_images`]), copied out of it once per
/// blob: every hash naming the same blob shares its copy. A file can point any number of hashes
/// at one large blob, and a copy for each would multiply it (#1059).
fn embedded_copies(msg: &Kv) -> BTreeMap<String, Arc<[u8]>> {
let blobs = msg.get("blobs").map(Kv::list).unwrap_or_default();
let mut copies: BTreeMap<usize, Arc<[u8]>> = BTreeMap::new();
let mut out = BTreeMap::new();
for (hash, index) in embedded_images(msg) {
let Some(bytes) = blob(blobs, index) else { continue };
out.insert(hash, Arc::clone(copies.entry(index).or_insert_with(|| Arc::from(bytes))));
}
out
}
/// Embedded images ([`embedded_copies`]) whose blob an earlier hash already names, each with that
/// hash. Hashes that also have an `images/` member keep their own name: the member is read first.
/// Pointing every alias at one name has the import copy and hash each blob once, however many
/// hashes a file points at it.
fn embedded_aliases(embedded: &BTreeMap<String, Arc<[u8]>>, in_zip: impl Fn(&str) -> bool) -> BTreeMap<String, String> {
let mut first: BTreeMap<usize, &String> = BTreeMap::new();
let mut out = BTreeMap::new();
for (hash, bytes) in embedded {
if in_zip(hash) {
continue;
}
// Hashes naming one blob share its copy, so the copy's address tells the blob.
let canonical = *first.entry(bytes.as_ptr() as usize).or_insert(hash);
if canonical != hash {
out.insert(hash.clone(), canonical.clone());
}
}
out
}
/// Rename every `imageRef` in REST `json` found in `aliases` to the name it maps to.
@@ -282,154 +173,6 @@ fn canonical_image_refs(json: &mut serde_json::Value, aliases: &BTreeMap<String,
}
}
/// The hash and blob index of an `Image` value whose `dataBlob` points at a usable blob.
fn embedded_image(image: &Kv, blobs: &[Kv]) -> Option<(String, usize)> {
let hash = image.get("hash").and_then(Kv::bytes).filter(|h| !h.is_empty() && h.len() <= MAX_IMAGE_HASH)?;
let Some(Kv::Uint(index)) = image.get("dataBlob") else { return None };
let index = usize::try_from(*index).ok()?;
let bytes = blob(blobs, index)?;
if bytes.is_empty() || bytes.len() as u64 > MAX_IMAGE {
return None;
}
Some((hash.iter().map(|b| format!("{b:02x}")).collect(), index))
}
/// The bytes of blob `index`.
fn blob<'k>(blobs: &'k [Kv], index: usize) -> Option<&'k [u8]> {
blobs.get(index)?.get("bytes").and_then(Kv::bytes)
}
/// Bytes left for ZIP image reads, including damaged images. The REST resolver caches references,
/// including failures, so each referenced member is read or counted as skipped only once.
struct ZipImageBudget {
remaining: Cell<u64>,
skipped: Cell<usize>,
}
/// The parts of a `.fig` archive the import reads.
struct Archive<'b> {
canvas: Vec<u8>,
file_name: Option<String>,
/// `images/<hash>` members (or nothing for a bare canvas).
zip: Option<zip::ZipArchive<std::io::Cursor<&'b [u8]>>>,
image_budget: ZipImageBudget,
}
impl<'b> Archive<'b> {
fn open(bytes: &'b [u8]) -> Result<Self, FormatError> {
let image_budget = ZipImageBudget { remaining: Cell::new(MAX_IMAGE_BYTES), skipped: Cell::new(0) };
if bytes.starts_with(b"fig-") {
return Ok(Archive { canvas: bytes.to_vec(), file_name: None, zip: None, image_budget });
}
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| corrupt(e.to_string()))?;
let canvas = crate::read_member(&mut z, "canvas.fig", MAX_CANVAS)?;
let file_name = crate::read_member(&mut z, "meta.json", 1 << 20)
.ok()
.and_then(|m| serde_json::from_slice::<serde_json::Value>(&m).ok())
.and_then(|m| m.get("file_name").and_then(serde_json::Value::as_str).map(str::to_string));
Ok(Archive { canvas, file_name, zip: Some(z), image_budget })
}
/// Whether `images/` has a member for image `hash`.
fn has_image(&self, hash: &str) -> bool {
self.zip.as_ref().is_some_and(|z| z.index_for_name(&format!("images/{hash}")).is_some())
}
/// Bytes of image `hash` (lowercase hex) from `images/`.
fn image(&self, hash: &str) -> Option<Vec<u8>> {
let budget = &self.image_budget;
if hash.is_empty() || hash.len() > 2 * MAX_IMAGE_HASH || !hash.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
// A clone shares the parsed directory and the bytes; the resolver only has shared access.
let mut z = self.zip.clone()?;
let f = z.by_name(&format!("images/{hash}")).ok()?;
let size = f.size();
if size > MAX_IMAGE {
return None;
}
let remaining = budget.remaining.get();
if size > remaining {
budget.skipped.set(budget.skipped.get() + 1);
return None;
}
let checksum = f.crc32();
let mut data = Vec::new();
// Probe past the per-image cap only when the total budget has room. Even a dishonest
// size cannot make the read exceed the remaining total; failed reads keep their charge.
let limit = remaining.min(MAX_IMAGE + 1);
let read = f.take(limit).read_to_end(&mut data);
let used = data.len() as u64;
budget.remaining.set(remaining - used);
if used > size && used == remaining {
budget.skipped.set(budget.skipped.get() + 1);
}
if read.is_err() || used != size {
return None;
}
// `take` can stop exactly at the budget without asking ZIP for EOF (where it checks CRC).
// Check it here too, without spending an extra byte on an overflow probe.
if used == limit {
let mut crc = flate2::Crc::new();
crc.update(&data);
if crc.sum() != checksum {
return None;
}
}
Some(data)
}
}
/// The schema and the raw message of a `canvas.fig`.
fn decode_canvas(canvas: &[u8]) -> Result<(Schema, Vec<u8>), FormatError> {
let chunks = chunks(canvas)?;
let mut it = chunks.into_iter();
let (Some(schema), Some(message)) = (it.next(), it.next()) else {
return Err(corrupt("canvas.fig needs a schema and a message chunk"));
};
let schema = Schema::decode(&inflate_within(schema, MAX_SCHEMA)?).map_err(|e| corrupt(e.to_string()))?;
Ok((schema, inflate(message)?))
}
/// The length-prefixed chunks after the 12-byte header.
fn chunks(canvas: &[u8]) -> Result<Vec<&[u8]>, FormatError> {
if canvas.len() < 12 || !canvas.starts_with(b"fig-") {
return Err(corrupt("canvas.fig has no header"));
}
let mut out = Vec::new();
let mut pos = 12usize;
while pos < canvas.len() && out.len() < MAX_CHUNKS {
let len = canvas.get(pos..pos + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes).ok_or_else(|| corrupt("truncated chunk"))?;
let start = pos + 4;
let end = start.checked_add(len as usize).filter(|e| *e <= canvas.len()).ok_or_else(|| corrupt("truncated chunk"))?;
out.push(canvas.get(start..end).unwrap_or_default());
pos = end;
}
Ok(out)
}
/// Decompress a chunk: zstd (by its magic number), else raw deflate, else zlib.
fn inflate(data: &[u8]) -> Result<Vec<u8>, FormatError> {
inflate_within(data, MAX_CHUNK)
}
/// [`inflate`] refusing a chunk that unpacks to more than `max` bytes.
fn inflate_within(data: &[u8], max: u64) -> Result<Vec<u8>, FormatError> {
let capped = |r: &mut dyn Read| -> Option<Vec<u8>> {
let mut out = Vec::new();
r.take(max.saturating_add(1)).read_to_end(&mut out).ok()?;
(out.len() as u64 <= max).then_some(out)
};
if data.starts_with(&[0x28, 0xB5, 0x2F, 0xFD]) {
let mut src = data;
let mut dec = ruzstd::decoding::StreamingDecoder::new(&mut src).map_err(|e| corrupt(format!("zstd: {e}")))?;
return capped(&mut dec).ok_or_else(|| corrupt("damaged or oversized zstd chunk"));
}
capped(&mut flate2::read::DeflateDecoder::new(data))
.or_else(|| capped(&mut flate2::read::ZlibDecoder::new(data)))
.ok_or_else(|| corrupt("damaged or oversized deflate chunk"))
}
/// Decoded message `msg` as JSON (byte arrays as their length), for looking at files.
#[cfg(test)]
pub(crate) fn debug_json(v: &kiwi::Kv) -> serde_json::Value {
@@ -249,7 +249,7 @@ fn a_routed_connector_takes_its_line_style_from_the_generated_record() {
assert_eq!(connector["strokeWeight"], 4.0);
assert_eq!(connector["strokeDashes"], serde_json::json!([16.0, 10.0]));
assert_eq!(connector["strokeCapEnd"], "ARROW");
assert!(warnings.iter().any(|warning| warning.contains("follow FigCraft's own routing")), "{warnings:?}");
assert!(warnings.iter().any(|warning| warning.contains("are routed afresh")), "{warnings:?}");
let (native, _) = crate::rest::import_fig(&rest, &|_| None).unwrap();
let node = find_node(&native, "synthetic connector");
+27 -80
View File
@@ -15,7 +15,6 @@
//! (`docs/file-format-parity.md` says what was checked); other apps may not open every file.
use std::collections::BTreeMap;
use std::io::Write;
use std::sync::Arc;
use figcraft_doc::color::{BlendMode, Color, GradientKind, ImageScaleMode, Paint, PaintKind};
@@ -33,8 +32,8 @@ use figcraft_doc::{
use serde_json::Value;
use sha1::Digest;
use super::kiwi::{Kv, Schema, encode};
use super::network;
use craft_fig::kiwi::{Kv, Schema, encode};
use craft_fig::{network, path};
use crate::FormatError;
mod assets;
@@ -50,18 +49,18 @@ mod shape;
const MAX_NODES: usize = crate::rest::MAX_NODES;
const MAX_DEPTH: usize = 256;
/// Largest `canvas.fig` and image written (what the import reads).
const MAX_CANVAS: u64 = super::MAX_CANVAS;
const MAX_CANVAS: u64 = craft_fig::MAX_CANVAS;
const MAX_IMAGE: u64 = super::MAX_IMAGE;
/// Largest whole file written.
const MAX_FILE: u64 = 2 * 1024 * 1024 * 1024;
/// Most characters of one text layer, and vertices + segments + loop entries of one vector
/// network (what the import reads).
const MAX_CHARS: usize = 1 << 20;
const MAX_NETWORK: usize = 200_000;
/// Largest magnitude written for a float (the format stores 32-bit floats).
const MAX_FLOAT: f64 = 1e30;
/// The version number in the `canvas.fig` header.
const VERSION: u32 = 101;
/// Grid track values, and the scale factor of a scaled instance, that the import follows.
const MAX_GRID_TRACK_VALUE: f64 = 1e6;
const MIN_SCALE_FACTOR: f64 = 1e-3;
const MAX_SCALE_FACTOR: f64 = 1e3;
/// Session ids of the GUIDs: layers, and grid tracks and component properties.
const LAYERS: u32 = 1;
const AUX: u32 = 2;
@@ -199,11 +198,11 @@ message Message MessageType type=1, uint sessionID=2, NodeChange[] nodeChanges=4
pub fn export(doc: &Document, thumbnail: Option<&[u8]>) -> Result<(Vec<u8>, Vec<String>), FormatError> {
let mut x = Exporter::new(doc);
let message = x.message();
let fail = |e: super::kiwi::KiwiError| FormatError::Io(format!(".fig export: {e}"));
let fail = |e: craft_fig::kiwi::KiwiError| FormatError::Io(format!(".fig export: {e}"));
let schema = Schema::parse(SCHEMA).map_err(fail)?;
let root = schema.find("Message").ok_or_else(|| FormatError::Io(".fig export: the schema has no Message".into()))?;
let body = encode::message(&schema, root, &message).map_err(fail)?;
let canvas = canvas(&encode::schema(&schema), &body)?;
let canvas = craft_fig::encode_canvas(&encode::schema(&schema), &body).map_err(super::fig_err)?;
let background = doc.pages.first().and_then(|p| doc.node(*p)).and_then(|p| match &p.kind {
NodeKind::Page { background, .. } => Some(*background),
_ => None,
@@ -212,28 +211,6 @@ pub fn export(doc: &Document, thumbnail: Option<&[u8]>) -> Result<(Vec<u8>, Vec<
Ok((bytes, x.warnings))
}
fn too_large() -> FormatError {
FormatError::Io("the document is too large for a .fig file".into())
}
/// `canvas.fig`: the magic, the version and the two deflated chunks, each after its length.
pub(super) fn canvas(schema: &[u8], message: &[u8]) -> Result<Vec<u8>, FormatError> {
let mut out = b"fig-kiwi".to_vec();
out.extend_from_slice(&VERSION.to_le_bytes());
for chunk in [schema, message] {
let mut z = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
z.write_all(chunk).map_err(crate::io_err)?;
let c = z.finish().map_err(crate::io_err)?;
let len = u32::try_from(c.len()).map_err(|_| too_large())?;
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(&c);
}
if out.len() as u64 > MAX_CANVAS {
return Err(too_large());
}
Ok(out)
}
/// The zip around `canvas.fig`.
fn archive(
name: &str,
@@ -242,33 +219,9 @@ fn archive(
images: &[(String, Arc<Vec<u8>>)],
thumbnail: Option<&[u8]>,
) -> Result<Vec<u8>, FormatError> {
let mut buf = std::io::Cursor::new(Vec::new());
{
let mut z = zip::ZipWriter::new(&mut buf);
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
crate::put(&mut z, "canvas.fig", stored, canvas)?;
if let Some(t) = thumbnail {
crate::put(&mut z, "thumbnail.png", stored, t)?;
}
let mut client = serde_json::json!({"background_color": {"r": background.r, "g": background.g, "b": background.b, "a": background.a}});
// A PNG says its size right after its signature and header chunk name.
let be = |at: usize| thumbnail.and_then(|t| t.get(at..at + 4)).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes);
if let (Some(w), Some(h)) = (be(16), be(20)) {
client["thumbnail_size"] = serde_json::json!({"width": w, "height": h});
}
let meta = serde_json::to_vec(&serde_json::json!({"client_meta": client, "file_name": name})).map_err(crate::io_err)?;
crate::put(&mut z, "meta.json", crate::json_options(), &meta)?;
let mut total = canvas.len() as u64;
for (hash, bytes) in images {
total += bytes.len() as u64;
if total > MAX_FILE {
return Err(too_large());
}
crate::put(&mut z, &format!("images/{hash}"), stored, bytes)?;
}
z.finish().map_err(crate::io_err)?;
}
Ok(buf.into_inner())
let images: Vec<(&str, &[u8])> = images.iter().map(|(hash, bytes)| (hash.as_str(), bytes.as_slice())).collect();
let background = [background.r, background.g, background.b, background.a].map(f64::from);
craft_fig::write_archive(&craft_fig::Package { file_name: name, background, canvas, images: &images, thumbnail }).map_err(super::fig_err)
}
type Fields = Vec<(&'static str, Kv<'static>)>;
@@ -303,7 +256,7 @@ fn b(x: bool) -> Kv<'static> {
/// importer keeps only those). `None`: the format has no name for it, so an equal track stands in.
fn grid_track_size(track: &GridTrack) -> Option<(&'static str, f64)> {
let value = track.value;
let fits = value.is_finite() && (0.0..=crate::fig::map::MAX_GRID_TRACK_VALUE).contains(&value);
let fits = value.is_finite() && (0.0..=MAX_GRID_TRACK_VALUE).contains(&value);
match track.sizing {
Sizing::Fixed if fits => Some(("FIXED", value)),
Sizing::Fill if fits && value > 0.0 => Some(("FLEX", value)),
@@ -416,27 +369,21 @@ fn join_name(j: StrokeJoin) -> &'static str {
}
}
/// A geometry blob (the import's `svg_path` reads it): commands `0` close, `1` move, `2` line,
/// `3` quadratic, `4` cubic, each followed by its points as little-endian `f32` pairs.
/// A geometry blob (`craft_fig::path`), with every value finite.
fn path_blob(p: &BezPath) -> Vec<u8> {
let mut out = Vec::new();
let mut put = |cmd: u8, pts: &[figcraft_doc::geom::Point]| {
out.push(cmd);
for q in pts {
out.extend_from_slice(&(finite(q.x) as f32).to_le_bytes());
out.extend_from_slice(&(finite(q.y) as f32).to_le_bytes());
}
};
for el in p.elements() {
match *el {
PathEl::MoveTo(a) => put(1, &[a]),
PathEl::LineTo(a) => put(2, &[a]),
PathEl::QuadTo(a, c) => put(3, &[a, c]),
PathEl::CurveTo(a, c, d) => put(4, &[a, c, d]),
PathEl::ClosePath => put(0, &[]),
}
}
out
let pt = |q: figcraft_doc::geom::Point| [finite(q.x) as f32, finite(q.y) as f32];
let commands: Vec<path::Command> = p
.elements()
.iter()
.map(|el| match *el {
PathEl::MoveTo(a) => path::Command::Move(pt(a)),
PathEl::LineTo(a) => path::Command::Line(pt(a)),
PathEl::QuadTo(a, c) => path::Command::Quad(pt(a), pt(c)),
PathEl::CurveTo(a, c, d) => path::Command::Cubic(pt(a), pt(c), pt(d)),
PathEl::ClosePath => path::Command::Close,
})
.collect();
path::encode(&commands)
}
/// Settings an overlay's destination carries.
+1 -1
View File
@@ -1,4 +1,4 @@
use super::super::map::{MAX_SCALE_FACTOR, MIN_SCALE_FACTOR};
use super::{MAX_SCALE_FACTOR, MIN_SCALE_FACTOR};
use super::common::{
bool_data, changed, layout_grid, prop_type, resolved_alike, scaled, sort_position, symbol_data, text_data, text_value, var_data, variant_pairs,
};
-690
View File
@@ -1,690 +0,0 @@
//! A generic decoder and encoder for the kiwi binary schema format (an open, MIT-licensed message
//! format with a public specification). A binary schema lists definitions (enums, structs and messages, each
//! with typed fields); a message is then decoded with it into a [`Kv`] tree keyed by field name,
//! and encoded back from one ([`encode`]).
//!
//! Everything here reads untrusted bytes: reads are bounds-checked, nesting is capped, array
//! counts can't exceed the bytes left, and the total number of decoded values is capped.
use std::collections::HashMap;
use std::fmt;
/// Deepest struct/message nesting decoded.
const MAX_DEPTH: usize = 64;
/// Most values decoded from one message (every scalar, array element and field counts): at least
/// [`MIN_VALUES`], and [`VALUES_PER_PACKED_BYTE`] per byte of the file's compressed canvas up to
/// [`MAX_VALUES`], so a large real file decodes while a small one can't expand into a huge tree,
/// however far its chunks inflate. 32-bit targets (wasm32) have a 4 GB address space in all, so
/// their ceiling is lower: past it the file is refused cleanly instead of running out of memory.
const MIN_VALUES: usize = 20_000_000;
/// Value budget per compressed canvas byte. The real files measured in
/// `large_files_get_a_larger_value_budget` hold up to 0.75 values per compressed byte without their
/// single scalar fields; counting those too adds 15–43% on the generated files in
/// `value_budget_tests` and up to 3.2 times on a rectangle carrying only geometry and paint, so a
/// dense file may reach about 2.4 per byte. Four leaves two-thirds more room than that. The
/// trade-off: from 32 MiB of canvas (8 MiB on 32-bit targets) a file gets all of [`MAX_VALUES`],
/// and above the floor it may decode four values for every byte it carries; [`MIN_VALUES`] and
/// [`MAX_VALUES`] still bound every file.
const VALUES_PER_PACKED_BYTE: usize = 4;
#[cfg(not(target_pointer_width = "32"))]
const MAX_VALUES: usize = 128 << 20;
#[cfg(target_pointer_width = "32")]
const MAX_VALUES: usize = 32 << 20;
/// Most definitions in a schema, and fields in one definition.
const MAX_DEFS: usize = 10_000;
const MAX_FIELDS: usize = 10_000;
/// Most fields in a schema, every definition together: Figma's own schema has a few thousand, and
/// without a total a schema of [`MAX_DEFS`] wide definitions could hold 100 million field records.
const MAX_SCHEMA_FIELDS: usize = 200_000;
#[derive(Debug, PartialEq, Eq)]
pub struct KiwiError(pub String);
impl fmt::Display for KiwiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
fn err<T>(msg: impl Into<String>) -> Result<T, KiwiError> {
Err(KiwiError(msg.into()))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
Enum,
Struct,
Message,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Type {
Bool,
Byte,
Int,
Uint,
Float,
Str,
Int64,
Uint64,
/// Index of a definition in the schema.
Def(usize),
}
/// Built-in types in their schema order (encoded as `!index`).
const BUILTINS: [Type; 8] = [Type::Bool, Type::Byte, Type::Int, Type::Uint, Type::Float, Type::Str, Type::Int64, Type::Uint64];
#[derive(Clone, Debug)]
pub struct Field {
pub name: String,
/// `None` for enum values.
pub ty: Option<Type>,
pub array: bool,
/// The field id (messages) or the value (enums).
pub value: u32,
}
#[derive(Clone, Debug)]
pub struct Def {
pub name: String,
pub kind: Kind,
pub fields: Vec<Field>,
}
#[derive(Clone, Debug, Default)]
pub struct Schema {
pub defs: Vec<Def>,
}
/// A decoded value. Field and enum names borrow from the schema.
#[derive(Clone, Debug, PartialEq)]
pub enum Kv<'s> {
Bool(bool),
Int(i64),
Uint(u64),
Float(f32),
Str(String),
/// A `byte[]` field.
Bytes(Vec<u8>),
Enum(&'s str),
Array(Vec<Kv<'s>>),
/// A struct or message: its fields in the order read.
Object(Vec<(&'s str, Kv<'s>)>),
}
impl<'s> Kv<'s> {
/// Field `name` of an object.
pub fn get(&self, name: &str) -> Option<&Kv<'s>> {
match self {
Kv::Object(f) => f.iter().find(|(n, _)| *n == name).map(|(_, v)| v),
_ => None,
}
}
pub fn f64(&self) -> Option<f64> {
let v = match self {
Kv::Float(f) => f64::from(*f),
Kv::Int(i) => *i as f64,
Kv::Uint(u) => *u as f64,
Kv::Bool(b) => f64::from(u8::from(*b)),
_ => return None,
};
v.is_finite().then_some(v)
}
pub fn bool(&self) -> Option<bool> {
match self {
Kv::Bool(b) => Some(*b),
_ => None,
}
}
pub fn str(&self) -> Option<&str> {
match self {
Kv::Str(s) => Some(s),
Kv::Enum(s) => Some(s),
_ => None,
}
}
pub fn list(&self) -> &[Kv<'s>] {
match self {
Kv::Array(a) => a,
_ => &[],
}
}
pub fn bytes(&self) -> Option<&[u8]> {
match self {
Kv::Bytes(b) => Some(b),
_ => None,
}
}
}
/// A bounds-checked reader over kiwi bytes.
struct Reader<'b> {
data: &'b [u8],
pos: usize,
}
impl<'b> Reader<'b> {
fn left(&self) -> usize {
self.data.len().saturating_sub(self.pos)
}
fn byte(&mut self) -> Result<u8, KiwiError> {
let b = *self.data.get(self.pos).ok_or_else(|| KiwiError("unexpected end of data".into()))?;
self.pos += 1;
Ok(b)
}
fn take(&mut self, n: usize) -> Result<&'b [u8], KiwiError> {
let end = self.pos.checked_add(n).filter(|e| *e <= self.data.len()).ok_or_else(|| KiwiError("unexpected end of data".into()))?;
let s = self.data.get(self.pos..end).ok_or_else(|| KiwiError("unexpected end of data".into()))?;
self.pos = end;
Ok(s)
}
fn var_uint(&mut self) -> Result<u32, KiwiError> {
let mut value = 0u32;
let mut shift = 0;
loop {
let b = self.byte()?;
let part = b & 127;
if shift == 28 && (part > 0x0f || b & 128 != 0) {
return err("u32 varint overflow");
}
value |= u32::from(part) << shift;
shift += 7;
if b & 128 == 0 {
return Ok(value);
}
}
}
fn var_int(&mut self) -> Result<i32, KiwiError> {
let v = self.var_uint()?;
Ok(if v & 1 != 0 { !((v >> 1) as i32) } else { (v >> 1) as i32 })
}
fn var_uint64(&mut self) -> Result<u64, KiwiError> {
let mut value = 0u64;
let mut shift = 0u32;
loop {
let b = self.byte()?;
if shift == 56 {
// The ninth byte carries a full eight bits.
return Ok(value | (u64::from(b) << 56));
}
value |= u64::from(b & 127) << shift;
shift += 7;
if b & 128 == 0 {
return Ok(value);
}
}
}
fn var_int64(&mut self) -> Result<i64, KiwiError> {
let v = self.var_uint64()?;
Ok(if v & 1 != 0 { !((v >> 1) as i64) } else { (v >> 1) as i64 })
}
fn var_float(&mut self) -> Result<f32, KiwiError> {
if self.data.get(self.pos) == Some(&0) {
self.pos += 1;
return Ok(0.0);
}
let b = self.take(4)?;
let bits = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
// The exponent is stored first so zero fits one byte; move it back into place.
Ok(f32::from_bits(bits.rotate_left(23)))
}
fn string(&mut self) -> Result<String, KiwiError> {
let rest = self.data.get(self.pos..).unwrap_or_default();
let len = rest.iter().position(|b| *b == 0).ok_or_else(|| KiwiError("unterminated string".into()))?;
let s = String::from_utf8_lossy(self.take(len)?).into_owned();
self.pos += 1;
Ok(s)
}
}
impl Schema {
/// Decode a binary schema.
pub fn decode(data: &[u8]) -> Result<Schema, KiwiError> {
let mut r = Reader { data, pos: 0 };
let count = r.var_uint()? as usize;
if count > MAX_DEFS || count > r.left() {
return err("schema: too many definitions");
}
let mut defs = Vec::with_capacity(count);
let mut raw_types = Vec::new();
let mut total_fields = 0usize;
for _ in 0..count {
let name = r.string()?;
let kind = match r.byte()? {
0 => Kind::Enum,
1 => Kind::Struct,
2 => Kind::Message,
k => return err(format!("schema: unknown definition kind {k}")),
};
let n = r.var_uint()? as usize;
total_fields = total_fields.saturating_add(n);
if n > MAX_FIELDS || n > r.left() || total_fields > MAX_SCHEMA_FIELDS {
return err("schema: too many fields");
}
let mut fields = Vec::with_capacity(n);
for _ in 0..n {
let fname = r.string()?;
let ty = r.var_int()?;
let array = r.byte()? & 1 != 0;
let value = r.var_uint()?;
if kind == Kind::Message && fields.iter().any(|f: &Field| f.value == value) {
return err(format!("schema: duplicate field id {value} in {name}"));
}
raw_types.push(ty);
fields.push(Field { name: fname, ty: None, array, value });
}
defs.push(Def { name, kind, fields });
}
let total = defs.len();
let mut types = raw_types.into_iter();
for d in &mut defs {
for f in &mut d.fields {
let raw = types.next().unwrap_or(0);
if d.kind == Kind::Enum {
continue;
}
f.ty = Some(if raw < 0 {
*BUILTINS.get((!raw) as usize).ok_or_else(|| KiwiError(format!("schema: unknown type {raw}")))?
} else {
let i = raw as usize;
if i >= total {
return err(format!("schema: field {} names missing definition {i}", f.name));
}
Type::Def(i)
});
}
}
Ok(Schema { defs })
}
pub fn find(&self, name: &str) -> Option<usize> {
self.defs.iter().position(|d| d.name == name)
}
/// Decode a message of definition `root`, within [`MIN_VALUES`] values.
pub fn decode_message<'s>(&'s self, root: usize, data: &[u8]) -> Result<Kv<'s>, KiwiError> {
self.decode_message_within(root, data, MIN_VALUES)
}
/// [`Self::decode_message`] within `budget` values (see [`value_budget`]).
pub fn decode_message_within<'s>(&'s self, root: usize, data: &[u8], budget: usize) -> Result<Kv<'s>, KiwiError> {
let mut d = Decoder { schema: self, r: Reader { data, pos: 0 }, budget, ids: vec![None; self.defs.len()] };
d.def(root, 0)
}
}
/// Most values decoded from a file whose compressed canvas is `packed` bytes.
pub(crate) fn value_budget(packed: usize) -> usize {
packed.saturating_mul(VALUES_PER_PACKED_BYTE).clamp(MIN_VALUES, MAX_VALUES)
}
struct Decoder<'s, 'b> {
schema: &'s Schema,
r: Reader<'b>,
budget: usize,
/// Per definition, the index of the first field with each id (message field ids, enum
/// values), built the first time the definition is decoded: a message can carry many tags
/// against a wide definition, and a scan per tag would multiply the two.
ids: Vec<Option<HashMap<u32, usize>>>,
}
impl<'s> Decoder<'s, '_> {
fn spend(&mut self, n: usize) -> Result<(), KiwiError> {
self.budget = self.budget.checked_sub(n).ok_or_else(|| KiwiError("message: too many values".into()))?;
Ok(())
}
/// The first field of definition `i` with id (or enum value) `id`.
fn field_by_id(&mut self, i: usize, id: u32) -> Option<&'s Field> {
let fields = &self.schema.defs.get(i)?.fields;
let ids = self.ids.get_mut(i)?.get_or_insert_with(|| {
let mut m = HashMap::with_capacity(fields.len());
for (j, f) in fields.iter().enumerate() {
m.entry(f.value).or_insert(j);
}
m
});
fields.get(*ids.get(&id)?)
}
fn def(&mut self, i: usize, depth: usize) -> Result<Kv<'s>, KiwiError> {
if depth > MAX_DEPTH {
return err("message: nested too deeply");
}
self.spend(1)?;
let schema = self.schema;
let d = schema.defs.get(i).ok_or_else(|| KiwiError("message: missing definition".into()))?;
match d.kind {
Kind::Enum => {
let v = self.r.var_uint()?;
Ok(self.field_by_id(i, v).map_or(Kv::Uint(u64::from(v)), |f| Kv::Enum(f.name.as_str())))
}
Kind::Struct => {
let mut out = Vec::with_capacity(d.fields.len());
for f in &d.fields {
out.push((f.name.as_str(), self.field(f, depth)?));
}
Ok(Kv::Object(out))
}
Kind::Message => {
let mut out = Vec::new();
loop {
let id = self.r.var_uint()?;
if id == 0 {
// Grown by doubling: trimmed, a large file's tree takes a good part less.
out.shrink_to_fit();
return Ok(Kv::Object(out));
}
let f = self.field_by_id(i, id).ok_or_else(|| KiwiError(format!("message: {} has no field {id}", d.name)))?;
out.push((f.name.as_str(), self.field(f, depth)?));
}
}
}
}
fn field(&mut self, f: &Field, depth: usize) -> Result<Kv<'s>, KiwiError> {
let ty = f.ty.ok_or_else(|| KiwiError("message: untyped field".into()))?;
if !f.array {
// A nested definition spends its own value in `def`.
if !matches!(ty, Type::Def(_)) {
self.spend(1)?;
}
return self.value(ty, depth);
}
let n = self.r.var_uint()? as usize;
// Every element takes at least one byte.
if n > self.r.left() {
return err("message: array longer than the data");
}
// The field itself counts, so empty arrays can't pile up for free.
self.spend(1)?;
if ty == Type::Byte {
return Ok(Kv::Bytes(self.r.take(n)?.to_vec()));
}
self.spend(n)?;
let mut out = Vec::with_capacity(n);
for _ in 0..n {
out.push(self.value(ty, depth)?);
}
Ok(Kv::Array(out))
}
fn value(&mut self, ty: Type, depth: usize) -> Result<Kv<'s>, KiwiError> {
Ok(match ty {
Type::Bool => Kv::Bool(self.r.byte()? != 0),
Type::Byte => Kv::Uint(u64::from(self.r.byte()?)),
Type::Int => Kv::Int(i64::from(self.r.var_int()?)),
Type::Uint => Kv::Uint(u64::from(self.r.var_uint()?)),
Type::Float => Kv::Float(self.r.var_float()?),
Type::Str => Kv::Str(self.r.string()?),
Type::Int64 => Kv::Int(self.r.var_int64()?),
Type::Uint64 => Kv::Uint(self.r.var_uint64()?),
Type::Def(i) => self.def(i, depth + 1)?,
})
}
}
/// An encoder for the same format: the `.fig` export writes its schema and message with it (and
/// test fixtures are built with it). Values are checked against the schema; it never panics.
pub mod encode {
use super::{BUILTINS, Field, Kind, KiwiError, Kv, MAX_DEPTH, Schema, Type, err};
#[derive(Default)]
pub struct Writer(pub Vec<u8>);
impl Writer {
pub fn byte(&mut self, b: u8) {
self.0.push(b);
}
pub fn var_uint(&mut self, mut v: u32) {
loop {
let b = (v & 127) as u8;
v >>= 7;
if v == 0 {
self.0.push(b);
return;
}
self.0.push(b | 128);
}
}
pub fn var_int(&mut self, v: i32) {
self.var_uint(((v << 1) ^ (v >> 31)) as u32);
}
pub fn var_uint64(&mut self, mut v: u64) {
for _ in 0..8 {
let b = (v & 127) as u8;
v >>= 7;
if v == 0 {
self.0.push(b);
return;
}
self.0.push(b | 128);
}
self.0.push(v as u8);
}
pub fn var_int64(&mut self, v: i64) {
self.var_uint64(((v << 1) ^ (v >> 63)) as u64);
}
pub fn var_float(&mut self, f: f32) {
let bits = f.to_bits().rotate_right(23);
if bits & 255 == 0 {
self.0.push(0);
} else {
self.0.extend_from_slice(&bits.to_le_bytes());
}
}
/// A NUL-terminated string (NULs inside are dropped: they would end it early).
pub fn string(&mut self, s: &str) {
self.0.extend(s.bytes().filter(|b| *b != 0));
self.0.push(0);
}
}
pub fn schema(s: &Schema) -> Vec<u8> {
let mut w = Writer::default();
w.var_uint(s.defs.len() as u32);
for d in &s.defs {
w.string(&d.name);
w.byte(match d.kind {
Kind::Enum => 0,
Kind::Struct => 1,
Kind::Message => 2,
});
w.var_uint(d.fields.len() as u32);
for f in &d.fields {
w.string(&f.name);
w.var_int(match f.ty {
None => 0,
Some(Type::Def(i)) => i32::try_from(i).unwrap_or(i32::MAX),
Some(t) => !(BUILTINS.iter().position(|b| *b == t).unwrap_or(0) as i32),
});
w.byte(u8::from(f.array));
w.var_uint(f.value);
}
}
w.0
}
/// Encode `v` as definition `def` (objects may leave message fields out; struct fields are
/// required, in order). Fails on what doesn't fit the schema: unknown fields or enum names,
/// missing struct fields, wrong types, numbers out of range, non-finite floats.
pub fn message(s: &Schema, def: usize, v: &Kv) -> Result<Vec<u8>, KiwiError> {
let mut w = Writer::default();
encode_def(s, def, v, &mut w, 0)?;
Ok(w.0)
}
fn encode_def(s: &Schema, i: usize, v: &Kv, w: &mut Writer, depth: usize) -> Result<(), KiwiError> {
if depth > MAX_DEPTH {
return err("encode: nested too deeply");
}
let d = s.defs.get(i).ok_or_else(|| KiwiError("encode: missing definition".into()))?;
match d.kind {
Kind::Enum => {
let value = match v {
Kv::Uint(u) => u32::try_from(*u).ok(),
_ => v.str().and_then(|name| d.fields.iter().find(|f| f.name == name)).map(|f| f.value),
};
w.var_uint(value.ok_or_else(|| KiwiError(format!("encode: {v:?} is not a value of {}", d.name)))?);
}
Kind::Struct => {
for f in &d.fields {
let fv = v.get(&f.name).ok_or_else(|| KiwiError(format!("encode: {} needs field {}", d.name, f.name)))?;
encode_field(s, f, fv, w, depth)?;
}
}
Kind::Message => {
let Kv::Object(fields) = v else { return err(format!("encode: {} needs an object", d.name)) };
for (name, fv) in fields {
let f = d.fields.iter().find(|f| f.name == *name).ok_or_else(|| KiwiError(format!("encode: {} has no field {name}", d.name)))?;
w.var_uint(f.value);
encode_field(s, f, fv, w, depth)?;
}
w.var_uint(0);
}
}
Ok(())
}
fn encode_field(s: &Schema, f: &Field, v: &Kv, w: &mut Writer, depth: usize) -> Result<(), KiwiError> {
let ty = f.ty.ok_or_else(|| KiwiError("encode: untyped field".into()))?;
if !f.array {
return encode_value(s, ty, v, w, depth);
}
let len = |n: usize| u32::try_from(n).map_err(|_| KiwiError(format!("encode: {} is too long", f.name)));
if let (Type::Byte, Kv::Bytes(b)) = (ty, v) {
w.var_uint(len(b.len())?);
w.0.extend_from_slice(b);
return Ok(());
}
let Kv::Array(items) = v else { return err(format!("encode: {} needs a list", f.name)) };
w.var_uint(len(items.len())?);
for it in items {
encode_value(s, ty, it, w, depth)?;
}
Ok(())
}
/// An integer value (whole finite floats count).
fn int(v: &Kv) -> Option<i128> {
match v {
Kv::Int(i) => Some(i128::from(*i)),
Kv::Uint(u) => Some(i128::from(*u)),
Kv::Bool(b) => Some(i128::from(*b)),
Kv::Float(f) if f.is_finite() && f.fract() == 0.0 => Some(*f as i128),
_ => None,
}
}
fn encode_value(s: &Schema, ty: Type, v: &Kv, w: &mut Writer, depth: usize) -> Result<(), KiwiError> {
let bad = || KiwiError(format!("encode: {v:?} is not a {ty:?}"));
let ranged = |lo: i128, hi: i128| int(v).filter(|i| (lo..=hi).contains(i)).ok_or_else(bad);
match ty {
Type::Bool => w.byte(u8::from(v.bool().ok_or_else(bad)?)),
Type::Byte => w.byte(ranged(0, 255)? as u8),
Type::Int => w.var_int(ranged(i32::MIN.into(), i32::MAX.into())? as i32),
Type::Uint => w.var_uint(ranged(0, u32::MAX.into())? as u32),
// Infinity is a value of its own (an axis with no maximum size); NaN is not.
Type::Float => w.var_float(match v {
Kv::Float(f) if f.is_infinite() => *f,
_ => v.f64().map(|x| x as f32).filter(|x| x.is_finite()).ok_or_else(bad)?,
}),
Type::Str => w.string(v.str().ok_or_else(bad)?),
Type::Int64 => w.var_int64(ranged(i64::MIN.into(), i64::MAX.into())? as i64),
Type::Uint64 => w.var_uint64(ranged(0, u64::MAX.into())? as u64),
Type::Def(i) => encode_def(s, i, v, w, depth + 1)?,
}
Ok(())
}
}
/// The built-in type names, in [`BUILTINS`] order.
const BUILTIN_NAMES: [&str; 8] = ["bool", "byte", "int", "uint", "float", "string", "int64", "uint64"];
impl Schema {
/// A schema from a compact text form, one definition per line: `enum Name A B …` (values from
/// 0), `struct Name type field, type field[] …` or `message Name …` (field ids from 1), the
/// types being built-in names or other definitions'. `A=3` or `type field=7` gives a value or
/// a field its own number.
pub fn parse(text: &str) -> Result<Schema, KiwiError> {
let lines: Vec<Vec<&str>> = text.lines().map(|l| l.split_whitespace().collect::<Vec<_>>()).filter(|l| !l.is_empty()).collect();
let index = |name: &str| lines.iter().position(|l| l.get(1) == Some(&name));
let mut defs = Vec::with_capacity(lines.len());
for l in &lines {
let (Some(kind), Some(name)) = (l.first(), l.get(1)) else { return err("schema text: a definition needs a kind and a name") };
let rest = l.get(2..).unwrap_or_default();
let (kind, fields) = match *kind {
"enum" => {
let mut fields = Vec::new();
for (n, i) in rest.iter().zip(0..) {
let (name, value) = numbered(n, i)?;
fields.push(Field { name, ty: None, array: false, value });
}
(Kind::Enum, fields)
}
"struct" | "message" => {
let mut fields = Vec::new();
for (f, id) in rest.join(" ").split(',').zip(1..) {
let Some((t, n)) = f.trim().split_once(' ') else { return err(format!("schema text: bad field `{f}` in {name}")) };
let base = t.trim_end_matches("[]");
let ty = match BUILTIN_NAMES.iter().position(|b| *b == base) {
Some(i) => BUILTINS.get(i).copied(),
None => index(base).map(Type::Def),
}
.ok_or_else(|| KiwiError(format!("schema text: unknown type {base}")))?;
let (name, value) = numbered(n.trim(), id)?;
if *kind == "message" && fields.iter().any(|f: &Field| f.value == value) {
return err(format!("schema text: duplicate field id {value} in {name}"));
}
fields.push(Field { name, ty: Some(ty), array: t.ends_with("[]"), value });
}
(if *kind == "struct" { Kind::Struct } else { Kind::Message }, fields)
}
k => return err(format!("schema text: unknown kind {k}")),
};
defs.push(Def { name: name.to_string(), kind, fields });
}
Ok(Schema { defs })
}
}
/// `name=number` as its parts, or `name` with `default`.
fn numbered(token: &str, default: u32) -> Result<(String, u32), KiwiError> {
match token.split_once('=') {
None => Ok((token.to_string(), default)),
Some((name, n)) => Ok((name.to_string(), n.parse().map_err(|_| KiwiError(format!("schema text: bad number in `{token}`")))?)),
}
}
#[cfg(test)]
mod tests {
use super::Reader;
#[test]
fn overflowing_u32_varint_is_rejected() {
let mut boundary = Reader { data: &[0xff, 0xff, 0xff, 0xff, 0x0f], pos: 0 };
assert_eq!(boundary.var_uint(), Ok(u32::MAX));
let mut overflowing = Reader { data: &[0xff, 0xff, 0xff, 0xff, 0x7f], pos: 0 };
assert!(overflowing.var_uint().is_err(), "overflowing u32 varint was accepted");
}
}
+62 -620
View File
@@ -1,105 +1,82 @@
//! Decoded `.fig` message → REST API file JSON, which [`crate::rest`] then imports.
//!
//! The message lists node changes; each names its id (`guid`), its parent and its position among
//! the siblings (`parentIndex`). Nodes are read by field name, so files written with newer
//! schemas still import what is understood. Instances carry only their overrides: their layers
//! are expanded here from the main component (overrides and derived layout matched by id path,
//! component property values applied) so the import looks as saved.
//! Decoded `.fig` message → REST API file JSON, which [`crate::rest`] then imports. The mapping
//! is `craft_fig::rest`; FigCraft gives it its own connector routing and text measure, so an
//! imported connector runs as one drawn here, and takes the networks it hands over.
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::rc::Rc;
use figcraft_doc::VectorNetwork;
use figcraft_doc::geom::{Affine, Point};
use figcraft_doc::proto::{MAX_ACTION_DEPTH, MAX_EXPR_DEPTH};
use serde_json::{Map, Value, json};
use craft_fig::rest::{self, Geometry, RouteEnd, Routing, SavedRuns, Side};
use figcraft_doc::geom::{Point, measure};
use figcraft_doc::{ConnectorPath, VectorNetwork};
use serde_json::Value;
use super::kiwi::Kv;
use super::network;
mod board;
mod component;
mod connector;
mod document;
mod layer;
mod layout;
mod paint;
mod proto;
mod text;
mod variables;
/// FigCraft's board routing and text measure, for the mapping.
struct FigGeometry;
use component::OverrideScope;
#[cfg(test)]
use paint::paint;
fn end(e: RouteEnd) -> figcraft_doc::RouteEnd {
match e {
RouteEnd::Point(p) => figcraft_doc::RouteEnd::Point(p),
RouteEnd::Box(b, s) => figcraft_doc::RouteEnd::Box(b, side(s)),
}
}
type Obj = Map<String, Value>;
fn side(s: Side) -> figcraft_doc::Side {
match s {
Side::Auto => figcraft_doc::Side::Auto,
Side::Top => figcraft_doc::Side::Top,
Side::Right => figcraft_doc::Side::Right,
Side::Bottom => figcraft_doc::Side::Bottom,
Side::Left => figcraft_doc::Side::Left,
}
}
/// Deepest nesting followed.
const MAX_DEPTH: usize = 256;
/// Most path commands read from one geometry blob.
const MAX_COMMANDS: usize = 200_000;
const MAX_GRID_TRACKS: usize = 1_000;
pub(super) const MAX_GRID_TRACK_VALUE: f64 = 1e6;
/// Smallest and largest scale factor of a scaled instance (`symbolData.uniformScaleFactor`, times
/// those of the instances around it) that is followed: a junk factor changes nothing.
pub(super) const MIN_SCALE_FACTOR: f64 = 1e-3;
pub(super) const MAX_SCALE_FACTOR: f64 = 1e3;
/// Id of "no style".
const NO_STYLE: &str = "4294967295:4294967295";
/// Longest asset `key` or `version` kept for resolving references given as assets.
const MAX_ASSET_PART: usize = 256;
fn side_back(s: figcraft_doc::Side) -> Side {
match s {
figcraft_doc::Side::Auto => Side::Auto,
figcraft_doc::Side::Top => Side::Top,
figcraft_doc::Side::Right => Side::Right,
figcraft_doc::Side::Bottom => Side::Bottom,
figcraft_doc::Side::Left => Side::Left,
}
}
impl Geometry for FigGeometry {
fn route(&self, from: RouteEnd, to: RouteEnd, routing: Routing, saved: Option<&SavedRuns>) -> Option<Vec<Point>> {
let routing = match routing {
Routing::Straight => figcraft_doc::Routing::Straight,
Routing::Elbow => figcraft_doc::Routing::Elbow,
Routing::Curved => figcraft_doc::Routing::Curved,
};
let path = saved.map(|s| ConnectorPath::Runs { across: s.across, at: s.at.clone() });
figcraft_doc::route_along(end(from), end(to), routing, path.as_ref())
}
fn attach(&self, from: RouteEnd, to: RouteEnd) -> ((Point, Option<Side>), (Point, Option<Side>)) {
let (a, b) = (end(from), end(to));
let ((p0, p1), (s0, s1)) = (figcraft_doc::attach_points(a, b), figcraft_doc::attached_sides(a, b));
((p0, s0.map(side_back)), (p1, s1.map(side_back)))
}
fn text_path_measurable(&self, d: &str) -> bool {
let path = figcraft_doc::geom::svg_path(d).unwrap_or_default();
measure::polylines(&path).and_then(|lines| lines.first().map(|line| line.length() > 1e-9)).unwrap_or(false)
}
}
/// The REST API file JSON for message `msg`, with the warnings about what was skipped and the
/// vector networks its layers name by `vectorNetworkRef` (for [`crate::rest::import_with_networks`]).
#[cfg(test)]
pub(super) fn to_rest(msg: &Kv, name: &str, max_nodes: usize) -> (Value, Vec<String>, Vec<Option<VectorNetwork>>) {
to_rest_for(msg, name, max_nodes, false)
}
/// The REST JSON (as `to_rest`) for a FigJam whiteboard (`board`) or a design file: a
/// whiteboard's stickies come out as native board items.
/// Map a FigJam whiteboard's stickies as native board items when `board` is true.
pub(super) fn to_rest_for(msg: &Kv, name: &str, max_nodes: usize, board: bool) -> (Value, Vec<String>, Vec<Option<VectorNetwork>>) {
let mut m = Mapper::new(msg, max_nodes);
m.board = board;
let _canon = VarCanon::install(&m);
let doc = m.document();
// Large values are moved in, never passed through `json!`, which serialises its arguments:
// a deep copy of the whole tree, briefly holding it twice.
let mut out = json!({"name": name});
out["document"] = doc;
if !m.library_contexts.is_empty() {
out[crate::rest::FIG_LIBRARY_CONTEXTS_FIELD] = Value::Array(std::mem::take(&mut m.library_contexts));
}
// Mains in the file that the layer cap kept out: their instances are told apart from those of
// components the file doesn't have (#981).
if m.count >= m.max_nodes {
let capped: Vec<Value> = m.referenced_mains.difference(&m.mapped_mains).map(|id| json!(id)).collect();
if !capped.is_empty() {
out[crate::rest::FIG_CAPPED_MAINS_FIELD] = Value::Array(capped);
}
}
if !m.styles.is_empty() {
out["styles"] = Value::Object(std::mem::take(&mut m.styles));
}
if !m.components.is_empty() {
out["components"] = Value::Object(std::mem::take(&mut m.components));
}
if !m.sets.is_empty() {
out["componentSets"] = Value::Object(std::mem::take(&mut m.sets));
}
if let Some(vars) = m.variables() {
out["meta"] = vars;
}
let styles = m.style_definitions();
if !styles.is_empty() {
out["styleDefinitions"] = Value::Array(styles);
}
(out, m.warnings.finish(), m.networks)
let options = rest::Options { max_nodes, geometry: &FigGeometry, outlined_stroke: Some(super::export::STASH), board };
let mapped = rest::to_rest(msg, name, &options);
// Each network passes the importer's own checks, as the JSON's `vectorNetwork` does.
let networks = mapped.networks.into_iter().map(|n| serde_json::to_value(n?).ok().and_then(|v| crate::rest::network(&v))).collect();
(mapped.json, mapped.warnings, networks)
}
#[cfg(test)]
mod image_filter_tests;
/// REST importer warnings reworded for a `.fig` import (`None`: drop it).
pub(super) fn adapt_warning(w: String) -> Option<String> {
if w.contains("geometry=paths") {
@@ -107,538 +84,3 @@ pub(super) fn adapt_warning(w: String) -> Option<String> {
}
Some(w.replace("the variables JSON", "the file").replace("the file JSON", "the file"))
}
// ---- small readers ----
/// A generated child is identified by one exact template GUID, not its array position.
fn generated_record<'a>(v: &View<'a>, field: &str, local_id: u32) -> Option<&'a Kv<'a>> {
v.get(field)?.get("overrides")?.list().iter().find(|row| {
row.get("guidPath").and_then(|path| path.get("guids")).map(Kv::list).is_some_and(|guids| {
guids.len() == 1 && num(&guids[0], "sessionID") == Some(40_000_000.0) && num(&guids[0], "localID") == Some(f64::from(local_id))
})
})
}
fn num(n: &Kv, k: &str) -> Option<f64> {
n.get(k).and_then(Kv::f64)
}
fn guid(v: &Kv) -> Option<String> {
Some(format!("{}:{}", num(v, "sessionID")? as u64, num(v, "localID")? as u64))
}
#[cfg(test)]
mod connector_snapshot_budget_tests;
/// A `GUID`-holding reference (`StyleId`, `VariableID`, …): its `guid`.
fn ref_guid(v: &Kv) -> Option<String> {
v.get("guid").and_then(guid).filter(|g| g != NO_STYLE)
}
fn vec2(v: &Kv) -> Option<Value> {
Some(json!({"x": num(v, "x")?, "y": num(v, "y")?}))
}
fn size(v: &Kv) -> Option<(f64, f64)> {
Some((num(v, "x")?, num(v, "y")?))
}
fn matrix(m: &Kv) -> Option<Affine> {
let c = |k: &str| num(m, k);
let a = Affine::new([c("m00")?, c("m10")?, c("m01")?, c("m11")?, c("m02")?, c("m12")?]);
a.as_coeffs().iter().all(|x| x.is_finite()).then_some(a)
}
fn rows(a: Affine) -> Value {
let [a, b, c, d, e, f] = a.as_coeffs();
json!([[a, c, e], [b, d, f]])
}
fn copy_num(o: &mut Obj, n: &View, from: &str, to: &str) {
if let Some(x) = n.num(from) {
o.insert(to.into(), json!(x));
}
}
/// [`copy_num`] for a length: the layers of a scaled instance give it at the instance's scale.
fn copy_len(o: &mut Obj, n: &View, from: &str, to: &str) {
if let Some(x) = n.len(from) {
o.insert(to.into(), json!(x));
}
}
/// `x` at scale `k`; `None` when the result is not finite.
fn scaled(x: f64, k: f64) -> Option<f64> {
if k == 1.0 {
return Some(x);
}
let r = x * k;
r.is_finite().then_some(r)
}
fn copy_str(o: &mut Obj, n: &View, from: &str, to: &str) {
if let Some(s) = n.str(from) {
o.insert(to.into(), json!(s));
}
}
/// A node seen through override layers: the last layer with a field wins.
#[derive(Clone)]
struct View<'a> {
layers: Vec<&'a Kv<'a>>,
/// The factor each layer's lengths take (`scales[i]` is for `layers[i]`): the layers of a main
/// inside a scaled instance have the instance's, the override and derived rows of the instance
/// 1, being at the instance's scale already (#2091).
scales: Vec<f64>,
/// Size and transform worked out from the constraints, over the layers' own.
fitted: Option<((f64, f64), Affine)>,
}
impl<'a> View<'a> {
fn of(n: &'a Kv<'a>) -> Self {
Self::of_scaled(n, 1.0)
}
/// A node of a main inside an instance scaled by `k`: its lengths read `k` times as long.
fn of_scaled(n: &'a Kv<'a>, k: f64) -> Self {
Self { layers: vec![n], scales: vec![k], fitted: None }
}
/// This view with the layers of `upper` over it (`upper`'s fitted size and place).
fn over(&self, upper: &Self) -> Self {
let mut v = self.clone();
v.layers.extend(upper.layers.iter().copied());
v.scales.extend(upper.scales.iter().copied());
v.fitted = upper.fitted;
v
}
/// The factor of the lowest layer (the layer's own main), for values a style gives.
fn base_scale(&self) -> f64 {
self.scales.first().copied().unwrap_or(1.0)
}
fn size(&self) -> Option<(f64, f64)> {
self.fitted.map(|f| f.0).or_else(|| {
let (v, k) = self.find("size")?;
let (x, y) = size(v)?;
Some((scaled(x, k)?, scaled(y, k)?))
})
}
fn transform(&self) -> Option<Affine> {
self.fitted.map(|f| f.1).or_else(|| {
let (v, k) = self.find("transform")?;
let m = matrix(v)?;
if k == 1.0 {
return Some(m);
}
let [a, b, c, d, e, f] = m.as_coeffs();
let m = Affine::new([a, b, c, d, scaled(e, k)?, scaled(f, k)?]);
Some(m)
})
}
/// This view with `top` over it, each layer with the factor its lengths take.
fn with(&self, top: impl IntoIterator<Item = (&'a Kv<'a>, f64)>) -> Self {
let mut v = self.clone();
for (layer, scale) in top {
v.layers.push(layer);
v.scales.push(scale);
}
v
}
/// The factor of the layer that links a style in `key` (or `legacy`), the lowest layer's when
/// none does: what the linked style's lengths take.
fn link_scale(&self, key: &str, legacy: &str) -> f64 {
self.find(key).or_else(|| self.find(legacy)).map_or_else(|| self.base_scale(), |f| f.1)
}
/// This view with `style` just above the layer that links it in `key` (or `legacy`): the style
/// beats that layer's and lower layers' copies of its fields (stale when the style changed
/// since, or written for the style linked before), and override rows above the link beat it.
fn styled(&self, style: Option<&'a Kv<'a>>, key: &str, legacy: &str) -> Self {
let mut v = self.clone();
if let Some(s) = style {
let linked_in = |k: &str| v.layers.iter().rposition(|l| l.get(k).is_some());
let at = linked_in(key).or_else(|| linked_in(legacy)).map_or(1, |i| i + 1);
let at = at.min(v.layers.len());
// The style is at the scale of the layer that links it.
let k = at.checked_sub(1).and_then(|i| v.scales.get(i)).copied().unwrap_or(1.0);
v.layers.insert(at, s);
v.scales.insert(at, k);
}
v
}
/// An effect/grid list follows the same precedence as text styles: an override above the
/// style link wins, while the style beats stale copies on and below its linking row. A style
/// without the list is empty; it must not expose a stale list from a lower row.
fn styled_list(&self, style: &'a Kv<'a>, list: &str, key: &str, legacy: &str) -> (&'a [Kv<'a>], f64) {
let linked_in = |k: &str| self.layers.iter().rposition(|l| l.get(k).is_some());
let above = linked_in(key).or_else(|| linked_in(legacy)).map_or(1, |i| i + 1);
self.layers
.iter()
.zip(&self.scales)
.skip(above)
.rev()
.find_map(|(row, scale)| row.get(list).map(|v| (v.list(), *scale)))
.unwrap_or_else(|| (style.get(list).map(Kv::list).unwrap_or_default(), self.link_scale(key, legacy)))
}
fn get(&self, k: &str) -> Option<&'a Kv<'a>> {
self.layers.iter().rev().find_map(|l| l.get(k))
}
/// [`Self::get`], and the factor of the layer that has it.
fn find(&self, k: &str) -> Option<(&'a Kv<'a>, f64)> {
self.layers.iter().zip(&self.scales).rev().find_map(|(l, s)| l.get(k).map(|v| (v, *s)))
}
/// A length: the number `k` holds at the scale of the layer that has it.
fn len(&self, k: &str) -> Option<f64> {
let (v, s) = self.find(k)?;
scaled(v.f64()?, s)
}
/// [`Self::list`], and the factor of the layer that has it.
fn list_scaled(&self, k: &str) -> (&'a [Kv<'a>], f64) {
self.find(k).map_or((&[], 1.0), |(v, s)| (v.list(), s))
}
fn num(&self, k: &str) -> Option<f64> {
self.get(k).and_then(Kv::f64)
}
fn str(&self, k: &str) -> Option<&'a str> {
self.get(k).and_then(Kv::str)
}
fn flag(&self, k: &str) -> Option<bool> {
self.get(k).and_then(Kv::bool)
}
fn list(&self, k: &str) -> &'a [Kv<'a>] {
self.get(k).map(Kv::list).unwrap_or_default()
}
}
struct Mapper<'a> {
nodes: BTreeMap<String, &'a Kv<'a>>,
kids: BTreeMap<String, Vec<(&'a str, String)>>,
blobs: Vec<&'a [u8]>,
/// Component property definitions: id → (REST name "Name#id", type).
prop_defs: BTreeMap<String, (String, &'a str)>,
/// The copies of a set's definitions each variant keeps (no name or type of their own; the
/// variant's layers and its instances' assignments use them): copy id → the definition's id.
prop_parents: BTreeMap<String, String>,
/// File nodes by asset `key` and `version`: references can name a collection, variable or
/// style as the asset instead of the guid, and the file carries the resources to resolve
/// them (the collection node itself says the same `key`).
assets: BTreeMap<String, BTreeMap<String, String>>,
styles: Obj,
/// Descriptions of components and component sets (REST `components` / `componentSets`).
components: Obj,
sets: Obj,
/// Layers and keys of each main, built when an instance of it first needs them.
override_scopes: BTreeMap<String, Rc<OverrideScope>>,
/// Entries held by `override_scopes` (bounded by `max_nodes` together).
override_scope_entries: usize,
/// Component-bearing branches of the file's internal canvases (cached library definitions).
library_contexts: Vec<Value>,
/// Layers visited while scanning internal canvases, across all of them.
library_scan_count: usize,
warnings: crate::rest::Warnings,
count: usize,
/// The mains in the file that instances or instance swap defaults name, and the mains mapped
/// as layers: the difference is what the layer cap kept out.
referenced_mains: BTreeSet<String>,
mapped_mains: BTreeSet<String>,
/// Most layers produced or scanned, instance expansion included; the one place the cap comes
/// from (see [`super::layer_budget`]).
max_nodes: usize,
/// Layers kept back for the visible pages' top level while library copies are mapped, so the
/// copies can't take the whole budget and leave the pages empty.
reserved: usize,
/// Vector networks, each built once: the layers that show one (every instance of a main
/// holding it) name it by `vectorNetworkRef`. Kept out of the JSON, where a large file's
/// networks took gigabytes.
networks: Vec<Option<VectorNetwork>>,
/// Index in `networks` by its blob, position scale, style table, point-radius scale and the
/// layer's corner radius (which points without one of their own take).
network_ids: HashMap<(usize, u64, u64, usize, u64, u64), usize>,
/// Region paint elements (paints and gradient stops) read so far: one budget for the whole
/// file, since layers with region paints don't share their network.
region_paint_elements: usize,
/// The file is a FigJam whiteboard: its objects are built as native board items.
board: bool,
}
impl<'a> Mapper<'a> {
fn new(msg: &'a Kv<'a>, max_nodes: usize) -> Self {
let mut nodes = BTreeMap::new();
let mut kids: BTreeMap<String, Vec<(&'a str, String)>> = BTreeMap::new();
let mut prop_defs = BTreeMap::new();
let mut prop_parents = BTreeMap::new();
let changes = msg.get("nodeChanges").map(Kv::list).unwrap_or_default();
// The last change of a GUID wins; a REMOVED change deletes what came before it.
for n in changes {
let Some(id) = n.get("guid").and_then(guid) else { continue };
if n.get("phase").and_then(Kv::str) == Some("REMOVED") {
nodes.remove(&id);
} else {
nodes.insert(id, n);
}
}
for n in changes {
let Some(id) = n.get("guid").and_then(guid) else { continue };
if !nodes.get(&id).is_some_and(|latest| std::ptr::eq(*latest, n)) {
continue;
}
// Variables and their collections are read from the node list (`of_type`), never
// as layers: out of the layer tree, an instance's expansion doesn't walk them again for
// every instance without counting them (#1077).
if let Some(p) = n.get("parentIndex")
&& let Some(parent) = p.get("guid").and_then(guid)
&& !matches!(n.get("type").and_then(Kv::str), Some("VARIABLE" | "VARIABLE_SET"))
{
kids.entry(parent).or_default().push((p.get("position").and_then(Kv::str).unwrap_or(""), id.clone()));
}
for d in n.get("componentPropDefs").map(Kv::list).unwrap_or_default() {
let Some(did) = d.get("id").and_then(guid) else { continue };
if let (Some(name), Some(ty)) = (d.get("name").and_then(Kv::str), d.get("type").and_then(Kv::str)) {
prop_defs.insert(did.clone(), (format!("{name}#{did}"), ty));
} else if let Some(parent) = d.get("parentPropDefId").and_then(guid) {
prop_parents.insert(did, parent);
}
}
}
for list in kids.values_mut() {
list.sort();
list.dedup_by(|a, b| a.1 == b.1);
}
let blobs =
msg.get("blobs").map(Kv::list).unwrap_or_default().iter().map(|b| b.get("bytes").and_then(Kv::bytes).unwrap_or_default()).collect();
let mut assets: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
for n in msg.get("nodeChanges").map(Kv::list).unwrap_or_default() {
let (Some(key), Some(id)) = (n.get("key").and_then(Kv::str), n.get("guid").and_then(guid)) else { continue };
if key.is_empty() || key.len() > MAX_ASSET_PART {
continue;
}
let version = n.get("version").and_then(Kv::str).filter(|v| !v.is_empty() && v.len() <= MAX_ASSET_PART).unwrap_or("");
assets.entry(key.to_string()).or_default().entry(version.to_string()).or_insert(id);
}
Self {
nodes,
kids,
blobs,
prop_defs,
prop_parents,
assets,
styles: Obj::new(),
components: Obj::new(),
sets: Obj::new(),
override_scopes: BTreeMap::new(),
override_scope_entries: 0,
library_contexts: Vec::new(),
library_scan_count: 0,
warnings: crate::rest::Warnings::default(),
count: 0,
referenced_mains: BTreeSet::new(),
mapped_mains: BTreeSet::new(),
max_nodes,
reserved: 0,
networks: Vec::new(),
board: false,
network_ids: HashMap::new(),
region_paint_elements: 0,
}
}
/// The id of the resource a `VariableSetID` (or `VariableID`, `StyleId`) names: its `guid`,
/// else the file node its asset `key` and `version` names (a collection from a library is
/// referenced as the asset, and the file's own node for it says the same `key`). `None` when
/// the reference has neither or the asset names nothing (or ambiguously) in the file.
/// Original code for this importer: the field names are those of the schema each file embeds.
fn resource_id(&self, v: &Kv) -> Option<String> {
// A guid names the resource when this file holds a node by it. One from another file (the
// library a style or collection came from, as a paste carries) names nothing here: the
// asset it also names finds the file's copy (#1638).
if let Some(g) = v.get("guid").and_then(guid).filter(|g| *g != NO_STYLE && self.nodes.contains_key(g)) {
return Some(g);
}
let asset = v.get("assetRef")?;
let key = asset.get("key").and_then(Kv::str)?;
let versions = self.assets.get(key)?;
let version = asset.get("version").and_then(Kv::str);
match version {
Some(v) => versions.get(v),
// Without a version: an unversioned record, else the file's only one.
None => versions.get("").or_else(|| (versions.len() == 1).then(|| versions.values().next()).flatten()),
}
.cloned()
}
/// The collection a `VariableSetID` names, as one collection per library asset: a file that
/// took several updates of a library holds a copy of the collection for each version it met,
/// all with the same asset `key`. Variables and explicit modes may name any of those copies,
/// while the mode ids stay the same across them, so every copy maps to the newest one (the
/// one [`Self::variables`] keeps, with the modes of the others added). Without this, a page set
/// to a mode an older copy didn't have yet lost its mode, and the layers fell back to the
/// default mode of the newest copy. A reference by asset that names no copy by itself (no
/// version, or a version the file doesn't hold, while it holds several) still names the asset:
/// its newest live copy.
pub(super) fn collection_id(&self, v: &Kv) -> Option<String> {
let by_asset = || v.get("assetRef")?.get("key").and_then(Kv::str).and_then(VarCanon::newest_set);
match self.resource_id(v) {
Some(id) if self.nodes.contains_key(&id) => Some(self.canonical_collection(&id).unwrap_or(id)),
// The asset index can name a node a later change removed: the asset's live copy instead.
Some(id) => by_asset().or(Some(id)),
None => by_asset(),
}
}
/// The newest live copy of collection node `id`'s library asset, when it has a key.
pub(super) fn canonical_collection(&self, id: &str) -> Option<String> {
let key = self.nodes.get(id)?.get("key").and_then(Kv::str).filter(|k| !k.is_empty())?;
VarCanon::newest_set(key)
}
/// A note about what this file's mapping left out or changed. Counted, as the REST importer
/// counts its own: these notes are met once per layer, so the kinds were named once each with
/// no sign of how much of the file they covered -- a file whose every text layer lost a
/// setting read the same as one where a single layer did (#467). The collector keeps the
/// first-seen order and the same cap, since some notes name fields of the file (image
/// filters).
fn warn(&mut self, w: impl Into<String>) {
self.warnings.add(w.into());
}
fn children(&self, id: &str) -> Vec<String> {
self.kids.get(id).map(|k| k.iter().map(|(_, c)| c.clone()).collect()).unwrap_or_default()
}
/// How many layers may be produced now: the cap, less what is kept for the visible pages.
fn budget(&self) -> usize {
self.max_nodes.saturating_sub(self.reserved)
}
}
/// A library version (`"40727:1183"`) as numbers to order by; unparsable parts rank lowest.
fn version_rank(v: &str) -> (u64, u64) {
let mut parts = v.split(':').map(|p| p.trim().parse::<u64>().unwrap_or(0));
(parts.next().unwrap_or(0), parts.next().unwrap_or(0))
}
/// Library variables, one per asset. A file that took several updates of a library keeps a copy
/// of a variable for each version it met (same `key`, another `version`), and layers and other
/// variables name them by that asset (`assetRef {key, version}`) rather than by guid. Each
/// reference goes to the newest copy: it has the values of every mode the library has now, where
/// an older one lacks the modes added since (a layer under a page set to such a mode fell back to
/// the default one). Kept per thread while a file is mapped: the paint and variable readers that
/// meet references are free functions.
#[derive(Default)]
pub(super) struct VarCanon {
by_key: HashMap<String, String>,
by_id: HashMap<String, String>,
/// Every live variable node of the file, with a key or not.
local: BTreeSet<String>,
/// The newest live copy of each library collection, by asset key. Read from the file's live
/// nodes (the last change of each guid, REMOVED ones gone), not from the asset index, which
/// keeps the first node it met for a key and version even when a later change removed it.
sets_by_key: HashMap<String, String>,
}
thread_local! {
static VAR_CANON: std::cell::RefCell<Option<VarCanon>> = const { std::cell::RefCell::new(None) };
}
/// Removes the thread's table when the mapping is done.
pub(super) struct VarCanonGuard;
impl Drop for VarCanonGuard {
/// Clears the thread's table, so a later file on this thread starts without it.
fn drop(&mut self) {
VAR_CANON.with(|c| c.borrow_mut().take());
}
}
impl VarCanon {
/// Builds the table from mapper `m`'s live nodes (newest copy per asset key, for variables and
/// for collections) and installs it for this thread until the returned guard is dropped.
fn install(m: &Mapper) -> VarCanonGuard {
let mut newest: HashMap<String, ((u64, u64), String)> = HashMap::new();
let mut copies: Vec<(String, String)> = Vec::new();
let mut local = BTreeSet::new();
let mut sets: HashMap<String, ((u64, u64), String)> = HashMap::new();
for (id, n) in &m.nodes {
let ty = n.get("type").and_then(Kv::str);
if !matches!(ty, Some("VARIABLE" | "VARIABLE_SET")) || n.get("isSoftDeleted").and_then(Kv::bool) == Some(true) {
continue;
}
if ty == Some("VARIABLE_SET") {
if let Some(key) = n.get("key").and_then(Kv::str).filter(|k| !k.is_empty()) {
let rank = version_rank(n.get("version").and_then(Kv::str).unwrap_or(""));
let e = sets.entry(key.to_string()).or_insert_with(|| (rank, id.clone()));
if rank > e.0 || (rank == e.0 && *id > e.1) {
*e = (rank, id.clone());
}
}
continue;
}
local.insert(id.clone());
let Some(key) = n.get("key").and_then(Kv::str).filter(|k| !k.is_empty()) else { continue };
let rank = version_rank(n.get("version").and_then(Kv::str).unwrap_or(""));
copies.push((id.clone(), key.to_string()));
let e = newest.entry(key.to_string()).or_insert_with(|| (rank, id.clone()));
if rank > e.0 || (rank == e.0 && *id > e.1) {
*e = (rank, id.clone());
}
}
let mut t = VarCanon::default();
for (id, key) in copies {
if let Some((_, canon)) = newest.get(&key) {
t.by_id.insert(id, canon.clone());
}
}
t.by_key = newest.into_iter().map(|(k, (_, id))| (k, id)).collect();
t.local = local;
t.sets_by_key = sets.into_iter().map(|(k, (_, id))| (k, id)).collect();
VAR_CANON.with(|c| *c.borrow_mut() = Some(t));
VarCanonGuard
}
/// The variable a reference (`{guid}` or `{assetRef}`) names, as its newest copy. A guid
/// counts when this file holds a variable by it; one from another file (a paste carries the
/// library's own guids) names nothing here, so the asset it also names is tried before it is
/// kept as it is.
pub(super) fn resolve(r: &Kv) -> Option<String> {
let by_guid = r.get("guid").and_then(guid).filter(|g| g != NO_STYLE);
VAR_CANON.with(|c| {
let c = c.borrow();
let Some(t) = c.as_ref() else { return by_guid.clone() };
if let Some(g) = by_guid.as_ref().filter(|g| t.local.contains(*g)) {
return Some(t.by_id.get(g).cloned().unwrap_or_else(|| g.clone()));
}
let by_asset = r.get("assetRef").and_then(|a| a.get("key")).and_then(Kv::str).and_then(|k| t.by_key.get(k)).cloned();
by_asset.or(by_guid)
})
}
/// The newest live copy of the library collection with asset key `key`.
pub(super) fn newest_set(key: &str) -> Option<String> {
VAR_CANON.with(|c| c.borrow().as_ref().and_then(|t| t.sets_by_key.get(key).cloned()))
}
/// Whether a table is installed on this thread (for tests: none outlives the mapping).
#[cfg(test)]
pub(crate) fn installed() -> bool {
VAR_CANON.with(|c| c.borrow().is_some())
}
/// Whether variable node `id` is an older copy another one replaces.
pub(super) fn superseded(id: &str) -> bool {
VAR_CANON.with(|c| c.borrow().as_ref().and_then(|t| t.by_id.get(id)).is_some_and(|canon| canon != id))
}
}
-88
View File
@@ -1,88 +0,0 @@
//! FigJam whiteboard objects as native board items: what a `.jam` import builds in place of the
//! generated frames a `.fig` keeps. The REST JSON carries them as a `board` field
//! ([`crate::rest`] reads it).
use super::layer::{own_text, valid_size};
use super::paint::paint;
use super::{Affine, Kv, MAX_DEPTH, Mapper, Obj, Value, View, generated_record, json, num};
impl<'a> Mapper<'a> {
/// A sticky as a native board sticky: its frame (size, place, opacity and shadows as saved,
/// the saved colour) holding the note and, when saved, the author line. `None` (nothing
/// changed) unless every generated record is complete: then the caller keeps the generated
/// layers.
pub(super) fn native_sticky(&mut self, o: &mut Obj, v: &View<'a>, rest_id: &str, depth: usize) -> Option<Vec<Value>> {
if v.get("nodeGenerationData").is_none() || v.get("derivedImmutableFrameData").and_then(|d| num(d, "version")) != Some(1.0) {
return None;
}
let record = |id: u32| {
let (style, bounds) = (generated_record(v, "nodeGenerationData", id)?, generated_record(v, "derivedImmutableFrameData", id)?);
let view = View::of(style).with([(bounds, 1.0)]);
(valid_size(view.get("size")).is_some() && view.transform().is_some()).then_some(view)
};
let background = record(0)?;
let fills = background.list("fillPaints");
let solid = matches!(fills, [p] if p.get("type").and_then(Kv::str) == Some("SOLID") && p.get("visible").and_then(Kv::bool) != Some(false));
if !solid || !background.list("strokePaints").is_empty() {
return None;
}
let note = record(1).filter(|n| own_text(n).is_some())?;
let author = record(2).filter(|a| a.flag("visible") != Some(false) && own_text(a).is_some());
let wanted = 1 + usize::from(author.is_some());
if depth >= MAX_DEPTH || self.count.saturating_add(wanted) > self.budget() {
return None;
}
let (w, h) = v.size().or_else(|| o.get("size").and_then(|s| Some((s.get("x")?.as_f64()?, s.get("y")?.as_f64()?))))?;
let at = note.transform().unwrap_or_default().as_coeffs();
let pad = |x: f64| if x.is_finite() { x.clamp(0.0, (w.min(h) / 2.0).max(0.0)) } else { 0.0 };
let (pad_x, pad_y) = (pad(at[4]), pad(at[5]));
// Everything is valid: build.
let paints: Vec<Value> = fills.iter().filter_map(|p| paint(p, &mut |m| self.warn(m))).collect();
o.insert("fills".into(), Value::Array(paints));
o.insert("strokes".into(), json!([]));
o.insert("clipsContent".into(), json!(false));
match background.len("cornerRadius") {
Some(r) => o.insert("cornerRadius".into(), json!(r)),
None => o.remove("cornerRadius"),
};
for (k, val) in [
("layoutMode", json!("VERTICAL")),
("primaryAxisAlignItems", json!("SPACE_BETWEEN")),
("counterAxisAlignItems", json!("MIN")),
("primaryAxisSizingMode", json!("AUTO")),
("counterAxisSizingMode", json!("FIXED")),
("minHeight", json!(h)),
("itemSpacing", json!(8.0)),
("paddingTop", json!(pad_y)),
("paddingBottom", json!(pad_y)),
("paddingLeft", json!(pad_x)),
("paddingRight", json!(pad_x)),
] {
o.insert(k.into(), val);
}
let mut kids = Vec::new();
let mut written = String::new();
for (local, view) in [(1, Some(note)), (2, author)] {
let Some(view) = view else { continue };
let chars = own_text(&view).unwrap_or_default();
let mut layer = Obj::new();
layer.insert("id".into(), json!(format!("{rest_id};generated{local}")));
layer.insert("type".into(), json!("TEXT"));
layer.insert("name".into(), json!(if local == 1 { chars.chars().take(40).collect::<String>() } else { "Author".to_string() }));
self.common(&mut layer, &view, "TEXT", Affine::IDENTITY, None);
self.text(&mut layer, &view);
if let Some(Value::Object(style)) = layer.get_mut("style") {
style.insert("textAutoResize".into(), json!("HEIGHT"));
style.insert("textAlignHorizontal".into(), json!("LEFT"));
}
layer.insert("layoutSizingHorizontal".into(), json!("FILL"));
if local == 2 {
written = chars.trim().to_string();
}
self.count += 1;
kids.push(Value::Object(layer));
}
o.insert("board".into(), json!({"type": "sticky", "author": written}));
Some(kids)
}
}
-506
View File
@@ -1,506 +0,0 @@
use super::layer::own_text;
use super::text::char_styles;
use super::{BTreeMap, BTreeSet, Kv, MAX_DEPTH, MAX_SCALE_FACTOR, MIN_SCALE_FACTOR, Mapper, Obj, Rc, Value, View, guid, json};
/// An override row names a layer of the main by its current GUID or by the `overrideKey` the
/// layer had when the row was written: the key becomes the current GUID, unless the layer is
/// current already or the key is ambiguous.
fn normalize_override_segment(path: &mut [String], index: usize, scope: &OverrideScope) {
let Some(segment) = path.get(index) else { return };
if scope.current.contains(segment) {
return;
}
if let Some(Some(current)) = scope.aliases.get(segment) {
path[index] = current.clone();
}
}
/// Overrides and derived layout by id path.
type Overrides<'a> = BTreeMap<Vec<String>, Vec<OverrideRow<'a>>>;
/// Where an override entry comes from.
#[derive(Clone, Copy, PartialEq)]
pub(super) enum Entry {
Override,
Derived,
/// Derived layout of a nested instance for a size it no longer has: its size and place no
/// longer hold, the rest (resolved colours, text) still does.
Stale,
}
/// One override or derived-layout row addressed to a layer.
#[derive(Clone)]
pub(super) struct OverrideRow<'a> {
pub(super) row: &'a Kv<'a>,
pub(super) entry: Entry,
/// How many mains are being expanded around the instance that wrote the row: the rows of
/// the enclosing (outer) instances win over those of the inner ones.
source_depth: usize,
/// How many levels below the writing instance the row restates (`overrideLevel`; 0 for the
/// instance's own rows).
level: usize,
/// Position among the rows of the instance that wrote it; later rows win.
ordinal: usize,
/// The factor the row's lengths take: 1 for the rows of an instance in the document, the
/// factor of the layer of the enclosing main that holds the rows for a nested instance's own
/// (they are written in that main's space, #2091).
pub(super) scale: f64,
}
/// The nested level an override row restates: its `overrideLevel`, capped; 0 when it has none.
fn override_level(row: &Kv) -> usize {
row.get("overrideLevel").and_then(Kv::f64).filter(|l| l.is_finite() && *l > 0.0).map_or(0, |l| l.min(MAX_DEPTH as f64) as usize)
}
/// The layers of a main by current GUID, and the old `overrideKey` of each that has one
/// (`None` when two layers claim the same key).
pub(super) struct OverrideScope {
current: BTreeSet<String>,
aliases: BTreeMap<String, Option<String>>,
}
/// Values by key that nested instances add to their outer instances' (property values, slot
/// contents): an outer instance's value wins. Each instance keeps only what it adds and shares the
/// rest, instead of copying every outer value (#1077); lookups walk at most the nesting depth.
pub(super) struct Chain<V> {
own: BTreeMap<String, V>,
outer: Option<Rc<Chain<V>>>,
}
impl<V> Chain<V> {
/// `outer`'s values, then `own`'s for keys `outer` lacks (shared as it is when `own` adds nothing).
fn over(outer: Option<&Rc<Chain<V>>>, own: BTreeMap<String, V>) -> Rc<Self> {
match outer {
Some(o) if own.is_empty() => Rc::clone(o),
_ => Rc::new(Self { own, outer: outer.cloned() }),
}
}
pub(super) fn get(&self, key: &str) -> Option<&V> {
self.outer.as_ref().and_then(|o| o.get(key)).or_else(|| self.own.get(key))
}
}
/// Where an instance's expanded layers are: the overrides and derived layout of the outermost
/// instance (by id path), the path of the instance being expanded, and the property values.
#[derive(Clone)]
pub(super) struct Inst<'a> {
pub(super) root: String,
pub(super) path: Vec<String>,
pub(super) overrides: Rc<Overrides<'a>>,
/// Property values with the factor their lengths take (the layer of the writing instance's
/// node that holds the assignment, as for override rows).
props: Rc<Chain<(&'a Kv<'a>, f64)>>,
/// Slot property GUID → private slot-content node GUID.
pub(super) slot_contents: Rc<Chain<String>>,
/// Layers imported from a slot's independent content subtree.
pub(super) slot_content: bool,
/// Mains being expanded (an instance inside its own main stops there).
symbols: Vec<String>,
/// The factor the lengths of the expanded layers take: the instance's `uniformScaleFactor`
/// times those of the instances around it (#2091). Override and derived rows are already at
/// the instance's scale and take none.
pub(super) scale: f64,
}
impl<'a> Mapper<'a> {
/// The layers of main `main` by current GUID and by `overrideKey`. An instance nested in the
/// main is not entered: its layers belong to another main.
fn override_scope(&mut self, main: &str) -> Rc<OverrideScope> {
if let Some(scope) = self.override_scopes.get(main) {
return Rc::clone(scope);
}
let mut current = BTreeSet::new();
let mut aliases: BTreeMap<String, Option<String>> = BTreeMap::new();
let mut visited = BTreeSet::new();
let mut pending: Vec<(String, usize)> = self.children(main).into_iter().map(|id| (id, 1)).collect();
let mut complete = true;
while let Some((id, depth)) = pending.pop() {
if depth > MAX_DEPTH || visited.len() >= self.max_nodes {
complete = false;
break;
}
if !visited.insert(id.clone()) {
continue;
}
let Some(node) = self.nodes.get(&id) else { continue };
current.insert(id.clone());
if let Some(alias) = node.get("overrideKey").and_then(guid) {
let target = aliases.entry(alias).or_insert_with(|| Some(id.clone()));
if target.as_deref().is_some_and(|target| target != id) {
*target = None;
}
}
if node.get("type").and_then(Kv::str) != Some("INSTANCE") {
pending.extend(self.children(&id).into_iter().map(|child| (child, depth + 1)));
}
}
// A scope cut short could name the wrong layer for a key: use none of the keys.
if !complete {
aliases.clear();
}
let scope = Rc::new(OverrideScope { current, aliases });
let cost = scope.current.len() + scope.aliases.len();
if cost <= self.max_nodes.saturating_sub(self.override_scope_entries) {
self.override_scope_entries += cost;
self.override_scopes.insert(main.to_string(), Rc::clone(&scope));
}
scope
}
pub(super) fn is_instance_swap(&self, def: &Kv) -> Option<()> {
matches!(self.prop_def(&guid(def)?), Some((_, "INSTANCE_SWAP"))).then_some(())
}
/// The definition a copy of it was made from, followed to the original: the values an instance
/// sets are kept under it, since an assignment and the layers that read it may name different
/// copies of one definition.
pub(super) fn prop_root(&self, def: &str) -> String {
let mut def = def;
for _ in 0..=MAX_DEPTH {
let Some(parent) = self.prop_parents.get(def) else { break };
def = parent;
}
def.to_string()
}
/// The definition `def` names: its own, or the set's for a variant's copy of it.
fn prop_def(&self, def: &str) -> Option<&(String, &'a str)> {
let mut def = def;
for _ in 0..=MAX_DEPTH {
if let Some(found) = self.prop_defs.get(def) {
return Some(found);
}
def = self.prop_parents.get(def)?;
}
None
}
/// Component property definitions and description.
pub(super) fn component(&mut self, o: &mut Obj, v: &View<'a>, id: &str) {
let mut defs = Obj::new();
for d in v.list("componentPropDefs") {
if d.get("isDeleted").and_then(Kv::bool) == Some(true) {
continue;
}
let Some((name, ty)) = d.get("id").and_then(guid).and_then(|i| self.prop_defs.get(&i).cloned()) else { continue };
let init = d.get("initialValue");
let def = match ty {
"BOOL" => json!({"type": "BOOLEAN", "defaultValue": init.and_then(|i| i.get("boolValue")).and_then(Kv::bool).unwrap_or(true)}),
"TEXT" => json!({"type": "TEXT", "defaultValue": init.and_then(prop_text).unwrap_or("")}),
"INSTANCE_SWAP" => match init.and_then(|i| i.get("guidValue")).and_then(guid) {
Some(g) => {
if self.nodes.contains_key(&g) {
self.referenced_mains.insert(g.clone());
}
json!({"type": "INSTANCE_SWAP", "defaultValue": g})
}
None => continue,
},
"VARIANT" => json!({"type": "VARIANT"}),
"SLOT" => {
self.warn_non_default_slot_config(d);
self.warn_dropped_slot_preferred_values(d);
json!({"type": "SLOT"})
}
_ => {
self.warn(format!("{} component properties are not supported and were skipped", ty.to_ascii_lowercase()));
continue;
}
};
defs.insert(name, def);
}
if !defs.is_empty() {
o.insert("componentPropertyDefinitions".into(), Value::Object(defs));
}
if let Some(d) = v.str("symbolDescription").or_else(|| v.str("description")).filter(|d| !d.is_empty()) {
let into = if o.get("type").and_then(Value::as_str) == Some("COMPONENT_SET") { &mut self.sets } else { &mut self.components };
into.insert(id.to_string(), json!({"description": d}));
}
}
/// Preferred slot values are source component keys, but this mapper has no proven way to map
/// those keys to file GUIDs. They are dropped instead of guessing native NodeIds.
fn warn_dropped_slot_preferred_values(&mut self, def: &Kv<'a>) {
let entries = def.get("preferredValues").and_then(|v| v.get("instanceSwapValues")).map(Kv::list).unwrap_or_default();
if !entries.is_empty() {
self.warn("preferred slot component keys cannot be resolved to file nodes and were skipped");
}
}
fn warn_non_default_slot_config(&mut self, def: &Kv<'a>) {
let Some(config) = def.get("slotPropConfig") else { return };
let non_default = [
("stretchChildOnInsert", Some(false)),
("displayByDefault", Some(false)),
("minChildren", None),
("maxChildren", None),
("allowPreferredValuesOnly", Some(false)),
]
.into_iter()
.any(|(field, bool_default)| {
let Some(value) = config.get(field) else { return false };
if let Some(default) = bool_default { value.bool() != Some(default) } else { value.f64() != Some(0.0) }
});
if non_default {
self.warn("non-default slot settings are not represented and were skipped");
}
}
/// A layer's component property references, and inside an instance the values they take.
pub(super) fn prop_refs(&mut self, o: &mut Obj, v: &View<'a>, inst: Option<&Inst<'a>>) {
let mut refs = Obj::new();
for e in v.get("parameterConsumptionMap").and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default() {
let field = match e.get("variableField").and_then(Kv::str) {
Some("VISIBLE") => "visible",
Some("TEXT_DATA") => "characters",
Some("OVERRIDDEN_SYMBOL_ID") => "mainComponent",
Some("SLOT_CONTENT_ID") => "slot",
_ => continue,
};
let Some(def) =
e.get("variableData").and_then(|d| d.get("value")).and_then(|x| x.get("propRefValue")).and_then(|p| p.get("defId")).and_then(guid)
else {
continue;
};
// Expanded frames must follow their main frame's source link. A direct slot reference
// makes the engine resolve ownership against the instance, where the property is not defined.
if !(field == "slot" && inst.is_some())
&& let Some((name, _)) = self.prop_def(&def)
{
refs.insert(field.into(), json!(name));
}
let Some(&(val, val_scale)) = inst.and_then(|i| i.props.get(&self.prop_root(&def))) else { continue };
match field {
"visible" => {
if let Some(b) = val.get("boolValue").and_then(Kv::bool) {
o.insert("visible".into(), json!(b));
}
}
"characters" => {
if let Some(t) = prop_text(val) {
o.insert("characters".into(), json!(t));
// Run styles count the chars they were made for: for other chars only
// the value's own apply.
if own_text(v) != Some(t) {
o.remove("characterStyleOverrides");
o.remove("styleOverrideTable");
o.remove("_figLineStarts");
if let Some(tv) = prop_text_data(val) {
let size = o.get("style").and_then(|s| s.get("fontSize")).and_then(Value::as_f64);
char_styles(o, tv, val_scale, size, &mut |w| self.warn(w));
}
}
}
}
_ => {}
}
}
if !refs.is_empty() {
o.insert("componentPropertyReferences".into(), Value::Object(refs));
}
}
/// An instance's main and properties; the context its expanded layers are built in.
pub(super) fn instance(&mut self, o: &mut Obj, v: &View<'a>, id: &str, outer: Option<&Inst<'a>>, rest_id: &str) -> Option<(Inst<'a>, String)> {
let sd = v.get("symbolData");
let mut sym = v.get("overriddenSymbolID").and_then(guid).or_else(|| sd.and_then(|s| s.get("symbolID")).and_then(guid));
// An instance swap property value picks the main.
if let Some(i) = outer
&& let Some(def) = prop_ref(v, "OVERRIDDEN_SYMBOL_ID")
&& let Some(g) = i.props.get(&self.prop_root(&def)).and_then(|(p, _)| prop_guid(p))
{
sym = Some(g);
}
let Some(sym) = sym else {
self.warn("instances without a main component were imported as empty frames");
return None;
};
o.insert("componentId".into(), json!(sym));
let assignments = v.list("componentPropAssignments");
let mut props = Obj::new();
for a in assignments {
let Some((name, ty)) = a.get("defID").and_then(guid).and_then(|d| self.prop_def(&d).cloned()) else { continue };
let val = assigned(a);
let p = match ty {
"BOOL" => val.and_then(|x| x.get("boolValue")).and_then(Kv::bool).map(|b| json!({"type": "BOOLEAN", "value": b})),
"TEXT" => val.and_then(prop_text).map(|t| json!({"type": "TEXT", "value": t})),
"INSTANCE_SWAP" => val.and_then(prop_guid).map(|g| json!({"type": "INSTANCE_SWAP", "value": g})),
_ => None,
};
if let Some(p) = p {
props.insert(name, p);
}
}
if !props.is_empty() {
o.insert("componentProperties".into(), Value::Object(props));
}
if !self.nodes.contains_key(&sym) {
self.warn("instances of components that aren't in the file show no layers");
return None;
}
if !self.referenced_mains.contains(&sym) {
self.referenced_mains.insert(sym.clone());
}
if outer.is_some_and(|i| i.symbols.contains(&sym)) {
return None;
}
// Overrides by id path below this instance (the instance itself is addressed by its main's id).
let own_path: Vec<String> = outer.map(|i| [i.path.as_slice(), &[id.to_string()]].concat()).unwrap_or_default();
let scope = self.override_scope(&sym);
let mut overrides: Overrides<'a> = BTreeMap::new();
if let Some(inherited) = outer {
// Only rows addressed below this instance can apply inside it (every path looked up in
// it starts with `own_path`); they sort together, so the others aren't even visited. A
// nested instance used to copy all of its outer instance's rows (#1077).
let below = inherited.overrides.range(own_path.clone()..).take_while(|(path, _)| path.starts_with(&own_path));
for (path, rows) in below.filter(|(path, _)| path.len() > own_path.len()) {
let mut path = path.clone();
// Rows an outer instance wrote for this instance's layers may use their old keys.
normalize_override_segment(&mut path, own_path.len(), &scope);
overrides.entry(path).or_default().extend(rows.iter().cloned());
}
}
// A nested instance's derived layout is for the size it has in its main: resized here, the
// sizes and places in it no longer hold and its layers follow their constraints instead.
// A scaled size is the factor's product, rounded to a float: a looser match.
let tolerance = if outer.is_some_and(|i| i.scale != 1.0) { 1e-4 } else { 1e-6 };
let differs = |a: f64, b: f64| (a - b).abs() > tolerance * a.abs().max(b.abs()).max(1.0);
let outer_scale = outer.map_or(1.0, |i| i.scale);
let resized = outer.is_some()
&& match (self.nodes.get(id).and_then(|b| View::of_scaled(b, outer_scale).size()), v.size()) {
(Some(a), Some(b)) => differs(a.0, b.0) || differs(a.1, b.1),
(a, b) => a.is_some() != b.is_some(),
};
let mut local: Overrides<'a> = BTreeMap::new();
// The lists are at the scale of the layer of this node that holds them.
let scale_of = |k: &str| v.find(k).map_or(1.0, |f| f.1);
let lists = [
(sd.and_then(|s| s.get("symbolOverrides")).map(Kv::list).unwrap_or_default(), scale_of("symbolData")),
(v.list("derivedSymbolData"), scale_of("derivedSymbolData")),
];
let source_depth = outer.map_or(0, |i| i.symbols.len());
let mut ordinal = 0;
for (layer, (list, row_scale)) in lists.into_iter().enumerate() {
for ov in list {
let row_ordinal = ordinal;
ordinal += 1;
let mut rel: Vec<String> =
ov.get("guidPath").and_then(|g| g.get("guids")).map(Kv::list).unwrap_or_default().iter().filter_map(guid).collect();
if rel.is_empty() || rel == [sym.clone()] {
continue;
}
normalize_override_segment(&mut rel, 0, &scope);
// Explicit overrides, then derived layout (stale once the instance is resized).
let entry = match (layer, resized) {
(0, _) => Entry::Override,
(_, false) => Entry::Derived,
(_, true) => Entry::Stale,
};
local.entry([own_path.as_slice(), &rel].concat()).or_default().push(OverrideRow {
row: ov,
entry,
source_depth,
level: override_level(ov),
ordinal: row_ordinal,
scale: row_scale,
});
}
}
for (path, rows) in local {
overrides.entry(path).or_default().extend(rows);
}
for rows in overrides.values_mut() {
// Deeper rows apply first and outer rows win: a row restating a nested level counts at
// that level's depth, wherever it sits on the wire. Equal depths keep the outer source
// last, then wire order.
rows.sort_by_key(|row| (std::cmp::Reverse(row.source_depth + row.level), std::cmp::Reverse(row.source_depth), row.ordinal));
}
// This instance's own property values and slot contents; the outer instances' win.
let assignment_scale = v.find("componentPropAssignments").map_or(1.0, |f| f.1);
let mut all_props: BTreeMap<String, (&'a Kv<'a>, f64)> = BTreeMap::new();
let mut slot_contents: BTreeMap<String, String> = BTreeMap::new();
for a in assignments {
if let (Some(d), Some(val)) = (a.get("defID").and_then(guid), assigned(a)) {
all_props.entry(self.prop_root(&d)).or_insert((val, assignment_scale));
}
let Some(def) = a.get("defID").and_then(guid) else { continue };
if self.prop_def(&def).is_none_or(|(_, ty)| *ty != "SLOT") {
continue;
}
let var_value = a.get("varValue");
if var_value.and_then(|v| v.get("dataType")).and_then(Kv::str) != Some("SLOT_CONTENT_ID") {
self.warn("malformed slot content assignment was skipped");
continue;
}
let target = var_value.and_then(|v| v.get("value")).and_then(|v| v.get("slotContentIdValue")).and_then(|v| v.get("guid")).and_then(guid);
let Some(target) = target else {
self.warn("malformed slot content assignment was skipped");
continue;
};
if target == "4294967295:4294967295" {
self.warn("slot content assignment uses the empty GUID sentinel and was skipped");
continue;
}
if self.nodes.get(&target).is_none_or(|n| n.get("isSlotContent").and_then(Kv::bool) != Some(true)) {
self.warn("slot content assignment target is missing or is not slot content");
continue;
}
slot_contents.entry(self.prop_root(&def)).or_insert(target);
}
let mut symbols = outer.map(|i| i.symbols.clone()).unwrap_or_default();
symbols.push(sym.clone());
let root = outer.map_or_else(|| rest_id.to_string(), |i| i.root.clone());
// The scale the Scale tool gave the instance; a junk factor changes nothing.
let factor = sd
.and_then(|s| s.get("uniformScaleFactor"))
.and_then(Kv::f64)
.filter(|k| (MIN_SCALE_FACTOR..=MAX_SCALE_FACTOR).contains(k))
.unwrap_or(1.0);
let scale = (outer_scale * factor).clamp(MIN_SCALE_FACTOR, MAX_SCALE_FACTOR);
Some((
Inst {
root,
path: own_path,
overrides: Rc::new(overrides),
props: Chain::over(outer.map(|i| &i.props), all_props),
slot_contents: Chain::over(outer.map(|i| &i.slot_contents), slot_contents),
slot_content: false,
symbols,
scale,
},
sym,
))
}
}
/// The text of a component property value.
fn prop_text<'k>(v: &'k Kv) -> Option<&'k str> {
prop_text_data(v).and_then(|t| t.get("characters")).and_then(Kv::str)
}
/// The text data of a component property value: `textValue`, or `textDataValue` in the variable form.
fn prop_text_data<'k, 's>(v: &'k Kv<'s>) -> Option<&'k Kv<'s>> {
v.get("textValue").or_else(|| v.get("textDataValue"))
}
/// The main an instance swap value names: `guidValue`, or `symbolIdValue` in the variable form.
pub(super) fn prop_guid(v: &Kv) -> Option<String> {
v.get("guidValue").and_then(guid).or_else(|| v.get("symbolIdValue")?.get("guid").and_then(guid))
}
/// The value a component property assignment sets: `value`, or the variable form `varValue` holds
/// when `value` is empty, as current files write many booleans and instance swaps.
pub(super) fn assigned<'k, 's>(assignment: &'k Kv<'s>) -> Option<&'k Kv<'s>> {
assignment.get("value").filter(|v| !matches!(v, Kv::Object(fields) if fields.is_empty())).or_else(|| assignment.get("varValue")?.get("value"))
}
/// The property a layer's `field` is bound to (`parameterConsumptionMap`).
pub(super) fn prop_ref(v: &View, field: &str) -> Option<String> {
v.get("parameterConsumptionMap")?.get("entries")?.list().iter().find_map(|e| {
if e.get("variableField").and_then(Kv::str) != Some(field) {
return None;
}
e.get("variableData")?.get("value")?.get("propRefValue")?.get("defId").and_then(guid)
})
}
-521
View File
@@ -1,521 +0,0 @@
use super::layer::{cap_name, own_text, svg_path, valid_size};
use super::paint::{paint, stroke};
use super::{Affine, Kv, Mapper, NO_STYLE, Obj, Point, Value, View, generated_record, guid, json, matrix, network, num, rows};
use figcraft_doc::geom::kurbo::{BezPath, PathEl, Rect, Shape};
use figcraft_doc::{ConnectorPath, MAX_RUNS, RouteEnd, Routing, Side, attach_points, attached_sides, route_along};
/// How a connector imports: from its saved snapshot (`terminal`: as one vector, no room for its
/// label), or drawn from its ends, as a frame with its line and `label` when it has one.
#[derive(Clone, Copy)]
pub(super) struct ConnectorShape {
pub snapshot: bool,
pub terminal: bool,
pub label: bool,
}
impl<'a> Mapper<'a> {
/// A connector's own properties on `o`; returns the children it adds (a line and a label).
pub(super) fn connector_layer(&mut self, o: &mut Obj, v: &View<'a>, rest_id: &str, space: Affine, shape: ConnectorShape) -> Vec<Value> {
if shape.snapshot {
if shape.terminal {
self.connector_snapshot_flatten(o, v, rest_id, space);
} else {
o.insert("fills".into(), json!([]));
o.insert("strokes".into(), json!([]));
for key in ["styles", "strokeWeight", "strokeAlign", "strokeJoin", "strokeDashes", "strokeCap"] {
o.remove(key);
}
o.insert("clipsContent".into(), json!(false));
}
return Vec::new();
}
let mid = self.connector(o, v, space);
self.connector_line_style(o, v);
if shape.label {
return self.connector_label(o, v, rest_id, mid);
}
if connector_has_saved_label(v) {
self.warn("connector labels were skipped");
}
Vec::new()
}
/// Whether a connector without a snapshot keeps its label: it has text of its own and there is
/// room for its two children under the depth and layer limits.
pub(super) fn connector_label_fits(&self, v: &View<'a>, depth: usize) -> bool {
own_text(v).is_some() && depth < super::MAX_DEPTH && self.count.saturating_add(2) < self.budget()
}
/// A connector's end caps, and when it has no saved outline or network its line between its
/// ends (free points, or a sibling layer): straight to the point given on the layer (its
/// centre unless given), or elbowed or curved from a side of it, as a board connector runs;
/// an elbow saved with its path (FigJam's control points) runs along it (#1910).
/// Returns the middle of that line by length, in the connector's own coordinates.
pub(super) fn connector(&mut self, o: &mut Obj, v: &View<'a>, space: Affine) -> Option<Point> {
for (from, to) in [("connectorStartCap", "strokeCapStart"), ("connectorEndCap", "strokeCapEnd")] {
if let Some(c) = v.str(from) {
o.insert(to.into(), json!(cap_name(c)));
}
}
if ["vectorNetwork", "vectorNetworkRef", "fillGeometry", "strokeGeometry"].iter().any(|k| o.contains_key(*k)) {
return None;
}
let routing = match v.str("connectorLineStyle") {
Some("ELBOWED") => Routing::Elbow,
Some("CURVED") => Routing::Curved,
_ => Routing::Straight,
};
let parent = v.get("parentIndex").and_then(|p| p.get("guid")).and_then(guid);
let end = |k: &str| -> Option<RouteEnd> {
let e = v.get(k)?;
let end = match e.get("endpointNodeID").and_then(guid).filter(|g| g != NO_STYLE) {
Some(id) => {
let n = *self.nodes.get(&id)?;
// Only siblings share the connector's coordinates.
if n.get("parentIndex").and_then(|p| p.get("guid")).and_then(guid) != parent {
return None;
}
let (w, h) = n.get("size").map_or((0.0, 0.0), |s| (num(s, "x").unwrap_or(0.0), num(s, "y").unwrap_or(0.0)));
let at = e.get("relativePosition").and_then(|r| Some((num(r, "x")?, num(r, "y")?)));
let t = n.get("transform").and_then(matrix).unwrap_or_default();
// An automatic magnet (`AUTO`: the side is picked when the connector is drawn;
// `CENTER`: the end aims at the layer's middle) attaches to the side facing the
// other end, as a board connector drawn here does; the file's `relativePosition`
// is not where such an end sits (#1909).
let automatic = matches!(e.get("magnet").and_then(Kv::str), Some("AUTO" | "CENTER"));
match routing {
Routing::Straight if !automatic => {
let (rx, ry) = at.unwrap_or((0.5, 0.5));
RouteEnd::Point(t * Point::new(w * rx, h * ry))
}
_ => RouteEnd::Box(t.transform_rect_bbox(Rect::new(0.0, 0.0, w, h)), side(e, at)),
}
}
None => RouteEnd::Point(e.get("position").and_then(|p| Some(Point::new(num(p, "x")?, num(p, "y")?)))?),
};
let finite = |p: Point| p.x.is_finite() && p.y.is_finite();
let ok = match end {
RouteEnd::Point(p) | RouteEnd::On(_, _, p) => finite(p),
RouteEnd::Box(b, _) => finite(b.origin()) && finite(Point::new(b.x1, b.y1)),
};
ok.then_some(end)
};
let ends = end("connectorStart").zip(end("connectorEnd"));
let path = ends.filter(|_| routing == Routing::Elbow).and_then(|(a, b)| saved_runs(v, a, b));
let Some(pts) = ends.and_then(|(a, b)| route_along(a, b, routing, path.as_ref())) else {
self.warn("connectors whose ends can't be placed are empty");
return None;
};
if routing != Routing::Straight && path.is_none() {
self.warn("elbowed and curved connectors without a saved path follow FigCraft's own routing");
}
// A curved route is one curve: its ends and the two control points between them.
let (path, vertices) = match (routing, pts.as_slice()) {
(Routing::Curved, [p0, c0, c1, p1]) => {
let mut path = BezPath::new();
path.move_to(*p0);
path.curve_to(*c0, *c1, *p1);
(path, vec![*p0, *p1])
}
_ => (pts.iter().enumerate().map(|(i, p)| if i == 0 { PathEl::MoveTo(*p) } else { PathEl::LineTo(*p) }).collect(), pts.clone()),
};
let b = path.bounding_box();
let (x0, y0) = (b.x0, b.y0);
o.insert("relativeTransform".into(), rows(space * Affine::translate((x0, y0))));
o.insert("size".into(), json!({"x": b.width(), "y": b.height()}));
let segments: Vec<Value> = match (routing, pts.as_slice()) {
(Routing::Curved, [p0, c0, c1, p1]) => vec![json!({"a": 0, "b": 1, "ta": [c0.x - p0.x, c0.y - p0.y], "tb": [c1.x - p1.x, c1.y - p1.y]})],
_ => (1..vertices.len()).map(|i| json!({"a": i - 1, "b": i})).collect(),
};
let vertices: Vec<Value> = vertices.iter().map(|p| json!({"x": p.x - x0, "y": p.y - y0})).collect();
o.insert("vectorNetwork".into(), json!({"vertices": vertices, "segments": segments}));
middle(&path).map(|m| m - b.origin().to_vec2())
}
/// The line's paint, weight, cap and dashes, which a connector keeps in its generated line
/// record rather than on itself: without a saved snapshot the routed line took none of them and
/// drew nothing (#1807). The connector's own values, when it has any, win.
fn connector_line_style(&mut self, o: &mut Obj, v: &View<'a>) {
let Some(line_style) = generated_record(v, "nodeGenerationData", 0) else { return };
let style = View::of(line_style);
// A connector that writes its own (even empty) paints or dash pattern keeps them: an empty
// list is no stroke, or a solid line, not a missing value.
if v.get("strokePaints").is_none() && o.get("strokes").and_then(Value::as_array).is_none_or(Vec::is_empty) {
let strokes: Vec<Value> = style.list("strokePaints").iter().filter_map(|p| paint(p, &mut |w| self.warn(w))).collect();
if !strokes.is_empty() {
o.insert("strokes".into(), Value::Array(strokes));
}
}
let mut line = Obj::new();
stroke(&mut line, &style);
if v.get("dashPattern").is_some() {
line.remove("strokeDashes");
}
for (key, value) in line {
o.entry(key).or_insert(value);
}
}
/// A connector with a label but no saved snapshot of it, as the frame a saved one imports as:
/// `o` keeps the connector's place and size and gives its line to a vector child, and the
/// label is a text child centred on `mid` (the line's middle; the frame's centre when the line
/// came saved). Returns the two children.
pub(super) fn connector_label(&mut self, o: &mut Obj, v: &View<'a>, rest_id: &str, mid: Option<Point>) -> Vec<Value> {
let (w, h) = o.get("size").map_or((0.0, 0.0), |s| (s["x"].as_f64().unwrap_or(0.0), s["y"].as_f64().unwrap_or(0.0)));
let mut line = Obj::new();
line.insert("id".into(), json!(format!("{rest_id};line")));
line.insert("type".into(), json!("VECTOR"));
line.insert("name".into(), json!(format!("{} line", v.str("name").unwrap_or("Connector"))));
line.insert("size".into(), json!({"x": w, "y": h}));
line.insert("relativeTransform".into(), rows(Affine::IDENTITY));
// The connector's fill paints colour its label; the line is only stroked.
let label_fills = o.remove("fills").filter(|f| f.as_array().is_some_and(|a| !a.is_empty()));
for key in LINE_KEYS {
if let Some(value) = o.remove(*key) {
line.insert((*key).into(), value);
}
}
o.insert("fills".into(), json!([]));
o.insert("strokes".into(), json!([]));
o.insert("clipsContent".into(), json!(false));
let mut text = Obj::new();
text.insert("id".into(), json!(format!("{rest_id};label")));
text.insert("type".into(), json!("TEXT"));
self.text(&mut text, v);
let characters = text.get("characters").and_then(Value::as_str).unwrap_or_default().to_string();
text.insert("name".into(), json!(characters));
text.insert("fills".into(), label_fills.unwrap_or_else(|| json!([{"type": "SOLID", "color": {"r": 0, "g": 0, "b": 0, "a": 1}}])));
// Centred on the line's middle in a box of the label's width, as tall as its lines (laid out
// again with the fonts at hand, so the box only centres it).
let size = text.get("style").and_then(|s| s["fontSize"].as_f64()).filter(|s| s.is_finite() && *s > 0.0).unwrap_or(16.0);
let lines = characters.lines().count().max(1) as f64;
let (bw, bh) = (LABEL_WIDTH, lines * size * 1.4);
if let Some(style) = text.get_mut("style").and_then(Value::as_object_mut) {
style.insert("textAlignHorizontal".into(), json!("CENTER"));
style.insert("textAlignVertical".into(), json!("CENTER"));
style.insert("textAutoResize".into(), json!("NONE"));
}
let at = mid.unwrap_or(Point::new(w / 2.0, h / 2.0));
text.insert("size".into(), json!({"x": bw, "y": bh}));
text.insert("relativeTransform".into(), rows(Affine::translate((at.x - bw / 2.0, at.y - bh / 2.0))));
self.count += 2;
self.warn("connector labels without a saved snapshot are centred on the line, without their background");
vec![Value::Object(line), Value::Object(text)]
}
pub(super) fn connector_snapshot_supported(&self, v: &View<'a>) -> bool {
if v.str("connectorType") != Some("MANUAL")
|| v.get("connectorVersion").and_then(Kv::f64) != Some(1.0)
|| v.str("connectorLineStyle") != Some("STRAIGHT")
|| v.get("derivedImmutableFrameData").and_then(|data| data.get("version")).and_then(Kv::f64) != Some(1.0)
{
return false;
}
let Some(line) = generated_record(v, "derivedImmutableFrameData", 0) else { return false };
if line.get("transform").and_then(matrix).is_none() || valid_size(line.get("size")).is_none() {
return false;
}
let geometry = ["fillGeometry", "strokeGeometry"].iter().any(|key| {
line.get(key).map(Kv::list).unwrap_or_default().iter().any(|path| {
num(path, "commandsBlob")
.and_then(|index| self.blobs.get(index as usize))
.and_then(|blob| svg_path(blob))
.is_some_and(|data| !data.is_empty())
})
});
let network = line
.get("vectorData")
.and_then(|data| num(data, "vectorNetworkBlob"))
.and_then(|index| self.blobs.get(index as usize))
.and_then(|blob| network::decode(blob))
.is_some_and(|network| !network.segments.is_empty());
(geometry || network) && generated_record(v, "nodeGenerationData", 0).is_some()
}
pub(super) fn connector_snapshot_flatten(&mut self, out: &mut Obj, v: &View<'a>, rest_id: &str, space: Affine) {
let parent = space * v.get("transform").and_then(matrix).unwrap_or_default();
let Some(vector) = self.connector_snapshot_vector(v, rest_id, parent, false) else {
self.warn("connector snapshot vector data is missing; connector was left empty");
return;
};
for key in [
"size",
"relativeTransform",
"fills",
"strokes",
"strokeWeight",
"strokeAlign",
"strokeJoin",
"strokeDashes",
"strokeCap",
"strokeCapStart",
"strokeCapEnd",
"styles",
"fillGeometry",
"strokeGeometry",
"vectorNetwork",
] {
if let Some(value) = vector.get(key) {
out.insert(key.to_string(), value.clone());
}
}
if vector.get("visible").and_then(Value::as_bool) == Some(false) {
out.insert("visible".into(), json!(false));
}
if let Some(line_opacity) = vector.get("opacity").and_then(Value::as_f64) {
let root_opacity = out.get("opacity").and_then(Value::as_f64).unwrap_or(1.0);
out.insert("opacity".into(), json!(root_opacity * line_opacity));
}
if connector_has_saved_label(v) {
let reason = if self.count >= self.budget() { "node budget" } else { "depth limit" };
self.warn(format!("connector label was skipped: {reason} prevents nested snapshot children"));
}
}
/// Build the saved vector child or flattened terminal connector from the same child-only view.
fn connector_snapshot_vector(&mut self, v: &View<'a>, rest_id: &str, space: Affine, nested: bool) -> Option<Obj> {
let (Some(line), Some(line_style)) = (generated_record(v, "derivedImmutableFrameData", 0), generated_record(v, "nodeGenerationData", 0))
else {
return None;
};
let mut vector = Obj::new();
vector.insert("id".into(), json!(if nested { format!("{rest_id};vector") } else { rest_id.to_string() }));
vector.insert("type".into(), json!("VECTOR"));
if let Some(name) = v.str("name") {
vector.insert("name".into(), json!(if nested { format!("{name} vector") } else { name.to_string() }));
}
let line_view = View::of(line).with([(line_style, 1.0)]);
self.common(&mut vector, &line_view, "VECTOR", space, None);
self.geometry(&mut vector, &line_view);
for (from, to) in [("connectorStartCap", "strokeCapStart"), ("connectorEndCap", "strokeCapEnd")] {
if let Some(cap) = v.str(from) {
vector.entry(to).or_insert_with(|| json!(cap_name(cap)));
}
}
Some(vector)
}
/// Snapshot the straight, manual connector contract carried by its generated children.
pub(crate) fn connector_snapshot_children(&mut self, v: &View<'a>, rest_id: &str) -> Vec<Value> {
if self.count >= self.budget() {
self.warn(format!("the file has more than {} layers; connector snapshot children were skipped", self.max_nodes));
return Vec::new();
}
let Some(vector) = self.connector_snapshot_vector(v, rest_id, Affine::IDENTITY, true) else {
self.warn("connector snapshot vector data is missing; connector was left empty");
return Vec::new();
};
let mut children = Vec::new();
children.push(Value::Object(vector));
self.count += 1;
let Some(text_frame) = generated_record(v, "derivedImmutableFrameData", 1) else {
if connector_has_saved_label(v) {
self.warn("connector label was skipped: derived text bounds are missing");
}
return children;
};
let Some(background) = generated_record(v, "derivedImmutableFrameData", 2) else {
if connector_has_saved_label(v) || valid_size(text_frame.get("size")).is_some_and(|(w, h)| w > 0.0 && h > 0.0) {
self.warn("connector label was skipped: derived background bounds are missing");
}
return children;
};
let Some(text_style) = generated_record(v, "nodeGenerationData", 1) else {
if valid_size(text_frame.get("size")).is_some_and(|(w, h)| w > 0.0 && h > 0.0) {
self.warn("connector label was skipped: text style data is missing");
}
return children;
};
let text_view = View::of(text_frame).with([(text_style, 1.0)]);
let Some(characters) = text_view.get("textData").and_then(|d| d.get("characters")).and_then(Kv::str).filter(|s| !s.trim().is_empty()) else {
return children;
};
let Some(text_transform) = text_frame.get("transform").and_then(matrix) else {
self.warn("connector label was skipped: text transform is invalid");
return children;
};
let Some(background_transform) = background.get("transform").and_then(matrix) else {
self.warn("connector label was skipped: background transform is invalid");
return children;
};
let Some(text_size) = valid_size(text_frame.get("size")) else {
self.warn("connector label was skipped: text bounds are invalid");
return children;
};
let Some(background_size) = valid_size(background.get("size")) else {
self.warn("connector label was skipped: background bounds are invalid");
return children;
};
let composed_text_transform = background_transform * text_transform;
if !composed_text_transform.as_coeffs().iter().all(|coefficient| coefficient.is_finite()) {
self.warn("connector label was skipped: composed text transform is invalid");
return children;
}
let Some(background_style) = generated_record(v, "nodeGenerationData", 2) else {
self.warn("connector label background was skipped: style data is missing");
self.add_connector_text(&mut children, rest_id, characters, text_size, composed_text_transform, &text_view);
return children;
};
let background_view = View::of(background).with([(background_style, 1.0)]);
if !background_view.list("fillPaints").is_empty() {
if self.count >= self.budget() {
self.warn(format!("the file has more than {} layers; connector label background was skipped", self.max_nodes));
} else {
let mut rect = Obj::new();
rect.insert("id".into(), json!(format!("{rest_id};label-background")));
rect.insert("type".into(), json!("RECTANGLE"));
rect.insert("name".into(), json!("connector label background"));
self.common(&mut rect, &background_view, "RECTANGLE", Affine::IDENTITY, None);
rect.insert("size".into(), json!({"x": background_size.0, "y": background_size.1}));
rect.insert("relativeTransform".into(), rows(background_transform));
rect.insert("clipsContent".into(), json!(false));
children.push(Value::Object(rect));
self.count += 1;
}
}
self.add_connector_text(&mut children, rest_id, characters, text_size, composed_text_transform, &text_view);
children
}
fn add_connector_text(
&mut self,
children: &mut Vec<Value>,
rest_id: &str,
characters: &str,
size: (f64, f64),
transform: Affine,
view: &View<'a>,
) {
if self.count >= self.budget() {
self.warn(format!("the file has more than {} layers; connector label was skipped", self.max_nodes));
return;
}
let mut text = Obj::new();
text.insert("id".into(), json!(format!("{rest_id};label-text")));
text.insert("type".into(), json!("TEXT"));
text.insert("name".into(), json!(characters));
text.insert("size".into(), json!({"x": size.0, "y": size.1}));
self.common(&mut text, view, "TEXT", Affine::IDENTITY, None);
text.insert("relativeTransform".into(), rows(transform));
self.text(&mut text, view);
children.push(Value::Object(text));
self.count += 1;
}
}
pub(super) fn connector_has_saved_label(v: &View<'_>) -> bool {
own_text(v).is_some()
|| generated_record(v, "nodeGenerationData", 1)
.and_then(|r| r.get("textData"))
.and_then(|d| d.get("characters"))
.and_then(Kv::str)
.is_some_and(|s| !s.trim().is_empty())
}
/// The path a FigJam elbow connector was saved with: its `connectorControlPoints`, one per run
/// between the first and the last, each a point on its run (in the connector's parent's
/// coordinates, like its ends) with the run's axis; the first run leaves the start's side and the
/// last arrives at the end's, so neither is saved. `None` without any, or when they don't make
/// such a path: a point off both axes, two runs in a row the same way, a first or last run that
/// would leave or arrive sideways to its side, or more runs than a hand could make here.
fn saved_runs(v: &View<'_>, from: RouteEnd, to: RouteEnd) -> Option<ConnectorPath> {
let saved = v.list("connectorControlPoints");
if saved.is_empty() || saved.len() + 2 > MAX_RUNS {
return None;
}
let (p0, p1) = attach_points(from, to);
let mid = p0.midpoint(p1);
let mut at = Vec::with_capacity(saved.len());
let (mut first, mut last) = (None, None);
for p in saved {
let pos = p.get("position").and_then(|q| Some(Point::new(num(q, "x")?, num(q, "y")?)))?;
let (ax, ay) = p.get("axis").and_then(|q| Some((num(q, "x")?, num(q, "y")?)))?;
let sideways = match (ax != 0.0, ay != 0.0) {
(true, false) => true,
(false, true) => false,
_ => return None,
};
if last == Some(sideways) {
return None;
}
first.get_or_insert(!sideways);
last = Some(sideways);
at.push(if sideways { pos.y - mid.y } else { pos.x - mid.x });
}
// The run through each end goes the way its side faces, out of the object: the first leaves
// the start's side for the first saved run, the last arrives at the end's from the last saved
// run. One along the side, or back into the object, doesn't.
let (across, last) = (first?, last?);
let (&a0, &a1) = (at.first()?, at.last()?);
// Where the run from `p` (`sideways`: going across) meets the saved run at `v`.
let onto = |sideways: bool, p: Point, v: f64| if sideways { Point::new(mid.x + v, p.y) } else { Point::new(p.x, mid.y + v) };
let out_of = |s: Option<Side>, p: Point, q: Point| s.is_none_or(|s| (q - p).dot(s.normal()) > 0.0);
let (s0, s1) = attached_sides(from, to);
let fits = out_of(s0, p0, onto(across, p0, a0)) && out_of(s1, p1, onto(!last, p1, a1));
fits.then_some(ConnectorPath::Runs { across, at })
}
/// The side of its layer a connector end leaves from: the `magnet` the file names (`AUTO` and
/// `CENTER`: the side facing the other end, whatever `relativePosition` says; #1909), else the edge
/// its point on the layer (`relativePosition`, 0–1) lies on, else the side facing the other end.
fn side(e: &Kv<'_>, at: Option<(f64, f64)>) -> Side {
match e.get("magnet").and_then(Kv::str) {
Some("TOP") => Side::Top,
Some("RIGHT") => Side::Right,
Some("BOTTOM") => Side::Bottom,
Some("LEFT") => Side::Left,
Some("AUTO" | "CENTER") => Side::Auto,
_ => match at {
Some((x, _)) if x <= 0.0 => Side::Left,
Some((x, _)) if x >= 1.0 => Side::Right,
Some((_, y)) if y <= 0.0 => Side::Top,
Some((_, y)) if y >= 1.0 => Side::Bottom,
_ => Side::Auto,
},
}
}
/// What a connector's line carries, moved to the line child when the connector becomes a frame.
const LINE_KEYS: &[&str] = &[
"vectorNetwork",
"vectorNetworkRef",
"fillGeometry",
"strokeGeometry",
"fills",
"strokes",
"strokeWeight",
"strokeAlign",
"strokeJoin",
"strokeDashes",
"strokeCap",
"strokeCapStart",
"strokeCapEnd",
"strokeWidthProfile",
"styles",
];
/// How wide a label without a saved snapshot is laid out, centred on its connector.
const LABEL_WIDTH: f64 = 200.0;
/// The point halfway along `path` by length.
fn middle(path: &BezPath) -> Option<Point> {
let mut pts: Vec<Point> = Vec::new();
figcraft_doc::geom::kurbo::flatten(path.iter(), 0.25, |el| {
if let PathEl::MoveTo(p) | PathEl::LineTo(p) = el {
pts.push(p);
}
});
let total: f64 = pts.windows(2).map(|s| (s[1] - s[0]).hypot()).sum();
let mut left = total / 2.0;
for s in pts.windows(2) {
let l = (s[1] - s[0]).hypot();
if l >= left && l > 0.0 {
return Some(s[0].lerp(s[1], left / l));
}
left -= l;
}
pts.first().copied()
}
@@ -1,178 +0,0 @@
//! Connector snapshot flattening stays within the node and depth budgets.
use super::{MAX_DEPTH, Mapper, View};
use crate::fig::kiwi::Kv;
use crate::rest::MAX_NODES;
use figcraft_doc::geom::Affine;
fn obj(fields: Vec<(&'static str, Kv<'static>)>) -> Kv<'static> {
Kv::Object(fields)
}
fn guid(local_id: u64) -> Kv<'static> {
obj(vec![("sessionID", Kv::Uint(40_000_000)), ("localID", Kv::Uint(local_id))])
}
fn path_blob() -> Vec<u8> {
let mut bytes = vec![1];
bytes.extend(0.0f32.to_le_bytes());
bytes.extend(0.0f32.to_le_bytes());
bytes.push(2);
bytes.extend(20.0f32.to_le_bytes());
bytes.extend(10.0f32.to_le_bytes());
bytes
}
fn record(local_id: u64, mut fields: Vec<(&'static str, Kv<'static>)>) -> Kv<'static> {
fields.push(("guidPath", obj(vec![("guids", Kv::Array(vec![guid(local_id)]))])));
obj(fields)
}
fn message() -> Kv<'static> {
let line = record(
0,
vec![
(
"transform",
obj(vec![
("m00", Kv::Float(1.0)),
("m01", Kv::Float(0.0)),
("m02", Kv::Float(0.0)),
("m10", Kv::Float(0.0)),
("m11", Kv::Float(1.0)),
("m12", Kv::Float(0.0)),
]),
),
("size", obj(vec![("x", Kv::Float(20.0)), ("y", Kv::Float(10.0))])),
("fillGeometry", Kv::Array(vec![obj(vec![("commandsBlob", Kv::Uint(0))])])),
],
);
let text = record(
1,
vec![
(
"transform",
obj(vec![
("m00", Kv::Float(1.0)),
("m01", Kv::Float(0.0)),
("m02", Kv::Float(0.0)),
("m10", Kv::Float(0.0)),
("m11", Kv::Float(1.0)),
("m12", Kv::Float(0.0)),
]),
),
("size", obj(vec![("x", Kv::Float(50.0)), ("y", Kv::Float(20.0))])),
],
);
let background = record(
2,
vec![
(
"transform",
obj(vec![
("m00", Kv::Float(1.0)),
("m01", Kv::Float(0.0)),
("m02", Kv::Float(0.0)),
("m10", Kv::Float(0.0)),
("m11", Kv::Float(1.0)),
("m12", Kv::Float(0.0)),
]),
),
("size", obj(vec![("x", Kv::Float(60.0)), ("y", Kv::Float(30.0))])),
],
);
let line_style = record(0, vec![]);
let text_style = record(1, vec![("textData", obj(vec![("characters", Kv::Str("budget label".into()))]))]);
let background_style = record(2, vec![("fillPaints", Kv::Array(vec![]))]);
obj(vec![
(
"nodeChanges",
Kv::Array(vec![obj(vec![
("guid", obj(vec![("sessionID", Kv::Uint(1)), ("localID", Kv::Uint(123))])),
("type", Kv::Enum("CONNECTOR")),
("name", Kv::Str("budget connector".into())),
("connectorType", Kv::Enum("MANUAL")),
("connectorVersion", Kv::Float(1.0)),
("connectorLineStyle", Kv::Enum("STRAIGHT")),
("derivedImmutableFrameData", obj(vec![("version", Kv::Float(1.0)), ("overrides", Kv::Array(vec![line, text, background]))])),
("nodeGenerationData", obj(vec![("overrides", Kv::Array(vec![line_style, text_style, background_style]))])),
])]),
),
("blobs", Kv::Array(vec![obj(vec![("bytes", Kv::Bytes(path_blob()))])])),
])
}
#[test]
fn connector_snapshot_respects_last_and_zero_child_slots() {
let message = message();
let node = message.get("nodeChanges").unwrap().list().first().unwrap();
let view = View::of(node);
let mut mapper = Mapper::new(&message, MAX_NODES);
mapper.count = MAX_NODES - 1;
let children = mapper.connector_snapshot_children(&view, "1:123");
assert_eq!(children.len(), 1, "the final available slot preserves the saved vector");
assert_eq!(children[0]["type"], "VECTOR");
assert!(!children[0]["fillGeometry"].as_array().unwrap().is_empty(), "saved outline survives the budget limit");
assert_eq!(children[0]["fillGeometry"][0]["path"], "M 0 0 L 20 10");
assert_eq!(mapper.count, MAX_NODES);
assert!(mapper.warnings.seen().any(|warning| warning == format!("the file has more than {MAX_NODES} layers; connector label was skipped")));
let mut mapper = Mapper::new(&message, MAX_NODES);
mapper.count = MAX_NODES;
let children = mapper.connector_snapshot_children(&view, "1:123");
assert!(children.is_empty());
assert_eq!(mapper.count, MAX_NODES, "the node budget is never exceeded");
assert!(mapper.warnings.seen().any(|warning| warning.contains("connector snapshot children were skipped")));
}
#[test]
fn connector_snapshot_flattens_vector_at_depth_and_node_limits() {
let message = message();
let mut mapper = Mapper::new(&message, MAX_NODES);
let depth_limited = mapper.layer("1:123", None, Affine::IDENTITY, None, None, MAX_DEPTH).expect("terminal connector");
assert_eq!(depth_limited["type"], "VECTOR");
assert!(!depth_limited["fillGeometry"].as_array().unwrap().is_empty());
assert!(depth_limited.get("children").is_none());
assert!(mapper.warnings.seen().any(|warning| warning == "connector label was skipped: depth limit prevents nested snapshot children"));
let mut mapper = Mapper::new(&message, MAX_NODES);
mapper.count = MAX_NODES - 1;
let budget_limited = mapper.layer("1:123", None, Affine::IDENTITY, None, None, 0).expect("last allowed connector");
assert_eq!(budget_limited["type"], "VECTOR");
assert!(!budget_limited["fillGeometry"].as_array().unwrap().is_empty());
assert!(budget_limited.get("children").is_none());
assert_eq!(mapper.count, MAX_NODES);
assert!(mapper.warnings.seen().any(|warning| warning == "connector label was skipped: node budget prevents nested snapshot children"));
}
/// A mapper note is met once per layer, so the kinds were named once each with no sign of how much
/// of the file they covered: a file whose every layer lost a setting read the same as one where a
/// single layer did (#467). They are counted now, with the `(×n)` the REST importer already uses
/// and the import report already reads back off before translating a note.
#[test]
fn a_note_met_more_than_once_carries_its_count() {
let message = message();
let mut mapper = Mapper::new(&message, MAX_NODES);
for _ in 0..2 {
mapper.layer("1:123", None, Affine::IDENTITY, None, None, MAX_DEPTH).expect("terminal connector");
}
let notes = std::mem::take(&mut mapper.warnings).finish();
let counted: Vec<&String> = notes.iter().filter(|n| n.contains("(×")).collect();
assert!(
counted.iter().any(|n| n.ends_with("(×2)") && n.contains("connector label was skipped: depth limit")),
"the note met twice says so: {notes:?}"
);
assert!(notes.iter().all(|n| !n.ends_with("(×1)")), "a count of one is left off: {notes:?}");
// One layer, and the same notes have no count at all.
let mut mapper = Mapper::new(&message, MAX_NODES);
mapper.layer("1:123", None, Affine::IDENTITY, None, None, MAX_DEPTH).expect("terminal connector");
let once = std::mem::take(&mut mapper.warnings).finish();
assert!(once.iter().all(|n| !n.contains("(×")), "{once:?}");
assert_eq!(
once.len(),
counted.len() + notes.iter().filter(|n| !n.contains("(×")).count(),
"the same kinds, counted or not: {once:?} vs {notes:?}"
);
}
-333
View File
@@ -1,333 +0,0 @@
use super::component::{assigned, prop_guid};
use super::paint::color;
use super::variables::variable_value;
use super::{Affine, BTreeMap, BTreeSet, Kv, MAX_DEPTH, Mapper, Obj, Value, View, copy_str, guid, json, rows};
impl<'a> Mapper<'a> {
/// The DOCUMENT with its pages.
pub(super) fn document(&mut self) -> Value {
let doc_id = self.nodes.iter().find(|(_, n)| n.get("type").and_then(Kv::str) == Some("DOCUMENT")).map(|(id, _)| id.clone());
let pages: Vec<String> = match &doc_id {
Some(d) => self.children(d),
None => self.nodes.iter().filter(|(_, n)| n.get("type").and_then(Kv::str) == Some("CANVAS")).map(|(id, _)| id.clone()).collect(),
};
let mut internal = Vec::new();
let mut visible = Vec::new();
for p in pages {
let Some(&n) = self.nodes.get(&p) else { continue };
if n.get("type").and_then(Kv::str) != Some("CANVAS") {
self.warn("document children other than pages were skipped");
continue;
}
// The hidden page that holds styles, variables and library copies.
if n.get("internalOnly").and_then(Kv::bool) == Some(true) {
internal.push((p, n));
} else {
visible.push((p, n));
}
}
// The library copies the pages use come first: in a file past the layer cap, the pages'
// last layers are skipped rather than the mains every instance of them needs (#981).
let visible_pages: Vec<String> = visible.iter().map(|(p, _)| p.clone()).collect();
let required_mains = self.referenced_component_mains(&visible_pages);
// The pages and their top-level layers, up to half the cap, stay for the pages.
let top: usize = visible_pages.iter().map(|p| 1 + self.children(p).len()).sum();
self.reserved = top.min(self.max_nodes / 2);
for (id, canvas) in internal {
if let Some(context) = self.library_context(&id, canvas, &required_mains) {
self.library_contexts.push(context);
}
}
self.reserved = 0;
let out: Vec<Value> = visible.into_iter().map(|(p, n)| self.page(&p, n)).collect();
if out.is_empty() {
self.warn("the file has no pages");
}
let mut doc = json!({"id": doc_id.unwrap_or_else(|| "0:0".into()), "type": "DOCUMENT"});
doc["children"] = Value::Array(out);
doc
}
/// The ids of the components the visible pages use, directly or through the components they
/// use in turn (nested instances, swap targets, overrides naming another main).
fn referenced_component_mains(&mut self, visible_pages: &[String]) -> BTreeSet<String> {
let mut mains = BTreeSet::new();
let mut visited = BTreeSet::new();
let mut pending: Vec<(String, usize)> = visible_pages.iter().flat_map(|page| self.children(page)).map(|id| (id, 1)).collect();
while let Some((id, depth)) = pending.pop() {
if depth > MAX_DEPTH {
self.warn(format!("component references below {MAX_DEPTH} layer levels were skipped"));
continue;
}
if !visited.insert(id.clone()) {
continue;
}
if visited.len() > self.max_nodes {
let max = self.max_nodes;
self.warn(format!("the component reference scan reached the {max}-layer limit"));
break;
}
let Some(node) = self.nodes.get(&id) else { continue };
for target in self.component_main_targets(node) {
if mains.insert(target.clone()) {
pending.push((target, 1));
}
}
pending.extend(self.children(&id).into_iter().map(|child| (child, depth + 1)));
}
mains
}
/// The mains a layer names: an instance's own, the ones its overrides swap in, the instance-swap
/// defaults and values of component properties.
fn component_main_targets(&self, node: &Kv<'a>) -> Vec<String> {
let mut targets: Vec<String> = node
.get("componentPropDefs")
.map(Kv::list)
.unwrap_or_default()
.iter()
.filter_map(|definition| {
self.is_instance_swap(definition.get("id")?)?;
definition.get("initialValue")?.get("guidValue").and_then(guid)
})
.collect();
if node.get("type").and_then(Kv::str) != Some("INSTANCE") {
return targets;
}
// The instance and the override rows below it (rows carry the same fields, nested).
let mut pending: Vec<(&Kv<'a>, usize)> = vec![(node, 0)];
let mut rows = 0usize;
while let Some((row, depth)) = pending.pop() {
rows += 1;
if depth > MAX_DEPTH || rows > self.max_nodes {
break;
}
let data = row.get("symbolData");
targets.extend(row.get("overriddenSymbolID").and_then(guid));
targets.extend(data.and_then(|d| d.get("symbolID")).and_then(guid));
for assignment in row.get("componentPropAssignments").map(Kv::list).unwrap_or_default() {
if assignment.get("defID").is_some_and(|def| self.is_instance_swap(def).is_some()) {
targets.extend(assigned(assignment).and_then(prop_guid));
}
}
let overrides = data.and_then(|d| d.get("symbolOverrides")).map(Kv::list).unwrap_or_default();
let derived = row.get("derivedSymbolData").map(Kv::list).unwrap_or_default();
pending.extend(overrides.iter().chain(derived).map(|row| (row, depth + 1)));
}
targets
}
/// The branches of the internal canvas `id` that hold the `required_mains` (with the frames
/// they sit in), as a frame of its own: the cached definitions of components that come from a
/// library. `None` when it holds none.
fn library_context(&mut self, id: &str, canvas: &'a Kv<'a>, required_mains: &BTreeSet<String>) -> Option<Value> {
let mut included = BTreeSet::new();
let mut parents: BTreeMap<String, String> = BTreeMap::new();
// The canvas itself is never a layer of the walk, whatever the parent links say.
let mut visited = BTreeSet::from([id.to_string()]);
// Each entry: layer, depth, inside a required definition.
let mut pending: Vec<(String, usize, bool)> = self.children(id).into_iter().map(|child| (child, 1, false)).collect();
while let Some((node_id, depth, in_definition)) = pending.pop() {
if depth > MAX_DEPTH {
self.warn(format!("internal library branches deeper than {MAX_DEPTH} levels were skipped"));
continue;
}
if !visited.insert(node_id.clone()) {
continue;
}
self.library_scan_count += 1;
if self.library_scan_count > self.max_nodes {
let max = self.max_nodes;
self.warn(format!("the internal library scan reached the {max}-layer limit; the remaining branches were skipped"));
break;
}
let Some(node) = self.nodes.get(&node_id) else { continue };
let kind = node.get("type").and_then(Kv::str);
let definition = (kind == Some("SYMBOL") || (kind == Some("FRAME") && node.get("isStateGroup").and_then(Kv::bool) == Some(true)))
&& required_mains.contains(&node_id);
let in_definition = in_definition || definition;
if in_definition {
included.insert(node_id.clone());
}
if definition {
// The frames around it keep the path its layers are addressed by.
let mut ancestor = parents.get(&node_id);
for _ in 0..=MAX_DEPTH {
let Some(parent) = ancestor else { break };
if !included.insert(parent.clone()) {
break;
}
ancestor = parents.get(parent);
}
}
for child in self.children(&node_id).into_iter().rev() {
// A layer reached again keeps the parent it was first reached by, so the parents
// lead back to the canvas.
if visited.contains(&child) {
continue;
}
parents.entry(child.clone()).or_insert_with(|| node_id.clone());
pending.push((child, depth + 1, in_definition));
}
}
if included.is_empty() {
return None;
}
let mut context = Obj::new();
context.insert("id".into(), json!(id));
context.insert("type".into(), json!("FRAME"));
copy_str(&mut context, &View::of(canvas), "name", "name");
context.insert("size".into(), json!({"x": 0.0, "y": 0.0}));
context.insert("relativeTransform".into(), rows(Affine::IDENTITY));
context.insert(crate::rest::FIG_SOURCE_FIELD.into(), json!({"origin": id, "expanded": false, "wireFrame": true, "wireInstance": false}));
self.variable_bindings(&mut context, &View::of(canvas));
let mut children = Vec::new();
for child in self.children(id) {
children.extend(self.layer_filtered(&child, None, Affine::IDENTITY, None, None, 0, Some(&included)));
}
if !children.is_empty() {
context.insert("children".into(), Value::Array(children));
}
Some(Value::Object(context))
}
fn page(&mut self, id: &str, n: &'a Kv<'a>) -> Value {
let v = View::of(n);
let mut o = Obj::new();
o.insert("id".into(), json!(id));
o.insert("type".into(), json!("CANVAS"));
copy_str(&mut o, &v, "name", "name");
if let Some(c) = v.get("backgroundColor") {
o.insert("backgroundColor".into(), color(c));
}
if let Some(c) = v.get("prototypeBackgroundColor") {
o.insert("prototypeBackgrounds".into(), json!([color(c)]));
}
self.variable_bindings(&mut o, &v);
let mut flows: Vec<(&str, Value)> = Vec::new();
let mut kids = Vec::new();
for c in self.children(id) {
if let Some(sp) = self.nodes.get(&c).and_then(|k| k.get("prototypeStartingPoint")) {
let name = sp.get("name").and_then(Kv::str).filter(|s| !s.is_empty()).unwrap_or("Flow");
flows.push((sp.get("position").and_then(Kv::str).unwrap_or(""), json!({"nodeId": c, "name": name})));
}
kids.extend(self.layer(&c, None, Affine::IDENTITY, None, None, 0));
}
flows.sort_by(|a, b| a.0.cmp(b.0));
if !flows.is_empty() {
o.insert("flowStartingPoints".into(), Value::Array(flows.into_iter().map(|f| f.1).collect()));
}
o.insert("children".into(), Value::Array(kids));
Value::Object(o)
}
/// The shared styles the file defines, in their order: each with the sample layer that holds
/// its values (`styleDefinitions`, our extension of the file JSON; the REST importer reads it).
pub(super) fn style_definitions(&mut self) -> Vec<Value> {
let mut found: Vec<(&'a str, String, &'a Kv<'a>)> = Vec::new();
for (id, n) in &self.nodes {
let live = n.get("isSoftDeleted").and_then(Kv::bool) != Some(true) && n.get("isSoftDeletedStyle").and_then(Kv::bool) != Some(true);
if live && n.get("styleType").and_then(Kv::str).is_some_and(|t| matches!(t, "FILL" | "TEXT" | "EFFECT" | "GRID")) {
found.push((n.get("sortPosition").and_then(Kv::str).unwrap_or(""), id.clone(), n));
}
}
found.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
let mut out = Vec::new();
for (_, id, n) in found {
let Some(node) = self.layer(&id, None, Affine::IDENTITY, None, None, 0) else { break };
let name = n.get("name").and_then(Kv::str).unwrap_or("Style");
let desc = n.get("styleDescription").or_else(|| n.get("description")).and_then(Kv::str).unwrap_or("");
let ty = n.get("styleType").and_then(Kv::str).unwrap_or("");
out.push(json!({"id": id, "name": name, "description": desc, "styleType": ty, "node": node}));
}
out
}
/// Collections and variables, as the local variables response's `meta`, with their ids in the
/// file's order (`collectionOrder` and `variableOrder`, our extensions).
pub(super) fn variables(&self) -> Option<Value> {
let of_type = |t: &'static str, key: &'static str| {
let live = move |n: &Kv| n.get("type").and_then(Kv::str) == Some(t) && n.get("isSoftDeleted").and_then(Kv::bool) != Some(true);
let mut list: Vec<(&str, &String, &'a Kv<'a>)> = self
.nodes
.iter()
.filter(|(_, n)| live(n))
.map(|(id, n)| (n.get(key).and_then(|p| p.get("position").or(Some(p))).and_then(Kv::str).unwrap_or(""), id, *n))
.collect();
list.sort_by(|a, b| (a.0, a.1).cmp(&(b.0, b.1)));
list.into_iter().map(|(_, id, n)| (id, n))
};
let mut order = Vec::new();
let mut collections = Obj::new();
// Copies of one library collection (one per version the file met) become one: the newest
// keeps its modes in its order, the others add the modes it no longer has.
let mut extra: BTreeMap<String, Vec<(String, Value)>> = BTreeMap::new();
for (id, n) in of_type("VARIABLE_SET", "parentIndex") {
let modes = sorted_modes(n);
match self.canonical_collection(id).filter(|c| c != id) {
Some(canonical) => extra.entry(canonical).or_default().extend(modes),
None => {
order.push(json!(id));
let default = modes.first().map_or(Value::Null, |m| m.1["modeId"].clone());
let name = n.get("name").and_then(Kv::str).unwrap_or("Collection");
let modes: Vec<Value> = modes.into_iter().map(|m| m.1).collect();
collections.insert(id.clone(), json!({"id": id, "name": name, "modes": modes, "defaultModeId": default}));
}
}
}
for (canonical, more) in extra {
let Some(modes) = collections.get_mut(&canonical).and_then(|c| c.get_mut("modes")).and_then(Value::as_array_mut) else { continue };
for (_, m) in more {
if !modes.iter().any(|x| x["modeId"] == m["modeId"]) {
modes.push(m);
}
}
}
let mut variable_order = Vec::new();
let mut variables = Obj::new();
for (id, n) in of_type("VARIABLE", "sortPosition") {
if super::VarCanon::superseded(id) {
continue;
}
let Some(set) = n.get("variableSetID").and_then(|s| self.collection_id(s)).filter(|s| collections.contains_key(s)) else { continue };
let ty = n.get("variableResolvedType").and_then(Kv::str).unwrap_or("");
if !matches!(ty, "BOOLEAN" | "FLOAT" | "STRING" | "COLOR") {
continue;
}
let mut values = Obj::new();
for e in n.get("variableDataValues").and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default() {
if let (Some(mode), Some(val)) = (e.get("modeID").and_then(guid), e.get("variableData").and_then(variable_value)) {
values.insert(mode, val);
}
}
let name = n.get("name").and_then(Kv::str).unwrap_or("Variable");
let desc = n.get("description").and_then(Kv::str).unwrap_or("");
let mut var =
json!({"id": id, "name": name, "variableCollectionId": set, "resolvedType": ty, "valuesByMode": values, "description": desc});
// The properties pickers offer the variable for, under the REST names.
if let Some(scopes) = n.get("variableScopes") {
var["scopes"] = scopes.list().iter().filter_map(Kv::str).map(|s| if s == "STROKE" { "STROKE_COLOR" } else { s }).collect();
}
variable_order.push(json!(id));
variables.insert(id.clone(), var);
}
(!collections.is_empty())
.then(|| json!({"variableCollections": collections, "variables": variables, "collectionOrder": order, "variableOrder": variable_order}))
}
}
/// A collection node's modes in their order (`sortPosition`), as `(position, {modeId, name})`.
fn sorted_modes(n: &Kv) -> Vec<(String, Value)> {
let mut modes: Vec<(String, Value)> = n
.get("variableSetModes")
.map(Kv::list)
.unwrap_or_default()
.iter()
.filter_map(|m| {
let mode = json!({"modeId": m.get("id").and_then(guid)?, "name": m.get("name").and_then(Kv::str).unwrap_or("Mode")});
Some((m.get("sortPosition").and_then(Kv::str).unwrap_or("").to_string(), mode))
})
.collect();
modes.sort_by(|a, b| a.0.cmp(&b.0));
modes
}
@@ -1,37 +0,0 @@
//! Malformed image paint filters are skipped with a warning, and image hashes too long to name
//! an image aren't spelled out.
use super::*;
#[test]
fn malformed_non_finite_filter_is_skipped_with_warning() {
let paint = Kv::Object(vec![("type", Kv::Enum("IMAGE")), ("paintFilter", Kv::Object(vec![("exposure", Kv::Float(f32::NAN))]))]);
let mut warnings = Vec::new();
let value = super::paint(&paint, &mut |warning| warnings.push(warning)).unwrap();
assert!(value.get("filters").is_none(), "{value}");
assert!(warnings.iter().any(|warning| warning.contains("malformed image filter `exposure`")), "{warnings:?}");
}
#[test]
fn boolean_filter_values_are_malformed_even_when_false() {
for (field, value, expected_warning) in [
("exposure", Kv::Bool(true), "malformed image filter `exposure`"),
("exposure", Kv::Bool(false), "malformed image filter `exposure`"),
("mystery", Kv::Bool(false), "malformed unsupported image filter `mystery`"),
] {
let paint = Kv::Object(vec![("type", Kv::Enum("IMAGE")), ("paintFilter", Kv::Object(vec![(field, value)]))]);
let mut warnings = Vec::new();
let value = super::paint(&paint, &mut |warning| warnings.push(warning)).unwrap();
assert!(value.get("filters").is_none(), "{value}");
assert!(warnings.iter().any(|warning| warning.contains(expected_warning)), "{warnings:?}");
}
}
#[test]
fn an_oversized_image_hash_is_not_spelled_out() {
let paint = |len: usize| Kv::Object(vec![("type", Kv::Enum("IMAGE")), ("image", Kv::Object(vec![("hash", Kv::Bytes(vec![0xab; len]))]))]);
let image_ref = |len: usize| super::paint(&paint(len), &mut |_| {}).and_then(|p| p.get("imageRef").cloned());
assert_eq!(image_ref(20), Some(Value::String("ab".repeat(20))));
assert_eq!(image_ref(crate::fig::MAX_IMAGE_HASH), Some(Value::String("ab".repeat(crate::fig::MAX_IMAGE_HASH))));
assert_eq!(image_ref(crate::fig::MAX_IMAGE_HASH + 1), Some(Value::String(String::new())));
}
-930
View File
@@ -1,930 +0,0 @@
use super::component::{Entry, Inst, OverrideRow, prop_ref};
use super::connector::{ConnectorShape, connector_has_saved_label};
use super::layout::{layout_grid, tracks};
use super::paint::{effect, export_setting, has_bound_variables, paint, paint_bound, stroke};
use super::{
Affine, BTreeMap, BTreeSet, Kv, MAX_COMMANDS, MAX_DEPTH, Mapper, Obj, Value, View, copy_len, copy_num, copy_str, generated_record, guid, json,
matrix, network, num, rows, scaled, size,
};
/// How the layers of a resized container follow it when the file gives no derived layout for
/// them: a frame's by their constraints, from its main's size to its own; a group's scale with it.
#[derive(Clone, Copy)]
pub(super) enum Fit {
Frame { from: (f64, f64), to: (f64, f64) },
Scale(f64, f64),
}
impl Fit {
fn frame(from: Option<(f64, f64)>, to: Option<(f64, f64)>) -> Option<Self> {
let (from, to) = (from?, to?);
((from.0 - to.0).abs() > 1e-9 || (from.1 - to.1).abs() > 1e-9).then_some(Fit::Frame { from, to })
}
fn scale(from: Option<(f64, f64)>, to: Option<(f64, f64)>) -> Option<Self> {
let (from, to) = (from?, to?);
let k = |a: f64, b: f64| if a > 1e-9 { b / a } else { 1.0 };
let (kx, ky) = (k(from.0, to.0), k(from.1, to.1));
((kx - 1.0).abs() > 1e-9 || (ky - 1.0).abs() > 1e-9).then_some(Fit::Scale(kx, ky))
}
/// The size and transform (in the parent's coordinates) of a `w` × `h` layer at `t` with
/// constraints `h_con`, `v_con`. Like the editor's constraint solver, a layer keeps its
/// rotation and flip and stretches along its own axes.
fn apply(self, w: f64, h: f64, t: Affine, h_con: Option<&str>, v_con: Option<&str>) -> ((f64, f64), Affine) {
let [a, b, c, d, _, _] = t.as_coeffs();
let lin = Affine::new([a, b, c, d, 0.0, 0.0]);
let bb = figcraft_doc::geom::transform_rect_bbox(t, figcraft_doc::geom::Rect::new(0.0, 0.0, w, h));
// Where the new box goes: its top-left (constraints) or its centre (a group's scale).
let (kx, ky, x0, y0, centred) = match self {
Fit::Frame { from, to } => {
let axis = |con: Option<&str>, lo: f64, hi: f64, old: f64, new: f64| match con {
Some("MAX" | "FIXED_MAX") => (lo + new - old, hi + new - old),
Some("STRETCH") => (lo, hi + new - old),
Some("CENTER") => (lo + (new - old) / 2.0, hi + (new - old) / 2.0),
Some("SCALE") if old > 1e-9 => (lo * new / old, hi * new / old),
_ => (lo, hi),
};
let (x0, x1) = axis(h_con, bb.x0, bb.x1, from.0, to.0);
let (y0, y1) = axis(v_con, bb.y0, bb.y1, from.1, to.1);
let k = |new: f64, old: f64| if old > 1e-9 { (new / old).max(0.0) } else { 1.0 };
(k(x1 - x0, bb.width()), k(y1 - y0, bb.height()), x0, y0, false)
}
Fit::Scale(kx, ky) => (kx, ky, bb.center().x * kx, bb.center().y * ky, true),
};
let along = |x: f64, y: f64, fallback: f64| {
let len = x.hypot(y);
let k = (kx * x).hypot(ky * y) / len;
if len > 1e-12 && k.is_finite() { k } else { fallback }
};
let size = (w * along(a, b, kx), h * along(c, d, ky));
let nb = figcraft_doc::geom::transform_rect_bbox(lin, figcraft_doc::geom::Rect::new(0.0, 0.0, size.0, size.1));
let at = if centred { nb.center() } else { figcraft_doc::geom::Point::new(nb.x0, nb.y0) };
(size, Affine::translate((x0 - at.x, y0 - at.y)) * lin)
}
}
struct LayerChildren<'s, 'a> {
id: &'s str,
inst: Option<&'s Inst<'a>>,
base: &'s Kv<'a>,
child_space: Affine,
view: &'s View<'a>,
object: &'s mut Obj,
ty: &'s str,
rest_id: &'s str,
connector_snapshot: bool,
connector_terminal: bool,
depth: usize,
included: Option<&'s BTreeSet<String>>,
slot_content: Option<String>,
}
impl<'a> Mapper<'a> {
/// Layer `id` (inside instance `inst` when expanding one) and its subtree. `space`: from the
/// parent's coordinates to the enclosing frame's (groups and booleans place their children in
/// the frame's space, like the REST JSON). `parent`: the parent's view (grid tracks).
pub(crate) fn layer(
&mut self,
id: &str,
inst: Option<&Inst<'a>>,
space: Affine,
parent: Option<&View<'a>>,
fit: Option<Fit>,
depth: usize,
) -> Option<Value> {
self.layer_filtered(id, inst, space, parent, fit, depth, None)
}
/// [`Self::layer`], keeping only the layers in `included` (all when `None`) below the
/// top-level one: the cached library definitions of an internal canvas.
pub(super) fn layer_filtered(
&mut self,
id: &str,
inst: Option<&Inst<'a>>,
space: Affine,
parent: Option<&View<'a>>,
fit: Option<Fit>,
depth: usize,
included: Option<&BTreeSet<String>>,
) -> Option<Value> {
if included.is_some_and(|ids| !ids.contains(id)) {
return None;
}
if depth > MAX_DEPTH {
self.warn(format!("layers nested deeper than {MAX_DEPTH} levels were skipped"));
return None;
}
if self.count >= self.budget() {
let max = self.max_nodes;
self.warn(format!("the file has more than {max} layers; the rest were skipped"));
return None;
}
let base = *self.nodes.get(id)?;
let path = inst.map(|i| [i.path.as_slice(), &[id.to_string()]].concat());
let entries: &[OverrideRow<'a>] = match (inst, &path) {
(Some(i), Some(p)) => i.overrides.get(p).map(Vec::as_slice).unwrap_or_default(),
_ => &[],
};
// Inside a scaled instance the main's lengths are the factor times as long; the rows of
// the instance are at its scale already (#2091).
let scale = inst.map_or(1.0, |i| i.scale);
let mut view = View::of_scaled(base, scale).with(entries.iter().map(|e| (e.row, e.scale)));
// A layer of a resized instance the file gives no derived layout for follows its
// constraints from the main's geometry, then takes the size and place overridden on it
// (auto layout places the others, as in the editor).
let derived = entries.iter().any(|e| e.entry == Entry::Derived && (e.row.get("size").is_some() || e.row.get("transform").is_some()));
let stacked = parent.and_then(|p| p.str("stackMode")).is_some_and(|m| matches!(m, "HORIZONTAL" | "VERTICAL" | "GRID"))
&& view.str("stackPositioning") != Some("ABSOLUTE");
let main = View::of_scaled(base, scale);
// The size and place a row gives, at the scale of the row (a nested instance's own rows
// are at its enclosing main's scale).
let set = |k: &str| entries.iter().rev().filter(|e| e.entry != Entry::Stale).find_map(|e| e.row.get(k).map(|r| (r, e.scale)));
let set_size = || {
let (r, k) = set("size")?;
let (x, y) = size(r)?;
Some((scaled(x, k)?, scaled(y, k)?))
};
let set_transform = || {
let (r, k) = set("transform")?;
let m = matrix(r)?;
let [a, b, c, d, e, f] = m.as_coeffs();
Some(Affine::new([a, b, c, d, scaled(e, k)?, scaled(f, k)?]))
};
if let (Some(f), false, false, Some((w, h))) = (fit, derived, stacked, main.size()) {
let t = main.transform().unwrap_or_default();
let (fw, ft) = f.apply(w, h, t, view.str("horizontalConstraint"), view.str("verticalConstraint"));
view.fitted = Some((set_size().unwrap_or(fw), set_transform().unwrap_or(ft)));
} else if entries.iter().any(|e| e.entry == Entry::Stale && (e.row.get("size").is_some() || e.row.get("transform").is_some()))
&& let Some(s) = set_size().or_else(|| main.size())
{
view.fitted = Some((s, set_transform().or_else(|| main.transform()).unwrap_or_default()));
}
let rest_id = match (inst, &path) {
(Some(i), Some(p)) => format!("I{};{}", i.root, p.join(";")),
_ => id.to_string(),
};
let own = view.str("type").unwrap_or("");
let connector_snapshot = own == "CONNECTOR" && self.connector_snapshot_supported(&view);
// Only a connector with a saved label needs the frame wrapper; an unlabelled one stays a single vector.
let connector_terminal =
connector_snapshot && (!connector_has_saved_label(&view) || depth >= MAX_DEPTH || self.count.saturating_add(1) >= self.budget());
// A supported connector snapshot has its own note below instead of the generic stand-in one.
let stand = stand_in(own).filter(|_| !connector_snapshot);
// A label without a snapshot: the connector becomes a frame holding its line and its label.
let connector_label = own == "CONNECTOR" && !connector_snapshot && self.connector_label_fits(&view, depth);
let ty = if connector_label { "FRAME" } else { Self::mapped_type(own, &view, connector_snapshot, connector_terminal, stand)? };
// A whiteboard's sticky says it imports as a frame only when it can't be a native one.
let native_sticky = self.board && own == "STICKY";
if let Some((_, what)) = stand.filter(|_| own != "TEXT_PATH" && !native_sticky) {
self.warn(format!("{} layers import as {what}", own.to_ascii_lowercase().replace('_', " ")));
}
if connector_snapshot {
self.warn("connector layers import as static frame/vector snapshots with labels when present; live endpoint bindings are not retained");
}
self.count += 1;
if own == "SYMBOL" && inst.is_none() {
self.mapped_mains.insert(id.to_string());
}
let mut o = Obj::new();
o.insert("id".into(), json!(rest_id));
// Private provenance for the .fig importer; ordinary REST import ignores it.
o.insert(
crate::rest::FIG_SOURCE_FIELD.into(),
json!({
"origin": id,
"expanded": inst.is_some(),
"wireFrame": own == "FRAME",
"wireInstance": own == "INSTANCE",
"slotContent": inst.is_some_and(|i| i.slot_content),
// A size the instance gives this layer itself (kept when the instance relays out).
"sizeOverride": entries.iter().any(|e| e.entry == Entry::Override && e.row.get("size").is_some()),
}),
);
o.insert("type".into(), json!(ty));
copy_str(&mut o, &view, "name", "name");
self.common(&mut o, &view, ty, space, parent);
match ty {
"TEXT" => {
self.text(&mut o, &view);
if own == "TEXT_PATH" {
self.text_path(&mut o, &view);
}
}
"VECTOR" | "STAR" | "REGULAR_POLYGON" => self.geometry(&mut o, &view),
"ELLIPSE" => {
if let Some(a) = view.get("arcData") {
let arc: Obj =
["startingAngle", "endingAngle", "innerRadius"].iter().filter_map(|k| Some((k.to_string(), json!(num(a, k)?)))).collect();
o.insert("arcData".into(), Value::Object(arc));
}
}
"BOOLEAN_OPERATION" => {
if let Some(op) = view.str("booleanOperation") {
o.insert("booleanOperation".into(), json!(if op == "XOR" { "EXCLUDE" } else { op }));
}
}
_ => {}
}
let connector_kids = if own == "CONNECTOR" {
let shape = ConnectorShape { snapshot: connector_snapshot, terminal: connector_terminal, label: connector_label };
self.connector_layer(&mut o, &view, &rest_id, space, shape)
} else {
Vec::new()
};
if ty == "COMPONENT" || ty == "COMPONENT_SET" {
self.component(&mut o, &view, &rest_id);
}
self.prop_refs(&mut o, &view, inst);
let slot_content = if own == "FRAME" {
prop_ref(&view, "SLOT_CONTENT_ID").and_then(|def| inst.and_then(|i| i.slot_contents.get(&self.prop_root(&def)).cloned()))
} else {
None
};
if slot_content.is_some()
&& let Some(source) = o.get_mut(crate::rest::FIG_SOURCE_FIELD).and_then(Value::as_object_mut)
{
source.insert("slotOverride".into(), json!(true));
}
let child_space = if matches!(ty, "GROUP" | "BOOLEAN_OPERATION") { space * view.transform().unwrap_or_default() } else { Affine::IDENTITY };
let mut kids = self.map_layer_children(LayerChildren {
id,
inst,
base,
child_space,
view: &view,
object: &mut o,
ty,
rest_id: &rest_id,
connector_snapshot,
connector_terminal,
depth,
included,
slot_content,
});
kids.splice(0..0, connector_kids);
if stand.is_some_and(|s| s.0 == "FRAME") {
let native = if native_sticky { self.native_sticky(&mut o, &view, &rest_id, depth) } else { None };
if let Some(native) = native {
kids.splice(0..0, native);
} else if let Some(snapshot) = self.board_snapshot(&mut o, &view, own, &rest_id, depth) {
kids.splice(0..0, snapshot);
} else {
kids.splice(0..0, self.shape_with_text(&mut o, &view, own, &rest_id));
kids.extend(self.inner_text(&o, &view, &rest_id, own == "STICKY"));
}
if native_sticky && !o.contains_key("board") {
self.warn("sticky layers import as frames with their text");
}
}
if !kids.is_empty() {
o.insert("children".into(), Value::Array(kids));
}
Some(Value::Object(o))
}
fn map_layer_children(&mut self, context: LayerChildren<'_, 'a>) -> Vec<Value> {
let LayerChildren {
id,
inst,
base,
child_space,
view,
object: o,
ty,
rest_id,
connector_snapshot,
connector_terminal,
depth,
included,
slot_content,
} = context;
let mut kids = if connector_snapshot && !connector_terminal { self.connector_snapshot_children(view, rest_id) } else { Vec::new() };
if connector_terminal {
// The saved vector is flattened into the connector node when no child slot remains.
} else if ty == "INSTANCE" {
if let Some((sub, sym)) = self.instance(o, view, id, inst, rest_id) {
let main = self.nodes.get(&sym).map(|m| View::of_scaled(m, sub.scale));
// A scaled main's size is the factor's product, rounded to a float: it matches an
// instance of that size without a fit that would round the layers again.
let near = |a: f64, b: f64| (a - b).abs() <= 1e-4 * a.abs().max(b.abs()).max(1.0);
let fit = match (main.as_ref().and_then(View::size), view.size()) {
(Some(a), Some(b)) if sub.scale != 1.0 && near(a.0, b.0) && near(a.1, b.1) => None,
(a, b) => Fit::frame(a, b),
};
// An instance that records no auto layout of its own has its main's.
let view = match main {
Some(m) if view.get("stackMode").is_none() && m.get("stackMode").is_some() => {
let layered = m.over(view);
self.auto_layout(o, &layered);
layered
}
_ => view.clone(),
};
for c in self.children(&sym) {
// The main's branch can live outside the cached context being mapped.
kids.extend(self.layer_filtered(&c, Some(&sub), child_space, Some(&view), fit, depth + 1, None));
}
}
} else if let Some(content) = slot_content {
let mut content_inst = inst.cloned();
if let Some(content_inst) = &mut content_inst {
content_inst.slot_content = true;
}
for c in self.children(&content) {
kids.extend(self.layer_filtered(&c, content_inst.as_ref(), child_space, Some(view), None, depth + 1, included));
}
} else {
// Inside an instance a layer's children are its main's: they follow it from the main's
// size to its own.
let fit = match (inst, ty) {
(None, _) => None,
(Some(i), "GROUP" | "BOOLEAN_OPERATION") => Fit::scale(View::of_scaled(base, i.scale).size(), view.size()),
(Some(i), _) => Fit::frame(View::of_scaled(base, i.scale).size(), view.size()),
};
for c in self.children(id) {
kids.extend(self.layer_filtered(&c, inst, child_space, Some(view), fit, depth + 1, included));
}
}
kids
}
fn mapped_type<'s>(
own: &'s str,
view: &View<'a>,
connector_snapshot: bool,
connector_terminal: bool,
stand: Option<(&'static str, &'static str)>,
) -> Option<&'s str> {
match own {
"ROUNDED_RECTANGLE" => Some("RECTANGLE"),
"SYMBOL" => Some("COMPONENT"),
"FRAME" if view.flag("isStateGroup") == Some(true) => Some("COMPONENT_SET"),
"FRAME" if view.flag("resizeToFit") == Some(true) => Some("GROUP"),
"VARIABLE" | "VARIABLE_SET" => None,
"CONNECTOR" if connector_snapshot && connector_terminal => Some("VECTOR"),
"CONNECTOR" if connector_snapshot => Some("FRAME"),
t => Some(stand.map_or(t, |s| s.0)),
}
}
/// FigJam notes and shapes keep their paints and text on generated template children,
/// not on the outer node. Keep the saved layout as ordinary editable layers (including a
/// note's author), without applying the background's opacity or transform to the wrapper.
/// When no generated body survives, retain usable outer text alongside successful children.
fn board_snapshot(&mut self, o: &mut Obj, v: &View<'a>, own: &str, rest_id: &str, depth: usize) -> Option<Vec<Value>> {
if !matches!(own, "STICKY" | "SHAPE_WITH_TEXT") || v.get("nodeGenerationData").is_none() {
return None;
}
if v.get("derivedImmutableFrameData").and_then(|d| num(d, "version")) != Some(1.0) {
self.warn("note or shape generated layout is missing or unsupported; using its outer frame");
return None;
}
let mut children = Vec::new();
let mut has_body = false;
for local_id in 0..if own == "STICKY" { 3 } else { 2 } {
let style = generated_record(v, "nodeGenerationData", local_id);
let bounds = generated_record(v, "derivedImmutableFrameData", local_id);
let (Some(style), Some(bounds)) = (style, bounds) else {
if style.is_some() || bounds.is_some() {
self.warn("note or shape generated child is missing its style or bounds; child skipped");
}
continue;
};
let child = View::of(style).with([(bounds, 1.0)]);
let chars = own_text(&child);
if local_id != 0 && chars.is_none() {
continue;
}
if valid_size(child.get("size")).is_none() || child.transform().is_none() {
self.warn("note or shape generated child has invalid bounds; child skipped");
continue;
}
if self.count >= self.budget() || depth >= MAX_DEPTH {
self.warn("note or shape generated children exceed the layer or nesting limit; remaining children skipped");
break;
}
let mut layer = Obj::new();
let ty = if local_id == 0 { "RECTANGLE" } else { "TEXT" };
layer.insert("id".into(), json!(format!("{rest_id};generated{local_id}")));
layer.insert("type".into(), json!(ty));
layer.insert("name".into(), json!(chars.map_or_else(|| "Background".into(), |c| c.chars().take(40).collect::<String>())));
self.common(&mut layer, &child, ty, Affine::IDENTITY, None);
if local_id == 0 {
// Network decoding is only enabled for vectors; choose that type before
// geometry sees a saved network, then restore the rectangle if it is unusable.
if child.get("vectorData").is_some() {
layer.insert("type".into(), json!("VECTOR"));
}
self.geometry(&mut layer, &child);
let outlined = |layer: &Obj| ["fillGeometry", "strokeGeometry", "vectorNetworkRef"].iter().any(|key| layer.contains_key(*key));
if own == "SHAPE_WITH_TEXT" && !outlined(&layer) {
self.geometry(&mut layer, v);
}
if outlined(&layer) {
layer.insert("type".into(), json!("VECTOR"));
} else {
layer.insert("type".into(), json!("RECTANGLE"));
if own == "SHAPE_WITH_TEXT" && !matches!(v.str("shapeWithTextType"), None | Some("SQUARE" | "ROUNDED_RECTANGLE")) {
self.warn("shape generated outline is missing or damaged; using its rectangle");
}
}
o.insert("fills".into(), json!([]));
o.insert("strokes".into(), json!([]));
} else {
self.text(&mut layer, &child);
if let Some(Value::Object(style)) = layer.get_mut("style") {
style.insert("textAutoResize".into(), json!("NONE"));
}
}
self.count += 1;
has_body |= local_id == 1;
children.push(Value::Object(layer));
}
if depth < MAX_DEPTH && !has_body {
children.extend(self.inner_text(o, v, rest_id, own == "STICKY"));
}
Some(children)
}
/// A non-rectangular shape-with-text's shape, as a vector under its frame (which gives up its
/// paints to it).
fn shape_with_text(&mut self, o: &mut Obj, v: &View<'a>, own: &str, rest_id: &str) -> Option<Value> {
let shape = v.str("shapeWithTextType").unwrap_or("SQUARE");
let outlined = !v.list("fillGeometry").is_empty() || !v.list("strokeGeometry").is_empty();
if own != "SHAPE_WITH_TEXT" || matches!(shape, "SQUARE" | "ROUNDED_RECTANGLE") || !outlined || self.count >= self.budget() {
return None;
}
let mut s = Obj::new();
s.insert("id".into(), json!(format!("{rest_id};shape")));
s.insert("type".into(), json!("VECTOR"));
s.insert("name".into(), json!(shape.to_ascii_lowercase().replace('_', " ")));
for k in ["size", "strokeWeight", "strokeAlign", "strokeJoin", "strokeDashes"] {
if let Some(x) = o.get(k) {
s.insert(k.into(), x.clone());
}
}
for k in ["fills", "strokes"] {
s.insert(k.into(), o.insert(k.into(), json!([])).unwrap_or_else(|| json!([])));
}
s.insert("relativeTransform".into(), rows(Affine::IDENTITY));
self.geometry(&mut s, v);
if !s.contains_key("fillGeometry") && !s.contains_key("strokeGeometry") {
// Damaged outlines: the frame keeps its paints and box.
for k in ["fills", "strokes"] {
o.insert(k.into(), s.remove(k).unwrap_or_else(|| json!([])));
}
return None;
}
self.count += 1;
Some(Value::Object(s))
}
/// The text a stand-in frame holds (a sticky note's, a shape's, a table cell's) as a text
/// layer filling it, inset a little; centred unless it's a note.
fn inner_text(&mut self, o: &Obj, v: &View<'a>, rest_id: &str, note: bool) -> Option<Value> {
let chars = own_text(v)?;
if self.count >= self.budget() {
return None;
}
self.count += 1;
let size = |k: &str| o.get("size").and_then(|s| s.get(k)).and_then(Value::as_f64).unwrap_or(0.0);
let (w, h) = (size("x"), size("y"));
let pad = (w.min(h) / 4.0).clamp(0.0, 16.0);
let mut t = Obj::new();
t.insert("id".into(), json!(format!("{rest_id};text")));
t.insert("type".into(), json!("TEXT"));
t.insert("name".into(), json!(chars.chars().take(40).collect::<String>()));
t.insert("size".into(), json!({"x": (w - 2.0 * pad).max(1.0), "y": (h - 2.0 * pad).max(1.0)}));
t.insert("relativeTransform".into(), rows(Affine::translate((pad, pad))));
self.text(&mut t, v);
if let Some(Value::Object(st)) = t.get_mut("style") {
let (hor, ver) = if note { ("LEFT", "TOP") } else { ("CENTER", "CENTER") };
st.entry("textAlignHorizontal").or_insert_with(|| json!(hor));
st.entry("textAlignVertical").or_insert_with(|| json!(ver));
st.insert("textAutoResize".into(), json!("NONE"));
}
Some(Value::Object(t))
}
/// Fields every layer type shares.
pub(super) fn common(&mut self, o: &mut Obj, v: &View<'a>, ty: &str, space: Affine, parent: Option<&View<'a>>) {
for (from, to) in [("visible", "visible"), ("locked", "locked"), ("mask", "isMask")] {
if let Some(b) = v.flag(from) {
o.insert(to.into(), json!(b));
}
}
if let Some(m) = v.str("maskType") {
o.insert("maskType".into(), json!(if m == "OUTLINE" { "VECTOR" } else { m }));
}
copy_num(o, v, "opacity", "opacity");
if let Some(b) = v.str("blendMode").filter(|b| *b != "UNSET") {
o.insert("blendMode".into(), json!(b));
}
if let Some((x, y)) = v.size() {
o.insert("size".into(), json!({"x": x, "y": y}));
}
let t = v.transform().unwrap_or_default();
o.insert("relativeTransform".into(), rows(space * t));
// Paints, from the shared style when one is linked (the layer's own copy can be stale),
// unless the layer's own paints are bound to variables: those bindings must not be lost.
let mut styles = Obj::new();
let own = |k: &str| Some(v.list(k)).filter(|l| l.iter().any(paint_bound));
let fill_style = self.style(v, "styleIdForFill", "inheritFillStyleID", "fill", &mut styles);
let fills = own("fillPaints")
.unwrap_or_else(|| fill_style.map_or_else(|| v.list("fillPaints"), |s| s.get("fillPaints").map(Kv::list).unwrap_or_default()));
o.insert("fills".into(), Value::Array(fills.iter().filter_map(|p| paint(p, &mut |w| self.warn(w))).collect()));
let stroke_style = self.style(v, "styleIdForStrokeFill", "inheritFillStyleIDForStroke", "stroke", &mut styles);
let strokes = own("strokePaints")
.unwrap_or_else(|| stroke_style.map_or_else(|| v.list("strokePaints"), |s| s.get("fillPaints").map(Kv::list).unwrap_or_default()));
o.insert("strokes".into(), Value::Array(strokes.iter().filter_map(|p| paint(p, &mut |w| self.warn(w))).collect()));
let effect_style = self.style(v, "styleIdForEffect", "inheritEffectStyleID", "effect", &mut styles);
let (effects, effects_scale) =
effect_style.map_or_else(|| v.list_scaled("effects"), |s| v.styled_list(s, "effects", "styleIdForEffect", "inheritEffectStyleID"));
if !effects.is_empty() {
o.insert("effects".into(), Value::Array(effects.iter().filter_map(|e| effect(e, effects_scale)).collect()));
}
let grid_style = self.style(v, "styleIdForGrid", "inheritGridStyleID", "grid", &mut styles);
let (grids, grids_scale) =
grid_style.map_or_else(|| v.list_scaled("layoutGrids"), |s| v.styled_list(s, "layoutGrids", "styleIdForGrid", "inheritGridStyleID"));
if !grids.is_empty() {
o.insert("layoutGrids".into(), Value::Array(grids.iter().map(|g| layout_grid(g, grids_scale)).collect()));
}
if ty == "TEXT" {
self.style(v, "styleIdForText", "inheritTextStyleID", "text", &mut styles);
}
if !styles.is_empty() {
o.insert("styles".into(), Value::Object(styles));
}
stroke(o, v);
copy_len(o, v, "cornerRadius", "cornerRadius");
copy_num(o, v, "cornerSmoothing", "cornerSmoothing");
if v.flag("rectangleCornerRadiiIndependent") == Some(true) {
let r = |k: &str| v.len(k).unwrap_or(0.0);
o.insert(
"rectangleCornerRadii".into(),
json!([
r("rectangleTopLeftCornerRadius"),
r("rectangleTopRightCornerRadius"),
r("rectangleBottomRightCornerRadius"),
r("rectangleBottomLeftCornerRadius")
]),
);
}
let constraint = |k: &str, min: &'static str, max: &'static str, both: &'static str| match v.str(k) {
Some("CENTER") => "CENTER",
Some("MAX" | "FIXED_MAX") => max,
Some("STRETCH") => both,
Some("SCALE") => "SCALE",
_ => min,
};
o.insert(
"constraints".into(),
json!({
"horizontal": constraint("horizontalConstraint", "LEFT", "RIGHT", "LEFT_RIGHT"),
"vertical": constraint("verticalConstraint", "TOP", "BOTTOM", "TOP_BOTTOM"),
}),
);
if matches!(ty, "FRAME" | "COMPONENT" | "COMPONENT_SET" | "INSTANCE" | "SECTION") {
o.insert("clipsContent".into(), json!(!v.flag("frameMaskDisabled").unwrap_or(false)));
self.auto_layout(o, v);
}
child_layout(o, v, parent);
let exports: Vec<Value> = v.list("exportSettings").iter().map(export_setting).collect();
if !exports.is_empty() {
o.insert("exportSettings".into(), Value::Array(exports));
}
if let Some(b) = v.flag("sectionContentsHidden") {
o.insert("sectionContentsHidden".into(), json!(b));
}
self.prototype(o, v);
self.variable_bindings(o, v);
}
/// `fillGeometry` / `strokeGeometry` as SVG path data, and star/polygon parameters.
pub(super) fn geometry(&mut self, o: &mut Obj, v: &View<'a>) {
for key in ["fillGeometry", "strokeGeometry"] {
let mut out = Vec::new();
for p in v.list(key) {
let Some(blob) = num(p, "commandsBlob").and_then(|i| self.blobs.get(i as usize)).copied() else { continue };
let Some(d) = svg_path(blob) else {
self.warn("damaged vector geometry was skipped");
continue;
};
if d.is_empty() {
continue;
}
let winding = if p.get("windingRule").and_then(Kv::str) == Some("ODD") { "EVENODD" } else { "NONZERO" };
out.push(json!({"path": d, "windingRule": winding}));
}
if !out.is_empty() {
o.insert(key.into(), Value::Array(out));
}
}
let vector = o.get("type").and_then(Value::as_str) == Some("VECTOR");
if vector && !self.network(o, v) && !o.contains_key("fillGeometry") && !o.contains_key("strokeGeometry") && v.str("type") != Some("CONNECTOR")
{
self.warn("vectors without geometry are empty");
}
if !vector {
copy_num(o, v, "count", "pointCount");
copy_num(o, v, "starInnerScale", "innerRadius");
}
}
/// The editable network of `vectorData` as `vectorNetwork` (the REST importer's extension),
/// scaled to the layer's size, with per-point corner radii, handle mirroring and end caps.
/// False when there's none or it's damaged (the outlines stand in).
fn network(&mut self, o: &mut Obj, v: &View<'a>) -> bool {
let Some((vd, radius_scale)) = v.find("vectorData") else { return false };
let Some(blob_id) = num(vd, "vectorNetworkBlob").map(|i| i as usize) else { return false };
let Some(blob) = self.blobs.get(blob_id).copied() else { return false };
let Some(net) = network::decode(blob) else {
self.warn("damaged vector networks import as outlines");
return false;
};
let styles: BTreeMap<u64, &Kv> =
vd.get("styleOverrideTable").map(Kv::list).unwrap_or_default().iter().filter_map(|s| Some((num(s, "styleID")? as u64, s))).collect();
let mut has_uninterpreted = false;
let mut has_missing = false;
for segment in &net.segments {
if segment.style == 0 {
continue;
}
if styles.contains_key(&u64::from(segment.style)) {
has_uninterpreted = true;
} else {
has_missing = true;
}
}
if has_uninterpreted {
self.warn("style settings of some vector segments aren't supported; those segments use the layer's stroke");
}
if has_missing {
self.warn("some vector segments refer to style settings missing from the file; they use the layer's stroke");
}
// An open path shows its fill, which regions can't hold: the outline keeps the look.
let fills_shown =
o.get("fills").and_then(Value::as_array).is_some_and(|f| f.iter().any(|p| p.get("visible").and_then(Value::as_bool) != Some(false)));
if net.segments.is_empty() || (net.regions.is_empty() && fills_shown && o.contains_key("fillGeometry")) {
return false;
}
let style = |id: u32| styles.get(&u64::from(id)).copied().filter(|_| id != 0);
let scale = |k: &str| {
let size = v.size().map(|s| if k == "x" { s.0 } else { s.1 });
let norm = vd.get("normalizedSize").and_then(|s| num(s, k));
size.zip(norm).filter(|(s, n)| s.is_finite() && *n > 1e-9).map_or(1.0, |(s, n)| s / n)
};
let (sx, sy) = (scale("x"), scale("y"));
// A point whose style doesn't set a radius takes the layer's, as the editor draws it.
let layer_radius = v.len("cornerRadius");
let vertices: Vec<Value> = net
.vertices
.iter()
.map(|p| {
let mut out = json!({"x": p.x * sx, "y": p.y * sy});
let st = style(p.style);
// Point radii are lengths in this row's space, unlike the normalized positions.
// An override/derived row already carries the instance's scale.
if let Some(r) = st.and_then(|s| num(s, "cornerRadius")).and_then(|r| scaled(r, radius_scale)).or(layer_radius).filter(|r| *r > 0.0) {
out["radius"] = json!(r);
}
match st.and_then(|s| s.get("handleMirroring")).and_then(Kv::str) {
Some("ANGLE") => out["mirror"] = json!("angle"),
Some("ANGLE_AND_LENGTH") => out["mirror"] = json!("angleAndLength"),
Some("NONE") => out["mirror"] = json!("none"),
_ => {}
}
if let Some(join @ ("MITER" | "BEVEL" | "ROUND")) = st.and_then(|s| s.get("strokeJoin")).and_then(Kv::str) {
out["join"] = json!(join.to_ascii_lowercase());
}
out
})
.collect();
let segments: Vec<Value> =
net.segments.iter().map(|s| json!({"a": s.a, "b": s.b, "ta": [s.ta.0 * sx, s.ta.1 * sy], "tb": [s.tb.0 * sx, s.tb.1 * sy]})).collect();
let regions: Vec<Value> = net
.regions
.iter()
.map(|r| {
let mut region = json!({"loops": r.loops, "winding": if r.non_zero { "nonZero" } else { "evenOdd" }});
if r.style != 0 {
match style(r.style) {
Some(s) => {
let fills = s.get("fillPaints").and_then(|own_fills| {
if own_fills.list().iter().any(paint_bound) {
return Some(own_fills);
}
let linked_style = self
.style_link(&View::of(s), "styleIdForFill", "inheritFillStyleID")
.and_then(|id| self.nodes.get(&id).copied())
.filter(|style| style.get("styleType").and_then(Kv::str) == Some("FILL"));
match linked_style {
Some(style) => style.get("fillPaints"),
None => Some(own_fills),
}
});
if let Some(fills) = fills {
if let Kv::Array(source_paints) = fills {
let paint_cost = source_paints.iter().fold(source_paints.len(), |cost, paint| {
cost.saturating_add(paint.get("stops").map(Kv::list).unwrap_or_default().len())
});
let oversized_stops =
source_paints.iter().any(|paint| paint.get("stops").map(Kv::list).unwrap_or_default().len() > 256);
if source_paints.len() > 256 || oversized_stops || self.region_paint_elements.saturating_add(paint_cost) > 200_000
{
self.warn("vector region paint data exceeded import limits and was skipped");
return region;
}
self.region_paint_elements += paint_cost;
let paints: Vec<Value> = source_paints.iter().filter_map(|p| paint(p, &mut |w| self.warn(w))).collect();
if paints.len() != source_paints.len() {
self.warn("invalid vector region paints were skipped");
}
if paints.iter().any(has_bound_variables) {
self.warn("variable-bound vector region paints keep resolved appearance; live bindings were left out");
}
if s.get("styleIdForFill").is_some() {
self.warn("linked vector region fill styles keep resolved appearance; live style links were left out");
}
region["_figRegionFills"] = Value::Array(paints);
} else {
self.warn("invalid vector region fill paint list; layer fill used");
}
}
}
None => self.warn("vector region references a missing fill style; layer fill used"),
}
}
region
})
.collect();
// An absent layer join is the source default.
let layer_join = v.str("strokeJoin").unwrap_or("MITER");
let point_join = |p: &network::Vertex| style(p.style).and_then(|s| s.get("strokeJoin"));
let known = |j: &str| matches!(j, "MITER" | "BEVEL" | "ROUND");
let unresolved_style = net.vertices.iter().any(|p| p.style != 0 && style(p.style).is_none());
let unknown_join = net.vertices.iter().any(|p| point_join(p).is_some_and(|j| !j.str().is_some_and(known)));
let unsupported_join = net.vertices.iter().any(|p| point_join(p).and_then(Kv::str).is_some_and(|j| known(j) && j != layer_join));
if unresolved_style {
self.warn("some vector points refer to point settings missing from the file; they use the layer's joins and plain corners");
}
if unknown_join {
self.warn("vector points with unknown stroke joins use the layer join");
}
if unsupported_join && (o.contains_key("strokeDashes") || o.contains_key("strokeWidthProfile")) {
self.warn("point stroke joins were preserved; dashed or variable-width strokes render with the layer join");
}
// The stroke starts at the first end point and stops at the last (as the editor draws it).
let mut degree = vec![0usize; net.vertices.len()];
for s in &net.segments {
for i in [s.a, s.b] {
if let Some(d) = degree.get_mut(i) {
*d += 1;
}
}
}
let ends: Vec<usize> = (0..degree.len()).filter(|i| degree.get(*i) == Some(&1)).collect();
let cap =
|i: Option<&usize>| i.and_then(|i| net.vertices.get(*i)).and_then(|p| style(p.style)).and_then(|s| s.get("strokeCap")).and_then(Kv::str);
let (first, last) = (cap(ends.first()), cap(ends.last()));
if first.is_some() || last.is_some() {
let node = v.str("strokeCap").unwrap_or("NONE");
o.insert("strokeCapStart".into(), json!(cap_name(first.unwrap_or(node))));
o.insert("strokeCapEnd".into(), json!(cap_name(last.unwrap_or(node))));
}
// Every instance of a main shows the same network: it is written once and shared.
let mut entry = json!({});
entry["vertices"] = Value::Array(vertices);
entry["segments"] = Value::Array(segments);
entry["regions"] = Value::Array(regions.clone());
if regions.iter().any(|r| r.get("_figRegionFills").is_some()) {
o.insert("vectorNetwork".into(), entry);
return true;
}
// A resized instance and a K-scaled one can have the same box but different radii.
let key =
(blob_id, sx.to_bits(), sy.to_bits(), std::ptr::from_ref(vd) as usize, radius_scale.to_bits(), layer_radius.unwrap_or(0.0).to_bits());
let id = match self.network_ids.get(&key) {
Some(id) => *id,
None => {
self.networks.push(crate::rest::network(&entry));
self.network_ids.insert(key, self.networks.len() - 1);
self.networks.len() - 1
}
};
o.insert("vectorNetworkRef".into(), json!(id));
true
}
}
/// The text a layer that isn't a text layer carries (a note's, a shape's), unless blank.
pub(super) fn own_text<'a>(v: &View<'a>) -> Option<&'a str> {
v.get("textData").and_then(|t| t.get("characters")).and_then(Kv::str).filter(|c| !c.trim().is_empty())
}
pub(super) fn valid_size(value: Option<&Kv<'_>>) -> Option<(f64, f64)> {
let x = num(value?, "x")?;
let y = num(value?, "y")?;
(x.is_finite() && y.is_finite() && x >= 0.0 && y >= 0.0).then_some((x, y))
}
/// The REST name of a stroke cap.
pub(super) fn cap_name(cap: &str) -> &str {
match cap {
"ARROW_LINES" => "LINE_ARROW",
"ARROW_EQUILATERAL" => "TRIANGLE_ARROW",
c => c,
}
}
/// The layer kind (and how the warning names it) that a layer the editor has no counterpart for
/// imports as: frames for notes, shapes, tables, slides and the like (their text as a text layer),
/// vectors for connectors and marker strokes, plain text for text on a path.
fn stand_in(ty: &str) -> Option<(&'static str, &'static str)> {
Some(match ty {
"STICKY"
| "SHAPE_WITH_TEXT"
| "CODE_BLOCK"
| "TABLE"
| "TABLE_CELL"
| "STAMP"
| "MEDIA"
| "WIDGET"
| "SLIDE"
| "SLIDE_GRID"
| "SLIDE_ROW"
| "INTERACTIVE_SLIDE_ELEMENT"
| "MODULE" => ("FRAME", "frames with their text"),
"CONNECTOR" | "HIGHLIGHT" | "WASHI_TAPE" => ("VECTOR", "vectors"),
"TEXT_PATH" => ("TEXT", "text off its path"),
_ => return None,
})
}
/// How a layer sizes and sits in an auto layout parent.
fn child_layout(o: &mut Obj, v: &View, parent: Option<&View>) {
copy_num(o, v, "stackChildPrimaryGrow", "layoutGrow");
if v.str("stackChildAlignSelf") == Some("STRETCH") {
o.insert("layoutAlign".into(), json!("STRETCH"));
}
if v.str("stackPositioning") == Some("ABSOLUTE") {
o.insert("layoutPositioning".into(), json!("ABSOLUTE"));
}
for (k, w, h) in [("minSize", "minWidth", "minHeight"), ("maxSize", "maxWidth", "maxHeight")] {
if let Some((val, scale)) = v.find(k).and_then(|(s, scale)| Some((s.get("value")?, scale))) {
for (axis, to) in [("x", w), ("y", h)] {
if let Some(x) = num(val, axis).and_then(|x| scaled(x, scale)).filter(|x| *x > 0.0 && *x < 1e9) {
o.insert(to.into(), json!(x));
}
}
}
}
if let Some(p) = parent.filter(|p| p.str("stackMode") == Some("GRID")) {
for (anchor, list, to) in [("gridColumnAnchor", "gridColumns", "gridColumnAnchorIndex"), ("gridRowAnchor", "gridRows", "gridRowAnchorIndex")]
{
if let Some(i) = v.get(anchor).and_then(guid).and_then(|g| tracks(p, list).iter().position(|t| *t == g)) {
o.insert(to.into(), json!(i));
}
}
copy_num(o, v, "gridColumnSpan", "gridColumnSpan");
copy_num(o, v, "gridRowSpan", "gridRowSpan");
}
}
/// SVG path data of a geometry blob: commands `0` close, `1` move, `2` line, `3` quadratic and
/// `4` cubic, each followed by its points as little-endian `f32` pairs. `None` when damaged.
pub(super) fn svg_path(b: &[u8]) -> Option<String> {
let mut out = String::new();
let mut i = 0usize;
let mut n = 0usize;
while i < b.len() {
n += 1;
if n > MAX_COMMANDS {
return None;
}
let (letter, points) = match *b.get(i)? {
0 => ('Z', 0),
1 => ('M', 1),
2 => ('L', 1),
3 => ('Q', 2),
4 => ('C', 3),
_ => return None,
};
i += 1;
if !out.is_empty() {
out.push(' ');
}
out.push(letter);
for _ in 0..points * 2 {
let bytes: [u8; 4] = b.get(i..i + 4)?.try_into().ok()?;
let x = f32::from_le_bytes(bytes);
if !x.is_finite() {
return None;
}
out.push_str(&format!(" {x}"));
i += 4;
}
}
Some(out)
}
-190
View File
@@ -1,190 +0,0 @@
use super::paint::{bound, color};
use super::{BTreeMap, BTreeSet, Kv, MAX_GRID_TRACK_VALUE, MAX_GRID_TRACKS, Mapper, Obj, Value, View, copy_len, guid, json, num, scaled};
impl<'a> Mapper<'a> {
pub(super) fn auto_layout(&mut self, o: &mut Obj, v: &View<'a>) {
let Some(mode) = v.str("stackMode").filter(|m| matches!(*m, "HORIZONTAL" | "VERTICAL" | "GRID")) else { return };
o.insert("layoutMode".into(), json!(mode));
copy_len(o, v, "stackSpacing", "itemSpacing");
copy_len(o, v, "stackCounterSpacing", "counterAxisSpacing");
if v.str("stackWrap") == Some("WRAP") {
o.insert("layoutWrap".into(), json!("WRAP"));
}
if v.str("stackCounterAlignContent") == Some("SPACE_BETWEEN") {
o.insert("counterAxisAlignContent".into(), json!("SPACE_BETWEEN"));
}
let (h, vert) = (v.len("stackHorizontalPadding").unwrap_or(0.0), v.len("stackVerticalPadding").unwrap_or(0.0));
o.insert("paddingLeft".into(), json!(h));
o.insert("paddingTop".into(), json!(vert));
o.insert("paddingRight".into(), json!(v.len("stackPaddingRight").unwrap_or(h)));
o.insert("paddingBottom".into(), json!(v.len("stackPaddingBottom").unwrap_or(vert)));
let main = match v.str("stackPrimaryAlignItems").or_else(|| v.str("stackJustify")) {
Some("CENTER") => "CENTER",
Some("MAX") => "MAX",
Some("SPACE_AROUND") => "SPACE_AROUND",
// `SPACE_EVENLY` is the format's older name for equal gaps without edge space.
Some("SPACE_EVENLY_CSS") => "SPACE_EVENLY",
Some("SPACE_BETWEEN" | "SPACE_EVENLY") => "SPACE_BETWEEN",
_ => "MIN",
};
o.insert("primaryAxisAlignItems".into(), json!(main));
let cross = match v.str("stackCounterAlignItems").or_else(|| v.str("stackAlign")) {
Some("CENTER") => "CENTER",
Some("MAX") => "MAX",
Some("BASELINE") => "BASELINE",
_ => "MIN",
};
o.insert("counterAxisAlignItems".into(), json!(cross));
let sizing = |k: &str| if v.str(k).is_some_and(|s| s.starts_with("RESIZE_TO_FIT")) { "AUTO" } else { "FIXED" };
// A flow that records no sizing for its primary axis sizes it by its content: auto layout
// did before that axis could be fixed.
let primary = if v.get("stackPrimarySizing").is_none() && mode != "GRID" { "AUTO" } else { sizing("stackPrimarySizing") };
o.insert("primaryAxisSizingMode".into(), json!(primary));
o.insert("counterAxisSizingMode".into(), json!(sizing("stackCounterSizing")));
if let Some(b) = v.flag("bordersTakeSpace") {
o.insert("strokesIncludedInLayout".into(), json!(b));
}
if let Some(b) = v.flag("stackReverseZIndex") {
o.insert("itemReverseZIndex".into(), json!(b));
}
if mode == "GRID" {
copy_len(o, v, "gridColumnGap", "gridColumnGap");
copy_len(o, v, "gridRowGap", "gridRowGap");
o.insert("gridColumnCount".into(), json!(tracks(v, "gridColumns").len()));
o.insert("gridRowCount".into(), json!(tracks(v, "gridRows").len()));
let track_data = [("columnTracks", "gridColumns", "gridColumnsSizing"), ("rowTracks", "gridRows", "gridRowsSizing")]
.into_iter()
.filter_map(|(out_key, order_key, sizing_key)| {
self.map_grid_tracks(v, order_key, sizing_key).map(|mapped| (out_key.to_string(), mapped))
})
.collect::<Obj>();
if !track_data.is_empty() {
let source = o.entry(crate::rest::FIG_SOURCE_FIELD).or_insert_with(|| json!({}));
if let Some(source) = source.as_object_mut() {
source.insert("gridTracks".into(), Value::Object(track_data));
}
}
}
}
/// Track sizing maps are keyed by GUID; their order comes from the separate track list.
fn map_grid_tracks(&mut self, v: &View<'a>, order_key: &str, sizing_key: &str) -> Option<Value> {
let track_entries = v.get(order_key).and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default();
let sizes = v.get(sizing_key).and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default();
if track_entries.len() > MAX_GRID_TRACKS {
self.warn("grid track list exceeds 1000 entries; the tracks use the default equal sizing");
return None;
}
if sizes.is_empty() {
return None;
}
if sizes.len() > MAX_GRID_TRACKS {
self.warn("grid track sizing list exceeds 1000 entries; the tracks use the default equal sizing");
return None;
}
let ordered = tracks(v, order_key);
let ordered_ids: BTreeSet<&str> = ordered.iter().map(String::as_str).collect();
let mut size_by_id: BTreeMap<String, Option<&Kv<'a>>> = BTreeMap::new();
for entry in sizes {
let Some(id) = entry.get("id").and_then(guid) else {
self.warn("stale or invalid grid track sizing entries were ignored");
continue;
};
if !ordered_ids.contains(id.as_str()) {
self.warn("stale or invalid grid track sizing entries were ignored");
continue;
}
match size_by_id.entry(id) {
std::collections::btree_map::Entry::Vacant(slot) => {
slot.insert(Some(entry));
}
std::collections::btree_map::Entry::Occupied(mut slot) => {
slot.insert(None);
self.warn("duplicate grid track sizing entries use equal tracks");
}
}
}
let mut mapped = Vec::with_capacity(ordered.len());
for id in ordered {
if let Some(Some(entry)) = size_by_id.get(&id) {
match grid_track_sizing(entry.get("trackSize")) {
Some(track) => mapped.push(track),
None => {
self.warn("grid track sizing is unsupported or malformed; affected tracks use equal sizing");
mapped.push(json!({"sizing": "fill", "value": 1.0}));
}
}
} else {
mapped.push(json!({"sizing": "fill", "value": 1.0}));
}
}
Some(Value::Array(mapped))
}
}
/// The track ids of a grid's columns or rows, in order.
pub(super) fn tracks(v: &View, key: &str) -> Vec<String> {
let mut t: Vec<(&str, String)> = v
.get(key)
.and_then(|m| m.get("entries"))
.map(Kv::list)
.unwrap_or_default()
.iter()
.filter_map(|e| Some((e.get("position").and_then(Kv::str).unwrap_or(""), e.get("id").and_then(guid)?)))
.collect();
t.sort();
t.into_iter().map(|e| e.1).collect()
}
/// Convert only unconstrained tracks, whose min and max sizing functions agree.
fn grid_track_sizing(size: Option<&Kv<'_>>) -> Option<Value> {
let size = size?;
let min = size.get("minSizing")?;
let max = size.get("maxSizing")?;
let min_type = min.get("type").and_then(Kv::str)?;
let max_type = max.get("type").and_then(Kv::str)?;
if min_type != max_type {
return None;
}
match min_type {
"FIXED" => {
let px = num(min, "value")?;
let max_px = num(max, "value")?;
(px == max_px && px.is_finite() && (0.0..=MAX_GRID_TRACK_VALUE).contains(&px)).then(|| json!({"sizing": "fixed", "value": px}))
}
"FLEX" => {
let fr = num(min, "value")?;
let max_fr = num(max, "value")?;
(fr == max_fr && fr.is_finite() && fr > 0.0 && fr <= MAX_GRID_TRACK_VALUE).then(|| json!({"sizing": "fill", "value": fr}))
}
"HUG" => Some(json!({"sizing": "hug", "value": 0.0})),
_ => None,
}
}
/// A layout grid; `k`: the factor its lengths take (a scaled instance's layer).
pub(super) fn layout_grid(g: &Kv, k: f64) -> Value {
let pattern = match (g.get("pattern").and_then(Kv::str), g.get("axis").and_then(Kv::str)) {
(Some("GRID"), _) => "GRID",
(_, Some("Y")) => "ROWS",
_ => "COLUMNS",
};
let mut o = json!({"pattern": pattern, "alignment": g.get("type").and_then(Kv::str).unwrap_or("STRETCH")});
for (from, to) in [("sectionSize", "sectionSize"), ("numSections", "count"), ("gutterSize", "gutterSize"), ("offset", "offset")] {
let length = from != "numSections";
if let Some(x) = num(g, from).and_then(|x| scaled(x, if length { k } else { 1.0 })) {
o[to] = json!(x);
}
}
if let Some(c) = g.get("color") {
o["color"] = color(c);
}
if let Some(b) = g.get("visible").and_then(Kv::bool) {
o["visible"] = json!(b);
}
let vars = [("numSectionsVar", "count"), ("offsetVar", "offset"), ("sectionSizeVar", "sectionSize"), ("gutterSizeVar", "gutterSize")];
if let Some(b) = bound(Some(g), &vars) {
o["boundVariables"] = b;
}
o
}
-337
View File
@@ -1,337 +0,0 @@
use super::layer::cap_name;
use super::variables::alias;
use super::{Affine, Kv, Mapper, NO_STYLE, Obj, Point, Value, View, copy_len, copy_str, guid, json, matrix, num, rows, scaled, vec2};
fn filter_number(value: &Kv) -> Option<f64> {
let number = match value {
Kv::Float(value) => f64::from(*value),
Kv::Int(value) => *value as f64,
Kv::Uint(value) => *value as f64,
_ => return None,
};
number.is_finite().then_some(number)
}
pub(super) fn color(c: &Kv) -> Value {
let ch = |k: &str| num(c, k).unwrap_or(0.0);
json!({"r": ch("r"), "g": ch("g"), "b": ch("b"), "a": num(c, "a").unwrap_or(1.0)})
}
impl<'a> Mapper<'a> {
/// The style a layer links in `key`, by guid or by library asset (the file's copy of a library
/// style), or in the older `legacy` field when `key` names none. An explicit NO_STYLE guid in
/// `key` with no asset is a detach and wins over the legacy field.
pub(super) fn style_link(&self, v: &View, key: &str, legacy: &str) -> Option<String> {
let fallback = || v.get(legacy).and_then(guid).filter(|g| g != NO_STYLE);
let Some(link) = v.get(key) else { return fallback() };
match (self.resource_id(link), link.get("guid").and_then(guid)) {
(Some(id), _) => Some(id),
(None, Some(_)) => None,
(None, None) => fallback(),
}
}
/// The style node a layer links in `key` (or the older `legacy` field), recorded for the REST
/// `styles` map under `slot`.
/// A link to a style the file doesn't hold leaves the layer its own values (the colours it
/// stored with it), with a note.
pub(super) fn style(&mut self, v: &View<'a>, key: &str, legacy: &str, slot: &str, out: &mut Obj) -> Option<&'a Kv<'a>> {
let found = self.style_link(v, key, legacy).and_then(|id| Some((self.nodes.get(&id).copied()?, id)));
let Some((node, id)) = found.filter(|(n, _)| n.get("styleType").and_then(Kv::str).is_some()) else {
let named = |l: &Kv| l.get("assetRef").is_some() || l.get("guid").and_then(guid).is_some_and(|g| g != NO_STYLE);
if v.get(key).is_some_and(named) || v.get(legacy).and_then(guid).is_some_and(|g| g != NO_STYLE) {
self.warn("links to shared styles that aren't in the file kept the layers' own values");
}
return None;
};
out.insert(slot.into(), json!(id));
if !self.styles.contains_key(&id) {
let name = node.get("name").and_then(Kv::str).unwrap_or("Style");
let desc = node.get("styleDescription").or_else(|| node.get("description")).and_then(Kv::str).unwrap_or("");
self.styles.insert(id, json!({"name": name, "description": desc}));
}
Some(node)
}
}
pub(super) fn stroke(o: &mut Obj, v: &View) {
copy_len(o, v, "strokeWeight", "strokeWeight");
if let Some(a) = v.str("strokeAlign").filter(|a| *a != "OFFSET") {
o.insert("strokeAlign".into(), json!(a));
}
copy_str(o, v, "strokeJoin", "strokeJoin");
if let Some(cap) = v.str("strokeCap") {
o.insert("strokeCap".into(), json!(cap_name(cap)));
}
// A layer written as the outline of its stroke keeps what it was as plugin data
// (`outlinedStroke`, our extension of the file JSON).
let (id, key) = crate::fig::export::STASH;
let kept = v.list("pluginData").iter().find(|p| p.get("pluginID").and_then(Kv::str) == Some(id) && p.get("key").and_then(Kv::str) == Some(key));
if let Some(original) = kept.and_then(|p| p.get("value")).and_then(Kv::str).and_then(|s| serde_json::from_str::<Value>(s).ok()) {
o.insert("outlinedStroke".into(), original);
}
// A variable width: each point's reach on the two sides of the path (shares of the stroke
// weight), as `[t, scale, bias]` (`strokeWidthProfile`, our extension of the file JSON).
let profile: Vec<Value> = v
.list("variableWidthPoints")
.iter()
.filter_map(|p| {
let (left, right) = (num(p, "ascent")?.max(0.0), num(p, "descent")?.max(0.0));
let bias = if left + right > 0.0 { (right - left) / (left + right) } else { 0.0 };
Some(json!([num(p, "position")?, left + right, bias]))
})
.collect();
if !profile.is_empty() {
o.insert("strokeWidthProfile".into(), Value::Array(profile));
}
let (dash_list, dash_scale) = v.list_scaled("dashPattern");
let dashes: Vec<f64> = dash_list.iter().filter_map(Kv::f64).filter_map(|d| scaled(d, dash_scale)).collect();
if !dashes.is_empty() {
o.insert("strokeDashes".into(), json!(dashes));
}
// A miter limit L bevels joins sharper than 2·asin(1/L).
if let Some(l) = v.num("miterLimit").filter(|l| *l > 1.0) {
o.insert("strokeMiterAngle".into(), json!((2.0 * (1.0 / l).asin()).to_degrees()));
}
if v.flag("borderStrokeWeightsIndependent") == Some(true) {
// A side that isn't written has no border.
let side = |k: &str| v.len(k).unwrap_or(0.0);
o.insert(
"individualStrokeWeights".into(),
json!({"top": side("borderTopWeight"), "right": side("borderRightWeight"), "bottom": side("borderBottomWeight"), "left": side("borderLeftWeight")}),
);
}
}
/// Whether a paint's colour or one of its gradient stops is bound to a variable.
pub(super) fn paint_bound(p: &Kv) -> bool {
alias(p.get("colorVar")).is_some() || p.get("stopsVar").map(Kv::list).unwrap_or_default().iter().any(|s| alias(s.get("colorVar")).is_some())
}
pub(crate) fn paint(p: &Kv, warn: &mut dyn FnMut(String)) -> Option<Value> {
let ty = p.get("type").and_then(Kv::str)?;
let mut o = Obj::new();
o.insert("type".into(), json!(ty));
if let Some(x) = num(p, "opacity") {
o.insert("opacity".into(), json!(x));
}
if let Some(b) = p.get("visible").and_then(Kv::bool) {
o.insert("visible".into(), json!(b));
}
if let Some(b) = p.get("blendMode").and_then(Kv::str) {
o.insert("blendMode".into(), json!(b));
}
let t = p.get("transform").and_then(matrix);
match ty {
"SOLID" => {
if let Some(c) = p.get("color") {
o.insert("color".into(), color(c));
}
if let Some(var) = alias(p.get("colorVar")) {
o.insert("boundVariables".into(), json!({"color": {"type": "VARIABLE_ALIAS", "id": var}}));
}
}
_ if ty.starts_with("GRADIENT_") => {
// The paint transform maps the layer's unit box to gradient space, where a linear
// gradient runs from (0, ½) to (1, ½) and the others are centred on (½, ½).
let inv = t.filter(|t| t.determinant().abs() > 1e-12).map_or(Affine::IDENTITY, Affine::inverse);
let pts = if ty == "GRADIENT_LINEAR" { [(0.0, 0.5), (1.0, 0.5), (0.0, 1.0)] } else { [(0.5, 0.5), (1.0, 0.5), (0.5, 1.0)] };
let handles: Vec<Value> = pts
.iter()
.map(|(x, y)| {
let q = inv * Point::new(*x, *y);
json!({"x": q.x, "y": q.y})
})
.collect();
o.insert("gradientHandlePositions".into(), Value::Array(handles));
// Stops bound to variables are listed again, each with its variable.
let vars = p.get("stopsVar").map(Kv::list).unwrap_or_default();
let stops: Vec<Value> = p
.get("stops")
.map(Kv::list)
.unwrap_or_default()
.iter()
.enumerate()
.map(|(i, s)| {
let mut stop = json!({"position": num(s, "position").unwrap_or(0.0), "color": s.get("color").map_or(Value::Null, color)});
if let Some(b) = bound(vars.get(i), &[("colorVar", "color")]) {
stop["boundVariables"] = b;
}
stop
})
.collect();
o.insert("gradientStops".into(), Value::Array(stops));
}
"IMAGE" => {
// A hash longer than any the file's images can be named by resolves to nothing: it
// isn't spelled out, which a shared style would repeat for every layer using it.
let hash: String = p
.get("image")
.and_then(|i| i.get("hash"))
.and_then(Kv::bytes)
.filter(|h| h.len() <= crate::fig::MAX_IMAGE_HASH)
.unwrap_or_default()
.iter()
.map(|b| format!("{b:02x}"))
.collect();
o.insert("imageRef".into(), json!(hash));
let mode = p.get("imageScaleMode").and_then(Kv::str).unwrap_or("FILL");
o.insert("scaleMode".into(), json!(mode));
if let (Some(t), "STRETCH") = (t, mode) {
o.insert("imageTransform".into(), rows(t));
}
if let Some(s) = num(p, "scale") {
o.insert("scalingFactor".into(), json!(s));
}
if let Some(r) = num(p, "rotation") {
o.insert("rotation".into(), json!(r));
}
match (p.get("filterColorAdjust"), p.get("paintFilter")) {
(Some(_), Some(_)) => {
warn("image filters are ambiguous because both filter containers are present; filter conversion was skipped".into())
}
(Some(_), None) => warn("legacy image filter container is unsupported; filter conversion was skipped".into()),
(None, Some(filters)) => {
let mut mapped = Obj::new();
let fields = [
("exposure", "exposure"),
("contrast", "contrast"),
("vibrance", "saturation"),
("temperature", "temperature"),
("tint", "tint"),
("highlights", "highlights"),
("shadows", "shadows"),
];
if let Kv::Object(values) = filters {
for (source, target) in fields {
if let Some(value) = filters.get(source) {
match filter_number(value) {
Some(value) => {
mapped.insert(target.into(), json!(value));
}
None => warn(format!("malformed image filter `{source}` was skipped")),
}
}
}
for (name, value) in values {
if fields.iter().any(|(known, _)| *known == *name) {
continue;
}
match filter_number(value) {
Some(0.0) => {}
Some(_) => warn(format!("nonzero unsupported image filter `{}` was skipped", crate::rest::props::clip_str(name))),
None => warn(format!("malformed unsupported image filter `{}` was skipped", crate::rest::props::clip_str(name))),
}
}
} else {
warn("malformed image paintFilter container was skipped".into());
}
if !mapped.is_empty() {
o.insert("filters".into(), Value::Object(mapped));
}
}
(None, None) => {}
}
}
_ => {}
}
Some(Value::Object(o))
}
pub(super) fn has_bound_variables(value: &Value) -> bool {
match value {
Value::Object(object) => object.contains_key("boundVariables") || object.values().any(has_bound_variables),
Value::Array(values) => values.iter().any(has_bound_variables),
_ => false,
}
}
/// The variables bound to the fields of a paint stop, an effect or a layout grid, as its REST
/// `boundVariables`: `fields` pairs the format's field with the REST name.
pub(super) fn bound(v: Option<&Kv>, fields: &[(&str, &str)]) -> Option<Value> {
let v = v?;
let found: Obj =
fields.iter().filter_map(|(from, to)| Some((to.to_string(), json!({"type": "VARIABLE_ALIAS", "id": alias(v.get(from))?})))).collect();
(!found.is_empty()).then_some(Value::Object(found))
}
/// A layer effect; `k`: the factor its lengths take (a scaled instance's layer).
pub(super) fn effect(e: &Kv, k: f64) -> Option<Value> {
let ty = match e.get("type").and_then(Kv::str)? {
"FOREGROUND_BLUR" => "LAYER_BLUR",
"GRAIN" => "TEXTURE",
t => t,
};
let mut o = Obj::new();
o.insert("type".into(), json!(ty));
for key in ["radius", "spread", "startRadius", "density", "opacity"] {
let length = matches!(key, "radius" | "spread" | "startRadius");
if let Some(x) = num(e, key).and_then(|x| scaled(x, if length { k } else { 1.0 })) {
o.insert(key.into(), json!(x));
}
}
for k in ["visible", "showShadowBehindNode", "clipToShape"] {
if let Some(b) = e.get(k).and_then(Kv::bool) {
o.insert(k.into(), json!(b));
}
}
for k in ["blendMode", "noiseType"] {
if let Some(s) = e.get(k).and_then(Kv::str) {
o.insert(k.into(), json!(s));
}
}
for k in ["color", "secondaryColor"] {
if let Some(c) = e.get(k) {
o.insert(k.into(), color(c));
}
}
// `startOffset` and `endOffset` are positions in the layer's unit box, not lengths: they stay.
for (from, to) in [("offset", "offset"), ("noiseSize", "noiseSizeVector")] {
if let Some(p) = e.get(from).and_then(vec2) {
o.insert(to.into(), scale_vec2(p, k));
}
}
for key in ["startOffset", "endOffset"] {
if let Some(p) = e.get(key).and_then(vec2) {
o.insert(key.into(), p);
}
}
if e.get("blurOpType").and_then(Kv::str) == Some("PROGRESSIVE") {
o.insert("blurType".into(), json!("PROGRESSIVE"));
}
let vars = [
("colorVar", "color"),
("radiusVar", "radius"),
("spreadVar", "spread"),
("xVar", "offsetX"),
("yVar", "offsetY"),
("startRadiusVar", "startRadius"),
];
if let Some(b) = bound(Some(e), &vars) {
o.insert("boundVariables".into(), b);
}
Some(Value::Object(o))
}
/// A `{"x", "y"}` vector at scale `k` (unchanged when that is not finite).
fn scale_vec2(p: Value, k: f64) -> Value {
let axis = |name: &str| p.get(name).and_then(Value::as_f64).and_then(|x| scaled(x, k));
match (axis("x"), axis("y")) {
(Some(x), Some(y)) => json!({"x": x, "y": y}),
_ => p,
}
}
pub(super) fn export_setting(e: &Kv) -> Value {
let c = e.get("constraint");
let ty = match c.and_then(|c| c.get("type")).and_then(Kv::str) {
Some("CONTENT_WIDTH") => "WIDTH",
Some("CONTENT_HEIGHT") => "HEIGHT",
_ => "SCALE",
};
json!({
"format": e.get("imageType").and_then(Kv::str).unwrap_or("PNG"),
"suffix": e.get("suffix").and_then(Kv::str).unwrap_or(""),
"constraint": {"type": ty, "value": c.and_then(|c| num(c, "value")).unwrap_or(1.0)},
})
}
-179
View File
@@ -1,179 +0,0 @@
use super::paint::color;
use super::variables::variable_data;
use super::{Kv, MAX_ACTION_DEPTH, Mapper, Obj, Value, View, copy_str, guid, json, num, ref_guid};
impl<'a> Mapper<'a> {
pub(super) fn prototype(&mut self, o: &mut Obj, v: &View<'a>) {
let mut list = Vec::new();
for it in v.list("prototypeInteractions") {
if it.get("isDeleted").and_then(Kv::bool) == Some(true) {
continue;
}
if let Some(i) = self.interaction(it) {
list.push(i);
}
}
if !list.is_empty() {
o.insert("interactions".into(), Value::Array(list));
}
let overflow = match v.str("scrollDirection") {
Some("HORIZONTAL") => Some("HORIZONTAL_SCROLLING"),
Some("VERTICAL") => Some("VERTICAL_SCROLLING"),
Some("BOTH") => Some("HORIZONTAL_AND_VERTICAL_SCROLLING"),
_ => None,
};
if let Some(d) = overflow {
o.insert("overflowDirection".into(), json!(d));
}
let behavior = match v.str("scrollBehavior") {
Some("FIXED_WHEN_CHILD_OF_SCROLLING_FRAME") => Some("FIXED"),
Some("STICKY_SCROLLS") => Some("STICKY_SCROLLS"),
_ => None,
};
if let Some(b) = behavior {
o.insert("scrollBehavior".into(), json!(b));
}
copy_str(o, v, "overlayPositionType", "overlayPositionType");
copy_str(o, v, "overlayBackgroundInteraction", "overlayBackgroundInteraction");
if let Some(a) = v.get("overlayBackgroundAppearance") {
let ty = a.get("backgroundType").and_then(Kv::str).unwrap_or("NONE");
o.insert("overlayBackground".into(), json!({"type": ty, "color": a.get("backgroundColor").map_or(Value::Null, color)}));
}
}
fn interaction(&mut self, it: &Kv) -> Option<Value> {
let ev = it.get("event");
let ty = match ev.and_then(|e| e.get("interactionType")).and_then(Kv::str).unwrap_or("ON_CLICK") {
"DRAG" => "ON_DRAG",
"MOUSE_IN" => "MOUSE_ENTER",
"MOUSE_OUT" => "MOUSE_LEAVE",
t => t,
};
let mut trigger = json!({"type": ty});
if let Some(t) = ev.and_then(|e| num(e, "transitionTimeout")) {
// Seconds as 32-bit floats: whole milliseconds.
trigger["timeout"] = json!((t * 1000.0).round());
}
if let Some(k) = ev.and_then(|e| e.get("keyTrigger")) {
let codes: Vec<f64> = k.get("keyCodes").map(Kv::list).unwrap_or_default().iter().filter_map(Kv::f64).collect();
trigger["keyCodes"] = json!(codes);
if let Some(d) = k.get("triggerDevice").and_then(Kv::str) {
trigger["device"] = json!(if d.contains("KEYBOARD") { "KEYBOARD" } else { d });
}
}
let actions: Vec<Value> = it.get("actions").map(Kv::list).unwrap_or_default().iter().filter_map(|a| self.action(a, 0)).collect();
(!actions.is_empty()).then(|| json!({"trigger": trigger, "actions": actions}))
}
/// One prototype action in the REST shape (`depth`: conditionals it's nested in).
fn action(&mut self, a: &Kv, depth: usize) -> Option<Value> {
Some(match a.get("connectionType").and_then(Kv::str) {
Some("INTERNAL_NODE") | None => {
let dest = a.get("transitionNodeID").and_then(guid)?;
let nav = match a.get("navigationType").and_then(Kv::str) {
Some("SWAP_STATE") => "CHANGE_TO",
Some(n) => n,
None => "NAVIGATE",
};
let mut act = json!({"type": "NODE", "destinationId": dest, "navigation": nav});
if let Some(t) = transition(a) {
act["transition"] = t;
}
if let Some(b) = a.get("transitionPreserveScroll").and_then(Kv::bool) {
act["preserveScrollPosition"] = json!(b);
}
if let Some(p) = a.get("overlayRelativePosition") {
act["overlayRelativePosition"] = json!({"x": num(p, "x"), "y": num(p, "y")});
}
act
}
Some("URL") => json!({"type": "URL", "url": a.get("connectionURL").and_then(Kv::str)?}),
Some("BACK") => json!({"type": "BACK"}),
Some("CLOSE") => json!({"type": "CLOSE"}),
Some("NONE") => return None,
Some("SET_VARIABLE") => {
let var = a.get("targetVariable").and_then(|t| t.get("id")).and_then(ref_guid).or_else(|| a.get("targetVariableID").and_then(guid));
// Missing parts are left out: the REST importer skips the action with a warning.
let mut act = json!({"type": "SET_VARIABLE", "variableId": var});
if let Some(d) = a.get("targetVariableData").and_then(|d| variable_data(d, 0)) {
act["variableValue"] = d;
}
act
}
Some("SET_VARIABLE_MODE") => json!({
"type": "SET_VARIABLE_MODE",
"variableCollectionId": a.get("targetVariableSetID").and_then(ref_guid),
"variableModeId": a.get("targetVariableModeID").and_then(guid),
}),
Some("CONDITIONAL") if depth < MAX_ACTION_DEPTH => {
let mut blocks = Vec::new();
for b in a.get("conditionalActions").map(Kv::list).unwrap_or_default() {
let actions: Vec<Value> =
b.get("actions").map(Kv::list).unwrap_or_default().iter().filter_map(|x| self.action(x, depth + 1)).collect();
let mut block = json!({"actions": actions});
// A condition that can't be read must not turn the block into an "else".
if let Some(c) = b.get("condition") {
block["condition"] = variable_data(c, 0).unwrap_or_else(|| json!({}));
}
blocks.push(block);
}
json!({"type": "CONDITIONAL", "conditionalBlocks": blocks})
}
Some(other) => {
self.warn(format!("{} prototype actions are not imported from .fig files yet", other.to_ascii_lowercase().replace('_', " ")));
return None;
}
})
}
}
/// A prototype action's transition (`None`: instant).
fn transition(a: &Kv) -> Option<Value> {
let ty = a.get("transitionType").and_then(Kv::str)?;
// Directions name where the incoming screen moves (SLIDE_FROM_LEFT moves right).
let (kind, dir) = match ty {
"INSTANT_TRANSITION" => return None,
"DISSOLVE" | "FADE" => ("DISSOLVE", None),
"SMART_ANIMATE" | "MAGIC_MOVE" => ("SMART_ANIMATE", None),
"SCROLL_ANIMATE" => ("SCROLL_ANIMATE", None),
t => {
let prefixes = [
("SLIDE_FROM_", "SLIDE_IN"),
("PUSH_FROM_", "PUSH"),
("MOVE_FROM_", "MOVE_IN"),
("SLIDE_OUT_TO_", "SLIDE_OUT"),
("MOVE_OUT_TO_", "MOVE_OUT"),
];
let (kind, side) = prefixes.iter().find_map(|(p, k)| t.strip_prefix(p).map(|r| (*k, r)))?;
let incoming = matches!(kind, "SLIDE_IN" | "PUSH" | "MOVE_IN");
let dir = match (side, incoming) {
("LEFT", true) | ("RIGHT", false) => "RIGHT",
("RIGHT", true) | ("LEFT", false) => "LEFT",
("TOP", true) | ("BOTTOM", false) => "BOTTOM",
_ => "TOP",
};
(kind, Some(dir))
}
};
let easing = match a.get("easingType").and_then(Kv::str) {
Some("IN_CUBIC" | "EASE_IN") => "EASE_IN",
Some("INOUT_CUBIC") => "EASE_IN_AND_OUT",
Some("LINEAR") => "LINEAR",
Some("IN_BACK_CUBIC") => "EASE_IN_BACK",
Some("OUT_BACK_CUBIC") => "EASE_OUT_BACK",
Some("INOUT_BACK_CUBIC") => "EASE_IN_AND_OUT_BACK",
Some("GENTLE_SPRING") => "GENTLE",
Some("CUSTOM_CUBIC") => "CUSTOM_CUBIC_BEZIER",
_ => "EASE_OUT",
};
let mut e = json!({"type": easing});
let f: Vec<f64> = a.get("easingFunction").map(Kv::list).unwrap_or_default().iter().filter_map(Kv::f64).collect();
if let (true, [x1, y1, x2, y2, ..]) = (easing == "CUSTOM_CUBIC_BEZIER", f.as_slice()) {
e["easingFunctionCubicBezier"] = json!({"x1": x1, "y1": y1, "x2": x2, "y2": y2});
}
let mut t = json!({"type": kind, "duration": (num(a, "transitionDuration").unwrap_or(0.3) * 1000.0).round(), "easing": e});
if let Some(d) = dir {
t["direction"] = json!(d);
}
Some(t)
}
-323
View File
@@ -1,323 +0,0 @@
use super::paint::paint;
use super::{Kv, Mapper, Obj, Value, View, copy_len, copy_str, json, network, num, scaled};
/// The value from the last override layer holding a valid one for an optional text setting; invalid instance rows fall through.
/// `length`: the setting is a length, at the scale of the layer that has it.
fn text_num(v: &View, key: &str, length: bool) -> Option<f64> {
v.layers.iter().zip(&v.scales).rev().find_map(|(layer, k)| {
layer.get(key).and_then(|value| match value {
Kv::Bool(_) => None,
_ => scaled(value.f64()?, if length { *k } else { 1.0 }),
})
})
}
fn text_flag(v: &View, key: &str) -> Option<bool> {
v.layers.iter().rev().find_map(|layer| layer.get(key).and_then(Kv::bool))
}
fn insert_text_options(style: &mut Obj, v: &View) {
if let Some(value) = text_num(v, "listSpacing", true) {
style.insert("listSpacing".into(), json!(value));
}
for k in ["hangingList", "hangingPunctuation"] {
if let Some(value) = text_flag(v, k) {
style.insert(k.into(), json!(value));
}
}
if let Some(value) = leading_trim(v) {
style.insert("leadingTrim".into(), json!(value));
}
}
fn leading_trim<'a>(v: &View<'a>) -> Option<&'a str> {
v.layers.iter().rev().find_map(|layer| match layer.get("leadingTrim").and_then(Kv::str) {
Some(value @ ("NONE" | "CAP_HEIGHT")) => Some(value),
_ => None,
})
}
/// The chars that start a wrapped line in the layout the file saved with the text
/// (`derivedTextData`'s `baselines`, each line's `firstCharacter` in UTF-16 units), when that layout
/// is for these characters; lines after a hard break are left out (empty: it wraps nowhere). None
/// when the saved layout is missing, for other text or inconsistent.
fn saved_line_starts(v: &View, chars: &str) -> Option<Vec<usize>> {
// Derived data from below the layer that set the characters was laid out for other text.
let top = |k: &str| v.layers.iter().rposition(|l| l.get(k).is_some());
if top("derivedTextData")? < top("textData")? {
return None;
}
let baselines = v.get("derivedTextData")?.get("baselines")?.list();
// UTF-16 offset of each char, and of the end.
let mut offsets: Vec<usize> = Vec::with_capacity(chars.len() + 1);
let mut unit = 0;
for c in chars.chars() {
offsets.push(unit);
unit += c.len_utf16();
}
offsets.push(unit);
let list: Vec<char> = chars.chars().collect();
let mut starts = Vec::new();
let mut prev = None;
for b in baselines {
let u = num(b, "firstCharacter").filter(|u| *u >= 0.0 && u.fract() == 0.0 && *u < unit as f64)? as usize;
let c = offsets.binary_search(&u).ok()?;
// Lines start at the first char, then further on each time.
if !prev.map_or(c == 0, |p| c > p) {
return None;
}
prev = Some(c);
if c > 0 && !list.get(c - 1).is_some_and(|ch| matches!(ch, '\n' | '\u{2028}')) {
starts.push(c);
}
}
prev.map(|_| starts)
}
impl<'a> Mapper<'a> {
pub(super) fn text(&mut self, o: &mut Obj, v: &View<'a>) {
let (key, legacy) = ("styleIdForText", "inheritTextStyleID");
let style_node = self.style_link(v, key, legacy).and_then(|s| self.nodes.get(&s).copied());
// Overrides above the one that links the style beat it; it beats the copies below.
let sv = v.styled(style_node.filter(|s| s.get("styleType").and_then(Kv::str) == Some("TEXT")), key, legacy);
let found = v.find("textData");
let (td, td_scale) = (found.map(|f| f.0), found.map_or(1.0, |f| f.1));
let chars = td.and_then(|t| t.get("characters")).and_then(Kv::str).unwrap_or("");
o.insert("characters".into(), json!(chars));
// Where the file's own layout wrapped the lines (not a REST field).
if let Some(starts) = saved_line_starts(v, chars) {
o.insert("_figLineStarts".into(), json!(starts));
}
let mut style = text_style(&sv, None);
// The style's typography beats the layer's own copy of it (the copy is stale when the style
// changed). The file cannot tell a stale copy of these four values from a deliberate local
// one, and "original", "none" and 0 could never be set locally if the style always won, so
// a value the layer or an override sets itself is taken as deliberate.
if let Some(decoration @ ("NONE" | "UNDERLINE" | "STRIKETHROUGH")) = v.str("textDecoration") {
style.insert("textDecoration".into(), json!(decoration));
}
if let Some(skip_ink) = v.flag("textDecorationSkipInk") {
style.insert("textDecorationSkipInk".into(), json!(skip_ink));
}
if let Some(case @ ("ORIGINAL" | "UPPER" | "LOWER" | "TITLE" | "SMALL_CAPS" | "SMALL_CAPS_FORCED")) = v.str("textCase") {
style.insert("textCase".into(), json!(case));
}
for k in ["textAlignHorizontal", "textAlignVertical", "textAutoResize", "textTruncation"] {
copy_str(&mut style, v, k, k);
}
// Paragraph settings belong to the effective text layer, including explicit zero clears.
for k in ["paragraphSpacing", "paragraphIndent"] {
copy_len(&mut style, v, k, k);
}
insert_text_options(&mut style, v);
if let Some(n) = v.num("maxLines").filter(|n| *n >= 1.0) {
style.insert("maxLines".into(), json!(n));
}
let size = sv.len("fontSize");
if let Some(b) = o.get_mut("style").and_then(|s| s.get_mut("boundVariables")).map(Value::take) {
style.insert("boundVariables".into(), b);
}
o.insert("style".into(), Value::Object(style));
let Some(td) = td else { return };
char_styles(o, td, td_scale, size, &mut |w| self.warn(w));
let raw = td.get("lines").map(Kv::list).unwrap_or_default();
let lines: Vec<&str> = raw
.iter()
.map(|l| match l.get("lineType").and_then(Kv::str) {
Some("ORDERED_LIST") => "ORDERED",
Some("UNORDERED_LIST") => "UNORDERED",
_ => "NONE",
})
.collect();
if lines.iter().any(|l| *l != "NONE") {
o.insert("lineTypes".into(), json!(lines));
}
// A forced base direction per paragraph (`sourceDirectionality`; not a REST field).
let dirs: Vec<&str> = raw
.iter()
.map(|l| match l.get("sourceDirectionality").and_then(Kv::str) {
Some("LTR") => "LTR",
Some("RTL") => "RTL",
_ => "AUTO",
})
.collect();
if dirs.iter().any(|d| *d != "AUTO") {
o.insert("lineDirections".into(), json!(dirs));
}
// Where a numbered list starts counting, less one (not a REST field).
let starts: Vec<u32> =
raw.iter().map(|l| num(l, "listStartOffset").filter(|n| n.is_finite()).map_or(0, |n| n.clamp(0.0, f64::from(u32::MAX)) as u32)).collect();
if starts.iter().any(|n| *n > 0) {
o.insert("listStartOffsets".into(), json!(starts));
}
}
/// Carry supported text-path data through the private .fig REST ingress only.
pub(super) fn text_path(&mut self, o: &mut Obj, v: &View<'a>) {
let fail = |this: &mut Self, reason: &str| {
this.warn(format!("text path {reason}; text imports off its path"));
};
let Some(start) = v.get("textPathStart") else {
fail(self, "has no start data");
return;
};
let (Some(t), Some(forward)) = (num(start, "tValue"), start.get("forward").and_then(Kv::bool)) else {
fail(self, "has invalid start data");
return;
};
let Some(vd) = v.get("vectorData") else {
fail(self, "has no vector data");
return;
};
let Some(blob) = num(vd, "vectorNetworkBlob")
.filter(|i| i.is_finite() && *i >= 0.0 && i.fract() == 0.0)
.and_then(|i| usize::try_from(i as u64).ok())
.and_then(|i| self.blobs.get(i))
.copied()
else {
fail(self, "has no vector network");
return;
};
let Some(net) = network::decode(blob) else {
fail(self, "has damaged vector data");
return;
};
let Some(norm) = vd.get("normalizedSize") else {
fail(self, "has invalid normalized size");
return;
};
let (Some(nx), Some(ny), Some((w, h))) = (num(norm, "x"), num(norm, "y"), v.size()) else {
fail(self, "has invalid dimensions");
return;
};
let axis_scale = |size: f64, normalized: f64, values: &[f64]| -> Option<f64> {
if !size.is_finite() || size < 0.0 || !normalized.is_finite() || normalized < 0.0 {
return None;
}
if normalized > 1e-9 {
if size <= 0.0 {
return None;
}
return Some(size / normalized);
}
(size == 0.0 && values.iter().all(|v| *v == 0.0)).then_some(1.0)
};
let xs: Vec<f64> = net.vertices.iter().map(|p| p.x).chain(net.segments.iter().flat_map(|s| [s.ta.0, s.tb.0])).collect();
let ys: Vec<f64> = net.vertices.iter().map(|p| p.y).chain(net.segments.iter().flat_map(|s| [s.ta.1, s.tb.1])).collect();
let sx = axis_scale(w, nx, &xs);
let sy = axis_scale(h, ny, &ys);
let (Some(sx), Some(sy)) = (sx, sy) else {
fail(self, "has invalid path scale");
return;
};
if v.str("textAlignVertical").is_some_and(|x| x != "TOP") {
fail(self, "uses unsupported vertical placement");
return;
}
if v.get("textData").and_then(|td| td.get("characters")).and_then(Kv::str).is_some_and(|s| s.chars().any(|c| matches!(c, '\n' | '\r'))) {
fail(self, "contains multiple lines");
return;
}
let path = match crate::fig::text_path::from_network(&net, sx, sy, t, forward) {
Ok(path) => path,
Err(reason) => {
fail(self, reason);
return;
}
};
let has_decoration = |style: &Value| style.get("textDecoration").and_then(Value::as_str).is_some_and(|x| x != "NONE");
let decorated = o.get("style").is_some_and(has_decoration)
|| o.get("styleOverrideTable").and_then(Value::as_object).into_iter().flat_map(|styles| styles.values()).any(has_decoration);
if decorated {
self.warn("text path underline or strikethrough decoration is not rendered");
}
o.insert("_figTextPath".into(), json!({"d": path.d, "start": path.start, "flip": path.flip}));
}
}
/// Per-character styles of text data `td` (`characterStyleIDs` + `styleOverrideTable`) as REST
/// `characterStyleOverrides` + `styleOverrideTable`; `scale`: the factor the lengths of `td` take
/// (a scaled instance's layer); `size`: the base font size.
pub(super) fn char_styles(o: &mut Obj, td: &Kv, scale: f64, size: Option<f64>, warn: &mut dyn FnMut(String)) {
let ids: Vec<u64> = td.get("characterStyleIDs").map(Kv::list).unwrap_or_default().iter().filter_map(Kv::f64).map(|i| i as u64).collect();
if ids.iter().all(|i| *i == 0) {
return;
}
let mut table = Obj::new();
for e in td.get("styleOverrideTable").map(Kv::list).unwrap_or_default() {
let Some(sid) = num(e, "styleID") else { continue };
let mut s = text_style(&View::of_scaled(e, scale), size);
if let Some(fills) = e.get("fillPaints") {
s.insert("fills".into(), Value::Array(fills.list().iter().filter_map(|p| paint(p, warn)).collect()));
}
table.insert((sid as u64).to_string(), Value::Object(s));
}
o.insert("characterStyleOverrides".into(), json!(ids));
o.insert("styleOverrideTable".into(), Value::Object(table));
}
/// The REST TypeStyle fields a node (or text override entry) sets. `size`: the font size that
/// letter spacing in percent refers to when the entry doesn't set one.
fn text_style(v: &View, size: Option<f64>) -> Obj {
let mut s = Obj::new();
if let Some(f) = v.get("fontName") {
for (from, to) in [("family", "fontFamily"), ("style", "fontStyle"), ("postscript", "fontPostScriptName")] {
if let Some(x) = f.get(from).and_then(Kv::str).filter(|x| !x.is_empty()) {
// Compact and spaced style names ("SemiBold", "Semi Bold") are the same style.
s.insert(to.into(), if from == "style" { json!(crate::fig::style_name(x)) } else { json!(x) });
}
}
}
copy_len(&mut s, v, "fontSize", "fontSize");
copy_len(&mut s, v, "paragraphSpacing", "paragraphSpacing");
copy_len(&mut s, v, "paragraphIndent", "paragraphIndent");
insert_text_options(&mut s, v);
copy_str(&mut s, v, "textCase", "textCase");
copy_str(&mut s, v, "textDecoration", "textDecoration");
if let Some(skip_ink) = v.flag("textDecorationSkipInk") {
s.insert("textDecorationSkipInk".into(), json!(skip_ink));
}
copy_str(&mut s, v, "leadingTrim", "leadingTrim");
let font_size = v.len("fontSize").or(size).unwrap_or(12.0);
if let Some((ls, k)) = v.find("letterSpacing") {
let val = num(ls, "value").unwrap_or(0.0);
let px = match ls.get("units").and_then(Kv::str) {
Some("PIXELS") => scaled(val, k).unwrap_or(val),
Some("PERCENT") => {
// Private (read by `rest/text.rs`): REST spacing is in pixels; keep the percent.
s.insert("_letterSpacingPercent".into(), json!(val));
val / 100.0 * font_size
}
_ => val * font_size,
};
s.insert("letterSpacing".into(), json!(px));
}
if let Some((lh, k)) = v.find("lineHeight") {
let val = num(lh, "value").unwrap_or(100.0);
match lh.get("units").and_then(Kv::str) {
Some("PIXELS") => {
s.insert("lineHeightUnit".into(), json!("PIXELS"));
s.insert("lineHeightPx".into(), json!(scaled(val, k).unwrap_or(val)));
}
// shortcut: percent units count from the font's own line height (100 % is "auto"), which
// the document can only say for 100 %; other values read as % of the font size. Upgrade
// when line heights can be a share of the font's own.
Some("PERCENT") if (val - 100.0).abs() < 1e-6 => {
s.insert("lineHeightUnit".into(), json!("INTRINSIC_%"));
}
Some("PERCENT") => {
s.insert("lineHeightUnit".into(), json!("FONT_SIZE_%"));
s.insert("lineHeightPercentFontSize".into(), json!(val));
}
_ => {
s.insert("lineHeightUnit".into(), json!("FONT_SIZE_%"));
s.insert("lineHeightPercentFontSize".into(), json!(val * 100.0));
}
}
}
// An empty URL is kept: on an override entry it clears the text's own link.
if let Some(u) = v.get("hyperlink").and_then(|h| h.get("url")).and_then(Kv::str) {
s.insert("hyperlink".into(), json!({"type": "URL", "url": u}));
}
s
}
-168
View File
@@ -1,168 +0,0 @@
use super::paint::color;
use super::{Kv, MAX_EXPR_DEPTH, Mapper, Obj, Value, View, guid, json, num, ref_guid};
impl<'a> Mapper<'a> {
/// Layer `v`'s variable bindings (`boundVariables`, and the text style's) and its explicitly
/// set modes (`explicitVariableModes`) into `o`, in the REST shape. Variables and collections
/// named by library asset resolve to their newest copy in the file.
pub(super) fn variable_bindings(&mut self, o: &mut Obj, v: &View<'a>) {
let mut bound = Obj::new();
let mut text_bound = Obj::new();
for e in v.get("variableConsumptionMap").and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default() {
let Some(var) = alias(e.get("variableData")) else { continue };
let a = json!({"type": "VARIABLE_ALIAS", "id": var});
let field = e.get("variableField").and_then(Kv::str).unwrap_or("");
match (binding_key(field), text_binding_key(field)) {
(Some(k), _) => {
bound.insert(k.into(), a);
}
(_, Some(k)) => {
text_bound.insert(k.into(), a);
}
_ if field == "CORNER_RADIUS" => {
for k in ["topLeftRadius", "topRightRadius", "bottomLeftRadius", "bottomRightRadius"] {
bound.insert(k.into(), a.clone());
}
}
_ if field.starts_with("BORDER_") => {
let side = match field {
"BORDER_TOP_WEIGHT" => "strokeTopWeight",
"BORDER_RIGHT_WEIGHT" => "strokeRightWeight",
"BORDER_BOTTOM_WEIGHT" => "strokeBottomWeight",
_ => "strokeLeftWeight",
};
bound.insert(side.into(), a);
}
_ if field == "WIDTH" || field == "HEIGHT" => {
let axis = if field == "WIDTH" { "x" } else { "y" };
let size = bound.entry("size").or_insert_with(|| json!({}));
size[axis] = a;
}
_ => self.warn(format!("variables bound to {} are not imported from .fig files yet", field.to_ascii_lowercase().replace('_', " "))),
}
}
if !bound.is_empty() {
o.insert("boundVariables".into(), Value::Object(bound));
}
// (The text style itself is added after this: it keeps these.)
if !text_bound.is_empty() {
o.insert("style".into(), json!({"boundVariables": text_bound}));
}
let mut modes = Obj::new();
let mut lost = false;
for e in v.get("variableModeBySetMap").and_then(|m| m.get("entries")).map(Kv::list).unwrap_or_default() {
let (Some(set), Some(mode)) = (e.get("variableSetID"), e.get("variableModeID").and_then(guid)) else { continue };
match self.collection_id(set) {
Some(id) => {
modes.insert(id, json!(mode));
}
// A mode of a collection neither named by guid nor resolvable in the file.
None if set.get("guid").is_some() || set.get("assetRef").is_some() => lost = true,
None => {}
}
}
if lost {
self.warn("variable modes of collections that aren't in the file were skipped");
}
if !modes.is_empty() {
o.insert("explicitVariableModes".into(), Value::Object(modes));
}
}
}
/// The REST `boundVariables` key of a bound layer field.
fn binding_key(field: &str) -> Option<&'static str> {
Some(match field {
"STROKE_WEIGHT" => "strokeWeight",
"STACK_SPACING" => "itemSpacing",
"STACK_COUNTER_SPACING" => "counterAxisSpacing",
"STACK_PADDING_LEFT" => "paddingLeft",
"STACK_PADDING_TOP" => "paddingTop",
"STACK_PADDING_RIGHT" => "paddingRight",
"STACK_PADDING_BOTTOM" => "paddingBottom",
"VISIBLE" => "visible",
"TEXT_DATA" => "characters",
"OPACITY" => "opacity",
"MIN_WIDTH" => "minWidth",
"MAX_WIDTH" => "maxWidth",
"MIN_HEIGHT" => "minHeight",
"MAX_HEIGHT" => "maxHeight",
"RECTANGLE_TOP_LEFT_CORNER_RADIUS" => "topLeftRadius",
"RECTANGLE_TOP_RIGHT_CORNER_RADIUS" => "topRightRadius",
"RECTANGLE_BOTTOM_LEFT_CORNER_RADIUS" => "bottomLeftRadius",
"RECTANGLE_BOTTOM_RIGHT_CORNER_RADIUS" => "bottomRightRadius",
_ => return None,
})
}
/// The REST text style `boundVariables` key of a bound text field.
fn text_binding_key(field: &str) -> Option<&'static str> {
Some(match field {
"FONT_SIZE" => "fontSize",
"FONT_FAMILY" => "fontFamily",
"FONT_STYLE" => "fontStyle",
"LETTER_SPACING" => "letterSpacing",
"LINE_HEIGHT" => "lineHeight",
"PARAGRAPH_SPACING" => "paragraphSpacing",
"PARAGRAPH_INDENT" => "paragraphIndent",
_ => return None,
})
}
/// A `VariableData` (literal, variable reference or expression) in the REST shape; `None` for
/// kinds a prototype can't evaluate.
pub(super) fn variable_data(d: &Kv, depth: usize) -> Option<Value> {
let v = d.get("value")?;
let ty = d.get("dataType").and_then(Kv::str)?;
let value = match ty {
"ALIAS" => json!({"type": "VARIABLE_ALIAS", "id": v.get("alias").and_then(ref_guid)?}),
"EXPRESSION" if depth < MAX_EXPR_DEPTH => {
let e = v.get("expressionValue")?;
let function = match e.get("expressionFunction").and_then(Kv::str)? {
"MULTIPLY" => "MULTIPLICATION",
"DIVIDE" => "DIVISION",
f => f,
};
let args = e.get("expressionArguments").map(Kv::list).unwrap_or_default();
let args = args.iter().map(|x| variable_data(x, depth + 1)).collect::<Option<Vec<Value>>>()?;
json!({"expressionFunction": function, "expressionArguments": args})
}
"BOOLEAN" => json!(v.get("boolValue").and_then(Kv::bool)?),
"FLOAT" => json!(num(v, "floatValue").filter(|x| x.is_finite())?),
"STRING" => json!(v.get("textValue").and_then(Kv::str)?),
"COLOR" => color(v.get("colorValue")?),
_ => return None,
};
let mut out = json!({"type": ty, "value": value});
if let Some(r) = d.get("resolvedDataType").and_then(Kv::str) {
out["resolvedType"] = json!(r);
}
Some(out)
}
/// The variable an `ALIAS` variable data points at: by guid, or by library asset (the newest copy
/// of it in the file).
pub(super) fn alias(d: Option<&Kv>) -> Option<String> {
let d = d?;
if d.get("dataType").and_then(Kv::str) != Some("ALIAS") {
return None;
}
d.get("value").and_then(|v| v.get("alias")).and_then(super::VarCanon::resolve)
}
pub(super) fn variable_value(d: &Kv) -> Option<Value> {
if let Some(a) = alias(Some(d)) {
return Some(json!({"type": "VARIABLE_ALIAS", "id": a}));
}
let v = d.get("value")?;
if let Some(c) = v.get("colorValue") {
return Some(color(c));
}
if let Some(b) = v.get("boolValue").and_then(Kv::bool) {
return Some(json!(b));
}
if let Some(x) = num(v, "floatValue") {
return Some(json!(x));
}
v.get("textValue").and_then(Kv::str).map(|s| json!(s))
}
-133
View File
@@ -1,133 +0,0 @@
//! Vector network blobs: the editable form of a vector layer (`vectorData.vectorNetworkBlob`).
//!
//! Little-endian throughout: `u32` counts of vertices, segments and regions; each vertex a style
//! id (`u32`) and its position (`f32` x, y); each segment a style id, its start vertex (`u32`) and
//! tangent (`f32` x, y), then its end vertex and tangent; each region a `u32` holding its style id
//! shifted left once with the winding rule in the low bit (set: non-zero, clear: even-odd), a loop
//! count, and per loop a segment count and the segment indices. Positions are in the space of
//! `vectorData.normalizedSize`. Style ids point into `vectorData.styleOverrideTable` (0: none).
/// Most vertices, segments, regions or loop entries read from one blob.
const MAX_ITEMS: usize = 200_000;
pub(super) struct Vertex {
pub style: u32,
pub x: f64,
pub y: f64,
}
pub(super) struct Segment {
pub style: u32,
pub a: usize,
pub ta: (f64, f64),
pub b: usize,
pub tb: (f64, f64),
}
pub(super) struct Region {
pub style: u32,
pub non_zero: bool,
pub loops: Vec<Vec<usize>>,
}
#[derive(Default)]
pub(super) struct Network {
pub vertices: Vec<Vertex>,
pub segments: Vec<Segment>,
pub regions: Vec<Region>,
}
struct Reader<'b> {
b: &'b [u8],
pos: usize,
}
impl Reader<'_> {
fn u32(&mut self) -> Option<u32> {
let v = u32::from_le_bytes(self.b.get(self.pos..self.pos + 4)?.try_into().ok()?);
self.pos += 4;
Some(v)
}
fn f32(&mut self) -> Option<f64> {
Some(f64::from(f32::from_bits(self.u32()?))).filter(|x| x.is_finite())
}
/// An index below `len`.
fn index(&mut self, len: usize) -> Option<usize> {
Some(self.u32()? as usize).filter(|i| *i < len)
}
/// A count of items of at least `size` bytes each that the rest of the blob can hold.
fn count(&mut self, size: usize) -> Option<usize> {
let n = self.u32()? as usize;
(n <= MAX_ITEMS && n * size <= self.b.len().saturating_sub(self.pos)).then_some(n)
}
}
/// The network in blob `b`; `None` when it's truncated, out of range or not finite.
pub(super) fn decode(b: &[u8]) -> Option<Network> {
let mut r = Reader { b, pos: 0 };
let (nv, ns, nr) = (r.u32()? as usize, r.u32()? as usize, r.u32()? as usize);
if nv > MAX_ITEMS || ns > MAX_ITEMS || nr > MAX_ITEMS || nv * 12 + ns * 28 + nr * 8 > b.len().saturating_sub(12) {
return None;
}
let mut net = Network::default();
for _ in 0..nv {
net.vertices.push(Vertex { style: r.u32()?, x: r.f32()?, y: r.f32()? });
}
for _ in 0..ns {
let style = r.u32()?;
net.segments.push(Segment { style, a: r.index(nv)?, ta: (r.f32()?, r.f32()?), b: r.index(nv)?, tb: (r.f32()?, r.f32()?) });
}
let mut entries = 0usize;
for _ in 0..nr {
let sw = r.u32()?;
let mut loops = Vec::new();
for _ in 0..r.count(4)? {
let n = r.count(4)?;
entries += n;
if entries > MAX_ITEMS {
return None;
}
loops.push((0..n).map(|_| r.index(ns)).collect::<Option<Vec<usize>>>()?);
}
net.regions.push(Region { style: sw >> 1, non_zero: sw & 1 == 1, loops });
}
Some(net)
}
/// The blob of `net` (the export writes it; so do test fixtures). Callers pass finite values.
pub(super) fn encode(net: &Network) -> Vec<u8> {
let mut out = Vec::new();
let u = |v: u32, out: &mut Vec<u8>| out.extend_from_slice(&v.to_le_bytes());
let fl = |v: f64, out: &mut Vec<u8>| out.extend_from_slice(&(v as f32).to_le_bytes());
for n in [net.vertices.len(), net.segments.len(), net.regions.len()] {
u(n as u32, &mut out);
}
for v in &net.vertices {
u(v.style, &mut out);
fl(v.x, &mut out);
fl(v.y, &mut out);
}
for s in &net.segments {
u(s.style, &mut out);
u(s.a as u32, &mut out);
fl(s.ta.0, &mut out);
fl(s.ta.1, &mut out);
u(s.b as u32, &mut out);
fl(s.tb.0, &mut out);
fl(s.tb.1, &mut out);
}
for r in &net.regions {
u((r.style << 1) | u32::from(r.non_zero), &mut out);
u(r.loops.len() as u32, &mut out);
for l in &r.loops {
u(l.len() as u32, &mut out);
for i in l {
u(*i as u32, &mut out);
}
}
}
out
}
+3 -3
View File
@@ -753,7 +753,7 @@ fn connectors_without_a_saved_path_follow_their_line_style() {
let line: Vec<_> =
network_of(&doc, "Line").vertices.iter().map(|v| world("Line") * figcraft_doc::geom::Point::new(v.x, v.y)).map(|p| (p.x, p.y)).collect();
assert_eq!(line, [(100.0, 650.0), (300.0, 650.0)]);
assert!(warnings.iter().any(|w| w.contains("FigCraft's own routing")), "{warnings:?}");
assert!(warnings.iter().any(|w| w.contains("are routed afresh")), "{warnings:?}");
}
/// #1909: a connector end whose magnet is `AUTO` (FigJam picks the side when it draws) or `CENTER`
@@ -1033,7 +1033,7 @@ fn an_elbow_connector_saved_with_its_path_keeps_it() {
// Each end starts `END_GAP` (4) outside its side's border.
assert_eq!(line, [(50.0, 704.0), (50.0, 740.0), (200.0, 740.0), (200.0, 850.0), (296.0, 850.0)]);
// The fixture's own Link, saved without a path, is routed here: the note counts it alone.
let routed: Vec<_> = warnings.iter().filter(|w| w.contains("FigCraft's own routing")).collect();
let routed: Vec<_> = warnings.iter().filter(|w| w.contains("are routed afresh")).collect();
assert_eq!(routed.len(), 1, "{warnings:?}");
assert!(!routed[0].contains('×'), "a connector with its path isn't counted among those routed here: {routed:?}");
}
@@ -1081,5 +1081,5 @@ fn an_elbow_whose_saved_path_doesnt_fit_its_sides_is_routed_here() {
for name in ["Sideways", "Askew", "Repeated", "Backwards", "Inside"] {
assert_eq!(line(name), plain, "{name} is routed as one without a path");
}
assert!(warnings.iter().any(|w| w.contains("FigCraft's own routing")), "{warnings:?}");
assert!(warnings.iter().any(|w| w.contains("are routed afresh")), "{warnings:?}");
}
+22 -24
View File
@@ -795,11 +795,11 @@ fn hashes_naming_one_blob_share_one_copy_of_it() {
("blobs", Kv::Array(vec![o(vec![("bytes", Kv::Bytes(png()))]), o(vec![("bytes", Kv::Bytes(vec![7; 16]))])])),
("nodeChanges", Kv::Array((1..=50).map(|h| image(h, 0)).chain([image(99, 1), image(98, 2)]).collect())),
]);
let copies = embedded_copies(&msg);
let copies = embedded_copies(&msg, MAX_IMAGE);
assert_eq!(copies.len(), 51, "the hash naming no blob is left out");
let first = &copies[&"01".repeat(20)];
assert_eq!(**first, *png());
assert_eq!(copies.values().filter(|c| Arc::ptr_eq(c, first)).count(), 50);
assert_eq!(copies.values().filter(|c| std::sync::Arc::ptr_eq(c, first)).count(), 50);
assert_eq!(*copies[&"63".repeat(20)], [7; 16]);
}
@@ -814,7 +814,7 @@ fn hashes_naming_one_blob_are_imported_once() {
let mut nodes = vec![node(0, None, "DOCUMENT", "D", vec![]), node(1, Some((0, "!")), "CANVAS", "P", vec![])];
nodes.extend((2..42).map(|i| rect(i, i as u8, 0)));
let msg = o(vec![("nodeChanges", arr(nodes)), ("blobs", arr(vec![o(vec![("bytes", Kv::Bytes(png()))])]))]);
let copies = embedded_copies(&msg);
let copies = embedded_copies(&msg, MAX_IMAGE);
let aliases = embedded_aliases(&copies, |_| false);
assert_eq!(aliases.len(), 39);
assert!(aliases.values().all(|c| *c == "02".repeat(20)));
@@ -974,13 +974,13 @@ mod image_budget {
// Shorten the first member's compressed size, cutting its deflate stream mid-image.
change_member_field(&mut zip, 1, 20, 15);
let limit = members[0].1.len() as u64;
let archive = Archive::open(&zip).unwrap();
archive.image_budget.remaining.set(limit);
let archive = Archive::open_with(&zip, Limits { max_image: MAX_IMAGE, max_image_total: limit }).unwrap();
assert!(archive.image(&"01".repeat(20)).is_none());
let remaining = archive.image_budget.remaining.get();
assert!(remaining > 0 && remaining < limit, "partial read left {remaining} of {limit} bytes");
// The failed read keeps its charge (craft-fig charges the declared size).
let remaining = archive.image_bytes_left();
assert!(remaining < limit, "partial read left {remaining} of {limit} bytes");
assert!(archive.image(&"02".repeat(20)).is_none());
assert_eq!(archive.image_budget.remaining.get(), remaining);
assert_eq!(archive.image_bytes_left(), remaining);
let (doc, warnings) = import_with_image_limit(&zip, "Images", limit).unwrap();
assert_ne!(bytes(&doc, 1), members[1].1);
assert_eq!(budget_warnings(&warnings).len(), 1, "{warnings:?}");
@@ -1013,14 +1013,13 @@ mod image_budget {
let members = [(1, image(1)), (2, image(2))];
let mut zip = file(&[1, 2], &members);
change_member_field(&mut zip, 1, 24, 1);
let archive = Archive::open(&zip).unwrap();
archive.image_budget.remaining.set(members[0].1.len() as u64 - 1);
let archive = Archive::open_with(&zip, Limits { max_image: MAX_IMAGE, max_image_total: members[0].1.len() as u64 - 1 }).unwrap();
// Whether the ZIP reader refuses the mismatch or reads on, the member is not returned
// and the budget is never overdrawn.
assert!(archive.image(&"01".repeat(20)).is_none());
assert_eq!(archive.image_budget.remaining.get(), 0);
assert_eq!(archive.image_budget.skipped.get(), 1);
assert!(archive.image_bytes_left() < members[0].1.len() as u64);
assert!(archive.image(&"02".repeat(20)).is_none());
assert_eq!(archive.image_budget.remaining.get(), 0);
assert_eq!(archive.image_budget.skipped.get(), 2);
assert!(archive.images_skipped() >= 1);
}
#[test]
@@ -1153,23 +1152,23 @@ fn mangled_kiwi_never_panics() {
#[test]
fn large_files_get_a_larger_value_budget() {
// Small files keep the fixed floor: a few bytes can't expand into a huge tree.
assert_eq!(kiwi::value_budget(0), 20_000_000);
assert_eq!(kiwi::value_budget(1 << 20), 20_000_000);
assert_eq!(craft_fig::value_budget(0), 20_000_000);
assert_eq!(craft_fig::value_budget(1 << 20), 20_000_000);
// A real 400 000-layer file: an 83.5 MB canvas whose message unpacks to 334 MB and holds
// 30.5 million values was refused as "too many values".
assert!(kiwi::value_budget(83_491_232) > 30_514_899);
assert!(craft_fig::value_budget(83_491_232) > 30_514_899);
// A real 194 MB design-system file (UX4G): a 39.5 MB canvas holding 29.7 million values.
assert!(kiwi::value_budget(39_528_420) > 29_703_730);
assert!(craft_fig::value_budget(39_528_420) > 29_703_730);
// Still bounded by the largest canvas read.
assert!(kiwi::value_budget(MAX_CANVAS as usize) <= 128 << 20);
assert!(craft_fig::value_budget(MAX_CANVAS as usize) <= 128 << 20);
// Counting every scalar field, a dense file may reach about 2.4 values per compressed byte (the
// 15 MB text-heavy fixture in `value_budget_tests` measures 1.8): below the ceiling the budget must
// cover that, the floor still holds up to 5 MB of canvas, and 32 MiB is enough for the ceiling.
assert!(kiwi::value_budget(8 << 20) >= (8 << 20) * 12 / 5);
assert!(craft_fig::value_budget(8 << 20) >= (8 << 20) * 12 / 5);
#[cfg(not(target_pointer_width = "32"))]
assert!(kiwi::value_budget(30_000_000) >= 30_000_000 * 12 / 5);
assert_eq!(kiwi::value_budget(5_000_000), 20_000_000);
assert_eq!(kiwi::value_budget(32 << 20), kiwi::value_budget(MAX_CANVAS as usize));
assert!(craft_fig::value_budget(30_000_000) >= 30_000_000 * 12 / 5);
assert_eq!(craft_fig::value_budget(5_000_000), 20_000_000);
assert_eq!(craft_fig::value_budget(32 << 20), craft_fig::value_budget(MAX_CANVAS as usize));
}
#[test]
@@ -1822,7 +1821,6 @@ fn same_version_library_copies_resolve_deterministically_and_leave_no_table() {
let page = doc.nodes().find(|n| n.name == "Page").unwrap();
assert_eq!(page.var_modes.get(&id).map(String::as_str), Some("1:90"));
picked.push(id);
assert!(!super::map::VarCanon::installed());
}
assert_eq!(picked, ["1:6", "1:6"]);
}
-316
View File
@@ -1,316 +0,0 @@
//! Convert the simple vector network used by text-on-path layers in `.fig` files into a native text path.
use figcraft_doc::{
TextPath,
geom::{
BezPath,
kurbo::{CubicBez, Line, ParamCurve, ParamCurveArclen, Point},
},
};
use super::network::{Network, Segment};
const START_SEGMENT_STYLE: u32 = 0x01ff_ffff;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Use {
segment: usize,
forward: bool,
}
/// Build a native path from a validated connected chain or cycle. The marked source segment is
/// oriented a→b; `t` is local to that line/cubic. Coordinates are scaled into layer space.
pub(super) fn from_network(net: &Network, sx: f64, sy: f64, t: f64, forward: bool) -> Result<TextPath, &'static str> {
if !t.is_finite() || !(0.0..=1.0).contains(&t) {
return Err("invalid start parameter");
}
if !sx.is_finite() || !sy.is_finite() || sx <= 0.0 || sy <= 0.0 {
return Err("invalid path scale");
}
let markers: Vec<usize> = net.segments.iter().enumerate().filter_map(|(i, s)| (s.style == START_SEGMENT_STYLE).then_some(i)).collect();
let [marked] = markers.as_slice() else {
return Err("missing or ambiguous start segment marker");
};
let (uses, closed) = ordered_chain(net, *marked)?;
let mut d = BezPath::new();
let mut total = 0.0;
let mut prefix = 0.0;
let mut start_segment_length = 0.0;
for use_ in &uses {
let (p0, p1, p2, p3) = cubic(net, *use_, sx, sy)?;
if d.elements().is_empty() {
d.move_to((p0.0, p0.1));
}
let line = p1 == p0 && p2 == p3;
let (length, partial) = if line {
d.line_to((p3.0, p3.1));
let line = Line::new(Point::new(p0.0, p0.1), Point::new(p3.0, p3.1));
(line.arclen(1e-9), line.arclen(1e-9) * t)
} else {
d.curve_to((p1.0, p1.1), (p2.0, p2.1), (p3.0, p3.1));
let curve = CubicBez::new((p0.0, p0.1), (p1.0, p1.1), (p2.0, p2.1), (p3.0, p3.1));
let partial = if t <= 0.0 { 0.0 } else { curve.subsegment(0.0..t).arclen(1e-9) };
(curve.arclen(1e-9), partial)
};
if !length.is_finite() || length <= 1e-10 {
return Err("zero-length path segment");
}
if use_.segment == *marked {
start_segment_length = partial;
if !start_segment_length.is_finite() {
return Err("invalid path length");
}
prefix = total;
}
total += length;
}
if closed {
d.close_path();
}
if !total.is_finite() || total <= 1e-10 {
return Err("zero-length path");
}
let mut start = ((prefix + start_segment_length) / total).clamp(0.0, 1.0);
let flip = !forward;
if flip {
start = 1.0 - start;
}
let path = TextPath { d: d.to_svg(), start, flip };
let lines = figcraft_doc::geom::measure::polylines(&path.path()).ok_or("cannot be measured by text renderer")?;
if lines.first().is_none_or(|line| line.length() <= 1e-9) {
return Err("cannot be measured by text renderer");
}
Ok(path)
}
/// Return every segment in chain order. Open chains are oriented so the marked segment runs a→b;
/// cycles start at the marked segment's a vertex and traverse it first.
fn ordered_chain(net: &Network, marked: usize) -> Result<(Vec<Use>, bool), &'static str> {
let marker = net.segments.get(marked).ok_or("invalid start segment index")?;
let mut adjacency = vec![Vec::<usize>::new(); net.vertices.len()];
for (i, s) in net.segments.iter().enumerate() {
if s.a >= adjacency.len() || s.b >= adjacency.len() {
return Err("invalid path vertex index");
}
if s.a == s.b {
return Err("path contains a segment that starts and ends at the same vertex");
}
adjacency[s.a].push(i);
adjacency[s.b].push(i);
}
let mut endpoints = Vec::new();
for edges in &adjacency {
if edges.len() > 2 {
return Err("branched path network");
}
if edges.len() == 1 {
endpoints.push(edges[0]);
}
}
if !endpoints.is_empty() && endpoints.len() != 2 {
return Err("disconnected or malformed path network");
}
let cycle = endpoints.is_empty();
let mut used = vec![false; net.segments.len()];
let mut out = Vec::with_capacity(net.segments.len());
if cycle {
out.push(Use { segment: marked, forward: true });
used[marked] = true;
let mut at = marker.b;
let mut visited = vec![false; net.vertices.len()];
visited[marker.a] = true;
visited[marker.b] = true;
while at != marker.a {
let next = adjacency[at].iter().copied().find(|i| !used[*i]).ok_or("disconnected path cycle")?;
used[next] = true;
let s = &net.segments[next];
let is_forward = s.a == at;
at = if is_forward { s.b } else { s.a };
if at != marker.a && std::mem::replace(&mut visited[at], true) {
return Err("path cycle is not simple");
}
out.push(Use { segment: next, forward: is_forward });
if out.len() > net.segments.len() {
return Err("invalid path cycle");
}
}
} else {
let endpoint = adjacency.iter().position(|e| e.len() == 1).ok_or("missing path endpoint")?;
let mut at = endpoint;
while let Some(next) = adjacency[at].iter().copied().find(|i| !used[*i]) {
used[next] = true;
let s = &net.segments[next];
let is_forward = s.a == at;
at = if is_forward { s.b } else { s.a };
out.push(Use { segment: next, forward: is_forward });
}
let Some(marker_use) = out.iter().find(|u| u.segment == marked) else { return Err("disconnected start segment") };
if !marker_use.forward {
out.reverse();
for u in &mut out {
u.forward = !u.forward;
}
}
}
if !used.iter().all(|x| *x) {
return Err("disconnected path network");
}
Ok((out, cycle))
}
type P = (f64, f64);
fn cubic(net: &Network, use_: Use, sx: f64, sy: f64) -> Result<(P, P, P, P), &'static str> {
let s: &Segment = net.segments.get(use_.segment).ok_or("invalid path segment index")?;
let a = net.vertices.get(s.a).ok_or("invalid path vertex index")?;
let b = net.vertices.get(s.b).ok_or("invalid path vertex index")?;
let p0 = (a.x * sx, a.y * sy);
let p3 = (b.x * sx, b.y * sy);
let ta = (s.ta.0 * sx, s.ta.1 * sy);
let tb = (s.tb.0 * sx, s.tb.1 * sy);
if [p0.0, p0.1, p3.0, p3.1, ta.0, ta.1, tb.0, tb.1].iter().any(|x| !x.is_finite()) {
return Err("invalid path coordinates");
}
let (p0, p1, p2, p3) =
if ta == (0.0, 0.0) && tb == (0.0, 0.0) { (p0, p0, p3, p3) } else { (p0, (p0.0 + ta.0, p0.1 + ta.1), (p3.0 + tb.0, p3.1 + tb.1), p3) };
if use_.forward { Ok((p0, p1, p2, p3)) } else { Ok((p3, p2, p1, p0)) }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fig::network::{Segment, Vertex};
use figcraft_doc::geom::kurbo::PathEl;
fn vertex(x: f64, y: f64) -> Vertex {
Vertex { style: 0, x, y }
}
fn segment(style: u32, a: usize, b: usize, ta: (f64, f64), tb: (f64, f64)) -> Segment {
Segment { style, a, ta, b, tb }
}
fn curved() -> Network {
Network {
vertices: vec![vertex(0.0, 82.5), vertex(300.0, 82.5), vertex(600.0, 82.5)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (70.0, -110.0), (-70.0, -110.0)), segment(0, 1, 2, (70.0, 110.0), (-70.0, 110.0))],
..Network::default()
}
}
#[test]
fn local_cubic_parameter_converts_to_path_distance() {
let path = from_network(&curved(), 1.0, 1.0, 0.0086954795, true).unwrap();
assert!((path.start - 0.004744078).abs() < 0.00001);
// Cubic t is not its fraction of total path length.
assert!((path.start - 0.0086954795).abs() > 0.003);
let elements = path.path().elements().to_vec();
assert!(matches!(elements.first(), Some(PathEl::MoveTo(p)) if p.x == 0.0 && p.y == 82.5));
assert!(matches!(elements.get(1), Some(PathEl::CurveTo(..))));
}
#[test]
fn second_segment_marker_and_t_are_respected() {
let mut net = curved();
net.segments[0].style = 0;
net.segments[1].style = START_SEGMENT_STYLE;
let path = from_network(&net, 1.0, 1.0, 0.49800667, true).unwrap();
assert!((path.start - 0.7490379757).abs() < 0.00002);
}
#[test]
fn reverse_direction_inverts_normalized_start() {
let net = Network {
vertices: vec![vertex(0.0, 0.0), vertex(600.0, 0.0)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0))],
..Network::default()
};
let path = from_network(&net, 1.0, 1.0, 0.83986014, false).unwrap();
assert!(path.flip);
assert!((path.start - 0.16013986).abs() < 1e-8);
assert!(matches!(path.path().elements().get(1), Some(PathEl::LineTo(p)) if p.x == 600.0 && p.y == 0.0));
}
#[test]
fn rejects_missing_duplicate_and_zero_length_segments() {
let mut net = curved();
net.segments[0].style = 0;
assert_eq!(from_network(&net, 1.0, 1.0, 0.5, true), Err("missing or ambiguous start segment marker"));
net.segments[1].style = START_SEGMENT_STYLE;
net.segments[0].style = START_SEGMENT_STYLE;
assert_eq!(from_network(&net, 1.0, 1.0, 0.5, true), Err("missing or ambiguous start segment marker"));
let zero = Network {
vertices: vec![vertex(0.0, 0.0), vertex(0.0, 0.0)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0))],
..Network::default()
};
assert_eq!(from_network(&zero, 1.0, 1.0, 0.5, true), Err("zero-length path segment"));
}
#[test]
fn rejects_branching_and_disconnected_segments() {
let branch = Network {
vertices: vec![vertex(0.0, 0.0), vertex(1.0, 0.0), vertex(2.0, 0.0), vertex(1.0, 1.0)],
segments: vec![
segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0)),
segment(0, 1, 2, (0.0, 0.0), (0.0, 0.0)),
segment(0, 1, 3, (0.0, 0.0), (0.0, 0.0)),
],
..Network::default()
};
assert_eq!(from_network(&branch, 1.0, 1.0, 0.5, true), Err("branched path network"));
let disconnected = Network {
vertices: vec![vertex(0.0, 0.0), vertex(1.0, 0.0), vertex(2.0, 0.0), vertex(3.0, 0.0)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0)), segment(0, 2, 3, (0.0, 0.0), (0.0, 0.0))],
..Network::default()
};
assert_eq!(from_network(&disconnected, 1.0, 1.0, 0.5, true), Err("disconnected or malformed path network"));
}
#[test]
fn scales_geometry_and_accepts_closed_cycles() {
let cycle = Network {
vertices: vec![vertex(0.0, 0.0), vertex(100.0, 0.0), vertex(100.0, 100.0)],
segments: vec![
segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0)),
segment(0, 1, 2, (0.0, 0.0), (0.0, 0.0)),
segment(0, 2, 0, (0.0, 0.0), (0.0, 0.0)),
],
..Network::default()
};
let path = from_network(&cycle, 2.0, 3.0, 0.5, true).unwrap();
let elements = path.path().elements().to_vec();
assert!(matches!(elements.first(), Some(PathEl::MoveTo(p)) if p.x == 0.0 && p.y == 0.0));
assert!(matches!(elements.get(1), Some(PathEl::LineTo(p)) if p.x == 200.0 && p.y == 0.0));
let total = 200.0 + 300.0 + 130_000.0_f64.sqrt();
assert!((path.start - 100.0 / total).abs() < 1e-8);
assert!(matches!(elements.last(), Some(PathEl::ClosePath)));
}
#[test]
fn rejects_paths_the_native_renderer_cannot_measure() {
let count = 40_000usize;
let many_lines = Network {
vertices: (0..=count).map(|i| vertex(i as f64, 0.0)).collect(),
segments: (0..count).map(|i| segment(if i == 0 { START_SEGMENT_STYLE } else { 0 }, i, i + 1, (0.0, 0.0), (0.0, 0.0))).collect(),
..Network::default()
};
assert_eq!(from_network(&many_lines, 1.0, 1.0, 0.5, true), Err("cannot be measured by text renderer"));
// Past the cap the builders hold their sizes to (`figcraft_geom::MAX_COORD`, inclusive).
let huge = Network {
vertices: vec![vertex(0.0, 0.0), vertex(2e9, 0.0)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0))],
..Network::default()
};
assert_eq!(from_network(&huge, 1.0, 1.0, 0.5, true), Err("cannot be measured by text renderer"));
let tiny = Network {
vertices: vec![vertex(0.0, 0.0), vertex(5e-10, 0.0)],
segments: vec![segment(START_SEGMENT_STYLE, 0, 1, (0.0, 0.0), (0.0, 0.0))],
..Network::default()
};
assert_eq!(from_network(&tiny, 1.0, 1.0, 0.5, true), Err("cannot be measured by text renderer"));
}
}
+5 -5
View File
@@ -4,7 +4,7 @@
use super::kiwi::{Kv, Schema, encode};
use super::tests::{Lcg, arr, at, f, g, node, o, s, schema, solid, u, xy};
use super::{decode_canvas, export, import, kiwi};
use super::{decode_canvas, import};
fn frac(r: &mut Lcg, hi: f64) -> f64 {
(r.next() % 100_000) as f64 / 100_000.0 * hi
@@ -84,7 +84,7 @@ fn one_list_canvas(sc: &Schema, count: u32, elements: impl IntoIterator<Item = u
let mut msg = head.0;
msg.extend(elements);
msg.push(0);
export::canvas(&encode::schema(sc), &msg).unwrap()
craft_fig::encode_canvas(&encode::schema(sc), &msg).unwrap()
}
/// `pages` pages of `rects` rectangles and `texts` texts of `chars` characters, framed the way the
@@ -125,7 +125,7 @@ fn opens_whole(label: &str, pages: usize, rects: usize, texts: usize, chars: usi
let file = canvas(pages, rects, texts, chars);
let (sc, msg) = decode_canvas(&file).unwrap();
let root = sc.find("Message").unwrap();
let one_per_byte = file.len().clamp(kiwi::value_budget(0), kiwi::value_budget(usize::MAX));
let one_per_byte = file.len().clamp(craft_fig::value_budget(0), craft_fig::value_budget(usize::MAX));
let refused = sc.decode_message_within(root, &msg, one_per_byte).is_err_and(|e| e.0 == "message: too many values");
assert!(refused, "{label}: one value per compressed byte no longer refuses it");
let (doc, warnings) = import(&file, label).unwrap_or_else(|e| panic!("{label}: refused: {e}"));
@@ -133,7 +133,7 @@ fn opens_whole(label: &str, pages: usize, rects: usize, texts: usize, chars: usi
assert!(warnings.is_empty(), "{label}: {warnings:?}");
println!("{label}: {} canvas bytes, {} layers", file.len(), doc.node_count());
drop(doc);
assert!(sc.decode_message_within(root, &msg, kiwi::value_budget(file.len()) / 10 * 9).is_ok(), "{label}: not within 90% of its budget");
assert!(sc.decode_message_within(root, &msg, craft_fig::value_budget(file.len()) / 10 * 9).is_ok(), "{label}: not within 90% of its budget");
file.len()
}
@@ -151,7 +151,7 @@ fn dense_canvases_open_whole() {
#[ignore = "heavy: --release --ignored, about 9 GB"]
fn a_canvas_budgeted_at_the_ceiling_opens_whole() {
let packed = opens_whole("50 MB text-heavy canvas", 106, 0, 5000, 100);
assert_eq!(kiwi::value_budget(packed), kiwi::value_budget(usize::MAX), "budgeted at the ceiling");
assert_eq!(craft_fig::value_budget(packed), craft_fig::value_budget(usize::MAX), "budgeted at the ceiling");
}
/// However far a small canvas inflates, the floor bounds what it may decode.
+8 -43
View File
@@ -61,18 +61,18 @@ pub use variables::export as export_variables;
pub const MAX_NODES: usize = 200_000;
/// Deepest layer nesting followed.
const MAX_DEPTH: usize = 256;
/// Private .fig provenance, consumed only by the `.fig` import ([`import_with_networks`]).
pub(crate) const FIG_SOURCE_FIELD: &str = "_figSource";
/// Hidden .fig library context; accepted only by the `.fig` import ([`import_with_networks`]).
pub(crate) const FIG_LIBRARY_CONTEXTS_FIELD: &str = "_figLibraryContexts";
/// Ids of mains in a `.fig` file that its layer cap kept out; read only by the `.fig` import.
pub(crate) const FIG_CAPPED_MAINS_FIELD: &str = "_figCappedMains";
/// The `.fig` mapping's private handoffs, read only by the `.fig` import ([`import_with_networks`]).
pub(crate) use craft_fig::rest::{FIG_CAPPED_MAINS_FIELD, FIG_LIBRARY_CONTEXTS_FIELD, FIG_SOURCE_FIELD};
/// Longest SVG path string read from one geometry entry.
const MAX_PATH_BYTES: usize = 4 << 20;
/// Most path elements in one vector.
const MAX_PATH_ELEMENTS: usize = 200_000;
/// Most distinct warnings reported.
pub(crate) const MAX_WARNINGS: usize = 200;
/// Distinct warnings in first-seen order, with repeat counts (at most 200). The
/// import report reads the `(×n)` suffix off before it looks the note up in the catalogue, so a
/// counted note is still translated (`ui-egui`'s `note_text`).
pub(crate) use craft_figma_rest::Warnings;
#[cfg(test)]
pub(crate) use craft_figma_rest::MAX_WARNINGS;
/// Most shared style definitions read.
const MAX_STYLES: usize = 10_000;
/// Image key used for image fills without an `imageRef`.
@@ -348,41 +348,6 @@ fn children(v: &Value) -> &[Value] {
v.get("children").and_then(Value::as_array).map(Vec::as_slice).unwrap_or_default()
}
/// Distinct warnings in first-seen order, with repeat counts. Shared with the `.fig` mapper
/// (`crate::fig::map`), whose notes repeat per layer in the same way.
#[derive(Default)]
pub(crate) struct Warnings {
list: Vec<(String, usize)>,
index: BTreeMap<String, usize>,
}
impl Warnings {
pub(crate) fn add(&mut self, msg: String) {
if let Some(i) = self.index.get(&msg) {
if let Some(e) = self.list.get_mut(*i) {
e.1 += 1;
}
} else if self.list.len() < MAX_WARNINGS {
self.index.insert(msg.clone(), self.list.len());
self.list.push((msg, 1));
}
}
/// The notes met so far, without their counts, for the tests that ask whether one was met
/// (the product reads them through [`Self::finish`], with the counts).
#[cfg(test)]
pub(crate) fn seen(&self) -> impl Iterator<Item = &str> {
self.list.iter().map(|(msg, _)| msg.as_str())
}
/// The notes, each with `(×n)` after it where it was met more than once. The import report
/// reads that suffix off before it looks the note up in the catalogue, so a counted note is
/// still translated (`ui-egui`'s `note_text`).
pub(crate) fn finish(self) -> Vec<String> {
self.list.into_iter().map(|(m, n)| if n > 1 { format!("{m} (×{n})") } else { m }).collect()
}
}
/// The coordinate spaces a child is placed in: its parent's (`world`) and that of the nearest
/// ancestor that is not a group or boolean (`frame`), both relative to the page.
#[derive(Clone, Copy)]
+1 -1
View File
@@ -1739,7 +1739,7 @@ status = "draft — needs native review"
"duplicate grid track sizing entries use equal tracks" = "duplirani unosi veličina traka mreže koriste jednake trake"
"e.g. count + 1" = "npr. count + 1"
"e.g. count >= 3 and not done" = "npr. count >= 3 and not done"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "kutni i zakrivljeni konektori bez sačuvane putanje slijede FigCraftovo vlastito usmjeravanje"
"elbowed and curved connectors without a saved path are routed afresh" = "kutni i zakrivljeni konektori bez sačuvane putanje slijede FigCraftovo vlastito usmjeravanje"
"entries without a node type were skipped" = "unosi bez vrste čvora su preskočeni"
"figcraft:// links from a browser, chat or email open this app and join the session." = "figcraft:// linkovi iz preglednika, chata ili e-pošte otvaraju ovu aplikaciju i pridružuju se sesiji."
"from {server}" = "sa servera {server}"
+1 -1
View File
@@ -1948,7 +1948,7 @@ status = "draft — needs native review"
"Your unsaved files were too large for the browser to keep." = "Ihre ungespeicherten Dateien waren zu groß, als dass der Browser sie behalten konnte."
"Z to A" = "Z bis A"
"connector labels without a saved snapshot are centred on the line, without their background" = "Verbinder-Beschriftungen ohne gespeicherten Schnappschuss werden mittig auf der Linie und ohne Hintergrund dargestellt"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "Abgewinkelte und gebogene Verbinder ohne gespeicherten Pfad folgen dem eigenen Routing von FigCraft"
"elbowed and curved connectors without a saved path are routed afresh" = "Abgewinkelte und gebogene Verbinder ohne gespeicherten Pfad folgen dem eigenen Routing von FigCraft"
"instance swap properties whose default component was past the layer limit were skipped" = "Instanztausch-Eigenschaften, deren Standardkomponente über dem Ebenenlimit lag, wurden übersprungen"
"instances whose component was past the layer limit were imported as frames" = "Instanzen, deren Komponente über dem Ebenenlimit lag, wurden als Rahmen importiert"
"invalid vector region fill paint list; layer fill used" = "Ungültige Füllungsliste der Vektorregion; Füllung der Ebene verwendet"
+1 -1
View File
@@ -1813,7 +1813,7 @@ name = "English"
"duplicate grid track sizing entries use equal tracks" = "duplicate grid track sizing entries use equal tracks"
"e.g. count + 1" = "e.g. count + 1"
"e.g. count >= 3 and not done" = "e.g. count >= 3 and not done"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "elbowed and curved connectors without a saved path follow FigCraft's own routing"
"elbowed and curved connectors without a saved path are routed afresh" = "elbowed and curved connectors without a saved path are routed afresh"
"entries without a node type were skipped" = "entries without a node type were skipped"
"figcraft:// links from a browser, chat or email open this app and join the session." = "figcraft:// links from a browser, chat or email open this app and join the session."
"from {server}" = "from {server}"
+1 -1
View File
@@ -1789,7 +1789,7 @@ status = "draft — needs native review"
"duplicate grid track sizing entries use equal tracks" = "päällekkäiset ruudukon kokomääritykset käyttävät tasakokoisia osia"
"e.g. count + 1" = "esim. count + 1"
"e.g. count >= 3 and not done" = "esim. count >= 3 and not done"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "kulmikkaat ja kaarevat yhdistimet, joilla ei ole tallennettua polkua, seuraavat FigCraftin omaa reititystä"
"elbowed and curved connectors without a saved path are routed afresh" = "kulmikkaat ja kaarevat yhdistimet, joilla ei ole tallennettua polkua, seuraavat FigCraftin omaa reititystä"
"entries without a node type were skipped" = "kohteet, joilta puuttuu solmutyyppi, ohitettiin"
"figcraft:// links from a browser, chat or email open this app and join the session." = "selaimen, keskustelun tai sähköpostin figcraft://-linkit avaavat tämän sovelluksen ja liittyvät istuntoon."
"from {server}" = "palvelimelta {server}"
+1 -1
View File
@@ -1810,7 +1810,7 @@ name = "Svenska"
"duplicate grid track sizing entries use equal tracks" = "dubbla storleksposter för rutnätsspår använder lika stora spår"
"e.g. count + 1" = "t.ex. count + 1"
"e.g. count >= 3 and not done" = "t.ex. count >= 3 and not done"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "vinklade och böjda kopplingar utan sparad bana följer FigCrafts egen dragning"
"elbowed and curved connectors without a saved path are routed afresh" = "vinklade och böjda kopplingar utan sparad bana följer FigCrafts egen dragning"
"entries without a node type were skipped" = "poster utan nodtyp hoppades över"
"figcraft:// links from a browser, chat or email open this app and join the session." = "figcraft://-länkar från en webbläsare, chatt eller e-post öppnar den här appen och ansluter till sessionen."
"from {server}" = "från {server}"
+1 -1
View File
@@ -1779,7 +1779,7 @@ status = "draft — needs native review"
"duplicate grid track sizing entries use equal tracks" = "重複的格線軌道大小項目會使用相等軌道"
"e.g. count + 1" = "例如 count + 1"
"e.g. count >= 3 and not done" = "例如 count >= 3 and not done"
"elbowed and curved connectors without a saved path follow FigCraft's own routing" = "沒有儲存路徑的肘形與曲線連接線會採用 FigCraft 自己的路徑規劃"
"elbowed and curved connectors without a saved path are routed afresh" = "沒有儲存路徑的肘形與曲線連接線會採用 FigCraft 自己的路徑規劃"
"entries without a node type were skipped" = "已略過沒有節點類型的項目"
"figcraft:// links from a browser, chat or email open this app and join the session." = "來自瀏覽器、聊天或電子郵件的 figcraft:// 連結會開啟此應用程式並加入工作階段。"
"from {server}" = "來自 {server}"
+21
View File
@@ -204,9 +204,30 @@ fn importer_notes() -> BTreeSet<String> {
keys.extend(literals(&std::fs::read_to_string(&path).unwrap(), &["warn("]));
}
}
// The `.fig` mapping itself is craft-fig's (`craft_fig::rest`); its notes are ours to translate.
let rest = craft_fig_src().join("rest");
for path in sources(&rest).into_iter().chain([rest.with_extension("rs")]) {
if !path.file_name().unwrap().to_string_lossy().contains("tests") {
keys.extend(literals(&std::fs::read_to_string(&path).unwrap(), &["warn("]));
}
}
keys
}
/// The `src` directory of the craft-fig sources this build uses, as Cargo resolved them.
fn craft_fig_src() -> std::path::PathBuf {
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
let out = std::process::Command::new(cargo)
.args(["metadata", "--format-version", "1", "--offline"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output()
.unwrap();
assert!(out.status.success(), "cargo metadata: {}", String::from_utf8_lossy(&out.stderr));
let meta: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
let package = meta["packages"].as_array().unwrap().iter().find(|p| p["name"] == "craft-fig").expect("craft-fig is a dependency");
std::path::Path::new(package["manifest_path"].as_str().unwrap()).parent().unwrap().join("src")
}
/// The lookup functions whose first argument is a catalog key.
const LOOKUPS: &[&str] = &["tr(", "trf(", "trn(", "tr_noop("];