فهرست منبع

Fix split text (#260)

* Unified h2d.Text splitText

* Fixed Text splitText
Pascal Peridont 8 سال پیش
والد
کامیت
0548fefb22
2فایلهای تغییر یافته به همراه12 افزوده شده و 31 حذف شده
  1. 10 24
      h2d/Text.hx
  2. 2 7
      hxd/Charset.hx

+ 10 - 24
h2d/Text.hx

@@ -174,15 +174,17 @@ class Text extends Drawable {
 					var e = font.getChar(cc);
 					size += e.width + letterSpacing + e.getKerningOffset(prevChar);
 					prevChar = cc;
+					if( font.charset.isBreakChar(cc) ) break;
 				}
 				if( size > realMaxWidth || (!breakFound && size + afterData > realMaxWidth) ) {
 					newline = true;
-					lines.push(text.substr(restPos, i - restPos));
-					restPos = i;
-					if( font.charset.isSpace(cc) ) {
+					if( font.charset.isSpace(cc) ){
+						lines.push(text.substr(restPos, i - restPos));
 						e = null;
-						restPos++;
+					}else{
+						lines.push(text.substr(restPos, i + 1 - restPos));
 					}
+					restPos = i + 1;
 				}
 			}
 			if( e != null )
@@ -219,35 +221,19 @@ class Text extends Drawable {
 		var dl = font.lineHeight + lineSpacing;
 		var calcLines = !rebuild && lines != null;
 		var yMin = 0;
-		for( i in 0...text.length ) {
-			var cc = text.charCodeAt(i);
+		var t = splitText(text);
+		for( i in 0...t.length ) {
+			var cc = t.charCodeAt(i);
 			var e = font.getChar(cc);
-			var newline = cc == '\n'.code;
 			var offs = e.getKerningOffset(prevChar);
 			var esize = e.width + offs;
 			// if the next word goes past the max width, change it into a newline
-			if( font.charset.isBreakChar(cc) && realMaxWidth >= 0 ) {
-				var size = x + esize + letterSpacing;
-				var k = i + 1, max = text.length;
-				var prevChar = prevChar;
-				while( size <= realMaxWidth && k < max ) {
-					var cc = text.charCodeAt(k++);
-					if( font.charset.isSpace(cc) || cc == '\n'.code ) break;
-					var e = font.getChar(cc);
-					size += e.width + letterSpacing + e.getKerningOffset(prevChar);
-					prevChar = cc;
-				}
-				if( size > realMaxWidth ) {
-					newline = true;
-					if( font.charset.isSpace(cc) ) e = null;
-				}
-			}
 			if( e != null ) {
 				if( rebuild ) glyphs.add(x + offs, y, e.t);
 				if( y == 0 && e.t.dy < yMin ) yMin = e.t.dy;
 				x += esize + letterSpacing;
 			}
-			if( newline ) {
+			if( cc == '\n'.code ) {
 				if( x > xMax ) xMax = x;
 				if( calcLines ) lines.push(x);
 				if( rebuild )

+ 2 - 7
hxd/Charset.hx

@@ -96,16 +96,11 @@ class Charset {
 	}
 
 	public function isSpace(code) {
-		return code == ' '.code || code == 0x3000 || isCJK(code);
+		return code == ' '.code || code == 0x3000;
 	}
 
 	public function isBreakChar(code) {
-		return switch( code ) {
-		case '!'.code, '?'.code, '.'.code, ','.code, ':'.code: true;
-		case '!'.code, '?'.code, '.'.code, ','.code, ':'.code: true; // full width
-		case '・'.code, '、'.code, '。'.code: true; // JP
-		default: isSpace(code);
-		}
+		return isSpace(code) || isCJK(code);
 	}
 
 	static var inst : Charset;