summaryrefslogtreecommitdiff
path: root/tests/typeset.rs
blob: a4e20774f0b680449fe120e755892b433cb60e00 (plain) (blame)
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::ops::Range;
use std::path::Path;
use std::rc::Rc;

use image::{GenericImageView, Rgba};
use tiny_skia as sk;
use ttf_parser::{GlyphId, OutlineBuilder};
use usvg::FitTo;
use walkdir::WalkDir;

use typst::diag::Error;
use typst::eval::{Smart, StyleMap, Value};
use typst::font::Face;
use typst::frame::{Element, Frame, Geometry, Shape, Stroke, Text};
use typst::geom::{self, Color, Length, Paint, PathElement, RgbaColor, Size, Transform};
use typst::image::{Image, RasterImage, Svg};
use typst::library::{PageNode, TextNode};
use typst::loading::FsLoader;
use typst::parse::Scanner;
use typst::source::SourceFile;
use typst::syntax::Span;
use typst::Context;

#[cfg(feature = "layout-cache")]
use {
    filedescriptor::{FileDescriptor, StdioDescriptor::*},
    std::fs::File,
    typst::layout::RootNode,
};

const TYP_DIR: &str = "./typ";
const REF_DIR: &str = "./ref";
const PNG_DIR: &str = "./png";
const PDF_DIR: &str = "./pdf";
const FONT_DIR: &str = "../fonts";

fn main() {
    env::set_current_dir(env::current_dir().unwrap().join("tests")).unwrap();

    let args = Args::new(env::args().skip(1));
    let mut filtered = Vec::new();

    // Since differents tests can affect each other through the layout cache, a
    // deterministic order is very important for reproducibility.
    for entry in WalkDir::new(".").sort_by_file_name() {
        let entry = entry.unwrap();
        if entry.depth() <= 1 {
            continue;
        }

        let src_path = entry.into_path();
        if src_path.extension() != Some(OsStr::new("typ")) {
            continue;
        }

        if args.matches(&src_path) {
            filtered.push(src_path);
        }
    }

    let len = filtered.len();
    if len == 1 {
        println!("Running test ...");
    } else if len > 1 {
        println!("Running {len} tests");
    }

    // Set page width to 120pt with 10pt margins, so that the inner page is
    // exactly 100pt wide. Page height is unbounded and font size is 10pt so
    // that it multiplies to nice round numbers.
    let mut styles = StyleMap::new();
    styles.set(PageNode::WIDTH, Smart::Custom(Length::pt(120.0)));
    styles.set(PageNode::HEIGHT, Smart::Auto);
    styles.set(PageNode::LEFT, Smart::Custom(Length::pt(10.0).into()));
    styles.set(PageNode::TOP, Smart::Custom(Length::pt(10.0).into()));
    styles.set(PageNode::RIGHT, Smart::Custom(Length::pt(10.0).into()));
    styles.set(PageNode::BOTTOM, Smart::Custom(Length::pt(10.0).into()));
    styles.set(TextNode::SIZE, Length::pt(10.0).into());

    // Hook up an assert function into the global scope.
    let mut std = typst::library::new();
    std.def_func("test", move |_, args| {
        let lhs = args.expect::<Value>("left-hand side")?;
        let rhs = args.expect::<Value>("right-hand side")?;
        if lhs != rhs {
            return Err(Error::boxed(
                args.span,
                format!("Assertion failed: {:?} != {:?}", lhs, rhs),
            ));
        }
        Ok(Value::None)
    });

    // Create loader and context.
    let loader = FsLoader::new().with_path(FONT_DIR).wrap();
    let mut ctx = Context::builder().std(std).styles(styles).build(loader);

    // Run all the tests.
    let mut ok = 0;
    for src_path in filtered {
        let path = src_path.strip_prefix(TYP_DIR).unwrap();
        let png_path = Path::new(PNG_DIR).join(path).with_extension("png");
        let ref_path = Path::new(REF_DIR).join(path).with_extension("png");
        let pdf_path =
            args.pdf.then(|| Path::new(PDF_DIR).join(path).with_extension("pdf"));

        ok += test(
            &mut ctx,
            &src_path,
            &png_path,
            &ref_path,
            pdf_path.as_deref(),
            args.debug,
        ) as usize;
    }

    if len > 1 {
        println!("{ok} / {len} tests passed.");
    }

    if ok < len {
        std::process::exit(1);
    }
}

struct Args {
    filter: Vec<String>,
    exact: bool,
    debug: bool,
    pdf: bool,
}

impl Args {
    fn new(args: impl Iterator<Item = String>) -> Self {
        let mut filter = Vec::new();
        let mut exact = false;
        let mut debug = false;
        let mut pdf = false;

        for arg in args {
            match arg.as_str() {
                // Ignore this, its for cargo.
                "--nocapture" => {}
                // Match only the exact filename.
                "--exact" => exact = true,
                // Generate PDFs.
                "--pdf" => pdf = true,
                // Debug print the layout trees.
                "--debug" | "-d" => debug = true,
                // Everything else is a file filter.
                _ => filter.push(arg),
            }
        }

        Self { filter, pdf, debug, exact }
    }

    fn matches(&self, path: &Path) -> bool {
        if self.exact {
            let name = path.file_name().unwrap().to_string_lossy();
            self.filter.iter().any(|v| v == &name)
        } else {
            let path = path.to_string_lossy();
            self.filter.is_empty() || self.filter.iter().any(|v| path.contains(v))
        }
    }
}

fn test(
    ctx: &mut Context,
    src_path: &Path,
    png_path: &Path,
    ref_path: &Path,
    pdf_path: Option<&Path>,
    debug: bool,
) -> bool {
    let name = src_path.strip_prefix(TYP_DIR).unwrap_or(src_path);
    println!("Testing {}", name.display());

    let src = fs::read_to_string(src_path).unwrap();

    let mut ok = true;
    let mut frames = vec![];
    let mut line = 0;
    let mut compare_ref = true;
    let mut compare_ever = false;
    let mut rng = LinearShift::new();

    let parts: Vec<_> = src.split("\n---").collect();
    for (i, &part) in parts.iter().enumerate() {
        let is_header = i == 0
            && parts.len() > 1
            && part
                .lines()
                .all(|s| s.starts_with("//") || s.chars().all(|c| c.is_whitespace()));

        if is_header {
            for line in part.lines() {
                if line.starts_with("// Ref: false") {
                    compare_ref = false;
                }
            }
        } else {
            let (part_ok, compare_here, part_frames) = test_part(
                ctx,
                src_path,
                part.into(),
                i,
                compare_ref,
                line,
                debug,
                &mut rng,
            );
            ok &= part_ok;
            compare_ever |= compare_here;
            frames.extend(part_frames);
        }

        line += part.lines().count() + 1;
    }

    if compare_ever {
        if let Some(pdf_path) = pdf_path {
            let pdf_data = typst::export::pdf(ctx, &frames);
            fs::create_dir_all(&pdf_path.parent().unwrap()).unwrap();
            fs::write(pdf_path, pdf_data).unwrap();
        }

        let canvas = draw(ctx, &frames, 2.0);
        fs::create_dir_all(&png_path.parent().unwrap()).unwrap();
        canvas.save_png(png_path).unwrap();

        if let Ok(ref_pixmap) = sk::Pixmap::load_png(ref_path) {
            if canvas != ref_pixmap {
                println!("  Does not match reference image. ❌");
                ok = false;
            }
        } else if !frames.is_empty() {
            println!("  Failed to open reference image. ❌");
            ok = false;
        }
    }

    if ok {
        if !debug {
            print!("\x1b[1A");
        }
        println!("Testing {} ✔", name.display());
    }

    ok
}

fn test_part(
    ctx: &mut Context,
    src_path: &Path,
    src: String,
    i: usize,
    compare_ref: bool,
    line: usize,
    debug: bool,
    rng: &mut LinearShift,
) -> (bool, bool, Vec<Rc<Frame>>) {
    let id = ctx.sources.provide(src_path, src);
    let source = ctx.sources.get(id);

    let (local_compare_ref, mut ref_errors) = parse_metadata(&source);
    let compare_ref = local_compare_ref.unwrap_or(compare_ref);
    let mut ok = test_reparse(ctx.sources.get(id).src(), i, rng);

    let (frames, mut errors) = match ctx.evaluate(id) {
        Ok(module) => {
            let tree = module.into_root();
            if debug {
                println!("{tree:#?}");
            }

            let mut frames = tree.layout(ctx);

            #[cfg(feature = "layout-cache")]
            (ok &= test_incremental(ctx, i, &tree, &frames));

            if !compare_ref {
                frames.clear();
            }

            (frames, vec![])
        }
        Err(errors) => (vec![], *errors),
    };

    // TODO: Also handle errors from other files.
    errors.retain(|error| error.span.source == id);
    for error in &mut errors {
        error.trace.clear();
    }

    // The comparison never fails since all spans are from the same source file.
    ref_errors.sort_by(|a, b| a.span.partial_cmp(&b.span).unwrap());
    errors.sort_by(|a, b| a.span.partial_cmp(&b.span).unwrap());

    if errors != ref_errors {
        println!("  Subtest {i} does not match expected errors. ❌");
        ok = false;

        let source = ctx.sources.get(id);
        for error in errors.iter() {
            if error.span.source == id && !ref_errors.contains(error) {
                print!("    Not annotated | ");
                print_error(&source, line, error);
            }
        }

        for error in ref_errors.iter() {
            if !errors.contains(error) {
                print!("    Not emitted   | ");
                print_error(&source, line, error);
            }
        }
    }

    (ok, compare_ref, frames)
}

#[cfg(feature = "layout-cache")]
fn test_incremental(
    ctx: &mut Context,
    i: usize,
    tree: &RootNode,
    frames: &[Rc<Frame>],
) -> bool {
    let mut ok = true;

    let reference = ctx.layouts.clone();
    for level in 0 .. reference.levels() {
        ctx.layouts = reference.clone();
        ctx.layouts.retain(|x| x == level);
        if ctx.layouts.is_empty() {
            continue;
        }

        ctx.layouts.turnaround();

        let cached = silenced(|| tree.layout(ctx));
        let total = reference.levels() - 1;
        let misses = ctx
            .layouts
            .entries()
            .filter(|e| e.level() == level && !e.hit() && e.age() == 2)
            .count();

        if misses > 0 {
            println!(
                "    Subtest {i} relayout had {misses} cache misses on level {level} of {total} ❌",
            );
            ok = false;
        }

        if cached != frames {
            println!(
                "    Subtest {i} relayout differs from clean pass on level {level} ❌",
            );
            ok = false;
        }
    }

    ctx.layouts = reference;
    ctx.layouts.turnaround();

    ok
}

/// Pseudorandomly edit the source file and test whether a reparse produces the
/// same result as a clean parse.
///
/// The method will first inject 10 strings once every 400 source characters
/// and then select 5 leaf node boundries to inject an additional, randomly
/// chosen string from the injection list.
fn test_reparse(src: &str, i: usize, rng: &mut LinearShift) -> bool {
    let supplements = [
        "[",
        ")",
        "#rect()",
        "a word",
        ", a: 1",
        "10.0",
        ":",
        "if i == 0 {true}",
        "for",
        "* hello *",
        "//",
        "/*",
        "\\u{12e4}",
        "```typst",
        " ",
        "trees",
        "\\",
        "$ a $",
        "2.",
        "-",
        "5",
    ];

    let mut ok = true;

    let apply = |replace: std::ops::Range<usize>, with| {
        let mut incr_source = SourceFile::detached(src);
        if incr_source.root().len() != src.len() {
            println!(
                "    Subtest {i} tree length {} does not match string length {} ❌",
                incr_source.root().len(),
                src.len(),
            );
            return false;
        }

        incr_source.edit(replace.clone(), with);
        let edited_src = incr_source.src();

        let ref_source = SourceFile::detached(edited_src);
        let incr_root = incr_source.root();
        let ref_root = ref_source.root();
        if incr_root != ref_root {
            println!(
                "    Subtest {i} reparse differs from clean parse when inserting '{with}' at {}-{} ❌\n",
                replace.start, replace.end,
            );
            println!("    Expected reference tree:\n{ref_root:#?}\n");
            println!("    Found incremental tree:\n{incr_root:#?}");
            println!("Full source ({}):\n\"{edited_src:?}\"", edited_src.len());
            false
        } else {
            true
        }
    };

    let mut pick = |range: Range<usize>| {
        let ratio = rng.next();
        (range.start as f64 + ratio * (range.end - range.start) as f64).floor() as usize
    };

    let insertions = (src.len() as f64 / 400.0).ceil() as usize;

    for _ in 0 .. insertions {
        let supplement = supplements[pick(0 .. supplements.len())];
        let start = pick(0 .. src.len());
        let end = pick(start .. src.len());

        if !src.is_char_boundary(start) || !src.is_char_boundary(end) {
            continue;
        }

        ok &= apply(start .. end, supplement);
    }

    let red = SourceFile::detached(src).red();

    let leafs = red.as_ref().leafs();

    let leaf_start = leafs[pick(0 .. leafs.len())].span().start;
    let supplement = supplements[pick(0 .. supplements.len())];

    ok &= apply(leaf_start .. leaf_start, supplement);

    ok
}

fn parse_metadata(source: &SourceFile) -> (Option<bool>, Vec<Error>) {
    let mut compare_ref = None;
    let mut errors = vec![];

    let lines: Vec<_> = source.src().lines().map(str::trim).collect();
    for (i, line) in lines.iter().enumerate() {
        if line.starts_with("// Ref: false") {
            compare_ref = Some(false);
        }

        if line.starts_with("// Ref: true") {
            compare_ref = Some(true);
        }

        let rest = if let Some(rest) = line.strip_prefix("// Error: ") {
            rest
        } else {
            continue;
        };

        fn num(s: &mut Scanner) -> usize {
            s.eat_while(|c| c.is_numeric()).parse().unwrap()
        }

        let comments =
            lines[i ..].iter().take_while(|line| line.starts_with("//")).count();

        let pos = |s: &mut Scanner| -> usize {
            let first = num(s) - 1;
            let (delta, column) =
                if s.eat_if(':') { (first, num(s) - 1) } else { (0, first) };
            let line = (i + comments) + delta;
            source.line_column_to_byte(line, column).unwrap()
        };

        let mut s = Scanner::new(rest);
        let start = pos(&mut s);
        let end = if s.eat_if('-') { pos(&mut s) } else { start };
        let span = Span::new(source.id(), start, end);

        errors.push(Error::new(span, s.rest().trim()));
    }

    (compare_ref, errors)
}

fn print_error(source: &SourceFile, line: usize, error: &Error) {
    let start_line = 1 + line + source.byte_to_line(error.span.start).unwrap();
    let start_col = 1 + source.byte_to_column(error.span.start).unwrap();
    let end_line = 1 + line + source.byte_to_line(error.span.end).unwrap();
    let end_col = 1 + source.byte_to_column(error.span.end).unwrap();
    println!(
        "Error: {start_line}:{start_col}-{end_line}:{end_col}: {}",
        error.message,
    );
}

fn draw(ctx: &Context, frames: &[Rc<Frame>], dpp: f32) -> sk::Pixmap {
    let pad = Length::pt(5.0);
    let width = 2.0 * pad + frames.iter().map(|l| l.size.x).max().unwrap_or_default();
    let height = pad + frames.iter().map(|l| l.size.y + pad).sum::<Length>();

    let pxw = (dpp * width.to_f32()) as u32;
    let pxh = (dpp * height.to_f32()) as u32;
    if pxw > 4000 || pxh > 4000 {
        panic!("overlarge image: {pxw} by {pxh} ({width:?} x {height:?})",);
    }

    let mut canvas = sk::Pixmap::new(pxw, pxh).unwrap();
    canvas.fill(sk::Color::BLACK);

    let mut mask = sk::ClipMask::new();
    let rect = sk::Rect::from_xywh(0.0, 0.0, pxw as f32, pxh as f32).unwrap();
    let path = sk::PathBuilder::from_rect(rect);
    mask.set_path(pxw, pxh, &path, sk::FillRule::default(), false);

    let mut ts =
        sk::Transform::from_scale(dpp, dpp).pre_translate(pad.to_f32(), pad.to_f32());

    for frame in frames {
        let mut background = sk::Paint::default();
        background.set_color(sk::Color::WHITE);

        let w = frame.size.x.to_f32();
        let h = frame.size.y.to_f32();
        let rect = sk::Rect::from_xywh(0.0, 0.0, w, h).unwrap();
        canvas.fill_rect(rect, &background, ts, None);

        draw_frame(&mut canvas, ts, &mask, ctx, frame, true);
        ts = ts.pre_translate(0.0, (frame.size.y + pad).to_f32());
    }

    canvas
}

