From 4de1f767510b48dad5dc4cac613a6fb0f9090e04 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 13 Feb 2023 11:16:26 -0800 Subject: [PATCH] limit oval shape aspect ratio --- d2graph/d2graph.go | 5 ++++- d2layouts/d2sequence/sequence_diagram.go | 2 +- lib/shape/shape_oval.go | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index d907c1999..317e30eee 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1296,8 +1296,11 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler obj.Width = sideLength obj.Height = sideLength } else if desiredHeight == 0 || desiredWidth == 0 { - if s.GetType() == shape.PERSON_TYPE { + switch s.GetType() { + case shape.PERSON_TYPE: obj.Width, obj.Height = shape.LimitAR(obj.Width, obj.Height, shape.PERSON_AR_LIMIT) + case shape.OVAL_TYPE: + obj.Width, obj.Height = shape.LimitAR(obj.Width, obj.Height, shape.OVAL_AR_LIMIT) } } } diff --git a/d2layouts/d2sequence/sequence_diagram.go b/d2layouts/d2sequence/sequence_diagram.go index 53cdc8e86..cac36e113 100644 --- a/d2layouts/d2sequence/sequence_diagram.go +++ b/d2layouts/d2sequence/sequence_diagram.go @@ -107,7 +107,7 @@ func newSequenceDiagram(objects []*d2graph.Object, messages []*d2graph.Edge) *se if actor.Width < MIN_ACTOR_WIDTH { dslShape := strings.ToLower(actor.Attributes.Shape.Value) switch dslShape { - case d2target.ShapePerson, d2target.ShapeSquare, d2target.ShapeCircle: + case d2target.ShapePerson, d2target.ShapeOval, d2target.ShapeSquare, d2target.ShapeCircle: // scale shape up to min width uniformly actor.Height *= MIN_ACTOR_WIDTH / actor.Width } diff --git a/lib/shape/shape_oval.go b/lib/shape/shape_oval.go index 9e75242eb..8e035f845 100644 --- a/lib/shape/shape_oval.go +++ b/lib/shape/shape_oval.go @@ -7,6 +7,8 @@ import ( "oss.terrastruct.com/util-go/go2" ) +const OVAL_AR_LIMIT = 3. + type shapeOval struct { *baseShape } @@ -38,7 +40,11 @@ func (s shapeOval) GetDimensionsToFit(width, height, paddingX, paddingY float64) paddedWidth := width + paddingX*math.Cos(theta) paddedHeight := height + paddingY*math.Sin(theta) // see https://stackoverflow.com/questions/433371/ellipse-bounding-a-rectangle - return math.Ceil(math.Sqrt2 * paddedWidth), math.Ceil(math.Sqrt2 * paddedHeight) + totalWidth, totalHeight := math.Ceil(math.Sqrt2*paddedWidth), math.Ceil(math.Sqrt2*paddedHeight) + + // prevent oval from becoming too flat + totalWidth, totalHeight = LimitAR(totalWidth, totalHeight, OVAL_AR_LIMIT) + return totalWidth, totalHeight } func (s shapeOval) GetInsidePlacement(width, height, paddingX, paddingY float64) geo.Point {