summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJovansonlee Cesar <ivanceras@gmail.com>2018-08-02 11:46:29 +0800
committerJovansonlee Cesar <ivanceras@gmail.com>2018-08-02 11:46:29 +0800
commitfc09a1ddf2d5432b27c6e0718761f05b7cc6e048 (patch)
tree3ec1e1675fcecd3d8c4d97aacb9707f7e8d1299f
parent4980c318b7f93d0117f9b243f77dae5e9157945c (diff)
Add enhance_circle module as a different enhance trait, this is run first before any enhancements are applied
-rw-r--r--svgbob/src/enhance.rs59
-rw-r--r--svgbob/src/enhance_circle.rs200
-rw-r--r--svgbob/src/focus_char.rs33
-rw-r--r--svgbob/src/grid.rs34
-rw-r--r--svgbob/src/lib.rs1
-rw-r--r--svgbob/src/location.rs8
6 files changed, 272 insertions, 63 deletions
diff --git a/svgbob/src/enhance.rs b/svgbob/src/enhance.rs
index 75331e9..785e8f1 100644
--- a/svgbob/src/enhance.rs
+++ b/svgbob/src/enhance.rs
@@ -11,6 +11,7 @@ pub trait Enhance {
}
impl<'g> Enhance for FocusChar<'g> {
+
fn enhance(&self) -> (Vec<Fragment>, Vec<Location>) {
let mut elm = vec![];
let mut consumed = vec![];
@@ -49,8 +50,15 @@ impl<'g> Enhance for FocusChar<'g> {
let top_right = || Location::go(TopRight);
let bottom_left = || Location::go(BottomLeft);
let bottom_right = || Location::go(BottomRight);
+
+ let top2 = || Location::jump(Top,2);
+ let bottom2 = || Location::jump(Bottom,2);
let left2 = || Location::jump(Left,2);
let right2 = || Location::jump(Right,2);
+ let top_right2 = || top().go_right(2);
+ let top_left2 = || top().go_left(2);
+ let bottom_right2 = || bottom().go_right(2);
+ let bottom_left2 = || bottom().go_left(2);
// _ underscore
if self.is('_') {
@@ -217,57 +225,6 @@ impl<'g> Enhance for FocusChar<'g> {
elm.extend(vec![arc(w, c, 5), line(k, o)]);
consumed.push(this());
}
- // circle1
- // _
- // (_)
- //
- if self.is('_')
- && self.left().is('(') && self.right().is(')')
- && self.top().is('_'){
- elm.push(open_circle(m, 4));
- consumed.extend(vec![this(), left(), right(),top()]);
- }
-
- // circle2
- // .-.
- // ( + )
- // '-'
- if self.in_left(2).is('(')
- && self.in_right(2).is(')')
- && self.top().is('-')
- && self.bottom().is('-')
- && self.bottom_left().any("`'")
- && self.bottom_right().is('\'')
- && self.top_left().any(".,")
- && self.top_right().is('.'){
- println!("circle2 matched");
-
- elm.push(open_circle(m,6));
- consumed.extend(vec![left2(), right2(), top(), bottom(),
- bottom_left(), bottom_right(), top_left(), top_right()]);
- }
- // .--.
- // ( + )
- // `--'
- //
- // _
- // .' '.
- // ( 3 )
- // `._.'
- // ___
- // ,' `.
- // / \
- // | |
- // \ /
- // `.___,'
- //
- // ______
- // ,' `.
- // / \
- // | |
- // | |
- // \ /
- // `.______,'
(elm, consumed)
}
diff --git a/svgbob/src/enhance_circle.rs b/svgbob/src/enhance_circle.rs
new file mode 100644
index 0000000..5f504f3
--- /dev/null
+++ b/svgbob/src/enhance_circle.rs
@@ -0,0 +1,200 @@
+
+use focus_char::FocusChar;
+use fragments::Fragment;
+use location::Location;
+use location::Direction::{Bottom, BottomLeft, BottomRight, Left, Right, Top, TopLeft, TopRight};
+use block::Block::{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y};
+use point_block::PointBlock;
+use fragments::{self, line, arc, open_circle, arrow_line, dashed_line};
+
+pub trait EnhanceCircle {
+ fn enhance_circle(&self) -> (Vec<Fragment>, Vec<Location>);
+}
+
+impl<'g> EnhanceCircle for FocusChar<'g> {
+
+ fn enhance_circle(&self) -> (Vec<Fragment>, Vec<Location>) {
+ let mut elm = vec![];
+ let mut consumed = vec![];
+ let a = &PointBlock::block(A);
+ let _b = &PointBlock::block(B);
+ let c = &PointBlock::block(C);
+ let _d = &PointBlock::block(D);
+ let e = &PointBlock::block(E);
+ let _f = &PointBlock::block(F);
+ let _g = &PointBlock::block(G);
+ let _h = &PointBlock::block(H);
+ let _i = &PointBlock::block(I);
+ let _j = &PointBlock::block(J);
+ let k = &PointBlock::block(K);
+ let l = &PointBlock::block(L);
+ let m = &PointBlock::block(M);
+ let n = &PointBlock::block(N);
+ let o = &PointBlock::block(O);
+ let _p = &PointBlock::block(P);
+ let _q = &PointBlock::block(Q);
+ let _r = &PointBlock::block(R);
+ let _s = &PointBlock::block(S);
+ let _t = &PointBlock::block(T);
+ let u = &PointBlock::block(U);
+ let _v = &PointBlock::block(V);
+ let w = &PointBlock::block(W);
+ let _x = &PointBlock::block(X);
+ let y = &PointBlock::block(Y);
+
+ let this = || Location::this();
+ let top = || Location::go(Top);
+ let bottom = || Location::go(Bottom);
+ let left = || Location::go(Left);
+ let right = || Location::go(Right);
+ let top_left = || Location::go(TopLeft);
+ let top_right = || Location::go(TopRight);
+ let bottom_left = || Location::go(BottomLeft);
+ let bottom_right = || Location::go(BottomRight);
+
+ let top2 = || Location::jump(Top,2);
+ let bottom2 = || Location::jump(Bottom,2);
+ let left2 = || Location::jump(Left,2);
+ let right2 = || Location::jump(Right,2);
+ let top_right2 = || top().go_right(2);
+ let top_left2 = || top().go_left(2);
+ let bottom_right2 = || bottom().go_right(2);
+ let bottom_left2 = || bottom().go_left(2);
+
+ let top3 = || Location::jump(Top,3);
+ let bottom3 = || Location::jump(Bottom,3);
+ let left3 = || Location::jump(Left,3);
+ let right3 = || Location::jump(Right,3);
+ let top_right3 = || top().go_right(3);
+ let top_left3 = || top().go_left(3);
+ let bottom_right3 = || bottom().go_right(3);
+ let bottom_left3 = || bottom().go_left(3);
+
+ // circle 4
+ // _
+ // (_)
+ //
+ if self.is('_')
+ && self.left().is('(') && self.right().is(')')
+ && self.top().is('_'){
+ elm.push(open_circle(m, 4));
+ consumed.extend(vec![this(), left(), right(),top()]);
+ }
+
+ // circle 6
+ // __
+ // (__)
+ //
+ if self.is('_')
+ && self.left().is('(') && self.in_right(2).is(')')
+ && self.top().is('_') && self.top_right().is('_')
+ && self.right().is('_'){
+ elm.push(open_circle(m, 6));
+ consumed.extend(vec![this(), left(), right2(), top(), top_right(), right()]);
+ }
+
+ // circle 8
+ // .-.
+ // ( + )
+ // '-'
+ if self.in_left(2).is('(')
+ && self.in_right(2).is(')')
+ && self.top().is('-')
+ && self.bottom().is('-')
+ && self.bottom_left().any("`'")
+ && self.bottom_right().is('\'')
+ && self.top_left().any(".,")
+ && self.top_right().is('.'){
+ elm.push(open_circle(m,8));
+ consumed.extend(vec![left2(), right2(), top(), bottom(),
+ bottom_left(), bottom_right(), top_left(), top_right()]);
+ }
+ // circle 10
+ // .--.
+ // ( + )
+ // `--'
+ if self.in_left(2).is('(')
+ && self.in_right(3).is(')')
+ && self.top_left().any(".,")
+ && self.top().is('-')
+ && self.top_right().is('-')
+ && self.top_right().right().is('.')
+ && self.bottom_left().any("`'")
+ && self.bottom().is('-')
+ && self.bottom_right().is('-')
+ && self.bottom_right().right().is('\''){
+ elm.push(open_circle(&this().o(),10));
+ consumed.extend(vec![left2(), right3(), top_left(), top(), top_right(),
+ top_right2(), bottom_left(), bottom(), bottom_right(), bottom_right2(),
+ ]);
+ }
+
+ // Circle 12
+ // _
+ // .' '.
+ // ( + )
+ // `._.'
+ if self.in_left(3).is('(')
+ && self.in_right(3).is(')')
+ && self.in_top(2).is('_')
+ && self.bottom().is('_')
+ && self.top().in_left(2).any(",.")
+ && self.top_left().is('\'')
+ && self.top_right().any("`'")
+ && self.top().in_right(2).is('.')
+ && self.bottom().in_left(2).any("`'")
+ && self.bottom_left().is('.')
+ && self.bottom_right().any(".,")
+ && self.bottom().in_right(2).is('\''){
+ elm.push(open_circle(m, 12));
+ consumed.extend(vec![
+ left3(), right3(), top2(), bottom(), top_left2(), top_left(), top_right(), top_right2(),
+ bottom_left2(), bottom_left(), bottom_right(), bottom_right2()
+ ]);
+ }
+ //
+ // __
+ // ,' `.
+ // ( + ) 14
+ // `.__,'
+ //
+ // ___
+ // ,' `.
+ // / + \ 16
+ // \ /
+ // `.___,'
+ //
+ // ____
+ // ,' `.
+ // / + \ 18
+ // \ /
+ // `.____,'
+ //
+ // ____
+ // ,' `.
+ // / \
+ // ( + ) 20
+ // \ /
+ // `.____,'
+ //
+ //
+ // _____
+ // ,' `.
+ // / \
+ // ( + ) 22
+ // \ /
+ // `._____,'
+ //
+ //
+ // ______
+ // ,' `.
+ // / \
+ // | + | 24
+ // | |
+ // \ /
+ // `.______,'
+ //
+ (elm, consumed)
+ }
+}
+
diff --git a/svgbob/src/focus_char.rs b/svgbob/src/focus_char.rs
index a16563e..964b7a0 100644
--- a/svgbob/src/focus_char.rs
+++ b/svgbob/src/focus_char.rs
@@ -17,6 +17,7 @@ use element::{line,dashed_line,circle_start_line, square_start_line, circle_open
use location::Location;
use settings::Settings;
use enhance::Enhance;
+use enhance_circle::EnhanceCircle;
#[derive(Debug, Clone)]
pub struct FocusChar<'g> {
@@ -206,6 +207,20 @@ impl<'g> FocusChar<'g> {
}
}
+ /// return the ehance circle elements and the consumed locations
+ pub fn get_enhance_circle_elements(&self) -> (Vec<Element>, Vec<Loc>) {
+ let (fragments, consumed_location) = self.get_enhance_circle_fragments();
+ let mut elements: Vec<Element> = fragments
+ .into_iter()
+ .map(|frag| self.to_element(frag))
+ .collect();
+ let consumed_loc: Vec<Loc> = consumed_location
+ .into_iter()
+ .map(|location| self.loc.from_location(&location))
+ .collect();
+ (elements, consumed_loc)
+ }
+
/// return the ehance elements and the consumed locations
pub fn get_enhance_elements(&self) -> (Vec<Element>, Vec<Loc>) {
let (fragments, consumed_location) = self.get_enhance_fragments();
@@ -217,8 +232,6 @@ impl<'g> FocusChar<'g> {
.into_iter()
.map(|location| self.loc.from_location(&location))
.collect();
- elements.sort();
- elements.dedup();
(elements, consumed_loc)
}
@@ -229,8 +242,6 @@ impl<'g> FocusChar<'g> {
.into_iter()
.map(|frag| self.to_element(frag))
.collect();
- elements.sort();
- elements.dedup();
(elements)
}
@@ -291,6 +302,20 @@ impl<'g> FocusChar<'g> {
(elm, consumed)
}
+ fn get_enhance_circle_fragments(&self) -> (Vec<Fragment>, Vec<Location>) {
+ let mut elm: Vec<Fragment> = vec![];
+ let mut consumed: Vec<Location> = vec![];
+ let (enhanced, enhance_consumed) = self.enhance_circle();
+ elm.extend(enhanced);
+ consumed.extend(enhance_consumed);
+ elm.sort();
+ elm.dedup();
+ consumed.sort();
+ consumed.dedup();
+ (elm, consumed)
+ }
+
+
/// get the fragements generated at this focus character and the
/// consumed locations
fn get_fragments(&self) -> Vec<Fragment>{
diff --git a/svgbob/src/grid.rs b/svgbob/src/grid.rs
index e1c97d1..88c5eaf 100644
--- a/svgbob/src/grid.rs
+++ b/svgbob/src/grid.rs
@@ -113,7 +113,9 @@ impl Grid {
FocusChar::new(&loc, self)
}
- fn get_enhance_elements(&self) -> (Vec<Vec<Vec<Element>>>, Vec<Loc>){
+ /// process the enhancing of circle elements
+ /// this should be called before other elements are extracted from the grid
+ fn get_enhance_circle_elements(&self) -> (Vec<Vec<Vec<Element>>>, Vec<Loc>){
let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len());
let mut all_consumed_loc: Vec<Loc> = vec![];
for (y,line) in self.index.iter().enumerate() {
@@ -121,7 +123,7 @@ impl Grid {
for (x,_cell) in line.iter().enumerate() {
let loc = Loc::new(x as i32, y as i32);
let focus_char = self.get_focuschar(&loc);
- let (cell_elements, consumed_loc) = focus_char.get_enhance_elements();
+ let (cell_elements, consumed_loc) = focus_char.get_enhance_circle_elements();
all_consumed_loc.extend(consumed_loc);
row.push(cell_elements);
}
@@ -130,12 +132,36 @@ impl Grid {
(rows, all_consumed_loc)
}
+ /// process the enhanced circle elements first
+ /// then process the generic enhancements
+ fn get_enhance_elements(&self) -> (Vec<Vec<Vec<Element>>>, Vec<Loc>){
+ let (enhanced_circle_elm, circle_consumed_loc) = self.get_enhance_circle_elements();
+ let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len());
+ rows.extend(enhanced_circle_elm);
+ let mut all_consumed_loc: Vec<Loc> = vec![];
+ for (y,line) in self.index.iter().enumerate() {
+ let mut row: Vec<Vec<Element>> = Vec::with_capacity(line.len());
+ for (x,_cell) in line.iter().enumerate() {
+ let loc = Loc::new(x as i32, y as i32);
+ if !circle_consumed_loc.contains(&loc){
+ let focus_char = self.get_focuschar(&loc);
+ let (cell_elements, consumed_loc) = focus_char.get_enhance_elements();
+ all_consumed_loc.extend(consumed_loc);
+ row.push(cell_elements);
+ }
+ }
+ rows.push(row);
+ }
+ all_consumed_loc.extend(circle_consumed_loc);
+ (rows, all_consumed_loc)
+ }
+
/// vector of each elements arranged in rows x columns
/// returns all the elements and the consumed location
fn get_all_elements(&self) -> Vec<Vec<Vec<Element>>>{
- let (enhanced_rows, enhance_consumed_locs) = self.get_enhance_elements();
+ let (enhanced_elms, enhance_consumed_locs) = self.get_enhance_elements();
let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len());
- rows.extend(enhanced_rows);
+ rows.extend(enhanced_elms);
for (y, line) in self.index.iter().enumerate() {
let mut row: Vec<Vec<Element>> = Vec::with_capacity(line.len());
for (x, _cell) in line.iter().enumerate() {
diff --git a/svgbob/src/lib.rs b/svgbob/src/lib.rs
index bd43554..5b0e6f4 100644
--- a/svgbob/src/lib.rs
+++ b/svgbob/src/lib.rs
@@ -57,6 +57,7 @@ mod point_block;
mod block;
mod focus_char;
mod loc_block;
+mod enhance_circle;
mod enhance;
/// generate an SVG from the ascii text input
diff --git a/svgbob/src/location.rs b/svgbob/src/location.rs
index 686a7f3..df67b23 100644
--- a/svgbob/src/location.rs
+++ b/svgbob/src/location.rs
@@ -50,16 +50,16 @@ impl Location {
loc
}
- fn go_top(&self, step: usize) -> Self {
+ pub fn go_top(&self, step: usize) -> Self {
self.go_jump(Top, step)
}
- fn go_left(&self, step: usize) -> Self {
+ pub fn go_left(&self, step: usize) -> Self {
self.go_jump(Left, step)
}
- fn go_bottom(&self, step: usize) -> Self {
+ pub fn go_bottom(&self, step: usize) -> Self {
self.go_jump(Bottom, step)
}
- fn go_right(&self, step: usize) -> Self {
+ pub fn go_right(&self, step: usize) -> Self {
self.go_jump(Right, step)
}