From b1beddc9e2d41a1e68683ce1b06faa62870f56c8 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 23 Jan 2023 19:10:31 -0800 Subject: [PATCH] Adjust padding per shape --- d2graph/d2graph.go | 12 ++++++++++++ lib/shape/shape.go | 4 +++- lib/shape/shape_callout.go | 4 ++++ lib/shape/shape_circle.go | 4 ++++ lib/shape/shape_cloud.go | 2 +- lib/shape/shape_cylinder.go | 4 ++++ lib/shape/shape_diamond.go | 4 ++++ lib/shape/shape_document.go | 8 ++++++-- lib/shape/shape_hexagon.go | 4 ++++ lib/shape/shape_package.go | 4 ++++ lib/shape/shape_page.go | 4 ++++ lib/shape/shape_person.go | 4 ++++ lib/shape/shape_queue.go | 4 ++++ lib/shape/shape_step.go | 4 ++++ lib/shape/shape_stored_data.go | 4 ++++ 15 files changed, 66 insertions(+), 4 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index d92f90233..dc5bd13c8 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1157,6 +1157,18 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler } } + // give shapes with icons extra padding + if obj.Attributes.Icon != nil { + paddingX += 20 + paddingY += 20 + } + if obj.Attributes.Link != "" { + paddingX += 32 + } + if obj.Attributes.Tooltip != "" { + paddingX += 32 + } + if desiredWidth == 0 && desiredHeight == 0 { newWidth, newHeight := s.GetDimensionsToFit(contentBox.Width, contentBox.Height, paddingX, paddingY) obj.Width = newWidth diff --git a/lib/shape/shape.go b/lib/shape/shape.go index 705d209e3..84468ad7b 100644 --- a/lib/shape/shape.go +++ b/lib/shape/shape.go @@ -31,6 +31,8 @@ const ( TEXT_TYPE = "Text" CODE_TYPE = "Code" IMAGE_TYPE = "Image" + + defaultPadding = 40. ) type Shape interface { @@ -101,7 +103,7 @@ func (s baseShape) GetDimensionsToFit(width, height, paddingX, paddingY float64) } func (s baseShape) GetDefaultPadding() (paddingX, paddingY float64) { - return 100., 100. + return defaultPadding, defaultPadding } func (s baseShape) Perimeter() []geo.Intersectable { diff --git a/lib/shape/shape_callout.go b/lib/shape/shape_callout.go index fec727fd1..6c91d2e2c 100644 --- a/lib/shape/shape_callout.go +++ b/lib/shape/shape_callout.go @@ -82,3 +82,7 @@ func (s shapeCallout) GetDimensionsToFit(width, height, paddingX, paddingY float } return width + paddingX, baseHeight } + +func (s shapeCallout) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding, defaultPadding / 2 +} diff --git a/lib/shape/shape_circle.go b/lib/shape/shape_circle.go index ef759f196..7261f37aa 100644 --- a/lib/shape/shape_circle.go +++ b/lib/shape/shape_circle.go @@ -45,3 +45,7 @@ func (s shapeCircle) GetInsidePlacement(width, height, padding float64) geo.Poin func (s shapeCircle) Perimeter() []geo.Intersectable { return []geo.Intersectable{geo.NewEllipse(s.Box.Center(), s.Box.Width/2, s.Box.Height/2)} } + +func (s shapeCircle) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding / 2, defaultPadding / 2 +} diff --git a/lib/shape/shape_cloud.go b/lib/shape/shape_cloud.go index 548d65310..710d7b4c8 100644 --- a/lib/shape/shape_cloud.go +++ b/lib/shape/shape_cloud.go @@ -119,5 +119,5 @@ func (s shapeCloud) GetSVGPathData() []string { } func (s shapeCloud) GetDefaultPadding() (paddingX, paddingY float64) { - return 50, 50 + return defaultPadding, defaultPadding / 2 } diff --git a/lib/shape/shape_cylinder.go b/lib/shape/shape_cylinder.go index 4273b49dd..79cc06e72 100644 --- a/lib/shape/shape_cylinder.go +++ b/lib/shape/shape_cylinder.go @@ -82,3 +82,7 @@ func (s shapeCylinder) GetDimensionsToFit(width, height, paddingX, paddingY floa totalHeight := height + paddingY + 3*defaultArcDepth return width + paddingX, totalHeight } + +func (s shapeCylinder) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding, defaultPadding / 2 +} diff --git a/lib/shape/shape_diamond.go b/lib/shape/shape_diamond.go index ef1853679..b6f95f7a8 100644 --- a/lib/shape/shape_diamond.go +++ b/lib/shape/shape_diamond.go @@ -60,3 +60,7 @@ func (s shapeDiamond) GetDimensionsToFit(width, height, paddingX, paddingY float totalHeight := 2 * (height + paddingY) return totalWidth, totalHeight } + +func (s shapeDiamond) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding / 4, defaultPadding / 2 +} diff --git a/lib/shape/shape_document.go b/lib/shape/shape_document.go index 81e65f9aa..3ee710a28 100644 --- a/lib/shape/shape_document.go +++ b/lib/shape/shape_document.go @@ -53,6 +53,10 @@ func (s shapeDocument) GetSVGPathData() []string { } func (s shapeDocument) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) { - baseHeight := (height + paddingX) * docPathHeight / docPathInnerBottom - return width + paddingY, baseHeight + baseHeight := (height + paddingY) * docPathHeight / docPathInnerBottom + return width + paddingX, baseHeight +} + +func (s shapeDocument) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding, defaultPadding * docPathInnerBottom / docPathHeight } diff --git a/lib/shape/shape_hexagon.go b/lib/shape/shape_hexagon.go index 0009426c7..ed6bbcb83 100644 --- a/lib/shape/shape_hexagon.go +++ b/lib/shape/shape_hexagon.go @@ -53,3 +53,7 @@ func (s shapeHexagon) GetDimensionsToFit(width, height, paddingX, paddingY float totalWidth := 2 * (width + paddingX) return totalWidth, height + paddingY } + +func (s shapeHexagon) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding / 4, defaultPadding +} diff --git a/lib/shape/shape_package.go b/lib/shape/shape_package.go index e0201c260..80219fa79 100644 --- a/lib/shape/shape_package.go +++ b/lib/shape/shape_package.go @@ -82,3 +82,7 @@ func (s shapePackage) GetDimensionsToFit(width, height, paddingX, paddingY float return width + paddingX, totalHeight } + +func (s shapePackage) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding, .8 * defaultPadding +} diff --git a/lib/shape/shape_page.go b/lib/shape/shape_page.go index 4e7609d2f..75717c8f2 100644 --- a/lib/shape/shape_page.go +++ b/lib/shape/shape_page.go @@ -97,3 +97,7 @@ func (s shapePage) GetDimensionsToFit(width, height, paddingX, paddingY float64) totalHeight = math.Max(totalHeight, pageCornerHeight) return totalWidth, totalHeight } + +func (s shapePage) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding, pageCornerHeight + defaultPadding +} diff --git a/lib/shape/shape_person.go b/lib/shape/shape_person.go index 68cf06ab8..e92ddab53 100644 --- a/lib/shape/shape_person.go +++ b/lib/shape/shape_person.go @@ -74,3 +74,7 @@ func (s shapePerson) GetDimensionsToFit(width, height, paddingX, paddingY float6 totalHeight := height + paddingY return totalWidth, totalHeight } + +func (s shapePerson) GetDefaultPadding() (paddingX, paddingY float64) { + return 10, defaultPadding +} diff --git a/lib/shape/shape_queue.go b/lib/shape/shape_queue.go index d5685fbb2..5a0a9d32f 100644 --- a/lib/shape/shape_queue.go +++ b/lib/shape/shape_queue.go @@ -78,3 +78,7 @@ func (s shapeQueue) GetDimensionsToFit(width, height, paddingX, paddingY float64 totalWidth := 3*defaultArcDepth + width + paddingX return totalWidth, height + paddingY } + +func (s shapeQueue) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding / 2, defaultPadding +} diff --git a/lib/shape/shape_step.go b/lib/shape/shape_step.go index 481de7ed8..8951e866a 100644 --- a/lib/shape/shape_step.go +++ b/lib/shape/shape_step.go @@ -58,3 +58,7 @@ func (s shapeStep) GetDimensionsToFit(width, height, paddingX, paddingY float64) totalWidth := width + paddingX + 2*STEP_WEDGE_WIDTH return totalWidth, height + paddingY } + +func (s shapeStep) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding / 4, defaultPadding + STEP_WEDGE_WIDTH +} diff --git a/lib/shape/shape_stored_data.go b/lib/shape/shape_stored_data.go index f5961c590..f952c3fd0 100644 --- a/lib/shape/shape_stored_data.go +++ b/lib/shape/shape_stored_data.go @@ -60,3 +60,7 @@ func (s shapeStoredData) GetDimensionsToFit(width, height, paddingX, paddingY fl totalWidth := width + paddingX + 2*storedDataWedgeWidth return totalWidth, height + paddingY } + +func (s shapeStoredData) GetDefaultPadding() (paddingX, paddingY float64) { + return defaultPadding - 10, defaultPadding +}