Skip to content

Commit

Permalink
dev(export::svg): localize clip path definitions (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Jan 3, 2024
1 parent f528532 commit 4303f6e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 37 deletions.
9 changes: 6 additions & 3 deletions exporter/svg/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,13 @@ impl<C: BuildClipPath> TransformContext<C> for SvgTextBuilder {
}

fn transform_clip(mut self, ctx: &mut C, path: &ir::PathItem) -> Self {
let clip_id = ctx.build_clip_path(path);

let clip_id = ctx.build_clip_path(path).as_svg_id("c");
self.content.push(SvgText::Plain(format!(
r##"<clipPath id="{}"><path d="{}"/></clipPath>"##,
clip_id, path.d
)));
self.attributes
.push(("clip-path", format!(r"url(#{})", clip_id.as_svg_id("c"))));
.push(("clip-path", format!(r"url(#{})", clip_id)));
self
}
}
Expand Down
12 changes: 1 addition & 11 deletions exporter/svg/src/frontend/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ use super::HasStatefulFill;
/// Maps the style name to the style definition.
/// See [`StyleNs`].
pub(crate) type StyleDefMap = HashMap<(StyleNs, ImmutStr), String>;
/// Maps the clip path id to clip path's data.
pub(crate) type ClipPathMap = HashMap<ImmutStr, Fingerprint>;
/// Maps paint fill id to the paint fill's data.
pub(crate) type PaintFillMap = HashMap<ImmutStr, (u8, Fingerprint, Option<bool>)>;

Expand All @@ -54,8 +52,6 @@ pub struct RenderContext<'m, 't, Feat: ExportFeature> {
pub(crate) glyph_defs: &'t mut GlyphPackBuilder,
/// Stores the style definitions used in the document.
pub(crate) style_defs: &'t mut StyleDefMap,
/// Stores the clip paths used in the document.
pub(crate) clip_paths: &'t mut ClipPathMap,
/// Stores the graidents used in the document.
pub(crate) gradients: &'t mut PaintFillMap,
/// Stores the patterns used in the document.
Expand Down Expand Up @@ -143,13 +139,7 @@ impl<'m, 't, Feat: ExportFeature> BuildFillStyleClass for RenderContext<'m, 't,

impl<'m, 't, Feat: ExportFeature> BuildClipPath for RenderContext<'m, 't, Feat> {
fn build_clip_path(&mut self, path: &PathItem) -> Fingerprint {
if let Some(id) = self.clip_paths.get(&path.d) {
return *id;
}

let fingerprint = self.fingerprint_builder.resolve(path);
self.clip_paths.insert(path.d.clone(), fingerprint);
fingerprint
self.fingerprint_builder.resolve(path)
}
}

Expand Down
1 change: 0 additions & 1 deletion exporter/svg/src/frontend/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ impl IncrSvgDocClient {
// attach the clip paths, and style defs

svg.push(r#"<defs class="clip-path">"#.into());
IncrExporter::clip_paths(t.clip_paths, &mut svg);
IncrExporter::gradients(gradients, &mut svg);
IncrExporter::patterns(patterns.into_iter(), &mut svg);
svg.push("</defs>".into());
Expand Down
23 changes: 1 addition & 22 deletions exporter/svg/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use typst_ts_core::{
};

pub(crate) mod context;
use context::{ClipPathMap, RenderContext, StyleDefMap};
use context::{RenderContext, StyleDefMap};

pub(crate) mod dynamic_layout;
pub use dynamic_layout::DynamicLayoutSvgExporter;
Expand Down Expand Up @@ -84,22 +84,6 @@ impl<Feat: ExportFeature> SvgExporter<Feat> {
svg.push("</style>".into());
}

/// Render the clip paths for SVG
/// <svg> <defs> <clipPath/> </defs> .. </svg>
/// ^^^^^^^^^^^
/// See [`ClipPathMap`].
fn clip_paths(clip_paths: ClipPathMap, svg: &mut Vec<SvgText>) {
let mut clip_paths = clip_paths.into_iter().collect::<Vec<_>>();
clip_paths.sort_by(|a, b| a.1.cmp(&b.1));
for (clip_path, id) in clip_paths {
svg.push(SvgText::Plain(format!(
r##"<clipPath id="{}"><path d="{}"/></clipPath>"##,
id.as_svg_id("c"),
clip_path
)));
}
}

/// Render the gradients for SVG
/// <svg> <defs> <gradient/> </defs> .. </svg>
/// ^^^^^^^^^^^
Expand Down Expand Up @@ -381,7 +365,6 @@ impl<Feat: ExportFeature> SvgExporter<Feat> {
svg.extend(glyphs);
svg.push("</defs>".into());
svg.push(r#"<defs class="clip-path">"#.into());
Self::clip_paths(t.clip_paths, &mut svg);
Self::gradients(gradients, &mut svg);
Self::patterns(patterns.into_iter(), &mut svg);
svg.push("</defs>".into());
Expand Down Expand Up @@ -421,8 +404,6 @@ pub struct SvgTask<Feat: ExportFeature> {
pub(crate) glyph_defs: GlyphPackBuilder,
/// Stores the style definitions used in the document.
pub(crate) style_defs: StyleDefMap,
/// Stores the clip paths used in the document.
pub(crate) clip_paths: ClipPathMap,
/// Stores the gradient used in the document.
pub(crate) gradients: PaintFillMap,
/// Stores the patterns used in the document.
Expand All @@ -441,7 +422,6 @@ impl<Feat: ExportFeature> Default for SvgTask<Feat> {

glyph_defs: GlyphPackBuilder::default(),
style_defs: StyleDefMap::default(),
clip_paths: ClipPathMap::default(),
gradients: PaintFillMap::default(),
patterns: PaintFillMap::default(),

Expand Down Expand Up @@ -482,7 +462,6 @@ impl<Feat: ExportFeature> SvgTask<Feat> {

glyph_defs: &mut self.glyph_defs,
style_defs: &mut self.style_defs,
clip_paths: &mut self.clip_paths,
gradients: &mut self.gradients,
patterns: &mut self.patterns,

Expand Down

0 comments on commit 4303f6e

Please sign in to comment.