From 77e52996673e1c3aa7a8beae4d1ee7eb9be0bafb Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 11 Mar 2019 18:02:47 +0100 Subject: [PATCH] =?UTF-8?q?Tidy=20up=20PDF=20crate=20=F0=9F=A7=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/font.rs | 8 ++++++-- src/parsing.rs | 2 ++ src/pdf.rs | 39 +++++++++++++++++++++------------------ src/utility.rs | 5 ++++- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/font.rs b/src/font.rs index 1e22e47db..2f6e2b6a9 100644 --- a/src/font.rs +++ b/src/font.rs @@ -1,9 +1,9 @@ //! Font utility and subsetting. +use std::collections::HashMap; use std::error; use std::fmt; use std::io::{self, Cursor, Seek, SeekFrom}; -use std::collections::HashMap; use byteorder::{BE, ReadBytesExt, WriteBytesExt}; use opentype::{OpenTypeReader, Outlines, TableRecord, Tag}; use opentype::tables::{Header, Name, NameEntry, CharMap, MaximumProfile, HorizontalMetrics, OS2}; @@ -49,11 +49,13 @@ impl Font { } /// Map a character to it's glyph index. + #[inline] pub fn map(&self, c: char) -> u16 { self.mapping.get(&c).map(|&g| g).unwrap_or(self.default_glyph) } /// Encode the given text for our font (into glyph ids). + #[inline] pub fn encode(&self, text: &str) -> Vec { println!("encoding {} with {:?}", text, self.mapping); let mut bytes = Vec::with_capacity(2 * text.len()); @@ -457,7 +459,8 @@ impl<'p> Subsetter<'p> { Err(_) => return Err(SubsettingError::MissingTable(tag.to_string())), }; - self.font.program.get(record.offset as usize .. (record.offset + record.length) as usize) + self.font.program + .get(record.offset as usize .. (record.offset + record.length) as usize) .take_bytes() } @@ -552,6 +555,7 @@ pub enum SubsettingError { } impl error::Error for SubsettingError { + #[inline] fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { SubsettingError::Opentype(err) => Some(err), diff --git a/src/parsing.rs b/src/parsing.rs index fc7c72ef6..ece65edd6 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -87,6 +87,7 @@ enum TokensState<'s> { } impl PartialEq for TokensState<'_> { + #[inline] fn eq(&self, other: &TokensState) -> bool { use TokensState as TS; @@ -262,6 +263,7 @@ pub struct SyntaxTree<'s> { impl<'s> SyntaxTree<'s> { /// Create an empty syntax tree. + #[inline] pub fn new() -> SyntaxTree<'s> { SyntaxTree { nodes: vec![] } } diff --git a/src/pdf.rs b/src/pdf.rs index 87d324226..a1d3dea9e 100644 --- a/src/pdf.rs +++ b/src/pdf.rs @@ -1,15 +1,12 @@ //! Writing of documents in the _PDF_ format. +use std::collections::HashSet; use std::fmt; use std::io::{self, Write, Cursor}; -use std::collections::HashSet; -use pdf::{PdfWriter, Id, Rect, Version, Trailer}; -use pdf::doc::{Catalog, PageTree, Page, Resource, Content}; -use pdf::text::Text; -use pdf::font::{ - Type0Font, CMapEncoding, CIDFont, CIDFontType, CIDSystemInfo, - WidthRecord, FontDescriptor, FontFlags, EmbeddedFont, GlyphUnit -}; +use pdf::{PdfWriter, Reference, Rect, Version, Trailer}; +use pdf::{Catalog, PageTree, Page, Resource, Text, Content}; +use pdf::font::{Type0Font, CMapEncoding, CIDFont, CIDFontType, CIDSystemInfo, WidthRecord, + FontDescriptor, FontFlags, EmbeddedFont, GlyphUnit}; use opentype::{OpenTypeReader, tables::{self, MacStyleFlags}}; use crate::doc::{self, Document, TextCommand}; use crate::font::Font; @@ -22,6 +19,7 @@ pub trait WritePdf { } impl WritePdf for W { + #[inline] fn write_pdf(&mut self, doc: &Document) -> PdfResult { PdfCreator::new(self, doc)?.write() } @@ -38,26 +36,30 @@ pub struct PdfWritingError { } impl From for PdfWritingError { + #[inline] fn from(err: io::Error) -> PdfWritingError { - PdfWritingError { message: format!("io error: {}", err) } + PdfWritingError { message: format!("{}", err) } } } impl From for PdfWritingError { + #[inline] fn from(err: opentype::Error) -> PdfWritingError { PdfWritingError { message: format!("{}", err) } } } impl From for PdfWritingError { + #[inline] fn from(err: crate::font::SubsettingError) -> PdfWritingError { PdfWritingError { message: format!("{}", err) } } } impl fmt::Display for PdfWritingError { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "pdf writing error: {}", self.message) + f.write_str(&self.message) } } @@ -73,11 +75,11 @@ struct PdfCreator<'a, W: Write> { /// Offsets for the various groups of ids. struct Offsets { - catalog: Id, - page_tree: Id, - pages: (Id, Id), - contents: (Id, Id), - fonts: (Id, Id), + catalog: Reference, + page_tree: Reference, + pages: (Reference, Reference), + contents: (Reference, Reference), + fonts: (Reference, Reference), } impl<'a, W: Write> PdfCreator<'a, W> { @@ -86,10 +88,10 @@ impl<'a, W: Write> PdfCreator<'a, W> { // Calculate a unique id for all object to come let catalog = 1; let page_tree = catalog + 1; - let pages = (page_tree + 1, page_tree + doc.pages.len() as Id); - let content_count = doc.pages.iter().flat_map(|p| p.text.iter()).count() as Id; + let pages = (page_tree + 1, page_tree + doc.pages.len() as Reference); + let content_count = doc.pages.iter().flat_map(|p| p.text.iter()).count() as Reference; let contents = (pages.1 + 1, pages.1 + content_count); - let fonts = (contents.1 + 1, contents.1 + 4 * doc.fonts.len() as Id); + let fonts = (contents.1 + 1, contents.1 + 4 * doc.fonts.len() as Reference); let offsets = Offsets { catalog, @@ -338,6 +340,7 @@ impl PdfFont { impl std::ops::Deref for PdfFont { type Target = Font; + #[inline] fn deref(&self) -> &Font { &self.font } diff --git a/src/utility.rs b/src/utility.rs index a0e6a2958..4e6bc7d9f 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -1,7 +1,7 @@ //! Utility functionality. -use std::str::Split; use std::iter::Peekable; +use std::str::Split; use unicode_xid::UnicodeXID; @@ -29,6 +29,7 @@ pub trait Splinor { } impl Splinor for str { + #[inline] fn spline<'s, T: Clone>(&'s self, pat: &'s str, splinor: T) -> Spline<'s, T> { Spline { splinor: Splined::Splinor(splinor), @@ -60,6 +61,7 @@ pub enum Splined<'s, T> { impl<'s, T: Clone> Iterator for Spline<'s, T> { type Item = Splined<'s, T>; + #[inline] fn next(&mut self) -> Option> { if self.next_splinor && self.split.peek().is_some() { self.next_splinor = false; @@ -87,6 +89,7 @@ impl StrExt for str { self.chars().all(|c| c.is_whitespace() && c != '\n') } + #[inline] fn is_identifier(&self) -> bool { let mut chars = self.chars();