rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
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
use skia_safe::{paint, Canvas, Color, FontMgr, Paint, PathBuilder};

use super::font_catalog::TypefaceCatalog;
use super::font_lookup::SystemFontFamilies;
mod glyphs;
use glyphs::{draw_hancom_glyphs, draw_stretch_bracket, draw_text, draw_text_with_family};

use crate::renderer::equation::ast::MatrixStyle;
use crate::renderer::equation::layout::{
    is_integral_symbol, LayoutBox, LayoutKind, BIG_OP_SCALE, INTEGRAL_SCALE, SCRIPT_SCALE,
};
use crate::renderer::equation::symbols::{DecoKind, FontStyleKind};

const EQ_FONT_FAMILY: &str =
    "Latin Modern Math, STIX Two Math, Cambria Math, DejaVu Math TeX Gyre, DejaVu Sans, Times New Roman, serif";

/// Font resources supplied to body text and every recursive equation branch.
pub(super) struct EquationFonts<'a> {
    pub(super) font_mgr: &'a FontMgr,
    pub(super) system_families: &'a SystemFontFamilies,
    pub(super) custom_typefaces: &'a TypefaceCatalog,
    pub(super) bundled_typefaces: &'a TypefaceCatalog,
}

pub(super) fn render_equation(
    canvas: &Canvas,
    fonts: &EquationFonts<'_>,
    layout: &LayoutBox,
    origin_x: f64,
    origin_y: f64,
    color: u32,
    base_font_size: f64,
) {
    render_box(
        canvas,
        fonts,
        layout,
        origin_x,
        origin_y,
        colorref_to_skia(color, 1.0),
        base_font_size,
        true,
        false,
    );
}

fn render_box(
    canvas: &Canvas,
    fonts: &EquationFonts<'_>,
    lb: &LayoutBox,
    parent_x: f64,
    parent_y: f64,
    color: Color,
    fs: f64,
    italic: bool,
    bold: bool,
) {
    let fs = if lb.font_size > 0.0 { lb.font_size } else { fs };
    let x = parent_x + lb.x;
    let y = parent_y + lb.y;

    match &lb.kind {
        LayoutKind::HancomGlyphs(run) => {
            draw_hancom_glyphs(
                canvas,
                fonts,
                run,
                x,
                y + lb.baseline + run.baseline_shift,
                fs,
                color,
            );
        }
        LayoutKind::Row(children) => {
            for child in children {
                render_box(canvas, fonts, child, x, y, color, fs, italic, bold);
            }
        }
        LayoutKind::Text(text) => {
            if crate::renderer::equation::text_has_cjk(text)
                && (lb.width
                    - crate::renderer::equation::layout::estimate_text_width(
                        text,
                        font_size_from_box(lb, fs),
                        false,
                    ))
                .abs()
                    > 0.01
            {
                let font_size = font_size_from_box(lb, fs);
                for (ch, offset) in
                    crate::renderer::equation::positioned_cjk_text(text, font_size, lb.width)
                {
                    draw_text(
                        canvas,
                        fonts,
                        &ch.to_string(),
                        x + offset,
                        y + lb.baseline,
                        font_size,
                        false,
                        bold,
                        color,
                        false,
                    );
                }
                return;
            }
            draw_text(
                canvas,
                fonts,
                text,
                x,
                y + lb.baseline,
                font_size_from_box(lb, fs),
                italic && !crate::renderer::equation::layout::is_styled_variable(text),
                bold && !crate::renderer::equation::layout::is_styled_variable(text),
                color,
                false,
            );
        }
        LayoutKind::Number(text) => {
            draw_text(
                canvas,
                fonts,
                text,
                x,
                y + lb.baseline,
                font_size_from_box(lb, fs),
                false,
                bold,
                color,
                false,
            );
        }
        LayoutKind::Symbol(text) => {
            draw_text(
                canvas,
                fonts,
                text,
                x + lb.width / 2.0,
                y + lb.baseline,
                font_size_from_box(lb, fs),
                false,
                false,
                color,
                true,
            );
        }
        LayoutKind::FontGlyph {
            text,
            font_family,
            outline,
            paint,
        } => {
            if let Some(commands) = outline {
                super::equation_glyph::draw_outline(canvas, commands, x, y, color, *paint);
                return;
            }
            let scale_x = paint.map_or(1.0, |p| p.scale_x);
            let (glyph_x, glyph_baseline) = if scale_x != 1.0 {
                canvas.save();
                canvas.translate((x as f32, (y + lb.baseline) as f32));
                canvas.scale((scale_x as f32, 1.0));
                (0.0, 0.0)
            } else {
                (x, y + lb.baseline)
            };
            draw_text_with_family(
                canvas,
                fonts,
                text,
                glyph_x,
                glyph_baseline,
                fs,
                false,
                false,
                color,
                false,
                Some(font_family),
            );
            if scale_x != 1.0 {
                canvas.restore();
            }
        }
        LayoutKind::MathSymbol(text) => {
            // Task #1317: 적분 기호(∫)는 폰트 text 가 아닌 stroke path 로 렌더(geom SSOT,
            // svg/canvas 와 동일). 그 외 MathSymbol 은 text 렌더.
            if is_integral_symbol(text) {
                draw_integral(canvas, x, y, fs, color);
            } else {
                draw_text(
                    canvas,
                    fonts,
                    text,
                    x,
                    y + lb.baseline,
                    font_size_from_box(lb, fs),
                    false,
                    false,
                    color,
                    false,
                );
            }
        }
        LayoutKind::Function(name) => {
            draw_text(
                canvas,
                fonts,
                name,
                x,
                y + lb.baseline,
                font_size_from_box(lb, fs),
                false,
                false,
                color,
                false,
            );
        }
        LayoutKind::Fraction {
            numer,
            denom,
            axis_height,
            rule_thickness,
            rule_start,
            rule_end,
        } => {
            render_box(canvas, fonts, numer, x, y, color, fs, italic, bold);
            let line_y = y + lb.baseline - axis_height;
            canvas.draw_line(
                ((x + rule_start) as f32, line_y as f32),
                ((x + rule_end) as f32, line_y as f32),
                &stroke_paint(color, *rule_thickness),
            );
            render_box(canvas, fonts, denom, x, y, color, fs, italic, bold);
        }
        LayoutKind::Atop { top, bottom } => {
            render_box(canvas, fonts, top, x, y, color, fs, italic, bold);
            render_box(canvas, fonts, bottom, x, y, color, fs, italic, bold);
        }
        LayoutKind::Sqrt { index, body } => {
            let geometry = crate::renderer::equation::radical_outline::geometry(lb, body, fs);
            let mut path = PathBuilder::new();
            for command in &geometry.outline {
                use crate::renderer::equation::GlyphOutlineCommand as Command;
                match *command {
                    Command::Move((a, b)) => {
                        path.move_to(((x + a) as f32, (y + b) as f32));
                    }
                    Command::Line((a, b)) => {
                        path.line_to(((x + a) as f32, (y + b) as f32));
                    }
                    Command::Cubic([(a, b), (c, d), (e, f)]) => {
                        path.cubic_to(
                            ((x + a) as f32, (y + b) as f32),
                            ((x + c) as f32, (y + d) as f32),
                            ((x + e) as f32, (y + f) as f32),
                        );
                    }
                    Command::Close => {
                        path.close();
                    }
                }
            }
            canvas.draw_path(&path.detach(), &fill_paint(color));
            canvas.draw_line(
                (
                    (x + geometry.bar_start.0) as f32,
                    (y + geometry.bar_start.1) as f32,
                ),
                (
                    (x + geometry.bar_end.0) as f32,
                    (y + geometry.bar_end.1) as f32,
                ),
                &stroke_paint(color, geometry.rule_thickness),
            );

            if let Some(index) = index {
                render_box(
                    canvas,
                    fonts,
                    index,
                    x,
                    y,
                    color,
                    fs * crate::renderer::equation::layout::RADICAL_DEGREE_SCALE,
                    false,
                    false,
                );
            }
            render_box(canvas, fonts, body, x, y, color, fs, italic, bold);
        }
        LayoutKind::Superscript { base, sup } => {
            render_box(canvas, fonts, base, x, y, color, fs, italic, bold);
            render_box(
                canvas,
                fonts,
                sup,
                x,
                y,
                color,
                fs * SCRIPT_SCALE,
                italic,
                bold,
            );
        }
        LayoutKind::Subscript { base, sub } => {
            render_box(canvas, fonts, base, x, y, color, fs, italic, bold);
            render_box(
                canvas,
                fonts,
                sub,
                x,
                y,
                color,
                fs * SCRIPT_SCALE,
                italic,
                bold,
            );
        }
        LayoutKind::SubSup { base, sub, sup } => {
            render_box(canvas, fonts, base, x, y, color, fs, italic, bold);
            render_box(
                canvas,
                fonts,
                sub,
                x,
                y,
                color,
                fs * SCRIPT_SCALE,
                italic,
                bold,
            );
            render_box(
                canvas,
                fonts,
                sup,
                x,
                y,
                color,
                fs * SCRIPT_SCALE,
                italic,
                bold,
            );
        }
        LayoutKind::BigOp {
            symbol,
            sub,
            sup,
            op_font_size,
            op_x,
            op_y,
            op_baseline,
            ..
        } => {
            let is_integral = is_integral_symbol(symbol);
            // 생산 단계에서 확정한 표시 글자 크기와 원점을 소비한다.
            let op_fs = *op_font_size;
            if is_integral {
                // Task #1317: 적분 기호는 stroke path 로 렌더(geom SSOT).
                draw_integral(canvas, x + op_x, y + op_y, fs, color);
            } else {
                let op_x = x + op_x;
                let op_y = y + op_baseline;
                draw_text(
                    canvas, fonts, symbol, op_x, op_y, op_fs, false, false, color, false,
                );
            }
            if let Some(sup) = sup {
                render_box(
                    canvas,
                    fonts,
                    sup,
                    x,
                    y,
                    color,
                    fs * SCRIPT_SCALE,
                    false,
                    false,
                );
            }
            if let Some(sub) = sub {
                render_box(
                    canvas,
                    fonts,
                    sub,
                    x,
                    y,
                    color,
                    fs * SCRIPT_SCALE,
                    false,
                    false,
                );
            }
        }
        LayoutKind::Limit {
            is_upper,
            name,
            name_font_size,
            sub_font_size,
            sub,
        } => {
            if let Some(name) = name {
                render_box(canvas, fonts, name, x, y, color, name.height, false, false);
            } else {
                let name = if *is_upper { "Lim" } else { "lim" };
                draw_text(
                    canvas,
                    fonts,
                    name,
                    x,
                    y + lb.baseline,
                    *name_font_size,
                    false,
                    false,
                    color,
                    false,
                );
            }
            if let Some(sub) = sub {
                render_box(
                    canvas,
                    fonts,
                    sub,
                    x,
                    y,
                    color,
                    *sub_font_size,
                    false,
                    false,
                );
            }
        }
        LayoutKind::Matrix { cells, style } => {
            let bracket_chars = match style {
                MatrixStyle::Paren => ("(", ")"),
                MatrixStyle::Bracket => ("[", "]"),
                MatrixStyle::Vert => ("|", "|"),
                MatrixStyle::Plain | MatrixStyle::Ladder | MatrixStyle::ShortLadder => ("", ""),
            };
            if !bracket_chars.0.is_empty() {
                draw_stretch_bracket(
                    canvas,
                    fonts,
                    bracket_chars.0,
                    x,
                    y,
                    fs * 0.3,
                    lb.height,
                    color,
                    fs,
                );
                draw_stretch_bracket(
                    canvas,
                    fonts,
                    bracket_chars.1,
                    x + lb.width - fs * 0.3,
                    y,
                    fs * 0.3,
                    lb.height,
                    color,
                    fs,
                );
            }
            for row in cells {
                for cell in row {
                    render_box(canvas, fonts, cell, x, y, color, fs, italic, bold);
                }
            }
        }
        LayoutKind::Rel { arrow, over, under } => {
            render_box(canvas, fonts, over, x, y, color, fs, italic, bold);
            render_box(canvas, fonts, arrow, x, y, color, fs, italic, bold);
            if let Some(under) = under {
                render_box(canvas, fonts, under, x, y, color, fs, italic, bold);
            }
        }
        LayoutKind::EqAlign { rows } => {
            for (left, right) in rows {
                render_box(canvas, fonts, left, x, y, color, fs, italic, bold);
                render_box(canvas, fonts, right, x, y, color, fs, italic, bold);
            }
        }
        LayoutKind::Paren {
            left,
            right,
            left_width,
            right_width,
            delimiter_height,
            glyph_delimiters,
            left_outline,
            right_outline,
            body,
        } => {
            let use_glyph = *glyph_delimiters;
            if !left.is_empty() {
                if let Some(commands) = left_outline {
                    super::equation_glyph::draw_outline(canvas, commands, x, y, color, None);
                } else if use_glyph && (left == "(" || left == ")") {
                    draw_text(
                        canvas,
                        fonts,
                        left,
                        x,
                        y + lb.baseline,
                        fs,
                        false,
                        false,
                        color,
                        false,
                    );
                } else {
                    draw_stretch_bracket(
                        canvas,
                        fonts,
                        left,
                        x,
                        y,
                        *left_width,
                        *delimiter_height,
                        color,
                        fs,
                    );
                }
            }
            render_box(canvas, fonts, body, x, y, color, fs, italic, bold);
            if !right.is_empty() {
                let right_x = x + lb.width - right_width;
                if let Some(commands) = right_outline {
                    super::equation_glyph::draw_outline(canvas, commands, right_x, y, color, None);
                } else if use_glyph && (right == "(" || right == ")") {
                    draw_text(
                        canvas,
                        fonts,
                        right,
                        right_x,
                        y + lb.baseline,
                        fs,
                        false,
                        false,
                        color,
                        false,
                    );
                } else {
                    draw_stretch_bracket(
                        canvas,
                        fonts,
                        right,
                        right_x,
                        y,
                        *right_width,
                        *delimiter_height,
                        color,
                        fs,
                    );
                }
            }
        }
        LayoutKind::Decoration {
            kind,
            body,
            negation,
            outline,
        } => {
            render_box(canvas, fonts, body, x, y, color, fs, italic, bold);
            if let Some(commands) = outline {
                super::equation_glyph::draw_outline(canvas, commands, x, y, color, None);
            } else if *kind == DecoKind::StrikeThrough {
                let g = negation.clone().unwrap_or_else(|| {
                    crate::renderer::equation::negation_geometry::geometry(body)
                });
                canvas.draw_line(
                    ((x + g.start.0) as f32, (y + g.start.1) as f32),
                    ((x + g.end.0) as f32, (y + g.end.1) as f32),
                    &stroke_paint(color, g.thickness),
                );
            } else if *kind == DecoKind::Box {
                canvas.draw_rect(
                    skia_safe::Rect::from_xywh(
                        x as f32,
                        y as f32,
                        lb.width as f32,
                        lb.height as f32,
                    ),
                    &stroke_paint(color, fs * 0.03),
                );
            } else {
                let deco_y = y + fs * 0.05;
                let mid_x = x + body.x + body.width / 2.0;
                draw_decoration(canvas, *kind, mid_x, deco_y, body.width, color, fs);
            }
        }
        LayoutKind::Color { rgb, body } => {
            // RGB 범위는 해당 하위 수식에만 적용하며 형제의 원래 색상은 보존한다.
            render_box(
                canvas,
                fonts,
                body,
                x,
                y,
                Color::from_rgb(rgb[0], rgb[1], rgb[2]),
                fs,
                italic,
                bold,
            );
        }
        LayoutKind::FontStyle { style, body } => {
            let (new_italic, new_bold) = match style {
                FontStyleKind::Roman | FontStyleKind::SansSerif | FontStyleKind::Monospace => {
                    (false, false)
                }
                FontStyleKind::Italic => (true, bold),
                FontStyleKind::Bold => (italic, true),
                FontStyleKind::Blackboard => (false, true),
                FontStyleKind::Calligraphy | FontStyleKind::Fraktur => (false, false),
            };
            render_box(canvas, fonts, body, x, y, color, fs, new_italic, new_bold);
        }
        LayoutKind::Space(_) | LayoutKind::Newline | LayoutKind::Empty => {}
    }
}

