ranim-items 0.2.1

The built-in items of Ranim.
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
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
use std::{
    collections::HashMap,
    io::Write,
    num::NonZeroUsize,
    sync::{Arc, Mutex, OnceLock},
};

use chrono::{DateTime, Datelike, Local};
use diff_match_patch_rs::{Efficient, Ops};
use lru::LruCache;
use regex::bytes::Regex;
use sha1::{Digest, Sha1};
use typst::{
    Library, LibraryExt, World,
    diag::{FileError, FileResult},
    foundations::{Bytes, Datetime},
    layout::Abs,
    syntax::{FileId, Source},
    text::{Font, FontBook},
    utils::LazyHash,
};
use typst_kit::fonts::{FontSearcher, Fonts};

use crate::vitem::{VItem, svg::SvgItem};
use ranim_core::Extract;
use ranim_core::traits::Interpolatable;
use ranim_core::{
    anchor::Aabb,
    color,
    components::width::Width,
    core_item::CoreItem,
    glam,
    traits::{
        Alignable, FillColor, Opacity, RotateTransform, ScaleTransform, ShiftTransform,
        StrokeColor, StrokeWidth, With,
    },
};

struct TypstLruCache {
    inner: LruCache<[u8; 20], String>,
}

impl TypstLruCache {
    fn new(cap: NonZeroUsize) -> Self {
        Self {
            inner: LruCache::new(cap),
        }
    }
    // fn get(&mut self, typst_str: &str) -> Option<&String> {
    //     let mut sha1 = Sha1::new();
    //     sha1.update(typst_str.as_bytes());
    //     let sha1 = sha1.finalize();
    //     self.inner.get::<[u8; 20]>(sha1.as_ref())
    // }
    fn get_or_insert(&mut self, typst_str: &str) -> &String {
        let mut sha1 = Sha1::new();
        sha1.update(typst_str.as_bytes());
        let sha1 = sha1.finalize();
        self.inner
            .get_or_insert_ref(AsRef::<[u8; 20]>::as_ref(&sha1), || {
                // let world = SingleFileTypstWorld::new(typst_str);
                let world = typst_world().lock().unwrap();
                let world = world.with_source_str(typst_str);
                // world.set_source(typst_str);
                let document = typst::compile(&world)
                    .output
                    .expect("failed to compile typst source");

                let svg = typst_svg::svg_merged(&document, Abs::pt(2.0));
                get_typst_element(&svg)
            })
    }
}

fn typst_lru() -> &'static Arc<Mutex<TypstLruCache>> {
    static LRU: OnceLock<Arc<Mutex<TypstLruCache>>> = OnceLock::new();
    LRU.get_or_init(|| {
        Arc::new(Mutex::new(TypstLruCache::new(
            NonZeroUsize::new(256).unwrap(),
        )))
    })
}

fn fonts() -> &'static Fonts {
    static FONTS: OnceLock<Fonts> = OnceLock::new();
    FONTS.get_or_init(|| FontSearcher::new().include_system_fonts(true).search())
}

fn typst_world() -> &'static Arc<Mutex<TypstWorld>> {
    static WORLD: OnceLock<Arc<Mutex<TypstWorld>>> = OnceLock::new();
    WORLD.get_or_init(|| Arc::new(Mutex::new(TypstWorld::new())))
}

/// Compiles typst string to SVG string
pub fn typst_svg(source: &str) -> String {
    typst_lru().lock().unwrap().get_or_insert(source).clone()
    // let world = SingleFileTypstWorld::new(source);
    // let document = typst::compile(&world)
    //     .output
    //     .expect("failed to compile typst source");

    // let svg = typst_svg::svg_merged(&document, Abs::pt(2.0));
    // get_typst_element(&svg)
}

struct FileEntry {
    bytes: Bytes,
    /// This field is filled on demand.
    source: Option<Source>,
}

impl FileEntry {
    fn source(&mut self, id: FileId) -> FileResult<Source> {
        // Fallible `get_or_insert`.
        let source = if let Some(source) = &self.source {
            source
        } else {
            let contents = std::str::from_utf8(&self.bytes).map_err(|_| FileError::InvalidUtf8)?;
            // Defuse the BOM!
            let contents = contents.trim_start_matches('\u{feff}');
            let source = Source::new(id, contents.into());
            self.source.insert(source)
        };
        Ok(source.clone())
    }
}

