1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
//! generate an SVG from the ascii text using the default settings
//!
//! ```
//! let input = r#"
//! .-------------------------------------.
//! | Hello here and there and everywhere |
//! '-------------------------------------'
//! "#;
//! println!("svg: {}",svgbob::to_svg(input));
//! ```
//! 
//! <svg font-family="Electrolize,Titillium Web, Trebuchet MS, Arial" font-size="14" height="80" width="344" xmlns="http://www.w3.org/2000/svg">
//! <defs>
//! <marker id="triangle" markerHeight="10" markerUnits="strokeWidth" markerWidth="10" orient="auto" refX="0" refY="5" viewBox="0 0 14 14">
//! <path d="M 0 0 L 10 5 L 0 10 z"/>
//! </marker>
//! </defs>
//! <style>
//!     line, path {
//!       stroke: black;
//!       stroke-width: 1;
//!     }
//! </style>
//! <path d=" M 36 28 L 36 48 M 40 24 A 4 4 0 0 0 36 28 M 40 24 L 336 24 M 340 28 L 340 48 M 340 28 A 4 4 0 0 0 336 24 M 36 32 L 36 48 M 340 32 L 340 48 M 36 48 L 36 52 A 4 4 0 0 0 40 56 L 336 56 M 340 48 L 340 52 M 336 56 A 4 4 0 0 0 340 52" fill="none"/>
//! <path d="" fill="none" stroke-dasharray="3 3"/>
//! <text x="50" y="44">
//! Hello here and there and everywhere
//! </text>
//! </svg>
//! 
//! 
//#![deny(warnings)]
#![feature(test)]
extern crate svg;
extern crate unicode_width;
#[cfg(test)]
#[macro_use] 
extern crate pretty_assertions;



use svg::Node;
use svg::node::element::Circle as SvgCircle;
use svg::node::element::Path as SvgPath;
use svg::node::element::Line as SvgLine;
use svg::node::element::Text as SvgText;
use svg::node::element::Style;
use svg::node::element::SVG;
use svg::node::element::Definitions;
use svg::node::element::Marker;
use optimizer::Optimizer;
use self::Feature::Arrow;
use self::Feature::Circle;
use self::Feature::Nothing;
use self::Stroke::Solid;
use self::Stroke::Dashed;
use unicode_width::UnicodeWidthStr;
use unicode_width::UnicodeWidthChar;
use patterns::FocusChar;

mod optimizer;
mod patterns;


/// generate an SVG from the ascii text input
///
/// Usage:
/// 
/// ```
/// let input = "------->";
/// println!("svg: {}", svgbob::to_svg(input));
/// ``` 
/// 
/// commercial version enhances memes automatically
pub fn to_svg(input: &str) -> SVG {
    Grid::from_str(&input, &Settings::default()).get_svg()
}

pub fn to_svg_with_size(input: &str, text_width: f32, text_height: f32) -> SVG {
    let settings = Settings::with_size(text_width, text_height);
    Grid::from_str(&input, &settings).get_svg()
}

pub fn to_svg_with_size_nooptimization(input: &str, text_width: f32, text_height: f32) -> SVG {
    let mut settings = Settings::no_optimization();
    settings.text_width = text_width;
    settings.text_height = text_height;
    Grid::from_str(&input, &settings).get_svg()
}


///  optimization options:
///  1. None -> Fastest, but not correct looking (paths and text are not reduced)
///  2. Fast -> Fast and correct looking (text are reduced)
///  3. All -> Correct looking but slow (paths and text are reduced)
#[derive(Debug)]
#[derive(Clone)]
pub struct Settings {
    pub text_width: f32,
    pub text_height: f32,
    /// do optimization? if false then every piece are disconnected
    optimize: bool,
    /// if optmization is enabled,
    /// true means all reduceable paths will be in 1 path definition
    compact_path: bool,
}

impl Settings {

    pub fn with_size(text_width: f32, text_height: f32) -> Self{
         Settings{
            text_width: text_width,
            text_height: text_height,
            optimize: true,
            compact_path: true,
         }
    }
    pub fn no_optimization() -> Settings {
        let mut settings = Settings::default();
        settings.optimize = false;
        settings.compact_path = false;
        settings
    }

    pub fn separate_lines() -> Settings {
        let mut settings = Settings::default();
        settings.optimize = true;
        settings.compact_path = false;
        settings
    }

