fix: cr, add labelMaskID for clipPath
This commit is contained in:
parent
833967a296
commit
2d66f3105a
5 changed files with 19 additions and 17 deletions
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"oss.terrastruct.com/d2/lib/svg"
|
"oss.terrastruct.com/d2/lib/svg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
func classHeader(labelMaskID string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
||||||
rectEl := d2themes.NewThemableElement("rect")
|
rectEl := d2themes.NewThemableElement("rect")
|
||||||
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
|
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
|
||||||
rectEl.Width, rectEl.Height = box.Width, box.Height
|
rectEl.Width, rectEl.Height = box.Width, box.Height
|
||||||
|
|
@ -20,7 +20,7 @@ func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, tex
|
||||||
rectEl.ClassName = "class_header"
|
rectEl.ClassName = "class_header"
|
||||||
str := rectEl.Render()
|
str := rectEl.Render()
|
||||||
if shape.BorderRadius != 0 {
|
if shape.BorderRadius != 0 {
|
||||||
str = strings.Replace(str, "/>", fmt.Sprintf(`clip-path="url(#%v)" />`, shape.ID), 1)
|
str = strings.Replace(str, "/>", fmt.Sprintf(`clip-path="url(#%v-%v)" />`, labelMaskID, shape.ID), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if text != "" {
|
if text != "" {
|
||||||
|
|
@ -85,7 +85,7 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
func drawClass(writer io.Writer, labelMaskID string, targetShape d2target.Shape) {
|
||||||
el := d2themes.NewThemableElement("rect")
|
el := d2themes.NewThemableElement("rect")
|
||||||
el.X = float64(targetShape.Pos.X)
|
el.X = float64(targetShape.Pos.X)
|
||||||
el.Y = float64(targetShape.Pos.Y)
|
el.Y = float64(targetShape.Pos.Y)
|
||||||
|
|
@ -108,7 +108,7 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
||||||
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
|
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
|
||||||
|
|
||||||
fmt.Fprint(writer,
|
fmt.Fprint(writer,
|
||||||
classHeader(targetShape, headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
classHeader(labelMaskID, targetShape, headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
||||||
)
|
)
|
||||||
|
|
||||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||||
|
|
|
||||||
|
|
@ -865,7 +865,7 @@ func render3dHexagon(targetShape d2target.Shape) string {
|
||||||
return borderMask + mainShapeRendered + renderedSides + renderedBorder
|
return borderMask + mainShapeRendered + renderedSides + renderedBorder
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
|
func drawShape(writer io.Writer, labelMaskID string, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
|
||||||
closingTag := "</g>"
|
closingTag := "</g>"
|
||||||
if targetShape.Link != "" {
|
if targetShape.Link != "" {
|
||||||
|
|
||||||
|
|
@ -880,7 +880,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
|
|
||||||
// this clipPath must be defined outside `g` element
|
// this clipPath must be defined outside `g` element
|
||||||
if targetShape.BorderRadius != 0 && (targetShape.Type == d2target.ShapeClass || targetShape.Type == d2target.ShapeSQLTable) {
|
if targetShape.BorderRadius != 0 && (targetShape.Type == d2target.ShapeClass || targetShape.Type == d2target.ShapeSQLTable) {
|
||||||
fmt.Fprint(writer, tableHeaderBorderRadius(targetShape))
|
fmt.Fprint(writer, tableHeaderBorderRadius(labelMaskID, targetShape))
|
||||||
}
|
}
|
||||||
fmt.Fprintf(writer, `<g id="%s"%s>`, svg.EscapeText(targetShape.ID), opacityStyle)
|
fmt.Fprintf(writer, `<g id="%s"%s>`, svg.EscapeText(targetShape.ID), opacityStyle)
|
||||||
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
||||||
|
|
@ -925,7 +925,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
}
|
}
|
||||||
fmt.Fprint(writer, out)
|
fmt.Fprint(writer, out)
|
||||||
} else {
|
} else {
|
||||||
drawClass(writer, targetShape)
|
drawClass(writer, labelMaskID, targetShape)
|
||||||
}
|
}
|
||||||
addAppendixItems(writer, targetShape)
|
addAppendixItems(writer, targetShape)
|
||||||
fmt.Fprint(writer, `</g>`)
|
fmt.Fprint(writer, `</g>`)
|
||||||
|
|
@ -939,7 +939,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
|
||||||
}
|
}
|
||||||
fmt.Fprint(writer, out)
|
fmt.Fprint(writer, out)
|
||||||
} else {
|
} else {
|
||||||
drawTable(writer, targetShape)
|
drawTable(writer, labelMaskID, targetShape)
|
||||||
}
|
}
|
||||||
addAppendixItems(writer, targetShape)
|
addAppendixItems(writer, targetShape)
|
||||||
fmt.Fprint(writer, `</g>`)
|
fmt.Fprint(writer, `</g>`)
|
||||||
|
|
@ -1680,7 +1680,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||||
labelMasks = append(labelMasks, labelMask)
|
labelMasks = append(labelMasks, labelMask)
|
||||||
}
|
}
|
||||||
} else if s, is := obj.(d2target.Shape); is {
|
} else if s, is := obj.(d2target.Shape); is {
|
||||||
labelMask, err := drawShape(buf, s, sketchRunner)
|
labelMask, err := drawShape(buf, labelMaskID, s, sketchRunner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if labelMask != "" {
|
} else if labelMask != "" {
|
||||||
|
|
|
||||||
|
|
@ -13,14 +13,16 @@ import (
|
||||||
"oss.terrastruct.com/util-go/go2"
|
"oss.terrastruct.com/util-go/go2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func tableHeaderBorderRadius(shape d2target.Shape) string {
|
// this func helps define a clipPath for shape class and sql_table
|
||||||
|
func tableHeaderBorderRadius(labelMaskID string, shape d2target.Shape) string {
|
||||||
box := geo.NewBox(
|
box := geo.NewBox(
|
||||||
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
|
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
|
||||||
float64(shape.Width),
|
float64(shape.Width),
|
||||||
float64(shape.Height),
|
float64(shape.Height),
|
||||||
)
|
)
|
||||||
topX, topY := box.TopLeft.X+box.Width, box.TopLeft.Y
|
topX, topY := box.TopLeft.X+box.Width, box.TopLeft.Y
|
||||||
out := fmt.Sprintf(`<clipPath id="%v">`, shape.ID)
|
|
||||||
|
out := fmt.Sprintf(`<clipPath id="%v-%v">`, labelMaskID, shape.ID)
|
||||||
out += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y, box.TopLeft.X+float64(shape.BorderRadius), box.TopLeft.Y)
|
out += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y, box.TopLeft.X+float64(shape.BorderRadius), box.TopLeft.Y)
|
||||||
out += fmt.Sprintf(`L %f %f L %f %f `, box.TopLeft.X+box.Width-float64(shape.BorderRadius), box.TopLeft.Y, topX-float64(shape.BorderRadius), topY)
|
out += fmt.Sprintf(`L %f %f L %f %f `, box.TopLeft.X+box.Width-float64(shape.BorderRadius), box.TopLeft.Y, topX-float64(shape.BorderRadius), topY)
|
||||||
|
|
||||||
|
|
@ -39,7 +41,7 @@ func tableHeaderBorderRadius(shape d2target.Shape) string {
|
||||||
return out + `fill="none" /> </clipPath>`
|
return out + `fill="none" /> </clipPath>`
|
||||||
}
|
}
|
||||||
|
|
||||||
func tableHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
func tableHeader(labelMaskID string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
||||||
rectEl := d2themes.NewThemableElement("rect")
|
rectEl := d2themes.NewThemableElement("rect")
|
||||||
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
|
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
|
||||||
rectEl.Width, rectEl.Height = box.Width, box.Height
|
rectEl.Width, rectEl.Height = box.Width, box.Height
|
||||||
|
|
@ -47,7 +49,7 @@ func tableHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, tex
|
||||||
rectEl.ClassName = "class_header"
|
rectEl.ClassName = "class_header"
|
||||||
str := rectEl.Render()
|
str := rectEl.Render()
|
||||||
if shape.BorderRadius != 0 {
|
if shape.BorderRadius != 0 {
|
||||||
str = strings.Replace(str, "/>", fmt.Sprintf(`clip-path="url(#%v)" />`, shape.ID), 1)
|
str = strings.Replace(str, "/>", fmt.Sprintf(`clip-path="url(#%v-%v)" />`, labelMaskID, shape.ID), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if text != "" {
|
if text != "" {
|
||||||
|
|
@ -112,7 +114,7 @@ func tableRow(shape d2target.Shape, box *geo.Box, nameText, typeText, constraint
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
func drawTable(writer io.Writer, labelMaskID string, targetShape d2target.Shape) {
|
||||||
rectEl := d2themes.NewThemableElement("rect")
|
rectEl := d2themes.NewThemableElement("rect")
|
||||||
rectEl.X = float64(targetShape.Pos.X)
|
rectEl.X = float64(targetShape.Pos.X)
|
||||||
rectEl.Y = float64(targetShape.Pos.Y)
|
rectEl.Y = float64(targetShape.Pos.Y)
|
||||||
|
|
@ -136,7 +138,7 @@ func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
||||||
headerBox := geo.NewBox(box.TopLeft, box.Width, rowHeight)
|
headerBox := geo.NewBox(box.TopLeft, box.Width, rowHeight)
|
||||||
|
|
||||||
fmt.Fprint(writer,
|
fmt.Fprint(writer,
|
||||||
tableHeader(targetShape, headerBox, targetShape.Label,
|
tableHeader(labelMaskID, targetShape, headerBox, targetShape.Label,
|
||||||
float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 515 KiB After Width: | Height: | Size: 515 KiB |
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 515 KiB After Width: | Height: | Size: 515 KiB |
Loading…
Reference in a new issue