Solved redundant spaces in multiline svg text annotations (#2833)

The issue occurs because of svg pretty formatting - indent spaces <text> tag treats as actual spaces. Because of that use some css magic to remove those spaces from <text> tag and assign class to it's children <tspan> tags.

Altough trying to solve this issue I've tried to disable svg pretty format and got empty svg as a result (only templates, styles, no annotations) for some reason.
This commit is contained in:
Andrej730
2023-03-16 19:00:47 +05:00
parent ae7f834a0f
commit 0b7b0a29a9
2 changed files with 14 additions and 8 deletions
@@ -48,12 +48,12 @@ text { /* 2.5mm */ fill: black; stroke: none; font-family: 'OpenGost Type B TT',
.PredefinedType-TEXT { fill: black; stroke: none; }
path.PredefinedType-TEXTLEADER { marker-end: url(#leader-marker); }
text.PredefinedType-TEXTLEADER { fill: black; stroke: none; }
text.title { /* 7mm */ font-size: 11.55px; }
text.header { /* 5mm */ font-size: 8.25px; }
text.large { /* 3.5mm */ font-size: 5.78px; }
text.regular { /* 2.5mm */ font-size: 4.13px; }
text.small { /* 1.8mm */ font-size: 2.97px; }
text.GRID { /* 5mm */ font-size: 8.25px; }
text.title, tspan.title { /* 7mm */ font-size: 11.55px; }
text.header, tspan.header { /* 5mm */ font-size: 8.25px; }
text.large, tspan.large { /* 3.5mm */ font-size: 5.78px; }
text.regular, tspan.regular { /* 2.5mm */ font-size: 4.13px; }
text.small, tspan.small { /* 1.8mm */ font-size: 2.97px; }
text.GRID, tspan.GRID { /* 5mm */ font-size: 8.25px; }
.material-blank { fill: white; }
.material-diagonal1 { fill: url(#diagonal1); }
.material-diagonal2 { fill: url(#diagonal2); }
@@ -660,22 +660,28 @@ class SvgWriter:
}
text_anchor = horizontal_alignment[ next(align for align in horizontal_alignment if align in box_alignment) ]
# after pretty indentation some redundant spaces can occur in svg tags
# this is why we apply "font-size: 0;" to the text tag to remove those spaces
# and add clases to the tspan tags
# ref: https://github.com/IfcOpenShell/IfcOpenShell/issues/2833#issuecomment-1471584960
text = text_obj.BIMTextProperties.value
text_tag = self.svg.text(
"",
class_=" ".join(self.get_attribute_classes(text_obj)),
**{
"text-anchor": text_anchor,
# using dominant-baseline because we plan to use <tspan> subtags
# otherwise alignment-baseline would be sufficient
"dominant-baseline": alignment_baseline,
"transform": transform,
"style": "font-size: 0;",
},
)
self.svg.add(text_tag)
classes = " ".join(self.get_attribute_classes(text_obj))
for line_number, text_line in enumerate(text.replace("\\n", "\n").split("\n")):
t_span = self.svg.tspan(text_line, insert=(text_position * self.svg_scale))
t_span = self.svg.tspan(text_line, insert=(text_position * self.svg_scale), class_=classes)
# doing it here and not in tspan constructor because it adds unnecessary spaces
t_span.update({"dy": f"{line_number}em"})
text_tag.add(t_span)