2016-09-23 01:53:39 +00:00
|
|
|
###
|
|
|
|
termap - Terminal Map Viewer
|
|
|
|
by Michael Strassburger <codepoet@cpan.org>
|
|
|
|
|
|
|
|
Simple pixel to barille character mapper
|
|
|
|
|
|
|
|
Implementation inspired by node-drawille (https://github.com/madbence/node-drawille)
|
|
|
|
* added color support
|
2016-09-27 11:57:59 +00:00
|
|
|
* added support for filled polygons
|
|
|
|
* added text label support
|
2016-09-23 01:53:39 +00:00
|
|
|
* general optimizations
|
|
|
|
-> more bit shifting/operations, less Math.floors
|
|
|
|
|
|
|
|
Will either be merged into node-drawille or become an own module at some point
|
|
|
|
###
|
|
|
|
|
|
|
|
module.exports = class BrailleBuffer
|
|
|
|
characterMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]]
|
|
|
|
|
|
|
|
pixelBuffer: null
|
|
|
|
charBuffer: null
|
2016-09-28 11:38:47 +00:00
|
|
|
foregroundBuffer: null
|
|
|
|
backgroundBuffer: null
|
2016-09-23 01:53:39 +00:00
|
|
|
|
2016-09-28 17:39:00 +00:00
|
|
|
globalBackground: null
|
|
|
|
|
2016-09-28 11:38:47 +00:00
|
|
|
termReset: "\x1B[39;49m"
|
2016-09-23 19:06:24 +00:00
|
|
|
|
2016-09-23 01:53:39 +00:00
|
|
|
constructor: (@width, @height) ->
|
|
|
|
@pixelBuffer = new Buffer @width*@height/8
|
|
|
|
@clear()
|
|
|
|
|
|
|
|
clear: ->
|
|
|
|
@pixelBuffer.fill 0
|
|
|
|
@charBuffer = []
|
2016-09-28 11:38:47 +00:00
|
|
|
@foregroundBuffer = []
|
|
|
|
@backgroundBuffer = []
|
|
|
|
|
2016-09-28 17:39:00 +00:00
|
|
|
setGlobalBackground: (@globalBackground) ->
|
|
|
|
|
2016-09-28 12:11:05 +00:00
|
|
|
setBackground: (x, y, color) ->
|
|
|
|
return unless 0 <= x < @width and 0 <= y < @height
|
|
|
|
idx = @_project x, y
|
2016-09-28 11:38:47 +00:00
|
|
|
@backgroundBuffer[idx] = color
|
2016-09-23 01:53:39 +00:00
|
|
|
|
|
|
|
setPixel: (x, y, color) ->
|
|
|
|
@_locate x, y, (idx, mask) =>
|
|
|
|
@pixelBuffer[idx] |= mask
|
2016-09-28 11:38:47 +00:00
|
|
|
@foregroundBuffer[idx] = color
|
2016-09-23 01:53:39 +00:00
|
|
|
|
|
|
|
unsetPixel: (x, y) ->
|
|
|
|
@_locate x, y, (idx, mask) =>
|
|
|
|
@pixelBuffer[idx] &= ~mask
|
|
|
|
|
2016-09-23 19:06:24 +00:00
|
|
|
_project: (x, y) ->
|
|
|
|
(x>>1) + (@width>>1)*(y>>2)
|
|
|
|
|
2016-09-23 01:53:39 +00:00
|
|
|
_locate: (x, y, cb) ->
|
|
|
|
return unless 0 <= x < @width and 0 <= y < @height
|
2016-09-23 19:06:24 +00:00
|
|
|
idx = @_project x, y
|
2016-09-23 01:53:39 +00:00
|
|
|
mask = @characterMap[y&3][x&1]
|
|
|
|
cb idx, mask
|
|
|
|
|
2016-09-28 11:38:47 +00:00
|
|
|
_termColor: (foreground, background) ->
|
2016-09-28 17:39:00 +00:00
|
|
|
background = background or @globalBackground
|
2016-09-28 11:38:47 +00:00
|
|
|
if foreground and background
|
|
|
|
"\x1B[38;5;#{foreground};48;5;#{background}m"
|
|
|
|
else if foreground
|
|
|
|
"\x1B[49;38;5;#{foreground}m"
|
|
|
|
else if background
|
|
|
|
"\x1B[39;48;5;#{background}m"
|
|
|
|
else
|
|
|
|
@termReset
|
|
|
|
|
2016-09-23 01:53:39 +00:00
|
|
|
frame: ->
|
|
|
|
output = []
|
2016-09-23 19:06:24 +00:00
|
|
|
currentColor = null
|
2016-09-23 01:53:39 +00:00
|
|
|
delimeter = "\n"
|
|
|
|
|
|
|
|
for idx in [0...@pixelBuffer.length]
|
2016-09-28 17:29:54 +00:00
|
|
|
output.push delimeter if idx and (idx % (@width/2)) is 0
|
2016-09-23 01:53:39 +00:00
|
|
|
|
2016-09-28 11:38:47 +00:00
|
|
|
if currentColor isnt colorCode = @_termColor @foregroundBuffer[idx], @backgroundBuffer[idx]
|
2016-09-23 19:06:24 +00:00
|
|
|
output.push currentColor = colorCode
|
|
|
|
|
|
|
|
output.push if @charBuffer[idx]
|
|
|
|
@charBuffer[idx]
|
2016-09-23 01:53:39 +00:00
|
|
|
else if @pixelBuffer[idx] is 0
|
2016-09-23 19:06:24 +00:00
|
|
|
' '
|
2016-09-23 01:53:39 +00:00
|
|
|
else
|
2016-09-23 19:06:24 +00:00
|
|
|
String.fromCharCode 0x2800+@pixelBuffer[idx]
|
2016-09-23 01:53:39 +00:00
|
|
|
|
2016-09-23 19:06:24 +00:00
|
|
|
output.push @termReset+delimeter
|
2016-09-23 01:53:39 +00:00
|
|
|
output.join ''
|
|
|
|
|
2016-09-23 19:06:24 +00:00
|
|
|
setChar: (char, x, y, color) ->
|
2016-09-27 15:23:58 +00:00
|
|
|
return unless 0 <= x < @width and 0 <= y < @height
|
2016-09-23 19:06:24 +00:00
|
|
|
idx = @_project x, y
|
2016-09-23 01:53:39 +00:00
|
|
|
@charBuffer[idx] = char
|
2016-09-28 11:38:47 +00:00
|
|
|
@foregroundBuffer[idx] = color
|
2016-09-23 19:06:24 +00:00
|
|
|
|
|
|
|
writeText: (text, x, y, color, center = true) ->
|
2016-09-27 11:57:59 +00:00
|
|
|
x -= text.length/2+1 if center
|
2016-09-23 19:06:24 +00:00
|
|
|
@setChar text.charAt(i), x+i*2, y, color for i in [0...text.length]
|