summaryrefslogtreecommitdiff
path: root/src/export/pdf.rs
blob: e8220b5461a311eca0a336d46c192233b21e1317 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! Exporting into _PDF_ documents.

use std::collections::HashMap;
use std::io::{self, Write};

use fontdock::FaceId;
use tide::content::Content;
use tide::doc::{Catalog, Page, PageTree, Resource, Text};
use tide::font::{
    CIDFont, CIDFontType, CIDSystemInfo, CMap, CMapEncoding, FontDescriptor,
    FontFlags, FontStream, GlyphUnit, Type0Font, WidthRecord,
};
use tide::{PdfWriter, Rect, Ref, Trailer, Version};
use ttf_parser::{name_id, GlyphId};

use crate::layout::elements::LayoutElement;
use crate::layout::BoxLayout;
use crate::length::Length;
use crate::SharedFontLoader;

/// Export a list of layouts into a _PDF_ document.
///
/// This creates one page per layout. Additionally to the layouts, you need to
/// pass in the font loader used for typesetting such that the fonts can be
/// included in the _PDF_.
///
/// The raw _PDF_ is written into the `target` writable, returning the number of
/// bytes written.
pub fn export<W: Write>(
    layout: &[BoxLayout],
    loader: &SharedFontLoader,
    target: W,
) -> io::Result<usize> {
    PdfExporter::new(layout, loader, target)?.write()
}

struct PdfExporter<'a, W: Write> {
    writer: PdfWriter<W>,
    layouts: &'a [BoxLayout],
    loader: &'a SharedFontLoader,
    /// We need to know exactly which indirect reference id will be used for
    /// which objects up-front to correctly declare the document catalogue, page
    /// tree and so on. These offsets are computed in the beginning and stored
    /// here.
    offsets: Offsets,
    // Font remapping, see below at `remap_fonts`.
    to_pdf: HashMap<FaceId, usize>,
    to_layout: Vec<FaceId>,
}

struct Offsets {
    catalog: Ref,
    page_tree: Ref,
    pages: (Ref, Ref),
    contents: (Ref, Ref),
    fonts: (Ref, Ref),
}

const NUM_OBJECTS_PER_FONT: u32 = 5;

impl<'a, W: Write> PdfExporter<'a, W> {
    fn new(
        layouts: &'a [BoxLayout],
        loader: &'a SharedFontLoader,
        target: W,
    ) -> io::Result<Self> {
        let (to_pdf, to_fontdock) = remap_fonts(layouts);
        let offsets = calculate_offsets(layouts.len(), to_pdf.len());

        Ok(Self {
            writer: PdfWriter::new(target),
            layouts,
            offsets,
            to_pdf,
            to_layout: to_fontdock,
            loader,
        })
    }

    fn write(&mut self) -> io::Result<usize> {
        self.writer.write_header(Version::new(1, 7))?;
        self.write_preface()?;
        self.write_pages()?;
        self.write_fonts()?;
        self.writer.write_xref_table()?;
        self.writer.write_trailer(Trailer::new(self.offsets.catalog))?;
        Ok(self.writer.written())
    }

    fn write_preface(&mut self) -> io::Result<()> {
        // The document catalog.
        self.writer.write_obj(self.offsets.catalog, &Catalog::new(self.offsets.page_tree))?;

        // The font resources.
        let start = self.offsets.fonts.0;
        let fonts = (0..self.to_pdf.len() as u32).map(|i| {
            Resource::Font(i + 1, start + (NUM_OBJECTS_PER_FONT * i))
        });

        // The root page tree.
        self.writer.write_obj(
            self.offsets.page_tree,
            PageTree::new()
                .kids(ids(self.offsets.pages))
                .resources(fonts),
        )?;

        // The page objects (non-root nodes in the page tree).
        for ((page_id, content_id), page) in ids(self.offsets.pages)
            .zip(ids(self.offsets.contents))
            .zip(self.layouts)
        {
            let rect = Rect::new(
                0.0,
                0.0,
                Length::raw(page.size.x).as_pt() as f32,
                Length::raw(page.size.y).as_pt() as f32,
            );

            self.writer.write_obj(
                page_id,
                Page::new(self.offsets.page_tree)
                    .media_box(rect)
                    .content(content_id),
            )?;
        }

        Ok(())
    }

    fn write_pages(&mut self) -> io::Result<()> {
        for (id, page) in ids(self.offsets.contents).zip(self.layouts) {
            self.write_page(id, &page)?;
        }
        Ok(())
    }

    fn write_page(&mut self, id: u32, page: &BoxLayout) -> io::Result<()> {
        let mut text = Text::new();

        // Font switching actions are only written when the face used for
        // shaped text changes. Hence, we need to remember the active face.
        let mut face = FaceId::MAX;
        let mut size = 0.0;

        for (pos, element) in &page.elements.0 {
            match element {
                LayoutElement::Text(shaped) => {
                    if shaped.face != face || shaped.size != size {
                        face = shaped.face;
                        size = shaped.size;
                        text.tf(
                            self.to_pdf[&shaped.face] as u32 + 1,
                            Length::raw(size).as_pt() as f32
                        );
                    }

                    let x = Length::raw(pos.x).as_pt();
                    let y = Length::raw(page.size.y - pos.y - size).as_pt();
                    text.tm(1.0, 0.0, 0.0, 1.0, x as f32, y as f32);
                    text.tj(shaped.encode_glyphs_be());
                }
            }
        }

        self.writer.write_obj(id, &text.to_stream())?;

        Ok(())
    }

