refactor
This commit is contained in:
parent
48c9cc7929
commit
a6b34634cd
5 changed files with 34 additions and 54 deletions
|
|
@ -63,26 +63,6 @@ func DefineFillPattern() string {
|
||||||
</defs>`, fillPattern)
|
</defs>`, fillPattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
func shapeStyle(shape d2target.Shape) string {
|
|
||||||
out := ""
|
|
||||||
|
|
||||||
if shape.Type == d2target.ShapeSQLTable || shape.Type == d2target.ShapeClass {
|
|
||||||
out += fmt.Sprintf(`fill:%s;`, shape.Stroke)
|
|
||||||
out += fmt.Sprintf(`stroke:%s;`, shape.Fill)
|
|
||||||
} else {
|
|
||||||
out += fmt.Sprintf(`fill:%s;`, shape.Fill)
|
|
||||||
out += fmt.Sprintf(`stroke:%s;`, shape.Stroke)
|
|
||||||
}
|
|
||||||
out += fmt.Sprintf(`opacity:%f;`, shape.Opacity)
|
|
||||||
out += fmt.Sprintf(`stroke-width:%d;`, shape.StrokeWidth)
|
|
||||||
if shape.StrokeDash != 0 {
|
|
||||||
dashSize, gapSize := svg.GetStrokeDashAttributes(float64(shape.StrokeWidth), shape.StrokeDash)
|
|
||||||
out += fmt.Sprintf(`stroke-dasharray:%f,%f;`, dashSize, gapSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func Rect(r *Runner, shape d2target.Shape) (string, error) {
|
func Rect(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
|
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
|
||||||
fill: "%s",
|
fill: "%s",
|
||||||
|
|
@ -98,7 +78,7 @@ func Rect(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
||||||
shape.Pos.X, shape.Pos.Y, p, shapeStyle(shape),
|
shape.Pos.X, shape.Pos.Y, p, shape.CSSStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
|
|
@ -123,7 +103,7 @@ func Oval(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
||||||
shape.Pos.X, shape.Pos.Y, p, shapeStyle(shape),
|
shape.Pos.X, shape.Pos.Y, p, shape.CSSStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
|
|
@ -150,7 +130,7 @@ func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) {
|
||||||
for _, p := range sketchPaths {
|
for _, p := range sketchPaths {
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
`<path class="shape" d="%s" style="%s" />`,
|
`<path class="shape" d="%s" style="%s" />`,
|
||||||
p, shapeStyle(shape),
|
p, shape.CSSStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
for _, p := range sketchPaths {
|
for _, p := range sketchPaths {
|
||||||
|
|
@ -200,7 +180,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
||||||
shape.Pos.X, shape.Pos.Y, p, shapeStyle(shape),
|
shape.Pos.X, shape.Pos.Y, p, shape.CSSStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -328,7 +308,7 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
output += fmt.Sprintf(
|
output += fmt.Sprintf(
|
||||||
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
`<path class="shape" transform="translate(%d %d)" d="%s" style="%s" />`,
|
||||||
shape.Pos.X, shape.Pos.Y, p, shapeStyle(shape),
|
shape.Pos.X, shape.Pos.Y, p, shape.CSSStyle(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
|
||||||
|
|
||||||
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||||
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
||||||
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(targetShape))
|
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, targetShape.CSSStyle())
|
||||||
|
|
||||||
box := geo.NewBox(
|
box := geo.NewBox(
|
||||||
geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y)),
|
geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y)),
|
||||||
|
|
|
||||||
|
|
@ -586,7 +586,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
||||||
)
|
)
|
||||||
border := targetShape
|
border := targetShape
|
||||||
border.Fill = "none"
|
border.Fill = "none"
|
||||||
borderStyle := shapeStyle(border)
|
borderStyle := border.CSSStyle()
|
||||||
renderedBorder := fmt.Sprintf(`<path d="%s" style="%s"/>`,
|
renderedBorder := fmt.Sprintf(`<path d="%s" style="%s"/>`,
|
||||||
strings.Join(borderSegments, " "), borderStyle)
|
strings.Join(borderSegments, " "), borderStyle)
|
||||||
|
|
||||||
|
|
@ -607,7 +607,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
||||||
mainShape := targetShape
|
mainShape := targetShape
|
||||||
mainShape.Stroke = "none"
|
mainShape.Stroke = "none"
|
||||||
mainRect := fmt.Sprintf(`<rect x="%d" y="%d" width="%d" height="%d" style="%s" mask="url(#%s)"/>`,
|
mainRect := fmt.Sprintf(`<rect x="%d" y="%d" width="%d" height="%d" style="%s" mask="url(#%s)"/>`,
|
||||||
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(mainShape), maskID,
|
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, mainShape.CSSStyle(), maskID,
|
||||||
)
|
)
|
||||||
|
|
||||||
// render the side shapes in the darkened color without stroke and the border mask
|
// render the side shapes in the darkened color without stroke and the border mask
|
||||||
|
|
@ -632,7 +632,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
||||||
sideShape.Fill = darkerColor
|
sideShape.Fill = darkerColor
|
||||||
sideShape.Stroke = "none"
|
sideShape.Stroke = "none"
|
||||||
renderedSides := fmt.Sprintf(`<polygon points="%s" style="%s" mask="url(#%s)"/>`,
|
renderedSides := fmt.Sprintf(`<polygon points="%s" style="%s" mask="url(#%s)"/>`,
|
||||||
strings.Join(sidePoints, " "), shapeStyle(sideShape), maskID)
|
strings.Join(sidePoints, " "), sideShape.CSSStyle(), maskID)
|
||||||
|
|
||||||
return borderMask + mainRect + renderedSides + renderedBorder
|
return borderMask + mainRect + renderedSides + renderedBorder
|
||||||
}
|
}
|
||||||
|
|
@ -647,7 +647,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
||||||
width := float64(targetShape.Width)
|
width := float64(targetShape.Width)
|
||||||
height := float64(targetShape.Height)
|
height := float64(targetShape.Height)
|
||||||
style := shapeStyle(targetShape)
|
style := targetShape.CSSStyle()
|
||||||
shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[targetShape.Type]
|
shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[targetShape.Type]
|
||||||
|
|
||||||
s := shape.NewShape(shapeType, geo.NewBox(tl, width, height))
|
s := shape.NewShape(shapeType, geo.NewBox(tl, width, height))
|
||||||
|
|
@ -943,29 +943,6 @@ func RenderText(text string, x, height float64) string {
|
||||||
return strings.Join(rendered, "")
|
return strings.Join(rendered, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func shapeStyle(shape d2target.Shape) string {
|
|
||||||
out := ""
|
|
||||||
|
|
||||||
if shape.Type == d2target.ShapeSQLTable || shape.Type == d2target.ShapeClass {
|
|
||||||
// Fill is used for header fill in these types
|
|
||||||
// This fill property is just background of rows
|
|
||||||
out += fmt.Sprintf(`fill:%s;`, shape.Stroke)
|
|
||||||
// Stroke (border) of these shapes should match the header fill
|
|
||||||
out += fmt.Sprintf(`stroke:%s;`, shape.Fill)
|
|
||||||
} else {
|
|
||||||
out += fmt.Sprintf(`fill:%s;`, shape.Fill)
|
|
||||||
out += fmt.Sprintf(`stroke:%s;`, shape.Stroke)
|
|
||||||
}
|
|
||||||
out += fmt.Sprintf(`opacity:%f;`, shape.Opacity)
|
|
||||||
out += fmt.Sprintf(`stroke-width:%d;`, shape.StrokeWidth)
|
|
||||||
if shape.StrokeDash != 0 {
|
|
||||||
dashSize, gapSize := svg.GetStrokeDashAttributes(float64(shape.StrokeWidth), shape.StrokeDash)
|
|
||||||
out += fmt.Sprintf(`stroke-dasharray:%f,%f;`, dashSize, gapSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func embedFonts(buf *bytes.Buffer, fontFamily *d2fonts.FontFamily) {
|
func embedFonts(buf *bytes.Buffer, fontFamily *d2fonts.FontFamily) {
|
||||||
content := buf.String()
|
content := buf.String()
|
||||||
buf.WriteString(`<style type="text/css"><![CDATA[`)
|
buf.WriteString(`<style type="text/css"><![CDATA[`)
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ func tableRow(shape d2target.Shape, box *geo.Box, nameText, typeText, constraint
|
||||||
|
|
||||||
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
||||||
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
fmt.Fprintf(writer, `<rect class="shape" x="%d" y="%d" width="%d" height="%d" style="%s"/>`,
|
||||||
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, shapeStyle(targetShape))
|
targetShape.Pos.X, targetShape.Pos.Y, targetShape.Width, targetShape.Height, targetShape.CSSStyle())
|
||||||
|
|
||||||
box := geo.NewBox(
|
box := geo.NewBox(
|
||||||
geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y)),
|
geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y)),
|
||||||
|
|
|
||||||
|
|
@ -160,6 +160,29 @@ type Shape struct {
|
||||||
NeutralAccentColor string `json:"neutralAccentColor,omitempty"`
|
NeutralAccentColor string `json:"neutralAccentColor,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Shape) CSSStyle() string {
|
||||||
|
out := ""
|
||||||
|
|
||||||
|
if s.Type == ShapeSQLTable || s.Type == ShapeClass {
|
||||||
|
// Fill is used for header fill in these types
|
||||||
|
// This fill property is just background of rows
|
||||||
|
out += fmt.Sprintf(`fill:%s;`, s.Stroke)
|
||||||
|
// Stroke (border) of these shapes should match the header fill
|
||||||
|
out += fmt.Sprintf(`stroke:%s;`, s.Fill)
|
||||||
|
} else {
|
||||||
|
out += fmt.Sprintf(`fill:%s;`, s.Fill)
|
||||||
|
out += fmt.Sprintf(`stroke:%s;`, s.Stroke)
|
||||||
|
}
|
||||||
|
out += fmt.Sprintf(`opacity:%f;`, s.Opacity)
|
||||||
|
out += fmt.Sprintf(`stroke-width:%d;`, s.StrokeWidth)
|
||||||
|
if s.StrokeDash != 0 {
|
||||||
|
dashSize, gapSize := svg.GetStrokeDashAttributes(float64(s.StrokeWidth), s.StrokeDash)
|
||||||
|
out += fmt.Sprintf(`stroke-dasharray:%f,%f;`, dashSize, gapSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Shape) SetType(t string) {
|
func (s *Shape) SetType(t string) {
|
||||||
// Some types are synonyms of other types, but with hinting for autolayout
|
// Some types are synonyms of other types, but with hinting for autolayout
|
||||||
// They should only have one representation in the final export
|
// They should only have one representation in the final export
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue