Skip to content

Commit

Permalink
Support text preserving in the usvg CLI tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV authored Dec 10, 2023
1 parent 7e1f99c commit 537dd26
Show file tree
Hide file tree
Showing 25 changed files with 562 additions and 10 deletions.
7 changes: 6 additions & 1 deletion crates/usvg-parser/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ fn resolve_text_flow(node: SvgNode, state: &converter::State) -> Option<TextFlow
node.resolve_length(AId::StartOffset, state, 0.0)
};

Some(TextFlow::Path(Rc::new(TextPath { start_offset, path })))
let id = linked_node.element_id().to_string();
Some(TextFlow::Path(Rc::new(TextPath {
id,
start_offset,
path,
})))
}

fn convert_font(node: SvgNode, state: &converter::State) -> Font {
Expand Down
6 changes: 5 additions & 1 deletion crates/usvg-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,11 @@ impl NodeExt for Node {

fn calc_node_bbox(node: &Node, ts: Transform) -> Option<BBox> {
match *node.borrow() {
NodeKind::Path(ref path) => path.data.compute_tight_bounds()?.transform(ts).map(BBox::from),
NodeKind::Path(ref path) => path
.data
.compute_tight_bounds()?
.transform(ts)
.map(BBox::from),
NodeKind::Image(ref img) => img.view_box.rect.transform(ts).map(BBox::from),
NodeKind::Group(_) => {
let mut bbox = BBox::default();
Expand Down
5 changes: 5 additions & 0 deletions crates/usvg-tree/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ impl Default for TextAnchor {
/// A path used by text-on-path.
#[derive(Clone, Debug)]
pub struct TextPath {
/// Element's ID.
///
/// Taken from the SVG itself.
pub id: String,

/// A text offset in SVG coordinates.
///
/// Percentage values already resolved.
Expand Down
7 changes: 6 additions & 1 deletion crates/usvg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ OPTIONS:
Refer to the explanation of the '--default-width'
option. [values: 1..4294967295 (inclusive)] [default: 100]
--preserve-text Do not convert text into paths.
--id-prefix Adds a prefix to each ID attribute
--indent INDENT Sets the XML nodes indent
[values: none, 0, 1, 2, 3, 4, tabs] [default: 4]
Expand Down Expand Up @@ -128,6 +129,7 @@ struct Args {
font_files: Vec<PathBuf>,
font_dirs: Vec<PathBuf>,
skip_system_fonts: bool,
preserve_text: bool,
list_fonts: bool,
default_width: u32,
default_height: u32,
Expand Down Expand Up @@ -187,6 +189,7 @@ fn collect_args() -> Result<Args, pico_args::Error> {
font_files: input.values_from_str("--use-font-file")?,
font_dirs: input.values_from_str("--use-fonts-dir")?,
skip_system_fonts: input.contains("--skip-system-fonts"),
preserve_text: input.contains("--preserve-text"),
list_fonts: input.contains("--list-fonts"),
default_width: input
.opt_value_from_fn("--default-width", parse_length)?
Expand Down Expand Up @@ -426,7 +429,9 @@ fn process(args: Args) -> Result<(), String> {
}?;

let mut tree = usvg_tree::Tree::from_data(&input_svg, &re_opt).map_err(|e| format!("{}", e))?;
tree.convert_text(&fontdb);
if !args.preserve_text {
tree.convert_text(&fontdb);
}

let xml_opt = usvg::XmlOptions {
id_prefix: args.id_prefix,
Expand Down
Loading

0 comments on commit 537dd26

Please sign in to comment.