From 1b77457cf2598cb4ce2ce35793139553d39872e7 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sat, 11 Nov 2023 14:34:42 +0800 Subject: [PATCH] dev: disable inlined svg --- exporter/svg/src/backend/mod.rs | 19 ++++++++++++------- exporter/svg/src/frontend/context.rs | 5 +++++ exporter/svg/src/frontend/incremental.rs | 1 + exporter/svg/src/lib.rs | 5 +++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/exporter/svg/src/backend/mod.rs b/exporter/svg/src/backend/mod.rs index af1055402..8daeb486a 100644 --- a/exporter/svg/src/backend/mod.rs +++ b/exporter/svg/src/backend/mod.rs @@ -35,6 +35,8 @@ pub trait NotifyPaint { } pub trait DynExportFeature { + fn enable_inlined_svg(&self) -> bool; + fn should_render_text_element(&self) -> bool; fn use_stable_glyph_id(&self) -> bool; @@ -487,8 +489,9 @@ impl< self.content.push(render_path(path, state, abs_ref)) } - fn render_image(&mut self, _ctx: &mut C, image_item: &ir::ImageItem) { - self.content.push(render_image_item(image_item)) + fn render_image(&mut self, ctx: &mut C, image_item: &ir::ImageItem) { + self.content + .push(render_image_item(image_item, ctx.enable_inlined_svg())) } #[inline] @@ -687,12 +690,14 @@ fn render_path(path: &ir::PathItem, state: RenderState, abs_ref: &Fingerprint) - /// Render a [`ir::ImageItem`] into svg text. #[comemo::memoize] -fn render_image_item(img: &ir::ImageItem) -> SvgText { - match &img.image.alt { - Some(t) if t.as_ref() == "!typst-inlined-svg" => { - return SvgText::Plain(String::from_utf8(img.image.data.clone()).unwrap()) +fn render_image_item(img: &ir::ImageItem, enable_inlined: bool) -> SvgText { + if enable_inlined { + match &img.image.alt { + Some(t) if t.as_ref() == "!typst-inlined-svg" => { + return SvgText::Plain(String::from_utf8(img.image.data.clone()).unwrap()) + } + _ => {} } - _ => {} } SvgText::Plain(render_image(&img.image, img.size, true, "")) diff --git a/exporter/svg/src/frontend/context.rs b/exporter/svg/src/frontend/context.rs index 8b94aa341..ced7cb971 100644 --- a/exporter/svg/src/frontend/context.rs +++ b/exporter/svg/src/frontend/context.rs @@ -73,6 +73,11 @@ pub struct RenderContext<'m, 't, Feat: ExportFeature> { } impl<'m, 't, Feat: ExportFeature> DynExportFeature for RenderContext<'m, 't, Feat> { + #[inline] + fn enable_inlined_svg(&self) -> bool { + Feat::ENABLE_INLINED_SVG + } + #[inline] fn should_render_text_element(&self) -> bool { Feat::SHOULD_RENDER_TEXT_ELEMENT && self.should_render_text_element diff --git a/exporter/svg/src/frontend/incremental.rs b/exporter/svg/src/frontend/incremental.rs index e161213d7..0b756974a 100644 --- a/exporter/svg/src/frontend/incremental.rs +++ b/exporter/svg/src/frontend/incremental.rs @@ -24,6 +24,7 @@ use crate::{ struct IncrementalExportFeature; impl ExportFeature for IncrementalExportFeature { + const ENABLE_INLINED_SVG: bool = false; const ENABLE_TRACING: bool = false; const SHOULD_ATTACH_DEBUG_INFO: bool = false; const SHOULD_RENDER_TEXT_ELEMENT: bool = true; diff --git a/exporter/svg/src/lib.rs b/exporter/svg/src/lib.rs index fe33f3a5b..21b1392a2 100644 --- a/exporter/svg/src/lib.rs +++ b/exporter/svg/src/lib.rs @@ -50,6 +50,9 @@ pub trait ExportFeature { /// Whether to attach debug info to svg elements. const SHOULD_ATTACH_DEBUG_INFO: bool; + /// Whether to enable inlined svg. + const ENABLE_INLINED_SVG: bool; + /// Whether to render text element. /// The text elements is selectable and searchable. const SHOULD_RENDER_TEXT_ELEMENT: bool; @@ -76,6 +79,7 @@ pub struct DefaultExportFeature; pub type DefaultSvgTask = SvgTask; impl ExportFeature for DefaultExportFeature { + const ENABLE_INLINED_SVG: bool = false; const ENABLE_TRACING: bool = false; const SHOULD_ATTACH_DEBUG_INFO: bool = false; const SHOULD_RENDER_TEXT_ELEMENT: bool = true; @@ -90,6 +94,7 @@ pub struct SvgExportFeature; pub type PlainSvgTask = SvgTask; impl ExportFeature for SvgExportFeature { + const ENABLE_INLINED_SVG: bool = false; const ENABLE_TRACING: bool = false; const SHOULD_ATTACH_DEBUG_INFO: bool = false; const SHOULD_RENDER_TEXT_ELEMENT: bool = true;