Repository: typst/typst
File: crates/typst-render/src/lib.rs (Rust)

Code before:
            );
        }
    }

    sk_paint
}

fn render_pattern_frame(state: &State, pattern: &Pattern) -> sk::Pixmap {
    let size = pattern.size() + pattern.spacing();
    let mut canvas = sk::Pixmap::new(
        (size.x.to_f32() * state.pixel_per_pt).round() as u32,
        (size.y.to_f32() * state.pixel_per_pt).round() as u32,
    )
    .unwrap();

    // Render the pattern into a new canvas.
    let ts = sk::Transform::from_scale(state.pixel_per_pt, state.pixel_per_pt);
    let temp_state = State::new(pattern.size(), ts, state.pixel_per_pt);
    render_frame(&mut canvas, temp_state, pattern.frame());
    canvas
}

fn to_sk_color(color: Color) -> sk::Color {
    let [r, g, b, a] = color.to_rgb().to_vec4_u8();
    sk::Color::from_rgba8(r, g, b, a)
}

fn to_sk_color_u8_without_alpha(color: Color) -> sk::ColorU8 {
    let [r, g, b, _] = color.to_rgb().to_vec4_u8();
    sk::ColorU8::from_rgba(r, g, b, 255)
}

fn to_sk_line_cap(cap: LineCap) -> sk::LineCap {
    match cap {
        LineCap::Butt => sk::LineCap::Butt,
        LineCap::Round => sk::LineCap::Round,
        LineCap::Square => sk::LineCap::Square,
    }
}

fn to_sk_line_join(join: LineJoin) -> sk::LineJoin {
    match join {
        LineJoin::Miter => sk::LineJoin::Miter,
        LineJoin::Round => sk::LineJoin::Round,
        LineJoin::Bevel => sk::LineJoin::Bevel,
    }
}

fn to_sk_transform(transform: &Transform) -> sk::Transform {
    let Transform { sx, ky, kx, sy, tx, ty } = *transform;
    sk::Transform::from_row(

Review comment (suggestion):
I suggest using math notation

```suggestion
        .expect("components must always be in the range [0, 1]")
```


Code after:
            );
        }
    }

    sk_paint
}

fn render_pattern_frame(state: &State, pattern: &Pattern) -> sk::Pixmap {
    let size = pattern.size() + pattern.spacing();
    let mut canvas = sk::Pixmap::new(
        (size.x.to_f32() * state.pixel_per_pt).round() as u32,
        (size.y.to_f32() * state.pixel_per_pt).round() as u32,
    )
    .unwrap();

    // Render the pattern into a new canvas.
    let ts = sk::Transform::from_scale(state.pixel_per_pt, state.pixel_per_pt);
    let temp_state = State::new(pattern.size(), ts, state.pixel_per_pt);
    render_frame(&mut canvas, temp_state, pattern.frame());
    canvas
}

fn to_sk_color(color: Color) -> sk::Color {
    let [r, g, b, a] = color.to_rgb().to_vec4();
    sk::Color::from_rgba(r, g, b, a)
        .expect("components must always be in the range [0..=1]")
}

fn to_sk_color_u8_without_alpha(color: Color) -> sk::ColorU8 {
    let [r, g, b, _] = color.to_rgb().to_vec4_u8();
    sk::ColorU8::from_rgba(r, g, b, 255)
}

fn to_sk_line_cap(cap: LineCap) -> sk::LineCap {
    match cap {
        LineCap::Butt => sk::LineCap::Butt,
        LineCap::Round => sk::LineCap::Round,
        LineCap::Square => sk::LineCap::Square,
    }
}

fn to_sk_line_join(join: LineJoin) -> sk::LineJoin {
    match join {
        LineJoin::Miter => sk::LineJoin::Miter,
        LineJoin::Round => sk::LineJoin::Round,
        LineJoin::Bevel => sk::LineJoin::Bevel,
    }
}

fn to_sk_transform(transform: &Transform) -> sk::Transform {
    let Transform { sx, ky, kx, sy, tx, ty } = *transform;