label fill

This commit is contained in:
Alexander Wang 2023-02-09 14:14:31 -08:00
parent 2af6681c7d
commit ad24108446
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 22 additions and 3 deletions

View file

@ -129,6 +129,11 @@ func toShape(obj *d2graph.Object, theme *d2themes.Theme) d2target.Shape {
shape.Color = text.GetColor(theme, shape.Italic)
applyStyles(shape, obj)
if obj.IsSequenceDiagramGroup() {
shape.StrokeWidth = 0
shape.Blend = true
}
switch obj.Attributes.Shape.Value {
case d2target.ShapeCode, d2target.ShapeText:
shape.Language = obj.Attributes.Language
@ -147,6 +152,9 @@ func toShape(obj *d2graph.Object, theme *d2themes.Theme) d2target.Shape {
shape.LabelHeight = text.Dimensions.Height
if obj.LabelPosition != nil {
shape.LabelPosition = *obj.LabelPosition
if obj.IsSequenceDiagramGroup() {
shape.LabelFill = shape.Fill
}
}
shape.Tooltip = obj.Attributes.Tooltip
@ -163,7 +171,6 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection
connection := d2target.BaseConnection()
connection.ID = edge.AbsID()
connection.ZIndex = edge.ZIndex
// edge.Edge.ID = go2.StringToIntHash(connection.ID)
text := edge.Text()
if edge.SrcArrow {

View file

@ -986,9 +986,20 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
fontColor = targetShape.Color
}
textStyle := fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "middle", targetShape.FontSize, fontColor)
x := labelTL.X + float64(targetShape.LabelWidth)/2.
// text is vertically positioned at its baseline which is at labelTL+FontSize
y := labelTL.Y + float64(targetShape.FontSize)
// background does not exist for <text>, so draw a rectangle behind it
if targetShape.LabelFill != "" {
fmt.Fprintf(writer, `<rect x="%f" y="%f" width="%d" height="%d" fill="%s"></rect>`,
labelTL.X, labelTL.Y,
targetShape.LabelWidth, targetShape.LabelHeight,
targetShape.LabelFill,
)
}
fmt.Fprintf(writer, `<text class="%s" x="%f" y="%f" style="%s">%s</text>`,
fontClass,
x, y,

View file

@ -236,8 +236,9 @@ type Text struct {
Bold bool `json:"bold"`
Underline bool `json:"underline"`
LabelWidth int `json:"labelWidth"`
LabelHeight int `json:"labelHeight"`
LabelWidth int `json:"labelWidth"`
LabelHeight int `json:"labelHeight"`
LabelFill string `json:"labelFill,omitempty"`
}
func BaseShape() *Shape {