Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Consider arrowhead for label position #1192

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Export diagrams to `.pptx` (PowerPoint)[#1139](https://github.com/terrastruct/d2/pull/1139)
- Customize gap size in grid diagrams with `grid-gap`, `vertical-gap`, or `horizontal-gap` [#1178](https://github.com/terrastruct/d2/issues/1178)
- Exclude arrowheads when position connection label [#793](https://github.com/terrastruct/d2/pull/1192)

#### Improvements 🧹

Expand Down
4 changes: 2 additions & 2 deletions d2renderers/d2sketch/testdata/connection_label/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions d2renderers/d2sketch/testdata/dots-real/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions d2renderers/d2sketch/testdata/opacity/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions d2renderers/d2sketch/testdata/opacity_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions d2renderers/d2sketch/testdata/paper-real/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions d2renderers/d2sketch/testdata/terminal/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions d2renderers/d2sketch/testdata/twitter/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions d2renderers/d2sketch/testdata/twitter_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,18 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co
markerEnd = fmt.Sprintf(`marker-end="url(#%s)" `, id)
}

srcAdj, dstAdj := getArrowheadAdjustments(connection, idToShape)
path := pathData(connection, srcAdj, dstAdj)
mask := fmt.Sprintf(`mask="url(#%s)"`, labelMaskID)

var labelTL *geo.Point
if connection.Label != "" {
labelTL = connection.GetLabelTopLeft()
srcArrowWidth, _ := arrowheadDimensions(connection.SrcArrow, float64(connection.StrokeWidth))
dstArrowWidth, _ := arrowheadDimensions(connection.DstArrow, float64(connection.StrokeWidth))

srcAdjDistance := geo.EuclideanDistance(srcAdj.X, srcAdj.Y, 0, 0) + srcArrowWidth
dstAdjDistance := geo.EuclideanDistance(dstAdj.X, dstAdj.Y, 0, 0) + dstArrowWidth
labelTL = connection.GetLabelTopLeft(srcAdjDistance, dstAdjDistance)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of updating GetLabelTopLeft, and GetPointOnRoute, I think it might be better to temporarily move the route start and end points to begin after the arrowheads (e.g. connection.Route[0].X += srcAdj.X) and then revert this after getting the label position on the route between arrowheads. This avoids the extra complexity in the GetPointOnRoute funcs.

Copy link
Contributor Author

@ShupingHe ShupingHe Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to temporarily construct the route, we can't just add a start and end point to original route, but also we need to remove the extra points before and after, can this be done by calcing the distance along the route? what if the connection is a curve?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant just you could modify the first and last points in the route to start after the arrowheads. Similar to how we adjust them here to account for the arrowhead+shape stroke widths:

path = append(path, fmt.Sprintf("M %f %f",
route[0].X+srcAdj.X,
route[0].Y+srcAdj.Y,
))

after computing the label position on the adjusted route, apply the -adjustments to get the original back going forwards

you can compute the adjustment similar to this:

v := geo.NewVector(end.X-start.X, end.Y-start.Y)
return v.Unit().Multiply(-distance).ToPoint()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if there are multiple points between original point and new point? if so there would still be issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true but there shouldn't be multiple points within the length of the arrowhead. It will be more robust to account for that though in case it does come up.

labelTL.X = math.Round(labelTL.X)
labelTL.Y = math.Round(labelTL.Y)

Expand All @@ -560,9 +569,6 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co
}
}

srcAdj, dstAdj := getArrowheadAdjustments(connection, idToShape)
path := pathData(connection, srcAdj, dstAdj)
mask := fmt.Sprintf(`mask="url(#%s)"`, labelMaskID)
if sketchRunner != nil {
out, err := d2sketch.Connection(sketchRunner, connection, path, mask)
if err != nil {
Expand Down Expand Up @@ -644,7 +650,7 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co
}

func renderArrowheadLabel(connection d2target.Connection, text string, position, width, height float64) string {
labelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, width, height)
labelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, width, height, 0, 0)

textEl := d2themes.NewThemableElement("text")
textEl.X = labelTL.X + width/2
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading