-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpitch_auto.html
219 lines (215 loc) · 7.14 KB
/
pitch_auto.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!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>
<script src="accdb.js"></script>
</head>
<body>
<div style="width: 700px; margin: 0 auto;">
<h3>SVG auto pitch</h1>
<table style="width: 393px; margin-bottom: 10px;">
<tr>
<td>text</td>
<td id="reading_txt">reading</td>
<tr>
<tr>
<td><input id="kanji" oninput="newinput()"></td>
<td id="reading_buttons">
<span style="cursor:pointer;" onclick="prev_reading()">◀</span> <!--
--><span style="cursor:pointer;" onclick="next_reading()">▶</span>
</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> | pitch accent data by <a href="https://www.wadoku.de/">Wadoku</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 newinput() {
window.reading_choice = -1;
draw();
}
function draw() {
kanji = document.getElementById('kanji').value;
if(kanji.length < 1) { bad_input(); return; }
acc_tups = acc_dict[kanji];
if(acc_tups == undefined) {
bad_input();
return false;
}
good_input();
window.num_readings = acc_tups.length
if(acc_tups.length > 1) { activate_readings(); }
else { deactivate_readings(); }
if(window.reading_choice && window.reading_choice > -1) {
reading_choice = window.reading_choice;
}
else {
reading_choice = 0;
}
acc_use = acc_tups[reading_choice];
kana = acc_use[0];
mora = to_mora_array(kana)
pitch = acc_use[1];
pitch = pitch.replace(/[lh]/g, '')
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 good_input() {
document.getElementById('kanji').style.color = '#000';
}
function bad_input() {
document.getElementById('kanji').style.color = '#e44';
document.getElementById('container').innerHTML = '';
document.getElementById('copypasta').value = '';
deactivate_readings();
}
function activate_readings() {
document.getElementById('reading_txt').style.visibility = 'visible';
document.getElementById('reading_buttons').style.visibility = 'visible';
}
function deactivate_readings() {
document.getElementById('reading_txt').style.visibility = 'hidden';
document.getElementById('reading_buttons').style.visibility = 'hidden';
}
function shift_reading(delta) {
new_val = (window.reading_choice + delta) % window.num_readings;
if(new_val == -1) { new_val = window.num_readings - 1; }
window.reading_choice = new_val;
draw();
}
function next_reading() {
shift_reading(1);
}
function prev_reading() {
shift_reading(-1);
}
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;
}
draw();
</script>
</body>
</html>