Ver Fonte

Replace charCodeAt by StringTools.fastCodeAt in h2d texts

Leonardo Jeanteur há 1 ano atrás
pai
commit
48e09f4bca
3 ficheiros alterados com 29 adições e 21 exclusões
  1. 14 10
      h2d/HtmlText.hx
  2. 11 7
      h2d/Text.hx
  3. 4 4
      h2d/TextInput.hx

+ 14 - 10
h2d/HtmlText.hx

@@ -298,7 +298,7 @@ class HtmlText extends Text {
 			var str = splitNode.node.nodeValue;
 			var info = metrics[metrics.length - 1];
 			var w = info.width;
-			var cc = str.charCodeAt(splitNode.pos);
+			var cc = StringTools.fastCodeAt(str, splitNode.pos);
 			// Restore line metrics to ones before split.
 			// Potential bug: `Text<split> [Image] text<split>text` - third line will use metrics as if image is present in the line.
 			info.width = splitNode.width;
@@ -421,12 +421,12 @@ class HtmlText extends Text {
 			var x = leftMargin;
 			var breakChars = 0;
 			for ( i in 0...text.length ) {
-				var cc = text.charCodeAt(i);
+				var cc = StringTools.fastCodeAt(text, i);
 				var g = font.getChar(cc);
 				var newline = cc == '\n'.code;
 				var esize = g.width + g.getKerningOffset(prevChar);
-				var nc = text.charCodeAt(i+1);
-				if ( font.charset.isBreakChar(cc) && (nc == null || !font.charset.isComplementChar(nc) )) {
+				var isComplement = (i < text.length - 1 && font.charset.isComplementChar(StringTools.fastCodeAt(text, i + 1)));
+				if ( font.charset.isBreakChar(cc) && !isComplement) {
 					// Case: Very first word in text makes the line too long hence we want to start it off on a new line.
 					if (x > maxWidth && textSplit.length == 0 && splitNode.node != null) {
 						metrics.push(makeLineInfo(x, info.height, info.baseLine));
@@ -437,13 +437,17 @@ class HtmlText extends Text {
 					var k = i + 1, max = text.length;
 					var prevChar = cc;
 					while ( size <= maxWidth && k < max ) {
-						var cc = text.charCodeAt(k++);
+						var cc = StringTools.fastCodeAt(text, k++);
 						if ( lineBreak && (font.charset.isSpace(cc) || cc == '\n'.code ) ) break;
 						var e = font.getChar(cc);
 						size += e.width + letterSpacing + e.getKerningOffset(prevChar);
 						prevChar = cc;
-						var nc = text.charCodeAt(k);
-						if ( font.charset.isBreakChar(cc) && (nc == null || !font.charset.isComplementChar(nc)) ) break;
+						if ( font.charset.isBreakChar(cc) ) {
+							if ( k >= text.length )
+								break;
+							var nc = StringTools.fastCodeAt(text, k);
+							if ( !font.charset.isComplementChar(nc) ) break;
+						}
 					}
 					// Avoid empty line when last char causes line-break while being CJK
 					if ( lineBreak && size > maxWidth && i != max - 1 ) {
@@ -557,7 +561,7 @@ class HtmlText extends Text {
 				var startI = 0;
 				var index = Lambda.indexOf(e.parent, e);
 				for (i in 0...text.length) {
-					if (text.charCodeAt(i) == '\n'.code) {
+					if (StringTools.fastCodeAt(text, i) == '\n'.code) {
 						var pre = text.substring(startI, i);
 						if (pre != "") e.parent.insertChild(Xml.createPCData(pre), index++);
 						e.parent.insertChild(Xml.createElement("br"),index++);
@@ -671,7 +675,7 @@ class HtmlText extends Text {
 					switch( a.toLowerCase() ) {
 					case "color":
 						if( prevColor == null ) prevColor = @:privateAccess glyphs.curColor.clone();
-						if( v.charCodeAt(0) == '#'.code && v.length == 4 )
+						if( v.length == 4 && StringTools.fastCodeAt(v, 0) == '#'.code )
 							v = "#" + v.charAt(1) + v.charAt(1) + v.charAt(2) + v.charAt(2) + v.charAt(3) + v.charAt(3);
 						glyphs.setDefaultColor(Std.parseInt("0x" + v.substr(1)));
 					case "opacity":
@@ -781,7 +785,7 @@ class HtmlText extends Text {
 			var t = e.nodeValue;
 			var dy = metrics[sizePos].baseLine - font.baseLine;
 			for( i in 0...t.length ) {
-				var cc = t.charCodeAt(i);
+				var cc = StringTools.fastCodeAt(t, i);
 				if( cc == "\n".code ) {
 					makeLineBreak();
 					dy = metrics[sizePos].baseLine - font.baseLine;

+ 11 - 7
h2d/Text.hx

@@ -328,12 +328,12 @@ class Text extends Drawable {
 		var x = leftMargin;
 		var wLastSep = 0.;
 		for( i in 0...text.length ) {
-			var cc = text.charCodeAt(i);
+			var cc = StringTools.fastCodeAt(text, i);
 			var e = font.getChar(cc);
 			var newline = cc == '\n'.code;
 			var esize = e.width + e.getKerningOffset(prevChar);
-			var nc = text.charCodeAt(i+1);
-			if( font.charset.isBreakChar(cc) && (nc == null || !font.charset.isComplementChar(nc)) ) {
+			var isComplement = (i < text.length - 1 && font.charset.isComplementChar(StringTools.fastCodeAt(text, i + 1)));
+			if( font.charset.isBreakChar(cc) && !isComplement ) {
 				if( lines.length == 0 && leftMargin > 0 && x > maxWidth ) {
 					lines.push("");
 					if ( sizes != null ) sizes.push(leftMargin);
@@ -344,7 +344,7 @@ class Text extends Drawable {
 				var prevChar = cc;
 				var breakFound = false;
 				while( size <= maxWidth && k < max ) {
-					var cc = text.charCodeAt(k++);
+					var cc = StringTools.fastCodeAt(text, k++);
 					if( lineBreak && (font.charset.isSpace(cc) || cc == '\n'.code ) ) {
 						breakFound = true;
 						break;
@@ -352,8 +352,12 @@ class Text extends Drawable {
 					var e = font.getChar(cc);
 					size += e.width + letterSpacing + e.getKerningOffset(prevChar);
 					prevChar = cc;
-					var nc = text.charCodeAt(k);
-					if( font.charset.isBreakChar(cc) && (nc == null || !font.charset.isComplementChar(nc)) ) break;
+					if ( font.charset.isBreakChar(cc) ) {
+						if ( k >= text.length )
+							break;
+						var nc = StringTools.fastCodeAt(text, k);
+						if ( !font.charset.isComplementChar(nc) ) break;
+					}
 				}
 				if( lineBreak && (size > maxWidth || (!breakFound && size + afterData > maxWidth)) ) {
 					newline = true;
@@ -429,7 +433,7 @@ class Text extends Drawable {
 		}
 
 		for( i in 0...t.length ) {
-			var cc = t.charCodeAt(i);
+			var cc = StringTools.fastCodeAt(t, i);
 			var e = font.getChar(cc);
 			var offs = e.getKerningOffset(prevChar);
 			var esize = e.width + offs;

+ 4 - 4
h2d/TextInput.hx

@@ -367,7 +367,7 @@ class TextInput extends Text {
 			lines.push( { line: line, startIndex: currIndex } );
 			var prevIndex = currIndex;
 			currIndex += line.length;
-			if( cursorIndex > prevIndex && cursorIndex < currIndex ) 
+			if( cursorIndex > prevIndex && cursorIndex < currIndex )
 				cursorLineIndex = currLineIndex;
 			currLineIndex++;
 		}
@@ -391,7 +391,7 @@ class TextInput extends Text {
 		var currOffset = 0.;
 		prevCC = null;
 		for( cI in 0...destination.line.length ) {
-			var cc = destination.line.charCodeAt(cI);
+			var cc = StringTools.fastCodeAt(destination.line, cI);
 			var c = font.getChar(cc);
 			var newCurrOffset = currOffset + c.width + c.getKerningOffset(prevCC) + letterSpacing;
 			if( newCurrOffset > xOffset ) {
@@ -451,8 +451,8 @@ class TextInput extends Text {
 		var currIndex = 0;
 		for( i in 0...lines.length ) {
 			var newCurrIndex = currIndex + lines[i].length;
-			if( cursorIndex < newCurrIndex ) 
-				return { value: lines[i], startIndex: currIndex };		
+			if( cursorIndex < newCurrIndex )
+				return { value: lines[i], startIndex: currIndex };
 			currIndex = newCurrIndex;
 		}
 		return { value: '', startIndex: -1 };