parsed styles and struggling with svg

This commit is contained in:
Shautvast 2025-01-19 21:34:55 +01:00
parent 25c3d008cc
commit 89e03c8cb6
2 changed files with 149 additions and 0 deletions

15
output_multiple_nodes.svg Normal file
View file

@ -0,0 +1,15 @@
<svg width="135" height="75" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><style>svg {
background-color: white;
}
.node {
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
padding: 1em;
font-size: 10px;
font-family: sans-serif;
}
</style><rect x="5" y="5" width="60" height="60" fill="none" stroke="green" /><text x="35" y="35" text-anchor="middle">Node 1</text><rect x="65" y="5" width="60" height="60" fill="none" stroke="green" /><text x="95" y="35" text-anchor="middle">Node 1</text><rect x="0" y="0" width="135" height="75" fill="none" stroke="red" stroke-width="2" /></svg>

After

Width:  |  Height:  |  Size: 684 B

134
src/render/svglib/mod.rs Normal file
View file

@ -0,0 +1,134 @@
pub mod circle;
pub mod div;
pub mod ellipse;
pub mod foreign_object;
pub mod group;
pub mod image;
pub mod line;
pub mod link;
pub mod path;
pub mod rect;
pub mod svg;
pub mod text;
#[derive(Debug)]
pub struct Value(String);
impl From<&str> for Value {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl Value {
pub fn to_string(&self) -> String {
self.0.clone()
}
}
impl From<usize> for Value {
fn from(s: usize) -> Self {
Self(s.to_string())
}
}
impl From<u32> for Value {
fn from(s: u32) -> Self {
Self(s.to_string())
}
}
impl From<i32> for Value {
fn from(s: i32) -> Self {
Self(s.to_string())
}
}
impl From<f32> for Value {
fn from(s: f32) -> Self {
Self(s.to_string())
}
}
impl From<String> for Value {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&String> for Value {
fn from(s: &String) -> Self {
Self(s.to_string())
}
}
pub enum ElementType {
Circle,
Ellipse,
ForeignObject,
Group(Vec<ElementType>),
Image,
Line,
Link,
Path,
Rect,
}
pub trait Element {
fn get_type(&self) -> ElementType;
fn to_string(&self) -> String;
fn atts(&self) -> &[Att];
}
pub trait Shape {
fn fill<V>(self, value: V) -> Self
where
V: Into<Value>;
fn stroke<V>(self, value: V) -> Self
where
V: Into<Value>;
fn transform<V>(self, value: V) -> Self
where
V: Into<Value>;
}
#[derive(Debug)]
pub struct Att {
name: String,
value: Value,
}
impl Att {
pub fn new(name: &str, value: Value) -> Self {
Self {
name: name.to_string(),
value,
}
}
}
fn att<V>(name: &str, value: V) -> Att
where
V: Into<Value>,
{
Att::new(name, value.into())
}
fn att_str(att: &Option<Att>) -> String {
att.as_ref()
.map(|a| format!(r#" {}="{}""#, a.name, a.value.to_string()))
.unwrap_or("".to_string())
}
fn att_str2(att_name: &str, att_val: &Option<String>) -> String {
att_val
.as_ref()
.map(|val| format!(r#" {}="{}""#, att_name, val))
.unwrap_or("".to_string())
}
fn att_str3(att: &Att) -> String {
format!(r#" {}="{}""#, att.name, att.value.to_string())
}