-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpitch.html
171 lines (167 loc) · 5.54 KB
/
pitch.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SVG pitch | Japanese pitch accent annotations in simple SVG.</title>
<meta name="description" content="Japanese pitch accent annotations in simple SVG." />
<meta property="og:type" content="WebSite" />
<meta property="og:title" content="SVG pitch" />
<meta property="og:url" content="https://illdepence.github.io/SVG_pitch/" />
<meta property="og:image" content="https://illdepence.github.io/SVG_pitch/preview.gif" />
<meta property="og:video" content="https://illdepence.github.io/SVG_pitch/preview.mp4" />
<meta property="og:description" content="Japanese pitch accent annotations in simple SVG." />
<script type="application/ld+json">
{"publisher":{"@type":"Person","name":"Tarek Saier"},"@type":"WebSite","url":"https://illdepence.github.io/SVG_pitch/","headline":"SVG pitch | Japanese pitch accent annotations in simple SVG.","name":"SVG pitch","description":"Japanese pitch accent annotations in simple SVG.","@context":"http://schema.org"}</script>
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP:300,900&subset=japanese" rel="stylesheet">
<style>
body, text {
font-family: 'Noto Sans JP', sans-serif;
font-weight: 300;
}
h3 {
font-family: 'Noto Sans JP', sans-serif;
font-weight: 900;
}
table, td {
border-collapse:collapse;
}
a {
color: #007f9a;
text-decoration: none;
}
a:hover {
color: #006378;
}
a:focus {
outline: none;
}
* {-webkit-font-smoothing: antialiased;}
</style>
</head>
<body>
<div style="width: 700px; margin: 0 auto;">
<h3>SVG pitch</h1>
<table style="width: 393px; margin-bottom: 10px;">
<tr>
<td>text</td>
<td>pitch (012 / lh)</td>
<tr>
<tr>
<td><input id="kana" oninput="draw()"></td>
<td><input id="pitch" oninput="draw()"></td>
<tr>
</table>
<div id="container" style="height: 84px;"></div>
<p>copy SVG: <input id="copypasta"></p>
<br>
<p><small>code on <a href="https://github.com/IllDepence/SVG_pitch">GitHub</a></small></p>
</div>
<script>
function circle(x, y, o) {
r = '<circle r="5" cx="'+x+'" cy="'+y+'" style="opacity:1;fill:#000;" />';
if(o) {
r += '<circle r="3.25" cx="'+x+'" cy="'+y+'" style="opacity:1;fill:#fff;" />';
}
return r
}
function text(x, mora) {
if(mora.length == 1) {
return '<text x="'+x+'" y="67.5" style="font-size:20px;fill:#000;">'+mora+'</text>';
}
else {
ret = '<text x="'+(x-5)+'" y="67.5" style="font-size:20px;fill:#000;">'+mora[0]+'</text>'
ret += '<text x="'+(x+12)+'" y="67.5" style="font-size:14px;fill:#000;">'+mora[1]+'</text>';
return ret
}
}
function path(x, y, typ, step_width) {
if(typ == 's') { // straight
delta = step_width+',0';
}
else if(typ == 'u') { // up
delta = step_width+',-25';
}
else if(typ == 'd') { // down
delta = step_width+',25';
}
return '<path d="m '+x+','+y+' '+delta+'" style="fill:none;stroke:#000;stroke-width:1.5;" />';
}
function draw() {
kana = document.getElementById('kana').value;
mora = to_mora_array(kana)
pitch = document.getElementById('pitch').value;
svg_container = document.getElementById('container');
copy_container = document.getElementById('copypasta');
positions = Math.max(mora.length, pitch.length);
step_width = 35
margin_lr = 16
svg_width = Math.max(0, ((positions-1) * step_width) + (margin_lr*2));
svg_str = '<svg width="'+svg_width+'px" height="75px" viewBox="0 0 '+svg_width+' 75">';
chars = '';
for(i=0; i<mora.length; i++) {
x_center = margin_lr + (i * step_width);
chars += text(x_center-11, mora[i]);
}
circles = '';
paths = '';
for(i=0; i<pitch.length; i++) {
x_center = margin_lr + (i * step_width);
if(['H', 'h', '1', '2'].indexOf(pitch[i]) >= 0) {
y_center = 5;
good_input();
}
else if(['L', 'l', '0'].indexOf(pitch[i]) >= 0) {
y_center = 30;
good_input();
}
else {
bad_input();
return false;
}
circles += circle(x_center, y_center, i>=mora.length);
if(i>0) {
if(prev_center[1] == y_center) { path_typ = 's'; }
if(prev_center[1] < y_center) { path_typ = 'd'; }
if(prev_center[1] > y_center) { path_typ = 'u'; }
paths += path(prev_center[0], prev_center[1], path_typ, step_width);
}
prev_center = [x_center, y_center];
}
svg_str += chars+paths+circles+'</svg>';
svg_container.innerHTML = svg_str;
if(kana.length == 0 && pitch.length == 0) {
copy_container.value = '';
}
else {
copy_container.value = svg_str;
}
}
function to_mora_array(hira) {
mora_arr = [];
combiners = ['ゃ','ゅ','ょ','ぁ','ぃ','ぅ','ぇ','ぉ',
'ャ','ュ','ョ','ァ','ィ','ゥ','ェ','ォ']
for(i=0; i<hira.length; i++) {
if(i+1<hira.length && combiners.indexOf(hira[i+1]) > -1) {
// add two
mora_arr.push(hira[i]+hira[i+1]);
i++; // jump
}
else {
// add one
mora_arr.push(hira[i]);
}
}
return mora_arr;
}
function good_input() {
document.getElementById('pitch').style.color = '#000';
}
function bad_input() {
document.getElementById('pitch').style.color = '#e44';
document.getElementById('container').innerHTML = '';
document.getElementById('copypasta').value = '';
}
draw();
</script>
</body>
</html>