Skip to content

Commit 7cb474e

Browse files
committed
Add embed text option
1 parent aa325ea commit 7cb474e

4 files changed

Lines changed: 56 additions & 34 deletions

File tree

src/lib.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ will still be turned into a vector graphic), so no quality is lost.
1313
This example reads an SVG file and writes the corresponding PDF back to the disk.
1414
1515
```
16-
# fn main() -> Result<(), Box<dyn std::error::Error>> {
17-
use svg2pdf::usvg::fontdb;
18-
use svg2pdf::Options;
19-
20-
let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
21-
let output = "target/stairs.pdf";
22-
23-
let 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)?;
28-
29-
let pdf = svg2pdf::to_pdf(&tree, Options::default(), &db);
30-
std::fs::write(output, pdf)?;
31-
# Ok(()) }
32-
```
16+
* # fn main() -> Result<(), Box<dyn std::error::Error>> {
17+
* use svg2pdf::usvg::fontdb;
18+
* use svg2pdf::ConversionOptions;
19+
*
20+
* let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
21+
* let output = "target/stairs.pdf";
22+
*
23+
* let 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)?;
28+
*
29+
* let pdf = svg2pdf::to_pdf(&tree, Options::default(), &db);
30+
* std::fs::write(output, pdf)?;
31+
* # Ok(()) }
32+
* ```
3333
3434
## Supported features
3535
In general, a very large part of the SVG specification is supported, including
@@ -80,8 +80,8 @@ static GRAY_ICC_DEFLATED: Lazy<Vec<u8>> =
8080
pub struct Options {
8181
/// Whether the content streams should be compressed.
8282
///
83-
/// The smaller PDFs generated by this are generally more practical but it
84-
/// increases runtime a bit.
83+
/// The smaller PDFs generated by this are generally more practical, but it
84+
/// might increase run-time a bit.
8585
///
8686
/// _Default:_ `true`.
8787
pub compress: bool,
@@ -93,11 +93,22 @@ pub struct Options {
9393
///
9494
/// _Default:_ 1
9595
pub raster_scale: f32,
96+
97+
/// Whether text should be embedded as actual selectable text inside
98+
/// the PDF. If this option is disabled, text will be converted into paths
99+
/// before rendering.
100+
///
101+
/// _Default:_ `true`.
102+
pub embed_text: bool,
96103
}
97104

98105
impl Default for Options {
99106
fn default() -> Self {
100-
Self { compress: false, raster_scale: 1.0 }
107+
Self {
108+
compress: false,
109+
raster_scale: 1.0,
110+
embed_text: true,
111+
}
101112
}
102113
}
103114

src/render/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,19 @@ impl Render for Node {
112112
}
113113
#[cfg(feature = "text")]
114114
Node::Text(ref text) => {
115-
text::render(text, chunk, content, ctx, rc, accumulated_transform);
116-
// group::render(
117-
// text.flattened(),
118-
// chunk,
119-
// content,
120-
// ctx,
121-
// accumulated_transform,
122-
// None,
123-
// );
115+
if ctx.options.embed_text {
116+
text::render(text, chunk, content, ctx, rc, accumulated_transform);
117+
} else {
118+
group::render(
119+
text.flattened(),
120+
chunk,
121+
content,
122+
ctx,
123+
accumulated_transform,
124+
None,
125+
rc,
126+
);
127+
}
124128
}
125129
#[cfg(not(feature = "text"))]
126130
Node::Text(_) => {

src/util/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ impl Context {
3838
sgray_ref: None,
3939
};
4040

41-
text::fill_fonts(tree.root(), &mut ctx, fontdb);
41+
if options.embed_text {
42+
text::fill_fonts(tree.root(), &mut ctx, fontdb);
43+
}
4244

4345
ctx
4446
}

tests/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ pub fn read_svg(svg_string: &str) -> Tree {
6969

7070
/// Converts an image into a PDF and returns the PDF as well as a rendered version
7171
/// of it.
72-
pub fn convert_svg(svg_string: &str) -> (Vec<u8>, RgbaImage) {
73-
let tree = read_svg(svg_string);
72+
pub fn convert_svg(svg_path: &Path) -> (Vec<u8>, RgbaImage) {
73+
let svg = fs::read_to_string(svg_path).unwrap();
74+
let tree = read_svg(&svg);
7475
let pdf = svg2pdf::to_pdf(
7576
&tree,
76-
Options { raster_scale: 1.5, compress: true },
77+
Options {
78+
raster_scale: 1.5,
79+
compress: true,
80+
embed_text: true,
81+
},
7782
&FONTDB.lock().unwrap(),
7883
);
7984
let image = render_pdf(pdf.as_slice());
@@ -169,7 +174,7 @@ pub fn run_test(test_name: &str) -> i32 {
169174
let ref_path = get_ref_path(test_name);
170175
let diff_path = get_diff_path(test_name);
171176

172-
let (_, actual_image) = convert_svg(&fs::read_to_string(svg_path).unwrap());
177+
let (_, actual_image) = convert_svg(&svg_path);
173178

174179
// Just as a convenience, if the test is supposed to run but there doesn't exist
175180
// a reference image yet, we create a new one. This allows us to conveniently generate

0 commit comments

Comments
 (0)