BlueGit

timfa bluegit

Files

bluegit / internal / gitgraph / svg.go

svg.go

svg.go 2.8 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 b.WriteString(fmt.Sprintf(`<svg class="commitgraph-svg" viewBox="0 0 %d %d" width="%d" height="%d" aria-hidden="true">`, w, h, w, h))
20 for _, e := range g.Edges {
21 col := safeColor(e.Color)
22 cls := ColorClass(e.Color)
23 x1, y1 := laneX(e.FromLane), rowY(e.FromRow)
24 x2, y2 := laneX(e.ToLane), rowY(e.ToRow)
25 d := orthogonalPath(x1, y1, x2, y2, Corner)
26 fmt.Fprintf(&b, `<path d="%s" fill="none" stroke="%s" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"`, d, col)
27 if cls != "" {
28 fmt.Fprintf(&b, ` class="%s"`, cls)
29 }
30 if e.Branch != "" {
31 fmt.Fprintf(&b, ` data-branch="%s"`, html.EscapeString(e.Branch))
32 }
33 b.WriteString(`/>`)
34 }
35 b.WriteString(`</svg>`)
36 return b.String()
37}
38
39// orthogonalPath gaat verticaal, dan horizontaal, dan weer verticaal,
40// met afgeronde 90-graden bochten. Bij dezelfde lane is het een rechte lijn.
41func orthogonalPath(x1, y1, x2, y2, r float64) string {
42 if almostEq(x1, x2) {
43 return fmt.Sprintf("M %.1f %.1f V %.1f", x1, y1, y2)
44 }
45 if almostEq(y1, y2) {
46 return fmt.Sprintf("M %.1f %.1f H %.1f", x1, y1, x2)
47 }
48 // Vroege overstap: bocht halverwege de volgende rij, daarna verticaal in de doellane.
49 turnY := y1 + float64(RowH)/2
50 if (y2-y1) > 0 && turnY > y2-1 {
51 turnY = (y1 + y2) / 2
52 }
53 dx := math.Abs(x2 - x1)
54 up := math.Abs(turnY - y1)
55 down := math.Abs(y2 - turnY)
56 maxR := math.Min(r, dx/2)
57 maxR = math.Min(maxR, up)
58 maxR = math.Min(maxR, down)
59 if maxR < 1 {
60 // te krap voor een boog: alsnog een knik, zonder afronding
61 return fmt.Sprintf("M %.1f %.1f V %.1f H %.1f V %.1f", x1, y1, turnY, x2, y2)
62 }
63 dir := 1.0
64 if x2 < x1 {
65 dir = -1
66 }
67 return fmt.Sprintf(
68 "M %.1f %.1f V %.1f Q %.1f %.1f, %.1f %.1f H %.1f Q %.1f %.1f, %.1f %.1f V %.1f",
69 x1, y1,
70 turnY-maxR,
71 x1, turnY, x1+dir*maxR, turnY,
72 x2-dir*maxR,
73 x2, turnY, x2, turnY+maxR,
74 y2,
75 )
76}
77
78func almostEq(a, b float64) bool {
79 return math.Abs(a-b) < 0.05
80}
81
82func laneX(lane int) float64 {
83 return float64(lane)*float64(LaneW) + float64(LaneW)/2
84}
85
86func rowY(row int) float64 {
87 return float64(row)*float64(RowH) + float64(RowH)/2
88}
89
90func safeColor(c string) string {
91 c = strings.TrimSpace(c)
92 if c == "var(--blue)" || c == "var(--muted)" {
93 return html.EscapeString(c)
94 }
95 if strings.HasPrefix(c, "var(--graph-") && strings.HasSuffix(c, ")") {
96 mid := c[len("var(--graph-") : len(c)-1]
97 if len(mid) == 1 && mid[0] >= '0' && mid[0] < '0'+byte(PaletteSize) {
98 return html.EscapeString(c)
99 }
100 }
101 return "var(--muted)"
102}