Skip to content

Commit

Permalink
Some more nits
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Jun 1, 2024
1 parent 388b452 commit cac7b9f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
30 changes: 6 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.
Expand All @@ -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)?;
///
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
///
Expand Down
3 changes: 1 addition & 2 deletions tests/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
29 changes: 13 additions & 16 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ use usvg::Tree;

use svg2pdf::{ConversionOptions, PageOptions};

static FONTDB: Lazy<Arc<fontdb::Database>> = Lazy::new(|| create_fontdb());
static FONTDB: Lazy<Arc<fontdb::Database>> = 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<std::sync::Mutex<Pdfium>> = Lazy::new(|| {
Expand Down Expand Up @@ -51,23 +62,9 @@ pub fn render_pdf(pdf: &[u8]) -> RgbaImage {
result
}

pub fn create_fontdb() -> Arc<fontdb::Database> {
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()
}

Expand Down

0 comments on commit cac7b9f

Please sign in to comment.