improve margin adjustments for outside labels
This commit is contained in:
parent
c647c6fb2e
commit
514bb89472
2 changed files with 39 additions and 12 deletions
|
|
@ -305,6 +305,33 @@ func (obj *Object) GetMargin() geo.Spacing {
|
||||||
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
|
case label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom:
|
||||||
margin.Right = labelWidth
|
margin.Right = labelWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if an outside label is larger than the object add margin accordingly
|
||||||
|
if labelWidth > obj.Width {
|
||||||
|
dx := labelWidth - obj.Width
|
||||||
|
switch position {
|
||||||
|
case label.OutsideTopLeft, label.OutsideBottomLeft:
|
||||||
|
// label fixed at left will overflow on right
|
||||||
|
margin.Right = dx
|
||||||
|
case label.OutsideTopCenter, label.OutsideBottomCenter:
|
||||||
|
margin.Left = math.Ceil(dx / 2)
|
||||||
|
margin.Right = math.Ceil(dx / 2)
|
||||||
|
case label.OutsideTopRight, label.OutsideBottomRight:
|
||||||
|
margin.Left = dx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if labelHeight > obj.Height {
|
||||||
|
dy := labelHeight - obj.Height
|
||||||
|
switch position {
|
||||||
|
case label.OutsideLeftTop, label.OutsideRightTop:
|
||||||
|
margin.Bottom = dy
|
||||||
|
case label.OutsideLeftMiddle, label.OutsideRightMiddle:
|
||||||
|
margin.Top = math.Ceil(dy / 2)
|
||||||
|
margin.Bottom = math.Ceil(dy / 2)
|
||||||
|
case label.OutsideLeftBottom, label.OutsideRightBottom:
|
||||||
|
margin.Top = dy
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Icon != nil && obj.IconPosition != nil && obj.Shape.Value != d2target.ShapeImage {
|
if obj.Icon != nil && obj.IconPosition != nil && obj.Shape.Value != d2target.ShapeImage {
|
||||||
|
|
|
||||||
|
|
@ -142,18 +142,6 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
|
||||||
func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*gridDiagram, error) {
|
func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*gridDiagram, error) {
|
||||||
gd := newGridDiagram(obj)
|
gd := newGridDiagram(obj)
|
||||||
|
|
||||||
// to handle objects with outside labels, we adjust their dimensions before layout and
|
|
||||||
// after layout, we remove the label adjustment and reposition TopLeft if needed
|
|
||||||
revertAdjustments := gd.sizeForOutsideLabels()
|
|
||||||
|
|
||||||
if gd.rows != 0 && gd.columns != 0 {
|
|
||||||
gd.layoutEvenly(g, obj)
|
|
||||||
} else {
|
|
||||||
gd.layoutDynamic(g, obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
revertAdjustments()
|
|
||||||
|
|
||||||
// position labels and icons
|
// position labels and icons
|
||||||
for _, o := range gd.objects {
|
for _, o := range gd.objects {
|
||||||
positionedLabel := false
|
positionedLabel := false
|
||||||
|
|
@ -182,6 +170,18 @@ func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*gridDiagram, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// to handle objects with outside labels, we adjust their dimensions before layout and
|
||||||
|
// after layout, we remove the label adjustment and reposition TopLeft if needed
|
||||||
|
revertAdjustments := gd.sizeForOutsideLabels()
|
||||||
|
|
||||||
|
if gd.rows != 0 && gd.columns != 0 {
|
||||||
|
gd.layoutEvenly(g, obj)
|
||||||
|
} else {
|
||||||
|
gd.layoutDynamic(g, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
revertAdjustments()
|
||||||
|
|
||||||
return gd, nil
|
return gd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue