typst-svg 0.15.0

SVG exporter for Typst.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Rendering of Typst documents into SVG images.

mod image;
mod paint;
mod path;
mod shape;
mod text;
mod write;

use comemo::Tracked;
pub use image::{WebImage, convert_image_scaling};
use indexmap::IndexMap;
use rustc_hash::FxBuildHasher;
use typst_library::model::{Destination, LateLinkResolver};

use std::hash::Hash;

use ecow::EcoString;
use typst_layout::{Page, PagedDocument};
use typst_library::layout::{
    Abs, Frame, FrameItem, FrameKind, GroupItem, Point, Ratio, Sides, Size, Transform,
};
use typst_library::visualize::{Geometry, Gradient, Tiling};
use xmlwriter::XmlWriter;

use crate::paint::{GradientRef, SVGSubGradient, TilingRef};
use crate::text::RenderedGlyph;
use crate::write::{SvgDisplay, SvgElem, SvgTransform, SvgUrl, SvgWrite};

/// Export a frame into an SVG file.
#[typst_macros::time(name = "svg")]
pub fn svg(page: &Page, opts: &SvgOptions) -> String {
    let (size, ts) = page_bleed(page, opts);

    let mut renderer = SVGRenderer::new();
    let mut xml = XmlWriter::new(xml_options(opts.pretty));
    let mut svg = svg_header(&mut xml, size);

    let state = State::new(size);
    renderer.render_page(&mut svg, &state, ts, page);
    renderer.finalize(svg);
    xml.end_document()
}

/// Export a page into an SVG file as part of a bundle.
///
/// Takes additional `anchor` locations that will be serialized as linkable
/// points. This enables other documents in the bundle to link into the
/// resulting SVG. Also takes a `link_resolver` for resolving cross-document
/// links.
#[typst_macros::time(name = "svg in bundle")]
pub fn svg_in_bundle(
    page: &Page,
    opts: &SvgOptions,
    anchors: &[(Point, EcoString)],
    link_resolver: Tracked<LateLinkResolver>,
) -> String {
    let (size, ts) = page_bleed(page, opts);

    let mut renderer = SVGRenderer::with_options(Some(link_resolver));
    let mut xml = XmlWriter::new(xml_options(opts.pretty));
    let mut svg = svg_header(&mut xml, size);

    let state = State::new(size);
    renderer.render_page(&mut svg, &state, ts, page);

    for (pos, id) in anchors {
        renderer.render_anchor(&mut svg, *pos, id);
    }

    renderer.finalize(svg);
    xml.end_document()
}

/// Export a frame into an SVG suitable for embedding into HTML.
///
/// Takes additional `anchor` locations that will be serialized as linkable
/// points. This enables other documents in the bundle to link into the
/// resulting SVG. Also takes a `link_resolver` for resolving links between the
/// frame and remaining document.
#[typst_macros::time(name = "svg in html")]
pub fn svg_in_html(
    frame: &Frame,
    text_size: Abs,
    pretty: bool,
    id: Option<&str>,
    styles: &str,
    anchors: &[(Point, EcoString)],
    link_resolver: Tracked<LateLinkResolver>,
) -> String {
    let mut renderer = SVGRenderer::with_options(Some(link_resolver));
    let mut xml = XmlWriter::new(xmlwriter::Options {
        indent: xmlwriter::Indent::None,
        ..xml_options(pretty)
    });
    let mut svg = svg_header_with_custom_attrs(&mut xml, frame.size(), |svg| {
        if let Some(id) = id {
            svg.attr("id", id);
        }
        svg.attr_with("style", |attr| {
            // TODO: Maybe make this a little more elegant?
            attr.push_str("overflow: visible; width: ");
            attr.push_num(frame.width() / text_size);
            attr.push_str("em; height: ");
            attr.push_num(frame.height() / text_size);
            attr.push_str("em;");
            if !styles.is_empty() {
                attr.push_str(" ");
                attr.push_str(styles);
            }
        });
    });

    let state = State::new(frame.size());
    renderer.render_frame(&mut svg, &state, frame);

    for (pos, id) in anchors {
        renderer.render_anchor(&mut svg, *pos, id);
    }

    renderer.finalize(svg);
    xml.end_document()
}

/// Export a document with potentially multiple pages into a single SVG file.
///
/// The gap will be added between the individual pages.
pub fn svg_merged(document: &PagedDocument, opts: &SvgOptions, gap: Abs) -> String {
    let num_gaps = document.pages().len().saturating_sub(1) as f64;
    let mut size = Size::new(Abs::zero(), num_gaps * gap);
    for page in document.pages() {
        let (page_size, _ts) = page_bleed(page, opts);
        size.x.set_max(page_size.x);
        size.y += page_size.y;
    }

    let mut renderer = SVGRenderer::new();
    let mut xml = XmlWriter::new(xml_options(opts.pretty));
    let mut svg = svg_header(&mut xml, size);

    let mut y = Abs::zero();
    for page in document.pages() {
        let (page_size, bleed_ts) = page_bleed(page, opts);
        let state = State::new(page_size);
        renderer.render_page(
            &mut svg,
            &state,
            Transform::translate(Abs::zero(), y).pre_concat(bleed_ts),
            page,
        );
        y += page_size.y + gap;
    }

    renderer.finalize(svg);
    xml.end_document()
}