    fn write_fonts(&mut self) -> io::Result<()> {
        let mut id = self.offsets.fonts.0;

        for &face_id in &self.to_layout {
            let loader = self.loader.borrow();
            let face = loader.get_loaded(face_id);

            let name = face
                .names()
                .find(|entry| {
                    entry.name_id() == name_id::POST_SCRIPT_NAME
                    && entry.is_unicode()
                })
                .map(|entry| entry.to_string())
                .flatten()
                .unwrap_or_else(|| "unknown".to_string());

            let base_font = format!("ABCDEF+{}", name);
            let system_info = CIDSystemInfo::new("Adobe", "Identity", 0);

            let units_per_em = face.units_per_em().unwrap_or(1000) as f64;
            let ratio = 1.0 / units_per_em;
            let to_glyph_unit = |font_unit: f64| {
                (1000.0 * ratio * font_unit).round() as GlyphUnit
            };

            let global_bbox = face.global_bounding_box();
            let bbox = Rect::new(
                to_glyph_unit(global_bbox.x_min as f64),
                to_glyph_unit(global_bbox.y_min as f64),
                to_glyph_unit(global_bbox.x_max as f64),
                to_glyph_unit(global_bbox.y_max as f64),
            );

            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();
            flags.set(FontFlags::SERIF, name.contains("Serif"));
            flags.set(FontFlags::FIXED_PITCH, monospace);
            flags.set(FontFlags::ITALIC, italic);
            flags.insert(FontFlags::SYMBOLIC);
            flags.insert(FontFlags::SMALL_CAP);

            let num_glyphs = face.number_of_glyphs();
            let widths: Vec<_> = (0..num_glyphs)
                .map(|g| face.glyph_hor_advance(GlyphId(g)).unwrap_or(0))
                .map(|w| to_glyph_unit(w as f64))
                .collect();

            let mut mapping = vec![];
            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) {
                            mapping.push((g.0, c));
                        }
                    }
                })
            }

            // Write the base font object referencing the CID font.
            self.writer.write_obj(
                id,
                Type0Font::new(
                    base_font.clone(),
                    CMapEncoding::Predefined("Identity-H".to_string()),
                    id + 1,
                )
                .to_unicode(id + 3),
            )?;

            // Write the CID font referencing the font descriptor.
            self.writer.write_obj(
                id + 1,
                CIDFont::new(
                    CIDFontType::Type2,
                    base_font.clone(),
                    system_info.clone(),
                    id + 2,
                )
                .widths(vec![WidthRecord::Start(0, widths)]),
            )?;

            // Write the font descriptor (contains metrics about the font).
            self.writer.write_obj(id + 2,
                FontDescriptor::new(base_font, flags, italic_angle)
                    .font_bbox(bbox)
                    .ascent(to_glyph_unit(ascender as f64))
                    .descent(to_glyph_unit(descender as f64))
                    .cap_height(to_glyph_unit(cap_height as f64))
                    .stem_v(stem_v as GlyphUnit)
                    .font_file_2(id + 4)
            )?;

            // Write the CMap, which maps glyph ids back to unicode codepoints
            // to enable copying out of the PDF.
            self.writer.write_obj(id + 3, &CMap::new(
                "Custom",
                system_info,
                mapping,
            ))?;

            // Write the face's bytes.
            self.writer.write_obj(id + 4, &FontStream::new(face.data()))?;

            id += NUM_OBJECTS_PER_FONT;
        }

        Ok(())
    }
}

/// Assigns a new PDF-internal index to each used face and returns two mappings:
/// - Forwards from the old face ids to the new pdf indices (hash map)
/// - Backwards from the pdf indices to the old face ids (vec)
fn remap_fonts(layouts: &[BoxLayout]) -> (HashMap<FaceId, usize>, Vec<FaceId>) {
    let mut to_pdf = HashMap::new();
    let mut to_layout = vec![];

    // We want to find out which font faces are used at all. To do that, look at
    // each text element to find out which face is uses.
    for layout in layouts {
        for (_, element) in &layout.elements.0 {
            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)
}

/// We need to know in advance which ids to use for which objects to
/// cross-reference them. Therefore, we calculate the indices in the beginning.
fn calculate_offsets(layout_count: usize, font_count: usize) -> Offsets {
    let catalog = 1;
    let page_tree = catalog + 1;
    let pages = (page_tree + 1, page_tree + layout_count as Ref);
    let contents = (pages.1 + 1, pages.1 + layout_count as Ref);
    let font_offsets = (contents.1 + 1, contents.1 + 5 * font_count as Ref);

    Offsets {
        catalog,
        page_tree,
        pages,
        contents,
        fonts: font_offsets,
    }
}

fn ids((start, end): (Ref, Ref)) -> impl Iterator<Item = Ref> {
    start..=end
}