From 849a0ea4726808ebb9ae2baecd37dd2bf35963a6 Mon Sep 17 00:00:00 2001 From: LeRoyce Pearson Date: Sun, 16 Sep 2018 19:11:36 -0500 Subject: [PATCH] Render images to PDF - Add in logging. - Update CHANGELOG - Reduce amount of extra space vertical used I had `line_spacing` set to 1.75 because I was going to use the program for proof-reading. That's fine, but because of how the program internals work, setting `line_spacing` to anything other than 1.0 causes pictures to take up and inordinate amount of space. I'll make it configurable from the command line later. - Added some vertical space beneath headers, so that the characters wouldn't collide with regular text. - Update to latest version of `printpdf` --- CHANGELOG.md | 1 + Cargo.lock | 143 +++++++++++++++-------------------------------- Cargo.toml | 2 + src/lib.rs | 30 +++++++++- src/pages.rs | 1 + src/resources.rs | 37 ++++++++++++ src/sectioner.rs | 35 +++++++++++- src/span.rs | 24 +++++++- src/util.rs | 6 ++ 9 files changed, 174 insertions(+), 105 deletions(-) create mode 100644 src/resources.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 90f6364..96d0292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Rendering of block quotes - Page breaks when an html element contains `style="page-break-after:always"` - Stdin is used as input when `-` is passed to `mdproof` +- Rendering of images diff --git a/Cargo.lock b/Cargo.lock index 27f6058..7ddd897 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,7 +24,7 @@ name = "approx" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -62,7 +62,7 @@ name = "backtrace-sys" version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -83,7 +83,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cc" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -91,25 +91,6 @@ name = "cfg-if" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "chrono" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "chrono" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "clap" version = "2.32.0" @@ -306,7 +287,7 @@ dependencies = [ "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "termcolor 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -421,7 +402,7 @@ dependencies = [ "num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", "num-rational 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -489,16 +470,16 @@ dependencies = [ [[package]] name = "lopdf" -version = "0.15.2" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "pom 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -518,8 +499,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "phf 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)", "phf_codegen 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_codegen 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -536,10 +517,12 @@ name = "mdproof" version = "0.1.0" dependencies = [ "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "printpdf 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "printpdf 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "quicli 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rusttype 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rusttype 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "scraper 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -561,7 +544,7 @@ name = "miniz-sys" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cc 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -578,22 +561,12 @@ name = "nodrop" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "num" -version = "0.1.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "num-derive" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -604,7 +577,7 @@ name = "num-integer" version = "0.1.39" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -613,7 +586,7 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -622,20 +595,12 @@ version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-traits" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num-traits" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -648,11 +613,10 @@ dependencies = [ [[package]] name = "ordered-float" -version = "0.5.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", - "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -712,15 +676,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "printpdf" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lopdf 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rusttype 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lopdf 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rusttype 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -770,8 +732,8 @@ dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "structopt-derive 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -870,20 +832,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "rusttype" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rusttype 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rusttype" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "approx 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "ordered-float 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "stb_truetype 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -935,17 +889,17 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.78" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.78" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -955,7 +909,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1002,7 +956,7 @@ dependencies = [ "new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "phf_shared 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)", "precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_codegen 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1070,7 +1024,7 @@ dependencies = [ [[package]] name = "syn" -version = "0.15.3" +version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1101,7 +1055,7 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1239,10 +1193,8 @@ dependencies = [ "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781" -"checksum cc 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "70f2a88c2e69ceee91c209d8ef25b81fc1a65f42c7f14dfd59d1fed189e514d1" +"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16" "checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" -"checksum chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "158b0bd7d75cbb6bf9c25967a48a2e9f77da95876b858eadfabaa99cd069de6e" -"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum clap-verbosity-flag 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26de1a8b4a21f6b05c99680edd48a6fded30afcf7aa13f1fa7e5c79198f5bac7" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" @@ -1287,7 +1239,7 @@ dependencies = [ "checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f" -"checksum lopdf 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e4777bc707e21e9f1a3aed4da3b3a711a7eb468963df3c077fe48409993c1ae9" +"checksum lopdf 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43734e7a4dc238936a6867a27eefbd828befd81ab357ed64cac8ad4b3af901de" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" "checksum mac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" "checksum markup5ever 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfedc97d5a503e96816d10fedcd5b42f760b2e525ce2f7ec71f6a41780548475" @@ -1297,15 +1249,13 @@ dependencies = [ "checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4" "checksum new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0cdc457076c78ab54d5e0d6fa7c47981757f1e34dc39ff92787f217dede586c4" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" -"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" "checksum num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d2c31b75c36a993d30c7a13d70513cb93f02acafdd5b7ba250f9b0e18615de7" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124" "checksum num-rational 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e" -"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" +"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" -"checksum ordered-float 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7004d4db808c6db5a9f54ec5c85918ad01a1ef9b35bf4dfa1e2ccba28de0c1b4" +"checksum ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f0015e9e8e28ee20c581cfbfe47c650cedeb9ed0721090e0b7ebb10b9cdbcc2" "checksum phf 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)" = "cec29da322b242f4c3098852c77a0ca261c9c01b806cae85a5572a1eb94db9a6" "checksum phf_codegen 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)" = "7d187f00cd98d5afbcd8898f6cf181743a449162aeb329dcd2f3849009e605ad" "checksum phf_generator 0.7.23 (registry+https://github.com/rust-lang/crates.io-index)" = "03dc191feb9b08b0dc1330d6549b795b9d81aec19efe6b4a45aec8d4caee0c4b" @@ -1313,7 +1263,7 @@ dependencies = [ "checksum png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f54b9600d584d3b8a739e1662a595fab051329eff43f20e7d8cc22872962145b" "checksum pom 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60f6ce597ecdcc9a098e7fddacb1065093a3d66446fa16c675e7e71d1b5c28e6" "checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" -"checksum printpdf 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "97ef4fb60df732b4b71ce685a2b05eaf001a69f5fff1bdea98ad363bb29c7c1c" +"checksum printpdf 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c9b5acd3f7041f24ae2abfd261ffde21495ce66c581730a8d8ead25ced2a3f2" "checksum proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7" "checksum proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "ffe022fb8c8bd254524b0b3305906c1921fa37a84a644e29079a9e62200c3901" "checksum procedural-masquerade 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9a1574a51c3fd37b26d2c0032b649d08a7d51d4cca9c41bbc5bf7118fa4509d0" @@ -1331,15 +1281,14 @@ dependencies = [ "checksum regex 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2069749032ea3ec200ca51e4a31df41759190a88edca0d2d86ee8bedf7073341" "checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" -"checksum rusttype 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "320899ed15d373f4bad3c1288da0a98dd50cef5c0b2e8d21c84d327a585917c5" -"checksum rusttype 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9415443fe2539a24aed195d66a2b03643c54e14bd6f68c07ae2e2f6c2a5b8d25" +"checksum rusttype 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b8eb11f5b0a98c8eca2fb1483f42646d8c340e83e46ab416f8a063a0fd0eeb20" "checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scraper 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3c96f4a74b2c58e2260c199478d7b28e4fd2956830cecb8dd1bc6dae6da6c3" "checksum selectors 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4c81ca1c531d2774be3fe90ed5dab4b2f88ca99b7bdf6a511d212fa44adf9dde" -"checksum serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)" = "92ec94e2754699adddbbc4f555791bd3acc2a2f5574cba16c93a4a9cf4a04415" -"checksum serde_derive 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)" = "0fb622d85245add5327d4f08b2d24fd51fa5d35fe1bba19ee79a1f211e9ac0ff" +"checksum serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "84257ccd054dc351472528c8587b4de2dbf0dc0fe2e634030c1a90bfdacebaa9" +"checksum serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "31569d901045afbff7a9479f793177fe9259819aff10ab4f89ef69bbc5f567fe" "checksum serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "59790990c5115d16027f00913e2e66de23a51f70422e549d2ad68c8c5f268f1c" "checksum servo_arc 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d98238b800e0d1576d8b6e3de32827c2d74bee68bb97748dcf5071fb53965432" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" @@ -1354,10 +1303,10 @@ dependencies = [ "checksum structopt-derive 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4cbce8ccdc62166bd594c14396a3242bf94c337a51dbfa9be1076dd74b3db2af" "checksum syn 0.13.11 (registry+https://github.com/rust-lang/crates.io-index)" = "14f9bf6292f3a61d2c716723fdb789a41bbe104168e6f496dc6497e531ea1b9b" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" -"checksum syn 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e5c1514eb7bb4216fc722b3cd08783d326d7de0d62f6d5e48a774f610bc97cb6" +"checksum syn 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9056ebe7f2d6a38bc63171816fd1d3430da5a43896de21676dc5c0a4b8274a11" "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" "checksum tendril 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9de21546595a0873061940d994bbbc5c35f024ae4fd61ec5c5b159115684f508" -"checksum termcolor 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ff3bac0e465b59f194e7037ed404b0326e56ff234d767edc4c5cc9cd49e7a2c7" +"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" diff --git a/Cargo.toml b/Cargo.toml index 94a11cf..3cf021b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,5 @@ printpdf = "*" rusttype = "*" failure = "*" scraper = "*" +image = "*" +log = "*" diff --git a/src/lib.rs b/src/lib.rs index dd8a241..0b6f2f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,16 @@ #[macro_use] extern crate failure; +extern crate image; extern crate printpdf; extern crate pulldown_cmark as cmark; extern crate rusttype; extern crate scraper; +#[macro_use] +extern crate log; mod page; mod pages; +mod resources; mod section; mod sectioner; mod span; @@ -14,12 +18,14 @@ mod util; use cmark::*; use failure::Error; -use printpdf::{Mm, PdfDocument, PdfDocumentReference}; +use printpdf::{Image, Mm, PdfDocument, PdfDocumentReference}; use rusttype::{Font, Scale}; use pages::Pages; +use resources::Resources; use sectioner::Sectioner; use span::Span; +use std::path::PathBuf; const REGULAR_FONT: &[u8] = include_bytes!("../assets/Noto_Sans/NotoSans-Regular.ttf"); const BOLD_FONT: &[u8] = include_bytes!("../assets/Noto_Sans/NotoSans-Bold.ttf"); @@ -29,6 +35,9 @@ const MONO_FONT: &[u8] = include_bytes!("../assets/Inconsolata/Inconsolata-Regul #[derive(Debug)] pub struct Config { + /// The path from which images will be loaded + pub resources_directory: PathBuf, + pub title: String, pub first_layer_name: String, @@ -72,6 +81,8 @@ impl Config { impl Default for Config { fn default() -> Self { Config { + resources_directory: PathBuf::new(), + title: "mdproof".into(), first_layer_name: "Layer 1".into(), @@ -89,7 +100,7 @@ impl Default for Config { h3_font_size: Scale::uniform(20.0), h4_font_size: Scale::uniform(16.0), - line_spacing: 1.75, // Text height * LINE_SPACING + line_spacing: 1.0, // Text height * LINE_SPACING list_indentation: Mm(10.0), list_point_offset: Mm(5.0), quote_indentation: Mm(20.0), @@ -110,10 +121,11 @@ pub fn markdown_to_pdf(markdown: &str, cfg: &Config) -> Result Result { + let image = Image::try_from_image(resources.load_image(path)?)?; + image.add_to_layer( + current_layer.clone(), + Some(span.pos.0), + Some(span.pos.1), + None, + None, + None, + None, + ); + } Span::Rect { width, height } => { use printpdf::{Line, Point}; let rect_points = vec![ diff --git a/src/pages.rs b/src/pages.rs index 656b570..2f2b748 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -30,6 +30,7 @@ impl<'collection> Pages<'collection> { pub fn render_sections(&mut self, sections: &[Section<'collection>], start_x: Mm) { let min_y = self.cfg.margin.1; for section in sections { + trace!("rendering section: {:?}", section); let height = section.min_step(); let delta_y = height * -self.cfg.line_spacing; if self.current_y + delta_y < min_y { diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..337840c --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,37 @@ +use failure::Error; +use image::{self, DynamicImage}; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +pub struct Resources { + root_path: PathBuf, + images: BTreeMap, +} + +impl Resources { + pub fn new(root_path: PathBuf) -> Self { + Self { + root_path: root_path, + images: BTreeMap::new(), + } + } + + pub fn load_image>(&mut self, path: P) -> Result<&DynamicImage, Error> { + let filename = self.root_path.join(path); + if self.images.contains_key(&filename) { + let image = self + .images + .get(&filename) + .expect("BTreeMap said it contained the key"); + Ok(image) + } else { + let image = image::open(&filename)?; + self.images.insert(filename.clone(), image); + let image = self + .images + .get(&filename) + .expect("I just inserted the key into the map"); + Ok(image) + } + } +} diff --git a/src/sectioner.rs b/src/sectioner.rs index 2b40a10..e409cd3 100644 --- a/src/sectioner.rs +++ b/src/sectioner.rs @@ -1,6 +1,8 @@ use super::Config; use cmark::{Event, Tag}; +use image::GenericImage; use printpdf::Mm; +use resources::Resources; use rusttype::Scale; use section::Section; use span::{FontType, Span}; @@ -21,6 +23,7 @@ pub struct Sectioner<'collection> { max_width: Mm, subsection: Option>>, pub is_code: bool, + is_alt_text: bool, cfg: &'collection Config, } @@ -36,17 +39,22 @@ impl<'collection> Sectioner<'collection> { max_width: max_width, subsection: None, is_code: false, + is_alt_text: false, cfg: cfg, } } - pub fn parse_event(&mut self, event: Event) -> Option { + pub fn parse_event( + &mut self, + resources: &mut Resources, + event: Event, + ) -> Option { if self.subsection.is_some() { let mut subsection = self .subsection .take() .expect("Checked if the subsection was `Some`"); - if let Some(sub_type) = subsection.parse_event(event) { + if let Some(sub_type) = subsection.parse_event(resources, event) { let section = match sub_type { SubsectionType::List => Section::list_item(subsection.get_vec()), SubsectionType::Quote => Section::block_quote(subsection.get_vec()), @@ -77,6 +85,7 @@ impl<'collection> Sectioner<'collection> { Event::End(Tag::Header(_)) => { self.current_scale = self.cfg.default_font_size; self.new_line(); + self.push_space(); } Event::Start(Tag::List(_)) => self.new_line(), @@ -99,6 +108,8 @@ impl<'collection> Sectioner<'collection> { } Event::End(Tag::BlockQuote) => return Some(SubsectionType::Quote), + Event::Text(ref _text) if self.is_alt_text => {} + Event::Text(ref text) if self.is_code => { let mut start = 0; for (pos, c) in text.char_indices() { @@ -129,6 +140,26 @@ impl<'collection> Sectioner<'collection> { } } + Event::Start(Tag::Image(url, _title)) => { + // TODO: Use title, and ignore alt-text + // Or should alt-text always be used? + if let Ok(image) = resources.load_image(url.clone().into_owned()) { + let (w, h) = image.dimensions(); + let (w, h) = ( + ::printpdf::Px(w as usize).into_pt(300.0).into(), + ::printpdf::Px(h as usize).into_pt(300.0).into(), + ); + let span = Span::image(w, h, url.into_owned().into()); + self.push_span(span); + self.is_alt_text = true; + } else { + warn!("Couldn't load image: {:?}", url); + } + } + Event::End(Tag::Image(_url, _title)) => { + self.is_alt_text = false; + } + Event::Start(Tag::Code) => self.current_font_type = self.current_font_type.mono(), Event::End(Tag::Code) => self.current_font_type = self.current_font_type.unmono(), diff --git a/src/span.rs b/src/span.rs index 6019895..bd9c649 100644 --- a/src/span.rs +++ b/src/span.rs @@ -1,6 +1,7 @@ -use printpdf::{Mm, Pt}; +use printpdf::Mm; use rusttype::{Font, Scale}; -use util::width_of_text; +use std::path::PathBuf; +use util::{font_height, width_of_text}; #[derive(PartialEq, Copy, Clone, Debug)] pub enum FontType { @@ -65,6 +66,11 @@ pub enum Span<'collection> { font_type: FontType, font_scale: Scale, }, + Image { + width: Mm, + height: Mm, + path: PathBuf, + }, Rect { width: Mm, height: Mm, @@ -86,6 +92,14 @@ impl<'collection> Span<'collection> { } } + pub fn image(width: Mm, height: Mm, path: PathBuf) -> Self { + Span::Image { + width, + height, + path, + } + } + pub fn rect(width: Mm, height: Mm) -> Self { Span::Rect { width, height } } @@ -98,13 +112,17 @@ impl<'collection> Span<'collection> { font_scale, .. } => width_of_text(&text, &font, *font_scale).into(), + Span::Image { width, .. } => width.clone(), Span::Rect { width, .. } => width.clone(), } } pub fn height(&self) -> Mm { match self { - Span::Text { font_scale, .. } => Pt(font_scale.y as f64).into(), + Span::Text { + font, font_scale, .. + } => font_height(font, *font_scale).into(), + Span::Image { height, .. } => height.clone(), Span::Rect { height, .. } => height.clone(), } } diff --git a/src/util.rs b/src/util.rs index 2a62585..482b930 100644 --- a/src/util.rs +++ b/src/util.rs @@ -13,3 +13,9 @@ pub fn width_of_text(text: &str, font: &Font, scale: Scale) -> Pt { }).sum(); Pt(glyph_space_width * scale.x as f64 / units_per_em) } + +pub fn font_height(font: &Font, scale: Scale) -> Pt { + let v_metrics = font.v_metrics(scale); + let height = (v_metrics.ascent - v_metrics.descent + v_metrics.line_gap) as f64; + Pt(height) +}