@@ -123,31 +123,33 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph, layout d2graph.L
123
123
if obj .LabelDimensions .Height != 0 {
124
124
labelHeight := float64 (obj .LabelDimensions .Height ) + 2 * label .PADDING
125
125
126
- {
127
- // also check for grid cells with outside top labels
128
- topY := gd .objects [0 ].TopLeft .Y
129
- highestLabel := topY
130
- for _ , o := range gd .objects {
131
- if o .TopLeft .Y > topY {
132
- if gd .rowDirected {
133
- break
134
- } else {
135
- continue
136
- }
126
+ // also check for grid cells with outside top labels
127
+ // the first grid object is at the top (and always exists)
128
+ topY := gd .objects [0 ].TopLeft .Y
129
+ highestLabel := topY
130
+ for _ , o := range gd .objects {
131
+ // we only want to compute label positions for objects at the top of the grid
132
+ if o .TopLeft .Y > topY {
133
+ if gd .rowDirected {
134
+ // if the grid is rowDirected (row1, row2, etc) we can stop after finishing the first row
135
+ break
136
+ } else {
137
+ // otherwise we continue until the next column
138
+ continue
137
139
}
138
- if o . LabelPosition != nil {
139
- labelPosition := label . Position ( * o .LabelPosition )
140
- if labelPosition . IsOutside () {
141
- labelTL := o . GetLabelTopLeft ()
142
- if labelTL . Y < highestLabel {
143
- highestLabel = labelTL .Y
144
- }
140
+ }
141
+ if o .LabelPosition != nil {
142
+ labelPosition := label . Position ( * o . LabelPosition )
143
+ if labelPosition . IsOutside () {
144
+ labelTL := o . GetLabelTopLeft ()
145
+ if labelTL .Y < highestLabel {
146
+ highestLabel = labelTL . Y
145
147
}
146
148
}
147
149
}
148
- if highestLabel < topY {
149
- labelHeight += topY - highestLabel + 2 * label . PADDING
150
- }
150
+ }
151
+ if highestLabel < topY {
152
+ labelHeight += topY - highestLabel + 2 * label . PADDING
151
153
}
152
154
153
155
if labelHeight > float64 (verticalPadding ) {
0 commit comments