Export images in PDF 🖼

This commit is contained in:
Laurenz 2020-11-27 20:01:54 +01:00
parent b4f809f1ea
commit bc997b7c33
6 changed files with 246 additions and 184 deletions

View File

@ -11,7 +11,7 @@ use typst::layout::layout;
use typst::parse::parse; use typst::parse::parse;
use typst::typeset; use typst::typeset;
const FONT_DIR: &str = "fonts"; const FONT_DIR: &str = "../fonts";
const COMA: &str = include_str!("../../tests/typ/coma.typ"); const COMA: &str = include_str!("../../tests/typ/coma.typ");
fn benchmarks(c: &mut Criterion) { fn benchmarks(c: &mut Criterion) {

View File

@ -3,8 +3,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use fontdock::FaceId; use fontdock::FaceId;
use image::RgbImage;
use pdf_writer::{ use pdf_writer::{
CIDFontType, FontFlags, Name, PdfWriter, Rect, Ref, Str, SystemInfo, TextStream, CidFontType, ColorSpace, Content, FontFlags, Name, PdfWriter, Rect, Ref, Str,
SystemInfo, UnicodeCmap,
}; };
use ttf_parser::{name_id, GlyphId}; use ttf_parser::{name_id, GlyphId};
@ -31,136 +33,167 @@ struct PdfExporter<'a> {
/// which objects up-front to correctly declare the document catalogue, page /// which objects up-front to correctly declare the document catalogue, page
/// tree and so on. These offsets are computed in the beginning and stored /// tree and so on. These offsets are computed in the beginning and stored
/// here. /// here.
offsets: Offsets, refs: Refs,
// Font remapping, for more information see `remap_fonts`. /// We assign a new PDF-internal index to each used face.
to_pdf: HashMap<FaceId, usize>, /// There are two mappings:
to_layout: Vec<FaceId>, /// Forwards from the old face ids to the new pdf indices.
fonts_to_pdf: HashMap<FaceId, usize>,
/// Backwards from the pdf indices to the old face ids.
fonts_to_layout: Vec<FaceId>,
/// The already visited images.
images: Vec<&'a RgbImage>,
/// The total number of images.
image_count: usize,
} }
struct Offsets {
catalog: Ref,
page_tree: Ref,
pages: (i32, i32),
contents: (i32, i32),
fonts: (i32, i32),
}
const NUM_OBJECTS_PER_FONT: i32 = 5;
impl<'a> PdfExporter<'a> { impl<'a> PdfExporter<'a> {
fn new(layouts: &'a [BoxLayout], loader: &'a FontLoader) -> Self { fn new(layouts: &'a [BoxLayout], loader: &'a FontLoader) -> Self {
let (to_pdf, to_fontdock) = remap_fonts(layouts);
let offsets = calculate_offsets(layouts.len(), to_pdf.len());
let mut writer = PdfWriter::new(1, 7); let mut writer = PdfWriter::new(1, 7);
writer.set_indent(2); writer.set_indent(2);
let mut fonts_to_pdf = HashMap::new();
let mut fonts_to_layout = vec![];
let mut image_count = 0;
for layout in layouts {
for (_, element) in &layout.elements {
match element {
LayoutElement::Text(shaped) => {
fonts_to_pdf.entry(shaped.face).or_insert_with(|| {
let next_id = fonts_to_layout.len();
fonts_to_layout.push(shaped.face);
next_id
});
}
LayoutElement::Image(_) => image_count += 1,
}
}
}
let refs = Refs::new(layouts.len(), fonts_to_pdf.len(), image_count);
Self { Self {
writer, writer,
layouts, layouts,
offsets,
to_pdf,
to_layout: to_fontdock,
loader, loader,
refs,
fonts_to_pdf,
fonts_to_layout,
images: vec![],
image_count,
} }
} }
fn write(mut self) -> Vec<u8> { fn write(mut self) -> Vec<u8> {
self.write_preface(); self.write_structure();
self.write_pages(); self.write_pages();
self.write_fonts(); self.write_fonts();
self.writer.end(self.offsets.catalog) self.write_images();
self.writer.finish(self.refs.catalog)
} }
fn write_preface(&mut self) { fn write_structure(&mut self) {
// The document catalog. // The document catalog.
self.writer self.writer.catalog(self.refs.catalog).pages(self.refs.page_tree);
.catalog(self.offsets.catalog)
.pages(self.offsets.page_tree);
// The root page tree. // The root page tree.
{ let mut pages = self.writer.pages(self.refs.page_tree);
let mut pages = self.writer.pages(self.offsets.page_tree); pages.kids(self.refs.pages());
pages.kids(ids(self.offsets.pages));
let mut resources = pages.resources(); let mut resources = pages.resources();
let mut fonts = resources.fonts(); let mut fonts = resources.fonts();
for i in 0 .. self.to_pdf.len() { for (refs, f) in self.refs.fonts().zip(0 .. self.fonts_to_pdf.len()) {
let mut buf = itoa::Buffer::new(); let name = format!("F{}", f);
fonts.pair( fonts.pair(Name(name.as_bytes()), refs.type0_font);
Name(buf.format(1 + i as i32).as_bytes()),
Ref::new(self.offsets.fonts.0 + NUM_OBJECTS_PER_FONT * i as i32),
);
}
} }
drop(fonts);
let mut images = resources.x_objects();
for (id, im) in self.refs.images().zip(0 .. self.image_count) {
let name = format!("Im{}", im);
images.pair(Name(name.as_bytes()), id);
}
drop(images);
drop(resources);
drop(pages);
// The page objects (non-root nodes in the page tree). // The page objects (non-root nodes in the page tree).
for ((page_id, content_id), page) in ids(self.offsets.pages) for ((page_id, content_id), page) in
.zip(ids(self.offsets.contents)) self.refs.pages().zip(self.refs.contents()).zip(self.layouts)
.zip(self.layouts)
{ {
let rect = Rect::new( self.writer
.page(page_id)
.parent(self.refs.page_tree)
.media_box(Rect::new(
0.0, 0.0,
0.0, 0.0,
page.size.width.to_pt() as f32, page.size.width.to_pt() as f32,
page.size.height.to_pt() as f32, page.size.height.to_pt() as f32,
); ))
self.writer
.page(page_id)
.parent(self.offsets.page_tree)
.media_box(rect)
.contents(content_id); .contents(content_id);
} }
} }
fn write_pages(&mut self) { fn write_pages(&mut self) {
for (id, page) in ids(self.offsets.contents).zip(self.layouts) { for (id, page) in self.refs.contents().zip(self.layouts) {
self.write_page(id, &page); self.write_page(id, &page);
} }
} }
fn write_page(&mut self, id: Ref, page: &BoxLayout) { fn write_page(&mut self, id: Ref, page: &'a BoxLayout) {
let mut text = TextStream::new(); let mut content = Content::new();
// Font switching actions are only written when the face used for // We only write font switching actions when the used face changes. To
// shaped text changes. Hence, we need to remember the active face. // do that, we need to remember the active face.
let mut face = FaceId::MAX; let mut face = FaceId::MAX;
let mut size = Length::ZERO; let mut size = Length::ZERO;
let mut text = content.text();
for (pos, element) in &page.elements { for (pos, element) in &page.elements {
match element { if let LayoutElement::Text(shaped) = element {
LayoutElement::Text(shaped) => {
// Check if we need to issue a font switching action. // Check if we need to issue a font switching action.
if shaped.face != face || shaped.font_size != size { if shaped.face != face || shaped.font_size != size {
face = shaped.face; face = shaped.face;
size = shaped.font_size; size = shaped.font_size;
let mut buf = itoa::Buffer::new(); let name = format!("F{}", self.fonts_to_pdf[&shaped.face]);
text = text.tf( text.font(Name(name.as_bytes()), size.to_pt() as f32);
Name(buf.format(1 + self.to_pdf[&shaped.face]).as_bytes()),
size.to_pt() as f32,
);
} }
let x = pos.x.to_pt(); let x = pos.x.to_pt() as f32;
let y = (page.size.height - pos.y - size).to_pt(); let y = (page.size.height - pos.y - size).to_pt() as f32;
text = text.tm(1.0, 0.0, 0.0, 1.0, x as f32, y as f32); text.matrix(1.0, 0.0, 0.0, 1.0, x, y);
text = text.tj(&shaped.encode_glyphs_be()); text.show(&shaped.encode_glyphs_be());
}
LayoutElement::Image(_image) => {
// TODO: Write image.
}
} }
} }
self.writer.stream(id, &text.end()); drop(text);
for (pos, element) in &page.elements {
if let LayoutElement::Image(image) = element {
let name = format!("Im{}", self.images.len());
let size = image.size;
let x = pos.x.to_pt() as f32;
let y = (page.size.height - pos.y - size.height).to_pt() as f32;
let w = size.width.to_pt() as f32;
let h = size.height.to_pt() as f32;
content.save_state();
content.matrix(w, 0.0, 0.0, h, x, y);
content.x_object(Name(name.as_bytes()));
content.restore_state();
self.images.push(&image.buf);
}
}
self.writer.stream(id, &content.finish());
} }
fn write_fonts(&mut self) { fn write_fonts(&mut self) {
let mut id = self.offsets.fonts.0; for (refs, &face_id) in self.refs.fonts().zip(&self.fonts_to_layout) {
for &face_id in &self.to_layout {
let owned_face = self.loader.get_loaded(face_id); let owned_face = self.loader.get_loaded(face_id);
let face = owned_face.get(); let face = owned_face.get();
@ -169,151 +202,179 @@ impl<'a> PdfExporter<'a> {
.find(|entry| { .find(|entry| {
entry.name_id() == name_id::POST_SCRIPT_NAME && entry.is_unicode() entry.name_id() == name_id::POST_SCRIPT_NAME && entry.is_unicode()
}) })
.map(|entry| entry.to_string()) .and_then(|entry| entry.to_string())
.flatten()
.unwrap_or_else(|| "unknown".to_string()); .unwrap_or_else(|| "unknown".to_string());
let base_font = format!("ABCDEF+{}", name); let base_font = format!("ABCDEF+{}", name);
let base_font = Name(base_font.as_bytes()); let base_font = Name(base_font.as_bytes());
let cmap_name = Name(b"Custom");
let system_info = SystemInfo { let system_info = SystemInfo {
registry: Str(b"Adobe"), registry: Str(b"Adobe"),
ordering: Str(b"Identity"), ordering: Str(b"Identity"),
supplement: 0, supplement: 0,
}; };
let units_per_em = face.units_per_em().unwrap_or(1000) as f32;
let ratio = 1.0 / units_per_em;
let to_glyph_unit = |font_unit: f32| (1000.0 * ratio * font_unit).round();
let global_bbox = face.global_bounding_box();
let bbox = Rect::new(
to_glyph_unit(global_bbox.x_min as f32),
to_glyph_unit(global_bbox.y_min as f32),
to_glyph_unit(global_bbox.x_max as f32),
to_glyph_unit(global_bbox.y_max as f32),
);
let monospace = face.is_monospaced();
let italic = face.is_italic();
let italic_angle = face.italic_angle().unwrap_or(0.0);
let ascender = face.typographic_ascender().unwrap_or(0);
let descender = face.typographic_descender().unwrap_or(0);
let cap_height = face.capital_height().unwrap_or(ascender);
let stem_v = 10.0 + 0.244 * (face.weight().to_number() as f32 - 50.0);
let mut flags = FontFlags::empty(); let mut flags = FontFlags::empty();
flags.set(FontFlags::SERIF, name.contains("Serif")); flags.set(FontFlags::SERIF, name.contains("Serif"));
flags.set(FontFlags::FIXED_PITCH, monospace); flags.set(FontFlags::FIXED_PITCH, face.is_monospaced());
flags.set(FontFlags::ITALIC, italic); flags.set(FontFlags::ITALIC, face.is_italic());
flags.insert(FontFlags::SYMBOLIC); flags.insert(FontFlags::SYMBOLIC);
flags.insert(FontFlags::SMALL_CAP); flags.insert(FontFlags::SMALL_CAP);
let num_glyphs = face.number_of_glyphs(); // Convert from OpenType font units to PDF glyph units.
let widths = (0 .. num_glyphs).map(|g| { let em_per_unit = 1.0 / face.units_per_em().unwrap_or(1000) as f32;
to_glyph_unit(face.glyph_hor_advance(GlyphId(g)).unwrap_or(0) as f32) let convert = |font_unit: f32| (1000.0 * em_per_unit * font_unit).round();
}); let convert_i16 = |font_unit: i16| convert(font_unit as f32);
let convert_u16 = |font_unit: u16| convert(font_unit as f32);
let mut mapping = vec![]; let global_bbox = face.global_bounding_box();
for subtable in face.character_mapping_subtables() { let bbox = Rect::new(
subtable.codepoints(|n| { convert_i16(global_bbox.x_min),
if let Some(c) = std::char::from_u32(n) { convert_i16(global_bbox.y_min),
if let Some(g) = face.glyph_index(c) { convert_i16(global_bbox.x_max),
mapping.push((g.0, c)); convert_i16(global_bbox.y_max),
} );
}
})
}
let type0_font_id = Ref::new(id); let italic_angle = face.italic_angle().unwrap_or(0.0);
let cid_font_id = Ref::new(id + 1); let ascender = convert_i16(face.typographic_ascender().unwrap_or(0));
let font_descriptor_id = Ref::new(id + 2); let descender = convert_i16(face.typographic_descender().unwrap_or(0));
let cmap_id = Ref::new(id + 3); let cap_height = face.capital_height().map(convert_i16);
let data_id = Ref::new(id + 4); let stem_v = 10.0 + 0.244 * (f32::from(face.weight().to_number()) - 50.0);
// Write the base font object referencing the CID font. // Write the base font object referencing the CID font.
self.writer self.writer
.type0_font(type0_font_id) .type0_font(refs.type0_font)
.base_font(base_font) .base_font(base_font)
.encoding_predefined(Name(b"Identity-H")) .encoding_predefined(Name(b"Identity-H"))
.descendant_font(cid_font_id) .descendant_font(refs.cid_font)
.to_unicode(cmap_id); .to_unicode(refs.cmap);
// Write the CID font referencing the font descriptor. // Write the CID font referencing the font descriptor.
self.writer self.writer
.cid_font(cid_font_id, CIDFontType::Type2) .cid_font(refs.cid_font, CidFontType::Type2)
.base_font(base_font) .base_font(base_font)
.system_info(system_info) .system_info(system_info)
.font_descriptor(font_descriptor_id) .font_descriptor(refs.font_descriptor)
.widths() .widths()
.individual(0, widths); .individual(0, {
let num_glyphs = face.number_of_glyphs();
(0 .. num_glyphs).map(|g| {
let advance = face.glyph_hor_advance(GlyphId(g));
convert_u16(advance.unwrap_or(0))
})
});
// Write the font descriptor (contains metrics about the font). // Write the font descriptor (contains metrics about the font).
self.writer self.writer
.font_descriptor(font_descriptor_id) .font_descriptor(refs.font_descriptor)
.font_name(base_font) .font_name(base_font)
.font_flags(flags) .font_flags(flags)
.font_bbox(bbox) .font_bbox(bbox)
.italic_angle(italic_angle) .italic_angle(italic_angle)
.ascent(to_glyph_unit(ascender as f32)) .ascent(ascender)
.descent(to_glyph_unit(descender as f32)) .descent(descender)
.cap_height(to_glyph_unit(cap_height as f32)) .cap_height(cap_height.unwrap_or(ascender))
.stem_v(stem_v) .stem_v(stem_v)
.font_file2(data_id); .font_file2(refs.data);
// Write the CMap, which maps glyph ids back to unicode codepoints // Write the to-unicode character map, which maps glyph ids back to
// to enable copying out of the PDF. // unicode codepoints to enable copying out of the PDF.
self.writer.cmap(cmap_id, Name(b"Custom"), system_info, mapping); self.writer
.cmap_stream(refs.cmap, &{
let mut cmap = UnicodeCmap::new(cmap_name, system_info);
for subtable in face.character_mapping_subtables() {
subtable.codepoints(|n| {
if let Some(c) = std::char::from_u32(n) {
if let Some(g) = face.glyph_index(c) {
cmap.pair(g.0, c);
}
}
})
}
cmap.finish()
})
.name(cmap_name)
.system_info(system_info);
// Write the face's bytes. // Write the face's bytes.
self.writer.stream(data_id, owned_face.data()); self.writer.stream(refs.data, owned_face.data());
}
}
id += NUM_OBJECTS_PER_FONT; fn write_images(&mut self) {
for (id, image) in self.refs.images().zip(&self.images) {
self.writer
.image_stream(id, &image.as_raw())
.width(image.width() as i32)
.height(image.height() as i32)
.color_space(ColorSpace::DeviceRGB)
.bits_per_component(8);
} }
} }
} }
/// Assigns a new PDF-internal index to each used face and returns two mappings: struct Refs {
/// - Forwards from the old face ids to the new pdf indices (hash map) catalog: Ref,
/// - Backwards from the pdf indices to the old face ids (vec) page_tree: Ref,
fn remap_fonts(layouts: &[BoxLayout]) -> (HashMap<FaceId, usize>, Vec<FaceId>) { pages_start: i32,
let mut to_pdf = HashMap::new(); contents_start: i32,
let mut to_layout = vec![]; fonts_start: i32,
images_start: i32,
// We want to find out which font faces are used at all. To do that, look at end: i32,
// each text element to find out which face is uses.
for layout in layouts {
for (_, element) in &layout.elements {
if let LayoutElement::Text(shaped) = element {
to_pdf.entry(shaped.face).or_insert_with(|| {
let next_id = to_layout.len();
to_layout.push(shaped.face);
next_id
});
}
}
} }
(to_pdf, to_layout) struct FontRefs {
type0_font: Ref,
cid_font: Ref,
font_descriptor: Ref,
cmap: Ref,
data: Ref,
} }
/// We need to know in advance which ids to use for which objects to impl Refs {
/// cross-reference them. Therefore, we calculate the indices in the beginning. const OBJECTS_PER_FONT: usize = 5;
fn calculate_offsets(layout_count: usize, font_count: usize) -> Offsets {
fn new(layouts: usize, fonts: usize, images: usize) -> Self {
let catalog = 1; let catalog = 1;
let page_tree = catalog + 1; let page_tree = catalog + 1;
let pages = (page_tree + 1, page_tree + layout_count as i32); let pages_start = page_tree + 1;
let contents = (pages.1 + 1, pages.1 + layout_count as i32); let contents_start = pages_start + layouts as i32;
let font_offsets = (contents.1 + 1, contents.1 + 5 * font_count as i32); let fonts_start = contents_start + layouts as i32;
let images_start = fonts_start + (Self::OBJECTS_PER_FONT * fonts) as i32;
let end = images_start + images as i32;
Offsets { Self {
catalog: Ref::new(catalog), catalog: Ref::new(catalog),
page_tree: Ref::new(page_tree), page_tree: Ref::new(page_tree),
pages, pages_start,
contents, contents_start,
fonts: font_offsets, fonts_start,
images_start,
end,
} }
} }
fn ids((start, end): (i32, i32)) -> impl Iterator<Item = Ref> { fn pages(&self) -> impl Iterator<Item = Ref> {
(start ..= end).map(Ref::new) (self.pages_start .. self.contents_start).map(Ref::new)
}
fn contents(&self) -> impl Iterator<Item = Ref> {
(self.contents_start .. self.images_start).map(Ref::new)
}
fn fonts(&self) -> impl Iterator<Item = FontRefs> {
(self.fonts_start .. self.images_start)
.step_by(Self::OBJECTS_PER_FONT)
.map(|id| FontRefs {
type0_font: Ref::new(id),
cid_font: Ref::new(id + 1),
font_descriptor: Ref::new(id + 2),
cmap: Ref::new(id + 3),
data: Ref::new(id + 4),
})
}
fn images(&self) -> impl Iterator<Item = Ref> {
(self.images_start .. self.end).map(Ref::new)
}
} }

