| 1 | package gitgraph |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "html" |
| 6 | "math" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // SVG tekent lanelijnen en commitknopen (avatars) in één coördinatenstelsel. |
| 11 | // Geen style-attributen: CSP style-src is 'self' zonder unsafe-inline. |
| 12 | // 1 CSS-pixel = 1 tekenunit (geen viewBox), zodat knopen op de lane-x blijven. |
| 13 | func (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" width="%d" height="%d" aria-hidden="true">`, 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 | writeSVGNodes(&b, g.Rows, g.CommitBase) |
| 36 | b.WriteString(`</svg>`) |
| 37 | return b.String() |
| 38 | } |
| 39 | |
| 40 | func writeSVGNodes(b *strings.Builder, rows []Row, commitBase string) { |
| 41 | var clips strings.Builder |
| 42 | for i, r := range rows { |
| 43 | if r.Avatar == "" { |
| 44 | continue |
| 45 | } |
| 46 | x, y := laneX(r.Lane), rowY(i) |
| 47 | fmt.Fprintf(&clips, `<clipPath id="cg-a-%d"><circle cx="%.1f" cy="%.1f" r="%.0f"/></clipPath>`, i, x, y, DotR) |
| 48 | } |
| 49 | if clips.Len() > 0 { |
| 50 | b.WriteString(`<defs>`) |
| 51 | b.WriteString(clips.String()) |
| 52 | b.WriteString(`</defs>`) |
| 53 | } |
| 54 | for i, r := range rows { |
| 55 | x, y := laneX(r.Lane), rowY(i) |
| 56 | b.WriteString(`<a class="commitgraph-node`) |
| 57 | if r.Class != "" { |
| 58 | fmt.Fprintf(b, ` %s`, html.EscapeString(r.Class)) |
| 59 | } |
| 60 | b.WriteByte('"') |
| 61 | if commitBase != "" && r.SHA != "" { |
| 62 | fmt.Fprintf(b, ` href="%s"`, html.EscapeString(commitBase+r.SHA)) |
| 63 | } |
| 64 | if r.Branch != "" { |
| 65 | fmt.Fprintf(b, ` data-branch="%s"`, html.EscapeString(r.Branch)) |
| 66 | } |
| 67 | if tip := r.Tip; tip != "" { |
| 68 | fmt.Fprintf(b, ` aria-label="%s"`, html.EscapeString(tip)) |
| 69 | } |
| 70 | b.WriteByte('>') |
| 71 | if r.Tip != "" { |
| 72 | fmt.Fprintf(b, `<title>%s</title>`, html.EscapeString(r.Tip)) |
| 73 | } |
| 74 | col := safeColor(r.Color) |
| 75 | cls := ColorClass(r.Color) |
| 76 | fmt.Fprintf(b, `<circle cx="%.1f" cy="%.1f" r="%.0f" fill="%s"`, x, y, DotR, col) |
| 77 | if cls != "" { |
| 78 | fmt.Fprintf(b, ` class="%s"`, cls) |
| 79 | } |
| 80 | b.WriteString(`/>`) |
| 81 | if r.Avatar != "" { |
| 82 | fmt.Fprintf(b, `<image href="%s" x="%.1f" y="%.1f" width="%.0f" height="%.0f" clip-path="url(#cg-a-%d)" preserveAspectRatio="xMidYMid slice"/>`, |
| 83 | html.EscapeString(r.Avatar), x-DotR, y-DotR, DotR*2, DotR*2, i) |
| 84 | } else if init := nodeInitial(r.Initial); init != "" { |
| 85 | fmt.Fprintf(b, `<text x="%.1f" y="%.1f" text-anchor="middle" dominant-baseline="central" fill="#fff" font-size="9" font-weight="700">%s</text>`, x, y, init) |
| 86 | } |
| 87 | b.WriteString(`</a>`) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func nodeInitial(s string) string { |
| 92 | s = strings.TrimSpace(s) |
| 93 | if s == "" { |
| 94 | return "" |
| 95 | } |
| 96 | for _, r := range s { |
| 97 | return html.EscapeString(string(r)) |
| 98 | } |
| 99 | return "" |
| 100 | } |
| 101 | |
| 102 | // orthogonalPath gaat verticaal, dan horizontaal, dan weer verticaal, |
| 103 | // met afgeronde 90-graden bochten. Bij dezelfde lane is het een rechte lijn. |
| 104 | func orthogonalPath(x1, y1, x2, y2, r float64) string { |
| 105 | if almostEq(x1, x2) { |
| 106 | return fmt.Sprintf("M %.1f %.1f V %.1f", x1, y1, y2) |
| 107 | } |
| 108 | if almostEq(y1, y2) { |
| 109 | return fmt.Sprintf("M %.1f %.1f H %.1f", x1, y1, x2) |
| 110 | } |
| 111 | // Vroege overstap: bocht halverwege de volgende rij, daarna verticaal in de doellane. |
| 112 | turnY := y1 + float64(RowH)/2 |
| 113 | if (y2-y1) > 0 && turnY > y2-1 { |
| 114 | turnY = (y1 + y2) / 2 |
| 115 | } |
| 116 | dx := math.Abs(x2 - x1) |
| 117 | up := math.Abs(turnY - y1) |
| 118 | down := math.Abs(y2 - turnY) |
| 119 | maxR := math.Min(r, dx/2) |
| 120 | maxR = math.Min(maxR, up) |
| 121 | maxR = math.Min(maxR, down) |
| 122 | if maxR < 1 { |
| 123 | // te krap voor een boog: alsnog een knik, zonder afronding |
| 124 | return fmt.Sprintf("M %.1f %.1f V %.1f H %.1f V %.1f", x1, y1, turnY, x2, y2) |
| 125 | } |
| 126 | dir := 1.0 |
| 127 | if x2 < x1 { |
| 128 | dir = -1 |
| 129 | } |
| 130 | return fmt.Sprintf( |
| 131 | "M %.1f %.1f V %.1f Q %.1f %.1f, %.1f %.1f H %.1f Q %.1f %.1f, %.1f %.1f V %.1f", |
| 132 | x1, y1, |
| 133 | turnY-maxR, |
| 134 | x1, turnY, x1+dir*maxR, turnY, |
| 135 | x2-dir*maxR, |
| 136 | x2, turnY, x2, turnY+maxR, |
| 137 | y2, |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | func almostEq(a, b float64) bool { |
| 142 | return math.Abs(a-b) < 0.05 |
| 143 | } |
| 144 | |
| 145 | func laneX(lane int) float64 { |
| 146 | return float64(lane)*float64(LaneW) + float64(LaneW)/2 |
| 147 | } |
| 148 | |
| 149 | func rowY(row int) float64 { |
| 150 | return float64(row)*float64(RowH) + float64(RowH)/2 |
| 151 | } |
| 152 | |
| 153 | func safeColor(c string) string { |
| 154 | c = strings.TrimSpace(c) |
| 155 | if c == "var(--blue)" || c == "var(--muted)" { |
| 156 | return html.EscapeString(c) |
| 157 | } |
| 158 | if strings.HasPrefix(c, "var(--graph-") && strings.HasSuffix(c, ")") { |
| 159 | mid := c[len("var(--graph-") : len(c)-1] |
| 160 | if len(mid) == 1 && mid[0] >= '0' && mid[0] < '0'+byte(PaletteSize) { |
| 161 | return html.EscapeString(c) |
| 162 | } |
| 163 | } |
| 164 | return "var(--muted)" |
| 165 | } |