Use extension traits

This commit is contained in:
Laurenz Stampfl 2024-12-05 23:37:42 +01:00
parent 237c696292
commit f53ac9bfd9
3 changed files with 79 additions and 46 deletions

View File

@ -24,6 +24,7 @@ use typst_library::visualize::{
FillRule, Geometry, Image, ImageKind, Path, PathItem, RasterFormat, RasterImage, FillRule, Geometry, Image, ImageKind, Path, PathItem, RasterFormat, RasterImage,
Shape, Shape,
}; };
use crate::primitive::{PointExt, SizeExt, TransformExt};
#[derive(TransparentWrapper)] #[derive(TransparentWrapper)]
#[repr(transparent)] #[repr(transparent)]
@ -122,7 +123,7 @@ pub fn handle_group(
let old = context.cur_transform; let old = context.cur_transform;
context.cur_transform = context.cur_transform.pre_concat(group.transform); context.cur_transform = context.cur_transform.pre_concat(group.transform);
surface.push_transform(&primitive::transform(group.transform)); surface.push_transform(&group.transform.as_krilla());
process_frame(&group.frame, surface, context); process_frame(&group.frame, surface, context);
context.cur_transform = old; context.cur_transform = old;
@ -180,10 +181,10 @@ pub fn handle_image(
ImageKind::Raster(raster) => { ImageKind::Raster(raster) => {
// TODO: Don't unwrap // TODO: Don't unwrap
let image = crate::image::raster(raster.clone()).unwrap(); let image = crate::image::raster(raster.clone()).unwrap();
surface.draw_image(image, primitive::size(size)); surface.draw_image(image, size.as_krilla());
} }
ImageKind::Svg(svg) => { ImageKind::Svg(svg) => {
surface.draw_svg(svg.tree(), primitive::size(size), SvgSettings::default()); surface.draw_svg(svg.tree(), size.as_krilla(), SvgSettings::default());
} }
} }
} }
@ -297,7 +298,7 @@ fn handle_link(
} }
Destination::Position(p) => { Destination::Position(p) => {
Target::Destination(krilla::destination::Destination::Xyz( Target::Destination(krilla::destination::Destination::Xyz(
XyzDestination::new(p.page.get() - 1, primitive::point(p.point)), XyzDestination::new(p.page.get() - 1, p.point.as_krilla()),
)) ))
} }
Destination::Location(_) => return, Destination::Location(_) => return,

View File

@ -2,17 +2,17 @@
use krilla::geom::NormalizedF32; use krilla::geom::NormalizedF32;
use typst_library::layout::Abs; use typst_library::layout::Abs;
use typst_library::visualize::{ColorSpace, DashPattern, FillRule, FixedStroke, Paint, Pattern}; use typst_library::visualize::{ColorSpace, DashPattern, FillRule, FixedStroke, Paint};
use crate::primitive::{linecap, linejoin};
use crate::AbsExt; use crate::AbsExt;
use crate::primitive::{FillRuleExt, LineCapExt, LineJoinExt};
pub(crate) fn fill(paint_: &Paint, fill_rule_: FillRule) -> krilla::path::Fill { pub(crate) fn fill(paint_: &Paint, fill_rule_: FillRule) -> krilla::path::Fill {
let (paint, opacity) = paint(paint_); let (paint, opacity) = paint(paint_);
krilla::path::Fill { krilla::path::Fill {
paint, paint,
rule: fill_rule(fill_rule_), rule: fill_rule_.as_krilla(),
opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(), opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(),
} }
} }
@ -23,8 +23,8 @@ pub(crate) fn stroke(stroke: &FixedStroke) -> krilla::path::Stroke {
paint, paint,
width: stroke.thickness.to_f32(), width: stroke.thickness.to_f32(),
miter_limit: stroke.miter_limit.get() as f32, miter_limit: stroke.miter_limit.get() as f32,
line_join: linejoin(stroke.join), line_join: stroke.join.as_krilla(),
line_cap: linecap(stroke.cap), line_cap: stroke.cap.as_krilla(),
opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(), opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(),
dash: stroke.dash.as_ref().map(|d| dash(d)), dash: stroke.dash.as_ref().map(|d| dash(d)),
} }
@ -55,14 +55,3 @@ fn paint(paint: &Paint) -> (krilla::paint::Paint, u8) {
Paint::Pattern(_) => (krilla::color::rgb::Color::black().into(), 255), Paint::Pattern(_) => (krilla::color::rgb::Color::black().into(), 255),
} }
} }
fn fill_rule(fill_rule: FillRule) -> krilla::path::FillRule {
match fill_rule {
FillRule::NonZero => krilla::path::FillRule::NonZero,
FillRule::EvenOdd => krilla::path::FillRule::EvenOdd,
}
}
fn pattern(pattern: &Pattern) -> krilla::paint::Pattern {
}

View File

@ -1,41 +1,84 @@
//! Convert basic primitive types from typst to krilla. //! Convert basic primitive types from typst to krilla.
use typst_library::layout::{Point, Size, Transform}; use typst_library::layout::{Point, Size, Transform};
use typst_library::visualize::{LineCap, LineJoin}; use typst_library::visualize::{FillRule, LineCap, LineJoin};
use crate::AbsExt; use crate::AbsExt;
pub(crate) fn size(s: Size) -> krilla::geom::Size { pub(crate) trait SizeExt {
krilla::geom::Size::from_wh(s.x.to_f32(), s.y.to_f32()).unwrap() fn as_krilla(&self) -> krilla::geom::Size;
} }
pub(crate) fn point(p: Point) -> krilla::geom::Point { impl SizeExt for Size {
krilla::geom::Point::from_xy(p.x.to_f32(), p.y.to_f32()) fn as_krilla(&self) -> krilla::geom::Size {
krilla::geom::Size::from_wh(self.x.to_f32(), self.y.to_f32()).unwrap()
}
} }
pub(crate) fn linecap(l: LineCap) -> krilla::path::LineCap { pub(crate) trait PointExt {
match l { fn as_krilla(&self) -> krilla::geom::Point;
}
impl PointExt for Point {
fn as_krilla(&self) -> krilla::geom::Point {
krilla::geom::Point::from_xy(self.x.to_f32(), self.y.to_f32())
}
}
pub(crate) trait LineCapExt {
fn as_krilla(&self) -> krilla::path::LineCap;
}
impl LineCapExt for LineCap {
fn as_krilla(&self) -> krilla::path::LineCap {
match self {
LineCap::Butt => krilla::path::LineCap::Butt, LineCap::Butt => krilla::path::LineCap::Butt,
LineCap::Round => krilla::path::LineCap::Round, LineCap::Round => krilla::path::LineCap::Round,
LineCap::Square => krilla::path::LineCap::Square, LineCap::Square => krilla::path::LineCap::Square,
} }
} }
}
pub(crate) fn linejoin(l: LineJoin) -> krilla::path::LineJoin { pub(crate) trait LineJoinExt {
match l { fn as_krilla(&self) -> krilla::path::LineJoin;
}
impl LineJoinExt for LineJoin {
fn as_krilla(&self) -> krilla::path::LineJoin {
match self {
LineJoin::Miter => krilla::path::LineJoin::Miter, LineJoin::Miter => krilla::path::LineJoin::Miter,
LineJoin::Round => krilla::path::LineJoin::Round, LineJoin::Round => krilla::path::LineJoin::Round,
LineJoin::Bevel => krilla::path::LineJoin::Bevel, LineJoin::Bevel => krilla::path::LineJoin::Bevel,
} }
} }
}
pub(crate) fn transform(t: Transform) -> krilla::geom::Transform { pub(crate) trait TransformExt {
fn as_krilla(&self) -> krilla::geom::Transform;
}
impl TransformExt for Transform {
fn as_krilla(&self) -> krilla::geom::Transform {
krilla::geom::Transform::from_row( krilla::geom::Transform::from_row(
t.sx.get() as f32, self.sx.get() as f32,
t.ky.get() as f32, self.ky.get() as f32,
t.kx.get() as f32, self.kx.get() as f32,
t.sy.get() as f32, self.sy.get() as f32,
t.tx.to_f32(), self.tx.to_f32(),
t.ty.to_f32(), self.ty.to_f32(),
) )
} }
}
pub(crate) trait FillRuleExt {
fn as_krilla(&self) -> krilla::path::FillRule;
}
impl FillRuleExt for FillRule {
fn as_krilla(&self) -> krilla::path::FillRule {
match self {
FillRule::NonZero => krilla::path::FillRule::NonZero,
FillRule::EvenOdd => krilla::path::FillRule::EvenOdd,
}
}
}