BlueGit

timfa bluegit

Files

bluegit / internal / gitgraph / svg.go

svg.go

svg.go 2.9 KiB Download Raw
1package gitgraph
2
3import (
4 "fmt"
5 "html"
6 "math"
7 "strings"
8)
9
10// SVG tekent de lanelijnen met afgeronde 90-graden bochten.
11// Alleen getallen en paletkleuren, veilig als HTML. De knopen zelf
12// zijn de avatars in de HTML, niet de SVG.
13func (g *Graph) SVG() string {
14 if g == nil || len(g.Rows) == 0 || g.Lanes < 1 {
15 return ""
16 }
17 w, h := g.Width, g.Height
18 var b strings.Builder
19 // Geen viewBox: 1 CSS-pixel = 1 tekenunit, zodat avatars en lijnen
20 // dezelfde x gebruiken en de grafiek niet geschaald wordt.
21 b.WriteString(fmt.Sprintf(`<svg class="commitgraph-svg" width="%d" height="%d" style="width:%dpx;height:%dpx;left:%dpx" aria-hidden="true">`, w, h, w, h, RefW))
22 for _, e := range g.Edges {
23 col := safeColor(e.Color)
24 cls := ColorClass(e.Color)
25 x1, y1 := laneX(e.FromLane), rowY(e.FromRow)
26 x2, y2 := laneX(e.ToLane), rowY(e.ToRow)
27 d := orthogonalPath(x1, y1, x2, y2, Corner)
28 fmt.Fprintf(&b, `<path d="%s" fill="none" stroke="%s" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"`, d, col)
29 if cls != "" {
30 fmt.Fprintf(&b, ` class="%s"`, cls)
31 }
32 if e.Branch != "" {
33 fmt.Fprintf(&b, ` data-branch="%s"`, html.EscapeString(e.Branch))
34 }
35 b.WriteString(`/>`)
36 }
37 b.WriteString(`</svg>`)
38 return b.String()
39}
40
41// orthogonalPath gaat verticaal, dan horizontaal, dan weer verticaal,
42// met afgeronde 90-graden bochten. Bij dezelfde lane is het een rechte lijn.
43func orthogonalPath(x1, y1, x2, y2, r float64) string {
44 if almostEq(x1, x2) {
45 return fmt.Sprintf("M %.1f %.1f V %.1f", x1, y1, y2)
46 }
47 if almostEq(y1, y2) {
48 return fmt.Sprintf("M %.1f %.1f H %.1f", x1, y1, x2)
49 }
50 // Vroege overstap: bocht halverwege de volgende rij, daarna verticaal in de doellane.
51 turnY := y1 + float64(RowH)/2
52 if (y2-y1) > 0 && turnY > y2-1 {
53 turnY = (y1 + y2) / 2
54 }
55 dx := math.Abs(x2 - x1)
56 up := math.Abs(turnY - y1)
57 down := math.Abs(y2 - turnY)
58 maxR := math.Min(r, dx/2)
59 maxR = math.Min(maxR, up)
60 maxR = math.Min(maxR, down)
61 if maxR < 1 {
62 // te krap voor een boog: alsnog een knik, zonder afronding
63 return fmt.Sprintf("M %.1f %.1f V %.1f H %.1f V %.1f", x1, y1, turnY, x2, y2)
64 }
65 dir := 1.0
66 if x2 < x1 {
67 dir = -1
68 }
69 return fmt.Sprintf(
70 "M %.1f %.1f V %.1f Q %.1f %.1f, %.1f %.1f H %.1f Q %.1f %.1f, %.1f %.1f V %.1f",
71 x1, y1,
72 turnY-maxR,
73 x1, turnY, x1+dir*maxR, turnY,
74 x2-dir*maxR,
75 x2, turnY, x2, turnY+maxR,
76 y2,
77 )
78}
79
80func almostEq(a, b float64) bool {
81 return math.Abs(a-b) < 0.05
82}
83
84func laneX(lane int) float64 {
85 return float64(lane)*float64(LaneW) + float64(LaneW)/2
86}
87
88func rowY(row int) float64 {
89 return float64(row)*float64(RowH) + float64(RowH)/2
90}
91
92func safeColor(c string) string {
93 c = strings.TrimSpace(c)
94 if c == "var(--blue)" || c == "var(--muted)" {
95 return html.EscapeString(c)
96 }
97 if strings.HasPrefix(c, "var(--graph-") && strings.HasSuffix(c, ")") {
98 mid := c[len("var(--graph-") : len(c)-1]
99 if len(mid) == 1 && mid[0] >= '0' && mid[0] < '0'+byte(PaletteSize) {
100 return html.EscapeString(c)
101 }
102 }
103 return "var(--muted)"
104}