don't use ruler if doesn't exist

This commit is contained in:
Alexander Wang 2022-12-23 08:44:46 -08:00
parent ce6d5d7775
commit 2a03b65f1c
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 56 additions and 48 deletions

View file

@ -1002,23 +1002,29 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler
maxTypeWidth := 0
constraintWidth := 0
font := d2fonts.SourceSansPro.Font(d2fonts.FONT_SIZE_L, d2fonts.FONT_STYLE_REGULAR)
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]
ctexts := c.Texts()
nameWidth, nameHeight := ruler.Measure(font, c.Name.Label)
c.Name.LabelWidth = nameWidth
c.Name.LabelHeight = nameHeight
if maxNameWidth < nameWidth {
maxNameWidth = nameWidth
nameDims := getTextDimensions(mtexts, ruler, ctexts[0], fontFamily)
if nameDims == nil {
return fmt.Errorf("dimensions for sql_table name %#v not found", ctexts[0].Text)
}
c.Name.LabelWidth = nameDims.Width
c.Name.LabelHeight = nameDims.Height
if maxNameWidth < nameDims.Width {
maxNameWidth = nameDims.Width
}
typeWidth, typeHeight := ruler.Measure(font, c.Type.Label)
c.Type.LabelWidth = typeWidth
c.Type.LabelHeight = typeHeight
if maxTypeWidth < typeWidth {
maxTypeWidth = typeWidth
typeDims := getTextDimensions(mtexts, ruler, ctexts[1], fontFamily)
if typeDims == nil {
return fmt.Errorf("dimensions for sql_table type %#v not found", ctexts[1].Text)
}
c.Type.LabelWidth = typeDims.Width
c.Type.LabelHeight = typeDims.Height
if maxTypeWidth < typeDims.Width {
maxTypeWidth = typeDims.Width
}
if c.Constraint != "" {

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 87 KiB

View file

@ -1,5 +1,7 @@
package d2target
import "oss.terrastruct.com/d2/d2renderers/d2fonts"
const (
NamePadding = 10
TypePadding = 20
@ -20,14 +22,14 @@ func (c SQLColumn) Texts() []*MText {
return []*MText{
{
Text: c.Name.Label,
FontSize: c.Name.FontSize,
FontSize: d2fonts.FONT_SIZE_L,
IsBold: false,
IsItalic: false,
Shape: "sql_table",
},
{
Text: c.Type.Label,
FontSize: c.Type.FontSize,
FontSize: d2fonts.FONT_SIZE_L,
IsBold: false,
IsItalic: false,
Shape: "sql_table",