Skip to content

Commit

Permalink
Render text + fix transforms (#26)
Browse files Browse the repository at this point in the history
Fixes #16

This enables naive text rendering support using usvg's support for
flattening text into paths. It also fixes application of transform for
nested SVGs.

With this patch, `vello_svg` can now render the "badges" from the Xilem
README:

<img width="726" alt="Screenshot 2024-07-29 at 11 14 21"
src="https://github.com/user-attachments/assets/4ddea064-7ea6-42ff-9570-0c138e76529e">
  • Loading branch information
nicoburns authored Jul 29, 2024
1 parent f7aa3e6 commit f4c5f71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

This release has an [MSRV][] of 1.75.

### Added

- Support for rendering basic text

### Fixed

- Transform of nested SVGs

## 0.3.0

This release has an [MSRV][] of 1.75.
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use vello;

/// Re-export usvg.
pub use usvg;
use vello::kurbo::Affine;

/// Render a [`Scene`] from an SVG string, with default error handling.
///
Expand Down Expand Up @@ -95,5 +96,5 @@ pub fn append_tree_with<F: FnMut(&mut vello::Scene, &usvg::Node)>(
svg: &usvg::Tree,
error_handler: &mut F,
) {
render::render_group(scene, svg.root(), error_handler);
render::render_group(scene, svg.root(), Affine::IDENTITY, error_handler);
}
12 changes: 7 additions & 5 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::util;
use vello::kurbo::Affine;
use vello::peniko::{BlendMode, Fill};
use vello::Scene;

pub(crate) fn render_group<F: FnMut(&mut Scene, &usvg::Node)>(
scene: &mut Scene,
group: &usvg::Group,
transform: Affine,
error_handler: &mut F,
) {
for node in group.children() {
let transform = util::to_affine(&node.abs_transform());
let transform = transform * util::to_affine(&node.abs_transform());
match node {
usvg::Node::Group(g) => {
let mut pushed_clip = false;
Expand All @@ -32,7 +34,7 @@ pub(crate) fn render_group<F: FnMut(&mut Scene, &usvg::Node)>(
}
}

render_group(scene, g, error_handler);
render_group(scene, g, Affine::IDENTITY, error_handler);

if pushed_clip {
scene.pop_layer();
Expand Down Expand Up @@ -110,12 +112,12 @@ pub(crate) fn render_group<F: FnMut(&mut Scene, &usvg::Node)>(
scene.draw_image(&image, image_ts);
}
usvg::ImageKind::SVG(svg) => {
render_group(scene, svg.root(), error_handler);
render_group(scene, svg.root(), transform, error_handler);
}
}
}
usvg::Node::Text(_) => {
error_handler(scene, node);
usvg::Node::Text(text) => {
render_group(scene, text.flattened(), transform, error_handler);
}
}
}
Expand Down

0 comments on commit f4c5f71

Please sign in to comment.