export column name/type dimensions to avoid ruler in renderer
This commit is contained in:
parent
e27999882d
commit
0c71c63b2d
10 changed files with 38 additions and 34 deletions
|
|
@ -669,8 +669,8 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) {
|
|||
typ = ""
|
||||
}
|
||||
d2Col := d2target.SQLColumn{
|
||||
Name: col.IDVal,
|
||||
Type: typ,
|
||||
Name: d2target.Text{Label: col.IDVal},
|
||||
Type: d2target.Text{Label: typ},
|
||||
}
|
||||
// The only map a sql table field could have is to specify constraint
|
||||
if col.Map != nil {
|
||||
|
|
|
|||
|
|
@ -1465,8 +1465,8 @@ b`, g.Objects[0].Attributes.Label.Value)
|
|||
if len(g.Objects) != 1 {
|
||||
t.Fatal(g.Objects)
|
||||
}
|
||||
assert.String(t, `GetType()`, g.Objects[0].SQLTable.Columns[0].Name)
|
||||
assert.String(t, `Is()`, g.Objects[0].SQLTable.Columns[1].Name)
|
||||
assert.String(t, `GetType()`, g.Objects[0].SQLTable.Columns[0].Name.Label)
|
||||
assert.String(t, `Is()`, g.Objects[0].SQLTable.Columns[1].Name.Label)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -1490,8 +1490,8 @@ b`, g.Objects[0].Attributes.Label.Value)
|
|||
if len(g.Objects[0].ChildrenArray) != 1 {
|
||||
t.Fatal(g.Objects)
|
||||
}
|
||||
assert.String(t, `GetType()`, g.Objects[1].SQLTable.Columns[0].Name)
|
||||
assert.String(t, `Is()`, g.Objects[1].SQLTable.Columns[1].Name)
|
||||
assert.String(t, `GetType()`, g.Objects[1].SQLTable.Columns[0].Name.Label)
|
||||
assert.String(t, `Is()`, g.Objects[1].SQLTable.Columns[1].Name.Label)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
|||
|
|
@ -995,23 +995,32 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler
|
|||
obj.Width = float64(maxWidth + 100)
|
||||
|
||||
case d2target.ShapeSQLTable:
|
||||
maxNameWidth := 0.
|
||||
maxTypeWidth := 0.
|
||||
constraintWidth := 0.
|
||||
maxNameWidth := 0
|
||||
maxTypeWidth := 0
|
||||
constraintWidth := 0
|
||||
|
||||
font := d2fonts.SourceSansPro.Font(d2fonts.FONT_SIZE_L, d2fonts.FONT_STYLE_REGULAR)
|
||||
for _, c := range obj.SQLTable.Columns {
|
||||
nameWidth, _ := ruler.MeasurePrecise(font, c.Name)
|
||||
for i := range obj.SQLTable.Columns {
|
||||
// Note: we want to set dimensions of actual column not the for loop copy of the struct
|
||||
c := &obj.SQLTable.Columns[i]
|
||||
|
||||
nameWidth, nameHeight := ruler.Measure(font, c.Name.Label)
|
||||
c.Name.LabelWidth = nameWidth
|
||||
c.Name.LabelHeight = nameHeight
|
||||
if maxNameWidth < nameWidth {
|
||||
maxNameWidth = nameWidth
|
||||
}
|
||||
typeWidth, _ := ruler.MeasurePrecise(font, c.Type)
|
||||
|
||||
typeWidth, typeHeight := ruler.Measure(font, c.Type.Label)
|
||||
c.Type.LabelWidth = typeWidth
|
||||
c.Type.LabelHeight = typeHeight
|
||||
if maxTypeWidth < typeWidth {
|
||||
maxTypeWidth = typeWidth
|
||||
}
|
||||
|
||||
if c.Constraint != "" {
|
||||
// covers UNQ constraint with padding
|
||||
constraintWidth = 60.
|
||||
constraintWidth = 60
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -589,7 +589,7 @@ func render3dRect(targetShape d2target.Shape) string {
|
|||
return borderMask + mainRect + renderedSides + renderedBorder
|
||||
}
|
||||
|
||||
func drawShape(writer io.Writer, targetShape d2target.Shape, ruler *textmeasure.Ruler) (labelMask string, err error) {
|
||||
func drawShape(writer io.Writer, targetShape d2target.Shape) (labelMask string, err error) {
|
||||
fmt.Fprintf(writer, `<g id="%s">`, escapeText(targetShape.ID))
|
||||
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
|
||||
width := float64(targetShape.Width)
|
||||
|
|
@ -629,7 +629,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, ruler *textmeasure.
|
|||
fmt.Fprintf(writer, `</g></g>`)
|
||||
return labelMask, nil
|
||||
case d2target.ShapeSQLTable:
|
||||
drawTable(writer, targetShape, ruler)
|
||||
drawTable(writer, targetShape)
|
||||
fmt.Fprintf(writer, `</g></g>`)
|
||||
return labelMask, nil
|
||||
case d2target.ShapeOval:
|
||||
|
|
@ -963,7 +963,7 @@ func embedFonts(buf *bytes.Buffer) {
|
|||
}
|
||||
|
||||
// TODO minify output at end
|
||||
func Render(diagram *d2target.Diagram, ruler *textmeasure.Ruler, pad int) ([]byte, error) {
|
||||
func Render(diagram *d2target.Diagram, pad int) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
w, h := setViewbox(buf, diagram, pad)
|
||||
|
||||
|
|
@ -1022,7 +1022,7 @@ func Render(diagram *d2target.Diagram, ruler *textmeasure.Ruler, pad int) ([]byt
|
|||
labelMasks = append(labelMasks, labelMask)
|
||||
}
|
||||
} else if s, is := obj.(d2target.Shape); is {
|
||||
labelMask, err := drawShape(buf, s, ruler)
|
||||
labelMask, err := drawShape(buf, s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if labelMask != "" {
|
||||
|
|
|
|||
|
|
@ -3,14 +3,12 @@ package d2svg
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"oss.terrastruct.com/d2/d2renderers/d2fonts"
|
||||
"oss.terrastruct.com/d2/d2target"
|
||||
"oss.terrastruct.com/d2/lib/geo"
|
||||
"oss.terrastruct.com/d2/lib/label"
|
||||
"oss.terrastruct.com/d2/lib/textmeasure"
|
||||
"oss.terrastruct.com/util-go/go2"
|
||||
)
|
||||
|
||||
const namePadding = 10
|
||||
|
|
@ -101,7 +99,7 @@ func constraintAbbr(constraint string) string {
|
|||
}
|
||||
}
|
||||
|
||||
func drawTable(writer io.Writer, targetShape d2target.Shape, ruler *textmeasure.Ruler) {
|
||||
func drawTable(writer io.Writer, targetShape d2target.Shape) {
|
||||
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))
|
||||
|
||||
|
|
@ -117,18 +115,16 @@ func drawTable(writer io.Writer, targetShape d2target.Shape, ruler *textmeasure.
|
|||
tableHeader(headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
||||
)
|
||||
|
||||
font := d2fonts.SourceSansPro.Font(targetShape.FontSize, d2fonts.FONT_STYLE_REGULAR)
|
||||
var longestNameWidth float64
|
||||
var longestNameWidth int
|
||||
for _, f := range targetShape.SQLTable.Columns {
|
||||
w, _ := ruler.MeasurePrecise(font, f.Name)
|
||||
longestNameWidth = math.Max(longestNameWidth, w)
|
||||
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
|
||||
}
|
||||
|
||||
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
||||
rowBox.TopLeft.Y += headerBox.Height
|
||||
for _, f := range targetShape.SQLTable.Columns {
|
||||
fmt.Fprint(writer,
|
||||
tableRow(rowBox, f.Name, f.Type, constraintAbbr(f.Constraint), float64(targetShape.FontSize), longestNameWidth),
|
||||
tableRow(rowBox, f.Name.Label, f.Type.Label, constraintAbbr(f.Constraint), float64(targetShape.FontSize), float64(longestNameWidth)),
|
||||
)
|
||||
rowBox.TopLeft.Y += rowHeight
|
||||
fmt.Fprintf(writer, `<line x1="%f" y1="%f" x2="%f" y2="%f" style="stroke-width:2;stroke:#0a0f25" />`,
|
||||
|
|
@ -136,5 +132,4 @@ func drawTable(writer io.Writer, targetShape d2target.Shape, ruler *textmeasure.
|
|||
rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@ type SQLTable struct {
|
|||
}
|
||||
|
||||
type SQLColumn struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Name Text `json:"name"`
|
||||
Type Text `json:"type"`
|
||||
Constraint string `json:"constraint"`
|
||||
Reference string `json:"reference"`
|
||||
}
|
||||
|
||||
func (c SQLColumn) Text() *MText {
|
||||
return &MText{
|
||||
Text: fmt.Sprintf("%s%s%s%s", c.Name, c.Type, c.Constraint, c.Reference),
|
||||
Text: fmt.Sprintf("%s%s%s%s", c.Name.Label, c.Type.Label, c.Constraint, c.Reference),
|
||||
FontSize: d2fonts.FONT_SIZE_L,
|
||||
IsBold: false,
|
||||
IsItalic: false,
|
||||
|
|
|
|||
|
|
@ -20,6 +20,6 @@ func main() {
|
|||
Ruler: ruler,
|
||||
ThemeID: d2themescatalog.GrapeSoda.ID,
|
||||
})
|
||||
out, _ := d2svg.Render(diagram, ruler, d2svg.DEFAULT_PADDING)
|
||||
out, _ := d2svg.Render(diagram, d2svg.DEFAULT_PADDING)
|
||||
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,6 @@ func main() {
|
|||
_ = graph.SetDimensions(nil, ruler)
|
||||
_ = d2dagrelayout.Layout(context.Background(), graph)
|
||||
diagram, _ := d2exporter.Export(context.Background(), graph, d2themescatalog.NeutralDefault.ID)
|
||||
out, _ := d2svg.Render(diagram, ruler, d2svg.DEFAULT_PADDING)
|
||||
out, _ := d2svg.Render(diagram, d2svg.DEFAULT_PADDING)
|
||||
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ func run(t *testing.T, tc testCase) {
|
|||
dataPath := filepath.Join("testdata", strings.TrimPrefix(t.Name(), "TestE2E/"), layoutName)
|
||||
pathGotSVG := filepath.Join(dataPath, "sketch.got.svg")
|
||||
|
||||
svgBytes, err := d2svg.Render(diagram, ruler, d2svg.DEFAULT_PADDING)
|
||||
svgBytes, err := d2svg.Render(diagram, d2svg.DEFAULT_PADDING)
|
||||
assert.Success(t, err)
|
||||
err = os.MkdirAll(dataPath, 0755)
|
||||
assert.Success(t, err)
|
||||
|
|
|
|||
2
main.go
2
main.go
|
|
@ -214,7 +214,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, pad,
|
|||
return nil, false, err
|
||||
}
|
||||
|
||||
svg, err := d2svg.Render(diagram, ruler, int(pad))
|
||||
svg, err := d2svg.Render(diagram, int(pad))
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue