Skip to content

Commit

Permalink
Converted millimeter and points to strong typing, instead of just f64…
Browse files Browse the repository at this point in the history
…, updated README + tests
  • Loading branch information
fschutt committed Feb 11, 2018
1 parent 29bba60 commit 6ff9138
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 120 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ sudo apt install libfreetype6-dev

#### Windows

#### -pc-windows-gnu
##### 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
##### pc-windows-msvc

Prebuilt libraries for freetype are available [here](https://github.com/PistonDevelopers/binaries).

Expand Down Expand Up @@ -72,8 +72,8 @@ use printpdf::*;
use std::fs::File;
use std::io::BufWriter;

let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
let (page2, layer1) = doc.add_page(10.0, 250.0,"Page 2, Layer 1");
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
let (page2, layer1) = doc.add_page(Mm(10.0), Mm(250.0),"Page 2, Layer 1");

doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap();
```
Expand All @@ -83,26 +83,26 @@ doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap(
```rust
use printpdf::*;

let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");

let mut current_layer = doc.get_page(page1).get_layer(layer1);

// Quadratic shape. The "false" determines if the next (following)
// point is a bezier handle (for curves)
// If you want holes, simply reorder the winding of the points to be
// counterclockwise instead of clockwise.
let points1 = vec![(Point::new(100.0, 100.0), false),
(Point::new(100.0, 200.0), false),
(Point::new(300.0, 200.0), false),
(Point::new(300.0, 100.0), false)];
let points1 = vec![(Point::new(Mm(100.0), Mm(100.0)), false),
(Point::new(Mm(100.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(100.0)), false)];

// Is the shape stroked? Is the shape closed? Is the shape filled?
let line1 = Line::new(points1, true, true, true);

// Triangle shape
let points2 = vec![(Point::new(150.0, 150.0), false),
(Point::new(150.0, 250.0), false),
(Point::new(350.0, 250.0), false)];
let points2 = vec![(Point::new(Mm(150.0), Mm(150.0)), false),
(Point::new(Mm(150.0), Mm(250.0)), false),
(Point::new(Mm(350.0), Mm(250.0)), false)];

let line2 = Line::new(points2, true, false, false);

Expand Down Expand Up @@ -147,14 +147,15 @@ Scaling of images is implicitly done to fit one pixel = one dot at 300 dpi.

```rust
extern crate printpdf;
extern crate image; /* currently: version 0.14.0 */

// imports the `image` library with the exact version that we are using
use printpdf::*;

use std::convert::From;
use std::fs::File;

fn main() {
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);

// currently, the only reliable file format is bmp (jpeg works, but not in release mode)
Expand All @@ -169,8 +170,8 @@ fn main() {

// you can also construct images manually from your data:
let mut image_file_2 = ImageXObject {
width: 200,
height: 200,
width: Px(200),
height: Px(200),
color_space: ColorSpace::Greyscale,
bits_per_component: ColorBits::Bit8,
interpolate: true,
Expand All @@ -197,7 +198,7 @@ the most common use-case).
use printpdf::*;
use std::fs::File;

let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);

let text = "Lorem ipsum";
Expand All @@ -207,7 +208,7 @@ let font = doc.add_external_font(File::open("assets/fonts/RobotoMedium.ttf").unw
let font2 = doc.add_external_font(File::open("assets/fonts/RobotoMedium.ttf").unwrap()).unwrap();

// text, font size, x from left edge, y from top edge, font
current_layer.use_text(text, 48, 200.0, 200.0, &font);
current_layer.use_text(text, 48, Mm(200.0), Mm(200.0), &font);

// For more complex layout of text, you can use functions
// defined on the PdfLayerReference
Expand All @@ -218,7 +219,7 @@ current_layer.begin_text_section();
// setup the general fonts.
// see the docs for these functions for details
current_layer.set_font(&font2, 33);
current_layer.set_text_cursor(10.0, 10.0);
current_layer.set_text_cursor(Mm(10.0), Mm(10.0));
current_layer.set_line_height(33);
current_layer.set_word_spacing(3000);
current_layer.set_character_spacing(10);
Expand Down
4 changes: 2 additions & 2 deletions examples/builtin_fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs::File;
use std::io::BufWriter;

fn main() {
let (mut doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 500.0, 300.0, "Layer 1");
let (mut doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(500.0), Mm(300.0), "Layer 1");
doc = doc.with_conformance(PdfConformance::Custom(CustomPdfConformance {
requires_icc_profile: false,
requires_xmp_metadata: false,
Expand All @@ -18,6 +18,6 @@ fn main() {
let text = "Lorem ipsum";

let font = doc.add_builtin_font(BuiltinFont::TimesBoldItalic).unwrap();
current_layer.use_text(text, 48, 10.0, 200.0, &font);
current_layer.use_text(text, 48, Mm(10.0), Mm(200.0), &font);
doc.save(&mut BufWriter::new(File::create("test_builtin_fonts.pdf").unwrap())).unwrap();
}
8 changes: 4 additions & 4 deletions examples/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::File;
use std::io::BufWriter;

fn main() {
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 500.0, 300.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(500.0), Mm(300.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);

let text = "Lorem ipsum";
Expand All @@ -15,7 +15,7 @@ fn main() {
let font = doc.add_external_font(&mut font_reader).unwrap();

// `use_text` is a wrapper around making a simple string
current_layer.use_text(text, 48, 10.0, 200.0, &font);
current_layer.use_text(text, 48, Mm(10.0), Mm(200.0), &font);

// text fill color = blue
let blue = Rgb::new(13.0 / 256.0, 71.0 / 256.0, 161.0 / 256.0, None);
Expand All @@ -28,11 +28,11 @@ fn main() {
// Make sure to wrap your commands
// in a `begin_text_section()` and `end_text_section()` wrapper
current_layer.begin_text_section();

// setup the general fonts.
// see the docs for these functions for details
current_layer.set_font(&font, 33);
current_layer.set_text_cursor(10.0, 100.0);
current_layer.set_text_cursor(Mm(10.0), Mm(100.0));
current_layer.set_line_height(33);
current_layer.set_word_spacing(3000);
current_layer.set_character_spacing(10);
Expand Down
16 changes: 8 additions & 8 deletions examples/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ use std::io::BufWriter;

fn main() {

let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test",297.0, 210.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(297.0), Mm(210.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);

// Quadratic shape. The "false" determines if the next (following)
// point is a bezier handle (for curves)
// If you want holes, simply reorder the winding of the points to be
// counterclockwise instead of clockwise.
let points1 = vec![(Point::new(100.0, 100.0), false),
(Point::new(100.0, 200.0), false),
(Point::new(300.0, 200.0), false),
(Point::new(300.0, 100.0), false)];
let points1 = vec![(Point::new(Mm(100.0), Mm(100.0)), false),
(Point::new(Mm(100.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(200.0)), false),
(Point::new(Mm(300.0), Mm(100.0)), false)];

// Is the shape stroked? Is the shape closed? Is the shape filled?
let line1 = Line::new(points1, true, true, true);

// Triangle shape
let points2 = vec![(Point::new(150.0, 150.0), false),
(Point::new(150.0, 250.0), false),
(Point::new(350.0, 250.0), false)];
let points2 = vec![(Point::new(Mm(150.0), Mm(150.0)), false),
(Point::new(Mm(150.0), Mm(250.0)), false),
(Point::new(Mm(350.0), Mm(250.0)), false)];
let line2 = Line::new(points2, true, false, false);

let fill_color = Color::Cmyk(Cmyk::new(0.0, 0.23, 0.0, 0.0, None));
Expand Down
2 changes: 1 addition & 1 deletion examples/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fs::File;
use std::io::BufWriter;

fn main() {
let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", 210.0, 297.0, "Layer 1");
let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);

// currently, the only reliable file format is bmp (jpeg works, but not in release mode)
Expand Down
2 changes: 1 addition & 1 deletion examples/no_icc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
// Currently, fonts need to use an embedded font, so if you need to write something, the file size
// will still be bloated (because of the embedded font)
// Also, OCG content is still enabled, even if you disable it here.
let (mut doc, _page1, _layer1) = PdfDocument::new("printpdf no_icc test", 297.0, 210.0, "Layer 1");
let (mut doc, _page1, _layer1) = PdfDocument::new("printpdf no_icc test", Mm(297.0), Mm(210.0), "Layer 1");
doc = doc.with_conformance(PdfConformance::Custom(CustomPdfConformance {
requires_icc_profile: false,
requires_xmp_metadata: false,
Expand Down
4 changes: 2 additions & 2 deletions examples/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ fn main() {
// To prevent empty documents, you must specify at least one page with one layer
// You can later on add more pages with the add_page() function
// You also have to specify the title of the PDF and the document creator
let (doc, _, _) = PdfDocument::new("printpdf page test", 210.0, 297.0, "Layer 1");
let (doc, _, _) = PdfDocument::new("printpdf page test", Mm(210.0), Mm(297.0), "Layer 1");

// You can add more pages and layers to the PDF.
// Just make sure you don't lose the references, otherwise, you can't add things to the layer anymore
let (page2, _) = doc.add_page(297.0, 210.0,"Page 2, Layer 1");
let (page2, _) = doc.add_page(Mm(297.0), Mm(210.0),"Page 2, Layer 1");
let _ = doc.get_page(page2).add_layer("Layer 3");

// If this is successful, you should see a PDF with two blank A4 pages
Expand Down
4 changes: 3 additions & 1 deletion src/glob_macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Macros for printpdf
/*
/// Convert millimeter to points
#[macro_export]
macro_rules! mm_to_pt {
Expand All @@ -9,5 +10,6 @@ macro_rules! mm_to_pt {
/// Convert point to millimeter
#[macro_export]
macro_rules! pt_to_mm {
($mm: expr) => ($mm * 0.352_778_f64);
($pt: expr) => ($pt * 0.352_778_f64);
}
*/
38 changes: 20 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
//!
//! ### Windows
//!
//! ### -pc-windows-gnu
//! #### 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
//! #### pc-windows-msvc
//!
//! Prebuilt libraries for freetype are available [here](https://github.com/PistonDevelopers/binaries).
//!
Expand Down Expand Up @@ -68,8 +68,8 @@
//! use std::fs::File;
//! use std::io::BufWriter;
//!
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
//! let (page2, layer1) = doc.add_page(10.0, 250.0,"Page 2, Layer 1");
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
//! let (page2, layer1) = doc.add_page(Mm(10.0), Mm(250.0),"Page 2, Layer 1");
//!
//! doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap();
//! ```
Expand All @@ -79,26 +79,26 @@
//! ```rust
//! use printpdf::*;
//!
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
//!
//! let mut current_layer = doc.get_page(page1).get_layer(layer1);
//!
//! // Quadratic shape. The "false" determines if the next (following)
//! // point is a bezier handle (for curves)
//! // If you want holes, simply reorder the winding of the points to be
//! // counterclockwise instead of clockwise.
//! let points1 = vec![(Point::new(100.0, 100.0), false),
//! (Point::new(100.0, 200.0), false),
//! (Point::new(300.0, 200.0), false),
//! (Point::new(300.0, 100.0), false)];
//! let points1 = vec![(Point::new(Mm(100.0), Mm(100.0)), false),
//! (Point::new(Mm(100.0), Mm(200.0)), false),
//! (Point::new(Mm(300.0), Mm(200.0)), false),
//! (Point::new(Mm(300.0), Mm(100.0)), false)];
//!
//! // Is the shape stroked? Is the shape closed? Is the shape filled?
//! let line1 = Line::new(points1, true, true, true);
//!
//! // Triangle shape
//! let points2 = vec![(Point::new(150.0, 150.0), false),
//! (Point::new(150.0, 250.0), false),
//! (Point::new(350.0, 250.0), false)];
//! let points2 = vec![(Point::new(Mm(150.0), Mm(150.0)), false),
//! (Point::new(Mm(150.0), Mm(250.0)), false),
//! (Point::new(Mm(350.0), Mm(250.0)), false)];
//!
//! let line2 = Line::new(points2, true, false, false);
//!
Expand Down Expand Up @@ -151,7 +151,7 @@
//! use std::fs::File;
//!
//! fn main() {
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
//! let current_layer = doc.get_page(page1).get_layer(layer1);
//!
//! // currently, the only reliable file format is bmp (jpeg works, but not in release mode)
Expand All @@ -166,8 +166,8 @@
//!
//! // you can also construct images manually from your data:
//! let mut image_file_2 = ImageXObject {
//! width: 200,
//! height: 200,
//! width: Px(200),
//! height: Px(200),
//! color_space: ColorSpace::Greyscale,
//! bits_per_component: ColorBits::Bit8,
//! interpolate: true,
Expand All @@ -194,7 +194,7 @@
//! use printpdf::*;
//! use std::fs::File;
//!
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", 247.0, 210.0, "Layer 1");
//! let (doc, page1, layer1) = PdfDocument::new("PDF_Document_title", Mm(247.0), Mm(210.0), "Layer 1");
//! let current_layer = doc.get_page(page1).get_layer(layer1);
//!
//! let text = "Lorem ipsum";
Expand All @@ -204,7 +204,7 @@
//! let font2 = doc.add_external_font(File::open("assets/fonts/RobotoMedium.ttf").unwrap()).unwrap();
//!
//! // text, font size, x from left edge, y from top edge, font
//! current_layer.use_text(text, 48, 200.0, 200.0, &font);
//! current_layer.use_text(text, 48, Mm(200.0), Mm(200.0), &font);
//!
//! // For more complex layout of text, you can use functions
//! // defined on the PdfLayerReference
Expand All @@ -215,7 +215,7 @@
//! // setup the general fonts.
//! // see the docs for these functions for details
//! current_layer.set_font(&font2, 33);
//! current_layer.set_text_cursor(10.0, 10.0);
//! current_layer.set_text_cursor(Mm(10.0), Mm(10.0));
//! current_layer.set_line_height(33);
//! current_layer.set_word_spacing(3000);
//! current_layer.set_character_spacing(10);
Expand Down Expand Up @@ -371,6 +371,7 @@ extern crate rand;
pub extern crate image;

pub mod types;
pub mod scale;
pub mod errors;
mod glob_defines;
mod indices;
Expand All @@ -383,6 +384,7 @@ pub use self::errors::pdf_error::ErrorKind as PdfErrorKind;
pub use self::errors::index_error::Error as IndexError;
pub use self::errors::index_error::ErrorKind as IndexErrorKind;

pub use self::scale::{Mm, Pt, Px};
pub use self::types::pdf_conformance::{CustomPdfConformance, PdfConformance};
pub use self::types::pdf_document::{PdfDocumentReference, PdfDocument};
pub use self::types::pdf_metadata::PdfMetadata;
Expand Down
Loading

0 comments on commit 6ff9138

Please sign in to comment.