pub(crate) struct TypstWorld {
    library: LazyHash<Library>,
    book: LazyHash<FontBook>,
    files: Mutex<HashMap<FileId, FileEntry>>,
}

impl TypstWorld {
    pub(crate) fn new() -> Self {
        let fonts = fonts();
        Self {
            library: LazyHash::new(Library::default()),
            book: LazyHash::new(fonts.book.clone()),
            files: Mutex::new(HashMap::new()),
        }
    }
    pub(crate) fn with_source_str(&self, source: &str) -> TypstWorldWithSource<'_> {
        self.with_source(Source::detached(source))
    }
    pub(crate) fn with_source(&self, source: Source) -> TypstWorldWithSource<'_> {
        TypstWorldWithSource {
            world: self,
            source,
            now: OnceLock::new(),
        }
    }

    // from https://github.com/mattfbacon/typst-bot
    // TODO: package things
    // Weird pattern because mapping a MutexGuard is not stable yet.
    fn file<T>(&self, id: FileId, map: impl FnOnce(&mut FileEntry) -> T) -> FileResult<T> {
        let mut files = self.files.lock().unwrap();
        if let Some(entry) = files.get_mut(&id) {
            return Ok(map(entry));
        }
        // `files` must stay locked here so we don't download the same package multiple times.
        // TODO proper multithreading, maybe with typst-kit.

        // 'x: {
        // 	if let Some(package) = id.package() {
        // 		let package_dir = self.ensure_package(package)?;
        // 		let Some(path) = id.vpath().resolve(&package_dir) else {
        // 			break 'x;
        // 		};
        // 		let contents = std::fs::read(&path).map_err(|error| FileError::from_io(error, &path))?;
        // 		let entry = files.entry(id).or_insert(FileEntry {
        // 			bytes: Bytes::new(contents),
        // 			source: None,
        // 		});
        // 		return Ok(map(entry));
        // 	}
        // }

        Err(FileError::NotFound(id.vpath().as_rootless_path().into()))
    }
}

pub(crate) struct TypstWorldWithSource<'a> {
    world: &'a TypstWorld,
    source: Source,
    now: OnceLock<DateTime<Local>>,
}

impl World for TypstWorldWithSource<'_> {
    fn library(&self) -> &LazyHash<Library> {
        &self.world.library
    }

    fn book(&self) -> &LazyHash<FontBook> {
        &self.world.book
    }

    fn main(&self) -> FileId {
        self.source.id()
    }

    fn source(&self, id: FileId) -> FileResult<Source> {
        if id == self.source.id() {
            Ok(self.source.clone())
        } else {
            self.world.file(id, |entry| entry.source(id))?
        }
    }

    fn file(&self, id: FileId) -> FileResult<Bytes> {
        self.world.file(id, |file| file.bytes.clone())
    }

    fn font(&self, index: usize) -> Option<Font> {
        fonts().fonts[index].get()
    }

    fn today(&self, offset: Option<i64>) -> Option<Datetime> {
        let now = self.now.get_or_init(chrono::Local::now);

        let naive = match offset {
            None => now.naive_local(),
            Some(o) => now.naive_utc() + chrono::Duration::hours(o),
        };

        Datetime::from_ymd(
            naive.year(),
            naive.month().try_into().ok()?,
            naive.day().try_into().ok()?,
        )
    }
}

/// A Text item construted through typst
///
/// Note that the methods this item provides assumes that the typst string
/// you provide only produces text output, otherwise undefined behaviours may happens.
#[derive(Clone)]
pub struct TypstText {
    chars: String,
    vitems: Vec<VItem>,
}

