From 0b7b0a29a9bd0df81bfc63d3f316ae2ed581dee2 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 16 Mar 2023 19:00:47 +0500 Subject: [PATCH] Solved redundant spaces in multiline svg text annotations (#2833) The issue occurs because of svg pretty formatting - indent spaces tag treats as actual spaces. Because of that use some css magic to remove those spaces from tag and assign class to it's children 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. --- .../blenderbim/bim/data/styles/default.css | 12 ++++++------ .../blenderbim/bim/module/drawing/svgwriter.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/data/styles/default.css b/src/blenderbim/blenderbim/bim/data/styles/default.css index 78c9ac41b1..75e5877d5b 100644 --- a/src/blenderbim/blenderbim/bim/data/styles/default.css +++ b/src/blenderbim/blenderbim/bim/data/styles/default.css @@ -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); } diff --git a/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py b/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py index 8caff39558..e97b1d48ca 100644 --- a/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py +++ b/src/blenderbim/blenderbim/bim/module/drawing/svgwriter.py @@ -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 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)