diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 54e278b56..4457be55c 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -2,12 +2,11 @@ - Tooltips can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) - Links can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) +- The `width` and `height` attributes are no longer restricted to images and can be applied to non-container shapes. [#498](https://github.com/terrastruct/d2/pull/498) #### Improvements 🧹 -- Now the `width` and `height` attributes are no longer restricted to images and can be applied to non-container shapes. [#498](https://github.com/terrastruct/d2/pull/498) - #### Bugfixes ⛑️ - Restricts where `near` key constant values can be used, with good error messages, instead of erroring (e.g. setting `near: top-center` on a container would cause bad layouts or error). [#538](https://github.com/terrastruct/d2/pull/538) -- Fixed rendering classes and tables with empty headers. [#498](https://github.com/terrastruct/d2/pull/498) +- Fixes rendering classes and tables with empty headers. [#498](https://github.com/terrastruct/d2/pull/498) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 53f682942..1b667c665 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -899,13 +899,13 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler for _, obj := range g.Objects { obj.Box = &geo.Box{} - var setWidth int - var setHeight int + var desiredWidth int + var desiredHeight int if obj.Attributes.Width != nil { - setWidth, _ = strconv.Atoi(obj.Attributes.Width.Value) + desiredWidth, _ = strconv.Atoi(obj.Attributes.Width.Value) } if obj.Attributes.Height != nil { - setHeight, _ = strconv.Atoi(obj.Attributes.Height.Value) + desiredHeight, _ = strconv.Atoi(obj.Attributes.Height.Value) } shapeType := strings.ToLower(obj.Attributes.Shape.Value) @@ -917,7 +917,7 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler d2target.ShapeText: // edge cases for unnamed class, etc default: - if obj.Attributes.Label.Value == "" && setWidth == 0 && setHeight == 0 { + if obj.Attributes.Label.Value == "" && desiredWidth == 0 && desiredHeight == 0 { obj.Width = 100 obj.Height = 100 continue @@ -980,38 +980,31 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler dims.Width += innerLabelPadding dims.Height += innerLabelPadding obj.LabelDimensions = *dims - obj.Width = float64(dims.Width) - obj.Height = float64(dims.Height) - - // the set dimensions must be at least as large as the text - if float64(setWidth) > obj.Width { - obj.Width = float64(setWidth) - } - if float64(setHeight) > obj.Height { - obj.Height = float64(setHeight) - } + // the desired dimensions must be at least as large as the text + obj.Width = float64(go2.Max(dims.Width, desiredWidth)) + obj.Height = float64(go2.Max(dims.Height, desiredHeight)) switch shapeType { default: - if setWidth == 0 { + if desiredWidth == 0 { obj.Width += 100 } - if setHeight == 0 { + if desiredHeight == 0 { obj.Height += 100 } case d2target.ShapeImage: - if setWidth == 0 { + if desiredWidth == 0 { obj.Width = 128 } - if setHeight == 0 { + if desiredHeight == 0 { obj.Height = 128 } case d2target.ShapeSquare, d2target.ShapeCircle: sideLength := go2.Max(obj.Width, obj.Height) padding := 0. - if setWidth == 0 && setHeight == 0 { + if desiredWidth == 0 && desiredHeight == 0 { padding = 100. } obj.Width = sideLength + padding @@ -1104,11 +1097,11 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler d2target.ShapeSQLTable, d2target.ShapeCode, d2target.ShapeText: - if float64(setWidth) > obj.Width { - obj.Width = float64(setWidth) + if float64(desiredWidth) > obj.Width { + obj.Width = float64(desiredWidth) } - if float64(setHeight) > obj.Height { - obj.Height = float64(setHeight) + if float64(desiredHeight) > obj.Height { + obj.Height = float64(desiredHeight) } }