fn draw_frame(
    canvas: &mut sk::Pixmap,
    ts: sk::Transform,
    mask: &sk::ClipMask,
    ctx: &Context,
    frame: &Frame,
    clip: bool,
) {
    let mut storage;
    let mut mask = mask;
    if clip {
        let w = frame.size.x.to_f32();
        let h = frame.size.y.to_f32();
        let rect = sk::Rect::from_xywh(0.0, 0.0, w, h).unwrap();
        let path = sk::PathBuilder::from_rect(rect).transform(ts).unwrap();
        let rule = sk::FillRule::default();
        storage = mask.clone();
        if storage.intersect_path(&path, rule, false).is_none() {
            // Fails if clipping rect is empty. In that case we just clip
            // everything by returning.
            return;
        }
        mask = &storage;
    }

    for (pos, element) in &frame.elements {
        let x = pos.x.to_f32();
        let y = pos.y.to_f32();
        let ts = ts.pre_translate(x, y);

        match *element {
            Element::Group(ref group) => {
                let ts = ts.pre_concat(convert_typst_transform(group.transform));
                draw_frame(canvas, ts, &mask, ctx, &group.frame, group.clips);
            }
            Element::Text(ref text) => {
                draw_text(canvas, ts, mask, ctx.fonts.get(text.face_id), text);
            }
            Element::Shape(ref shape) => {
                draw_shape(canvas, ts, mask, shape);
            }
            Element::Image(id, size) => {
                draw_image(canvas, ts, mask, ctx.images.get(id), size);
            }
            Element::Link(_, s) => {
                let fill = RgbaColor::new(40, 54, 99, 40).into();
                let shape = Shape::filled(Geometry::Rect(s), fill);
                draw_shape(canvas, ts, mask, &shape);
            }
        }
    }
}

fn draw_text(
    canvas: &mut sk::Pixmap,
    ts: sk::Transform,
    mask: &sk::ClipMask,
    face: &Face,
    text: &Text,
) {
    let ttf = face.ttf();
    let size = text.size.to_f32();
    let units_per_em = face.units_per_em as f32;
    let pixels_per_em = text.size.to_f32() * ts.sy;
    let scale = size / units_per_em;

    let mut x = 0.0;
    for glyph in &text.glyphs {
        let glyph_id = GlyphId(glyph.id);
        let offset = x + glyph.x_offset.resolve(text.size).to_f32();
        let ts = ts.pre_translate(offset, 0.0);

        if let Some(tree) = ttf
            .glyph_svg_image(glyph_id)
            .and_then(|data| std::str::from_utf8(data).ok())
            .map(|svg| {
                let viewbox = format!("viewBox=\"0 0 {0} {0}\" xmlns", units_per_em);
                svg.replace("xmlns", &viewbox)
            })
            .and_then(|s| {
                usvg::Tree::from_str(&s, &usvg::Options::default().to_ref()).ok()
            })
        {
            for child in tree.root().children() {
                if let usvg::NodeKind::Path(node) = &*child.borrow() {
                    // SVG is already Y-down, no flipping required.
                    let ts = convert_usvg_transform(node.transform)
                        .post_scale(scale, scale)
                        .post_concat(ts);

                    if let Some(fill) = &node.fill {
                        let path = convert_usvg_path(&node.data);
                        let (paint, fill_rule) = convert_usvg_fill(fill);
                        canvas.fill_path(&path, &paint, fill_rule, ts, Some(mask));
                    }
                }
            }
        } else if let Some(raster) =
            ttf.glyph_raster_image(glyph_id, pixels_per_em as u16)
        {
            // TODO: Vertical alignment isn't quite right for Apple Color Emoji,
            // and maybe also for Noto Color Emoji. And: Is the size calculation
            // correct?
            let img = RasterImage::parse(&raster.data).unwrap();
            let h = text.size;
            let w = (img.width() as f64 / img.height() as f64) * h;
            let dx = (raster.x as f32) / (img.width() as f32) * size;
            let dy = (raster.y as f32) / (img.height() as f32) * size;
            let ts = ts.pre_translate(dx, -size - dy);
            draw_image(canvas, ts, mask, &Image::Raster(img), Size::new(w, h));
        } else {
            // Otherwise, draw normal outline.
            let mut builder = WrappedPathBuilder(sk::PathBuilder::new());
            if ttf.outline_glyph(glyph_id, &mut builder).is_some() {
                // Flip vertically because font design coordinate system is Y-up.
                let ts = ts.pre_scale(scale, -scale);
                let path = builder.0.finish().unwrap();
                let paint = convert_typst_paint(text.fill);
                canvas.fill_path(&path, &paint, sk::FillRule::default(), ts, Some(mask));
            }
        }

        x += glyph.x_advance.resolve(text.size).to_f32();
    }
}

fn draw_shape(
    canvas: &mut sk::Pixmap,
    ts: sk::Transform,
    mask: &sk::ClipMask,
    shape: &Shape,
) {
    let path = match shape.geometry {
        Geometry::Rect(size) => {
            let w = size.x.to_f32();
            let h = size.y.to_f32();
            let rect = sk::Rect::from_xywh(0.0, 0.0, w, h).unwrap();
            sk::PathBuilder::from_rect(rect)
        }
        Geometry::Ellipse(size) => {
            let approx = geom::Path::ellipse(size);
            convert_typst_path(&approx)
        }
        Geometry::Line(target) => {
            let mut builder = sk::PathBuilder::new();
            builder.line_to(target.x.to_f32(), target.y.to_f32());
            builder.finish().unwrap()
        }
        Geometry::Path(ref path) => convert_typst_path(path),
    };

    if let Some(fill) = shape.fill {
        let mut paint = convert_typst_paint(fill);
        if matches!(shape.geometry, Geometry::Rect(_)) {
            paint.anti_alias = false;
        }

        let rule = sk::FillRule::default();
        canvas.fill_path(&path, &paint, rule, ts, Some(mask));
    }

    if let Some(Stroke { paint, thickness }) = shape.stroke {
        let paint = convert_typst_paint(paint);
        let mut stroke = sk::Stroke::default();
        stroke.width = thickness.to_f32();
        canvas.stroke_path(&path, &paint, &stroke, ts, Some(mask));
    }
}

fn draw_image(
    canvas: &mut sk::Pixmap,
    ts: sk::Transform,
    mask: &sk::ClipMask,
    img: &Image,
    size: Size,
) {
    let view_width = size.x.to_f32();
    let view_height = size.y.to_f32();

    let pixmap = match img {
        Image::Raster(img) => {
            let w = img.buf.width();
            let h = img.buf.height();
            let mut pixmap = sk::Pixmap::new(w, h).unwrap();
            for ((_, _, src), dest) in img.buf.pixels().zip(pixmap.pixels_mut()) {
                let Rgba([r, g, b, a]) = src;
                *dest = sk::ColorU8::from_rgba(r, g, b, a).premultiply();
            }
            pixmap
        }
        Image::Svg(Svg(tree)) => {
            let size = tree.svg_node().size;
            let aspect = (size.width() / size.height()) as f32;
            let scale = ts.sx.max(ts.sy);
            let w = (scale * view_width.max(aspect * view_height)).ceil() as u32;
            let h = ((w as f32) / aspect).ceil() as u32;
            let mut pixmap = sk::Pixmap::new(w, h).unwrap();
            resvg::render(
                &tree,
                FitTo::Size(w, h),
                sk::Transform::identity(),
                pixmap.as_mut(),
            );
            pixmap
        }
    };

    let scale_x = view_width / pixmap.width() as f32;
    let scale_y = view_height / pixmap.height() as f32;

    let mut paint = sk::Paint::default();
    paint.shader = sk::Pattern::new(
        pixmap.as_ref(),
        sk::SpreadMode::Pad,
        sk::FilterQuality::Bilinear,
        1.0,
        sk::Transform::from_scale(scale_x, scale_y),
    );

    let rect = sk::Rect::from_xywh(0.0, 0.0, view_width, view_height).unwrap();
    canvas.fill_rect(rect, &paint, ts, Some(mask));
}

fn convert_typst_transform(transform: Transform) -> sk::Transform {
    let Transform { sx, ky, kx, sy, tx, ty } = transform;
    sk::Transform::from_row(
        sx.get() as _,
        ky.get() as _,
        kx.get() as _,
        sy.get() as _,
        tx.to_f32(),
        ty.to_f32(),
    )
}

fn convert_typst_paint(paint: Paint) -> sk::Paint<'static> {
    let Paint::Solid(Color::Rgba(c)) = paint;
    let mut paint = sk::Paint::default();
    paint.set_color_rgba8(c.r, c.g, c.b, c.a);
    paint.anti_alias = true;
    paint
}