fn page_bleed(page: &Page, opts: &SvgOptions) -> (Size, Transform) {
    let bleed = if opts.render_bleed { page.bleed } else { Sides::default() };
    let size = page.frame.size() + bleed.sum_by_axis();
    let ts = Transform::translate(bleed.left, bleed.top);
    (size, ts)
}

fn xml_options(pretty: bool) -> xmlwriter::Options {
    xmlwriter::Options {
        use_single_quote: false,
        indent: if pretty {
            xmlwriter::Indent::Spaces(2)
        } else {
            xmlwriter::Indent::None
        },
        attributes_indent: xmlwriter::Indent::None,
    }
}

/// Settings for SVG export.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct SvgOptions {
    /// By default, SVG documents are bounded to the page size. In some
    /// circumstances, such as when preparing documents for print, it may be
    /// desirable to include content beyond these bounds to account for bleed
    /// margins. This field allows expanding the document area to include such
    /// bleed.
    pub render_bleed: bool,
    /// Whether to format the SVG in a human-readable way.
    pub pretty: bool,
}

/// Renders one or multiple frames to an SVG file.
struct SVGRenderer<'a> {
    /// The document's introspector, if we're writing an HTML frame.
    link_resolver: Option<Tracked<'a, LateLinkResolver<'a>>>,
    /// Prepared glyphs.
    glyphs: Deduplicator<Option<RenderedGlyph>>,
    /// Clip paths are used to clip a group. A clip path is a path that defines
    /// the clipping region. The clip path is referenced by the `clip-path`
    /// attribute of the group. The clip path is in the format of `M x y L x y C
    /// x1 y1 x2 y2 x y Z`.
    clip_paths: Deduplicator<EcoString>,
    /// These are the actual gradients being written in the SVG file.
    /// These gradients are deduplicated because they do not contain the transform
    /// matrix, allowing them to be reused across multiple invocations.
    ///
    /// The `Ratio` is the aspect ratio of the gradient, this is used to correct
    /// the angle of the gradient.
    gradients: Deduplicator<(Gradient, Ratio)>,
    /// Deduplicated gradients with transform matrices. They use a reference
    /// (`href`) to a "source" gradient instead of being defined inline.
    /// This saves a lot of space since gradients are often reused but with
    /// different transforms. Therefore this allows us to reuse the same gradient
    /// multiple times.
    gradient_refs: Deduplicator<GradientRef>,
    /// These are the gradients that compose a conic gradient.
    conic_subgradients: Deduplicator<SVGSubGradient>,
    /// These are the actual tilings being written in the SVG file.
    /// These tilings are deduplicated because they do not contain the transform
    /// matrix, allowing them to be reused across multiple invocations.
    ///
    /// The `String` is the rendered tiling frame.
    tilings: Deduplicator<Tiling>,
    /// Deduplicated tilings with transform matrices. They use a reference
    /// (`href`) to a "source" tiling instead of being defined inline.
    /// This saves a lot of space since tilings are often reused but with
    /// different transforms. Therefore this allows us to reuse the same gradient
    /// multiple times.
    tiling_refs: Deduplicator<TilingRef>,
}

/// Contextual information for rendering.
#[derive(Copy, Clone)]
struct State {
    /// The transform of the current item.
    transform: Transform,
    /// The size of the first hard frame in the hierarchy.
    size: Size,
}

impl State {
    fn new(size: Size) -> Self {
        Self { size, transform: Transform::identity() }
    }

    /// Pre translate the current item's transform.
    fn pre_translate(self, pos: Point) -> Self {
        self.pre_concat(Transform::translate(pos.x, pos.y))
    }

    /// Pre concat the current item's transform.
    fn pre_concat(self, transform: Transform) -> Self {
        Self {
            transform: self.transform.pre_concat(transform),
            ..self
        }
    }

    /// Sets the size of the first hard frame in the hierarchy.
    fn with_size(self, size: Size) -> Self {
        Self { size, ..self }
    }

    /// Sets the current item's transform.
    fn with_transform(self, transform: Transform) -> Self {
        Self { transform, ..self }
    }
}

impl<'a> SVGRenderer<'a> {
    /// Create a new SVG renderer with empty glyph and clip path.
    fn new() -> Self {
        Self::with_options(None)
    }