fn draw_decoration(
    canvas: &Canvas,
    kind: DecoKind,
    mid_x: f64,
    y: f64,
    width: f64,
    color: Color,
    fs: f64,
) {
    let half_w = width / 2.0;
    let paint = stroke_paint(color, fs * 0.03);

    match kind {
        DecoKind::Hat => {
            let mut path = PathBuilder::new();
            path.move_to(((mid_x - half_w * 0.6) as f32, (y + fs * 0.15) as f32));
            path.line_to((mid_x as f32, y as f32));
            path.line_to(((mid_x + half_w * 0.6) as f32, (y + fs * 0.15) as f32));
            canvas.draw_path(&path.detach(), &paint);
        }
        DecoKind::Bar | DecoKind::Overline => {
            canvas.draw_line(
                ((mid_x - half_w) as f32, (y + fs * 0.05) as f32),
                ((mid_x + half_w) as f32, (y + fs * 0.05) as f32),
                &paint,
            );
        }
        DecoKind::Vec | DecoKind::Dyad => {
            let arrow_y = y + fs * 0.05;
            canvas.draw_line(
                ((mid_x - half_w) as f32, arrow_y as f32),
                ((mid_x + half_w) as f32, arrow_y as f32),
                &paint,
            );
            let mut head = PathBuilder::new();
            head.move_to((
                (mid_x + half_w - fs * 0.1) as f32,
                (arrow_y - fs * 0.06) as f32,
            ));
            head.line_to(((mid_x + half_w) as f32, arrow_y as f32));
            head.line_to((
                (mid_x + half_w - fs * 0.1) as f32,
                (arrow_y + fs * 0.06) as f32,
            ));
            canvas.draw_path(&head.detach(), &paint);
            if kind == DecoKind::Dyad {
                let mut head = PathBuilder::new();
                head.move_to((
                    (mid_x - half_w + fs * 0.1) as f32,
                    (arrow_y - fs * 0.06) as f32,
                ));
                head.line_to(((mid_x - half_w) as f32, arrow_y as f32));
                head.line_to((
                    (mid_x - half_w + fs * 0.1) as f32,
                    (arrow_y + fs * 0.06) as f32,
                ));
                canvas.draw_path(&head.detach(), &paint);
            }
        }
        DecoKind::Tilde => {
            let ty = y + fs * 0.08;
            let mut path = PathBuilder::new();
            path.move_to(((mid_x - half_w * 0.6) as f32, ty as f32));
            path.quad_to(
                ((mid_x - half_w * 0.2) as f32, (ty - fs * 0.08) as f32),
                (mid_x as f32, ty as f32),
            );
            path.quad_to(
                ((mid_x + half_w * 0.2) as f32, (ty + fs * 0.08) as f32),
                ((mid_x + half_w * 0.6) as f32, ty as f32),
            );
            canvas.draw_path(&path.detach(), &paint);
        }
        DecoKind::Dot => {
            canvas.draw_circle(
                (mid_x as f32, (y + fs * 0.06) as f32),
                (fs * 0.03) as f32,
                &fill_paint(color),
            );
        }
        DecoKind::DDot => {
            let gap = fs * 0.1;
            let fill = fill_paint(color);
            canvas.draw_circle(
                ((mid_x - gap) as f32, (y + fs * 0.06) as f32),
                (fs * 0.03) as f32,
                &fill,
            );
            canvas.draw_circle(
                ((mid_x + gap) as f32, (y + fs * 0.06) as f32),
                (fs * 0.03) as f32,
                &fill,
            );
        }
        DecoKind::Underline | DecoKind::Under => {
            let underline_y = y + fs * 1.1;
            canvas.draw_line(
                ((mid_x - half_w) as f32, underline_y as f32),
                ((mid_x + half_w) as f32, underline_y as f32),
                &paint,
            );
        }
        _ => {
            canvas.draw_line(
                ((mid_x - half_w * 0.5) as f32, (y + fs * 0.1) as f32),
                ((mid_x + half_w * 0.5) as f32, (y + fs * 0.1) as f32),
                &paint,
            );
        }
    }
}

