diff --git a/crates/typst-pdf/src/convert.rs b/crates/typst-pdf/src/convert.rs index dd0c18938..c19e53a6e 100644 --- a/crates/typst-pdf/src/convert.rs +++ b/crates/typst-pdf/src/convert.rs @@ -330,6 +330,11 @@ fn finish( ) -> SourceResult> { let validator = configuration.validator(); + let get_span = |loc: Option| { + loc.map(|l| Span::from_raw(NonZeroU64::new(l).unwrap())) + .unwrap_or(Span::detached()) + }; + match document.finish() { Ok(r) => Ok(r), Err(e) => match e { @@ -343,11 +348,6 @@ fn finish( KrillaError::Validation(ve) => { let prefix = format!("{} error:", validator.as_str()); - let get_span = |loc: Option| { - loc.map(|l| Span::from_raw(NonZeroU64::new(l).unwrap())) - .unwrap_or(Span::detached()) - }; - let errors = ve.iter().map(|e| { match e { ValidationError::TooLongString => { @@ -548,9 +548,9 @@ fn finish( Err(errors) } - KrillaError::Image(i) => { - let span = gc.image_to_spans.get(&i).unwrap(); - bail!(*span, "failed to process image"); + KrillaError::Image(_, loc) => { + let span = get_span(loc); + bail!(span, "failed to process image"); } KrillaError::SixteenBitImage(image, _) => { let span = gc.image_to_spans.get(&image).unwrap(); diff --git a/crates/typst-pdf/src/shape.rs b/crates/typst-pdf/src/shape.rs index c3a8ab265..a4b487e17 100644 --- a/crates/typst-pdf/src/shape.rs +++ b/crates/typst-pdf/src/shape.rs @@ -19,8 +19,8 @@ pub(crate) fn handle_shape( surface.push_transform(&fc.state().transform().to_krilla()); if let Some(path) = convert_geometry(&shape.geometry) { - if let Some(paint) = &shape.fill { - let fill = paint::convert_fill( + let fill = if let Some(paint) = &shape.fill { + Some(paint::convert_fill( gc, paint, shape.fill_rule, @@ -28,11 +28,10 @@ pub(crate) fn handle_shape( surface, fc.state(), shape.geometry.bbox_size(), - )?; - - surface.set_fill(fill); - surface.fill_path(&path); - } + )?) + } else { + None + }; let stroke = shape.stroke.as_ref().and_then(|stroke| { if stroke.thickness.to_f32() > 0.0 { @@ -42,7 +41,7 @@ pub(crate) fn handle_shape( } }); - if let Some(stroke) = &stroke { + let stroke = if let Some(stroke) = &stroke { let stroke = paint::convert_stroke( gc, stroke, @@ -52,9 +51,14 @@ pub(crate) fn handle_shape( shape.geometry.bbox_size(), )?; - surface.set_stroke(stroke); - surface.stroke_path(&path); - } + Some(stroke) + } else { + None + }; + + surface.set_fill(fill); + surface.set_stroke(stroke); + surface.draw_path(&path); } surface.pop(); diff --git a/crates/typst-pdf/src/text.rs b/crates/typst-pdf/src/text.rs index 1e2b15fb9..03739926e 100644 --- a/crates/typst-pdf/src/text.rs +++ b/crates/typst-pdf/src/text.rs @@ -32,13 +32,22 @@ pub(crate) fn handle_text( fc.state(), Size::zero(), )?; + let stroke = + if let Some(stroke) = t.stroke.as_ref().map(|s| { + paint::convert_stroke(gc, s, true, surface, fc.state(), Size::zero()) + }) { + Some(stroke?) + } else { + None + }; let text = t.text.as_str(); let size = t.size; let glyphs: &[PdfGlyph] = TransparentWrapper::wrap_slice(t.glyphs.as_slice()); surface.push_transform(&fc.state().transform().to_krilla()); - surface.set_fill(fill); - surface.fill_glyphs( + surface.set_fill(Some(fill)); + surface.set_stroke(stroke); + surface.draw_glyphs( krilla::geom::Point::from_xy(0.0, 0.0), glyphs, font.clone(), @@ -47,25 +56,6 @@ pub(crate) fn handle_text( false, ); - if let Some(stroke) = t - .stroke - .as_ref() - .map(|s| paint::convert_stroke(gc, s, true, surface, fc.state(), Size::zero())) - { - let stroke = stroke?; - - surface.set_stroke(stroke); - surface.stroke_glyphs( - krilla::geom::Point::from_xy(0.0, 0.0), - glyphs, - font, - text, - size.to_f32(), - // To prevent text from being embedded twice, we outline it instead if a stroke exists. - true, - ); - } - surface.pop(); Ok(())