impl TypstText {
    fn _new(str: &str) -> Self {
        let svg = SvgItem::new(typst_svg(str));
        let chars = str.to_string();

        let vitems = Vec::<VItem>::from(svg);
        assert_eq!(chars.len(), vitems.len());
        Self { chars, vitems }
    }
    /// Create a TypstText with typst string.
    ///
    /// The typst string you provide should only produces text output,
    /// otherwise undefined behaviours may happens.
    pub fn new(typst_str: &str) -> Self {
        let svg = SvgItem::new(typst_svg(typst_str));
        let chars = typst_str
            .replace(" ", "")
            .replace("\n", "")
            .replace("\r", "")
            .replace("\t", "");

        let vitems = Vec::<VItem>::from(svg);
        assert_eq!(chars.len(), vitems.len());
        Self { chars, vitems }
    }

    /// Inline code
    pub fn new_inline_code(code: &str) -> Self {
        let svg = SvgItem::new(typst_svg(format!("`{code}`").as_str()));
        let chars = code
            .replace(" ", "")
            .replace("\n", "")
            .replace("\r", "")
            .replace("\t", "");

        let vitems = Vec::<VItem>::from(svg);
        assert_eq!(chars.len(), vitems.len());
        Self { chars, vitems }
    }

    /// Multiline code
    pub fn new_multiline_code(code: &str, language: Option<&str>) -> Self {
        let language = language.unwrap_or("");
        // Self::new(format!("```{language}\n{code}\n```").as_str())
        let svg = SvgItem::new(typst_svg(format!("```{language}\n{code}```").as_str()));
        let chars = code
            .replace(" ", "")
            .replace("\n", "")
            .replace("\r", "")
            .replace("\t", "");

        let vitems = Vec::<VItem>::from(svg);
        assert_eq!(chars.len(), vitems.len());
        Self { chars, vitems }
    }
}

impl Alignable for TypstText {
    fn is_aligned(&self, other: &Self) -> bool {
        self.vitems.len() == other.vitems.len()
            && self
                .vitems
                .iter()
                .zip(&other.vitems)
                .all(|(a, b)| a.is_aligned(b))
    }
    fn align_with(&mut self, other: &mut Self) {
        let dmp = diff_match_patch_rs::DiffMatchPatch::new();
        let diffs = dmp
            .diff_main::<Efficient>(&self.chars, &other.chars)
            .unwrap();

        let len = self.vitems.len().max(other.vitems.len());
        let mut vitems_self: Vec<VItem> = Vec::with_capacity(len);
        let mut vitems_other: Vec<VItem> = Vec::with_capacity(len);
        let mut ia = 0;
        let mut ib = 0;
        let mut last_neq_idx_a = 0;
        let mut last_neq_idx_b = 0;
        let align_and_push_diff = |vitems_self: &mut Vec<VItem>,
                                   vitems_other: &mut Vec<VItem>,
                                   ia,
                                   ib,
                                   last_neq_idx_a,
                                   last_neq_idx_b| {
            if last_neq_idx_a != ia || last_neq_idx_b != ib {
                let mut vitems_a = self.vitems[last_neq_idx_a..ia].to_vec();
                let mut vitems_b = other.vitems[last_neq_idx_b..ib].to_vec();
                if vitems_a.is_empty() {
                    vitems_a.extend(vitems_b.iter().map(|x| {
                        x.clone().with(|x| {
                            x.shrink();
                        })
                    }));
                }
                if vitems_b.is_empty() {
                    vitems_b.extend(vitems_a.iter().map(|x| {
                        x.clone().with(|x| {
                            x.shrink();
                        })
                    }));
                }
                if last_neq_idx_a != ia && last_neq_idx_b != ib {
                    vitems_a.align_with(&mut vitems_b);
                }
                vitems_self.extend(vitems_a);
                vitems_other.extend(vitems_b);
            }
        };

        for diff in &diffs {
            // println!("[{ia}] {last_neq_idx_a} [{ib}] {last_neq_idx_b}");
            // println!("{diff:?}");
            match diff.op() {
                Ops::Equal => {
                    align_and_push_diff(
                        &mut vitems_self,
                        &mut vitems_other,
                        ia,
                        ib,
                        last_neq_idx_a,
                        last_neq_idx_b,
                    );
                    let l = diff.size();
                    vitems_self.extend(self.vitems[ia..ia + l].iter().cloned());
                    vitems_other.extend(other.vitems[ib..ib + l].iter().cloned());
                    ia += l;
                    ib += l;
                    last_neq_idx_a = ia;
                    last_neq_idx_b = ib;
                }
                Ops::Delete => {
                    ia += diff.size();
                }
                Ops::Insert => {
                    ib += diff.size();
                }
            }
        }
        align_and_push_diff(
            &mut vitems_self,
            &mut vitems_other,
            ia,
            ib,
            last_neq_idx_a,
            last_neq_idx_b,
        );

        assert_eq!(vitems_self.len(), vitems_other.len());
        vitems_self
            .iter_mut()
            .zip(vitems_other.iter_mut())
            .for_each(|(a, b)| {
                // println!("{i} {}", a.is_aligned(b));
                // println!("{} {}", a.vpoints.len(), b.vpoints.len());
                if !a.is_aligned(b) {
                    a.align_with(b);
                }
            });

        self.vitems = vitems_self;
        other.vitems = vitems_other;
    }
}

