Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
234 lines (217 sloc) 7.86 KB
class this.Spindlytext
piOver180: Math.PI / 180
latFactor: 0.00001
fontHeight: 5
nbsp: String.fromCharCode(160)
constructor: (@lat = 0, @lon = 0, @alt = 0, 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
o.log ?= no
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 chr in text.split ''
if chr in [" ", @nbsp, "\n", "\r"]
xCursor += o.spaceWidth
continue
if chr is "\t"
xCursor = Math.ceil((xCursor + 1) / tabWidth) * tabWidth
continue
paths = o.font[chr]
unless paths
console.log 'Unsupported character: U+' + chr.charCodeAt(0).toString(16).toUpperCase() if o.log
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>'
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>"
# for SVG use, set latFactor = 1, give an HTML colour, and omit lat/lon/alt -- e.g.
# Spindlytext.prototype.latFactor = 1;
# new Spindlytext().text('Merry Xmas', {colour: '#000000'}).svg()
svgFragment: ->
paths = []
for lineCoordSets, i in @allCoordSets
o = @lineOpts[i]
d = []
for lineCoords in lineCoordSets
for coords, j in lineCoords
d.push "#{if j is 0 then 'M' else 'L'} #{coords[0]} #{-coords[2]}"
paths.push "<path fill='none' stroke='#{o.colour}' stroke-width='#{o.lineWidth}' d='#{d.join ' '}' />"
paths.join ''
svg: ->
"<?xml version='1.0' encoding='UTF-8'?><svg xmlns='http://www.w3.org/2000/svg'>#{@svgFragment()}</svg>"
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,2,0,0,4]]
"8": [[1,0,0,1,2,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[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,3,0,4]]
"E": [[2,0,0,0,0,4,2,4],[0,2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