-
Notifications
You must be signed in to change notification settings - Fork 1
/
e_svg_transforming.py
138 lines (112 loc) · 3.82 KB
/
e_svg_transforming.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from ._common import *
from . import c_parsing
logger = get_logger(__file__, LOGGING_LEVEL)
class SvgTransforming(c_parsing.Parsing):
def transform_svg(
s,
cell: Cell,
xml_str: XmlStr,
) -> XmlStr:
document = xml.dom.minidom.parseString(xml_str)
svg = document.childNodes[0]
s.handle_vertical_align_offset(
svg=svg,
vertical_align_offset=cell.opts.vertical_align_offset,
)
s.handle_scale(
svg=svg,
scale=cell.opts.scale,
)
s.handle_positioning(
svg=svg,
positioning=cell.opts.positioning,
)
ret = s.compile_svg(svg)
return ret
@staticmethod
def handle_vertical_align_offset(
svg: Svg,
vertical_align_offset: VerticalAlignOffset,
):
if vertical_align_offset != 0:
attrs = SvgTransforming.get_attrs(svg)
a = svg.attributes
vertical_align = str(attrs.vertical_align + vertical_align_offset) + "ex"
a["style"].value = f"vertical-align: {vertical_align};"
@staticmethod
def handle_scale(
svg: Svg,
scale: Scale,
):
if scale != 100:
attrs = SvgTransforming.get_attrs(svg)
a = svg.attributes
vertical_align = str(attrs.vertical_align * scale / 100) + "ex"
width = str(attrs.width * scale / 100) + "ex"
height = str(attrs.height * scale / 100) + "ex"
a["style"].value = f"vertical-align: {vertical_align};"
a["width"].value = width
a["height"].value = height
elif scale == 100:
pass
else:
raise AdocMathException(DEAD_CODE_MSG)
@staticmethod
def handle_positioning(
svg: Svg,
positioning: Positioning,
):
if positioning == Positioning.POSITION:
attrs = SvgTransforming.get_attrs(svg)
vertical_position_perc = attrs.vertical_align / attrs.height
viewbox_post = ViewBox(
min_x=attrs.viewbox.min_x,
min_y=attrs.viewbox.min_y,
width=attrs.viewbox.width,
height=Ex(attrs.viewbox.height * (1 + vertical_position_perc)),
)
a = svg.attributes
a["viewBox"].value = str(viewbox_post)
a["height"].value = str(attrs.height - attrs.vertical_align) + "ex"
elif positioning == Positioning.DONT_POSITION:
pass
else:
raise AdocMathException(DEAD_CODE_MSG)
@staticmethod
def get_attrs(
svg: Svg,
) -> SvgAttributesParsed:
attrs = svg.attributes
raw = SvgAttributesRaw(
style=attrs["style"].value,
width=attrs["width"].value,
height=attrs["height"].value,
viewbox=attrs["viewBox"].value,
)
key = "vertical-align:"
idx_of_key = raw.style.index(key)
vertical_position: str = raw.style[idx_of_key + len(key) + 1 :]
def parse_viewbox(vb: str) -> ViewBox:
args = [Ex(float(el)) for el in vb.split(" ")]
return ViewBox(*args)
def parse_ex(ex_str: str) -> Ex:
interm = rshave(ex_str, ";")
interm = rshave(interm, "ex")
return Ex(float(interm))
return SvgAttributesParsed(
vertical_align=parse_ex(vertical_position),
width=parse_ex(raw.width),
height=parse_ex(raw.height),
viewbox=parse_viewbox(raw.viewbox),
)
@staticmethod
def compile_svg(
svg: Svg,
) -> XmlStr:
ret = svg.toprettyxml(indent=" " * 4)
# remove the weird newline issue
ret = join_with(
(s for s in ret.splitlines()),
os.linesep,
)
return XmlStr(ret)