summaryrefslogtreecommitdiff
path: root/crates/typst-pdf/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-06-06 17:30:49 +0200
committerGitHub <noreply@github.com>2024-06-06 15:30:49 +0000
commit8f7ba8d4958184366180efeb00ecfe8835c69b11 (patch)
tree689d9d521e029fe13e962a177942e361af1baaef /crates/typst-pdf/src
parent681badf76a029e421415986aa3a46c0081d28470 (diff)
Bump SVG & PDF cinematic universe (#4316)
Diffstat (limited to 'crates/typst-pdf/src')
-rw-r--r--crates/typst-pdf/src/image.rs32
-rw-r--r--crates/typst-pdf/src/page.rs49
2 files changed, 35 insertions, 46 deletions
diff --git a/crates/typst-pdf/src/image.rs b/crates/typst-pdf/src/image.rs
index 9da7158c..9951dac5 100644
--- a/crates/typst-pdf/src/image.rs
+++ b/crates/typst-pdf/src/image.rs
@@ -90,12 +90,12 @@ pub fn write_images(context: &WithGlobalRefs) -> (PdfChunk, HashMap<Image, Ref>)
}
}
}
- EncodedImage::Svg(svg_chunk) => {
+ EncodedImage::Svg(svg_chunk, id) => {
let mut map = HashMap::new();
svg_chunk.renumber_into(&mut chunk.chunk, |old| {
*map.entry(old).or_insert_with(|| chunk.alloc.bump())
});
- out.insert(image.clone(), map[&Ref::new(1)]);
+ out.insert(image.clone(), map[&id]);
}
}
}
@@ -132,7 +132,10 @@ pub fn deferred_image(image: Image) -> (Deferred<EncodedImage>, Option<ColorSpac
EncodedImage::Raster { data, filter, has_color, width, height, icc, alpha }
}
- ImageKind::Svg(svg) => EncodedImage::Svg(encode_svg(svg)),
+ ImageKind::Svg(svg) => {
+ let (chunk, id) = encode_svg(svg);
+ EncodedImage::Svg(chunk, id)
+ }
});
(deferred, color_space)
@@ -176,25 +179,8 @@ fn encode_alpha(raster: &RasterImage) -> (Vec<u8>, Filter) {
}
/// Encode an SVG into a chunk of PDF objects.
-///
-/// The main XObject will have ID 1.
-fn encode_svg(svg: &SvgImage) -> Chunk {
- let mut chunk = Chunk::new();
-
- // Safety: We do not keep any references to tree nodes beyond the
- // scope of `with`.
- unsafe {
- svg.with(|tree| {
- svg2pdf::convert_tree_into(
- tree,
- svg2pdf::Options::default(),
- &mut chunk,
- Ref::new(1),
- );
- });
- }
-
- chunk
+fn encode_svg(svg: &SvgImage) -> (Chunk, Ref) {
+ svg2pdf::to_chunk(svg.tree(), svg2pdf::ConversionOptions::default())
}
/// A pre-encoded image.
@@ -219,5 +205,5 @@ pub enum EncodedImage {
/// A vector graphic.
///
/// The chunk is the SVG converted to PDF objects.
- Svg(Chunk),
+ Svg(Chunk, Ref),
}
diff --git a/crates/typst-pdf/src/page.rs b/crates/typst-pdf/src/page.rs
index f796d0c8..c6881eb6 100644
--- a/crates/typst-pdf/src/page.rs
+++ b/crates/typst-pdf/src/page.rs
@@ -115,29 +115,12 @@ fn write_page(
return;
};
- let global_resources_ref = ctx.resources.reference;
- let mut page_writer = chunk.page(page_ref);
- page_writer.parent(page_tree_ref);
-
- let w = page.content.size.x.to_f32();
- let h = page.content.size.y.to_f32();
- page_writer.media_box(Rect::new(0.0, 0.0, w, h));
- page_writer.contents(content_id);
- page_writer.pair(Name(b"Resources"), global_resources_ref);
-
- if page.content.uses_opacities {
- page_writer
- .group()
- .transparency()
- .isolated(false)
- .knockout(false)
- .color_space()
- .srgb();
- }
-
- let mut annotations = page_writer.annotations();
+ let mut annotations = Vec::with_capacity(page.content.links.len());
for (dest, rect) in &page.content.links {
- let mut annotation = annotations.push();
+ let id = chunk.alloc();
+ annotations.push(id);
+
+ let mut annotation = chunk.annotation(id);
annotation.subtype(AnnotationType::Link).rect(*rect);
annotation.border(0.0, 0.0, 0.0, None).flags(AnnotationFlags::PRINT);
@@ -180,7 +163,27 @@ fn write_page(
}
}
- annotations.finish();
+ let mut page_writer = chunk.page(page_ref);
+ page_writer.parent(page_tree_ref);
+
+ let w = page.content.size.x.to_f32();
+ let h = page.content.size.y.to_f32();
+ page_writer.media_box(Rect::new(0.0, 0.0, w, h));
+ page_writer.contents(content_id);
+ page_writer.pair(Name(b"Resources"), ctx.resources.reference);
+
+ if page.content.uses_opacities {
+ page_writer
+ .group()
+ .transparency()
+ .isolated(false)
+ .knockout(false)
+ .color_space()
+ .srgb();
+ }
+
+ page_writer.annotations(annotations);
+
page_writer.finish();
chunk