    pub fn compact() -> Settings {
        let mut settings = Settings::default();
        settings.optimize = true;
        settings.compact_path = true;
        settings
    }
}

impl Default for Settings {
    fn default() -> Settings {
        Settings {
            text_width: 8.0,
            text_height: 16.0,
            optimize: true,
            compact_path: true,
        }
    }
}

enum SvgElement {
    Circle(SvgCircle),
    Line(SvgLine),
    Path(SvgPath),
    Text(SvgText),
}


#[derive(PartialEq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum Stroke {
    Solid,
    Dashed,
}


#[derive(PartialEq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum Feature {
    Arrow, //end
    Circle, //start
    Nothing,
}

#[derive(PartialEq)]
#[derive(Debug)]
#[derive(Clone)]
pub struct Point {
    x: f32,
    y: f32,
}
impl Point {
    fn new(x: f32, y: f32) -> Point {
        Point { x: x, y: y }
    }
}

#[derive(Debug)]
#[derive(Clone)]
#[derive(PartialEq)]
pub struct Loc {
    pub x: i32,
    pub y: i32,
}

impl Loc {
    pub fn new(x: i32, y: i32) -> Loc {
        Loc { x: x, y: y }
    }

    pub fn top(&self) -> Loc {
        Loc {
            x: self.x,
            y: self.y - 1,
        }
    }
    pub fn left(&self) -> Loc {
        Loc {
            x: self.x - 1,
            y: self.y,
        }
    }
    pub fn bottom(&self) -> Loc {
        Loc {
            x: self.x,
            y: self.y + 1,
        }
    }
    pub fn right(&self) -> Loc {
        Loc {
            x: self.x + 1,
            y: self.y,
        }
    }

    pub fn top_left(&self) -> Loc {
        Loc {
            x: self.x - 1,
            y: self.y - 1,
        }
    }

    pub fn top_right(&self) -> Loc {
        Loc {
            x: self.x + 1,
            y: self.y - 1,
        }
    }

    pub fn bottom_left(&self) -> Loc {
        Loc {
            x: self.x - 1,
            y: self.y + 1,
        }
    }

    pub fn bottom_right(&self) -> Loc {
        Loc {
            x: self.x + 1,
            y: self.y + 1,
        }
    }

    /// make a lower and upper bound loc with
    /// ry units top + ry units bottom
    /// rx units left + rx units right
    pub fn get_range(&self, rx: i32, ry: i32 ) -> (Loc, Loc) {
        let loc1 = Loc::new(self.x - rx, self.y - ry);
        let loc2 = Loc::new(self.x + rx, self.y + ry);
        (loc1, loc2)
    }

}

#[derive(Debug)]
#[derive(Clone)]
#[derive(PartialEq)]
pub enum Element {
    Circle(Point, f32, String),
    Line(Point, Point, Stroke, Feature),
    Arc(Point, Point, f32, bool, Stroke, Feature),
    Text(Loc, String),
    Path(Point, Point, String, Stroke),
}

impl Element {
    fn solid_circle(c: &Point, r: f32) -> Element{
        Element::Circle(c.clone(), r, "solid".into())
    }
    fn open_circle(c: &Point, r: f32) -> Element{
        Element::Circle(c.clone(), r, "open".into())
    }
    fn solid_line(s: &Point, e: &Point) -> Element {
        Element::line(s, e, Solid, Nothing)
    }

    fn arrow_line(s: &Point, e: &Point) -> Element {
        Element::line(s,e,Solid,Arrow)
    }

    fn line(s: &Point, e: &Point, stroke: Stroke, feature: Feature) -> Element {
        Element::Line(s.clone(), e.clone(), stroke, feature)
    }
    fn arc(s: &Point, e: &Point, radius: f32, sweep: bool) -> Element {
        Element::Arc(s.clone(), e.clone(), radius, sweep, Solid, Nothing)
    }

    fn arrow_arc(s: &Point, e: &Point, radius: f32, sweep: bool) -> Element {
        Element::Arc(s.clone(), e.clone(), radius, sweep, Solid, Arrow)
    }

