@@ -16,17 +16,17 @@ This example reads an SVG file and writes the corresponding PDF back to the disk
1616# fn main() -> Result<(), Box<dyn std::error::Error>> {
1717use svg2pdf::usvg::fontdb;
1818use svg2pdf::{ConversionOptions, PageOptions};
19+ use std::sync::Arc;
1920
2021let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
2122let output = "target/stairs.pdf";
2223
2324let svg = std::fs::read_to_string(input)?;
24- let options = svg2pdf::usvg::Options::default();
25- let mut db = fontdb::Database::new();
26- db.load_system_fonts();
27- let tree = svg2pdf::usvg::Tree::from_str(&svg, &options, &db)?;
25+ let mut options = svg2pdf::usvg::Options::default();
26+ options.fontdb_mut().load_system_fonts();
27+ let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
2828
29- let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default(), &db );
29+ let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default());
3030std::fs::write(output, pdf)?;
3131# Ok(()) }
3232```
@@ -60,8 +60,6 @@ pub use usvg;
6060
6161use once_cell:: sync:: Lazy ;
6262use pdf_writer:: { Chunk , Content , Filter , Finish , Pdf , Ref , TextStr } ;
63- #[ cfg( feature = "text" ) ]
64- use usvg:: fontdb;
6563use usvg:: { Size , Transform , Tree } ;
6664
6765use crate :: render:: { tree_to_stream, tree_to_xobject} ;
@@ -120,7 +118,7 @@ pub struct ConversionOptions {
120118impl Default for ConversionOptions {
121119 fn default ( ) -> Self {
122120 Self {
123- compress : false ,
121+ compress : true ,
124122 raster_scale : 1.5 ,
125123 embed_text : true ,
126124 }
@@ -129,9 +127,6 @@ impl Default for ConversionOptions {
129127
130128/// Convert a [`usvg` tree](Tree) into a standalone PDF buffer.
131129///
132- /// IMPORTANT: The fontdb that is passed to this function needs to be the
133- /// same one that was used to convert the SVG string into a [`usvg` tree](Tree)!
134- ///
135130/// ## Example
136131/// The example below reads an SVG file, processes text within it, then converts
137132/// it into a PDF and finally writes it back to the file system.
@@ -140,34 +135,27 @@ impl Default for ConversionOptions {
140135/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
141136/// use svg2pdf::usvg::fontdb;
142137/// use svg2pdf::{ConversionOptions, PageOptions};
138+ /// use std::sync::Arc;
143139///
144140/// let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
145141/// let output = "target/stairs.pdf";
146142///
147143/// let svg = std::fs::read_to_string(input)?;
148- /// let options = svg2pdf::usvg::Options::default();
149- /// let mut db = fontdb::Database::new();
150- /// db.load_system_fonts();
151- /// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options, &db)?;
144+ /// let mut options = svg2pdf::usvg::Options::default();
145+ /// options.fontdb_mut().load_system_fonts();
146+ /// let mut tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;
152147///
153148///
154- /// let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default(), &db );
149+ /// let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default());
155150/// std::fs::write(output, pdf)?;
156151/// # Ok(()) }
157152/// ```
158153pub fn to_pdf (
159154 tree : & Tree ,
160155 conversion_options : ConversionOptions ,
161156 page_options : PageOptions ,
162- #[ cfg( feature = "text" ) ] fontdb : & fontdb:: Database ,
163157) -> Vec < u8 > {
164- let mut ctx = Context :: new (
165- #[ cfg( feature = "text" ) ]
166- tree,
167- conversion_options,
168- #[ cfg( feature = "text" ) ]
169- fontdb,
170- ) ;
158+ let mut ctx = Context :: new ( tree, conversion_options) ;
171159 let mut pdf = Pdf :: new ( ) ;
172160
173161 let dpi_ratio = 72.0 / page_options. dpi ;
@@ -225,9 +213,6 @@ pub fn to_pdf(
225213
226214/// Convert a [Tree] into a [`Chunk`].
227215///
228- /// IMPORTANT: The fontdb that is passed to this function needs to be the
229- /// same one that was used to convert the SVG string into a [`usvg` tree](Tree)!
230- ///
231216/// This method is intended for use in an existing [`pdf-writer`] workflow. It
232217/// will always produce a chunk that contains all the necessary objects
233218/// to embed the SVG into an existing chunk. This method returns the chunk that
@@ -245,6 +230,7 @@ pub fn to_pdf(
245230/// ```
246231/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
247232/// use std::collections::HashMap;
233+ /// use std::sync::Arc;
248234/// use svg2pdf;
249235/// use pdf_writer::{Content, Finish, Name, Pdf, Rect, Ref, Str};
250236/// use svg2pdf::usvg::fontdb;
@@ -262,10 +248,10 @@ pub fn to_pdf(
262248/// // Let's first convert the SVG into an independent chunk.
263249/// let path = "tests/svg/custom/integration/wikimedia/coat_of_the_arms_of_edinburgh_city_council.svg";
264250/// let svg = std::fs::read_to_string(path)?;
265- /// let mut db = fontdb::Database::new ();
266- /// db .load_system_fonts();
267- /// let tree = svg2pdf::usvg::Tree::from_str(&svg, &svg2pdf::usvg::Options::default(), &db )?;
268- /// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default(), &db );
251+ /// let mut options = svg2pdf::usvg::Options::default ();
252+ /// options.fontdb_mut() .load_system_fonts();
253+ /// let tree = svg2pdf::usvg::Tree::from_str(&svg, &options )?;
254+ /// let (mut svg_chunk, svg_id) = svg2pdf::to_chunk(&tree, svg2pdf::ConversionOptions::default());
269255///
270256/// // Renumber the chunk so that we can embed it into our existing workflow, and also make sure
271257/// // to update `svg_id`.
@@ -320,20 +306,10 @@ pub fn to_pdf(
320306/// std::fs::write("target/embedded.pdf", pdf.finish())?;
321307/// # Ok(()) }
322308/// ```
323- pub fn to_chunk (
324- tree : & Tree ,
325- conversion_options : ConversionOptions ,
326- #[ cfg( feature = "text" ) ] fontdb : & fontdb:: Database ,
327- ) -> ( Chunk , Ref ) {
309+ pub fn to_chunk ( tree : & Tree , conversion_options : ConversionOptions ) -> ( Chunk , Ref ) {
328310 let mut chunk = Chunk :: new ( ) ;
329311
330- let mut ctx = Context :: new (
331- #[ cfg( feature = "text" ) ]
332- tree,
333- conversion_options,
334- #[ cfg( feature = "text" ) ]
335- fontdb,
336- ) ;
312+ let mut ctx = Context :: new ( tree, conversion_options) ;
337313 let x_ref = tree_to_xobject ( tree, & mut chunk, & mut ctx) ;
338314 ctx. write_global_objects ( & mut chunk) ;
339315 ( chunk, x_ref)
0 commit comments