From 7e255edc53cb7beed484eaffe4083298a26f4827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Fri, 12 May 2017 18:48:38 +0200 Subject: [PATCH 1/3] :art: adding braille-free mode, activate with 'c' --- src/BrailleBuffer.coffee | 43 +++++++++++++++++++++++++++++++++++----- src/Mapscii.coffee | 4 ++++ src/Tile.coffee | 4 ++-- src/config.coffee | 2 ++ src/utils.coffee | 7 +++++++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/BrailleBuffer.coffee b/src/BrailleBuffer.coffee index 489fa79..5b49ae0 100644 --- a/src/BrailleBuffer.coffee +++ b/src/BrailleBuffer.coffee @@ -6,24 +6,34 @@ Implementation inspired by node-drawille (https://github.com/madbence/node-drawille) * added color support - * added support for filled polygons * added text label support * general optimizations - -> more bit shifting/operations, less Math.floors Will either be merged into node-drawille or become an own module at some point ### stringWidth = require 'string-width' config = require './config' +utils = require './utils' 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+2+4+8] + # "▐": [16+32+64+128] + "▀": [1+2+16+32] + "▄": [4+8+64+128] + "■": [2+4+32+64] + #"▓": [1+4+32+128, 2+8+16+64] + "█": [255] pixelBuffer: null charBuffer: null foregroundBuffer: null backgroundBuffer: null + asciiToBraille: [] + globalBackground: null termReset: "\x1B[39;49m" @@ -33,6 +43,8 @@ module.exports = class BrailleBuffer @pixelBuffer = new Buffer size @foregroundBuffer = new Buffer size @backgroundBuffer = new Buffer size + + @_mapBraille() @clear() clear: -> @@ -63,9 +75,27 @@ module.exports = class BrailleBuffer _locate: (x, y, cb) -> return unless 0 <= x < @width and 0 <= y < @height idx = @_project x, y - mask = @characterMap[y&3][x&1] + mask = @brailleMap[y&3][x&1] 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 + + reducer = (best, mask) -> + covered = utils.population(mask.mask&i) + return if not best or best.covered < covered + char: mask.char, covered: covered + else + best + + for i in [1..255] + @asciiToBraille[i] = masks.reduce(reducer, undefined).char + _termColor: (foreground, background) -> background = background or @globalBackground if foreground and background @@ -102,7 +132,10 @@ module.exports = class BrailleBuffer char else if not skip - String.fromCharCode 0x2800+@pixelBuffer[idx] + if config.useBraille + String.fromCharCode 0x2800+@pixelBuffer[idx] + else + @asciiToBraille[@pixelBuffer[idx]] else skip-- '' diff --git a/src/Mapscii.coffee b/src/Mapscii.coffee index fedcfa8..c463dc5 100644 --- a/src/Mapscii.coffee +++ b/src/Mapscii.coffee @@ -179,6 +179,10 @@ module.exports = class Mapscii when "up" 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 null diff --git a/src/Tile.coffee b/src/Tile.coffee index 45f5631..173ca50 100644 --- a/src/Tile.coffee +++ b/src/Tile.coffee @@ -57,8 +57,8 @@ class Tile feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type] if @styler - style = @styler.getStyleFor name, feature - continue unless style + style = @styler.getStyleFor name, feature + continue unless style color = style.paint['line-color'] or diff --git a/src/config.coffee b/src/config.coffee index 45fd8cc..2856379 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -15,6 +15,8 @@ module.exports = simplifyPolylines: false + useBraille: true + # Downloaded files get persisted in ~/.mapscii persistDownloadedTiles: true diff --git a/src/utils.coffee b/src/utils.coffee index 57f08e0..9e05419 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -64,4 +64,11 @@ utils = ll + population: (val) -> + bits = 0 + while val>0 + bits += val & 1 + val >>= 1 + bits + module.exports = utils From 7edde3b30fcb3cbe1d81943e98fdf40688f19f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Fri, 12 May 2017 19:58:39 +0200 Subject: [PATCH 2/3] :art: moving poiMarker to config, improving asciiMap --- src/BrailleBuffer.coffee | 20 ++++++++++---------- src/Renderer.coffee | 7 +++---- src/config.coffee | 4 +++- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/BrailleBuffer.coffee b/src/BrailleBuffer.coffee index 5b49ae0..870cbca 100644 --- a/src/BrailleBuffer.coffee +++ b/src/BrailleBuffer.coffee @@ -18,14 +18,15 @@ utils = require './utils' module.exports = class BrailleBuffer brailleMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]] asciiMap: - #"▬": [2+32, 4+64] + # "▬": [2+32, 4+64] # "▌": [1+2+4+8] # "▐": [16+32+64+128] + #"¯": [1+16] "▀": [1+2+16+32] "▄": [4+8+64+128] "■": [2+4+32+64] - #"▓": [1+4+32+128, 2+8+16+64] "█": [255] + #"▓": [1+4+32+128, 2+8+16+64] pixelBuffer: null charBuffer: null @@ -86,15 +87,14 @@ module.exports = class BrailleBuffer continue unless bits instanceof Array masks.push mask: mask, char: char for mask in bits - reducer = (best, mask) -> - covered = utils.population(mask.mask&i) - return if not best or best.covered < covered - char: mask.char, covered: covered - else - best - for i in [1..255] - @asciiToBraille[i] = masks.reduce(reducer, undefined).char + @asciiToBraille[i] = masks.reduce(((best, mask) -> + covered = utils.population mask.mask&i + if not best or best.covered < covered + char: mask.char, covered: covered + else + best + ), undefined).char _termColor: (foreground, background) -> background = background or @globalBackground diff --git a/src/Renderer.coffee b/src/Renderer.coffee index 209ed83..bb58bde 100644 --- a/src/Renderer.coffee +++ b/src/Renderer.coffee @@ -175,9 +175,8 @@ module.exports = class Renderer @canvas.polygon points, feature.color when "symbol" - genericSymbol = null text = feature.label or - genericSymbol = "◉" + genericSymbol = config.poiMarker return false if @_seen[text] and not genericSymbol @@ -192,8 +191,8 @@ module.exports = class Renderer break else if config.layers[feature.layer]?.cluster and - @labelBuffer.writeIfPossible "◉", point.x, point.y, feature, 3 - @canvas.text "◉", point.x, point.y, feature.color + @labelBuffer.writeIfPossible config.poiMarker, point.x, point.y, feature, 3 + @canvas.text config.poiMarker, point.x, point.y, feature.color placed = true break diff --git a/src/config.coffee b/src/config.coffee index 2856379..9267745 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -36,4 +36,6 @@ module.exports = headless: false - delimeter: "\n\r" \ No newline at end of file + delimeter: "\n\r" + + poiMarker: "◉" \ No newline at end of file From 724e605317169f4ee4ab302c8dcb36c52c3d8e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Fri, 12 May 2017 23:11:42 +0200 Subject: [PATCH 3/3] :art: convert braille idx to bitstring to allow ascii selection --- src/BrailleBuffer.coffee | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/BrailleBuffer.coffee b/src/BrailleBuffer.coffee index 870cbca..643cb0e 100644 --- a/src/BrailleBuffer.coffee +++ b/src/BrailleBuffer.coffee @@ -19,14 +19,14 @@ module.exports = class BrailleBuffer brailleMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]] asciiMap: # "▬": [2+32, 4+64] - # "▌": [1+2+4+8] - # "▐": [16+32+64+128] - #"¯": [1+16] + # "¯": [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] - #"▓": [1+4+32+128, 2+8+16+64] pixelBuffer: null charBuffer: null @@ -88,8 +88,10 @@ module.exports = class BrailleBuffer 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&i + covered = utils.population(mask.mask&braille) if not best or best.covered < covered char: mask.char, covered: covered else