From dbb89189e96cefe6d551e3db2b5e79ad24818e8b Mon Sep 17 00:00:00 2001 From: Jovansonlee Cesar Date: Fri, 5 Oct 2018 16:50:51 +0800 Subject: Remove unused code All elements is now class based styled, to allow overriding in the html embedding in css --- svgbob/Cargo.lock | 2 +- svgbob/src/element.rs | 46 ++++--------- svgbob/src/grid.rs | 177 ++++++++++++++++++++++++------------------------ svgbob/src/optimizer.rs | 6 +- 4 files changed, 107 insertions(+), 124 deletions(-) diff --git a/svgbob/Cargo.lock b/svgbob/Cargo.lock index d99a4cd..518c637 100644 --- a/svgbob/Cargo.lock +++ b/svgbob/Cargo.lock @@ -10,7 +10,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "svgbob" -version = "0.4.0" +version = "0.4.1" dependencies = [ "pom 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "svg 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/svgbob/src/element.rs b/svgbob/src/element.rs index 9a1902c..7fa3889 100644 --- a/svgbob/src/element.rs +++ b/svgbob/src/element.rs @@ -29,7 +29,7 @@ use svg; #[derive(Debug, Clone, PartialEq, PartialOrd )] pub enum Element { - Circle(Point, f32, String), + Circle(Point, f32), // start, end, stroke , start_feature, end feature Line(Point, Point, Stroke, Feature, Feature), // start, end, radius, sweep, stroke, start_feat, end_feat @@ -37,29 +37,6 @@ pub enum Element { Text(Loc, String), } -impl Element{ - - /* - fn longer_line<'a>(&'a self, other: &'a Element) -> &'a Element { - match self{ - Element::Line(s1, e1, _, _, _) => match other{ - Element::Line(s2, e2, _, _,_) => { - let d1 = point::distance(s1, e1); - let d2 = point::distance(s2, e2); - if d1 > d2 { - self - } - else{ - other - } - } - _ => panic!("only for lines"), - } - _ => panic!("only for lines"), - } - } - */ -} #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)] pub enum Stroke { @@ -143,7 +120,7 @@ pub fn arc(a: &Point, b: &Point, r: f32) -> Element { pub fn open_circle(c: &Point, r: f32) -> Element { - Element::Circle(c.clone(), r.clone(), "open".to_string()) + Element::Circle(c.clone(), r.clone()) } pub fn arrow_line(s: &Point, e: &Point) -> Element { @@ -205,8 +182,8 @@ impl Element { match *other { Element::Line(ref s2, ref e2, ref stroke2, ref start_feature2, ref end_feature2) => { // note: dual 3 point check for trully collinear lines - if collinear(s, e, s2) - && collinear(s, e, e2) + if collinear(s, e, s2) + && collinear(s, e, e2) && stroke == stroke2{ // same length line if s == s2 && e == e2 && start_feature == start_feature2 && end_feature == end_feature2 { @@ -318,9 +295,9 @@ impl Element { /// convert drawing element to SVG element pub fn to_svg(&self, settings: &Settings) -> SvgElement { match *self { - Element::Circle(ref c, r, ref class) => { + Element::Circle(ref c, r) => { let svg_circle = SvgCircle::new() - .set("class", class.clone()) + .set("class", "fg_stroke no_fill") .set("cx", c.x) .set("cy", c.y) .set("r", r); @@ -329,11 +306,12 @@ impl Element { } Element::Line(ref s, ref e, ref stroke, ref start_feature, ref end_feature) => { let mut svg_line = SvgLine::new() + .set("class", "fg_stroke") .set("x1", s.x) .set("y1", s.y) .set("x2", e.x) .set("y2", e.y); - + if let Some(marker) = start_feature.get_marker(){ svg_line.assign("marker-start", marker); } @@ -343,7 +321,7 @@ impl Element { match *stroke { Solid => (), Dashed => { - svg_line.assign("class","dashed"); + svg_line.assign("class","fg_stroke dashed"); } }; SvgElement::Line(svg_line) @@ -359,6 +337,7 @@ impl Element { s.x, s.y, radius, radius, arc_flag, sweept, e.x, e.y ); let mut svg_arc = SvgPath::new() + .set("class","fg_stroke no_fill") .set("d", d); if let Some(marker) = start_feature.get_marker(){ svg_arc.assign("marker-start", marker); @@ -371,7 +350,10 @@ impl Element { Element::Text(ref loc, ref string) => { let sx = loc.x as f32 * settings.text_width + settings.text_width / 8.0; let sy = loc.y as f32 * settings.text_height + settings.text_height * 3.0 / 4.0; - let mut svg_text = SvgText::new().set("x", sx).set("y", sy); + let mut svg_text = SvgText::new() + .set("class","fg_fill") + .set("x", sx) + .set("y", sy); let text_node = svg::node::Text::new(string.to_string()); svg_text.append(text_node); SvgElement::Text(svg_text) diff --git a/svgbob/src/grid.rs b/svgbob/src/grid.rs index 83e9ba8..679122d 100644 --- a/svgbob/src/grid.rs +++ b/svgbob/src/grid.rs @@ -344,50 +344,44 @@ fn get_styles(settings: &Settings) -> Style { rect.backdrop {{ fill: {background_color}; }} -text{{ + +.fg_fill {{ fill: {stroke_color}; }} -circle {{ - fill: none; - stroke: {stroke_color}; - stroke-width: {stroke_width}; + +.bg_fill {{ + fill: {background_color}; }} -line {{ +.fg_stroke {{ stroke: {stroke_color}; stroke-width: {stroke_width}; - stroke-opacity: 1; - fill-opacity: 1; - stroke-linecap: round; - stroke-linejoin: miter; }} -path {{ +.no_fill {{ fill: none; - stroke: {stroke_color}; - stroke-width: {stroke_width}; - stroke-opacity: 1; - fill-opacity: 1; - stroke-linecap: round; - stroke-linejoin: miter; }} -line.dashed {{ +.dashed {{ stroke-dasharray: 5; }} -.fg_fill {{ + +text{{ fill: {stroke_color}; }} - -.bg_fill {{ - fill: {background_color}; - stroke: {stroke_color}; - stroke-width: {stroke_width}; +line, path {{ + stroke-linecap: round; + stroke-linejoin: miter; + stroke-opacity: 1; + fill-opacity: 1; }} + + + tspan.head{{ fill: none; stroke: none; @@ -431,7 +425,7 @@ fn clear_arrow_marker() -> Marker { let path = SvgPolygon::new() .set("points", "2,2 2,12 18,7 2,2") - .set("class", "bg_fill"); + .set("class", "bg_fill fg_stroke"); marker.append(path); marker @@ -495,7 +489,7 @@ fn open_circle_marker() -> Marker { .set("cx",10) .set("cy",10) .set("r",4) - .set("class", "bg_fill"); + .set("class", "bg_fill fg_stroke"); marker.append(circle); marker } @@ -513,7 +507,7 @@ fn big_open_circle_marker() -> Marker { .set("cx",20) .set("cy",20) .set("r",6) - .set("class", "bg_fill"); + .set("class", "bg_fill fg_stroke"); marker.append(circle); marker } @@ -587,67 +581,6 @@ fn exclude_escaped_text(line: &str) -> (String, Vec<(usize, String)>) { (buffer, text_elm) } -#[test] -fn test_escaped_string() { - let input3 = r#"The "qu/i/ck" brown "fox\"s" jumps over the lazy "do|g""#; - let mut raw3 = TextInput::new(input3); - let output3 = line_parse().parse(&mut raw3); - println!("output3: {:?}", output3); - //assert_eq!(Ok(vec![(4, 12), (20, 27), (49, 54)]), output3); - let mut matches = vec![]; - let mut recons = String::new(); - let mut text_elm: Vec<(usize, String)> = vec![]; - let mut index = 0; - if let Ok(output) = output3 { - for (start, end) in output { - println!("matches: {}", &input3[start..end + 1]); - matches.push(input3[start..end + 1].to_string()); - let slice = &input3[index..start]; - recons.push_str(slice); - recons.push_str(&" ".repeat(end + 1 - start)); - text_elm.push((start, input3[start + 1..end].to_string())); - index = end + 1; - } - } - println!("input3: {}", input3); - println!("recons: {}", recons); - println!("escaped: {:?}", text_elm); - assert_eq!(vec![r#""qu/i/ck""#, r#""fox\"s""#, r#""do|g""#], matches); - assert_eq!(input3.len(), recons.len()); -} - -#[test] -fn test_escaped_multiline_string() { - let input3 = r#"The "qu/i/ck brown fox \njumps over the lazy do|g""#; - let mut raw3 = TextInput::new(input3); - let output3 = line_parse().parse(&mut raw3); - println!("output3: {:?}", output3); - assert_eq!(Ok(vec![(4, 49)]), output3); - let mut matches = vec![]; - let mut recons = String::new(); - let mut text_elm: Vec<(usize, String)> = vec![]; - let mut index = 0; - if let Ok(output) = output3 { - for (start, end) in output { - println!("matches: {}", &input3[start..end + 1]); - matches.push(input3[start..end + 1].to_string()); - let slice = &input3[index..start]; - recons.push_str(slice); - recons.push_str(&" ".repeat(end + 1 - start)); - text_elm.push((start, input3[start + 1..end].to_string())); - index = end + 1; - } - } - println!("input3: {}", input3); - println!("recons: {}", recons); - println!("escaped: {:?}", text_elm); - assert_eq!( - vec![r#""qu/i/ck brown fox \njumps over the lazy do|g""#], - matches - ); - assert_eq!(input3.len(), recons.len()); -} - fn escape_string() -> pom::parser::Parser<'static, char, (usize, usize)> { let escape_sequence = sym('\\') * sym('"'); //escape sequence \" let char_string = escape_sequence | none_of("\""); @@ -659,3 +592,71 @@ fn line_parse() -> pom::parser::Parser<'static, char, Vec<(usize, usize)>> { escape_string().repeat(0..) } +#[cfg(test)] +mod test{ + + use super::*; + + #[test] + fn test_escaped_string() { + let input3 = r#"The "qu/i/ck" brown "fox\"s" jumps over the lazy "do|g""#; + let mut raw3 = TextInput::new(input3); + let output3 = line_parse().parse(&mut raw3); + println!("output3: {:?}", output3); + //assert_eq!(Ok(vec![(4, 12), (20, 27), (49, 54)]), output3); + let mut matches = vec![]; + let mut recons = String::new(); + let mut text_elm: Vec<(usize, String)> = vec![]; + let mut index = 0; + if let Ok(output) = output3 { + for (start, end) in output { + println!("matches: {}", &input3[start..end + 1]); + matches.push(input3[start..end + 1].to_string()); + let slice = &input3[index..start]; + recons.push_str(slice); + recons.push_str(&" ".repeat(end + 1 - start)); + text_elm.push((start, input3[start + 1..end].to_string())); + index = end + 1; + } + } + println!("input3: {}", input3); + println!("recons: {}", recons); + println!("escaped: {:?}", text_elm); + assert_eq!(vec![r#""qu/i/ck""#, r#""fox\"s""#, r#""do|g""#], matches); + assert_eq!(input3.len(), recons.len()); + } + + #[test] + fn test_escaped_multiline_string() { + let input3 = r#"The "qu/i/ck brown fox \njumps over the lazy do|g""#; + let mut raw3 = TextInput::new(input3); + let output3 = line_parse().parse(&mut raw3); + println!("output3: {:?}", output3); + assert_eq!(Ok(vec![(4, 49)]), output3); + let mut matches = vec![]; + let mut recons = String::new(); + let mut text_elm: Vec<(usize, String)> = vec![]; + let mut index = 0; + if let Ok(output) = output3 { + for (start, end) in output { + println!("matches: {}", &input3[start..end + 1]); + matches.push(input3[start..end + 1].to_string()); + let slice = &input3[index..start]; + recons.push_str(slice); + recons.push_str(&" ".repeat(end + 1 - start)); + text_elm.push((start, input3[start + 1..end].to_string())); + index = end + 1; + } + } + println!("input3: {}", input3); + println!("recons: {}", recons); + println!("escaped: {:?}", text_elm); + assert_eq!( + vec![r#""qu/i/ck brown fox \njumps over the lazy do|g""#], + matches + ); + assert_eq!(input3.len(), recons.len()); + } + +} + diff --git a/svgbob/src/optimizer.rs b/svgbob/src/optimizer.rs index 603ee6e..375626d 100644 --- a/svgbob/src/optimizer.rs +++ b/svgbob/src/optimizer.rs @@ -43,7 +43,7 @@ impl Optimizer { } } None - + } /// trace the and try to reduce this element against the elements /// at this location(loc), @@ -106,7 +106,7 @@ impl Optimizer { /// grouped elements together that shares some end_points fn group_elements<'e>(&self, elements: Vec) -> Vec> { - let mut consumed = vec![]; + let mut consumed = vec![]; let mut all_group = vec![]; for (i, elm) in elements.iter().enumerate(){ if !consumed.contains(&i){ @@ -164,7 +164,7 @@ impl Optimizer { let mut circles = vec![]; for elm in elements { match elm { - Element::Circle(_, _, _) => { + Element::Circle(_, _,) => { circles.push(elm.clone()); } Element::Line(_, _, ref stroke, ref start_feature, ref end_feature) => { -- cgit v1.2.3