-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-html.rb
executable file
·474 lines (447 loc) · 12 KB
/
make-html.rb
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# charset: UTF-8
OFFSET_X = 700
OFFSET_Y = 300
WIDTH = 1050
HEIGHT = 650
LINE_H = 13
STEP = 12
CIRCLE_R = 4
FILES = ["jp-jr", "jp-west", "jp-east", "tw", "kr", "kp", "cn"]
#----------------------------------------------
def get_length(s)
len = 0
s.each_codepoint do |c|
if c < 0x3000
len += 7
else
len += 12
end
end
return len
end
# 駅名
def draw_stations(out, stn, stn_link)
# 乗り換え
for a in stn_link
out.print '<path class="stn-link" d="M', a[0], ',', a[1], ' L', a[2], ',', a[3], '"/>', "\n"
end
for a in stn
base = 5
name = a[3]
name = name.gsub('_', ' ')
case a[0]
when 'box'
width = get_length(name) + 4
out.print '<rect class="stn" x="', a[1] - width / 2, '" y="', a[2] - LINE_H / 2 - 1, '" width="', width, '" height="', LINE_H + 2, '"/>', "\n"
out.print '<text class="stn-name" text-anchor="middle" x="', a[1], '" y="', a[2] + base, '" title="', (a[1] / STEP) - OFFSET_X, ',', (a[2] / STEP) - OFFSET_Y, '">', name, '</text>', "\n"
when 'lbox'
width = a[4] * 6 + 4
height = a[5] * LINE_H + 2
out.print '<rect class="stn" x="', a[1] - width / 2, '" y="', a[2] - height / 2 - 1, '" width="', width, '" height="', height, '"/>', "\n"
out.print '<text class="stn-name" text-anchor="middle" x="', a[1], '" y="', a[2] + base, '" title="', (a[1] / STEP) - OFFSET_X, ',', (a[2] / STEP) - OFFSET_Y, '">', name, '</text>', "\n"
when 'uc'
text_x = a[1]
text_y = a[2] + base
out.print '<circle class="stn" cx="', a[1], '" cy="', a[2], '" r="', CIRCLE_R, '"/>', "\n"
out.print '<g transform="rotate(-45,', text_x, ',', text_y, ')">'
out.print '<text class="stn-name" text-anchor="start" x="', text_x + CIRCLE_R * 3, '" y="', text_y, '">', name, '</text></g>', "\n"
when 'dc'
text_x = a[1]
text_y = a[2] + base
out.print '<circle class="stn" cx="', a[1], '" cy="', a[2], '" r="', CIRCLE_R, '"/>', "\n"
out.print '<g transform="rotate(45,', text_x, ',', text_y, ')">'
out.print '<text class="stn-name" text-anchor="start" x="', text_x + CIRCLE_R, '" y="', text_y, '">', name, '</text></g>', "\n"
when 'ur'
text_x = a[1]
text_y = a[2] + base
out.print '<circle class="stn" cx="', a[1], '" cy="', a[2], '" r="', CIRCLE_R, '"/>', "\n"
out.print '<g transform="rotate(45,', text_x, ',', text_y, ')">'
out.print '<text class="stn-name" text-anchor="end" x="', text_x - CIRCLE_R * 3, '" y="', text_y, '">', name, '</text></g>', "\n"
when 'dr'
text_x = a[1]
text_y = a[2] + base
out.print '<circle class="stn" cx="', a[1], '" cy="', a[2], '" r="', CIRCLE_R, '"/>', "\n"
out.print '<g transform="rotate(-45,', text_x, ',', text_y, ')">'
out.print '<text class="stn-name" text-anchor="end" x="', text_x - CIRCLE_R, '" y="', text_y, '">', name, '</text></g>', "\n"
end
end
end
# --------------------------------------------------------------------------------------
x = 0
y = 0
prev_dst = nil
stn = []
stn_link_name = {}
stn_link = []
# --------------------------------------------------------------------------------------
open("dst/style.css", "w") do |out|
out.print <<EOS
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
body {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
svg {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: 'Noto Sans CJK';
}
.land {
fill: #ddf;
stroke-width: 2px;
stroke: #88a
}
.sea {
fill: white;
}
.rail-l {
stroke-width: 4px;
fill: none;
}
.rail {
stroke-width: 8px;
fill: none;
}
.rail-h {
stroke-width: 12px;
fill: none;
}
.stn {
fill: white;
}
.stn-name {
font-size: 12px;
fill: black;
cursor: default;
}
.stn-name-p {
font-size: 12px;
fill: gray;
}
.stn-link {
stroke-width: 8px;
stroke: white;
}
EOS
# 路線ごとに色分け
open("src/line-color.txt", "r") do |f|
f.each_line do |line|
line.chomp!
next if line.empty? || line.match(/^#/)
a = line.split(" ")
if a.size >= 3 && a[2] == "*"
out.print ".", a[0], " {\n stroke: ", a[1], "; stroke-dasharray: 2, 2;\n}\n"
elsif a.size >= 2
out.print ".", a[0], " {\n stroke: ", a[1], ";\n}\n"
end
end
out.print ".others {\n stroke: #808080;\n}\n"
end
end
# --------------------------------------------------------------------------------------
open("dst/map.html", "w") do |out|
out.print <<EOS
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>rail-world</title>
<style type="text/css"> @import "style.css"; </style>
<script src="map.js"></script>
</head>
<body>
EOS
out.print '<svg width="', WIDTH * STEP, '" height="', HEIGHT * STEP, '">', "\n"
out.print <<EOS
<g id="main">
EOS
# --------------------------------------------------------------------------------------
# 海
out.print '<g id="layer-sea">', "\n"
out.print '<rect x="0" y="0" width="', WIDTH * STEP, '" height="', HEIGHT * STEP, '" class="sea"/>', "\n"
out.print '</g>', "\n"
# --------------------------------------------------------------------------------------
# 陸地
first = false
output = false
out.print '<g id="layer-land">', "\n"
open("src/land.txt", "r") do |f|
f.each_line do |line|
line.chomp!
next if line.empty? || line.match(/^#/)
a = line.split(" ")
case a[0]
when 'begin'
out.print '<path class="land" d="'
first = true
when 'end'
out.print ' Z" />', "\n"
when 'm'
dx = a[1]
if dx.match?(/^-?\d+$/) || dx == '<' || dx == '>'
if dx == '<'
x = 0
elsif dx == '>'
x = WIDTH * STEP
else
x = (dx.to_i + OFFSET_X) * STEP
end
dy = a[2]
if dy == '^'
y = 0
elsif dy == '_'
y = HEIGHT * STEP
else
y = (dy.to_i + OFFSET_Y) * STEP
end
else
if stn_link_name.has_key?(dx)
b = stn_link_name[dx]
x = b[0]
y = b[1]
if a.size >= 4
x += a[2].to_i * STEP
y += a[3].to_i * STEP
end
else
raise "Station name #{a[1]} not found"
end
end
output = true;
when 'pos'
stn_link_name[a[1]] = [x, y]
#print a[1], "=", (x/STEP)-OFFSET_X, ",", (y/STEP)-OFFSET_Y, "\n"
when 'n'
if a[1] == '*'
y = 0
else
y -= a[1].to_i * STEP
end
output = true;
when 's'
if a[1] == '*'
y = HEIGHT * STEP
else
y += a[1].to_i * STEP
end
output = true;
when 'w'
if a[1] == '*'
x = 0
else
x -= a[1].to_i * STEP
end
output = true;
when 'e'
if a[1] == '*'
x = WIDTH * STEP
else
x += a[1].to_i * STEP
end
output = true;
when 'ne'
x += a[1].to_i * STEP
y -= a[1].to_i * STEP
output = true;
when 'nw'
x -= a[1].to_i * STEP
y -= a[1].to_i * STEP
output = true;
when 'se'
x += a[1].to_i * STEP
y += a[1].to_i * STEP
output = true;
when 'sw'
x -= a[1].to_i * STEP
y += a[1].to_i * STEP
output = true;
else
raise "Unknown command: '#{a[0]}'"
end
if output
if first
out.print 'M', x, ',', y
first = false
else
out.print ' L', x, ',', y
end
output = false
end
end
end
out.print '</g>', "\n"
# --------------------------------------------------------------------------------------
# 名前と位置の対応
for file in FILES do
open("lines/" + file + ".txt", "r") do |f|
linenum = 0
f.each_line do |line|
linenum += 1
line.chomp!
next if line.empty? || line.match(/^#/)
a = line.split(" ")
case a[0]
when 'm'
if a[1].match?(/^-?\d+$/)
x = (a[1].to_i + OFFSET_X) * STEP
y = (a[2].to_i + OFFSET_Y) * STEP
else
if stn_link_name.has_key?(a[1])
b = stn_link_name[a[1]]
x = b[0]
y = b[1]
if a.size >= 4
x += a[2].to_i * STEP
y += a[3].to_i * STEP
end
else
raise "Station name #{a[1]} not found"
end
end
when 'box', 'lbox', 'uc', 'dc', 'ur', 'dr'
if a.size >= 3
if stn_link_name.has_key?(a[2])
b = stn_link_name[a[2]]
stn_link.push [x, y, b[0], b[1]]
else
stn_link_name[a[2]] = [x, y]
end
end
when 'n', 'nw', 'ne', 's', 'sw', 'se', 'w', 'e'
n = a[1].to_i * STEP
case a[0]
when 'n'
y -= n
when 'nw'
x -= n
y -= n
when 'ne'
x += n
y -= n
when 's'
y += n
when 'sw'
x -= n
y += n
when 'se'
x += n
y += n
when 'w'
x -= n
when 'e'
x += n
end
end
end
end
end
# --------------------------------------------------------------------------------------
# 路線
out.print '<g id="layer-line">', "\n"
for file in FILES do
open("lines/" + file + ".txt", "r") do |f|
path_class = []
linenum = 0
f.each_line do |line|
linenum += 1
line.chomp!
next if line.empty? || line.match(/^#/)
a = line.split(" ")
case a[0]
when 'begin'
a.shift
path_class = a
out.print '<path class="', path_class.join(' '), '" d="'
when 'end'
out.print "L", x, ",", y, '"/>', "\n"
prev_dst = nil
when 'm'
if a[1].match?(/^-?\d+$/)
x = (a[1].to_i + OFFSET_X) * STEP
y = (a[2].to_i + OFFSET_Y) * STEP
else
if stn_link_name.has_key?(a[1])
b = stn_link_name[a[1]]
x = b[0]
y = b[1]
if a.size >= 4
x += a[2].to_i * STEP
y += a[3].to_i * STEP
end
else
raise "Station name #{a[1]} not found"
end
end
out.print "M", x, ",", y, " "
when 'box', 'lbox', 'uc', 'dc', 'ur', 'dr'
if a.size >= 5
stn.push [a[0], x, y, a[1], a[3].to_i, a[4].to_i]
else
stn.push [a[0], x, y, a[1]]
end
when 'add'
a.shift
out.print "L", x, ",", y, '"/>', "\n"
prev_dst = nil
path_class += a
out.print '<path class="', path_class.join(' '), '" d="M', x, ",", y, " "
when 'del'
a.shift
out.print "L", x, ",", y, '"/>', "\n"
prev_dst = nil
path_class -= a
out.print '<path class="', path_class.join, '" d="M', x, ",", y, " "
when 'n', 'nw', 'ne', 's', 'sw', 'se', 'w', 'e'
if !prev_dst || prev_dst != a[0]
out.print "L", x, ",", y, " "
prev_dst = a[0]
end
n = a[1].to_i * STEP
case a[0]
when 'n'
y -= n
when 'nw'
x -= n
y -= n
when 'ne'
x += n
y -= n
when 's'
y += n
when 'sw'
x -= n
y += n
when 'se'
x += n
y += n
when 'w'
x -= n
when 'e'
x += n
end
else
raise "Unknown command '#{a[0]}' at #{file}(#{linenum})"
end
end
end
end
out.print '</g>', "\n"
# 駅
out.print '<g id="layer-station">', "\n"
draw_stations out, stn, stn_link
out.print '</g>', "\n"
out.print <<EOS
</g>
</svg>
</body>
</html>
EOS
end