fix for class

This commit is contained in:
Alexander Wang 2023-02-06 00:47:13 -08:00
parent 301e692a24
commit 01445768d8
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
7 changed files with 435 additions and 40 deletions

View file

@ -795,17 +795,22 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R
case d2target.ShapeClass: case d2target.ShapeClass:
maxWidth := go2.Max(12, labelDims.Width) maxWidth := go2.Max(12, labelDims.Width)
fontSize := d2fonts.FONT_SIZE_L
if obj.Attributes.Style.FontSize != nil {
fontSize, _ = strconv.Atoi(obj.Attributes.Style.FontSize.Value)
}
for _, f := range obj.Class.Fields { for _, f := range obj.Class.Fields {
fdims := GetTextDimensions(mtexts, ruler, f.Text(), go2.Pointer(d2fonts.SourceCodePro)) fdims := GetTextDimensions(mtexts, ruler, f.Text(fontSize), go2.Pointer(d2fonts.SourceCodePro))
if fdims == nil { if fdims == nil {
return nil, fmt.Errorf("dimensions for class field %#v not found", f.Text()) return nil, fmt.Errorf("dimensions for class field %#v not found", f.Text(fontSize))
} }
maxWidth = go2.Max(maxWidth, fdims.Width) maxWidth = go2.Max(maxWidth, fdims.Width)
} }
for _, m := range obj.Class.Methods { for _, m := range obj.Class.Methods {
mdims := GetTextDimensions(mtexts, ruler, m.Text(), go2.Pointer(d2fonts.SourceCodePro)) mdims := GetTextDimensions(mtexts, ruler, m.Text(fontSize), go2.Pointer(d2fonts.SourceCodePro))
if mdims == nil { if mdims == nil {
return nil, fmt.Errorf("dimensions for class method %#v not found", m.Text()) return nil, fmt.Errorf("dimensions for class method %#v not found", m.Text(fontSize))
} }
maxWidth = go2.Max(maxWidth, mdims.Width) maxWidth = go2.Max(maxWidth, mdims.Width)
} }
@ -820,9 +825,9 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R
// All rows should be the same height // All rows should be the same height
var anyRowText *d2target.MText var anyRowText *d2target.MText
if len(obj.Class.Fields) > 0 { if len(obj.Class.Fields) > 0 {
anyRowText = obj.Class.Fields[0].Text() anyRowText = obj.Class.Fields[0].Text(fontSize)
} else if len(obj.Class.Methods) > 0 { } else if len(obj.Class.Methods) > 0 {
anyRowText = obj.Class.Methods[0].Text() anyRowText = obj.Class.Methods[0].Text(fontSize)
} }
if anyRowText != nil { if anyRowText != nil {
rowHeight := GetTextDimensions(mtexts, ruler, anyRowText, go2.Pointer(d2fonts.SourceCodePro)).Height + d2target.VerticalPadding rowHeight := GetTextDimensions(mtexts, ruler, anyRowText, go2.Pointer(d2fonts.SourceCodePro)).Height + d2target.VerticalPadding
@ -1333,11 +1338,15 @@ func (g *Graph) Texts() []*d2target.MText {
texts = appendTextDedup(texts, obj.Text()) texts = appendTextDedup(texts, obj.Text())
} }
if obj.Class != nil { if obj.Class != nil {
fontSize := d2fonts.FONT_SIZE_L
if obj.Attributes.Style.FontSize != nil {
fontSize, _ = strconv.Atoi(obj.Attributes.Style.FontSize.Value)
}
for _, field := range obj.Class.Fields { for _, field := range obj.Class.Fields {
texts = appendTextDedup(texts, field.Text()) texts = appendTextDedup(texts, field.Text(fontSize))
} }
for _, method := range obj.Class.Methods { for _, method := range obj.Class.Methods {
texts = appendTextDedup(texts, method.Text()) texts = appendTextDedup(texts, method.Text(fontSize))
} }
} else if obj.SQLTable != nil { } else if obj.SQLTable != nil {
colFontSize := d2fonts.FONT_SIZE_L colFontSize := d2fonts.FONT_SIZE_L

View file

@ -2,8 +2,6 @@ package d2target
import ( import (
"fmt" "fmt"
"oss.terrastruct.com/d2/d2renderers/d2fonts"
) )
const ( const (
@ -25,10 +23,10 @@ type ClassField struct {
Visibility string `json:"visibility"` Visibility string `json:"visibility"`
} }
func (cf ClassField) Text() *MText { func (cf ClassField) Text(fontSize int) *MText {
return &MText{ return &MText{
Text: fmt.Sprintf("%s%s", cf.Name, cf.Type), Text: fmt.Sprintf("%s%s", cf.Name, cf.Type),
FontSize: d2fonts.FONT_SIZE_L, FontSize: fontSize,
IsBold: false, IsBold: false,
IsItalic: false, IsItalic: false,
Shape: "class", Shape: "class",
@ -52,10 +50,10 @@ type ClassMethod struct {
Visibility string `json:"visibility"` Visibility string `json:"visibility"`
} }
func (cm ClassMethod) Text() *MText { func (cm ClassMethod) Text(fontSize int) *MText {
return &MText{ return &MText{
Text: fmt.Sprintf("%s%s", cm.Name, cm.Return), Text: fmt.Sprintf("%s%s", cm.Name, cm.Return),
FontSize: d2fonts.FONT_SIZE_L, FontSize: fontSize,
IsBold: false, IsBold: false,
IsItalic: false, IsItalic: false,
Shape: "class", Shape: "class",

View file

@ -1861,6 +1861,32 @@ Humor in the Court2: {
style.font-color: orange style.font-color: orange
style.font-size: 30 style.font-size: 30
} }
manager: BatchManager {
shape: class
style.font-size: 20
-num: int
-timeout: int
-pid
+getStatus(): Enum
+getJobs(): "Job[]"
+setTimeout(seconds int)
}
manager2: BatchManager {
shape: class
style.font-size: 30
-num: int
-timeout: int
-pid
+getStatus(): Enum
+getJobs(): "Job[]"
+setTimeout(seconds int)
}
`, `,
}, },
{ {

View file

@ -7,7 +7,7 @@
"type": "sql_table", "type": "sql_table",
"pos": { "pos": {
"x": 0, "x": 0,
"y": 18 "y": 178
}, },
"width": 678, "width": 678,
"height": 108, "height": 108,
@ -107,7 +107,7 @@
"type": "sql_table", "type": "sql_table",
"pos": { "pos": {
"x": 738, "x": 738,
"y": 0 "y": 160
}, },
"width": 992, "width": 992,
"height": 144, "height": 144,
@ -201,6 +201,156 @@
"primaryAccentColor": "#0D32B2", "primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3", "secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E" "neutralAccentColor": "#676C7E"
},
{
"id": "manager",
"type": "class",
"pos": {
"x": 1790,
"y": 48
},
"width": 422,
"height": 368,
"opacity": 1,
"strokeDash": 0,
"strokeWidth": 2,
"borderRadius": 0,
"fill": "#0A0F25",
"stroke": "#FFFFFF",
"shadow": false,
"3d": false,
"multiple": false,
"double-border": false,
"tooltip": "",
"link": "",
"icon": null,
"iconPosition": "",
"blend": false,
"fields": [
{
"name": "num",
"type": "int",
"visibility": "private"
},
{
"name": "timeout",
"type": "int",
"visibility": "private"
},
{
"name": "pid",
"type": "",
"visibility": "private"
}
],
"methods": [
{
"name": "getStatus()",
"return": "Enum",
"visibility": "public"
},
{
"name": "getJobs()",
"return": "Job[]",
"visibility": "public"
},
{
"name": "setTimeout(seconds int)",
"return": "void",
"visibility": "public"
}
],
"columns": null,
"label": "BatchManager",
"fontSize": 20,
"fontFamily": "DEFAULT",
"language": "",
"color": "#0A0F25",
"italic": false,
"bold": false,
"underline": false,
"labelWidth": 170,
"labelHeight": 31,
"zIndex": 0,
"level": 1,
"primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E"
},
{
"id": "manager2",
"type": "class",
"pos": {
"x": 2272,
"y": 0
},
"width": 582,
"height": 464,
"opacity": 1,
"strokeDash": 0,
"strokeWidth": 2,
"borderRadius": 0,
"fill": "#0A0F25",
"stroke": "#FFFFFF",
"shadow": false,
"3d": false,
"multiple": false,
"double-border": false,
"tooltip": "",
"link": "",
"icon": null,
"iconPosition": "",
"blend": false,
"fields": [
{
"name": "num",
"type": "int",
"visibility": "private"
},
{
"name": "timeout",
"type": "int",
"visibility": "private"
},
{
"name": "pid",
"type": "",
"visibility": "private"
}
],
"methods": [
{
"name": "getStatus()",
"return": "Enum",
"visibility": "public"
},
{
"name": "getJobs()",
"return": "Job[]",
"visibility": "public"
},
{
"name": "setTimeout(seconds int)",
"return": "void",
"visibility": "public"
}
],
"columns": null,
"label": "BatchManager",
"fontSize": 30,
"fontFamily": "DEFAULT",
"language": "",
"color": "#0A0F25",
"italic": false,
"bold": false,
"underline": false,
"labelWidth": 241,
"labelHeight": 43,
"zIndex": 0,
"level": 1,
"primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E"
} }
], ],
"connections": [] "connections": []

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 328 KiB

After

Width:  |  Height:  |  Size: 514 KiB

View file

@ -7,7 +7,7 @@
"type": "sql_table", "type": "sql_table",
"pos": { "pos": {
"x": 12, "x": 12,
"y": 30 "y": 190
}, },
"width": 678, "width": 678,
"height": 108, "height": 108,
@ -107,7 +107,7 @@
"type": "sql_table", "type": "sql_table",
"pos": { "pos": {
"x": 710, "x": 710,
"y": 12 "y": 172
}, },
"width": 992, "width": 992,
"height": 144, "height": 144,
@ -201,6 +201,156 @@
"primaryAccentColor": "#0D32B2", "primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3", "secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E" "neutralAccentColor": "#676C7E"
},
{
"id": "manager",
"type": "class",
"pos": {
"x": 1722,
"y": 60
},
"width": 422,
"height": 368,
"opacity": 1,
"strokeDash": 0,
"strokeWidth": 2,
"borderRadius": 0,
"fill": "#0A0F25",
"stroke": "#FFFFFF",
"shadow": false,
"3d": false,
"multiple": false,
"double-border": false,
"tooltip": "",
"link": "",
"icon": null,
"iconPosition": "",
"blend": false,
"fields": [
{
"name": "num",
"type": "int",
"visibility": "private"
},
{
"name": "timeout",
"type": "int",
"visibility": "private"
},
{
"name": "pid",
"type": "",
"visibility": "private"
}
],
"methods": [
{
"name": "getStatus()",
"return": "Enum",
"visibility": "public"
},
{
"name": "getJobs()",
"return": "Job[]",
"visibility": "public"
},
{
"name": "setTimeout(seconds int)",
"return": "void",
"visibility": "public"
}
],
"columns": null,
"label": "BatchManager",
"fontSize": 20,
"fontFamily": "DEFAULT",
"language": "",
"color": "#0A0F25",
"italic": false,
"bold": false,
"underline": false,
"labelWidth": 170,
"labelHeight": 31,
"zIndex": 0,
"level": 1,
"primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E"
},
{
"id": "manager2",
"type": "class",
"pos": {
"x": 2164,
"y": 12
},
"width": 582,
"height": 464,
"opacity": 1,
"strokeDash": 0,
"strokeWidth": 2,
"borderRadius": 0,
"fill": "#0A0F25",
"stroke": "#FFFFFF",
"shadow": false,
"3d": false,
"multiple": false,
"double-border": false,
"tooltip": "",
"link": "",
"icon": null,
"iconPosition": "",
"blend": false,
"fields": [
{
"name": "num",
"type": "int",
"visibility": "private"
},
{
"name": "timeout",
"type": "int",
"visibility": "private"
},
{
"name": "pid",
"type": "",
"visibility": "private"
}
],
"methods": [
{
"name": "getStatus()",
"return": "Enum",
"visibility": "public"
},
{
"name": "getJobs()",
"return": "Job[]",
"visibility": "public"
},
{
"name": "setTimeout(seconds int)",
"return": "void",
"visibility": "public"
}
],
"columns": null,
"label": "BatchManager",
"fontSize": 30,
"fontFamily": "DEFAULT",
"language": "",
"color": "#0A0F25",
"italic": false,
"bold": false,
"underline": false,
"labelWidth": 241,
"labelHeight": 43,
"zIndex": 0,
"level": 1,
"primaryAccentColor": "#0D32B2",
"secondaryAccentColor": "#4A6FF3",
"neutralAccentColor": "#676C7E"
} }
], ],
"connections": [] "connections": []

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 328 KiB

After

Width:  |  Height:  |  Size: 514 KiB