JFLib

pdf crate root#

/// A4 portrait in points
A4: (f64, f64) = (595.275_590_551_181_1, 841.889_763_779_527_6)
/// A3 portrait in points
A3: (f64, f64) = (841.889_763_779_527_6, 1_190.551_181_102_362_2)
/// A5 portrait in points
A5: (f64, f64) = (419.527_559_055_118_1, 595.275_590_551_181_1)
/// US Letter portrait in points
LETTER: (f64, f64) = (612.0, 792.0)
/// US Legal portrait in points
LEGAL: (f64, f64) = (612.0, 1008.0)
  • fn mm(v: f64) -> f64 millimetres in points

  • fn inch(v: f64) -> f64 inches in points

/// the smallest and largest page side PDF readers take (ISO 32000-1, Annex C)
PAGE_MIN: f64 = 3.0
PAGE_MAX: f64 = 14_400.0
  • enum Error

    • Font(&'static str) -- the font file cannot be used: damaged, or a kind not taken
    • FontLicense(&'static str) -- the font's licence (OS/2 fsType) does not allow embedding
    • Image(&'static str) -- image data that does not fit what it claims
    • Unsupported(&'static str) -- valid input this writer does not handle (CFF2 fonts, arithmetic-coded JPEG, ...)
    • Invalid(&'static str) -- the document cannot be written as it is (no pages, a link to a page it lacks, ...)
  • struct Glyph a shaped glyph for Canvas::glyphs: Document::shape gives them, a text layout that shaped its lines already builds its own

    • font: usize -- which of the fonts passed along (0: the first)
    • id: u32 -- the glyph id in that font
    • advance: i32 -- how far the pen moves after the glyph, in font units
    • x: i32 -- the glyph's offset from the pen in font units (y up)
    • y: i32
    • cluster: usize -- the byte range of the text the glyph stands for (its cluster): cluster..end
    • end: usize
  • struct PageId(pub u32) a page of a document (Document::add_page), counted from 0

  • struct FontId(u32) a font of a document (Document::add_font)

  • struct ImageId(u32) an image of a document (Document::add_image)

  • struct OutlineId(u32) an outline item (Document::add_outline)

  • enum Dest a place in the document for links and outline items

    • Fit(PageId) -- the whole page in the window
    • Xyz { page: PageId, left: f64, top: f64, zoom: Option<f64> } -- the point (left, top) of PDF page space (points from the bottom left, y up) at the top left of the window; zoom None keeps the reader's (Canvas::dest builds one from a canvas's own coordinates)
  • struct Date a date of the document information: local time and its offset from UTC

    • year: u16
    • month: u8
    • day: u8
    • hour: u8
    • minute: u8
    • second: u8
    • offset_minutes: i16 -- minutes east of UTC (+60: Central European Time), -1439 to 1439
  • struct Info the document information (the Info dictionary and the same in XMP metadata); unset fields are left out

    • title: Option<String>
    • author: Option<String>
    • subject: Option<String>
    • keywords: Option<String>
    • creator: Option<String> -- the program the document was made with
    • producer: Option<String> -- the program that wrote the PDF (None: "JFLib")
    • created: Option<Date>
    • modified: Option<Date>
  • struct Document a PDF document being built: its pages, fonts, images, outlines and information

impl Document

  • fn new() -> Document
  • fn add_page(&mut self, width: f64, height: f64) -> PageId a new page at the end, width and height in points (clamped to PAGE_MIN .. PAGE_MAX)
  • fn page_count(&self) -> usize
  • fn page_size(&self, page: PageId) -> Option<(f64, f64)> width and height in points
  • fn canvas(&mut self, page: PageId, origin: Origin) -> Canvas<'_> drawing on a page (panics for a page this document does not have)
  • fn add_font(&mut self, data: Vec<u8>, face: u32) -> Result<FontId, Error> a TrueType or OpenType font (glyf or CFF outlines; face: of a collection, else 0)
  • fn font_metrics(&self, font: FontId) -> Option<FontMetrics>
  • fn shape(&self, fonts: &[FontId], text: &str) -> Vec<Glyph> one line shaped with the fonts (the first, then fallbacks per script run); empty for an unknown font or a line beyond 64 KiB
  • fn text_width(&self, fonts: &[FontId], size: f64, text: &str) -> f64 the advance of one shaped line at size points
  • fn add_image(&mut self, img: Image) -> ImageId
  • fn add_outline(&mut self, parent: Option<OutlineId>, title: &str, dest: Dest) -> OutlineId an outline item (bookmark) under parent (None: at the top), after its siblings; open (its children shown) until set_outline_open says otherwise
  • fn set_outline_open(&mut self, item: OutlineId, open: bool)
  • fn set_info(&mut self, info: Info)
  • fn set_language(&mut self, lang: &str) the document's language (BCP 47, e.g. "de-DE")
  • fn set_compression(&mut self, level: u8) DEFLATE level 0 (streams uncompressed, for reading the file) to 9; default 6
  • fn set_encryption(&mut self, e: Option<Encryption>) encrypt the file (None: not, the default); see Encryption
  • fn write(&self) -> Result<Vec<u8>, Error> the PDF file

Source: docs/API.md in the JFLib repository.