diff --git a/d2exporter/export.go b/d2exporter/export.go index cb75b6809..8524e7542 100644 --- a/d2exporter/export.go +++ b/d2exporter/export.go @@ -102,7 +102,7 @@ func applyStyles(shape *d2target.Shape, obj *d2graph.Object) { shape.Multiple, _ = strconv.ParseBool(obj.Attributes.Style.Multiple.Value) } if obj.Attributes.Style.BorderRadius != nil { - shape.BorderRadius, _ = strconv.ParseFloat(obj.Attributes.Style.BorderRadius.Value, 64) + shape.BorderRadius, _ = strconv.Atoi(obj.Attributes.Style.BorderRadius.Value) } if obj.Attributes.Style.FontColor != nil { diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index ee3700f16..0f0c4daf0 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -172,10 +172,6 @@ type Style struct { DoubleBorder *Scalar `json:"doubleBorder,omitempty"` } -func hasDecimalValue(value float64) bool { - return math.Mod(value, 1) != 0 -} - func (s *Style) Apply(key, value string) error { switch key { case "opacity": @@ -233,12 +229,9 @@ func (s *Style) Apply(key, value string) error { if s.BorderRadius == nil { break } - f, err := strconv.ParseFloat(value, 64) - if err != nil || (f < 0 || f > 20) { - return errors.New(`expected "border-radius" to be a number between 0 and 20`) - } - if f > 1 && hasDecimalValue(f) { - return errors.New(`expected "border-radius" to be an integer if greater than 1`) + f, err := strconv.Atoi(value) + if err != nil || (f < 0) { + return errors.New(`expected "border-radius" to be a number greater or equal to 0`) } s.BorderRadius.Value = value case "shadow": diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index 8f39ff6b2..6186222bd 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -1027,7 +1027,7 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, case d2target.ShapeRectangle, d2target.ShapeSequenceDiagram, "": borderRadius := math.MaxFloat64 if targetShape.BorderRadius != 0 { - borderRadius = targetShape.BorderRadius + borderRadius = float64(targetShape.BorderRadius) } if targetShape.ThreeDee { fmt.Fprint(writer, render3dRect(targetShape)) @@ -1043,7 +1043,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) } if sketchRunner != nil { @@ -1063,7 +1062,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) } } else { @@ -1078,7 +1076,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) el = d2themes.NewThemableElement("rect") @@ -1090,7 +1087,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) } if sketchRunner != nil { @@ -1110,7 +1106,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) el = d2themes.NewThemableElement("rect") @@ -1122,7 +1117,6 @@ func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, el.Stroke = stroke el.Style = style el.Rx = borderRadius - el.Ry = borderRadius fmt.Fprint(writer, el.Render()) } } @@ -1785,7 +1779,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) { backgroundEl.Fill = diagram.Root.Fill backgroundEl.Stroke = diagram.Root.Stroke backgroundEl.FillPattern = diagram.Root.FillPattern - backgroundEl.Rx = diagram.Root.BorderRadius + backgroundEl.Rx = float64(diagram.Root.BorderRadius) if diagram.Root.StrokeDash != 0 { dashSize, gapSize := svg.GetStrokeDashAttributes(float64(diagram.Root.StrokeWidth), diagram.Root.StrokeDash) backgroundEl.StrokeDashArray = fmt.Sprintf("%f, %f", dashSize, gapSize) diff --git a/d2target/d2target.go b/d2target/d2target.go index ec8c4a6e6..5729354e1 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -313,7 +313,7 @@ type Shape struct { StrokeDash float64 `json:"strokeDash"` StrokeWidth int `json:"strokeWidth"` - BorderRadius float64 `json:"borderRadius"` + BorderRadius int `json:"borderRadius"` Fill string `json:"fill"` FillPattern string `json:"fillPattern,omitempty"` diff --git a/d2themes/element.go b/d2themes/element.go index 515e694fe..344525d5c 100644 --- a/d2themes/element.go +++ b/d2themes/element.go @@ -140,10 +140,10 @@ func (el *ThemableElement) Render() string { out += fmt.Sprintf(` r="%f"`, el.R) } if el.Rx != math.MaxFloat64 { - out += fmt.Sprintf(` rx="%f"`, calculateAxisRadius(el.Rx, el.Width)) + out += fmt.Sprintf(` rx="%f"`, calculateAxisRadius(el.Rx, el.Width, el.Height)) } if el.Ry != math.MaxFloat64 { - out += fmt.Sprintf(` ry="%f"`, calculateAxisRadius(el.Ry, el.Height)) + out += fmt.Sprintf(` ry="%f"`, calculateAxisRadius(el.Ry, el.Width, el.Height)) } if el.Cx != math.MaxFloat64 { out += fmt.Sprintf(` cx="%f"`, el.Cx) @@ -228,12 +228,8 @@ func (el *ThemableElement) Render() string { return out } -func calculateAxisRadius(borderRadius float64, sideLength float64) float64 { - if borderRadius >= 1 { - return borderRadius - } - if sideLength == math.MaxFloat64 { - return 0 - } - return borderRadius * sideLength +func calculateAxisRadius(borderRadius float64, width float64, height float64) float64 { + var minimumSideSize = math.Min(width, height) + var maximumBorderRadiusValue = minimumSideSize / 2.0 + return math.Min(borderRadius, maximumBorderRadiusValue) }