feat: revert percentage border-radius. Accept higher border-radius value and limit it to half of the smaller shape side to be rendered as a pill

This commit is contained in:
Antoine Poivey 2023-04-13 16:20:31 +02:00
parent e59a50fd08
commit ed79ccae74
No known key found for this signature in database
GPG key ID: 6AA1C83421F1A287
5 changed files with 13 additions and 30 deletions

View file

@ -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 {

View file

@ -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":

View file

@ -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)

View file

@ -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"`

View file

@ -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)
}