asc2svg

asc2svg was intended to be an ASCII diagrams to SVG, like `ditaa` and Svgbob
Log | Files | Refs

asc2svg.go (3177B)


      1 package main
      2 
      3 import (
      4         "fmt"
      5         "strings"
      6         "io/ioutil"
      7 )
      8 
      9 const (
     10         symJoint = "+"
     11         symLineV = "|"
     12         symLineH = "-"
     13 )
     14 
     15 
     16 /* Drawable interface and shape structs that implement it */
     17 type drawable interface {
     18         draw()
     19 }
     20 type canvas struct {
     21         x0, y0, x1, y1 int
     22         array [][]rune
     23         processed [][]bool
     24         shapes []drawable
     25 }
     26 type block struct {
     27         x0, y0, x1, y1 uint
     28 }
     29 type line struct {
     30         x0, y0, x1, y1 uint
     31 }
     32 
     33 /* Draw functions for each drawable element */
     34 func (b block) draw() {
     35         fmt.Printf("Block covering (%v, %v) to (%v, %v)", b.x0, b.y0, b.x1, b.y1)
     36 }
     37 func (l line) draw() {
     38         fmt.Printf("Line covering (%v, %v) to (%v, %v)", l.x0, l.y0, l.x1, l.y1)
     39 }
     40 
     41 /* Initialization ("constructor") */
     42 func NewCanvas(raw_ascii string) *canvas {
     43         lines := strings.Split(raw_ascii, "\n")
     44         // 1. Find width and height, and create 2D slice / "array" (rename later)
     45         height := len(lines)
     46         width := 0
     47         // TODO: Optimize later. Or just don't. 
     48         for i := 0; i < height; i++ {
     49                 if len(lines[i]) > width {
     50                         width = len(lines[i])
     51                 }
     52         }
     53         c := canvas{0, 0, width , height, nil, nil, nil}
     54         // 2. Make slices to represent the ascii canvas
     55         c.array = make([][]rune, height)
     56         c.processed = make([][]bool, height)
     57         for y := 0; y < height; y++ {
     58                 c.array[y] = make([]rune, width)
     59                 c.processed[y] = make([]bool, width)
     60                 for x, char := range lines[y] {
     61                         c.array[y][x] = rune(char)
     62                 }
     63         }
     64         return &c
     65 }
     66 /* Analysis / detection functions */
     67 func (c *canvas) analyze() {
     68         for y := 0; y < len(c.array); y++ {
     69                 for x, char := range c.array[y] {
     70                         if c.processed[y][x] {
     71                                 continue
     72                         }
     73                         switch string(char) {
     74                                 case symJoint:
     75                                         c.detect_block(x, y)
     76                                         //c.detect_line(x, y)
     77                                 case symLineV:
     78                                 default:
     79                         }
     80                 }
     81         }
     82 }
     83 func (c *canvas) detect_block(x, y int) {
     84         fmt.Printf("block detection at (%v, %v)\n", x, y)
     85         /* Nothing to see here! Just delete :]
     86         c.shapes = append(c.shapes, block{1, 2, 3, 4})
     87         c.shapes = append(c.shapes, block{5, 6, 7, 8})
     88         */
     89 }
     90 /* ^ NOTE: Using a canvas *pointer* instead of passing by value makes so that 
     91  * the contents are persisted (just like in C)
     92  */
     93 
     94 
     95 /* File management */
     96 func check(e error) {
     97         if e != nil {
     98                 panic(e)
     99         }
    100 }
    101 
    102 func main() {
    103         figfile, err := ioutil.ReadFile("./fig.note")
    104         check(err)
    105         figascii := string(figfile)
    106         main_canvas := NewCanvas(figascii)
    107         main_canvas.analyze()
    108         for i := 0; i < len(main_canvas.shapes); i++ {
    109                 main_canvas.shapes[i].draw()
    110                 fmt.Println()
    111         }
    112 }