Created
June 7, 2018 11:08
-
-
Save rhee-airilab/1e216e515c7b8c63469f3fea96cf3e93 to your computer and use it in GitHub Desktop.
python simplify svg for webgl demo problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from traceback import print_exc | |
| import sys | |
| def simplify_svg(svg_str,to_integer=True): | |
| def to_number(s): | |
| try: | |
| if to_integer: | |
| return int(float(str(s).strip())) | |
| else: | |
| return float(str(s).strip()) | |
| except: | |
| print(('number parsing error',s),file=sys.stderr) | |
| # python2-3 workaround | |
| try: from itertools import izip | |
| except: izip = zip | |
| def pairwise(iterable): | |
| a = iter(iterable) | |
| return izip(a, a) | |
| def parse_coord(coord_str): | |
| _x, _y = pairwise(re.split(r', ',coord_str)) | |
| return to_number(_x),to_number(_y) | |
| def parse_coord_list(coord_list_str): | |
| return [(to_number(_x),to_number(_y)) for _x, _y in pairwise(re.split(r', ',coord_list_str))] | |
| svg_seq = re.split(r'([mcz])',svg_str)[1:] | |
| start_coord = (0.0, 0.0) | |
| last_coord = (0.0, 0.0) | |
| simplified_seq = [] | |
| i = 0 | |
| while i < len(svg_seq): | |
| code = svg_seq[i] | |
| print(('trace','i',i,'code',code)) | |
| lx, ly = last_coord | |
| if code in 'MLCSQT': # <code> x1 y1 x2 y2 ... | |
| param = svg_seq[i+1] | |
| for x, y in parse_coord_list(param): | |
| simplified_seq.append('{}{},{}'.format(code,x,y)) | |
| lx, ly = x, y | |
| last_coord = lx, ly | |
| if code == 'M': | |
| start_coord = last_coord | |
| i += 2 | |
| continue | |
| if code in 'mlcsqt': # <code> dx1 dy1 dx2 dy2 ... | |
| param = svg_seq[i+1] | |
| for dx, dy in parse_coord_list(param): | |
| simplified_seq.append('{}{},{}'.format(code,dx,dy)) | |
| lx, ly = last_coord[0] + dx, last_coord[1] + dy | |
| last_coord = lx, ly | |
| if code == 'm': | |
| start_coord = last_coord | |
| i += 2 | |
| continue | |
| if code in 'H': # <code> x1 x2 ... | |
| param = svg_seq[i+1] | |
| for x in [to_number(s) for s in re.split(r', ',param)]: | |
| simplified_seq.append('{}{}'.format(code,n)) | |
| lx, ly = x, last_coord[1] | |
| last_coord = lx, ly | |
| i += 2 | |
| continue | |
| if code in 'V': # <code> y1 y2 ... | |
| param = svg_seq[i+1] | |
| for y in [to_number(s) for s in re.split(r', ',param)]: | |
| simplified_seq.append('{}{}'.format(code,n)) | |
| lx, ly = last_coord[0], y | |
| last_coord = lx, ly | |
| i += 2 | |
| continue | |
| if code in 'h': # <code> dx1 dx2 ... | |
| param = svg_seq[i+1] | |
| for dx in [to_number(s) for s in re.split(r', ',param)]: | |
| simplified_seq.append('{}{}'.format(code,n)) | |
| lx, ly = last_coord[0] + dx, last_coord[1] | |
| last_coord = lx, ly | |
| i += 2 | |
| continue | |
| if code in 'v': # <code> dy1 dy2 ... | |
| param = svg_seq[i+1] | |
| for dy in [to_number(s) for s in re.split(r', ',param)]: | |
| simplified_seq.append('{}{}'.format(code,n)) | |
| lx, ly = last_coord[0], last_coord[1] + dy | |
| last_coord = lx, ly | |
| i += 2 | |
| continue | |
| if code in 'Zz': # Z or z | |
| simplified_seq.append('{}'.format(code)) | |
| last_coord = start_coord | |
| i += 1 | |
| continue | |
| # if code == 'A': # A rx ry a f1 f2 x y | |
| # pass | |
| # if code == 'a': # a rx ry a f1 f2 dx dy | |
| # pass | |
| raise RuntimeError(('unknown code',code)) | |
| return ' '.join(simplified_seq) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment