Skip to content

Commit 597997d

Browse files
committed
Implement first prototype of text embedding
1 parent 97274ce commit 597997d

386 files changed

Lines changed: 2461 additions & 1858 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ oxipng = { version = "9", default-features = false, features = ["filetime", "par
2525
pdf-writer = "0.9"
2626
pdfium-render = "0.8.6"
2727
termcolor = "1.2"
28-
usvg = { version = "0.40", default-features = false, features = ["text"] }
28+
usvg = { path = "../resvg/crates/usvg", default-features = false, features = ["text"] }
2929
tiny-skia = "0.11.4"
30-
resvg = "0.40"
30+
owned_ttf_parser = "0.20.0"
31+
unicode-properties = "0.1.1"
32+
resvg = {path = "../resvg/crates/resvg"}
33+
subsetter = "0.1.1"
3134

3235
[package]
3336
name = "svg2pdf"
@@ -49,11 +52,14 @@ image = ["dep:image"]
4952
filters = ["image", "dep:tiny-skia", "dep:resvg"]
5053

5154
[dependencies]
55+
unicode-properties = { workspace = true }
5256
miniz_oxide = { workspace = true }
5357
once_cell = { workspace = true }
5458
pdf-writer = { workspace = true }
5559
usvg = { workspace = true }
60+
owned_ttf_parser = { workspace = true }
5661
log = { workspace = true }
5762
image = { workspace = true, optional = true }
5863
tiny-skia = {workspace = true, optional = true }
5964
resvg = {workspace = true, optional = true }
65+
subsetter = { workspace = true }

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ miniz_oxide = { workspace = true }
2727
pdf-writer = { workspace = true }
2828
svg2pdf = { workspace = true }
2929
termcolor = { workspace = true }
30+
ttf-parser = "0.20.0"
3031
usvg = { workspace = true }
3132

3233
[features]

cli/src/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn convert_(
3737
let tree =
3838
usvg::Tree::from_str(&svg, &options, &fontdb).map_err(|err| err.to_string())?;
3939

40-
let pdf = svg2pdf::convert_tree(&tree, Options { dpi, ..Options::default() });
40+
let pdf = svg2pdf::convert_tree(&tree, Options { dpi, ..Options::default() }, fontdb);
4141

4242
std::fs::write(output, pdf).map_err(|_| "Failed to write PDF file")?;
4343

src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ mod util;
5858

5959
pub use usvg;
6060

61+
use crate::render::text::write_font;
6162
use once_cell::sync::Lazy;
6263
use pdf_writer::{Chunk, Content, Filter, Finish, Pdf, Rect, Ref, TextStr};
6364
use usvg::{fontdb, Align, AspectRatio, NonZeroRect, Size, Transform, Tree, ViewBox};
@@ -154,7 +155,7 @@ pub fn convert_str(
154155
usvg_options.default_size = size;
155156
}
156157
let tree = Tree::from_str(src, &usvg_options, fontdb)?;
157-
Ok(convert_tree(&tree, options))
158+
Ok(convert_tree(&tree, options, fontdb.clone()))
158159
}
159160

160161
/// Convert a [`usvg` tree](Tree) into a standalone PDF buffer.
@@ -182,9 +183,9 @@ pub fn convert_str(
182183
/// std::fs::write(output, pdf)?;
183184
/// # Ok(()) }
184185
/// ```
185-
pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
186+
pub fn convert_tree(tree: &Tree, options: Options, fontdb: fontdb::Database) -> Vec<u8> {
186187
let pdf_size = pdf_size(tree, options);
187-
let mut ctx = Context::new(tree, options, None);
188+
let mut ctx = Context::new(tree, options, None, fontdb);
188189
let mut pdf = Pdf::new();
189190

190191
let catalog_ref = ctx.alloc_ref();
@@ -229,6 +230,11 @@ pub fn convert_tree(tree: &Tree, options: Options) -> Vec<u8> {
229230
page.contents(content_ref);
230231
page.finish();
231232

233+
let font_ids = ctx.fonts.keys().copied().collect::<Vec<_>>();
234+
for font_id in font_ids {
235+
write_font(&mut pdf, &mut ctx, font_id).unwrap();
236+
}
237+
232238
write_color_spaces(&mut ctx, &mut pdf);
233239

234240
let document_info_id = ctx.alloc_ref();
@@ -334,9 +340,10 @@ pub fn convert_tree_into(
334340
options: Options,
335341
chunk: &mut Chunk,
336342
start_ref: Ref,
343+
fontdb: fontdb::Database,
337344
) -> Ref {
338345
let pdf_size = pdf_size(tree, options);
339-
let mut ctx = Context::new(tree, options, Some(start_ref.get()));
346+
let mut ctx = Context::new(tree, options, Some(start_ref.get()), fontdb);
340347

341348
let x_ref = ctx.alloc_ref();
342349
ctx.deferrer.push();

src/render/image.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ fn create_svg_image(
216216
let image_name = ctx.deferrer.add_x_object(image_ref);
217217
// convert_tree_into will automatically scale it in a way so that its dimensions are 1x1, like
218218
// regular ImageXObjects. So afterwards, we can just treat them the same.
219-
let next_ref = convert_tree_into(tree, Options::default(), chunk, image_ref);
219+
let next_ref =
220+
convert_tree_into(tree, Options::default(), chunk, image_ref, ctx.fontdb.clone());
220221
ctx.deferrer.set_next_ref(next_ref.get());
221222
(image_name, tree.size())
222223
}

src/render/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod image;
1414
pub mod mask;
1515
pub mod path;
1616
pub mod pattern;
17+
pub mod text;
1718

1819
/// Write a tree into a stream. Assumes that the stream belongs to transparency group and has the
1920
/// right bounding boxes.
@@ -71,14 +72,15 @@ impl Render for Node {
7172
log::warn!("Images have been disabled in this build of svg2pdf.")
7273
}
7374
Node::Text(ref text) => {
74-
group::render(
75-
text.flattened(),
76-
chunk,
77-
content,
78-
ctx,
79-
accumulated_transform,
80-
None,
81-
);
75+
text::render(text, chunk, content, ctx, accumulated_transform);
76+
// group::render(
77+
// text.flattened(),
78+
// chunk,
79+
// content,
80+
// ctx,
81+
// accumulated_transform,
82+
// None,
83+
// );
8284
}
8385
}
8486
}

0 commit comments

Comments
 (0)