diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-07-31 03:17:56 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-07-31 03:17:56 +0800 |
commit | 9a8bab5817f995b5564967cbd0fdc60501f2c5bc (patch) | |
tree | d9adc38687c6a14c7861e4d58585bb004b757445 | |
parent | df27c2d8295e6f1d478b79bd94769471f6a5353a (diff) |
Add preprocessing of grid for swaping out characters
-rw-r--r-- | svgbob/src/focus_char.rs | 6 | ||||
-rw-r--r-- | svgbob/src/grid.rs | 75 | ||||
-rw-r--r-- | svgbob/src/loc.rs | 13 |
3 files changed, 90 insertions, 4 deletions
diff --git a/svgbob/src/focus_char.rs b/svgbob/src/focus_char.rs index 97d30cc..d6e9a19 100644 --- a/svgbob/src/focus_char.rs +++ b/svgbob/src/focus_char.rs @@ -27,6 +27,7 @@ pub struct FocusChar<'g> { impl<'g> FocusChar<'g> { pub fn new(loc: &Loc, grid: &'g Grid) -> Self { + // make a new focus char from the grid at this location let s: Option<&String> = grid.get(loc); // if there is a text in this location, take the first char as the focus char let ch = match s { @@ -41,6 +42,7 @@ impl<'g> FocusChar<'g> { } } + /// get the text of self char, including complex block /// concatenated with multiple strings in utf8 encoding fn text(&self) -> String { @@ -56,8 +58,8 @@ impl<'g> FocusChar<'g> { } /// if the character matches given argument - pub fn is(&self, ch: char) -> bool { - self.ch.is(ch) + pub fn is(&self, arg: char) -> bool { + self.ch.is(arg) } /// if character is any character in the string diff --git a/svgbob/src/grid.rs b/svgbob/src/grid.rs index 0b210e3..184cbbb 100644 --- a/svgbob/src/grid.rs +++ b/svgbob/src/grid.rs @@ -20,7 +20,11 @@ use unicode_width::UnicodeWidthChar; #[derive(Debug)] pub struct Grid { pub settings: Settings, + /// cell value is in string instead of char to accomodate multiple width characters + /// each line is Vec<String> index: Vec<Vec<String>>, + /// This are text elements that are escaped and are not processed for diagram + /// matching text_elm: Vec<(usize, usize, String)>, } impl Grid { @@ -64,11 +68,13 @@ impl Grid { } rows.push(row); } - Grid { + let g = Grid { settings: settings.clone(), index: rows, text_elm: text_elm, - } + }; + // do the pre processing here + g.pre_process() } @@ -94,6 +100,12 @@ impl Grid { } + fn text(&self, loc: &Loc) -> &str { + match self.get(loc) { + Some(ref s) => s, + None => "" + } + } /// get the focus char at this location pub fn get_focuschar(&self, loc: &Loc) -> FocusChar { @@ -231,6 +243,65 @@ impl Grid { } svg } + /// traverse each element of the grid and swap characters as needed + fn pre_process(&self) -> Self { + let mut new_index: Vec<Vec<String>> = vec![]; + for (y,line) in self.index.iter().enumerate(){ + let mut row: Vec<String> = vec![]; + for (x,cell) in line.iter().enumerate(){ + let loc = &Loc::new(x as i32, y as i32); + let swap = self.swap_char(loc); + row.push(swap.to_string()); + } + new_index.push(row); + } + Grid{ + settings: self.settings.clone(), + index: new_index, + text_elm: self.text_elm.clone() + } + } + + /// swap characters - - - with ~~~~~ + fn swap_char(&self, loc: &Loc) -> &str { + let cell = self.text(loc); + + let left = self.text(&loc.left()); + let left2 = self.text(&loc.in_left(2)); + let left3 = self.text(&loc.in_left(3)); + let left4 = self.text(&loc.in_left(4)); + let right = self.text(&loc.right()); + let right2 = self.text(&loc.in_right(2)); + let right3 = self.text(&loc.in_right(3)); + let right4 = self.text(&loc.in_right(4)); + // [- - -] + // ^ + // if `-` and right is ` ` and right(2) is `-` right(3) is ` ` and right(4) is `-` + if (cell == "-" && right == " " && right2 == "-" && right3 == " " && right4 == "-") + // [- - -] + // ^ + // if ` ` and left is `-` and right is `-` and right(2) is ` ` and right(3) is `-` + || (cell == " " && left == "-" && right == "-" && right2 == " " && right3 == "-") + // [- - -] + // ^ + // if `-` , and left is ` ` and right is ` ` and left(2) is `-` and right(2) is `-` + || (cell == "-" && left == " " && right == " " && left2 == "-" && right2 == "-") + // [- - -] + // ^ + // if ` `, and left is `-` and right is `-` and left(2) is ` ` and left(3) is `-` + || (cell == " " && left == "-" && right == "-" && left2 == " " && left3 == "-") + // [- - -] + // ^ + // if `-`, and left is ` ` and left(2) is `-` and left(3) is ` ` and left(4) is `-` + || (cell == "-" && left == " " && left2 == "-" && left3 == " " && left4 == "-") + { + "~" + } + else{ + cell + } + } + } fn get_defs() -> Definitions { diff --git a/svgbob/src/loc.rs b/svgbob/src/loc.rs index ce9c685..dbf0cf0 100644 --- a/svgbob/src/loc.rs +++ b/svgbob/src/loc.rs @@ -70,6 +70,12 @@ impl Loc { y: self.y, } } + pub fn in_left(&self, step: i32) -> Loc{ + Loc{ + x: self.x - step, + y: self.y + } + } pub fn bottom(&self) -> Loc { Loc { x: self.x, @@ -83,6 +89,13 @@ impl Loc { } } + pub fn in_right(&self, step: i32) -> Loc { + Loc{ + x: self.x + step, + y: self.y + } + } + pub fn top_left(&self) -> Loc { Loc { x: self.x - 1, |