mirror of
https://github.com/typst/typst
synced 2025-06-08 13:16:24 +08:00
Use extension traits
This commit is contained in:
parent
237c696292
commit
f53ac9bfd9
@ -24,6 +24,7 @@ use typst_library::visualize::{
|
||||
FillRule, Geometry, Image, ImageKind, Path, PathItem, RasterFormat, RasterImage,
|
||||
Shape,
|
||||
};
|
||||
use crate::primitive::{PointExt, SizeExt, TransformExt};
|
||||
|
||||
#[derive(TransparentWrapper)]
|
||||
#[repr(transparent)]
|
||||
@ -122,7 +123,7 @@ pub fn handle_group(
|
||||
let old = context.cur_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);
|
||||
|
||||
context.cur_transform = old;
|
||||
@ -180,10 +181,10 @@ pub fn handle_image(
|
||||
ImageKind::Raster(raster) => {
|
||||
// TODO: Don't 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) => {
|
||||
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) => {
|
||||
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,
|
||||
|
@ -2,17 +2,17 @@
|
||||
|
||||
use krilla::geom::NormalizedF32;
|
||||
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::primitive::{FillRuleExt, LineCapExt, LineJoinExt};
|
||||
|
||||
pub(crate) fn fill(paint_: &Paint, fill_rule_: FillRule) -> krilla::path::Fill {
|
||||
let (paint, opacity) = paint(paint_);
|
||||
|
||||
krilla::path::Fill {
|
||||
paint,
|
||||
rule: fill_rule(fill_rule_),
|
||||
rule: fill_rule_.as_krilla(),
|
||||
opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(),
|
||||
}
|
||||
}
|
||||
@ -23,8 +23,8 @@ pub(crate) fn stroke(stroke: &FixedStroke) -> krilla::path::Stroke {
|
||||
paint,
|
||||
width: stroke.thickness.to_f32(),
|
||||
miter_limit: stroke.miter_limit.get() as f32,
|
||||
line_join: linejoin(stroke.join),
|
||||
line_cap: linecap(stroke.cap),
|
||||
line_join: stroke.join.as_krilla(),
|
||||
line_cap: stroke.cap.as_krilla(),
|
||||
opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(),
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
}
|
@ -1,41 +1,84 @@
|
||||
//! Convert basic primitive types from typst to krilla.
|
||||
|
||||
use typst_library::layout::{Point, Size, Transform};
|
||||
use typst_library::visualize::{LineCap, LineJoin};
|
||||
use typst_library::visualize::{FillRule, LineCap, LineJoin};
|
||||
|
||||
use crate::AbsExt;
|
||||
|
||||
pub(crate) fn size(s: Size) -> krilla::geom::Size {
|
||||
krilla::geom::Size::from_wh(s.x.to_f32(), s.y.to_f32()).unwrap()
|
||||
pub(crate) trait SizeExt {
|
||||
fn as_krilla(&self) -> krilla::geom::Size;
|
||||
}
|
||||
|
||||
pub(crate) fn point(p: Point) -> krilla::geom::Point {
|
||||
krilla::geom::Point::from_xy(p.x.to_f32(), p.y.to_f32())
|
||||
}
|
||||
|
||||
pub(crate) fn linecap(l: LineCap) -> krilla::path::LineCap {
|
||||
match l {
|
||||
LineCap::Butt => krilla::path::LineCap::Butt,
|
||||
LineCap::Round => krilla::path::LineCap::Round,
|
||||
LineCap::Square => krilla::path::LineCap::Square,
|
||||
impl SizeExt for Size {
|
||||
fn as_krilla(&self) -> krilla::geom::Size {
|
||||
krilla::geom::Size::from_wh(self.x.to_f32(), self.y.to_f32()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn linejoin(l: LineJoin) -> krilla::path::LineJoin {
|
||||
match l {
|
||||
LineJoin::Miter => krilla::path::LineJoin::Miter,
|
||||
LineJoin::Round => krilla::path::LineJoin::Round,
|
||||
LineJoin::Bevel => krilla::path::LineJoin::Bevel,
|
||||
pub(crate) trait PointExt {
|
||||
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) fn transform(t: Transform) -> krilla::geom::Transform {
|
||||
krilla::geom::Transform::from_row(
|
||||
t.sx.get() as f32,
|
||||
t.ky.get() as f32,
|
||||
t.kx.get() as f32,
|
||||
t.sy.get() as f32,
|
||||
t.tx.to_f32(),
|
||||
t.ty.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::Round => krilla::path::LineCap::Round,
|
||||
LineCap::Square => krilla::path::LineCap::Square,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait LineJoinExt {
|
||||
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::Round => krilla::path::LineJoin::Round,
|
||||
LineJoin::Bevel => krilla::path::LineJoin::Bevel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
self.sx.get() as f32,
|
||||
self.ky.get() as f32,
|
||||
self.kx.get() as f32,
|
||||
self.sy.get() as f32,
|
||||
self.tx.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,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user