diff --git a/Cargo.toml b/Cargo.toml index e2d602f..4c45df6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "printpdf" -version = "0.2.2" +version = "0.2.3" authors = ["Felix Schütt "] repository = "https://github.com/fschutt/printpdf" homepage = "https://github.com/fschutt/printpdf" @@ -10,7 +10,7 @@ description = "Rust library for writing PDF files" [dependencies] lopdf = "0.15.1" -rusttype = { git = "https://github.com/redox-os/rusttype", rev = "753ea0df5d537a73b3a94e6c5ff406e204b4431e" } +rusttype = "0.3.1" error-chain = "0.11.0" chrono = "0.4.0" rand = { version = "0.4.2", default-features = false } diff --git a/README.md b/README.md index 9b3b4bd..cd8f98a 100644 --- a/README.md +++ b/README.md @@ -27,43 +27,6 @@ Currently, printpdf can only write documents, not read them. ## Getting started -### Installation requirements - -#### Linux - -You need to install freetype, if you haven't already installed it: -```rust -sudo apt install libfreetype6-dev -``` - -#### Windows - -##### pc-windows-gnu - -In order to easily setup freetype just get MSYS2 and install either the `mingw-w64-x86_64-freetype` or `mingw-w64-i686-freetype` package and then use Rust from within the correct mingw shell of MSYS2. - -More information on setting up MSYS2 for Rust can be found in [the Rust readme](https://github.com/rust-lang/rust#building-on-windows) - -##### pc-windows-msvc - -Prebuilt libraries for freetype are available [here](https://github.com/PistonDevelopers/binaries). - -Then in the root of your project, in one of the parent directories, or in your home directory, create a .cargo directory. This directory should contain a `config` file that contains the following snippet: - -```toml -[target.i686-pc-windows-msvc.freetype] -rustc-link-search = ["C:\\Path\\To\\binaries\\i686"] -rustc-link-lib = ["freetype"] - -[target.x86_64-pc-windows-msvc.freetype] -rustc-link-search = ["C:\\Path\\To\\binaries\\x86_64"] -rustc-link-lib = ["freetype"] -``` - -For more informations, check [the official Cargo documentation](http://doc.crates.io/build-script.html#overriding-build-scripts). - -(taken from [this README](https://github.com/PistonDevelopers/freetype-sys/blob/master/README.md)) - ### Writing PDF #### Simple page diff --git a/src/errors/errors.rs b/src/errors/errors.rs index e1323b1..40a4279 100644 --- a/src/errors/errors.rs +++ b/src/errors/errors.rs @@ -1,7 +1,5 @@ #![allow(unused_qualifications)] -extern crate rusttype; - use super::*; error_chain! { diff --git a/src/lib.rs b/src/lib.rs index a185ee9..e05b7c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,43 +21,6 @@ //! - PDF layers (you should be able to open the PDF in Illustrator and have the layers appear) //! //! # Getting started -//! -//! ## Installation requirements -//! -//! ### Linux -//! -//! You need to install freetype, if you haven't already installed it: -//! ``` -//! sudo apt install libfreetype6-dev -//! ``` -//! -//! ### Windows -//! -//! #### pc-windows-gnu -//! -//! In order to easily setup freetype just get MSYS2 and install either the `mingw-w64-x86_64-freetype` or `mingw-w64-i686-freetype` package and then use Rust from within the correct mingw shell of MSYS2. -//! -//! More information on setting up MSYS2 for Rust can be found in [the Rust readme](https://github.com/rust-lang/rust#building-on-windows) -//! -//! #### pc-windows-msvc -//! -//! Prebuilt libraries for freetype are available [here](https://github.com/PistonDevelopers/binaries). -//! -//! Then in the root of your project, in one of the parent directories, or in your home directory, create a .cargo directory. This directory should contain a `config` file that contains the following snippet: -//! -//! ```toml -//! [target.i686-pc-windows-msvc.freetype] -//! rustc-link-search = ["C:\\Path\\To\\binaries\\i686"] -//! rustc-link-lib = ["freetype"] -//! -//! [target.x86_64-pc-windows-msvc.freetype] -//! rustc-link-search = ["C:\\Path\\To\\binaries\\x86_64"] -//! rustc-link-lib = ["freetype"] -//! ``` -//! -//! For more informations, check [the official Cargo documentation](http://doc.crates.io/build-script.html#overriding-build-scripts). -//! -//! (taken from [this README](https://github.com/PistonDevelopers/freetype-sys/blob/master/README.md)) //! //! ## Writing PDF //! diff --git a/src/types/pdf_layer.rs b/src/types/pdf_layer.rs index 394b379..5f99d3c 100644 --- a/src/types/pdf_layer.rs +++ b/src/types/pdf_layer.rs @@ -1,7 +1,6 @@ //! PDF layer management. Layers can contain referenced or real content. use lopdf; -use rusttype; use indices::{PdfPageIndex, PdfLayerIndex}; use std::rc::Weak; diff --git a/src/types/plugins/graphics/two_dimensional/font.rs b/src/types/plugins/graphics/two_dimensional/font.rs index 8c0e745..df23aed 100644 --- a/src/types/plugins/graphics/two_dimensional/font.rs +++ b/src/types/plugins/graphics/two_dimensional/font.rs @@ -2,8 +2,6 @@ //! Embedding fonts in 2D for Pdf use lopdf; -use rusttype; - use lopdf::{Stream as LoStream, Dictionary as LoDictionary}; use lopdf::StringFormat; use std::collections::{HashMap, BTreeMap}; @@ -142,6 +140,8 @@ impl ExternalFont { pub fn new(mut font_stream: R, font_index: usize) -> Result where R: ::std::io::Read { + use errors::FontError; + // read font from stream and parse font metrics let mut buf = Vec::::new(); font_stream.read_to_end(&mut buf)?; @@ -152,7 +152,7 @@ impl ExternalFont { if let None = font { if let None = collection.into_fonts().nth(0) { - return Err(Error::from_kind(FontError)); + return Err(PrintpdfError::from_kind(FontError)); } } @@ -220,28 +220,35 @@ impl ExternalFont { // Widths (or heights, depends on self.vertical_writing) // of the individual characters, indexed by glyph id let mut widths = HashMap::::new(); - // Height of the space (0x0020 character), to scale the font correctly - let mut space_height; // Glyph IDs - (Unicode IDs - character width, character height) let mut cmap = BTreeMap::::new(); cmap.insert(0, (0, 1000, 1000)); for unicode in 0x0000..0xffff { + let glyph = font.glyph(Cpg(Cp(unicode))); + if let Some(glyph) = glyph { - if glyph.id().0 == 0 { continue; } + + if glyph.id().0 == 0 { + continue; + } + let glyph_id = glyph.id().0; + if let Some(glyph) = font.glyph(Cgid(Gid(glyph_id))) { + if let Some(glyph_metrics) = glyph.standalone().get_data() { + if let Some(extents) = glyph_metrics.extents { + let w = glyph_metrics.unit_h_metrics.advance_width; let h = extents.max.y - extents.min.y - face_metrics.descent as i32; - - // large T - // if unicode == 0x0020 { space_height = h; } - - if h > max_height { max_height = h; }; + + if h > max_height { + max_height = h; + }; total_width += w as u32; cmap.insert(glyph_id, (unicode as u32, w as u32, h as u32)); @@ -251,9 +258,6 @@ impl ExternalFont { } } - let v_metrics = font.v_metrics_unscaled(); - space_height = v_metrics.ascent /* - v_metrics.line_gap + v_metrics.descent + v_metrics.line_gap*/ as i32; - // Maps the character index to a unicode value // Add this to the "ToUnicode" dictionary // To explain this structure: Glyph IDs have to be in segments where the first byte of the @@ -309,7 +313,7 @@ impl ExternalFont { let mut current_width_vec = Vec::::new(); // scale the font width so that it sort-of fits into an 1000 unit square - let percentage_font_scaling = 1000.0 / (face.em_size() as f64); + let percentage_font_scaling = 1000.0 / (font.units_per_em() as f64); for (gid, width) in widths { if gid == current_high_gid {