Skip to content

Commit d5c831d

Browse files
authored
Merge pull request #145 from vladimirkosmala/improve-regex-latex
Cached Latex regex to improve drawing speed
2 parents 7c53fba + 9b2e5d6 commit d5c831d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

scripts/JSRootPainter.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@
403403

404404
JSROOT.Painter = Painter; // export here to avoid ambiguity
405405

406+
Painter.convertSymbol = function(charactere) {
407+
// example: '#pi' will give '\u03A0'
408+
return Painter.symbols_map[charactere];
409+
}
410+
406411
Painter.createMenu = function(painter, maincallback) {
407412
// dummy functions, forward call to the jquery function
408413
document.body.style.cursor = 'wait';
@@ -1064,10 +1069,12 @@
10641069
while ((str.length>2) && (str[0]=='{') && (str[str.length-1]=='}'))
10651070
str = str.substr(1,str.length-2);
10661071

1067-
// str = str.replace(/\^2/gi, '^{2}').replace(/\^3/gi,'^{3}');
1072+
if (!Painter.symbolsRegexCache) {
1073+
// Create a single regex to detect any symbol to replace
1074+
Painter.symbolsRegexCache = new RegExp('(' + Object.keys(Painter.symbols_map).join('|').replace(/\{/g, '\{').replace(/\\}/g, '\\}') + ')', 'g');
1075+
}
10681076

1069-
for (var i in this.symbols_map)
1070-
str = str.replace(new RegExp(i,'g'), this.symbols_map[i]);
1077+
str = str.replace(Painter.symbolsRegexCache, Painter.convertSymbol);
10711078

10721079
return str;
10731080
}

0 commit comments

Comments
 (0)