forked from toollabs/convert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FFlow2TextBySed.py
executable file
·117 lines (109 loc) · 4.91 KB
/
FFlow2TextBySed.py
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
# !/usr/bin/env python3
import sys
#inputfile = "in.svg"
#outputfile = "out.svg"
inputfile = (sys.argv[1])
outputfile = (sys.argv[2])
with open(inputfile, "r+", encoding='utf-8') as fp:
alllines = fp.readlines()
for i in range(len(alllines)):
if "<g" in alllines[i]:
line = alllines[i]
tags = line.replace(">", "<")
tags = tags.split("<")
# print(line)
flowParaNR = -10
for i3 in range(len(tags)):
tag = tags[i3].strip()
# words = words.split(" ")
# print(tag)
if tag == "":
pass
else:
tag = tag.split(" ")
for attribute in tag:
# print(attribute)
if attribute[0:11] == 'font-size=\"':
fs = attribute[11:]
# fs = fs.replace(">", " ")
fs = fs.strip()
fs = fs.split('"')
fs = fs[0]
fs = fs.replace("px", " ")
fs = float(fs)
# print(fs)
if "<flowRoot" in alllines[i]:
line = alllines[i]
tags = line.replace(">", "<")
tags = tags.split("<")
# print(line)
flowParaNR = -10
for i3 in range(len(tags)):
tag = tags[i3].strip()
# words = words.split(" ")
# print(tag)
if tag == "":
pass
else:
tag = tag.split(" ")
for attribute in tag:
# print(attribute)
if attribute[0:11] == 'font-size=\"':
fs = attribute[11:]
# fs = fs.replace(">", " ")
fs = fs.strip()
fs = fs.split('"')
fs = fs[0]
fs = fs.replace("px", " ")
fs = float(fs)
# print(fs)
if tag[0] == 'flowRoot':
flowRoottags = tag[1:]
cflowRoottags = " ".join(flowRoottags)
# print(cflowRoottags)
elif tag[0] == 'rect':
rx = 0.0 # Default value if there is no x
ry = 0.0 # Default value if there is no y
for attribute in tag:
if attribute[0:3] == 'x=\"':
# print(attribute)
rx = attribute[3:]
# tx = tx.replace(">", " ")
rx = rx.strip()
rx = rx.split('"')
rx = rx[0]
elif attribute[0:3] == "y=\"":
ry = attribute[3:-1].strip()
tx = float(rx)
# print(tx)
elif tag[0] == 'flowPara':
flowParatags = tag[1:]
cflowParatags = " ".join(flowParatags)
flowParaNR = i3
ty = float(ry) + .88476562*fs
elif i3 == flowParaNR+1:
#print(tag)
flowParaText = " ".join(tag[0:])
#print(flowParaText)
line = "<text x=\""+str(tx)+"\" y=\""+str(ty) + \
"\" "+str(cflowRoottags)+">" + \
"<tspan x=\""+str(tx)+"\" y=\""+str(ty)+"\" " + \
str(cflowParatags)+">"+str(flowParaText)+"</tspan></text>\n"
alllines[i] = line
dateiX = "".join(alllines)
# print(dateiX)
fp.close()
fp2 = open(outputfile, "w", encoding='utf-8')
fp2.write(dateiX)
fp2.close()
# sed -ri "s/<flowRoot([-[:alnum:]\.=\" \:\(\)\%\#\,\';]*)>[[:space:]]*<flowRegion([-[:alnum:]=:\" #;\.%\',]*)>[[:space:]]*<rect([-[:lower:][:digit:]\"= \.:;]*) x=\"([-[:digit:]\. ]+)\" y=\"([-[:digit:]\. ]+)\"([-[:alnum:]=\.\" \#:;\',]*)\/>[[:space:]]*<\/flowRegion>[[:space:]]*<flowPara([-[:alnum:]\.=\" \:\#;\%]*)>([-[:alnum:] \{\}\(\)\+\ \ \.\?\']+)<\/flowPara>[[:space:]]*<\/flowRoot>/<text x=\"\4\" y=\"\5\"\1><tspan x=\"\4\" y=\"\5\"\7>\8<\/tspan><\/text>/g" $i
# <flowRoot([-[:alnum:]\.=\" \:\(\)\%\#\,\';]*)>[[:space:]]*
# <flowRegion([-[:alnum:]=:\" #;\.%\',]*)>[[:space:]]*
# <rect([-[:lower:][:digit:]\"= \.:;]*) x=\"([-[:digit:]\. ]+)\" y=\"([-[:digit:]\. ]+)\"([-[:alnum:]=\.\" \#:;\',]*)\/>[[:space:]]*
# <\/flowRegion>[[:space:]]*
# <flowPara([-[:alnum:]\.=\" \:\#;\%]*)>
# ([-[:alnum:] \{\}\(\)\+\ \ \.\?\']+)
# <\/flowPara>[[:space:]]*
# <\/flowRoot>
# /
# <text x=\"\4\" y=\"\5\"\1><tspan x=\"\4\" y=\"\5\"\7>\8<\/tspan><\/text>/g" $i