Commit 119f6672 authored by RazrFalcon's avatar RazrFalcon

(usvg) Fixed groups regrouping.

parent 9d8c608f
......@@ -201,7 +201,7 @@ pub fn prepare_doc(doc: &mut svgdom::Document, opt: &Options) {
prepare_clip_path_children(doc);
ungroup_groups(doc, svg, opt);
ungroup_groups(doc, opt);
regroup_elements(doc, svg);
prepare_text_nodes(doc);
......
......@@ -5,85 +5,70 @@
use super::prelude::*;
pub fn ungroup_groups(doc: &mut Document, svg: &Node, opt: &Options) {
let mut groups = Vec::with_capacity(16);
pub fn ungroup_groups(doc: &mut Document, opt: &Options) {
while let Some(mut g) = find_group(doc, opt) {
ungroup_group(&mut g);
doc.remove_node(g);
}
}
loop {
_ungroup_groups(svg, opt, &mut groups);
fn find_group(doc: &Document, opt: &Options) -> Option<Node> {
for node in doc.root().descendants().filter(|n| n.is_tag_name(EId::G)) {
if !node.has_children() {
// Groups with a `filter` attribute can't be ungroupped.
//
// Because this is a valid SVG:
// <filter id="filter1" filterUnits="userSpaceOnUse" x="20" y="20" width="160" height="160">
// <feFlood flood-color="green"/>
// </filter>
// <g filter="url(#filter1)"/>
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Filter) {
continue;
}
if groups.is_empty() {
break;
return Some(node.clone());
}
while let Some(mut g) = groups.pop() {
ungroup_group(&mut g);
doc.remove_node(g);
if opt.keep_named_groups && node.has_id() {
continue;
}
}
}
fn _ungroup_groups(parent: &Node, opt: &Options, groups: &mut Vec<Node>) {
for node in parent.children() {
if node.has_children() {
_ungroup_groups(&node, opt, groups);
// Groups with a `clip-path` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::ClipPath) {
continue;
}
if node.is_tag_name(EId::G) {
if !node.has_children() {
// Groups with a `filter` attribute can't be ungroupped.
//
// Because this is a valid SVG:
// <filter id="filter1" filterUnits="userSpaceOnUse" x="20" y="20" width="160" height="160">
// <feFlood flood-color="green"/>
// </filter>
// <g filter="url(#filter1)"/>
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Filter) {
continue;
}
groups.push(node.clone());
continue;
}
if opt.keep_named_groups && node.has_id() {
continue;
}
// Do not ungroup groups inside `clipPath`.
// They will be removed during conversion.
if node.ancestors().skip(1).any(|n| n.is_tag_name(EId::ClipPath)) {
// Groups that was created from 'use' can be ungroupped.
if !node.has_attribute("usvg-group") {
continue;
}
}
// Groups with a `mask` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Mask) {
continue;
}
// Groups with a `clip-path` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::ClipPath) {
continue;
}
// Groups with a `filter` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Filter) {
continue;
}
// Groups with a `mask` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Mask) {
// We can ungroup group with opacity only when it has only one child.
let opacity = node.attributes().get_number_or(AId::Opacity, 1.0);
if opacity.fuzzy_ne(&1.0) {
if node.children().count() != 1 {
continue;
}
}
// Groups with a `filter` attribute can't be ungroupped.
if let Some(&AValue::FuncLink(_)) = node.attributes().get_value(AId::Filter) {
// Do not ungroup groups inside `clipPath`.
// They will be removed during conversion.
if node.ancestors().skip(1).any(|n| n.is_tag_name(EId::ClipPath)) {
// Groups that was created from 'use' can be ungroupped.
if !node.has_attribute("usvg-group") {
continue;
}
// We can ungroup group with opacity only when it has only one child.
let opacity = node.attributes().get_number_or(AId::Opacity, 1.0);
if opacity.fuzzy_ne(&1.0) {
if node.children().count() != 1 {
continue;
}
}
groups.push(node.clone());
}
return Some(node.clone());
}
None
}
fn ungroup_group(g: &mut Node) {
......
......@@ -954,3 +954,4 @@ e-filter-062,32cc3451
e-path-045,47fe0aec
e-path-046,d5800e26
e-symbol-009,1a5c441c
a-opacity-008,3b77e07b
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment