From 0e5ce02e796aad0864c282b211480ecd684c70b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Wed, 10 May 2017 14:16:36 +0200 Subject: [PATCH] :art: support label characters with width>1 --- src/BrailleBuffer.coffee | 36 ++++++++++++++++++++++++++---------- src/LabelBuffer.coffee | 4 ++-- src/config.coffee | 2 ++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/BrailleBuffer.coffee b/src/BrailleBuffer.coffee index f46f41b..489fa79 100644 --- a/src/BrailleBuffer.coffee +++ b/src/BrailleBuffer.coffee @@ -13,6 +13,8 @@ Will either be merged into node-drawille or become an own module at some point ### +stringWidth = require 'string-width' +config = require './config' module.exports = class BrailleBuffer characterMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]] @@ -78,20 +80,34 @@ module.exports = class BrailleBuffer frame: -> output = [] currentColor = null - delimeter = "\n" + skip = 0 - for idx in [0...@pixelBuffer.length] - output.push delimeter if idx and (idx % (@width/2)) is 0 + for y in [0...@height/4] + skip = 0 - if currentColor isnt colorCode = @_termColor @foregroundBuffer[idx], @backgroundBuffer[idx] - output.push currentColor = colorCode + for x in [0...@width/2] + idx = y*@width/2 + x - output.push if @charBuffer[idx] - @charBuffer[idx] - else - String.fromCharCode 0x2800+@pixelBuffer[idx] + if idx and not x + output.push config.delimeter - output.push @termReset+delimeter + if currentColor isnt colorCode = @_termColor @foregroundBuffer[idx], @backgroundBuffer[idx] + output.push currentColor = colorCode + + output.push if char = @charBuffer[idx] + skip += stringWidth(char)-1 + if skip+x >= @width/2 + '' + else + char + else + if not skip + String.fromCharCode 0x2800+@pixelBuffer[idx] + else + skip-- + '' + + output.push @termReset+config.delimeter output.join '' setChar: (char, x, y, color) -> diff --git a/src/LabelBuffer.coffee b/src/LabelBuffer.coffee index fec3a1f..cd07a36 100644 --- a/src/LabelBuffer.coffee +++ b/src/LabelBuffer.coffee @@ -5,8 +5,8 @@ Using 2D spatial indexing to avoid overlapping labels and markers and to find labels underneath a mouse cursor's position ### - rbush = require 'rbush' +stringWidth = require 'string-width' module.exports = class LabelBuffer tree: null @@ -41,5 +41,5 @@ module.exports = class LabelBuffer _calculateArea: (text, x, y, margin = 0) -> minX: x-margin minY: y-margin/2 - maxX: x+margin+text.length + maxX: x+margin+stringWidth(text) maxY: y+margin/2 diff --git a/src/config.coffee b/src/config.coffee index 6cd6bc9..45fd8cc 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -33,3 +33,5 @@ module.exports = output: process.stdout headless: false + + delimeter: "\n\r" \ No newline at end of file