From cac7b9f5dabf2da7ba01ec4f5f33c177ded28e9c Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl Date: Sat, 1 Jun 2024 15:34:59 +0200 Subject: [PATCH] Some more nits --- src/lib.rs | 30 ++++++------------------------ tests/src/api.rs | 3 +-- tests/src/lib.rs | 29 +++++++++++++---------------- 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fc89ab04..fd48a9e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,12 +22,8 @@ let input = "tests/svg/custom/integration/matplotlib/stairs.svg"; let output = "target/stairs.pdf"; let svg = std::fs::read_to_string(input)?; -let mut db = fontdb::Database::new(); -db.load_system_fonts(); -let options = svg2pdf::usvg::Options { - fontdb: Arc::new(db), - ..svg2pdf::usvg::Options::default() -}; +let mut options = svg2pdf::usvg::Options::default(); +options.fontdb_mut().load_system_fonts(); let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?; let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default()); @@ -131,9 +127,6 @@ impl Default for ConversionOptions { /// Convert a [`usvg` tree](Tree) into a standalone PDF buffer. /// -/// IMPORTANT: The fontdb that is passed to this function needs to be the -/// same one that was used to convert the SVG string into a [`usvg` tree](Tree)! -/// /// ## Example /// The example below reads an SVG file, processes text within it, then converts /// it into a PDF and finally writes it back to the file system. @@ -148,12 +141,8 @@ impl Default for ConversionOptions { /// let output = "target/stairs.pdf"; /// /// let svg = std::fs::read_to_string(input)?; -/// let mut db = fontdb::Database::new(); -/// db.load_system_fonts(); -/// let options = svg2pdf::usvg::Options { -/// fontdb: Arc::new(db), -/// ..svg2pdf::usvg::Options::default() -/// }; +/// let mut options = svg2pdf::usvg::Options::default(); +/// options.fontdb_mut().load_system_fonts(); /// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?; /// /// @@ -224,9 +213,6 @@ pub fn to_pdf( /// Convert a [Tree] into a [`Chunk`]. /// -/// IMPORTANT: The fontdb that is passed to this function needs to be the -/// same one that was used to convert the SVG string into a [`usvg` tree](Tree)! -/// /// This method is intended for use in an existing [`pdf-writer`] workflow. It /// will always produce a chunk that contains all the necessary objects /// to embed the SVG into an existing chunk. This method returns the chunk that @@ -262,12 +248,8 @@ pub fn to_pdf( /// // Let's first convert the SVG into an independent chunk. /// let path = "tests/svg/custom/integration/wikimedia/coat_of_the_arms_of_edinburgh_city_council.svg"; /// let svg = std::fs::read_to_string(path)?; -/// let mut db = fontdb::Database::new(); -/// db.load_system_fonts(); -/// let options = svg2pdf::usvg::Options { -/// fontdb: Arc::new(db), -/// ..svg2pdf::usvg::Options::default() -/// }; +/// let mut options = svg2pdf::usvg::Options::default(); +/// options.fontdb_mut().load_system_fonts(); /// let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?; /// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default()); /// diff --git a/tests/src/api.rs b/tests/src/api.rs index 6aa62f40..f9e3b7c6 100644 --- a/tests/src/api.rs +++ b/tests/src/api.rs @@ -47,8 +47,7 @@ fn to_chunk() { let path = "svg/custom/integration/wikimedia/coat_of_the_arms_of_edinburgh_city_council.svg"; let svg = std::fs::read_to_string(path).unwrap(); - let mut options = svg2pdf::usvg::Options::default(); - options.fontdb = FONTDB.clone(); + let options = usvg::Options { fontdb: FONTDB.clone(), ..usvg::Options::default() }; let tree = svg2pdf::usvg::Tree::from_str(&svg, &options).unwrap(); let (svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default()); diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 63beeb02..cde6f6de 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -17,7 +17,18 @@ use usvg::Tree; use svg2pdf::{ConversionOptions, PageOptions}; -static FONTDB: Lazy> = Lazy::new(|| create_fontdb()); +static FONTDB: Lazy> = Lazy::new(|| { + let mut fontdb = fontdb::Database::new(); + fontdb.load_fonts_dir("fonts"); + + fontdb.set_serif_family("Noto Serif"); + fontdb.set_sans_serif_family("Noto Sans"); + fontdb.set_cursive_family("Yellowtail"); + fontdb.set_fantasy_family("Sedgwick Ave Display"); + fontdb.set_monospace_family("Noto Mono"); + + Arc::new(fontdb) +}); /// The global pdfium instance. static PDFIUM: Lazy> = Lazy::new(|| { @@ -51,23 +62,9 @@ pub fn render_pdf(pdf: &[u8]) -> RgbaImage { result } -pub fn create_fontdb() -> Arc { - let mut fontdb = fontdb::Database::new(); - fontdb.load_fonts_dir("fonts"); - - fontdb.set_serif_family("Noto Serif"); - fontdb.set_sans_serif_family("Noto Sans"); - fontdb.set_cursive_family("Yellowtail"); - fontdb.set_fantasy_family("Sedgwick Ave Display"); - fontdb.set_monospace_family("Noto Mono"); - - Arc::new(fontdb) -} - /// Converts an SVG string into a usvg Tree pub fn read_svg(svg_string: &str) -> Tree { - let mut options = usvg::Options::default(); - options.fontdb = FONTDB.clone(); + let options = usvg::Options { fontdb: FONTDB.clone(), ..usvg::Options::default() }; Tree::from_str(svg_string, &options).unwrap() }