diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index f74b14496..dc6ffcbbe 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -477,7 +477,7 @@ func (obj *Object) Text() *d2target.MText { } // Class and Table objects have Label set to header if obj.Class != nil || obj.SQLTable != nil { - fontSize = d2fonts.FONT_SIZE_XL + fontSize += d2target.HeaderFontAdd } if obj.Class != nil { isBold = false @@ -836,10 +836,16 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R maxTypeWidth := 0 constraintWidth := 0 + colFontSize := d2fonts.FONT_SIZE_L + if obj.Attributes.Style.FontSize != nil { + colFontSize, _ = strconv.Atoi(obj.Attributes.Style.FontSize.Value) + } + 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() + + ctexts := c.Texts(colFontSize) nameDims := GetTextDimensions(mtexts, ruler, ctexts[0], fontFamily) if nameDims == nil { @@ -1334,8 +1340,12 @@ func (g *Graph) Texts() []*d2target.MText { texts = appendTextDedup(texts, method.Text()) } } else if obj.SQLTable != nil { + colFontSize := d2fonts.FONT_SIZE_L + if obj.Attributes.Style.FontSize != nil { + colFontSize, _ = strconv.Atoi(obj.Attributes.Style.FontSize.Value) + } for _, column := range obj.SQLTable.Columns { - for _, t := range column.Texts() { + for _, t := range column.Texts(colFontSize) { texts = appendTextDedup(texts, t) } } diff --git a/d2target/sqltable.go b/d2target/sqltable.go index 8332a34c8..6ffbddf05 100644 --- a/d2target/sqltable.go +++ b/d2target/sqltable.go @@ -1,11 +1,13 @@ package d2target -import "oss.terrastruct.com/d2/d2renderers/d2fonts" - const ( NamePadding = 10 TypePadding = 20 - HeaderPadding = 20 + HeaderPadding = 10 + + // Setting table font size sets it for columns + // The header needs to be a little larger for visual hierarchy + HeaderFontAdd = 4 ) type SQLTable struct { @@ -19,18 +21,18 @@ type SQLColumn struct { Reference string `json:"reference"` } -func (c SQLColumn) Texts() []*MText { +func (c SQLColumn) Texts(fontSize int) []*MText { return []*MText{ { Text: c.Name.Label, - FontSize: d2fonts.FONT_SIZE_L, + FontSize: fontSize, IsBold: false, IsItalic: false, Shape: "sql_table", }, { Text: c.Type.Label, - FontSize: d2fonts.FONT_SIZE_L, + FontSize: fontSize, IsBold: false, IsItalic: false, Shape: "sql_table", diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go index a489f6398..de4ff5c74 100644 --- a/e2etests/stable_test.go +++ b/e2etests/stable_test.go @@ -1838,6 +1838,29 @@ x.y -> a.b: { style.animated: true target-arrowhead.shape: cf-many } +`, + }, + { + name: "sql_table_column_styles", + script: `Humor in the Court: { + shape: sql_table + Could you see him from where you were standing?: "I could see his head." + And where was his head?: Just above his shoulders. + style.fill: red + style.stroke: lightgray + style.font-color: orange + style.font-size: 20 +} + +Humor in the Court2: { + shape: sql_table + Could you see him from where you were standing?: "I could see his head." + And where was his head?: Just above his shoulders. + style.fill: red + style.stroke: lightgray + style.font-color: orange + style.font-size: 30 +} `, }, { diff --git a/e2etests/testdata/stable/sql_table_column_styles/dagre/board.exp.json b/e2etests/testdata/stable/sql_table_column_styles/dagre/board.exp.json new file mode 100644 index 000000000..fb282c898 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_column_styles/dagre/board.exp.json @@ -0,0 +1,207 @@ +{ + "name": "", + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "Humor in the Court", + "type": "sql_table", + "pos": { + "x": 0, + "y": 18 + }, + "width": 678, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "lightgray", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "Could you see him from where you were standing?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 418, + "labelHeight": 26 + }, + "type": { + "label": "I could see his head.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 166, + "labelHeight": 26 + }, + "constraint": "", + "reference": "" + }, + { + "name": { + "label": "And where was his head?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 208, + "labelHeight": 26 + }, + "type": { + "label": "Just above his shoulders.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 210, + "labelHeight": 26 + }, + "constraint": "", + "reference": "" + } + ], + "label": "Humor in the Court", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "orange", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 202, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "#0D32B2", + "secondaryAccentColor": "#4A6FF3", + "neutralAccentColor": "#676C7E" + }, + { + "id": "Humor in the Court2", + "type": "sql_table", + "pos": { + "x": 738, + "y": 0 + }, + "width": 992, + "height": 144, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "lightgray", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "Could you see him from where you were standing?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 627, + "labelHeight": 38 + }, + "type": { + "label": "I could see his head.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 248, + "labelHeight": 38 + }, + "constraint": "", + "reference": "" + }, + { + "name": { + "label": "And where was his head?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 312, + "labelHeight": 38 + }, + "type": { + "label": "Just above his shoulders.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 315, + "labelHeight": 38 + }, + "constraint": "", + "reference": "" + } + ], + "label": "Humor in the Court2", + "fontSize": 30, + "fontFamily": "DEFAULT", + "language": "", + "color": "orange", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 303, + "labelHeight": 43, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "#0D32B2", + "secondaryAccentColor": "#4A6FF3", + "neutralAccentColor": "#676C7E" + } + ], + "connections": [] +} diff --git a/e2etests/testdata/stable/sql_table_column_styles/dagre/sketch.exp.svg b/e2etests/testdata/stable/sql_table_column_styles/dagre/sketch.exp.svg new file mode 100644 index 000000000..7e008d4c8 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_column_styles/dagre/sketch.exp.svg @@ -0,0 +1,60 @@ + +Humor in the CourtCould you see him from where you were standing? +I could see his head. +And where was his head? +Just above his shoulders. +Humor in the Court2Could you see him from where you were standing? +I could see his head. +And where was his head? +Just above his shoulders. + + + + \ No newline at end of file diff --git a/e2etests/testdata/stable/sql_table_column_styles/elk/board.exp.json b/e2etests/testdata/stable/sql_table_column_styles/elk/board.exp.json new file mode 100644 index 000000000..633a19d2c --- /dev/null +++ b/e2etests/testdata/stable/sql_table_column_styles/elk/board.exp.json @@ -0,0 +1,207 @@ +{ + "name": "", + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "Humor in the Court", + "type": "sql_table", + "pos": { + "x": 12, + "y": 30 + }, + "width": 678, + "height": 108, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "lightgray", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "Could you see him from where you were standing?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 418, + "labelHeight": 26 + }, + "type": { + "label": "I could see his head.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 166, + "labelHeight": 26 + }, + "constraint": "", + "reference": "" + }, + { + "name": { + "label": "And where was his head?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 208, + "labelHeight": 26 + }, + "type": { + "label": "Just above his shoulders.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 210, + "labelHeight": 26 + }, + "constraint": "", + "reference": "" + } + ], + "label": "Humor in the Court", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "orange", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 202, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "#0D32B2", + "secondaryAccentColor": "#4A6FF3", + "neutralAccentColor": "#676C7E" + }, + { + "id": "Humor in the Court2", + "type": "sql_table", + "pos": { + "x": 710, + "y": 12 + }, + "width": 992, + "height": 144, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "lightgray", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "Could you see him from where you were standing?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 627, + "labelHeight": 38 + }, + "type": { + "label": "I could see his head.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 248, + "labelHeight": 38 + }, + "constraint": "", + "reference": "" + }, + { + "name": { + "label": "And where was his head?", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 312, + "labelHeight": 38 + }, + "type": { + "label": "Just above his shoulders.", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 315, + "labelHeight": 38 + }, + "constraint": "", + "reference": "" + } + ], + "label": "Humor in the Court2", + "fontSize": 30, + "fontFamily": "DEFAULT", + "language": "", + "color": "orange", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 303, + "labelHeight": 43, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "#0D32B2", + "secondaryAccentColor": "#4A6FF3", + "neutralAccentColor": "#676C7E" + } + ], + "connections": [] +} diff --git a/e2etests/testdata/stable/sql_table_column_styles/elk/sketch.exp.svg b/e2etests/testdata/stable/sql_table_column_styles/elk/sketch.exp.svg new file mode 100644 index 000000000..5e82cf15c --- /dev/null +++ b/e2etests/testdata/stable/sql_table_column_styles/elk/sketch.exp.svg @@ -0,0 +1,60 @@ + +Humor in the CourtCould you see him from where you were standing? +I could see his head. +And where was his head? +Just above his shoulders. +Humor in the Court2Could you see him from where you were standing? +I could see his head. +And where was his head? +Just above his shoulders. + + + + \ No newline at end of file