Repository: typst/typst
File: crates/typst-library/src/visualize/image/mod.rs (Rust)

Code before:
                engine.sink.warn(warning!(
                span,
                "image contains foreign object";
                hint: "SVG images with foreign objects might render incorrectly in typst";
                hint: "see https://github.com/typst/typst/issues/1421 for more information"
            ));
            }
        }

        // Construct the image itself.
        let kind = match format {
            ImageFormat::Raster(format) => ImageKind::Raster(
                RasterImage::new(
                    loaded.data.clone(),
                    format,
                    self.icc.get_ref(styles).as_ref().map(|icc| icc.derived.clone()),
                )
                .at(span)?,
            ),
            ImageFormat::Vector(VectorFormat::Svg) => {
                // We need to decide from which file images are linked,
                // to resolve relative href image paths in the SVG.
                // Try to locate the typst source file, or fall back to the main file.
                let svg_parent_file = match span.id() {
                    Some(file) => file,
                    None => engine.world.main(),
                };
                // If the SVG is loaded from an actual file, account for its location.
                let svg_parent_file = match self.source.source {
                    DataSource::Path(ref path) => svg_parent_file.join(path),
                    DataSource::Bytes(_) => svg_parent_file,
                };
                ImageKind::Svg(
                    SvgImage::with_fonts(
                        svg_parent_file,
                        loaded.data.clone(),
                        engine.world,
                        &families(styles).map(|f| f.as_str()).collect::<Vec<_>>(),
                    )
                    .within(loaded)?,
                )
            }
            ImageFormat::Vector(VectorFormat::Pdf) => {
                let document = match PdfDocument::new(loaded.data.clone()) {
                    Ok(doc) => doc,
                    Err(e) => match e {
                        LoadPdfError::Encryption => {
                            bail!(
                                span,
                                "the PDF is encrypted or password-protected";
                                hint: "such PDFs are currently not supported";

Review comment (suggestion):
This fallback shouldn't happen here. Overall, it would be better if this code deferred to the existing logic here:

https://github.com/typst/typst/blob/77ba73eb1e942e5379ef6392ffba921e1a32baf5/crates/typst-syntax/src/span.rs#L169-L174

(Note that if just passing the span through that makes the caching problem even worse, so we'd need to handle that properly then. But as mentioned above, I can also do that myself.) 

Code after:
                span,
                "image contains foreign object";
                hint: "SVG images with foreign objects might render incorrectly in typst";
                hint: "see https://github.com/typst/typst/issues/1421 for more information"
            ));
            }
        }

        // Construct the image itself.
        let kind = match format {
            ImageFormat::Raster(format) => ImageKind::Raster(
                RasterImage::new(
                    loaded.data.clone(),
                    format,
                    self.icc.get_ref(styles).as_ref().map(|icc| icc.derived.clone()),
                )
                .at(span)?,
            ),
            ImageFormat::Vector(VectorFormat::Svg) => {
                // Identify the SVG file in case contained hrefs need to be resolved.
                let svg_file = match self.source.source {
                    DataSource::Path(ref path) => span.resolve_path(path).ok(),
                    DataSource::Bytes(_) => span.id(),
                };
                ImageKind::Svg(
                    SvgImage::with_fonts_images(
                        loaded.data.clone(),
                        engine.world,
                        &families(styles).map(|f| f.as_str()).collect::<Vec<_>>(),
                        svg_file,
                    )
                    .within(loaded)?,
                )
            }
            ImageFormat::Vector(VectorFormat::Pdf) => {
                let document = match PdfDocument::new(loaded.data.clone()) {
                    Ok(doc) => doc,
                    Err(e) => match e {
                        LoadPdfError::Encryption => {
                            bail!(
                                span,
                                "the PDF is encrypted or password-protected";
                                hint: "such PDFs are currently not supported";
                                hint: "preprocess the PDF to remove the encryption"
                            );
                        }
                        LoadPdfError::Invalid => {
                            bail!(
                                span,
                                "the PDF could not be loaded";
                                hint: "perhaps the PDF file is malformed"