Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove transform attribute from images and paths #668

Merged
merged 9 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/resvg/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,6 @@ fn apply_image(

let uimage = usvg::Image {
id: String::new(),
transform: usvg::Transform::default(),
visibility: usvg::Visibility::Visible,
view_box,
rendering_mode: fe.rendering_mode,
Expand Down
6 changes: 1 addition & 5 deletions crates/resvg/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub enum ImageKind {
}

pub struct Image {
pub transform: tiny_skia::Transform,
pub view_box: usvg::ViewBox,
pub quality: tiny_skia::FilterQuality,
pub kind: ImageKind,
Expand All @@ -22,7 +21,6 @@ pub fn convert(image: &usvg::Image, children: &mut Vec<Node>) -> Option<BBoxes>
let object_bbox = image.view_box.rect.to_rect();
let bboxes = BBoxes {
object: usvg::BBox::from(object_bbox),
transformed_object: usvg::BBox::from(object_bbox.transform(image.transform)?),
layer: usvg::BBox::from(object_bbox),
};

Expand All @@ -47,7 +45,6 @@ pub fn convert(image: &usvg::Image, children: &mut Vec<Node>) -> Option<BBoxes>
};

children.push(Node::Image(Image {
transform: image.transform,
view_box: image.view_box,
quality,
kind,
Expand Down Expand Up @@ -84,7 +81,7 @@ fn render_vector(
let mut sub_pixmap = tiny_skia::Pixmap::new(pixmap.width(), pixmap.height()).unwrap();

let source_transform = transform;
let transform = transform.pre_concat(image.transform).pre_concat(ts);
let transform = transform.pre_concat(ts);

tree.render(transform, &mut sub_pixmap.as_mut());

Expand Down Expand Up @@ -241,7 +238,6 @@ mod raster_images {
None
};

let transform = transform.pre_concat(image.transform);
pixmap.fill_rect(rect.to_rect(), &paint, transform, mask.as_ref());

Some(())
Expand Down
17 changes: 1 addition & 16 deletions crates/resvg/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ use crate::render::Context;
use crate::tree::{BBoxes, Node};

pub struct FillPath {
pub transform: tiny_skia::Transform,
pub paint: Paint,
pub rule: tiny_skia::FillRule,
pub anti_alias: bool,
pub path: Rc<tiny_skia::Path>,
}

pub struct StrokePath {
pub transform: tiny_skia::Transform,
pub paint: Paint,
pub stroke: tiny_skia::Stroke,
pub anti_alias: bool,
pub path: Rc<tiny_skia::Path>,
}

pub fn convert(upath: &usvg::Path, children: &mut Vec<Node>) -> Option<BBoxes> {
let transform = upath.transform;
let anti_alias = upath.rendering_mode.use_shape_antialiasing();

let fill_path = upath.fill.as_ref().and_then(|ufill| {
convert_fill_path(
ufill,
upath.data.clone(),
transform,
upath.text_bbox,
anti_alias,
)
Expand All @@ -42,7 +38,6 @@ pub fn convert(upath: &usvg::Path, children: &mut Vec<Node>) -> Option<BBoxes> {
convert_stroke_path(
ustroke,
upath.data.clone(),
transform,
upath.text_bbox,
anti_alias,
)
Expand All @@ -63,8 +58,6 @@ pub fn convert(upath: &usvg::Path, children: &mut Vec<Node>) -> Option<BBoxes> {
bboxes.object = bboxes.object.expand(o_bbox);
}

bboxes.transformed_object = bboxes.object.transform(upath.transform)?;

// Do not add hidden paths, but preserve the bbox.
// visibility=hidden still affects the bbox calculation.
if upath.visibility != usvg::Visibility::Visible {
Expand Down Expand Up @@ -95,7 +88,6 @@ pub fn convert(upath: &usvg::Path, children: &mut Vec<Node>) -> Option<BBoxes> {
fn convert_fill_path(
ufill: &usvg::Fill,
path: Rc<tiny_skia::Path>,
transform: tiny_skia::Transform,
text_bbox: Option<tiny_skia::NonZeroRect>,
anti_alias: bool,
) -> Option<(FillPath, usvg::BBox, usvg::BBox)> {
Expand All @@ -118,7 +110,6 @@ fn convert_fill_path(
crate::paint_server::convert(&ufill.paint, ufill.opacity, object_bbox.to_non_zero_rect())?;

let path = FillPath {
transform,
paint,
rule,
anti_alias,
Expand All @@ -131,7 +122,6 @@ fn convert_fill_path(
fn convert_stroke_path(
ustroke: &usvg::Stroke,
path: Rc<tiny_skia::Path>,
transform: tiny_skia::Transform,
text_bbox: Option<tiny_skia::NonZeroRect>,
anti_alias: bool,
) -> Option<(StrokePath, usvg::BBox, usvg::BBox)> {
Expand Down Expand Up @@ -173,9 +163,7 @@ fn convert_stroke_path(

// TODO: explain
// TODO: expand by stroke width for round/bevel joins
let resolution_scale = tiny_skia::PathStroker::compute_resolution_scale(&transform);
let resolution_scale = resolution_scale.max(10.0);
let stroked_path = path.stroke(&stroke, resolution_scale)?;
let stroked_path = path.stroke(&stroke, 1.0)?;

let mut layer_bbox = usvg::BBox::from(stroked_path.bounds());
if let Some(text_bbox) = text_bbox {
Expand All @@ -186,7 +174,6 @@ fn convert_stroke_path(
// TODO: preserve stroked path

let path = StrokePath {
transform,
paint,
stroke,
anti_alias,
Expand Down Expand Up @@ -227,7 +214,6 @@ pub fn render_fill_path(
paint.anti_alias = path.anti_alias;
paint.blend_mode = blend_mode;

let transform = transform.pre_concat(path.transform);
pixmap.fill_path(&path.path, &paint, path.rule, transform, None);

Some(())
Expand Down Expand Up @@ -266,7 +252,6 @@ pub fn render_stroke_path(

// TODO: fallback to a stroked path when possible

let transform = transform.pre_concat(path.transform);
pixmap.stroke_path(&path.path, &paint, &path.stroke, transform, None);

Some(())
Expand Down
9 changes: 1 addition & 8 deletions crates/resvg/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ pub struct BBoxes {
/// Just a shape/image bbox as per SVG spec.
pub object: usvg::BBox,

/// The same as above, but transformed using object's transform.
pub transformed_object: usvg::BBox,

/// Similar to `object`, but expanded to fit the stroke as well.
pub layer: usvg::BBox,
}
Expand All @@ -167,7 +164,7 @@ fn convert_group(
};

let (filters, filter_bbox) =
crate::filter::convert(&ugroup.filters, bboxes.transformed_object.to_rect());
crate::filter::convert(&ugroup.filters, bboxes.object.to_rect());

// TODO: figure out a nicer solution
// Ignore groups with filters but invalid filter bboxes.
Expand All @@ -192,7 +189,6 @@ fn convert_group(
};

bboxes.object = bboxes.object.transform(ugroup.transform)?;
bboxes.transformed_object = bboxes.transformed_object.transform(ugroup.transform)?;
bboxes.layer = bboxes.layer.transform(ugroup.transform)?;

children.push(Node::Group(group));
Expand Down Expand Up @@ -222,7 +218,6 @@ fn convert_empty_group(ugroup: &usvg::Group, children: &mut Vec<Node>) -> Option
let bboxes = BBoxes {
// TODO: find a better solution
object: usvg::BBox::default(),
transformed_object: usvg::BBox::default(),
layer: usvg::BBox::from(layer_bbox),
};

Expand All @@ -236,8 +231,6 @@ fn convert_children(parent: usvg::Node, children: &mut Vec<Node>) -> Option<BBox
for node in parent.children() {
if let Some(bboxes2) = convert_node_inner(node, children) {
bboxes.object = bboxes.object.expand(bboxes2.object);
bboxes.transformed_object =
bboxes.transformed_object.expand(bboxes2.transformed_object);
bboxes.layer = bboxes.layer.expand(bboxes2.layer);
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/usvg-parser/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ fn convert_path(

parent.append_kind(NodeKind::Path(Path {
id,
transform: Default::default(),
visibility,
fill,
stroke,
Expand Down
1 change: 0 additions & 1 deletion crates/usvg-parser/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ pub(crate) fn convert(node: SvgNode, state: &converter::State, parent: &mut Node

parent.append_kind(NodeKind::Image(Image {
id,
transform: Default::default(),
visibility,
view_box,
rendering_mode,
Expand Down
1 change: 0 additions & 1 deletion crates/usvg-text-layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,6 @@ fn convert_span(

let path = Path {
id: String::new(),
transform: Transform::default(),
visibility: span.visibility,
fill,
stroke: span.stroke.clone(),
Expand Down
11 changes: 2 additions & 9 deletions crates/usvg-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,8 @@ impl NodeKind {
pub fn transform(&self) -> Transform {
match self {
NodeKind::Group(ref e) => e.transform,
NodeKind::Path(ref e) => e.transform,
NodeKind::Image(ref e) => e.transform,
NodeKind::Path(_) => Transform::default(),
NodeKind::Image(_) => Transform::default(),
NodeKind::Text(ref e) => e.transform,
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -844,9 +844,6 @@ pub struct Path {
/// Can be empty.
pub id: String,

/// Element transform.
pub transform: Transform,

/// Element visibility.
pub visibility: Visibility,

Expand Down Expand Up @@ -892,7 +889,6 @@ impl Path {
pub fn new(data: Rc<tiny_skia_path::Path>) -> Self {
Path {
id: String::new(),
transform: Transform::default(),
visibility: Visibility::Visible,
fill: None,
stroke: None,
Expand Down Expand Up @@ -940,9 +936,6 @@ pub struct Image {
/// Can be empty.
pub id: String,

/// Element transform.
pub transform: Transform,

/// Element visibility.
pub visibility: Visibility,

Expand Down
11 changes: 5 additions & 6 deletions crates/usvg/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn conv_elements(parent: &Node, is_clip_path: bool, opt: &XmlOptions, xml: &mut
fn conv_element(node: &Node, is_clip_path: bool, opt: &XmlOptions, xml: &mut XmlWriter) {
match *node.borrow() {
NodeKind::Path(ref p) => {
write_path(p, is_clip_path, None, opt, xml);
write_path(p, is_clip_path, Transform::default(), None, opt, xml);
}
NodeKind::Image(ref img) => {
xml.start_svg_element(EId::Image);
Expand All @@ -585,7 +585,6 @@ fn conv_element(node: &Node, is_clip_path: bool, opt: &XmlOptions, xml: &mut Xml
}
}

xml.write_transform(AId::Transform, img.transform, opt);
xml.write_image_data(&img.kind);

xml.end_element();
Expand All @@ -597,11 +596,10 @@ fn conv_element(node: &Node, is_clip_path: bool, opt: &XmlOptions, xml: &mut Xml
// `clip-path` on it.

if let NodeKind::Path(ref path) = *node.first_child().unwrap().borrow() {
let mut path = path.clone();
path.transform = g.transform.pre_concat(path.transform);
let path = path.clone();

let clip_id = g.clip_path.as_ref().map(|cp| cp.id.as_str());
write_path(&path, is_clip_path, clip_id, opt, xml);
write_path(&path, is_clip_path, g.transform, clip_id, opt, xml);
}

return;
Expand Down Expand Up @@ -983,6 +981,7 @@ fn write_base_grad(g: &BaseGradient, xml: &mut XmlWriter, opt: &XmlOptions) {
fn write_path(
path: &Path,
is_clip_path: bool,
path_transform: Transform,
clip_path: Option<&str>,
opt: &XmlOptions,
xml: &mut XmlWriter,
Expand Down Expand Up @@ -1013,7 +1012,7 @@ fn write_path(
xml.write_func_iri(AId::ClipPath, id, opt);
}

xml.write_transform(AId::Transform, path.transform, opt);
xml.write_transform(AId::Transform, path_transform, opt);

xml.write_attribute_raw("d", |buf| {
use tiny_skia_path::PathSegment;
Expand Down
Loading