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

Add support for the transform-origin attribute. #679

Merged
merged 19 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/resvg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
pico-args = { version = "0.5", features = ["eq-separator"] }
png = { version = "0.17", optional = true }
rgb = "0.8"
svgtypes = "0.12"
svgtypes = { git = "https://github.com/LaurenzV/svgtypes" }
tiny-skia = "0.11.2"
usvg = { path = "../usvg", version = "0.36.0", default-features = false }

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/bottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/center.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/length-px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/on-group.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/on-image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/on-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/top-left.svg
11 changes: 11 additions & 0 deletions crates/resvg/tests/tests/structure/transform-origin/top.svg
2 changes: 1 addition & 1 deletion crates/usvg-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ log = "0.4"
roxmltree = "0.18"
simplecss = "0.2"
siphasher = "0.3" # perfect hash implementation
svgtypes = "0.12"
svgtypes = { git = "https://github.com/LaurenzV/svgtypes" }
usvg-tree = { path = "../usvg-tree", version = "0.36.0" }
7 changes: 4 additions & 3 deletions crates/usvg-parser/src/clippath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::str::FromStr;
use usvg_tree::{ClipPath, Group, Node, NodeKind, Transform, Units};

use crate::converter;
use crate::converter::{resolve_transform_origin, State};
use crate::svgtree::{AId, EId, SvgNode};

pub(crate) fn convert(
Expand All @@ -21,7 +22,7 @@ pub(crate) fn convert(
}

// The whole clip path should be ignored when a transform is invalid.
let transform = resolve_transform(node)?;
let transform = resolve_clip_path_transform(node, state)?;

// Check if this element was already converted.
if let Some(clip) = cache.clip_paths.get(node.element_id()) {
Expand Down Expand Up @@ -67,7 +68,7 @@ pub(crate) fn convert(
}
}

fn resolve_transform(node: SvgNode) -> Option<Transform> {
fn resolve_clip_path_transform(node: SvgNode, state: &State) -> Option<Transform> {
// Do not use Node::attribute::<Transform>, because it will always
// return a valid transform.

Expand All @@ -94,7 +95,7 @@ fn resolve_transform(node: SvgNode) -> Option<Transform> {
);

if ts.is_valid() {
Some(ts)
Some(resolve_transform_origin(node, state, ts))
} else {
None
}
Expand Down
44 changes: 42 additions & 2 deletions crates/usvg-parser/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::hash::{Hash, Hasher};
use std::rc::Rc;
use std::str::FromStr;

use svgtypes::{Length, LengthUnit as Unit};
use svgtypes::{Length, LengthUnit as Unit, TransformOrigin};
use usvg_tree::*;

use crate::svgtree::{self, AId, EId, FromValue, SvgNode};
use crate::units;
use crate::units::convert_length;
use crate::{Error, Options};

#[derive(Clone)]
Expand Down Expand Up @@ -560,7 +561,11 @@ pub(crate) fn convert_group(
filters
};

let transform: Transform = node.attribute(AId::Transform).unwrap_or_default();
let transform: Transform = resolve_transform_origin(
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
node,
state,
node.attribute(AId::Transform).unwrap_or_default(),
);
let blend_mode: BlendMode = node.attribute(AId::MixBlendMode).unwrap_or_default();
let isolation: Isolation = node.attribute(AId::Isolation).unwrap_or_default();
let isolate = isolation == Isolation::Isolate;
Expand Down Expand Up @@ -602,6 +607,41 @@ pub(crate) fn convert_group(
}
}

pub(crate) fn resolve_transform_origin(
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
node: SvgNode,
state: &State,
mut transform: Transform,
) -> Transform {
let transform_origin: Option<TransformOrigin> = node.attribute(AId::TransformOrigin);

if let Some(transform_origin) = transform_origin {
let transform_origin = Transform::from_translate(
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
convert_length(
transform_origin.x_offset,
node,
AId::Width,
Units::UserSpaceOnUse,
state,
),
convert_length(
transform_origin.y_offset,
node,
AId::Height,
Units::UserSpaceOnUse,
state,
),
);
transform = transform_origin
.pre_concat(transform)
.pre_concat(Transform::from_translate(
-transform_origin.tx,
-transform_origin.ty,
));
}

transform
}

fn remove_empty_groups(tree: &mut Tree) {
fn rm(parent: Node) -> bool {
let mut changed = false;
Expand Down
2 changes: 2 additions & 0 deletions crates/usvg-parser/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ fn convert_paint(
from_fallback(node, fallback, opacity)
}
}
// Ignore `context-fill` and `context-stroke for now
LaurenzV marked this conversation as resolved.
Show resolved Hide resolved
_ => None,
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/usvg-parser/src/svgtree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ impl AId {
| AId::TextOverflow
| AId::TextRendering
| AId::Transform
| AId::TransformOrigin
| AId::UnicodeBidi
| AId::VectorEffect
| AId::Visibility
Expand Down Expand Up @@ -801,6 +802,7 @@ fn is_non_inheritable(id: AId) -> bool {
| AId::StopOpacity
| AId::TextDecoration
| AId::Transform
| AId::TransformOrigin
)
}

Expand Down Expand Up @@ -871,6 +873,12 @@ impl<'a, 'input: 'a> FromValue<'a, 'input> for usvg_tree::Transform {
}
}

impl<'a, 'input: 'a> FromValue<'a, 'input> for svgtypes::TransformOrigin {
fn parse(_: SvgNode, _: AId, value: &str) -> Option<Self> {
Self::from_str(value).ok()
}
}

impl<'a, 'input: 'a> FromValue<'a, 'input> for svgtypes::ViewBox {
fn parse(_: SvgNode, _: AId, value: &str) -> Option<Self> {
Self::from_str(value).ok()
Expand Down
Loading
Loading