This commit is contained in:
Laurenz Stampfl 2024-12-17 19:44:38 +01:00
parent c7a647d083
commit 47c7af2e86
9 changed files with 186 additions and 310 deletions

152
Cargo.lock generated
View File

@ -806,20 +806,6 @@ dependencies = [
"roxmltree", "roxmltree",
] ]
[[package]]
name = "fontdb"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37be9fc20d966be438cd57a45767f73349477fb0f85ce86e000557f787298afb"
dependencies = [
"fontconfig-parser",
"log",
"memmap2",
"slotmap",
"tinyvec",
"ttf-parser",
]
[[package]] [[package]]
name = "fontdb" name = "fontdb"
version = "0.22.0" version = "0.22.0"
@ -1333,23 +1319,23 @@ dependencies = [
"comemo", "comemo",
"flate2", "flate2",
"float-cmp 0.10.0", "float-cmp 0.10.0",
"fontdb 0.22.0", "fontdb",
"gif", "gif",
"image-webp", "image-webp",
"imagesize", "imagesize",
"miniz_oxide", "miniz_oxide",
"once_cell", "once_cell",
"pdf-writer 0.12.0 (git+https://github.com/LaurenzV/pdf-writer?rev=f95a19c)", "pdf-writer",
"rayon", "rayon",
"resvg 0.44.0", "resvg",
"rustybuzz", "rustybuzz",
"siphasher 1.0.1", "siphasher 1.0.1",
"skrifa", "skrifa",
"subsetter 0.2.0 (git+https://github.com/typst/subsetter?rev=172416a)", "subsetter",
"tiny-skia", "tiny-skia",
"tiny-skia-path", "tiny-skia-path",
"usvg 0.44.0", "usvg",
"xmp-writer 0.3.0 (git+https://github.com/LaurenzV/xmp-writer?rev=1c2b8ae9)", "xmp-writer",
"yoke", "yoke",
"zune-jpeg", "zune-jpeg",
"zune-png", "zune-png",
@ -1781,18 +1767,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]]
name = "pdf-writer"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be17f48d7fbbd22c6efedb58af5d409aa578e407f40b29a0bcb4e66ed84c5c98"
dependencies = [
"bitflags 2.6.0",
"itoa",
"memchr",
"ryu",
]
[[package]] [[package]]
name = "pdf-writer" name = "pdf-writer"
version = "0.12.0" version = "0.12.0"
@ -2112,23 +2086,6 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "resvg"
version = "0.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7314563c59c7ce31c18e23ad3dd092c37b928a0fa4e1c0a1a6504351ab411d1"
dependencies = [
"gif",
"image-webp",
"log",
"pico-args",
"rgb",
"svgtypes",
"tiny-skia",
"usvg 0.43.0",
"zune-jpeg",
]
[[package]] [[package]]
name = "resvg" name = "resvg"
version = "0.44.0" version = "0.44.0"
@ -2142,7 +2099,7 @@ dependencies = [
"rgb", "rgb",
"svgtypes", "svgtypes",
"tiny-skia", "tiny-skia",
"usvg 0.44.0", "usvg",
"zune-jpeg", "zune-jpeg",
] ]
@ -2511,37 +2468,11 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "subsetter"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74f98178f34057d4d4de93d68104007c6dea4dfac930204a69ab4622daefa648"
[[package]] [[package]]
name = "subsetter" name = "subsetter"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/typst/subsetter?rev=172416a#172416a806246e6e9010d400d5690ca7a026e53d" source = "git+https://github.com/typst/subsetter?rev=172416a#172416a806246e6e9010d400d5690ca7a026e53d"
[[package]]
name = "svg2pdf"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5014c9dadcf318fb7ef8c16438e95abcc9de1ae24d60d5bccc64c55100c50364"
dependencies = [
"fontdb 0.21.0",
"image",
"log",
"miniz_oxide",
"once_cell",
"pdf-writer 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"resvg 0.43.0",
"siphasher 1.0.1",
"subsetter 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tiny-skia",
"ttf-parser",
"usvg 0.43.0",
]
[[package]] [[package]]
name = "svgtypes" name = "svgtypes"
version = "0.15.2" version = "0.15.2"
@ -2988,7 +2919,7 @@ dependencies = [
"ecow", "ecow",
"env_proxy", "env_proxy",
"flate2", "flate2",
"fontdb 0.22.0", "fontdb",
"native-tls", "native-tls",
"once_cell", "once_cell",
"openssl", "openssl",
@ -3045,7 +2976,7 @@ dependencies = [
"csv", "csv",
"ecow", "ecow",
"flate2", "flate2",
"fontdb 0.22.0", "fontdb",
"hayagriva", "hayagriva",
"icu_properties", "icu_properties",
"icu_provider", "icu_provider",
@ -3084,7 +3015,7 @@ dependencies = [
"unicode-math-class", "unicode-math-class",
"unicode-segmentation", "unicode-segmentation",
"unscanny", "unscanny",
"usvg 0.44.0", "usvg",
"wasmi", "wasmi",
"xmlwriter", "xmlwriter",
] ]
@ -3116,32 +3047,6 @@ dependencies = [
"typst-utils", "typst-utils",
] ]
[[package]]
name = "typst-pdf-old"
version = "0.12.0"
dependencies = [
"arrayvec",
"base64",
"bytemuck",
"comemo",
"ecow",
"image",
"indexmap 2.6.0",
"miniz_oxide",
"pdf-writer 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde",
"subsetter 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"svg2pdf",
"ttf-parser",
"typst-assets",
"typst-library",
"typst-macros",
"typst-syntax",
"typst-timing",
"typst-utils",
"xmp-writer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "typst-realize" name = "typst-realize"
version = "0.12.0" version = "0.12.0"
@ -3166,7 +3071,7 @@ dependencies = [
"comemo", "comemo",
"image", "image",
"pixglyph", "pixglyph",
"resvg 0.44.0", "resvg",
"tiny-skia", "tiny-skia",
"ttf-parser", "ttf-parser",
"typst-library", "typst-library",
@ -3388,33 +3293,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "usvg"
version = "0.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6803057b5cbb426e9fb8ce2216f3a9b4ca1dd2c705ba3cbebc13006e437735fd"
dependencies = [
"base64",
"data-url",
"flate2",
"fontdb 0.21.0",
"imagesize",
"kurbo",
"log",
"pico-args",
"roxmltree",
"rustybuzz",
"simplecss",
"siphasher 1.0.1",
"strict-num",
"svgtypes",
"tiny-skia-path",
"unicode-bidi",
"unicode-script",
"unicode-vo",
"xmlwriter",
]
[[package]] [[package]]
name = "usvg" name = "usvg"
version = "0.44.0" version = "0.44.0"
@ -3424,7 +3302,7 @@ dependencies = [
"base64", "base64",
"data-url", "data-url",
"flate2", "flate2",
"fontdb 0.22.0", "fontdb",
"imagesize", "imagesize",
"kurbo", "kurbo",
"log", "log",
@ -3809,12 +3687,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"
[[package]]
name = "xmp-writer"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8254499146a4fd0c86e3e99cf4a9f468f595808fb49ff8f3e495f2b117bf4ebc"
[[package]] [[package]]
name = "xmp-writer" name = "xmp-writer"
version = "0.3.0" version = "0.3.0"

View File

@ -31,6 +31,7 @@ use typst_syntax::Span;
/// which is mainly needed to resolve gradients and patterns correctly. /// which is mainly needed to resolve gradients and patterns correctly.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct State { pub(crate) struct State {
/// The current transform.
transform: Transform, transform: Transform,
/// The transform of first hard frame in the hierarchy. /// The transform of first hard frame in the hierarchy.
container_transform: Transform, container_transform: Transform,

View File

@ -1,5 +1,3 @@
use crate::krilla::{FrameContext, GlobalContext};
use crate::util::{AbsExt, PointExt};
use krilla::action::{Action, LinkAction}; use krilla::action::{Action, LinkAction};
use krilla::annotation::{LinkAnnotation, Target}; use krilla::annotation::{LinkAnnotation, Target};
use krilla::destination::XyzDestination; use krilla::destination::XyzDestination;
@ -7,7 +5,9 @@ use krilla::geom::Rect;
use typst_library::layout::{Abs, Point, Size}; use typst_library::layout::{Abs, Point, Size};
use typst_library::model::Destination; use typst_library::model::Destination;
/// Save a link for later writing in the annotations dictionary. use crate::krilla::{FrameContext, GlobalContext};
use crate::util::{AbsExt, PointExt};
pub(crate) fn handle_link( pub(crate) fn handle_link(
fc: &mut FrameContext, fc: &mut FrameContext,
gc: &mut GlobalContext, gc: &mut GlobalContext,
@ -55,14 +55,14 @@ pub(crate) fn handle_link(
} }
Destination::Position(p) => *p, Destination::Position(p) => *p,
Destination::Location(loc) => { Destination::Location(loc) => {
if let Some(named_dest) = gc.loc_to_named.get(loc) { if let Some(nd) = gc.loc_to_named.get(loc) {
// If a named destination has been registered, it's already guaranteed to // If a named destination has been registered, it's already guaranteed to
// not point to an excluded page. // not point to an excluded page.
fc.annotations.push( fc.annotations.push(
LinkAnnotation::new( LinkAnnotation::new(
rect, rect,
Target::Destination(krilla::destination::Destination::Named( Target::Destination(krilla::destination::Destination::Named(
named_dest.clone(), nd.clone(),
)), )),
) )
.into(), .into(),

View File

@ -56,39 +56,35 @@ fn convert_date(
) -> Option<krilla::metadata::DateTime> { ) -> Option<krilla::metadata::DateTime> {
let year = datetime.year().filter(|&y| y >= 0)? as u16; let year = datetime.year().filter(|&y| y >= 0)? as u16;
let mut krilla_date = krilla::metadata::DateTime::new(year); let mut kd = krilla::metadata::DateTime::new(year);
if let Some(month) = datetime.month() { if let Some(month) = datetime.month() {
krilla_date = krilla_date.month(month); kd = kd.month(month);
} }
if let Some(day) = datetime.day() { if let Some(day) = datetime.day() {
krilla_date = krilla_date.day(day); kd = kd.day(day);
} }
if let Some(h) = datetime.hour() { if let Some(h) = datetime.hour() {
krilla_date = krilla_date.hour(h); kd = kd.hour(h);
} }
if let Some(m) = datetime.minute() { if let Some(m) = datetime.minute() {
krilla_date = krilla_date.minute(m); kd = kd.minute(m);
} }
if let Some(s) = datetime.second() { if let Some(s) = datetime.second() {
krilla_date = krilla_date.second(s); kd = kd.second(s);
} }
match tz { match tz {
Some(Timezone::UTC) => { Some(Timezone::UTC) => kd = kd.utc_offset_hour(0).utc_offset_minute(0),
krilla_date = krilla_date.utc_offset_hour(0).utc_offset_minute(0)
}
Some(Timezone::Local { hour_offset, minute_offset }) => { Some(Timezone::Local { hour_offset, minute_offset }) => {
krilla_date = krilla_date kd = kd.utc_offset_hour(hour_offset).utc_offset_minute(minute_offset)
.utc_offset_hour(hour_offset)
.utc_offset_minute(minute_offset)
} }
None => {} None => {}
} }
Some(krilla_date) Some(kd)
} }

View File

@ -9,52 +9,6 @@ use typst_library::model::HeadingElem;
use crate::krilla::GlobalContext; use crate::krilla::GlobalContext;
use crate::util::AbsExt; use crate::util::AbsExt;
#[derive(Debug)]
struct HeadingNode<'a> {
element: &'a Packed<HeadingElem>,
level: NonZeroUsize,
bookmarked: bool,
children: Vec<HeadingNode<'a>>,
}
impl<'a> HeadingNode<'a> {
fn leaf(element: &'a Packed<HeadingElem>) -> Self {
HeadingNode {
level: element.resolve_level(StyleChain::default()),
// 'bookmarked' set to 'auto' falls back to the value of 'outlined'.
bookmarked: element
.bookmarked(StyleChain::default())
.unwrap_or_else(|| element.outlined(StyleChain::default())),
element,
children: Vec::new(),
}
}
fn to_krilla(&self, gc: &GlobalContext) -> Option<OutlineNode> {
let loc = self.element.location().unwrap();
let title = self.element.body().plain_text().to_string();
let pos = gc.document.introspector.position(loc);
let page_index = pos.page.get() - 1;
if !gc.page_excluded(page_index) {
let y = (pos.point.y - Abs::pt(10.0)).max(Abs::zero());
let dest = XyzDestination::new(
page_index,
krilla::geom::Point::from_xy(pos.point.x.to_f32(), y.to_f32()),
);
let mut outline_node = OutlineNode::new(title, dest);
for child in convert_nodes(&self.children, gc) {
outline_node.push_child(child);
}
return Some(outline_node);
}
None
}
}
pub(crate) fn build_outline(gc: &GlobalContext) -> Outline { pub(crate) fn build_outline(gc: &GlobalContext) -> Outline {
let mut tree: Vec<HeadingNode> = vec![]; let mut tree: Vec<HeadingNode> = vec![];
@ -147,6 +101,52 @@ pub(crate) fn build_outline(gc: &GlobalContext) -> Outline {
outline outline
} }
#[derive(Debug)]
struct HeadingNode<'a> {
element: &'a Packed<HeadingElem>,
level: NonZeroUsize,
bookmarked: bool,
children: Vec<HeadingNode<'a>>,
}
impl<'a> HeadingNode<'a> {
fn leaf(element: &'a Packed<HeadingElem>) -> Self {
HeadingNode {
level: element.resolve_level(StyleChain::default()),
// 'bookmarked' set to 'auto' falls back to the value of 'outlined'.
bookmarked: element
.bookmarked(StyleChain::default())
.unwrap_or_else(|| element.outlined(StyleChain::default())),
element,
children: Vec::new(),
}
}
fn to_krilla(&self, gc: &GlobalContext) -> Option<OutlineNode> {
let loc = self.element.location().unwrap();
let title = self.element.body().plain_text().to_string();
let pos = gc.document.introspector.position(loc);
let page_index = pos.page.get() - 1;
if !gc.page_excluded(page_index) {
let y = (pos.point.y - Abs::pt(10.0)).max(Abs::zero());
let dest = XyzDestination::new(
page_index,
krilla::geom::Point::from_xy(pos.point.x.to_f32(), y.to_f32()),
);
let mut outline_node = OutlineNode::new(title, dest);
for child in convert_nodes(&self.children, gc) {
outline_node.push_child(child);
}
return Some(outline_node);
}
None
}
}
fn convert_nodes(nodes: &[HeadingNode], gc: &GlobalContext) -> Vec<OutlineNode> { fn convert_nodes(nodes: &[HeadingNode], gc: &GlobalContext) -> Vec<OutlineNode> {
nodes.iter().flat_map(|node| node.to_krilla(gc)).collect() nodes.iter().flat_map(|node| node.to_krilla(gc)).collect()
} }

View File

@ -3,7 +3,7 @@ use std::num::NonZeroUsize;
use krilla::page::{NumberingStyle, PageLabel}; use krilla::page::{NumberingStyle, PageLabel};
use typst_library::model::Numbering; use typst_library::model::Numbering;
pub trait PageLabelExt { pub(crate) trait PageLabelExt {
/// Create a new `PageLabel` from a `Numbering` applied to a page /// Create a new `PageLabel` from a `Numbering` applied to a page
/// number. /// number.
fn generate(numbering: &Numbering, number: usize) -> Option<PageLabel>; fn generate(numbering: &Numbering, number: usize) -> Option<PageLabel>;

View File

@ -1,7 +1,5 @@
//! Convert paint types from typst to krilla. //! Convert paint types from typst to krilla.
use crate::krilla::{handle_frame, FrameContext, GlobalContext, State};
use crate::util::{AbsExt, ColorExt, FillRuleExt, LineCapExt, LineJoinExt, TransformExt};
use krilla::geom::NormalizedF32; use krilla::geom::NormalizedF32;
use krilla::paint::SpreadMethod; use krilla::paint::SpreadMethod;
use krilla::surface::Surface; use krilla::surface::Surface;
@ -13,6 +11,9 @@ use typst_library::visualize::{
}; };
use typst_utils::Numeric; use typst_utils::Numeric;
use crate::krilla::{handle_frame, FrameContext, GlobalContext, State};
use crate::util::{AbsExt, ColorExt, FillRuleExt, LineCapExt, LineJoinExt, TransformExt};
pub(crate) fn convert_fill( pub(crate) fn convert_fill(
gc: &mut GlobalContext, gc: &mut GlobalContext,
paint_: &Paint, paint_: &Paint,
@ -53,13 +54,6 @@ pub(crate) fn convert_stroke(
}) })
} }
fn convert_dash(dash: &DashPattern<Abs, Abs>) -> krilla::path::StrokeDash {
krilla::path::StrokeDash {
array: dash.array.iter().map(|e| e.to_f32()).collect(),
offset: dash.phase.to_f32(),
}
}
fn convert_paint( fn convert_paint(
gc: &mut GlobalContext, gc: &mut GlobalContext,
paint: &Paint, paint: &Paint,
@ -104,7 +98,7 @@ fn convert_solid(color: &Color) -> (krilla::paint::Paint, u8) {
255, 255,
) )
} }
// Convert all remaining colors into RGB // Convert all other colors in different colors spaces into RGB
_ => { _ => {
let (c, a) = color.to_krilla_rgb(); let (c, a) = color.to_krilla_rgb();
(c.into(), a) (c.into(), a)
@ -148,11 +142,103 @@ fn convert_gradient(
RelativeTo::Parent => state.container_size(), RelativeTo::Parent => state.container_size(),
}; };
let rotation = gradient.angle().unwrap_or_else(Angle::zero); let angle = gradient.angle().unwrap_or_else(Angle::zero);
let base_transform = correct_transform(state, gradient.unwrap_relative(on_text)); let base_transform = correct_transform(state, gradient.unwrap_relative(on_text));
let stops = convert_gradient_stops(gradient);
match &gradient {
Gradient::Linear(_) => {
let (x1, y1, x2, y2) = {
let (mut sin, mut cos) = (angle.sin(), angle.cos());
let angle = rotation; // Scale to edges of unit square.
let factor = cos.abs() + sin.abs();
sin *= factor;
cos *= factor;
match angle.quadrant() {
Quadrant::First => (0.0, 0.0, cos as f32, sin as f32),
Quadrant::Second => (1.0, 0.0, cos as f32 + 1.0, sin as f32),
Quadrant::Third => (1.0, 1.0, cos as f32 + 1.0, sin as f32 + 1.0),
Quadrant::Fourth => (0.0, 1.0, cos as f32, sin as f32 + 1.0),
}
};
let linear = krilla::paint::LinearGradient {
x1,
y1,
x2,
y2,
// x and y coordinates are normalized, so need to scale by the size.
transform: base_transform
.pre_concat(Transform::scale(
Ratio::new(size.x.to_f32() as f64),
Ratio::new(size.y.to_f32() as f64),
))
.to_krilla(),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(linear.into(), 255)
}
Gradient::Radial(radial) => {
let radial = krilla::paint::RadialGradient {
fx: radial.focal_center.x.get() as f32,
fy: radial.focal_center.y.get() as f32,
fr: radial.focal_radius.get() as f32,
cx: radial.center.x.get() as f32,
cy: radial.center.y.get() as f32,
cr: radial.radius.get() as f32,
transform: base_transform.to_krilla().pre_concat(
krilla::geom::Transform::from_scale(size.x.to_f32(), size.y.to_f32()),
),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(radial.into(), 255)
}
Gradient::Conic(conic) => {
// Correct the gradient's angle
let cx = size.x.to_f32() * conic.center.x.get() as f32;
let cy = size.y.to_f32() * conic.center.y.get() as f32;
let actual_transform = base_transform
// Adjust for the angle
.pre_concat(Transform::rotate_at(
angle,
Abs::pt(cx as f64),
Abs::pt(cy as f64),
))
// Default start point in krilla and typst are at the opposite side, so we need
// to flip it horizontally.
.pre_concat(Transform::scale_at(
-Ratio::one(),
Ratio::one(),
Abs::pt(cx as f64),
Abs::pt(cy as f64),
));
let sweep = krilla::paint::SweepGradient {
cx,
cy,
start_angle: 0.0,
end_angle: 360.0,
transform: actual_transform.to_krilla(),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(sweep.into(), 255)
}
}
}
fn convert_gradient_stops(
gradient: &Gradient,
) -> Vec<krilla::paint::Stop<krilla::color::rgb::Color>> {
let mut stops: Vec<krilla::paint::Stop<krilla::color::rgb::Color>> = vec![]; let mut stops: Vec<krilla::paint::Stop<krilla::color::rgb::Color>> = vec![];
let mut add_single = |color: &Color, offset: Ratio| { let mut add_single = |color: &Color, offset: Ratio| {
@ -246,94 +332,13 @@ fn convert_gradient(
} }
} }
match &gradient { stops
Gradient::Linear(_) => { }
let (x1, y1, x2, y2) = {
let (mut sin, mut cos) = (angle.sin(), angle.cos());
// Scale to edges of unit square. fn convert_dash(dash: &DashPattern<Abs, Abs>) -> krilla::path::StrokeDash {
let factor = cos.abs() + sin.abs(); krilla::path::StrokeDash {
sin *= factor; array: dash.array.iter().map(|e| e.to_f32()).collect(),
cos *= factor; offset: dash.phase.to_f32(),
match angle.quadrant() {
Quadrant::First => (0.0, 0.0, cos as f32, sin as f32),
Quadrant::Second => (1.0, 0.0, cos as f32 + 1.0, sin as f32),
Quadrant::Third => (1.0, 1.0, cos as f32 + 1.0, sin as f32 + 1.0),
Quadrant::Fourth => (0.0, 1.0, cos as f32, sin as f32 + 1.0),
}
};
let linear = krilla::paint::LinearGradient {
x1,
y1,
x2,
y2,
// x and y coordinates are normalized, so need to scale by the size.
transform: base_transform
.pre_concat(Transform::scale(
Ratio::new(size.x.to_f32() as f64),
Ratio::new(size.y.to_f32() as f64),
))
.to_krilla(),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(linear.into(), 255)
}
Gradient::Radial(radial) => {
let radial = krilla::paint::RadialGradient {
fx: radial.focal_center.x.get() as f32,
fy: radial.focal_center.y.get() as f32,
fr: radial.focal_radius.get() as f32,
cx: radial.center.x.get() as f32,
cy: radial.center.y.get() as f32,
cr: radial.radius.get() as f32,
transform: base_transform.to_krilla().pre_concat(
krilla::geom::Transform::from_scale(size.x.to_f32(), size.y.to_f32()),
),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(radial.into(), 255)
}
Gradient::Conic(conic) => {
// Correct the gradient's angle
let cx = size.x.to_f32() * conic.center.x.get() as f32;
let cy = size.y.to_f32() * conic.center.y.get() as f32;
let actual_transform = base_transform
// Adjust for the angle
.pre_concat(Transform::rotate_at(
angle,
Abs::pt(cx as f64),
Abs::pt(cy as f64),
))
// Default start point in krilla and typst are at the opposite side, so we need
// to flip it horizontally.
.pre_concat(Transform::scale_at(
-Ratio::one(),
Ratio::one(),
Abs::pt(cx as f64),
Abs::pt(cy as f64),
));
let sweep = krilla::paint::SweepGradient {
cx,
cy,
start_angle: 0.0,
end_angle: 360.0,
transform: actual_transform.to_krilla(),
spread_method: SpreadMethod::Pad,
stops: stops.into(),
anti_alias: gradient.anti_alias(),
};
(sweep.into(), 255)
}
} }
} }

View File

@ -1,17 +1,19 @@
use crate::krilla::{FrameContext, GlobalContext}; use std::ops::Range;
use crate::paint; use std::sync::Arc;
use crate::util::{display_font, AbsExt, TransformExt};
use bytemuck::TransparentWrapper; use bytemuck::TransparentWrapper;
use krilla::font::{GlyphId, GlyphUnits}; use krilla::font::{GlyphId, GlyphUnits};
use krilla::surface::Surface; use krilla::surface::Surface;
use std::ops::Range;
use std::sync::Arc;
use typst_library::diag::{bail, SourceResult}; use typst_library::diag::{bail, SourceResult};
use typst_library::layout::Size; use typst_library::layout::Size;
use typst_library::text::{Font, Glyph, TextItem}; use typst_library::text::{Font, Glyph, TextItem};
use typst_library::visualize::FillRule; use typst_library::visualize::FillRule;
use typst_syntax::Span; use typst_syntax::Span;
use crate::krilla::{FrameContext, GlobalContext};
use crate::paint;
use crate::util::{display_font, AbsExt, TransformExt};
pub(crate) fn handle_text( pub(crate) fn handle_text(
fc: &mut FrameContext, fc: &mut FrameContext,
t: &TextItem, t: &TextItem,

View File

@ -113,7 +113,7 @@ impl ColorExt for Color {
} }
} }
/// Display the font family and variant. /// Display the font family and variant of a font.
pub(crate) fn display_font(font: &Font) -> String { pub(crate) fn display_font(font: &Font) -> String {
let font_family = &font.info().family; let font_family = &font.info().family;
let font_variant = font.info().variant; let font_variant = font.info().variant;