Skip to content

Commit

Permalink
Merge pull request #8 from svg-rust/convert_ellipse_to_circle
Browse files Browse the repository at this point in the history
feat: convert_ellipse_to_circle
  • Loading branch information
SyMind authored Sep 30, 2023
2 parents 186c515 + 116d30a commit 38c7f29
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/core/__fixture__/plugins/convertEllipseToCircle.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions crates/core/src/plugins/convert_ellipse_to_circle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Converts non-eccentric <ellipse>s to <circle>s.

use swc_core::common::DUMMY_SP;
use swc_xml_ast::*;
use swc_xml_visit::{VisitMut, VisitMutWith};
use serde::Deserialize;

struct Visitor {}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Params {}

impl Default for Params {
fn default() -> Self {
Self {}
}
}

impl Visitor {
fn new() -> Self {
Self {}
}
}

impl VisitMut for Visitor {
fn visit_mut_element(&mut self, n: &mut Element) {
if n.tag_name.to_string() == "ellipse" {
let mut rx = "0".to_string();
let mut ry = "0".to_string();
let mut new_attributes = vec![];
for attr in n.attributes.clone() {
if attr.name.to_string() == "rx" {
if let Some(value) = attr.value.clone() {
rx = value.to_string();
}
} else if attr.name.to_string() == "ry" {
if let Some(value) = attr.value.clone() {
ry = value.to_string();
}
} else {
new_attributes.push(attr);
}
}

if rx == ry ||
rx == "auto" ||
ry == "auto" // SVG2
{
n.tag_name = "circle".to_string().into();
let radius = if rx == "auto" { ry } else { rx };
new_attributes.push(Attribute {
span: DUMMY_SP,
namespace: None,
prefix: None,
name: "r".to_string().into(),
raw_name: None,
value: Some(radius.into()),
raw_value: None,
});
n.attributes = new_attributes;
}
}
n.visit_mut_children_with(self);
}
}

pub fn apply(doc: &mut Document, _: &Params) {
let mut v = Visitor::new();
doc.visit_mut_with(&mut v);
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use crate::testing::test_plugin;
use super::*;

#[testing::fixture("__fixture__/plugins/convertEllipseToCircle.*.svg")]
fn pass(input: PathBuf) {
test_plugin(apply, input);
}
}
1 change: 1 addition & 0 deletions crates/core/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod cleanup_ids;
pub mod cleanup_numeric_values;
pub mod collapse_groups;
pub mod convert_colors;
pub mod convert_ellipse_to_circle;

0 comments on commit 38c7f29

Please sign in to comment.