diff --git a/crates/typst-pdf/src/convert.rs b/crates/typst-pdf/src/convert.rs index 80d7aa2b7..7ce903ca4 100644 --- a/crates/typst-pdf/src/convert.rs +++ b/crates/typst-pdf/src/convert.rs @@ -2,14 +2,14 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use std::num::NonZeroU64; use ecow::EcoVec; -use krilla::annotation::Annotation; -use krilla::destination::{NamedDestination, XyzDestination}; -use krilla::embed::EmbedError; use krilla::error::KrillaError; use krilla::page::PageLabel; use krilla::path::PathBuilder; use krilla::surface::Surface; use krilla::{Configuration, Document, PageSettings, SerializeSettings, ValidationError}; +use krilla::interactive::annotation::Annotation; +use krilla::interactive::destination::{NamedDestination, XyzDestination}; +use krilla::interchange::embed::EmbedError; use krilla_svg::render_svg_glyph; use typst_library::diag::{bail, error, SourceResult}; use typst_library::foundations::NativeElement; @@ -208,13 +208,13 @@ impl FrameContext { /// Globally needed context for converting a typst document. pub(crate) struct GlobalContext<'a> { /// Cache the conversion between krilla and Typst fonts (forward and backward). - pub(crate) fonts_forward: HashMap, - pub(crate) fonts_backward: HashMap, + pub(crate) fonts_forward: HashMap, + pub(crate) fonts_backward: HashMap, /// Mapping between images and their span. // Note: In theory, the same image can have multiple spans // if it appears in the document multiple times. We just store the // first appearance, though. - pub(crate) image_to_spans: HashMap, + pub(crate) image_to_spans: HashMap, pub(crate) image_spans: HashSet, pub(crate) document: &'a PagedDocument, pub(crate) options: &'a PdfOptions<'a>, @@ -311,7 +311,7 @@ pub(crate) fn handle_group( .and_then(|p| p.transform(fc.state().transform.to_krilla())); if let Some(clip_path) = &clip_path { - surface.push_clip_path(clip_path, &krilla::path::FillRule::NonZero); + surface.push_clip_path(clip_path, &krilla::graphics::paint::FillRule::NonZero); } handle_frame(fc, &group.frame, None, surface, context)?; @@ -336,18 +336,14 @@ fn finish(document: Document, gc: GlobalContext) -> SourceResult> { match document.finish() { Ok(r) => Ok(r), Err(e) => match e { - KrillaError::FontError(f, s) => { + KrillaError::Font(f, s) => { let font_str = display_font(gc.fonts_backward.get(&f).unwrap()); bail!(Span::detached(), "failed to process font {font_str} ({s})"; hint: "make sure the font is valid"; hint: "this could also be a bug in the Typst compiler" ); } - KrillaError::UserError(u) => { - // This is an error which indicates misuse on the typst-pdf side. - bail!(Span::detached(), "internal error ({u})"; hint: "please report this as a bug") - } - KrillaError::ValidationError(ve) => { + KrillaError::Validation(ve) => { // We can only produce 1 error, so just take the first one. let prefix = format!("validated export with {} failed:", validator.as_str()); @@ -503,7 +499,7 @@ fn finish(document: Document, gc: GlobalContext) -> SourceResult> { Err(errors) } - KrillaError::ImageError(i) => { + KrillaError::Image(i) => { let span = gc.image_to_spans.get(&i).unwrap(); bail!(*span, "failed to process image"); } diff --git a/crates/typst-pdf/src/embed.rs b/crates/typst-pdf/src/embed.rs index 0d501184a..307ab5255 100644 --- a/crates/typst-pdf/src/embed.rs +++ b/crates/typst-pdf/src/embed.rs @@ -1,7 +1,7 @@ use std::sync::Arc; -use krilla::embed::{AssociationKind, EmbeddedFile}; use krilla::Document; +use krilla::interchange::embed::{AssociationKind, EmbeddedFile}; use typst_library::diag::{bail, SourceResult}; use typst_library::foundations::{NativeElement, StyleChain}; use typst_library::layout::PagedDocument; diff --git a/crates/typst-pdf/src/image.rs b/crates/typst-pdf/src/image.rs index 1648c471b..98b8682b8 100644 --- a/crates/typst-pdf/src/image.rs +++ b/crates/typst-pdf/src/image.rs @@ -2,7 +2,7 @@ use std::hash::{Hash, Hasher}; use std::sync::{Arc, OnceLock}; use image::{DynamicImage, EncodableLayout, GenericImageView, Rgba}; -use krilla::image::{BitsPerComponent, CustomImage, ImageColorspace}; +use krilla::graphics::image::{BitsPerComponent, CustomImage, ImageColorspace}; use krilla::surface::Surface; use krilla_svg::{SurfaceExt, SvgSettings}; use typst_library::diag::{bail, SourceResult}; @@ -164,23 +164,23 @@ impl CustomImage for PdfImage { fn convert_raster( raster: RasterImage, interpolate: bool, -) -> Option { +) -> Option { match raster.format() { RasterFormat::Exchange(e) => match e { ExchangeFormat::Jpg => { if !raster.is_rotated() { let image_data: Arc + Send + Sync> = Arc::new(raster.data().clone()); - krilla::image::Image::from_jpeg(image_data.into(), interpolate) + krilla::graphics::image::Image::from_jpeg(image_data.into(), interpolate) } else { // Can't embed original JPEG data if it had to be rotated. - krilla::image::Image::from_custom(PdfImage::new(raster), interpolate) + krilla::graphics::image::Image::from_custom(PdfImage::new(raster), interpolate) } } - _ => krilla::image::Image::from_custom(PdfImage::new(raster), interpolate), + _ => krilla::graphics::image::Image::from_custom(PdfImage::new(raster), interpolate), }, RasterFormat::Pixel(_) => { - krilla::image::Image::from_custom(PdfImage::new(raster), interpolate) + krilla::graphics::image::Image::from_custom(PdfImage::new(raster), interpolate) } } } diff --git a/crates/typst-pdf/src/link.rs b/crates/typst-pdf/src/link.rs index d59cfe227..156f7351b 100644 --- a/crates/typst-pdf/src/link.rs +++ b/crates/typst-pdf/src/link.rs @@ -1,7 +1,7 @@ -use krilla::action::{Action, LinkAction}; -use krilla::annotation::{LinkAnnotation, Target}; -use krilla::destination::XyzDestination; use krilla::geom::Rect; +use krilla::interactive::action::{Action, LinkAction}; +use krilla::interactive::annotation::{LinkAnnotation, Target}; +use krilla::interactive::destination::XyzDestination; use typst_library::layout::{Abs, Point, Size}; use typst_library::model::Destination; @@ -65,7 +65,7 @@ pub(crate) fn handle_link( LinkAnnotation::new( rect, None, - Target::Destination(krilla::destination::Destination::Named( + Target::Destination(krilla::interactive::destination::Destination::Named( nd.clone(), )), ) @@ -84,7 +84,7 @@ pub(crate) fn handle_link( LinkAnnotation::new( rect, None, - Target::Destination(krilla::destination::Destination::Xyz( + Target::Destination(krilla::interactive::destination::Destination::Xyz( XyzDestination::new(page_index, pos.point.to_krilla()), )), ) diff --git a/crates/typst-pdf/src/metadata.rs b/crates/typst-pdf/src/metadata.rs index ebdae1ccf..eea59dffe 100644 --- a/crates/typst-pdf/src/metadata.rs +++ b/crates/typst-pdf/src/metadata.rs @@ -1,5 +1,5 @@ use ecow::EcoString; -use krilla::metadata::Metadata; +use krilla::interchange::metadata::Metadata; use typst_library::foundations::{Datetime, Smart}; use crate::convert::GlobalContext; @@ -53,10 +53,10 @@ pub(crate) fn build_metadata(gc: &GlobalContext) -> Metadata { fn convert_date( datetime: Datetime, tz: Option, -) -> Option { +) -> Option { let year = datetime.year().filter(|&y| y >= 0)? as u16; - let mut kd = krilla::metadata::DateTime::new(year); + let mut kd = krilla::interchange::metadata::DateTime::new(year); if let Some(month) = datetime.month() { kd = kd.month(month); diff --git a/crates/typst-pdf/src/outline.rs b/crates/typst-pdf/src/outline.rs index a656db007..92364994a 100644 --- a/crates/typst-pdf/src/outline.rs +++ b/crates/typst-pdf/src/outline.rs @@ -1,7 +1,6 @@ use std::num::NonZeroUsize; - -use krilla::destination::XyzDestination; -use krilla::outline::{Outline, OutlineNode}; +use krilla::interactive::destination::XyzDestination; +use krilla::interchange::outline::{Outline, OutlineNode}; use typst_library::foundations::{NativeElement, Packed, StyleChain}; use typst_library::layout::Abs; use typst_library::model::HeadingElem; diff --git a/crates/typst-pdf/src/paint.rs b/crates/typst-pdf/src/paint.rs index 4d13d8904..d40860b7e 100644 --- a/crates/typst-pdf/src/paint.rs +++ b/crates/typst-pdf/src/paint.rs @@ -1,7 +1,8 @@ //! Convert paint types from typst to krilla. use krilla::geom::NormalizedF32; -use krilla::paint::SpreadMethod; +use krilla::graphics::color::{cmyk, luma, rgb}; +use krilla::graphics::paint::{Fill, LinearGradient, Pattern, RadialGradient, SpreadMethod, Stop, Stroke, StrokeDash, SweepGradient}; use krilla::surface::Surface; use typst_library::diag::SourceResult; use typst_library::layout::{Abs, Angle, Quadrant, Ratio, Size, Transform}; @@ -22,10 +23,10 @@ pub(crate) fn convert_fill( surface: &mut Surface, state: &State, size: Size, -) -> SourceResult { +) -> SourceResult { let (paint, opacity) = convert_paint(gc, paint_, on_text, surface, state, size)?; - Ok(krilla::path::Fill { + Ok(Fill { paint, rule: fill_rule_.to_krilla(), opacity: NormalizedF32::new(opacity as f32 / 255.0).unwrap(), @@ -39,11 +40,11 @@ pub(crate) fn convert_stroke( surface: &mut Surface, state: &State, size: Size, -) -> SourceResult { +) -> SourceResult { let (paint, opacity) = convert_paint(fc, &stroke.paint, on_text, surface, state, size)?; - Ok(krilla::path::Stroke { + Ok(Stroke { paint, width: stroke.thickness.to_f32(), miter_limit: stroke.miter_limit.get() as f32, @@ -61,7 +62,7 @@ fn convert_paint( surface: &mut Surface, state: &State, mut size: Size, -) -> SourceResult<(krilla::paint::Paint, u8)> { +) -> SourceResult<(krilla::graphics::paint::Paint, u8)> { // Edge cases for strokes. if size.x.is_zero() { size.x = Abs::pt(1.0); @@ -78,16 +79,16 @@ fn convert_paint( } } -fn convert_solid(color: &Color) -> (krilla::paint::Paint, u8) { +fn convert_solid(color: &Color) -> (krilla::graphics::paint::Paint, u8) { match color.space() { ColorSpace::D65Gray => { let components = color.to_vec4_u8(); - (krilla::color::luma::Color::new(components[0]).into(), components[3]) + (luma::Color::new(components[0]).into(), components[3]) } ColorSpace::Cmyk => { let components = color.to_vec4_u8(); ( - krilla::color::cmyk::Color::new( + cmyk::Color::new( components[0], components[1], components[2], @@ -112,7 +113,7 @@ fn convert_pattern( on_text: bool, surface: &mut Surface, state: &State, -) -> SourceResult<(krilla::paint::Paint, u8)> { +) -> SourceResult<(krilla::graphics::paint::Paint, u8)> { let transform = correct_transform(state, pattern.unwrap_relative(on_text)); let mut stream_builder = surface.stream_builder(); @@ -121,7 +122,7 @@ fn convert_pattern( handle_frame(&mut fc, pattern.frame(), None, &mut surface, gc)?; surface.finish(); let stream = stream_builder.finish(); - let pattern = krilla::paint::Pattern { + let pattern = Pattern { stream, transform: transform.to_krilla(), width: (pattern.size().x + pattern.spacing().x).to_pt() as _, @@ -136,7 +137,7 @@ fn convert_gradient( on_text: bool, state: &State, size: Size, -) -> (krilla::paint::Paint, u8) { +) -> (krilla::graphics::paint::Paint, u8) { let size = match gradient.unwrap_relative(on_text) { RelativeTo::Self_ => size, RelativeTo::Parent => state.container_size(), @@ -163,7 +164,7 @@ fn convert_gradient( } }; - let linear = krilla::paint::LinearGradient { + let linear = LinearGradient { x1, y1, x2, @@ -183,7 +184,7 @@ fn convert_gradient( (linear.into(), 255) } Gradient::Radial(radial) => { - let radial = krilla::paint::RadialGradient { + let radial = RadialGradient { fx: radial.focal_center.x.get() as f32, fy: radial.focal_center.y.get() as f32, fr: radial.focal_radius.get() as f32, @@ -223,7 +224,7 @@ fn convert_gradient( Abs::pt(cy as f64), )); - let sweep = krilla::paint::SweepGradient { + let sweep = SweepGradient { cx, cy, start_angle: 0.0, @@ -241,14 +242,14 @@ fn convert_gradient( fn convert_gradient_stops( gradient: &Gradient, -) -> Vec> { - let mut stops: Vec> = vec![]; +) -> Vec> { + let mut stops: Vec> = vec![]; let mut add_single = |color: &Color, offset: Ratio| { let (color, opacity) = color.to_krilla_rgb(); let opacity = NormalizedF32::new((opacity as f32) / 255.0).unwrap(); let offset = NormalizedF32::new(offset.get() as f32).unwrap(); - let stop = krilla::paint::Stop { offset, color, opacity }; + let stop = Stop { offset, color, opacity }; stops.push(stop); }; @@ -338,8 +339,8 @@ fn convert_gradient_stops( stops } -fn convert_dash(dash: &DashPattern) -> krilla::path::StrokeDash { - krilla::path::StrokeDash { +fn convert_dash(dash: &DashPattern) -> StrokeDash { + StrokeDash { array: dash.array.iter().map(|e| e.to_f32()).collect(), offset: dash.phase.to_f32(), } diff --git a/crates/typst-pdf/src/text.rs b/crates/typst-pdf/src/text.rs index ad33df3e2..8f0a904ef 100644 --- a/crates/typst-pdf/src/text.rs +++ b/crates/typst-pdf/src/text.rs @@ -2,7 +2,7 @@ use std::ops::Range; use std::sync::Arc; use bytemuck::TransparentWrapper; -use krilla::font::GlyphId; +use krilla::text::GlyphId; use krilla::surface::{Location, Surface}; use typst_library::diag::{bail, SourceResult}; use typst_library::layout::{Abs, Size}; @@ -74,14 +74,14 @@ pub(crate) fn handle_text( fn convert_font( gc: &mut GlobalContext, typst_font: Font, -) -> SourceResult { +) -> SourceResult { if let Some(font) = gc.fonts_forward.get(&typst_font) { Ok(font.clone()) } else { let font_data: Arc + Send + Sync> = Arc::new(typst_font.data().clone()); let font = - match krilla::font::Font::new(font_data.into(), typst_font.index(), true) { + match krilla::text::Font::new(font_data.into(), typst_font.index(), true) { None => { let font_str = display_font(&typst_font); bail!(Span::detached(), "failed to process font {font_str}"); @@ -100,7 +100,7 @@ fn convert_font( #[repr(transparent)] struct PdfGlyph(Glyph); -impl krilla::font::Glyph for PdfGlyph { +impl krilla::text::Glyph for PdfGlyph { fn glyph_id(&self) -> GlyphId { GlyphId::new(self.0.id as u32) } diff --git a/crates/typst-pdf/src/util.rs b/crates/typst-pdf/src/util.rs index 4833829ef..4bc96d2c6 100644 --- a/crates/typst-pdf/src/util.rs +++ b/crates/typst-pdf/src/util.rs @@ -1,8 +1,8 @@ //! Basic utilities for converting typst types to krilla. -use krilla::color::rgb as kr; +use krilla::graphics::color::rgb as kr; use krilla::geom as kg; -use krilla::path as kp; +use krilla::graphics::paint as kp; use krilla::path::PathBuilder; use typst_library::layout::{Abs, Point, Size, Transform}; use typst_library::text::Font;