-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rust-plugins): 💡 svg builder
- Loading branch information
Showing
10 changed files
with
191 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @farmfe/plugin-icons | ||
|
||
## 0.0.6 | ||
|
||
### Patch Changes | ||
|
||
- refactor: svg builder | ||
|
||
## 0.0.5 | ||
|
||
### Patch Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,32 @@ | ||
use super::svg_modifier::SvgModifier; | ||
use super::struct_config::IconifyLoaderOptions; | ||
use super::svg_builder::SvgBuilder; | ||
use super::{ | ||
icon_to_svg::{icon_to_svg, IconifyIconBuildResult}, | ||
struct_config::IconifyIcon, | ||
}; | ||
|
||
pub fn gen_svg_for_icon_data( | ||
icon_data: Option<IconifyIcon>, | ||
options: Option<IconifyLoaderOptions>, | ||
icon_data: IconifyIcon, | ||
options: IconifyLoaderOptions, | ||
) -> Option<String> { | ||
if let Some(icon) = icon_data { | ||
let IconifyIconBuildResult { | ||
mut attributes, | ||
body, | ||
.. | ||
} = icon_to_svg(icon.clone(), None); | ||
let scale = options.as_ref().and_then(|o| o.scale); | ||
if let Some(s) = scale { | ||
if s != 0.0 { | ||
attributes.height = Some(format!("{}{}", s, "em")); | ||
attributes.width = Some(format!("{}{}", s, "em")); | ||
} | ||
let IconifyIconBuildResult { | ||
mut attributes, | ||
body, | ||
.. | ||
} = icon_to_svg(icon_data, None); | ||
if let Some(s) = options.scale { | ||
if s != 0.0 { | ||
attributes.height = Some(format!("{}{}", s, "em")); | ||
attributes.width = Some(format!("{}{}", s, "em")); | ||
} | ||
let svg = SvgModifier::new(SvgModifier { | ||
width: attributes.width, | ||
height: attributes.height, | ||
view_box: Some(attributes.view_box), | ||
..options | ||
.as_ref() | ||
.and_then(|o| o.customizations.clone()) | ||
.unwrap_or_default() | ||
}); | ||
return Some(svg.apply_to_svg(&format!("<svg>{}</svg>", body))); | ||
} else { | ||
panic!("Icon data is missing"); | ||
} | ||
} | ||
let svg_content = format!("<svg>{}</svg>", body); | ||
let svg = SvgBuilder::new(&svg_content) | ||
.width(attributes.width) | ||
.height(attributes.height) | ||
.view_box(Some(attributes.view_box)) | ||
.insert_customizations(options.customizations.unwrap_or_default()) | ||
.build(); | ||
|
||
Some(svg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use xmltree::Element; | ||
pub struct SvgBuilder { | ||
element: Element, | ||
} | ||
|
||
#[derive(Default, Deserialize)] | ||
pub struct SvgCustomizations { | ||
pub fill: Option<String>, | ||
pub stroke: Option<String>, | ||
pub stroke_width: Option<String>, | ||
pub width: Option<String>, | ||
pub height: Option<String>, | ||
pub view_box: Option<String>, | ||
pub class: Option<String>, | ||
pub style: Option<Value>, | ||
} | ||
|
||
impl SvgBuilder { | ||
pub fn new(svg_content: &str) -> Self { | ||
let element = Element::parse(svg_content.as_bytes()).unwrap(); | ||
SvgBuilder { element } | ||
} | ||
|
||
pub fn fill(mut self, fill: Option<String>) -> Self { | ||
if let Some(fill) = fill { | ||
self.element.attributes.insert("fill".to_string(), fill); | ||
} | ||
self | ||
} | ||
|
||
pub fn stroke(mut self, stroke: Option<String>) -> Self { | ||
if let Some(stroke) = stroke { | ||
self.element.attributes.insert("stroke".to_string(), stroke); | ||
} | ||
self | ||
} | ||
|
||
pub fn stroke_width(mut self, stroke_width: Option<String>) -> Self { | ||
if let Some(stroke_width) = stroke_width { | ||
self | ||
.element | ||
.attributes | ||
.insert("stroke-width".to_string(), stroke_width); | ||
} | ||
self | ||
} | ||
|
||
pub fn width(mut self, width: Option<String>) -> Self { | ||
if let Some(width) = width { | ||
self.element.attributes.insert("width".to_string(), width); | ||
} | ||
self | ||
} | ||
|
||
pub fn height(mut self, height: Option<String>) -> Self { | ||
if let Some(height) = height { | ||
self.element.attributes.insert("height".to_string(), height); | ||
} | ||
self | ||
} | ||
|
||
pub fn view_box(mut self, view_box: Option<String>) -> Self { | ||
if let Some(view_box) = view_box { | ||
self | ||
.element | ||
.attributes | ||
.insert("viewBox".to_string(), view_box); | ||
} | ||
self | ||
} | ||
|
||
pub fn class(mut self, class: Option<String>) -> Self { | ||
if let Some(class) = class { | ||
self.element.attributes.insert("class".to_string(), class); | ||
} | ||
self | ||
} | ||
|
||
pub fn style(mut self, style: Option<Value>) -> Self { | ||
if let Some(style) = style { | ||
let style_str = style.as_object().map_or(String::new(), |obj| { | ||
obj | ||
.iter() | ||
.map(|(key, value)| { | ||
format!( | ||
"{}:{};", | ||
key, | ||
value.as_str().unwrap_or("").replace("\"", "") | ||
) | ||
}) | ||
.collect::<Vec<_>>() | ||
.join(" ") | ||
}); | ||
self | ||
.element | ||
.attributes | ||
.insert("style".to_string(), style_str); | ||
} | ||
self | ||
} | ||
|
||
pub fn insert_customizations(self, customizations: SvgCustomizations) -> Self { | ||
self | ||
.fill(customizations.fill) | ||
.class(customizations.class) | ||
.height(customizations.height) | ||
.stroke(customizations.stroke) | ||
.stroke_width(customizations.stroke_width) | ||
.style(customizations.style) | ||
.view_box(customizations.view_box) | ||
.width(customizations.width) | ||
} | ||
|
||
pub fn build(self) -> String { | ||
let mut buffer = Vec::new(); | ||
self.element.write(&mut buffer).unwrap(); | ||
String::from_utf8(buffer).unwrap() | ||
} | ||
} |
Oops, something went wrong.