Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exporter::svg): disable inlined svg #407

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions exporter/svg/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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, ""))
Expand Down
5 changes: 5 additions & 0 deletions exporter/svg/src/frontend/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions exporter/svg/src/frontend/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions exporter/svg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -76,6 +79,7 @@ pub struct DefaultExportFeature;
pub type DefaultSvgTask = SvgTask<DefaultExportFeature>;

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;
Expand All @@ -90,6 +94,7 @@ pub struct SvgExportFeature;
pub type PlainSvgTask = SvgTask<SvgExportFeature>;

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;
Expand Down