impl Interpolatable for TypstText {
    fn lerp(&self, target: &Self, t: f64) -> Self {
        let vitems = self
            .vitems
            .iter()
            .zip(&target.vitems)
            .map(|(a, b)| a.lerp(b, t))
            .collect::<Vec<_>>();
        Self {
            chars: self.chars.clone(),
            vitems,
        }
    }
}

impl From<TypstText> for Vec<VItem> {
    fn from(value: TypstText) -> Self {
        value.vitems
    }
}

impl Extract for TypstText {
    type Target = CoreItem;
    fn extract_into(&self, buf: &mut Vec<Self::Target>) {
        self.vitems.extract_into(buf);
    }
}

impl Aabb for TypstText {
    fn aabb(&self) -> [glam::DVec3; 2] {
        self.vitems.aabb()
    }
}

impl ShiftTransform for TypstText {
    fn shift(&mut self, shift: glam::DVec3) -> &mut Self {
        self.vitems.shift(shift);
        self
    }
}

impl RotateTransform for TypstText {
    fn rotate_on_axis(&mut self, axis: glam::DVec3, angle: f64) -> &mut Self {
        self.vitems.rotate_on_axis(axis, angle);
        self
    }
}

impl ScaleTransform for TypstText {
    fn scale(&mut self, scale: glam::DVec3) -> &mut Self {
        self.vitems.scale(scale);
        self
    }
}

impl FillColor for TypstText {
    fn fill_color(&self) -> color::AlphaColor<color::Srgb> {
        self.vitems[0].fill_color()
    }
    fn set_fill_color(&mut self, color: color::AlphaColor<color::Srgb>) -> &mut Self {
        self.vitems.set_fill_color(color);
        self
    }
    fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
        self.vitems.set_fill_opacity(opacity);
        self
    }
}

impl StrokeColor for TypstText {
    fn stroke_color(&self) -> color::AlphaColor<color::Srgb> {
        self.vitems[0].fill_color()
    }
    fn set_stroke_color(&mut self, color: color::AlphaColor<color::Srgb>) -> &mut Self {
        self.vitems.set_stroke_color(color);
        self
    }
    fn set_stroke_opacity(&mut self, opacity: f32) -> &mut Self {
        self.vitems.set_stroke_opacity(opacity);
        self
    }
}

impl Opacity for TypstText {
    fn set_opacity(&mut self, opacity: f32) -> &mut Self {
        self.vitems.set_fill_opacity(opacity);
        self.vitems.set_stroke_opacity(opacity);
        self
    }
}

impl StrokeWidth for TypstText {
    fn stroke_width(&self) -> f32 {
        self.vitems.stroke_width()
    }
    fn apply_stroke_func(&mut self, f: impl for<'a> Fn(&'a mut [Width])) -> &mut Self {
        self.vitems.iter_mut().for_each(|vitem| {
            vitem.apply_stroke_func(&f);
        });
        self
    }
    fn set_stroke_width(&mut self, width: f32) -> &mut Self {
        self.vitems.set_stroke_width(width);
        self
    }
}

