efficient program to spit out SVG for guitar/uke chord frett diagram
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
		printf("<svg width=\"60px\" height=\"75px\" "
			"version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" overflow=\"visible\" "
			"style=\"stroke-width:2; stroke:black;\" "
			"viewBox=\"-5 -15 50 75\" "
			">"
		);
		int fretsCount = strlen(argv[1]);

		//var origin = { x: 0, y: 0};
		//var props = { width: 50, height: 60 };

		// horizontals
		int i;
		for (i = 0; i <= 4; i++) {
			printf("<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />",
				0 - 1, (i * 60/4),
				0+50+1, (i * 60/4)
			);
		}
	
		// verts
		for ( i = 0; i < fretsCount; i++ ) {
			printf("<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />",
				0 + (i * 50 / (fretsCount - 1)), 0,
				0 + (i * 50 / (fretsCount - 1)), 0 + 60
			);
		}
	
		// label
		printf("<text x=\"%d\" y=\"%d\" style=\"stroke-width:0; text-anchor:middle;\">%s</text>",
		    0 + 50/2, 0 - 5, argv[2]
		);
	
		// If any dots are after 4, adjust the root to be the lowest non-zero fret
		// If the root is zero , thicken the top fret
	
		/*var baseFret = 0;
		var adjBaseFret = false;
		var lowestFret = 0;
	
		for (j=0; j < chord.frets.length; j++) {
			if (chord.frets.charAt(j) != 'x' && chord.frets.charAt(j) != 'X' && chord.frets.charAt(j) > 0) {
				if (chord.frets.charAt(j) < lowestFret || lowestFret == 0) {
					lowestFret = chord.frets.charAt(j);
				}
				if (chord.frets.charAt(j) > 4) {
					adjBaseFret = true;
				}
			}
		}
	
		if (adjBaseFret) {
			baseFret = lowestFret - 1;
		}
	
		// dots
	
		var dotsize = 3.5;
	
		var i = 0;
	
		for (j=0; j < chord.frets.length; j++) {
	
			ctx.alignText = "left";
			ctx.fillText(chord.frets.charAt(j), origin.x + (i * props.width / (fretsCount - 1)), origin.y + props.height + 12);
	
			if (chord.frets.charAt(j) == 0) {
	
	
			} else if (chord.frets.charAt(j) == 'x' || chord.frets.charAt(j) == 'X') {
	
			} else {
				ctx.beginPath();
				ctx.arc(origin.x + (i * props.width / (fretsCount -1)), origin.y - 7.5 + (15 * (chord.frets.charAt(j) - baseFret)), dotsize, 0, Math.PI*2,true);
				ctx.closePath();
				ctx.fill();
			}
			i++;
		}
	
		// base fret indicator
	
		if (baseFret > 0) {
			ctx.alignText = "left";
			ctx.fillText(baseFret + 1, origin.x - 10, origin.y + 11);
		} else {
			ctx.alignText = "left";
			ctx.fillText("3", origin.x - 10, origin.y + 41);
		}*/
		printf("</svg>");
}