Make to be a style attribute and remove shape

This commit is contained in:
OneRain2333 2022-12-31 15:26:38 +08:00
parent c75e540840
commit 9575c768dc
9 changed files with 67 additions and 75 deletions

View file

@ -235,6 +235,8 @@ func (c *compiler) compileAttributes(attrs *d2graph.Attributes, mk *d2ast.Key) {
attrs.Width = &d2graph.Scalar{MapKey: mk} attrs.Width = &d2graph.Scalar{MapKey: mk}
} else if reserved == "height" { } else if reserved == "height" {
attrs.Height = &d2graph.Scalar{MapKey: mk} attrs.Height = &d2graph.Scalar{MapKey: mk}
} else if reserved == "double-border" {
attrs.Style.DoubleBorder = &d2graph.Scalar{MapKey: mk}
} }
} }

View file

@ -93,6 +93,9 @@ func applyStyles(shape *d2target.Shape, obj *d2graph.Object) {
if obj.Attributes.Style.Font != nil { if obj.Attributes.Style.Font != nil {
shape.FontFamily = obj.Attributes.Style.Font.Value shape.FontFamily = obj.Attributes.Style.Font.Value
} }
if obj.Attributes.Style.DoubleBorder != nil {
shape.DoubleBorder, _ = strconv.ParseBool(obj.Attributes.Style.DoubleBorder.Value)
}
} }
func toShape(obj *d2graph.Object, theme *d2themes.Theme) d2target.Shape { func toShape(obj *d2graph.Object, theme *d2themes.Theme) d2target.Shape {

View file

@ -147,6 +147,7 @@ type Style struct {
Italic *Scalar `json:"italic,omitempty"` Italic *Scalar `json:"italic,omitempty"`
Underline *Scalar `json:"underline,omitempty"` Underline *Scalar `json:"underline,omitempty"`
Filled *Scalar `json:"filled,omitempty"` Filled *Scalar `json:"filled,omitempty"`
DoubleBorder *Scalar `json:"doubleBorder,omitempty"`
} }
func (s *Style) Apply(key, value string) error { func (s *Style) Apply(key, value string) error {
@ -300,6 +301,15 @@ func (s *Style) Apply(key, value string) error {
return errors.New(`expected "filled" to be true or false`) return errors.New(`expected "filled" to be true or false`)
} }
s.Filled.Value = value s.Filled.Value = value
case "double-border":
if s.DoubleBorder == nil {
break
}
_, err := strconv.ParseBool(value)
if err != nil {
return errors.New(`expected "double-border" to be true or false`)
}
s.DoubleBorder.Value = value
default: default:
return fmt.Errorf("unknown style key: %s", key) return fmt.Errorf("unknown style key: %s", key)
} }
@ -1286,6 +1296,7 @@ var StyleKeywords = map[string]struct{}{
// Only for shapes // Only for shapes
"shadow": {}, "shadow": {},
"multiple": {}, "multiple": {},
"double-border": {},
// Only for squares // Only for squares
"3d": {}, "3d": {},

View file

@ -284,6 +284,11 @@ func _set(g *d2graph.Graph, key string, tag, value *string) error {
attrs.Style.Multiple.MapKey.SetScalar(mk.Value.ScalarBox()) attrs.Style.Multiple.MapKey.SetScalar(mk.Value.ScalarBox())
return nil return nil
} }
case "double-border":
if attrs.Style.DoubleBorder != nil {
attrs.Style.DoubleBorder.MapKey.SetScalar(mk.Value.ScalarBox())
return nil
}
case "font": case "font":
if attrs.Style.Font != nil { if attrs.Style.Font != nil {
attrs.Style.Font.MapKey.SetScalar(mk.Value.ScalarBox()) attrs.Style.Font.MapKey.SetScalar(mk.Value.ScalarBox())

View file

@ -589,7 +589,7 @@ func renderOval(tl *geo.Point, width, height float64, style string) string {
return fmt.Sprintf(`<ellipse class="shape" cx="%f" cy="%f" rx="%f" ry="%f" style="%s" />`, cx, cy, rx, ry, style) return fmt.Sprintf(`<ellipse class="shape" cx="%f" cy="%f" rx="%f" ry="%f" style="%s" />`, cx, cy, rx, ry, style)
} }
func renderDoubleCircle(tl *geo.Point, width, height float64, style string) string { func renderDoubleOval(tl *geo.Point, width, height float64, style string) string {
return renderOval(tl, width, height, style) + renderOval(&geo.Point{X: tl.X + 5, Y: tl.Y + 5}, width-10, height-10, style) return renderOval(tl, width, height, style) + renderOval(&geo.Point{X: tl.X + 5, Y: tl.Y + 5}, width-10, height-10, style)
} }
@ -764,6 +764,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
fmt.Fprintf(writer, closingTag) fmt.Fprintf(writer, closingTag)
return labelMask, nil return labelMask, nil
case d2target.ShapeOval: case d2target.ShapeOval:
if !targetShape.DoubleBorder {
if targetShape.Multiple { if targetShape.Multiple {
fmt.Fprint(writer, renderOval(multipleTL, width, height, style)) fmt.Fprint(writer, renderOval(multipleTL, width, height, style))
} }
@ -776,9 +777,9 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
} else { } else {
fmt.Fprint(writer, renderOval(tl, width, height, style)) fmt.Fprint(writer, renderOval(tl, width, height, style))
} }
case d2target.ShapeDoubleCircle: } else {
if targetShape.Multiple { if targetShape.Multiple {
fmt.Fprint(writer, renderDoubleCircle(multipleTL, width, height, style)) fmt.Fprint(writer, renderDoubleOval(multipleTL, width, height, style))
} }
if sketchRunner != nil { if sketchRunner != nil {
out, err := d2sketch.DoubleOval(sketchRunner, targetShape) out, err := d2sketch.DoubleOval(sketchRunner, targetShape)
@ -787,8 +788,22 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
} }
fmt.Fprintf(writer, out) fmt.Fprintf(writer, out)
} else { } else {
fmt.Fprint(writer, renderDoubleCircle(tl, width, height, style)) fmt.Fprint(writer, renderDoubleOval(tl, width, height, style))
} }
}
// case d2target.ShapeDoubleCircle:
// if targetShape.Multiple {
// fmt.Fprint(writer, renderDoubleCircle(multipleTL, width, height, style))
// }
// if sketchRunner != nil {
// out, err := d2sketch.DoubleOval(sketchRunner, targetShape)
// if err != nil {
// return "", err
// }
// fmt.Fprintf(writer, out)
// } else {
// fmt.Fprint(writer, renderDoubleCircle(tl, width, height, style))
// }
case d2target.ShapeImage: case d2target.ShapeImage:
fmt.Fprintf(writer, `<image href="%s" x="%d" y="%d" width="%d" height="%d" style="%s" />`, fmt.Fprintf(writer, `<image href="%s" x="%d" y="%d" width="%d" height="%d" style="%s" />`,

View file

@ -146,6 +146,7 @@ type Shape struct {
Shadow bool `json:"shadow"` Shadow bool `json:"shadow"`
ThreeDee bool `json:"3d"` ThreeDee bool `json:"3d"`
Multiple bool `json:"multiple"` Multiple bool `json:"multiple"`
DoubleBorder bool `json:"double-border"`
Tooltip string `json:"tooltip"` Tooltip string `json:"tooltip"`
Link string `json:"link"` Link string `json:"link"`

View file

@ -42,14 +42,13 @@ oval: {shape: "oval"}
circle: {shape: "circle"} circle: {shape: "circle"}
hexagon: {shape: "hexagon"} hexagon: {shape: "hexagon"}
cloud: {shape: "cloud"} cloud: {shape: "cloud"}
double_circle: {shape: "double_circle"}
rectangle -> square -> page rectangle -> square -> page
parallelogram -> document -> cylinder parallelogram -> document -> cylinder
queue -> package -> step queue -> package -> step
callout -> stored_data -> person callout -> stored_data -> person
diamond -> oval -> circle diamond -> oval -> circle
hexagon -> cloud -> double_circle hexagon -> cloud
`, `,
}, },
{ {
@ -72,14 +71,13 @@ oval: {shape: "oval"}
circle: {shape: "circle"} circle: {shape: "circle"}
hexagon: {shape: "hexagon"} hexagon: {shape: "hexagon"}
cloud: {shape: "cloud"} cloud: {shape: "cloud"}
double_circle: {shape: "double_circle"}
rectangle -> square -> page rectangle -> square -> page
parallelogram -> document -> cylinder parallelogram -> document -> cylinder
queue -> package -> step queue -> package -> step
callout -> stored_data -> person callout -> stored_data -> person
diamond -> oval -> circle diamond -> oval -> circle
hexagon -> cloud -> double_circle hexagon -> cloud
rectangle.multiple: true rectangle.multiple: true
square.multiple: true square.multiple: true
@ -98,7 +96,6 @@ oval.multiple: true
circle.multiple: true circle.multiple: true
hexagon.multiple: true hexagon.multiple: true
cloud.multiple: true cloud.multiple: true
double_circle.multiple: true
`, `,
}, },
{ {
@ -121,14 +118,13 @@ oval: {shape: "oval"}
circle: {shape: "circle"} circle: {shape: "circle"}
hexagon: {shape: "hexagon"} hexagon: {shape: "hexagon"}
cloud: {shape: "cloud"} cloud: {shape: "cloud"}
double_circle: {shape: "double_circle"}
rectangle -> square -> page rectangle -> square -> page
parallelogram -> document -> cylinder parallelogram -> document -> cylinder
queue -> package -> step queue -> package -> step
callout -> stored_data -> person callout -> stored_data -> person
diamond -> oval -> circle diamond -> oval -> circle
hexagon -> cloud -> double_circle hexagon -> cloud
rectangle.shadow: true rectangle.shadow: true
square.shadow: true square.shadow: true
@ -147,7 +143,6 @@ oval.shadow: true
circle.shadow: true circle.shadow: true
hexagon.shadow: true hexagon.shadow: true
cloud.shadow: true cloud.shadow: true
double_circle.shadow: true
`, `,
}, },
{ {

View file

@ -24,7 +24,6 @@ const (
CIRCLE_TYPE = "Circle" CIRCLE_TYPE = "Circle"
HEXAGON_TYPE = "Hexagon" HEXAGON_TYPE = "Hexagon"
CLOUD_TYPE = "Cloud" CLOUD_TYPE = "Cloud"
DOUBLE_CIRCLE_TYPE = "DoubleCircle"
TABLE_TYPE = "Table" TABLE_TYPE = "Table"
CLASS_TYPE = "Class" CLASS_TYPE = "Class"
@ -109,8 +108,6 @@ func NewShape(shapeType string, box *geo.Box) Shape {
return NewCallout(box) return NewCallout(box)
case CIRCLE_TYPE: case CIRCLE_TYPE:
return NewCircle(box) return NewCircle(box)
case DOUBLE_CIRCLE_TYPE:
return NewDoubleCircle(box)
case CLASS_TYPE: case CLASS_TYPE:
return NewClass(box) return NewClass(box)
case CLOUD_TYPE: case CLOUD_TYPE:

View file

@ -1,37 +0,0 @@
package shape
import (
"math"
"oss.terrastruct.com/d2/lib/geo"
)
type shapeDoubleCircle struct {
*baseShape
}
func NewDoubleCircle(box *geo.Box) Shape {
return shapeDoubleCircle{
baseShape: &baseShape{
Type: DOUBLE_CIRCLE_TYPE,
Box: box,
},
}
}
func (s shapeDoubleCircle) AspectRatio1() bool {
return true
}
func (s shapeDoubleCircle) GetDimensionsToFit(width, height, padding float64) (float64, float64) {
radius := math.Ceil(math.Sqrt(math.Pow(width/2, 2)+math.Pow(height/2, 2))) + padding
return radius * 2, radius * 2
}
func (s shapeDoubleCircle) GetInsidePlacement(width, height, padding float64) geo.Point {
return *geo.NewPoint(s.Box.TopLeft.X+math.Ceil(s.Box.Width/2-width/2), s.Box.TopLeft.Y+math.Ceil(s.Box.Height/2-height/2))
}
func (s shapeDoubleCircle) Perimeter() []geo.Intersectable {
return []geo.Intersectable{geo.NewEllipse(s.Box.Center(), s.Box.Width/2, s.Box.Height/2)}
}