    // if this element can reduce the other, return the new reduced element
    // for line it has to be collinear and in can connect start->end->start
    // for text, the other text should apear on the right side of this text
    fn reduce(&self, other: &Element) -> Option<Element> {
        match *self {
            Element::Line(ref s, ref e, ref stroke, ref feature) => {
                match *other {
                    Element::Line(ref s2, ref e2, ref stroke2, ref feature2) => {
                        // note: dual 3 point check for trully collinear lines
                        if collinear(s, e, s2) && collinear(s, e, e2) && e == s2 &&
                           stroke == stroke2 && *feature == Nothing
                           && *feature2 != Circle
                           {
                            let reduced = Some(Element::Line(s.clone(),
                                                             e2.clone(),
                                                             stroke.clone(),
                                                             feature2.clone()));
                            reduced
                        } else {
                            None
                        }
                    }
                    _ => None,
                }
            }
            Element::Text(ref loc, ref text) => {
                match *other {
                    Element::Text(ref loc2, ref text2) => {
                        // reduce if other is next to it
                        let uwidth = text.width() as i32;
                        if loc.y == loc2.y && loc.x + uwidth == loc2.x {
                            let merged_text = text.clone() + text2;
                            let reduced = Some(Element::Text(loc.clone(), merged_text));
                            reduced
                        } else {
                            None
                        }
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    /// convert drawing element to SVG element
    fn to_svg(&self, settings: &Settings) -> SvgElement {
        match *self {
            Element::Circle(ref c, r, ref class) => {
                let svg_circle = SvgCircle::new()
                    .set("class",class.clone())
                    .set("cx", c.x)
                    .set("cy", c.y)
                    .set("r", r);

                SvgElement::Circle(svg_circle)
            },
            Element::Line(ref s, ref e, ref stroke, ref feature) => {
                let mut svg_line = SvgLine::new()
                    .set("x1", s.x)
                    .set("y1", s.y)
                    .set("x2", e.x)
                    .set("y2", e.y);

                match *feature {
                    Arrow => {
                        svg_line.assign("marker-end", "url(#triangle)");
                    },
                    Circle => {
                        svg_line.assign("marker-start", "url(#circle)");
                    },
                    Nothing => (),
                };
                match *stroke {
                    Solid => (),
                    Dashed => {
                        svg_line.assign("stroke-dasharray", (3, 3));
                        svg_line.assign("fill", "none");
                    }
                };

                SvgElement::Line(svg_line)
            }
            Element::Arc(ref s, ref e, radius, sweep, _, ref feature) => {
                let sweept = if sweep { "1" } else { "0" };
                let d = format!("M {} {} A {} {} 0 0 {} {} {}",
                                s.x,
                                s.y,
                                radius,
                                radius,
                                sweept,
                                e.x,
                                e.y);
                let mut svg_arc = SvgPath::new()
                    .set("d", d)
                    .set("fill", "none");
                match *feature {
                    Arrow => {
                        svg_arc.assign("marker-end", "url(#triangle)");
                    },
                    Circle => {
                        svg_arc.assign("marker-start", "url(#circle)");
                    },
                    Nothing => (),
                };
                SvgElement::Path(svg_arc)
            }
            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 text_node = svg::node::Text::new(string.to_string());
                svg_text.append(text_node);
                SvgElement::Text(svg_text)
            }
            Element::Path(_, _, ref d, ref stroke) => {
                let mut path = SvgPath::new()
                    .set("d", d.to_owned())
                    .set("fill", "none");

                match *stroke {
                    Solid => (),
                    Dashed => {
                        path.assign("stroke-dasharray", (3, 3));
                    }
                };
                SvgElement::Path(path)
            }
        }
    }
}


// 3 points are collinear when the area of the triangle connecting them is 0;
fn collinear(a: &Point, b: &Point, c: &Point) -> bool {
    a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) == 0.0
}



#[derive(Debug)]
pub struct Grid {
    settings: Settings,
    index: Vec<Vec<String>>
}
impl Grid {
    /// instantiate a grid from input ascii text
    /// Issues:
    /// 1. 2-width, 5 bytes, single character  i.e. 统
    /// 2. 1-width, 2 bytes, single character  i.e. ö 
    /// 3. 1-width, 3 bytes, single character  i.e. o͡͡͡
    pub fn from_str(s: &str, settings: &Settings) -> Grid {
        let lines: Vec<&str> = s.lines().collect();
        let mut rows: Vec<Vec<String>> = Vec::with_capacity(lines.len());
        for line in lines{
            let mut row: Vec<String> = Vec::with_capacity(line.chars().count());
            for ch in line.chars(){
                if let Some(1) = ch.width(){
                    row.push(format!("{}",ch));
                }
                else if let Some(2) = ch.width(){
                    row.push(format!("{}",ch));
                    row.push(format!("\0"));//push a blank
                }
                // if zero width char, append it to the previous string
                else if let Some(0) = ch.width(){
                    let prev: Option<String> = row.pop();
                    match prev{
                        Some(mut prev) => {
                            prev.push(ch);
                            row.push(prev);
                        },
                        None => (),
                    }
                }
            }
            rows.push(row);
        }
        Grid {
            settings: settings.clone(),
            index: rows,
        }
    }

    /// reassemble the Grid content into a string
    /// trimming unneeded whitespace to the right for every line
    pub fn to_string(&self) -> String {
        let mut buff = String::new();
        let mut do_ln = false;
        for row in self.index.iter(){
            let mut line = String::new();
            if do_ln{//first line don't do \n
                buff.push('\n');
            }else{
                do_ln = true;
            }
            for cell in row{
                line.push_str(cell);
            }
            buff.push_str(&line);
        }
        buff
    }

    pub fn rows(&self) -> usize {
        self.index.len()
    }

    /// get the maximum row len
    pub fn columns(&self) -> usize {
        self.index.iter()
            .map(|r| r.len())
            .max()
            .unwrap_or(0)
    }


    /// get a character at this location
    /// widths are computed since there are
    /// characters that spans 2 columns
    /// and characters that has 0 width
    ///
    pub fn get(&self, loc: &Loc) -> Option<&String> {
        match self.index.get(loc.y as usize) {
            Some(row) => {
                row.get(loc.x as usize)
            }
            None => None,
        }
    }

    /// put a text into this location
    /// prepare the grid for this location first
    pub fn put(&mut self, loc: &Loc, s: &str) {
        let new_loc = self.accomodate(loc);
        if let Some(row) = self.index.get_mut(new_loc.y as usize){
            if let Some(cell) = row.get_mut(new_loc.x as usize){
                *cell = s.to_owned();
            }
            else{
                panic!("no cell on this {}", new_loc.x);
            }
        }else{
            panic!("no row on this {}", new_loc.y);
        }
    }

    /// insert a new line to at this point
    pub fn insert_line(&mut self, line: usize) {
        self.accomodate(&Loc::new(0, line as i32));
        self.index.insert(line, vec![]);
    }

    /// join this line to the previous line
    pub fn join_line(&mut self, line: usize) {
        let mut row = self.index.remove(line);
        self.index.get_mut(line - 1)
            .map( | prev | prev.append(&mut row));
    }

    /// get the line len at this index
    pub fn get_line_len(&self, line: usize) -> Option<usize> {
        self.index.get(line).map(|r|r.len())
    }

    /// prepare the grid to accomodate this loc
    /// if loc.y < 0 => insert abs(loc.y) rows at element 0 to self.index
    /// if loc.y > row.y => append (loc.y-row.y) rows to the self.x 
    /// if loc.x < 0 => insert abs(loc.x) columns at element 0, to all rows
    /// if loc.x > row.x => append (loc.x-row.x) elements to the row 
    /// returns the corrected location, -1,-1 will be on 0,0
    pub fn accomodate(&mut self, loc: &Loc) -> Loc {
        let mut new_loc = loc.clone();
        if loc.y < 0 {
            let lack_row = (0 - loc.y) as usize; // 0 - -5 = 5
            for _ in 0..lack_row{
                self.index.insert(0,vec![]);
            }
            new_loc.y = 0;
        }
        if loc.x < 0 {
            let lack_cell = (0 - loc.x) as usize;
            let add_cells: String = " ".repeat(lack_cell);
            // insert add_cells to all rows at 0
            for row in self.index.iter_mut(){
                row.insert(0, add_cells.clone());
            }
            new_loc.x = 0;
        }

        // check again using the new location adjusted
        // for missing cells 
        if new_loc.y >= self.index.len() as i32 {
            let lack_row = new_loc.y - self.index.len() as i32 + 1;
            let mut add_rows: Vec<Vec<String>> = Vec::with_capacity(lack_row as usize);
            for _ in 0..lack_row{
                add_rows.push(vec![]);
            }
            self.index.append(&mut add_rows);
        }
        // IMPORTANT NOTE:
        // using new_loc as adjusted when -negative
        // is important since the priliminary rows inserted
        // are just empty rows
        if let Some(row) = self.index.get_mut(new_loc.y as usize){
            if new_loc.x >= row.len() as i32 {
                let lack_cell = new_loc.x - row.len() as i32 + 1;
                let mut add_cells:Vec<String> = Vec::with_capacity(lack_cell as usize);
                for _ in 0..lack_cell{
                    add_cells.push(" ".to_string());// use space for empty cells
                }
                (&mut *row).append(&mut add_cells);
            }
        }
        new_loc
    }


    /// Vector arranged in row x col
    pub fn get_text_in_range(&self, loc1: &Loc, loc2: &Loc) -> Vec<Vec<Option<&String>>> {
        let x1 = std::cmp::min(loc1.x, loc2.x);
        let y1 = std::cmp::min(loc1.y, loc2.y);
        let x2 = std::cmp::max(loc2.x, loc1.x);
        let y2 = std::cmp::max(loc2.y, loc1.y);
        let mut text = Vec::with_capacity((y2 - y1 + 1) as usize);
        for j in y1..y2+1{
            let mut row = Vec::with_capacity((x2 - x1 + 1) as usize);
            for i in x1..x2+1{
                let loc = Loc::new(i,j);
                let cell = self.get(&loc);
                row.push(cell);
            }
            text.push(row);
        }
        text
    }

    
    /// get the focus char at this location
    pub fn get_focuschar(&self, loc: &Loc) -> FocusChar {
        FocusChar::new(&loc,self)
    }


    /// vector of each elements arranged in rows x columns
    fn get_all_elements(&self) -> Vec<Vec<Vec<Element>>> {
        let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len());
        let mut y = 0;
        for line in &self.index{
            let mut x = 0;
            let mut row: Vec<Vec<Element>> = Vec::with_capacity(line.len());
            for _ in line {
                let loc = Loc::new(x,y);
                let focus_char = self.get_focuschar(&loc);
                let cell_elements = focus_char.get_elements();
                row.push(cell_elements);
                x += 1;
            }
            rows.push(row);
            y += 1;
        }
        rows
    }

    /// each component has its relative location retain
    /// use this info for optimizing svg by checking closest neigbor
    fn get_svg_nodes(&self) -> Vec<SvgElement> {
        let mut nodes = vec![];
        let start = std::time::SystemTime::now();
        let elements = self.get_all_elements();
        let input = if self.settings.optimize {
            let now = std::time::SystemTime::now();
            let optimizer = Optimizer::new(elements);
            let optimized_elements = optimizer.optimize(&self.settings);
            optimized_elements
        } else {
            // flatten Vec<Vec<Vec<Elements>>> to Vec<Element>
            elements.into_iter().flat_map(
                |elm| {
                    elm.into_iter().flat_map(|e2|e2)
                }
            ).collect()
        };
        for elem in input {
            let element:SvgElement = elem.to_svg(&self.settings);
            nodes.push(element);
        }
        nodes
    }


    /// get the generated svg according to the settings specified
    pub fn get_svg(&self) -> SVG {
        let nodes = self.get_svg_nodes();
        let width = self.settings.text_width * self.columns()  as f32;
        let height = self.settings.text_height * self.rows() as f32;
        let mut svg = SVG::new()
            .set("font-size", 14)
            .set("font-family",
                "arial"
                )
            .set("width", width)
            .set("height", height);

        svg.append(get_defs());
        svg.append(get_styles());

        for node in nodes {
            match node {
                SvgElement::Circle(circle) => {
                    svg.append(circle);
                }
                SvgElement::Line(line) => {
                    svg.append(line);
                }
                SvgElement::Path(path) => {
                    svg.append(path);
                }
                SvgElement::Text(text) => {
                    svg.append(text);
                }
            }
        }
        svg
    }
}

fn get_defs() -> Definitions {
    let mut defs = Definitions::new();
    defs.append(arrow_marker());
    defs
}

fn get_styles() -> Style {
    let style = r#"
    line, path {
      stroke: black;
      stroke-width: 2;
      stroke-opacity: 1;
      fill-opacity: 1;
      stroke-linecap: round;
      stroke-linejoin: miter;
    }
    circle {
      stroke: black;
      stroke-width: 2;
      stroke-opacity: 1;
      fill-opacity: 1;
      stroke-linecap: round;
      stroke-linejoin: miter;
      fill:white;
    }
    circle.solid {
      fill:black;
    }
    circle.open {
      fill:white;
    }
    tspan.head{
        fill: none;
        stroke: none;
    }
    "#;
    Style::new(style)
}

fn arrow_marker() -> Marker {
    let mut marker = Marker::new()
        .set("id", "triangle")
        .set("viewBox", "0 0 50 20")
        .set("refX", 15)
        .set("refY", 10)
        .set("markerUnits", "strokeWidth")
        .set("markerWidth", 10)
        .set("markerHeight", 10)
        .set("orient", "auto");

    let path = SvgPath::new().set("d", "M 0 0 L 30 10 L 0 20 z");
    marker.append(path);
    marker

}

fn escape_char(ch: &str) -> String {
    let escs = [("\"", "&quot;"), ("'", "&apos;"), ("<", "&lt;"), (">", "&gt;"), ("&", "&amp;")];
    let quote_match: Option<&(&str, &str)> = escs.iter()
        .find(|pair| {
            let &(e, _) = *pair;
            e == ch
        });
    let quoted: String = match quote_match {
        Some(&(_, quoted)) => String::from(quoted),
        None => {
            let mut s = String::new();
            s.push_str(&ch);
            s
        }
    };
    quoted

}

#[cfg(test)]
mod test_lib{
    use super::Grid;
    use super::Settings;
    use super::Loc;

    #[test]
    fn test_grid(){
        let g = Grid::from_str("a统öo͡͡͡", &Settings::compact());
        println!("{:?}", g.index);
        assert_eq!(g.index, vec![vec!["a".to_string(), "统".to_string(), "\u{0}".to_string(), "ö".to_string(), "o͡͡͡".to_string()]]);
    }

    #[test]
    fn test_text_in_range(){
        let txt = "
1234567890
abcdefghij
klmnopqrst
uvwxyz1234
567890abcd
        ";
        let g = Grid::from_str(txt, &Settings::compact());
        let loc = Loc::new(4,3);// at 'o'
        let (loc1, loc2) = loc.get_range(2,1);
        let text = g.get_text_in_range(&loc1, &loc2);
        assert_eq!(text, 
        vec![
            vec![Some(&"c".to_string()),Some(&"d".to_string()),Some(&"e".to_string()),Some(&"f".to_string()),Some(&"g".to_string())],
            vec![Some(&"m".to_string()),Some(&"n".to_string()),Some(&"o".to_string()),Some(&"p".to_string()),Some(&"q".to_string())],
            vec![Some(&"w".to_string()),Some(&"x".to_string()),Some(&"y".to_string()),Some(&"z".to_string()),Some(&"1".to_string())],
        ]);
    }

    #[test]
    fn test_to_string(){
        let txt = "The quick brown fox
jumps over
     the lazy dog.
       ]";
        let g = Grid::from_str(txt, &Settings::compact());
        assert_eq!(txt, &*g.to_string());
    }
    #[test]
    fn test_to_trimmed_string(){
        let txt = "

The quick brown fox

jumps over

     the lazy dog.

        ";
        let g = Grid::from_str(txt, &Settings::compact());
        assert_eq!(txt, &*g.to_string());
    }

    #[test]
    fn test_insert_text(){
        let txt = 
"1234567890
abcdefghij
klmnopqrst
uvwxyz1234
567890abcd";
        let mut g = Grid::from_str(txt, &Settings::compact());
        g.put(&Loc::new(-1,-1),"-");
        let expected = 
"-
 1234567890
 abcdefghij
 klmnopqrst
 uvwxyz1234
 567890abcd";
        assert_eq!(expected, &*g.to_string());
    }

    #[test]
    fn test_insert_text_after(){
        let txt = "\
1234567890
abcdefghij
klmnopqrst
uvwxyz1234
567890abcd\
";
        let mut g = Grid::from_str(txt, &Settings::compact());
        g.put(&Loc::new(11,5),"1");
        let expected = "\
1234567890
abcdefghij
klmnopqrst
uvwxyz1234
567890abcd
           1\
";
        assert_eq!(expected, &*g.to_string());
    }

}

#[cfg(test)]
mod benchmark{
    extern crate test;
    use self::test::Bencher;

