diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index c50ee617b..cff3ca2fe 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -167,6 +167,10 @@ 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": @@ -220,8 +224,8 @@ func (s *Style) Apply(key, value string) error { if err != nil || (f < 0 || f > 20) { return errors.New(`expected "border-radius" to be a number between 0 and 20`) } - if hasDecimalValue := math.Mod(f, 1) != 0; f > 1 && hasDecimalValue { - return errors.New(`expected "border-radius" to be an integer if superior to 1`) + if f > 1 && hasDecimalValue(f) { + return errors.New(`expected "border-radius" to be an integer if greater than 1`) } s.BorderRadius.Value = value case "shadow": diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index 0f6c57905..b66a3aa43 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -983,12 +983,6 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske // TODO should standardize "" to rectangle case d2target.ShapeRectangle, d2target.ShapeSequenceDiagram, "": - // TODO use Rx property of NewThemableElement instead - // DO - rx := "" - if targetShape.BorderRadius != 0 { - rx = fmt.Sprintf(` rx="%s"`, d2themes.FormatRxRy(targetShape.BorderRadius)) - } if targetShape.ThreeDee { fmt.Fprint(writer, render3dRect(targetShape)) } else { @@ -1002,7 +996,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) } if sketchRunner != nil { @@ -1020,7 +1015,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) } } else { @@ -1033,7 +1029,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) el = d2themes.NewThemableElement("rect") @@ -1044,7 +1041,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) } if sketchRunner != nil { @@ -1062,7 +1060,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) el = d2themes.NewThemableElement("rect") @@ -1073,7 +1072,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske el.Fill = fill el.Stroke = stroke el.Style = style - el.Attributes = rx + el.Rx = targetShape.BorderRadius + el.Ry = targetShape.BorderRadius fmt.Fprint(writer, el.Render()) } } diff --git a/d2themes/element.go b/d2themes/element.go index b222187b5..8e8f5fb84 100644 --- a/d2themes/element.go +++ b/d2themes/element.go @@ -135,10 +135,10 @@ func (el *ThemableElement) Render() string { out += fmt.Sprintf(` r="%f"`, el.R) } if el.Rx != math.MaxFloat64 { - out += fmt.Sprintf(` rx="%s"`, FormatRxRy(el.Rx)) + out += fmt.Sprintf(` rx="%f"`, calculateAxisRadius(el.Rx, el.Width)) } if el.Ry != math.MaxFloat64 { - out += fmt.Sprintf(` ry="%s"`, FormatRxRy(el.Ry)) + out += fmt.Sprintf(` ry="%f"`, calculateAxisRadius(el.Ry, el.Height)) } if el.Cx != math.MaxFloat64 { out += fmt.Sprintf(` cx="%f"`, el.Cx) @@ -207,9 +207,12 @@ func (el *ThemableElement) Render() string { return out + " />" } -func FormatRxRy(value float64) string { - if 0 < value && value < 1 { - return fmt.Sprintf(`%v%%`, int(value*100)) +func calculateAxisRadius(borderRadius float64, sideLength float64) float64 { + if borderRadius >= 1 { + return borderRadius } - return fmt.Sprintf(`%f`, value) + if sideLength == math.MaxFloat64 { + return 0 + } + return borderRadius * sideLength }