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",
]
[[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]]
name = "fontdb"
version = "0.22.0"
@ -1333,23 +1319,23 @@ dependencies = [
"comemo",
"flate2",
"float-cmp 0.10.0",
"fontdb 0.22.0",
"fontdb",
"gif",
"image-webp",
"imagesize",
"miniz_oxide",
"once_cell",
"pdf-writer 0.12.0 (git+https://github.com/LaurenzV/pdf-writer?rev=f95a19c)",
"pdf-writer",
"rayon",
"resvg 0.44.0",
"resvg",
"rustybuzz",
"siphasher 1.0.1",
"skrifa",
"subsetter 0.2.0 (git+https://github.com/typst/subsetter?rev=172416a)",
"subsetter",
"tiny-skia",
"tiny-skia-path",
"usvg 0.44.0",
"xmp-writer 0.3.0 (git+https://github.com/LaurenzV/xmp-writer?rev=1c2b8ae9)",
"usvg",
"xmp-writer",
"yoke",
"zune-jpeg",
"zune-png",
@ -1781,18 +1767,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "pdf-writer"
version = "0.12.0"
@ -2112,23 +2086,6 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "resvg"
version = "0.44.0"
@ -2142,7 +2099,7 @@ dependencies = [
"rgb",
"svgtypes",
"tiny-skia",
"usvg 0.44.0",
"usvg",
"zune-jpeg",
]
@ -2511,37 +2468,11 @@ dependencies = [
"syn",
]
[[package]]
name = "subsetter"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74f98178f34057d4d4de93d68104007c6dea4dfac930204a69ab4622daefa648"
[[package]]
name = "subsetter"
version = "0.2.0"
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]]
name = "svgtypes"
version = "0.15.2"
@ -2988,7 +2919,7 @@ dependencies = [
"ecow",
"env_proxy",
"flate2",
"fontdb 0.22.0",
"fontdb",
"native-tls",
"once_cell",
"openssl",
@ -3045,7 +2976,7 @@ dependencies = [
"csv",
"ecow",
"flate2",
"fontdb 0.22.0",
"fontdb",
"hayagriva",
"icu_properties",
"icu_provider",
@ -3084,7 +3015,7 @@ dependencies = [
"unicode-math-class",
"unicode-segmentation",
"unscanny",
"usvg 0.44.0",
"usvg",
"wasmi",
"xmlwriter",
]
@ -3116,32 +3047,6 @@ dependencies = [
"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]]
name = "typst-realize"
version = "0.12.0"
@ -3166,7 +3071,7 @@ dependencies = [
"comemo",
"image",
"pixglyph",
"resvg 0.44.0",
"resvg",
"tiny-skia",
"ttf-parser",
"typst-library",
@ -3388,33 +3293,6 @@ dependencies = [
"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]]
name = "usvg"
version = "0.44.0"
@ -3424,7 +3302,7 @@ dependencies = [
"base64",
"data-url",
"flate2",
"fontdb 0.22.0",
"fontdb",
"imagesize",
"kurbo",
"log",
@ -3809,12 +3687,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"
[[package]]
name = "xmp-writer"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8254499146a4fd0c86e3e99cf4a9f468f595808fb49ff8f3e495f2b117bf4ebc"
[[package]]
name = "xmp-writer"
version = "0.3.0"

View File

@ -31,6 +31,7 @@ use typst_syntax::Span;
/// which is mainly needed to resolve gradients and patterns correctly.
#[derive(Debug, Clone)]
pub(crate) struct State {
/// The current transform.
transform: Transform,
/// The transform of first hard frame in the hierarchy.
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::annotation::{LinkAnnotation, Target};
use krilla::destination::XyzDestination;
@ -7,7 +5,9 @@ use krilla::geom::Rect;
use typst_library::layout::{Abs, Point, Size};
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(
fc: &mut FrameContext,
gc: &mut GlobalContext,
@ -55,14 +55,14 @@ pub(crate) fn handle_link(
}
Destination::Position(p) => *p,
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
// not point to an excluded page.
fc.annotations.push(
LinkAnnotation::new(
rect,
Target::Destination(krilla::destination::Destination::Named(
named_dest.clone(),
nd.clone(),
)),
)
.into(),

View File

@ -56,39 +56,35 @@ fn convert_date(
) -> Option<krilla::metadata::DateTime> {
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() {
krilla_date = krilla_date.month(month);
kd = kd.month(month);
}
if let Some(day) = datetime.day() {
krilla_date = krilla_date.day(day);
kd = kd.day(day);
}
if let Some(h) = datetime.hour() {
krilla_date = krilla_date.hour(h);
kd = kd.hour(h);
}
if let Some(m) = datetime.minute() {
krilla_date = krilla_date.minute(m);
kd = kd.minute(m);
}
if let Some(s) = datetime.second() {
krilla_date = krilla_date.second(s);
kd = kd.second(s);
}
match tz {
Some(Timezone::UTC) => {
krilla_date = krilla_date.utc_offset_hour(0).utc_offset_minute(0)
}
Some(Timezone::UTC) => kd = kd.utc_offset_hour(0).utc_offset_minute(0),
Some(Timezone::Local { hour_offset, minute_offset }) => {
krilla_date = krilla_date
.utc_offset_hour(hour_offset)
.utc_offset_minute(minute_offset)
kd = kd.utc_offset_hour(hour_offset).utc_offset_minute(minute_offset)
}
None => {}
}
Some(krilla_date)
Some(kd)
}

View File

@ -9,52 +9,6 @@ use typst_library::model::HeadingElem;
use crate::krilla::GlobalContext;
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 {
let mut tree: Vec<HeadingNode> = vec![];
@ -147,6 +101,52 @@ pub(crate) fn build_outline(gc: &GlobalContext) -> 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> {
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 typst_library::model::Numbering;
pub trait PageLabelExt {
pub(crate) trait PageLabelExt {
/// Create a new `PageLabel` from a `Numbering` applied to a page
/// number.
fn generate(numbering: &Numbering, number: usize) -> Option<PageLabel>;

View File

@ -1,7 +1,5 @@
//! 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::paint::SpreadMethod;
use krilla::surface::Surface;
@ -13,6 +11,9 @@ use typst_library::visualize::{
};
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(
gc: &mut GlobalContext,
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(
gc: &mut GlobalContext,
paint: &Paint,
@ -104,7 +98,7 @@ fn convert_solid(color: &Color) -> (krilla::paint::Paint, u8) {
255,
)
}
// Convert all remaining colors into RGB
// Convert all other colors in different colors spaces into RGB
_ => {
let (c, a) = color.to_krilla_rgb();
(c.into(), a)
@ -148,11 +142,103 @@ fn convert_gradient(
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 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 add_single = |color: &Color, offset: Ratio| {
@ -246,94 +332,13 @@ fn convert_gradient(
}
}
match &gradient {
Gradient::Linear(_) => {
let (x1, y1, x2, y2) = {
let (mut sin, mut cos) = (angle.sin(), angle.cos());
// 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),
stops
}
};
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_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(),
}
}

View File

@ -1,17 +1,19 @@
use crate::krilla::{FrameContext, GlobalContext};
use crate::paint;
use crate::util::{display_font, AbsExt, TransformExt};
use std::ops::Range;
use std::sync::Arc;
use bytemuck::TransparentWrapper;
use krilla::font::{GlyphId, GlyphUnits};
use krilla::surface::Surface;
use std::ops::Range;
use std::sync::Arc;
use typst_library::diag::{bail, SourceResult};
use typst_library::layout::Size;
use typst_library::text::{Font, Glyph, TextItem};
use typst_library::visualize::FillRule;
use typst_syntax::Span;
use crate::krilla::{FrameContext, GlobalContext};
use crate::paint;
use crate::util::{display_font, AbsExt, TransformExt};
pub(crate) fn handle_text(
fc: &mut FrameContext,
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 {
let font_family = &font.info().family;
let font_variant = font.info().variant;