/
skytext.coffee
212 lines (199 loc) · 7.12 KB
/
skytext.coffee
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
class this.SkyText
piOver180: Math.PI / 180
latFactor: 0.00001
fontHeight: 5
nbsp: String.fromCharCode(160)
constructor: (@lat, @lon, @alt, o = {}) ->
o.lineWidth ?= 1
o.colour ?= 'ffffffff'
@allCoordSets = []
@lineOpts = []
if o.lineWidth > 0
@allCoordSets.push([[[@lon, @lat, 0], [@lon, @lat, @alt]]])
@lineOpts.push(o)
lonRatio = 1 / Math.cos(@lat * @piOver180)
@lonFactor = @latFactor * lonRatio
text: (text, o) -> @line(line, o) for line in text.split("\n")
line: (text, o = {}) ->
o.bearing ?= 0 # text will be readable straight on when viewed facing this way
o.size ?= 2
o.lineWidth ?= 2
o.colour ?= 'ffffffff'
o.lineSpace ?= 1 # vertical space between lines
o.charSpace ?= 1 # horizontal space between chars
o.spaceWidth ?= 2
o.tabSpaces ?= 4 # tabs are this many spaces wide
o.offset ?= 5 # horizontal space at start of line (independent of o.size)
o.font ?= @font
bRad = -o.bearing * @piOver180
sinB = Math.sin(bRad)
cosB = Math.cos(bRad)
latFactor = sinB * o.size * @latFactor
lonFactor = cosB * o.size * @lonFactor
latStart = @lat + sinB * o.offset * @latFactor
lonStart = @lon + cosB * o.offset * @lonFactor
xCursor = 0
tabWidth = o.tabSpaces * o.spaceWidth
lineCoordSets = []
for char in text.split('')
if char in [" ", @nbsp, "\n", "\r"]
xCursor += o.spaceWidth
continue
if char is "\t"
xCursor = Math.ceil((xCursor + 1) / tabWidth) * tabWidth
continue
paths = o.font[char]
unless paths
# console.log('Unsupported character: ', char, char.charCodeAt(0))
paths = o.font['na']
maxX = 0
for path in paths
coords = for x, i in path by 2
y = path[i + 1]
maxX = x if x > maxX
absX = xCursor + x
lat = latStart + absX * latFactor
lon = lonStart + absX * lonFactor
alt = @alt - (y * o.size)
[lon, lat, alt]
lineCoordSets.push(coords)
xCursor += maxX + o.charSpace
@alt -= (o.size * @fontHeight) + o.lineSpace
@lineOpts.push(o)
@allCoordSets.push(lineCoordSets)
@
kmlFragment: ->
k = []
k.push('<Document>')
coordStrs = for lineCoordSets, i in @allCoordSets
o = @lineOpts[i]
k.push "<Style id='l#{i}'><LineStyle><color>#{o.colour}</color><width>#{o.lineWidth}</width></LineStyle></Style>"
for lineCoords in lineCoordSets
coordStr = ((coords.join(',') for coords in lineCoords).join(' '))
k.push "<Placemark>
<styleUrl>#l#{i}</styleUrl>
<LineString><altitudeMode>absolute</altitudeMode><coordinates>#{coordStr}</coordinates></LineString>
</Placemark>"
k.push('</Document>')
k.join('')
kml: ->
"<?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://www.opengis.net/kml/2.2'>#{@kmlFragment()}</kml>"
font:
"na": [[0,2,1,2,1,3,0,3,0,2]] # small square for missing chars
"0": [[0,1,0,3,1,4,2,3,2,1,1,0,0,1],[0,3,2,1]]
"1": [[0,0,0,4]]
"2": [[0,1,1,0,2,1,0,4,2,4]]
"3": [[0,0,2,0,0,2,2,3,0,4]]
"4": [[0,0,0,3,2,3],[1,1,1,4]]
"5": [[2,0,0,0,0,1,2,3,0,4]]
"6": [[0,0,0,3,1,4,2,3,2,2,0,1]]
"7": [[0,0,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]": [[0,0,1,0,1,4,0,4]]
":": [[0,0,0,1],[0,3,0,4]]
";": [[1,0,1,1],[1,3,0,5]]
"?": [[0,0,2,0,2,1,1,2],[1,4,1,3]]
"!": [[0,0,0,2],[0,3,0,4]]
"_": [[0,4,2,4]]
"-": [[0,2,2,2]]
"–": [[0,2,3,2]]
"—": [[0,2,4,2]]
"+": [[0,2,2,2],[1,1,1,3]]
"*": [[0,3,2,1],[0,1,2,3],[1,0,1,4]]
"\\": [[0,0,2,4]]
"=": [[0,1,2,1],[0,3,2,3]]
">": [[0,0,2,2,0,4]]
"<": [[2,0,0,2,2,4]]
"«": [[1,1,0,2,1,3], [2,1,1,2,2,3]]
"»": [[0,1,1,2,0,3], [1,1,2,2,1,3]]
"^": [[0,1,1,0,2,1]]
"$": [[2,1,1,1,0,2,2,3,1,4,0,4],[1,0,1,5]]
"¢": [[2,1,1,1,0,2,0,3,1,4,2,4],[1,0,1,5]]
"%": [[0,4,3,0],[0,1,1,1,1,0,0,1],[2,3,2,4,3,3,2,3]]
"~": [[0,2,1,1,2,3,3,2]]
"`": [[0,0,1,2]]
"£": [[2,0,1,0,0,4,2,4],[0,2,1,2]]
"&": [[3,4,0,1,1,0,2,1,0,3,1,4,3,2]]
"“": [[0,0,1,1],[1,0,2,1]]
"‘": [[0,0,1,1]]
"”": [[2,0,1,1],[1,0,0,1]]
"’": [[1,0,0,1]]
"€": [[0,2,2,2],[2,0,1,0,1,3,2,4],[0,1,2,1]]
"{": [[2,0,1,0,1,1,0,2,1,3,1,4,2,4]]
"}": [[0,0,1,0,1,1,2,2,1,3,1,4,0,4]]
"(": [[1,0,0,1,0,3,1,4]]
")": [[0,0,1,1,1,3,0,4]]
"§": [[0,4,1,4,2,3,0,2,1,1],[2,0,1,0,0,1,2,2,1,3]]
"A": [[0,4,1,0,2,4],[0,3,2,3]]
"B": [[0,4,0,0,2,1,0,2,2,3,0,4]]
"C": [[2,0,1,0,0,2,1,4,2,4]]
"D": [[0,4,0,0,2,1,2,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