    /// Create a new SVG renderer with the given configuration.
    fn with_options(link_resolver: Option<Tracked<'a, LateLinkResolver<'a>>>) -> Self {
        SVGRenderer {
            link_resolver,
            glyphs: Deduplicator::new('g'),
            clip_paths: Deduplicator::new('c'),
            gradients: Deduplicator::new('f'),
            gradient_refs: Deduplicator::new('r'),
            conic_subgradients: Deduplicator::new('s'),
            tilings: Deduplicator::new('t'),
            tiling_refs: Deduplicator::new('p'),
        }
    }

    /// Render a page with the given transform.
    fn render_page(
        &mut self,
        svg: &mut SvgElem,
        state: &State,
        ts: Transform,
        page: &Page,
    ) {
        let mut svg = svg.lazy_elem("g");
        if !ts.is_identity() {
            svg.init().attr("transform", SvgTransform(ts));
        }

        if let Some(fill) = page.fill_or_white() {
            let shape =
                Geometry::Rect(page.frame.size() + page.bleed.sum_by_axis()).filled(fill);
            let state =
                &state.pre_translate(Point { x: -page.bleed.left, y: -page.bleed.top });
            self.render_shape(svg.lazy(), state, &shape);
        }

        self.render_frame(svg.lazy(), state, &page.frame);
    }

    /// Render a frame with the given transform.
    fn render_frame(&mut self, svg: &mut SvgElem, state: &State, frame: &Frame) {
        for (pos, item) in frame.items() {
            let state = state.pre_translate(*pos);
            match item {
                FrameItem::Group(group) => self.render_group(svg, &state, group),
                FrameItem::Text(text) => self.render_text(svg, &state, text),
                FrameItem::Shape(shape, _) => self.render_shape(svg, &state, shape),
                FrameItem::Image(image, size, _) => {
                    self.render_image(svg, &state, image, size)
                }
                FrameItem::Link(dest, size) => self.render_link(svg, &state, dest, *size),
                FrameItem::Tag(_) => {}
            };
        }
    }

    /// Render a group. If the group has `clips` set to true, a clip path will
    /// be created.
    fn render_group(&mut self, svg: &mut SvgElem, state: &State, group: &GroupItem) {
        let mut svg = svg.lazy_elem("g");

        let state = match group.frame.kind() {
            FrameKind::Soft => state.pre_concat(group.transform),
            FrameKind::Hard => {
                // Always generate a group for hard frames.
                svg.init();

                let transform = state.transform.pre_concat(group.transform);
                if !transform.is_identity() {
                    svg.init().attr("transform", SvgTransform(transform));
                }
                state
                    .with_transform(Transform::identity())
                    .with_size(group.frame.size())
            }
        };

        if let Some(label) = group.label {
            svg.init().attr("data-typst-label", label.resolve());
        }

        if let Some(clip_curve) = &group.clip {
            let offset = Point::new(state.transform.tx, state.transform.ty);
            let id = self.clip_paths.insert_with((clip_curve, offset), || {
                shape::convert_curve(offset, clip_curve)
            });
            svg.init().attr("clip-path", SvgUrl(id));
        }

        self.render_frame(svg.lazy(), &state, &group.frame);
    }

    /// Render a link element.
    fn render_link(
        &mut self,
        svg: &mut SvgElem,
        state: &State,
        dest: &Destination,
        size: Size,
    ) {
        let mut a = svg.elem("a");
        if !state.transform.is_identity() {
            a.attr("transform", SvgTransform(state.transform));
        }

        match dest {
            Destination::Url(url) => {
                a.attr("href", url.as_str());
                a.attr("xlink:href", url.as_str());
            }
            Destination::Position(_) => {
                // TODO: Links on the same page could be supported.
            }
            Destination::Location(loc) => {
                // TODO: Location links on the same page could also be supported
                // outside of HTML.
                if let Some(resolver) = self.link_resolver
                    && let Some(link) = resolver.resolve(*loc)
                    && let Ok(uri) = link.into_relative_uri()
                {
                    a.attr("href", &uri);
                    a.attr("xlink:href", &uri);
                }
            }
        }

        a.elem("rect")
            .attr("width", size.x.to_pt())
            .attr("height", size.y.to_pt())
            .attr("fill", "transparent")
            .attr("stroke", "none");
    }

    /// Renders a linkable point that can be used to link into an HTML frame.
    fn render_anchor(&mut self, svg: &mut SvgElem, pos: Point, id: &str) {
        svg.elem("g")
            .attr("id", id)
            .attr("transform", SvgTransform(Transform::translate(pos.x, pos.y)));
    }

    /// Finalize the SVG file. This must be called after all rendering is done.
    fn finalize(mut self, mut svg: SvgElem) {
        self.write_glyph_defs(&mut svg);
        self.write_clip_path_defs(&mut svg);
        self.write_gradients(&mut svg);
        self.write_gradient_refs(&mut svg);
        self.write_subgradients(&mut svg);
        self.write_tilings(&mut svg);
        self.write_tiling_refs(&mut svg);
    }

    /// Build the clip path definitions.
    fn write_clip_path_defs(&self, svg: &mut SvgElem) {
        if self.clip_paths.is_empty() {
            return;
        }

        let mut defs = svg.elem("defs");
        for (id, path) in self.clip_paths.iter() {
            defs.elem("clipPath").attr("id", id).with(|svg| {
                svg.elem("path").attr("d", path);
            });
        }
    }
}

/// Write the default SVG header, including a `typst-doc` class, the
/// `viewBox` and `width` and `height` attributes.
fn svg_header(xml: &mut XmlWriter, size: Size) -> SvgElem<'_> {
    svg_header_with_custom_attrs(xml, size, |_| {})
}

/// Write the SVG header with additional attributes and standard attributes.
fn svg_header_with_custom_attrs(
    xml: &mut XmlWriter,
    size: Size,
    write_custom_attrs: impl FnOnce(&mut SvgElem),
) -> SvgElem<'_> {
    // Clamp the size of SVGs to at least one pt. resvg and probably also
    // other SVG parsers don't handle SVGs with 0 sized dimensions.
    let size = size.max(Size::splat(Abs::pt(1.0)));

    let mut svg = SvgElem::new(xml, "svg");

    write_custom_attrs(&mut svg);

    svg.attr_with("viewBox", |attr| {
        attr.push_nums([0.0, 0.0, size.x.to_pt(), size.y.to_pt()])
    });
    svg.attr_with("width", |attr| {
        attr.push_num(size.x.to_pt());
        attr.push_str("pt");
    });
    svg.attr_with("height", |attr| {
        attr.push_num(size.y.to_pt());
        attr.push_str("pt");
    });
    svg.attr("xmlns", "http://www.w3.org/2000/svg");
    svg.attr("xmlns:xlink", "http://www.w3.org/1999/xlink");
    svg.attr("xmlns:h5", "http://www.w3.org/1999/xhtml");

    svg
}

/// Deduplicates its elements. It is used to deduplicate glyphs and clip paths.
/// The `H` is the hash type, and `T` is the value type. The `PREFIX` is the
/// prefix of the index. This is used to distinguish between glyphs and clip
/// paths.
#[derive(Debug, Default, Clone)]
struct Deduplicator<T> {
    kind: char,
    map: IndexMap<u128, T, FxBuildHasher>,
}

impl<T> Deduplicator<T> {
    fn new(kind: char) -> Self {
        Self { kind, map: IndexMap::default() }
    }

    /// Inserts a value into the vector. If the hash is already present, returns
    /// the index of the existing value and `f` will not be called. Otherwise,
    /// inserts the value and returns the id of the inserted value.
    #[must_use = "returns the id of the inserted value"]
    fn insert_with<K, F>(&mut self, key: K, f: F) -> DedupId
    where
        K: Hash,
        F: FnOnce() -> T,
    {
        self.insert_with_val(key, f).0
    }

    /// Same as [`Self::insert_with`], but it also returns a reference to the
    /// cached or inserted value.
    #[must_use]
    fn insert_with_val<K, F>(&mut self, key: K, f: F) -> (DedupId, &mut T)
    where
        K: Hash,
        F: FnOnce() -> T,
    {
        let hash = typst_utils::hash128(&key);
        let val = self.map.entry(hash).or_insert_with(f);
        (DedupId(self.kind, hash), val)
    }

    /// Iterate over the elements alongside their ids.
    fn iter(&self) -> impl Iterator<Item = (DedupId, &T)> {
        self.map.iter().map(|(hash, v)| (DedupId(self.kind, *hash), v))
    }

    /// Returns true if the deduplicator is empty.
    fn is_empty(&self) -> bool {
        self.map.is_empty()
    }
}

/// Identifies a `<def>`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct DedupId(char, u128);

impl SvgDisplay for DedupId {
    fn fmt(&self, f: &mut impl SvgWrite) {
        let Self(kind, hash) = *self;
        f.push_char(kind);

        let mut digits = [0; 32];
        for (i, byte) in hash.to_be_bytes().into_iter().enumerate() {
            digits[2 * i] = to_hex_digit((byte >> 4) & 0x0F);
            digits[2 * i + 1] = to_hex_digit(byte & 0x0F);
        }

        // The digits are all valid ASCII hex characters.
        let str = std::str::from_utf8(&digits).unwrap();
        f.push_str(str.trim_start_matches('0'));

        fn to_hex_digit(nibble: u8) -> u8 {
            match nibble {
                0..10 => b'0' + nibble,
                _ => b'A' + (nibble - 10),
            }
        }
    }
}