View File

@ -8,7 +8,7 @@ mod spacing;
mod stack; mod stack;
mod text; mod text;
use image::RgbaImage; use image::RgbImage;
use crate::font::SharedFontLoader; use crate::font::SharedFontLoader;
use crate::geom::*; use crate::geom::*;
@ -185,7 +185,7 @@ pub enum LayoutElement {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct ImageElement { pub struct ImageElement {
/// The image. /// The image.
pub buf: RgbaImage, pub buf: RgbImage,
/// The document size of the image. /// The document size of the image.
pub size: Size, pub size: Size,
} }

View File

@ -3,7 +3,7 @@ use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use image::io::Reader; use image::io::Reader;
use image::RgbaImage; use image::RgbImage;
use crate::layout::*; use crate::layout::*;
use crate::prelude::*; use crate::prelude::*;
@ -25,7 +25,7 @@ pub fn image(mut args: Args, ctx: &mut EvalContext) -> Value {
.with_guessed_format() .with_guessed_format()
.map_err(|err| err.into()) .map_err(|err| err.into())
.and_then(|reader| reader.decode()) .and_then(|reader| reader.decode())
.map(|img| img.into_rgba8()) .map(|img| img.into_rgb8())
{ {
Ok(buf) => { Ok(buf) => {
ctx.push(Image { ctx.push(Image {
@ -49,7 +49,7 @@ pub fn image(mut args: Args, ctx: &mut EvalContext) -> Value {
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
struct Image { struct Image {
/// The image. /// The image.
buf: RgbaImage, buf: RgbImage,
/// The fixed width, if any. /// The fixed width, if any.
width: Option<Linear>, width: Option<Linear>,
/// The fixed height, if any. /// The fixed height, if any.

View File

@ -5,3 +5,4 @@
- `png`: PNG files produced by tests - `png`: PNG files produced by tests
- `ref`: Reference images which the PNGs are compared to byte-wise to determine - `ref`: Reference images which the PNGs are compared to byte-wise to determine
whether the test passed or failed whether the test passed or failed
- `res`: Resource files used by tests

View File

@ -247,8 +247,8 @@ fn draw_text(canvas: &mut Canvas, loader: &FontLoader, shaped: &Shaped, pos: Poi
fn draw_image(canvas: &mut Canvas, image: &ImageElement, pos: Point) { fn draw_image(canvas: &mut Canvas, image: &ImageElement, pos: Point) {
let mut pixmap = Pixmap::new(image.buf.width(), image.buf.height()).unwrap(); let mut pixmap = Pixmap::new(image.buf.width(), image.buf.height()).unwrap();
for (src, dest) in image.buf.pixels().zip(pixmap.pixels_mut()) { for (src, dest) in image.buf.pixels().zip(pixmap.pixels_mut()) {
let [r, g, b, a] = src.0; let [r, g, b] = src.0;
*dest = ColorU8::from_rgba(r, g, b, a).premultiply(); *dest = ColorU8::from_rgba(r, g, b, 255).premultiply();
} }
let view_width = image.size.width.to_pt() as f32; let view_width = image.size.width.to_pt() as f32;