fn font_size_from_box(lb: &LayoutBox, base_fs: f64) -> f64 {
    if lb.font_size > 0.0 {
        lb.font_size
    } else {
        base_fs
    }
}

/// 적분 기호(∫)를 stroke path 로 렌더 (Task #1317).
///
/// SVG·Canvas2D와 같은 공개 적분 윤곽 및 공통 ink 영역을 소비한다.
fn draw_integral(canvas: &Canvas, x: f64, y: f64, fs: f64, color: Color) {
    use crate::renderer::equation::integral_outline::{commands, Command};
    let mut path = PathBuilder::new();
    for command in commands(x, y, fs) {
        match command {
            Command::Move((x, y)) => {
                path.move_to((x as f32, y as f32));
            }
            Command::Line((x, y)) => {
                path.line_to((x as f32, y as f32));
            }
            Command::Cubic([(a, b), (c, d), (e, f)]) => {
                path.cubic_to(
                    (a as f32, b as f32),
                    (c as f32, d as f32),
                    (e as f32, f as f32),
                );
            }
            Command::Close => {
                path.close();
            }
        }
    }
    canvas.draw_path(&path.detach(), &fill_paint(color));
}

fn fill_paint(color: Color) -> Paint {
    let mut paint = Paint::default();
    paint.set_anti_alias(true);
    paint.set_style(paint::Style::Fill);
    paint.set_color(color);
    paint
}

fn stroke_paint(color: Color, width: f64) -> Paint {
    let mut paint = Paint::default();
    paint.set_anti_alias(true);
    paint.set_style(paint::Style::Stroke);
    paint.set_stroke_width(width.max(0.5) as f32);
    paint.set_color(color);
    paint
}

fn colorref_to_skia(color: u32, alpha_scale: f32) -> Color {
    let b = ((color >> 16) & 0xFF) as u8;
    let g = ((color >> 8) & 0xFF) as u8;
    let r = (color & 0xFF) as u8;
    let a = (255.0 * alpha_scale.clamp(0.0, 1.0)).round() as u8;
    Color::from_argb(a, r, g, b)
}