Skip to content

Commit 52c8838

Browse files
author
Steven Yau
committed
Reused symbols array to removed non visible symbols
1 parent c03e862 commit 52c8838

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/framework/components/element/text-element.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function TextElement(element) {
4949
// public
5050
this._text = ""; // the original user-defined text
5151
this._symbols = []; // array of symbols with unicode processing and markup removed
52-
this._visibleSymbols = []; // array of symbols that is visible to the user
5352
this._colorPalette = []; // per-symbol color palette
5453
this._symbolColors = null; // per-symbol color indexes. only set for text with markup.
5554
this._i18nKey = null;
@@ -156,7 +155,7 @@ var NON_VISIBLE_CHAR = new Set([
156155
'\u202D', // left-to-right override
157156
'\u202E', // right-to-left override
158157
'\u202C', // pop directional formatting
159-
'\u2069', // pop directional isolate
158+
'\u2069' // pop directional isolate
160159
]);
161160

162161
Object.assign(TextElement.prototype, {
@@ -506,13 +505,17 @@ Object.assign(TextElement.prototype, {
506505
this._element.addModelToLayers(this._model);
507506
}
508507

509-
this._visibleSymbols.length = 0;
508+
// Remove the non visible symbols
509+
var inputIndex = 0;
510510
for (i = 0; i < this._symbols.length; i++) {
511511
if (!NON_VISIBLE_CHAR.has(this._symbols[i])) {
512-
this._visibleSymbols.push(this._symbols[i]);
512+
this._symbols[inputIndex] = this._symbols[i];
513+
inputIndex++;
513514
}
514515
}
515516

517+
this._symbols.length = inputIndex;
518+
516519
this._updateMeshes();
517520

518521
// update render range
@@ -590,7 +593,7 @@ Object.assign(TextElement.prototype, {
590593
}
591594

592595
var MAGIC = 32;
593-
var l = this._visibleSymbols.length;
596+
var l = this._symbols.length;
594597
var _x = 0; // cursors
595598
var _y = 0;
596599
var _z = 0;
@@ -701,7 +704,7 @@ Object.assign(TextElement.prototype, {
701704
// In right-to-left mode we loop through the symbols from end to the beginning
702705
// in order to wrap lines in the correct order
703706
for (i = 0; i < l; i++) {
704-
char = this._visibleSymbols[i];
707+
char = this._symbols[i];
705708

706709
var x = 0;
707710
var y = 0;
@@ -728,9 +731,9 @@ Object.assign(TextElement.prototype, {
728731
if (numCharsThisLine > 0) {
729732
var kernTable = this._font.data.kerning;
730733
if (kernTable) {
731-
var kernLeft = kernTable[string.getCodePoint(this._visibleSymbols[i - 1]) || 0];
734+
var kernLeft = kernTable[string.getCodePoint(this._symbols[i - 1]) || 0];
732735
if (kernLeft) {
733-
kerning = kernLeft[string.getCodePoint(this._visibleSymbols[i]) || 0] || 0;
736+
kerning = kernLeft[string.getCodePoint(this._symbols[i]) || 0] || 0;
734737
}
735738
}
736739
}
@@ -749,7 +752,7 @@ Object.assign(TextElement.prototype, {
749752
if (isLineBreak) {
750753
numBreaksThisLine++;
751754
if (this._maxLines < 0 || lines < this._maxLines) {
752-
breakLine(this._visibleSymbols, i, _xMinusTrailingWhitespace);
755+
breakLine(this._symbols, i, _xMinusTrailingWhitespace);
753756
wordStartIndex = i + 1;
754757
lineStartIndex = i + 1;
755758
}
@@ -771,7 +774,7 @@ Object.assign(TextElement.prototype, {
771774
// broken onto multiple lines.
772775
if (numWordsThisLine === 0) {
773776
wordStartIndex = i;
774-
breakLine(this._visibleSymbols, i, _xMinusTrailingWhitespace);
777+
breakLine(this._symbols, i, _xMinusTrailingWhitespace);
775778
} else {
776779
// Move back to the beginning of the current word.
777780
var backtrack = Math.max(i - wordStartIndex, 0);
@@ -784,7 +787,7 @@ Object.assign(TextElement.prototype, {
784787
var backtrackStart = wordStartIndex;
785788
var backtrackEnd = i;
786789
for (j = backtrackStart; j < backtrackEnd; j++) {
787-
var backChar = this._visibleSymbols[j];
790+
var backChar = this._symbols[j];
788791
var backCharData = json.chars[backChar];
789792
var backMeshInfo = this._meshInfo[(backCharData && backCharData.map) || 0];
790793
backMeshInfo.lines[lines - 1] -= 1;
@@ -794,7 +797,7 @@ Object.assign(TextElement.prototype, {
794797

795798
i -= backtrack + 1;
796799

797-
breakLine(this._visibleSymbols, wordStartIndex, wordStartX);
800+
breakLine(this._symbols, wordStartIndex, wordStartX);
798801
continue;
799802
}
800803
}
@@ -933,7 +936,7 @@ Object.assign(TextElement.prototype, {
933936
// there will almost always be some leftover text on the final line which has
934937
// not yet been pushed to _lineContents.
935938
if (lineStartIndex < l) {
936-
breakLine(this._visibleSymbols, l, _x);
939+
breakLine(this._symbols, l, _x);
937940
}
938941
}
939942

0 commit comments

Comments
 (0)