/// remove `r"<path[^>]*(?:>.*?<\/path>|\/>)"`
pub fn get_typst_element(svg: &str) -> String {
    let re = Regex::new(r"<path[^>]*(?:>.*?<\/path>|\/>)").unwrap();
    let removed_bg = re.replace(svg.as_bytes(), b"");
    let re = Regex::new(r#"\s+(?:viewBox|width|height)="[^"]*""#).unwrap();
    let removed_size = re.replace_all(&removed_bg, b"");

    // println!("{}", String::from_utf8_lossy(&output));
    // println!("{}", String::from_utf8_lossy(&removed_bg));
    String::from_utf8_lossy(&removed_size).to_string()
}

/// Compiles typst code to SVG string by spawning a typst process
pub fn compile_typst_code(typst_code: &str) -> String {
    let mut child = std::process::Command::new("typst")
        .arg("compile")
        .arg("-")
        .arg("-")
        .arg("-fsvg")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .expect("failed to spawn typst");

    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(typst_code.as_bytes())
            .expect("failed to write to typst's stdin");
    }

    let output = child.wait_with_output().unwrap().stdout;
    let output = String::from_utf8_lossy(&output);

    output.to_string()
}

#[cfg(test)]
mod tests {
    use std::time::Instant;

    use super::*;

    /*
    fonts search: 322.844709ms
    world construct: 1.901541ms
    set source: 958ns
    file: 736
    file: 818
    document compile: 89.835583ms
    svg output: 185.458µs
    get element: 730.792µs
     */
    #[test]
    fn test_single_file_typst_world_foo() {
        let start = Instant::now();
        fonts();
        println!("fonts search: {:?}", start.elapsed());

        let start = Instant::now();
        let world = TypstWorld::new();
        println!("world construct: {:?}", start.elapsed());

        let start = Instant::now();
        let world = world.with_source_str("r");
        println!("set source: {:?}", start.elapsed());

        let start = Instant::now();
        let document = typst::compile(&world)
            .output
            .expect("failed to compile typst source");
        println!("document compile: {:?}", start.elapsed());

        let start = Instant::now();
        let svg = typst_svg::svg_merged(&document, Abs::pt(2.0));
        println!("{svg}");
        println!("svg output: {:?}", start.elapsed());

        let start = Instant::now();
        let res = get_typst_element(&svg);
        println!("get element: {:?}", start.elapsed());

        println!("{res}");
        // println!("{}", typst_svg!(source))
    }

    ///
    /// ```
    /// <svg class="typst-doc" viewBox="0 0 11.483999999999998 11" width="11.483999999999998pt" height="11pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    ///    <path class="typst-shape" fill="#ffffff" fill-rule="nonzero" d="M 0 0v 11 h 11.484 v -11 Z "/>
    ///    <g>
    ///        <g class="typst-text" transform="matrix(1 0 0 -1 0 11)">
    ///            <use xlink:href="#gB5279FC30F2C6542A76CE0CDC73F9462" x="0" y="0" fill="#000000" fill-rule="nonzero"/>
    ///            <use xlink:href="#gC5A0A6F735BE491513D9F5FD3BD367ED" x="6.457" y="0" fill="#000000" fill-rule="nonzero"/>
    ///        </g>
    ///    </g>
    /// ```
    /// ```
    /// <svg class="typst-doc" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    /// <g>
    ///     <g class="typst-text" transform="matrix(1 0 0 -1 0 11)">
    ///         <use xlink:href="#gB5279FC30F2C6542A76CE0CDC73F9462" x="0" y="0" fill="#000000" fill-rule="nonzero"/>
    ///         <use xlink:href="#gC5A0A6F735BE491513D9F5FD3BD367ED" x="6.457" y="0" fill="#000000" fill-rule="nonzero"/>
    ///     </g>
    /// </g>
    /// ```
    #[test]
    fn foo_page() {
        let text = r#"Ra"#;
        let res = compile_typst_code(text);
        println!("{res}");

        let res = typst_svg(text);
        println!("{res}");
    }

    #[test]
    fn foo() {
        let code_a = r#"#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;
}
"#;
        let mut code_a = TypstText::new_multiline_code(code_a, Some("cpp"));
        let code_b = r#"fn main() {
    println!("Hello World!");
}"#;
        let mut code_b = TypstText::new_multiline_code(code_b, Some("rust"));

        code_a.align_with(&mut code_b);
    }
}