    #[bench]
    fn convert(b: &mut Bencher) {
        b.iter(|| super::to_svg(&get_str()))
    }
    
fn get_str() -> String {
r#"
Svgbob is a diagramming model
which uses a set of typing characters
to approximate the desired shape.

       .---.
      /-o-/--
   .-/ / /->
  ( *  \/
   '-.  \
      \ /
       ' 
It uses a combination of this characters "`[(/<^.|+v*>\)]'"

It can do basic shapes such as:
                                                    ,
   +------+   .------.    .------.      /\        ,' `.
   |      |   |      |   (        )    /  \     .'     `.
   +------+   '------'    '------'    '----'     `.   ,'
     _______            ________                   `.'
    /       \      /\   \       \
   /         \    /  \   )       )
   \         /    \  /  /_______/
    \_______/      \/

    .-----------.       .   <.      .>  .
   (             )     (      )    (     )
    '-----+ ,---'       `>   '      `  <'
          |/
          

Quick logo scribbles
        .---.                      _
       /-o-/--       .--.         | |               .--.       |\       
    .-/ / /->       /--. \     .--(-|    .----.    //.-.\      | \..-.  
   ( *  \/         / o  )|     |  | |    |->  |   (+(-*-))      \((   ) 
    '-.  \        /\ |-//      .  * |    '----'    \\'-'/        \ '+'  
       \ /        \ '+'/        \__/                '--'          '-'   
        '          '--'            

Even unicode box drawing characters are supported
            ┌─┬┐  ╔═╦╗  ╓─╥╖  ╒═╤╕
            ├─┼┤  ╠═╬╣  ╟─╫╢  ╞═╪╡
            └─┴┘  ╚═╩╝  ╙─╨╜  ╘═╧╛

Mindmaps

                                        .-->  Alpha
                                       /
                                      .---->  Initial Release
      Planning  -------.             /         \      
                        \           /           '---> Patch 1
  Initial research       \         /             \
            \             \       /               '-->  Patch 2
             \             \     /
              \             \   .----------->   Beta
               \             \ /
                \          .---.
                 '------  (     )
                           `---'
                           /  \ \ \
                          /    \ \ \  
                      .--'      \ \ \
                     /           \ \ '---  Push backs
                    .             \ \      \
                   /|              \ \      '----> Setbacks
         Team   __/ .               \ \
                   /|                \ '-----> Reception
       Workload __/ .                 \
                   /|                  \
       Holiday  __/ .                   '--- Career change
                   / 
                  V  
            Bugs


It can do complex stuff such as circuit diagrams


 +10-15V           ___0,047R       
  *------o------o-|___|-o--o---------o----o-------o
         |      |       |  |         |    |       |
        ---     |       | .-.        |    |       |
  470uF ###     |       | | | 2k2    |    |       |
         | +    |       | | |        |    |      .-.
  *------o      '--.    | '-'       .+.   |      '-'
         |         |6   |7 |8    1k | |   |       |
        GND      .------------.     | |   |       |
                 |            |     '+'   |       |
                 |            |1     |  |/  BC    |
                 |            |------o--|   547   |
                 |            |      |  |`>       |
                 |            |     ,+.   |       |
                 |            | 220R| |   o----||-+  IRF9Z34
                 |            |     | |   |    |+->
                 |  MC34063   |     `+'   |    ||-+
                 |            |      |    |       |  BYV29     -12V6
                 |            |      '----'       o--|<-o----o--X OUT
                 |            |2                  |     |    |
                 |            |--|                C|    |    |
                 |            | GND         30uH  C|    |   --- 470
                 |            |3      1nF         C|    |   ###  uF
                 |            |-------||--.       |     |    | +
                 '------------'           |      GND    |   GND
                      5|   4|             |             |
                       |    '-------------o-------------o
                       |                           ___  |
                       '------/\/\/------------o--|___|-'
                                               |       1k0
                                              .-.
                                              | | 5k6 + 3k3
                                              | | in Serie
                                              '-'
                                               |
                                              GND
"#.to_string()
}
}