mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-03-30 16:26:02 +02:00
Merge pull request #20 from rastapasta/ascii
Braille-less ASCII mode, using ▀ ▄ █ ▌▐ ■
This commit is contained in:
commit
5873af5c96
@ -6,24 +6,35 @@
|
|||||||
|
|
||||||
Implementation inspired by node-drawille (https://github.com/madbence/node-drawille)
|
Implementation inspired by node-drawille (https://github.com/madbence/node-drawille)
|
||||||
* added color support
|
* added color support
|
||||||
* added support for filled polygons
|
|
||||||
* added text label support
|
* added text label support
|
||||||
* general optimizations
|
* general optimizations
|
||||||
-> more bit shifting/operations, less Math.floors
|
|
||||||
|
|
||||||
Will either be merged into node-drawille or become an own module at some point
|
Will either be merged into node-drawille or become an own module at some point
|
||||||
###
|
###
|
||||||
stringWidth = require 'string-width'
|
stringWidth = require 'string-width'
|
||||||
config = require './config'
|
config = require './config'
|
||||||
|
utils = require './utils'
|
||||||
|
|
||||||
module.exports = class BrailleBuffer
|
module.exports = class BrailleBuffer
|
||||||
characterMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]]
|
brailleMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]]
|
||||||
|
asciiMap:
|
||||||
|
# "▬": [2+32, 4+64]
|
||||||
|
# "¯": [1+16]
|
||||||
|
"▀": [1+2+16+32]
|
||||||
|
"▄": [4+8+64+128]
|
||||||
|
"■": [2+4+32+64]
|
||||||
|
"▌": [1+2+4+8]
|
||||||
|
"▐": [16+32+64+128]
|
||||||
|
# "▓": [1+4+32+128, 2+8+16+64]
|
||||||
|
"█": [255]
|
||||||
|
|
||||||
pixelBuffer: null
|
pixelBuffer: null
|
||||||
charBuffer: null
|
charBuffer: null
|
||||||
foregroundBuffer: null
|
foregroundBuffer: null
|
||||||
backgroundBuffer: null
|
backgroundBuffer: null
|
||||||
|
|
||||||
|
asciiToBraille: []
|
||||||
|
|
||||||
globalBackground: null
|
globalBackground: null
|
||||||
|
|
||||||
termReset: "\x1B[39;49m"
|
termReset: "\x1B[39;49m"
|
||||||
@ -33,6 +44,8 @@ module.exports = class BrailleBuffer
|
|||||||
@pixelBuffer = new Buffer size
|
@pixelBuffer = new Buffer size
|
||||||
@foregroundBuffer = new Buffer size
|
@foregroundBuffer = new Buffer size
|
||||||
@backgroundBuffer = new Buffer size
|
@backgroundBuffer = new Buffer size
|
||||||
|
|
||||||
|
@_mapBraille()
|
||||||
@clear()
|
@clear()
|
||||||
|
|
||||||
clear: ->
|
clear: ->
|
||||||
@ -63,9 +76,28 @@ module.exports = class BrailleBuffer
|
|||||||
_locate: (x, y, cb) ->
|
_locate: (x, y, cb) ->
|
||||||
return unless 0 <= x < @width and 0 <= y < @height
|
return unless 0 <= x < @width and 0 <= y < @height
|
||||||
idx = @_project x, y
|
idx = @_project x, y
|
||||||
mask = @characterMap[y&3][x&1]
|
mask = @brailleMap[y&3][x&1]
|
||||||
cb idx, mask
|
cb idx, mask
|
||||||
|
|
||||||
|
_mapBraille: ->
|
||||||
|
@asciiToBraille = [" "]
|
||||||
|
|
||||||
|
masks = []
|
||||||
|
for char, bits of @asciiMap
|
||||||
|
continue unless bits instanceof Array
|
||||||
|
masks.push mask: mask, char: char for mask in bits
|
||||||
|
|
||||||
|
for i in [1..255]
|
||||||
|
braille = (i&7) + ((i&56)<<1) + ((i&64)>>3) + (i&128)
|
||||||
|
|
||||||
|
@asciiToBraille[i] = masks.reduce(((best, mask) ->
|
||||||
|
covered = utils.population(mask.mask&braille)
|
||||||
|
if not best or best.covered < covered
|
||||||
|
char: mask.char, covered: covered
|
||||||
|
else
|
||||||
|
best
|
||||||
|
), undefined).char
|
||||||
|
|
||||||
_termColor: (foreground, background) ->
|
_termColor: (foreground, background) ->
|
||||||
background = background or @globalBackground
|
background = background or @globalBackground
|
||||||
if foreground and background
|
if foreground and background
|
||||||
@ -102,7 +134,10 @@ module.exports = class BrailleBuffer
|
|||||||
char
|
char
|
||||||
else
|
else
|
||||||
if not skip
|
if not skip
|
||||||
String.fromCharCode 0x2800+@pixelBuffer[idx]
|
if config.useBraille
|
||||||
|
String.fromCharCode 0x2800+@pixelBuffer[idx]
|
||||||
|
else
|
||||||
|
@asciiToBraille[@pixelBuffer[idx]]
|
||||||
else
|
else
|
||||||
skip--
|
skip--
|
||||||
''
|
''
|
||||||
|
@ -179,6 +179,10 @@ module.exports = class Mapscii
|
|||||||
when "up" then @moveBy 6/Math.pow(2, @zoom), 0
|
when "up" then @moveBy 6/Math.pow(2, @zoom), 0
|
||||||
when "down" then @moveBy -6/Math.pow(2, @zoom), 0
|
when "down" then @moveBy -6/Math.pow(2, @zoom), 0
|
||||||
|
|
||||||
|
when "c"
|
||||||
|
config.useBraille = !config.useBraille
|
||||||
|
true
|
||||||
|
|
||||||
else
|
else
|
||||||
null
|
null
|
||||||
|
|
||||||
|
@ -175,9 +175,8 @@ module.exports = class Renderer
|
|||||||
@canvas.polygon points, feature.color
|
@canvas.polygon points, feature.color
|
||||||
|
|
||||||
when "symbol"
|
when "symbol"
|
||||||
genericSymbol = null
|
|
||||||
text = feature.label or
|
text = feature.label or
|
||||||
genericSymbol = "◉"
|
genericSymbol = config.poiMarker
|
||||||
|
|
||||||
return false if @_seen[text] and not genericSymbol
|
return false if @_seen[text] and not genericSymbol
|
||||||
|
|
||||||
@ -192,8 +191,8 @@ module.exports = class Renderer
|
|||||||
break
|
break
|
||||||
|
|
||||||
else if config.layers[feature.layer]?.cluster and
|
else if config.layers[feature.layer]?.cluster and
|
||||||
@labelBuffer.writeIfPossible "◉", point.x, point.y, feature, 3
|
@labelBuffer.writeIfPossible config.poiMarker, point.x, point.y, feature, 3
|
||||||
@canvas.text "◉", point.x, point.y, feature.color
|
@canvas.text config.poiMarker, point.x, point.y, feature.color
|
||||||
placed = true
|
placed = true
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -57,8 +57,8 @@ class Tile
|
|||||||
feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type]
|
feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type]
|
||||||
|
|
||||||
if @styler
|
if @styler
|
||||||
style = @styler.getStyleFor name, feature
|
style = @styler.getStyleFor name, feature
|
||||||
continue unless style
|
continue unless style
|
||||||
|
|
||||||
color =
|
color =
|
||||||
style.paint['line-color'] or
|
style.paint['line-color'] or
|
||||||
|
@ -15,6 +15,8 @@ module.exports =
|
|||||||
|
|
||||||
simplifyPolylines: false
|
simplifyPolylines: false
|
||||||
|
|
||||||
|
useBraille: true
|
||||||
|
|
||||||
# Downloaded files get persisted in ~/.mapscii
|
# Downloaded files get persisted in ~/.mapscii
|
||||||
persistDownloadedTiles: true
|
persistDownloadedTiles: true
|
||||||
|
|
||||||
@ -34,4 +36,6 @@ module.exports =
|
|||||||
|
|
||||||
headless: false
|
headless: false
|
||||||
|
|
||||||
delimeter: "\n\r"
|
delimeter: "\n\r"
|
||||||
|
|
||||||
|
poiMarker: "◉"
|
@ -64,4 +64,11 @@ utils =
|
|||||||
|
|
||||||
ll
|
ll
|
||||||
|
|
||||||
|
population: (val) ->
|
||||||
|
bits = 0
|
||||||
|
while val>0
|
||||||
|
bits += val & 1
|
||||||
|
val >>= 1
|
||||||
|
bits
|
||||||
|
|
||||||
module.exports = utils
|
module.exports = utils
|
||||||
|
Loading…
Reference in New Issue
Block a user