asc2svg

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

commit bf9c292fe56a683516e53a7fa77d7d42e9bda5a6
parent 73c7be92aa96eef70ed13c83d0ed10cdf99e1420
Author: bkopf <vetlehaf@stud.ntnu.no>
Date:   Tue, 20 Nov 2018 21:56:32 +0100

Implement working basic functionality with the exception of arrows

Diffstat:
Ma2svg.py | 100+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mascii.txt | 30+++++++++++++++---------------
2 files changed, 74 insertions(+), 56 deletions(-)

diff --git a/a2svg.py b/a2svg.py @@ -54,6 +54,7 @@ class Figure: return self._figarray def process_char(self, y, x): + new_elements = None symbol = self._figarray[y][x] if symbol == '+': rects = self.rectangle(y, x) @@ -61,19 +62,23 @@ class Figure: # TODO: Refactor this text extraction for r in rects: r.extract_text(self) - self._elements += rects + new_elements = rects # If not a rectangle, it must be a line joint else: - new_lines = self.polyline(y, x, "FIND") - self._elements += new_lines - if symbol == '-': - new_lines = self.polyline(y, x, "RIGHT") - self._elements += new_lines - if symbol == '|': - new_lines = self.polyline(y, x, "DOWN") - self._elements += new_lines - - #new_element.process() + new_elements = self.polyline(y, x, "FIND") + # Finding lines without joints nor arrows should not be possible + #elif symbol == '-': + # new_elements = self.polyline(y, x, "RIGHT") + #elif symbol == '|': + # new_elements = self.polyline(y, x, "DOWN") + elif symbol == '^': + new_elements = self.polyline(y, x, "DOWN") + #new_elements += [Arrow()] + elif symbol == '<': + new_elements = self.polyline(y, x, "RIGHT") + + if new_elements: + self._elements += new_elements self._processed[y][x] = True def __str__(self): @@ -96,9 +101,10 @@ class Figure: return True return False - def __line_end_in(self, y, x): - if self.__has_char(y, x, '+'): - return True + def __joint_in(self, y, x): + return self.__has_char(y, x, '+') + + def __arrow_in(self, y, x): if self.__has_char(y, x, '<'): return True if self.__has_char(y, x, '>'): @@ -114,10 +120,13 @@ class Figure: MAYBE: The best option is to just use SVG 'Line'. Polylines do not handle crossing lines well, I think. (So TODO : refactor name) """ + # TODO: Refactor this mess entirely def polyline(self, y, x, direction): # TODO : Deal with arrows. Only one arrow is allowed for each direction, so this shouldn't be too hard # Find lines line_list = [] + # TODO: No reason for this horrible recursive mess! Just make a function + # for all the directions. if direction == "FIND": if self.__has_char(y+1, x, '|'): line_list += self.polyline(y, x, "DOWN") @@ -134,43 +143,65 @@ class Figure: while self.__has_char(y, x_test, '-'): self._processed[y][x_test] = True x_test -= 1 - if self.__line_end_in(y, x_test): - line_list.append(Line((y, x_test), (y, x))) + if self.__arrow_in(y, x_test): + line_list.append(Line((y, x_test - 1), (y, x))) + self._processed[y][x_test - 1] + elif self.__joint_in(y, x_test): + line_list.append(Line((y, x), (y, x_test))) + self._processed[y][x_test] else: - print("NOT LINE END: [{}, {}]".format(y, x_test)) line_list.append(Line((y, x_test + 1), (y, x))) x = x_test elif direction == "RIGHT": - x_test = x + 1 + x_test = x + if self.__has_char(y, x_test, '<'): + self._processed[y][x_test] = True + x -= 1 + x_test += 1 + x_test += 1 while self.__has_char(y, x_test, '-'): self._processed[y][x_test] = True x_test += 1 - if self.__line_end_in(y, x_test): + if self.__arrow_in(y, x_test): + line_list.append(Line((y, x), (y, x_test + 1))) + elif self.__joint_in(y, x_test): line_list.append(Line((y, x), (y, x_test))) else: - print("NOT LINE END: [{}, {}]".format(y, x_test)) line_list.append(Line((y, x), (y, x_test - 1))) x = x_test + + # Up should actually never be called, as we always find line tops. TODO: + # Remove? elif direction == "UP": + print("UUUUUUP") y_test = y + 1 while self.__has_char(y_test, x, '|'): self._processed[y_test][x] = True y_test -= 1 - if self.__line_end_in(y_test, x): + if self.__arrow_in(y_test, x): + line_list.append(Line((y_test-1, x), (y, x))) + elif self.__joint_in(y_test, x): line_list.append(Line((y_test, x), (y, x))) else: - print("NOT LINE END: [{}, {}]".format(y_test, x)) line_list.append(Line((y_test+1, x), (y, x))) y = y_test + + # WORKING MESS elif direction == "DOWN": - y_test = y + 1 + y_test = y + if self.__has_char(y_test, x, '^'): + self._processed[y_test][x] = True + y -= 1 + y_test = y + 1 + y_test += 1 while self.__has_char(y_test, x, '|'): self._processed[y_test][x] = True y_test += 1 - if self.__line_end_in(y_test, x): + if self.__arrow_in(y_test, x): + line_list.append(Line((y, x), (y_test+1, x))) + elif self.__joint_in(y_test, x): line_list.append(Line((y, x), (y_test, x))) else: - print("NOT LINE END: [{}, {}]".format(y_test, x)) line_list.append(Line((y, x), (y_test-1, x))) y = y_test else: @@ -341,24 +372,11 @@ class Joint: def __init__(self, position): # (pos should be a tuple) (self._x, self._y) = position - - - pass def get_position(self): return (self._x, self._y) - def process(self): - pass - - def find_connected(self, figure): - self._connected = [] - #if self._x > 0 and - - def get_connected(self): - return self._connected - # ~~The best alternative here is to use a *polyline*~~ nah # https://www.w3schools.com/graphics/svg_polyline.asp @@ -367,9 +385,6 @@ class Line: (self._y_start, self._x_start) = pos_start (self._y_stop, self._x_stop) = pos_stop - def process(self): - pass - def draw(self): svg_text = "\t<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" ".format( x_scaling * self._x_start * font_size, @@ -393,6 +408,9 @@ class Arrow(): pass + +### TESTING ### + with open("ascii.txt") as f: ascii_text = f.readlines() f.close() diff --git a/ascii.txt b/ascii.txt @@ -2,27 +2,27 @@ +----------------+ - | +-----+ + | Numbr 1 +-----+ | | | - +----------------+ | - ^ | - | | - | +-----------+---------+ - | +------+ A BLOCK +--+ - | | +---------------------+ | - | | | -+-----+-+--------+ +--------------------+-+ -| Block 1 +-+ Additional block +----+ -+-------+--------+ +----------+-----------+ | - | | | - | +----------+-----------+ | - +----------+ Second block +----+ + +--++---------+--+ | + ^|| | | + ||| v | + ||| +-----------+---------+ + ||| +--+ A BLOCK +--+ + ||| | +---------------------+ | + ||v | | ++-----++----+----+ +--------------------+-+ +| Block 1 +-+ Additional block +----+ +--------------------+ ++-------+--------+ +----------+-----------+ | | | + | | +-------->| BIG BOX BOYY | + | +----------+-----------+ | | | + +--------->| Second block |<---+ +--------------------+ | +----------------------+ | | | | | - List of stuff here | | | - Good stuff too, | | | I'd say | - | | | + +--------->| | | | | | | | | | |