Fix dimension text rendering upside-down when drawn right-to-left

Normalize the dimension direction in both the SVG writer and the viewport
decorator so text always reads left-to-right (or bottom-to-top for vertical
dims) regardless of which anchor was placed first. Also guards against a
pre-existing crash when two dimension endpoints project to the same screen
position (zero-length vector passed to angle_signed).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ryan Schultz
2026-08-15 13:48:46 -05:00
parent 99f95bee6a
commit 253a6c56a1
2 changed files with 15 additions and 2 deletions
@@ -740,6 +740,10 @@ class DimensionDecorator(BaseDecorator):
text_dir = p1 - p0
if text_dir.length < 1:
continue
# Normalize so text always reads left-to-right (or bottom-to-top for
# vertical dims) regardless of which end was drawn first.
if text_dir.x < 0 or (abs(text_dir.x) < 1e-6 and text_dir.y < 0):
text_dir = -text_dir
perpendicular = Vector((-text_dir.y, text_dir.x)).normalized()
text_offset = perpendicular * text_offset_value
@@ -748,7 +752,7 @@ class DimensionDecorator(BaseDecorator):
"multiline": True,
"text_dir": text_dir,
}
base_pos = p1 if is_ordinate else p0 + text_dir * 0.5
base_pos = p1 if is_ordinate else (p0 + p1) / 2
if not show_description_only:
segment_length = (v1 - v0).length
@@ -1588,8 +1588,10 @@ class SvgWriter:
end = (offset + v1.xy * Vector((1, -1))) * self.svg_scale
mid = ((end - start) / 2) + start
vector = end - start
sheet_dimension = vector.length
if sheet_dimension < 1e-6:
return
perpendicular = Vector((vector.y, -vector.x)).normalized()
sheet_dimension = (end - start).length
# if annotation can't fit offset text to the right of marker
if distance_override is not None:
@@ -1597,6 +1599,13 @@ class SvgWriter:
else:
text_position = mid if sheet_dimension > 5 else (end + (3 * vector.normalized()))
angle = math.degrees(vector.angle_signed(Vector((1, 0))))
# Keep text readable regardless of draw direction: if the dimension runs
# right-to-left the raw angle is near ±180° which renders text upside-down.
# Flip both angle and perpendicular so text always reads left-to-right and
# stays on the same side of the dimension line.
if abs(angle) > 90:
angle += 180
perpendicular = -perpendicular
line = self.svg.line(start=start, end=end, class_=" ".join(classes))
self.svg.add(line)