fn convert_typst_path(path: &geom::Path) -> sk::Path {
    let mut builder = sk::PathBuilder::new();
    for elem in &path.0 {
        match elem {
            PathElement::MoveTo(p) => {
                builder.move_to(p.x.to_f32(), p.y.to_f32());
            }
            PathElement::LineTo(p) => {
                builder.line_to(p.x.to_f32(), p.y.to_f32());
            }
            PathElement::CubicTo(p1, p2, p3) => {
                builder.cubic_to(
                    p1.x.to_f32(),
                    p1.y.to_f32(),
                    p2.x.to_f32(),
                    p2.y.to_f32(),
                    p3.x.to_f32(),
                    p3.y.to_f32(),
                );
            }
            PathElement::ClosePath => {
                builder.close();
            }
        };
    }
    builder.finish().unwrap()
}

fn convert_usvg_transform(transform: usvg::Transform) -> sk::Transform {
    let usvg::Transform { a, b, c, d, e, f } = transform;
    sk::Transform::from_row(a as _, b as _, c as _, d as _, e as _, f as _)
}

fn convert_usvg_fill(fill: &usvg::Fill) -> (sk::Paint<'static>, sk::FillRule) {
    let mut paint = sk::Paint::default();
    paint.anti_alias = true;

    if let usvg::Paint::Color(usvg::Color { red, green, blue }) = fill.paint {
        paint.set_color_rgba8(red, green, blue, fill.opacity.to_u8())
    }

    let rule = match fill.rule {
        usvg::FillRule::NonZero => sk::FillRule::Winding,
        usvg::FillRule::EvenOdd => sk::FillRule::EvenOdd,
    };

    (paint, rule)
}

fn convert_usvg_path(path: &usvg::PathData) -> sk::Path {
    let mut builder = sk::PathBuilder::new();
    for seg in path.iter() {
        match *seg {
            usvg::PathSegment::MoveTo { x, y } => {
                builder.move_to(x as _, y as _);
            }
            usvg::PathSegment::LineTo { x, y } => {
                builder.line_to(x as _, y as _);
            }
            usvg::PathSegment::CurveTo { x1, y1, x2, y2, x, y } => {
                builder.cubic_to(x1 as _, y1 as _, x2 as _, y2 as _, x as _, y as _);
            }
            usvg::PathSegment::ClosePath => {
                builder.close();
            }
        }
    }
    builder.finish().unwrap()
}

struct WrappedPathBuilder(sk::PathBuilder);

impl OutlineBuilder for WrappedPathBuilder {
    fn move_to(&mut self, x: f32, y: f32) {
        self.0.move_to(x, y);
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.0.line_to(x, y);
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.0.quad_to(x1, y1, x, y);
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.0.cubic_to(x1, y1, x2, y2, x, y);
    }

    fn close(&mut self) {
        self.0.close();
    }
}

/// Additional methods for [`Length`].
trait LengthExt {
    /// Convert an em length to a number of points.
    fn to_f32(self) -> f32;
}

impl LengthExt for Length {
    fn to_f32(self) -> f32 {
        self.to_pt() as f32
    }
}

/// Disable stdout and stderr during execution of `f`.
#[cfg(feature = "layout-cache")]
fn silenced<F, T>(f: F) -> T
where
    F: FnOnce() -> T,
{
    let path = if cfg!(windows) { "NUL" } else { "/dev/null" };
    let null = File::create(path).unwrap();
    let stderr = FileDescriptor::redirect_stdio(&null, Stderr).unwrap();
    let stdout = FileDescriptor::redirect_stdio(&null, Stdout).unwrap();
    let result = f();
    FileDescriptor::redirect_stdio(&stderr, Stderr).unwrap();
    FileDescriptor::redirect_stdio(&stdout, Stdout).unwrap();
    result
}

/// This is an Linear-feedback shift register using XOR as its shifting
/// function. It can be used as PRNG.
struct LinearShift(u64);

impl LinearShift {
    /// Initialize the shift register with a pre-set seed.
    pub fn new() -> Self {
        Self(0xACE5)
    }

    /// Return a pseudo-random number between `0.0` and `1.0`.
    pub fn next(&mut self) -> f64 {
        self.0 ^= self.0 >> 3;
        self.0 ^= self.0 << 14;
        self.0 ^= self.0 >> 28;
        self.0 ^= self.0 << 36;
        self.0 ^= self.0 >> 52;
        self.0 as f64 / u64::MAX as f64
    }
}