From 4fda03abdc240f7aa8a3853a1a2c674e0cd2f192 Mon Sep 17 00:00:00 2001 From: Nixon <43715558+nixon-voxell@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:05:20 +0800 Subject: [PATCH] Animation-friendly export (#4822) --- crates/typst-svg/src/lib.rs | 4 ++++ crates/typst/src/layout/container.rs | 11 +++++++++++ crates/typst/src/layout/frame.rs | 9 ++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/typst-svg/src/lib.rs b/crates/typst-svg/src/lib.rs index 145e23f86..deb5fb00c 100644 --- a/crates/typst-svg/src/lib.rs +++ b/crates/typst-svg/src/lib.rs @@ -240,6 +240,10 @@ impl SVGRenderer { self.xml.start_element("g"); self.xml.write_attribute("class", "typst-group"); + if let Some(label) = group.label { + self.xml.write_attribute("data-typst-label", label.as_str()); + } + if let Some(clip_path) = &group.clip_path { let hash = hash128(&group); let id = self.clip_paths.insert_with(hash, || shape::convert_path(clip_path)); diff --git a/crates/typst/src/layout/container.rs b/crates/typst/src/layout/container.rs index 443e660ab..5f4a7cd2b 100644 --- a/crates/typst/src/layout/container.rs +++ b/crates/typst/src/layout/container.rs @@ -196,6 +196,10 @@ impl Packed { frame.fill_and_stroke(fill, &stroke, &outset, &radius, self.span()); } + if let Some(label) = self.label() { + frame.group(|group| group.label = Some(label)) + } + Ok(frame) } @@ -660,6 +664,13 @@ impl Packed { } } + // Assign label to each frame in the fragment. + if let Some(label) = self.label() { + for frame in fragment.iter_mut() { + frame.group(|group| group.label = Some(label)) + } + } + Ok(fragment) } diff --git a/crates/typst/src/layout/frame.rs b/crates/typst/src/layout/frame.rs index 09a4362de..60b690dd3 100644 --- a/crates/typst/src/layout/frame.rs +++ b/crates/typst/src/layout/frame.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use smallvec::SmallVec; -use crate::foundations::{cast, dict, Dict, StyleChain, Value}; +use crate::foundations::{cast, dict, Dict, Label, StyleChain, Value}; use crate::introspection::Tag; use crate::layout::{ Abs, Axes, Corners, FixedAlignment, HideElem, Length, Point, Rel, Sides, Size, @@ -380,7 +380,7 @@ impl Frame { styled_rect(size, radius, fill, stroke) .into_iter() .map(|x| (pos, FrameItem::Shape(x, span))), - ) + ); } /// Arbitrarily transform the contents of the frame. @@ -402,7 +402,7 @@ impl Frame { } /// Wrap the frame's contents in a group and modify that group with `f`. - fn group(&mut self, f: F) + pub fn group(&mut self, f: F) where F: FnOnce(&mut GroupItem), { @@ -549,6 +549,8 @@ pub struct GroupItem { pub transform: Transform, /// Whether the frame should be a clipping boundary. pub clip_path: Option, + /// The group's label. + pub label: Option