feat: add possiblity to set shape border-radius with percentage value

This commit is contained in:
Antoine Poivey 2023-03-09 17:10:12 +01:00
parent 6670e1967c
commit 6632740fb1
No known key found for this signature in database
GPG key ID: 6AA1C83421F1A287
5 changed files with 17 additions and 7 deletions

View file

@ -76,7 +76,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.Atoi(obj.Attributes.Style.BorderRadius.Value)
shape.BorderRadius, _ = strconv.ParseFloat(obj.Attributes.Style.BorderRadius.Value, 64)
}
if obj.Attributes.Style.FontColor != nil {

View file

@ -216,10 +216,13 @@ func (s *Style) Apply(key, value string) error {
if s.BorderRadius == nil {
break
}
f, err := strconv.Atoi(value)
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 hasDecimalValue := math.Mod(f, 1) != 0; f > 1 && hasDecimalValue {
return errors.New(`expected "border-radius" to be an integer if superior to 1`)
}
s.BorderRadius.Value = value
case "shadow":
if s.Shadow == nil {

View file

@ -987,7 +987,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
// DO
rx := ""
if targetShape.BorderRadius != 0 {
rx = fmt.Sprintf(` rx="%d"`, targetShape.BorderRadius)
rx = fmt.Sprintf(` rx="%s"`, d2themes.FormatRxRy(targetShape.BorderRadius))
}
if targetShape.ThreeDee {
fmt.Fprint(writer, render3dRect(targetShape))
@ -1735,7 +1735,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
backgroundEl.Height = float64(h)
backgroundEl.Fill = diagram.Root.Fill
backgroundEl.Stroke = diagram.Root.Stroke
backgroundEl.Rx = float64(diagram.Root.BorderRadius)
backgroundEl.Rx = 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

@ -174,7 +174,7 @@ type Shape struct {
StrokeDash float64 `json:"strokeDash"`
StrokeWidth int `json:"strokeWidth"`
BorderRadius int `json:"borderRadius"`
BorderRadius float64 `json:"borderRadius"`
Fill string `json:"fill"`
Stroke string `json:"stroke"`

View file

@ -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="%f"`, el.Rx)
out += fmt.Sprintf(` rx="%s"`, FormatRxRy(el.Rx))
}
if el.Ry != math.MaxFloat64 {
out += fmt.Sprintf(` ry="%f"`, el.Ry)
out += fmt.Sprintf(` ry="%s"`, FormatRxRy(el.Ry))
}
if el.Cx != math.MaxFloat64 {
out += fmt.Sprintf(` cx="%f"`, el.Cx)
@ -206,3 +206,10 @@ func (el *ThemableElement) Render() string {
}
return out + " />"
}
func FormatRxRy(value float64) string {
if 0 < value && value < 1 {
return fmt.Sprintf(`%v%%`, int(value*100))
}
return fmt.Sprintf(`%f`, value)
}