mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-22 14:38:05 +00:00
draw_text_annotation refactor
Refactored a code a bit and replaced literal parsing with `text_obj.BIMTextProperties.text` since it's already done beforehand in `tool.Drawing.update_text_value()`.
This commit is contained in:
@@ -640,58 +640,41 @@ class SvgWriter:
|
|||||||
if symbol:
|
if symbol:
|
||||||
self.svg.add(self.svg.use(f"#{symbol}", insert=tuple(text_position * self.svg_scale)))
|
self.svg.add(self.svg.use(f"#{symbol}", insert=tuple(text_position * self.svg_scale)))
|
||||||
|
|
||||||
if text_literal.BoxAlignment == "top-left":
|
box_alignment = text_literal.BoxAlignment
|
||||||
alignment_baseline = "hanging"
|
|
||||||
text_anchor = "start"
|
|
||||||
elif text_literal.BoxAlignment == "top-middle":
|
|
||||||
alignment_baseline = "hanging"
|
|
||||||
text_anchor = "middle"
|
|
||||||
elif text_literal.BoxAlignment == "top-right":
|
|
||||||
alignment_baseline = "hanging"
|
|
||||||
text_anchor = "end"
|
|
||||||
elif text_literal.BoxAlignment == "middle-left":
|
|
||||||
alignment_baseline = "middle"
|
|
||||||
text_anchor = "start"
|
|
||||||
elif text_literal.BoxAlignment == "center":
|
|
||||||
alignment_baseline = "middle"
|
|
||||||
text_anchor = "middle"
|
|
||||||
elif text_literal.BoxAlignment == "middle-right":
|
|
||||||
alignment_baseline = "middle"
|
|
||||||
text_anchor = "end"
|
|
||||||
elif text_literal.BoxAlignment == "bottom-left":
|
|
||||||
alignment_baseline = "baseline"
|
|
||||||
text_anchor = "start"
|
|
||||||
elif text_literal.BoxAlignment == "bottom-middle":
|
|
||||||
alignment_baseline = "baseline"
|
|
||||||
text_anchor = "middle"
|
|
||||||
elif text_literal.BoxAlignment == "bottom-right":
|
|
||||||
alignment_baseline = "baseline"
|
|
||||||
text_anchor = "end"
|
|
||||||
|
|
||||||
literal = text_literal.Literal
|
# reference for alignment values:
|
||||||
|
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor
|
||||||
product = tool.Drawing.get_assigned_product(element)
|
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/alignment-baseline
|
||||||
variables = {}
|
vertical_alignment = {
|
||||||
if product != None:
|
"top": "hanging",
|
||||||
for variable in re.findall("{{.*?}}", literal):
|
"bottom": "baseline",
|
||||||
literal = literal.replace(
|
"center": "middle",
|
||||||
variable,
|
"middle": "middle",
|
||||||
str(ifcopenshell.util.selector.get_element_value(product, variable[2:-2]) or "")
|
}
|
||||||
)
|
alignment_baseline = vertical_alignment[ next(align for align in vertical_alignment if align in box_alignment) ]
|
||||||
|
horizontal_alignment = {
|
||||||
|
"left": "start",
|
||||||
|
"right": "end",
|
||||||
|
"center": "middle",
|
||||||
|
"middle": "middle",
|
||||||
|
}
|
||||||
|
text_anchor = horizontal_alignment[ next(align for align in horizontal_alignment if align in box_alignment) ]
|
||||||
|
|
||||||
|
text = text_obj.BIMTextProperties.value
|
||||||
text_tag = self.svg.text(
|
text_tag = self.svg.text(
|
||||||
"",
|
"",
|
||||||
class_=" ".join(self.get_attribute_classes(text_obj)),
|
class_=" ".join(self.get_attribute_classes(text_obj)),
|
||||||
**{
|
**{
|
||||||
"text-anchor": text_anchor,
|
"text-anchor": text_anchor,
|
||||||
"alignment-baseline": alignment_baseline,
|
# using dominant-baseline because we plan to use <tspan> subtags
|
||||||
|
# otherwise alignment-baseline would be sufficient
|
||||||
"dominant-baseline": alignment_baseline,
|
"dominant-baseline": alignment_baseline,
|
||||||
"transform": transform,
|
"transform": transform,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.svg.add(text_tag)
|
self.svg.add(text_tag)
|
||||||
|
|
||||||
for line_number, text_line in enumerate(literal.replace("\\n", "\n").split("\n")):
|
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))
|
||||||
# doing it here and not in tspan constructor because it adds unnecessary spaces
|
# doing it here and not in tspan constructor because it adds unnecessary spaces
|
||||||
t_span.update({"dy": f"{line_number}em"})
|
t_span.update({"dy": f"{line_number}em"})
|
||||||
|
|||||||
Reference in New Issue
Block a user