From 1b1775a4c2ca6068c6322da5bd744f17eaa32f3e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 15 Apr 2015 00:37:03 -0700 Subject: [PATCH] add doubleWidthPerfect option. --- README.md | 3 +++ lib/widget.js | 35 +++++++++++++++++++++++++++++++++-- test/widget-eaw.js | 5 ++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 66d6ff7..5b6a4fd 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,9 @@ The screen on which every other node renders. overlapping, depending on position (__experimental__). for example: - __doubleWidth__ - allow for rendering of East Asian double-width characters. this is behind an option because it may affect performance negatively. +- __doubleWidthPerfect__ - handle high code point double-width characters, + without this option, high code point double width characters just show up as + `?`. that being said, this option will slow content parsing a fair amount. These border-overlapped elements: diff --git a/lib/widget.js b/lib/widget.js index 3ba9e8f..f0b2052 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2335,6 +2335,29 @@ Element.prototype.parseContent = function(noTags) { content = content.replace(wideChars, '?'); } + if (this.screen.options.doubleWidthPerfect) { + var _content = content; + content = ''; + for (var i = 0; i < _content.length; i++) { + var point = _content.codePointAt(i); + if ((point >= 0x20000 && point <= 0x2fffd) + || (point >= 0x30000 && point <= 0x3fffd)) { + if (this.screen.options.doubleWidth + && (this.screen.tput.unicode + || this.screen.tput.numbers.U8 === 1)) { + content += _content[i] + ' '; + } else { + // NOTE: could use two chars: '? ' depending on what is intended. + // if we did, we could remove the unicode checks above in this if + // statement. + content += '?'; + } + } else { + content += _content[i]; + } + } + } + if (!noTags) { content = this._parseTags(content); } @@ -8591,7 +8614,14 @@ function hsort(obj) { }); } -var wideChars = new RegExp('([' +var wideChars = new RegExp('(' + // 0x20000 - 0x2fffd: + // + '[\\ud840-\\ud87f][\\udc00-\\udffd]' + // + '|' + // 0x30000 - 0x3fffd: + // + '[\\ud880-\\ud8bf][\\udc00-\\udffd]' + // + '|' + + '[' + '\\u1100-\\u115f' /* Hangul Jamo init. consonants */ + '\\u2329\\u232a' + '\\u2e80-\\u303e\\u3040-\\ua4cf' /* CJK ... Yi */ @@ -8606,7 +8636,8 @@ var wideChars = new RegExp('([' // however, the next char on the screen will be eaten. // + '\\u20000-\\u2fffd' // + '\\u30000-\\u3fffd' -+ '])', 'g'); + + ']' + + ')', 'g'); function findFile(start, target) { return (function read(dir) { diff --git a/test/widget-eaw.js b/test/widget-eaw.js index 879e9f1..3162c0b 100644 --- a/test/widget-eaw.js +++ b/test/widget-eaw.js @@ -5,10 +5,12 @@ screen = blessed.screen({ dump: __dirname + '/logs/eaw.log', smartCSR: true, dockBorders: true, - doubleWidth: true + doubleWidth: true, + doubleWidthPerfect: true }); var DW = '杜'; +var DW2 = String.fromCodePoint ? String.fromCodePoint(0x30000) : 'a'; // At cols=44, the bug that is avoided by this occurs: // || angles[line[x + 1][1]]) { @@ -59,6 +61,7 @@ var lorem = 'Non eram nescius Brute cum quae summis ingeniis exquisitaque' + ' legantur'; lorem = lorem.replace(/e/gi, DW); +lorem = lorem.replace(/a/gi, DW2); var main = blessed.box({ parent: screen,