Browse Source

Add hxd.UString abstract

Pascal Peridont 9 năm trước cách đây
mục cha
commit
8eb711994e
3 tập tin đã thay đổi với 61 bổ sung22 xóa
  1. 16 16
      h2d/Text.hx
  2. 39 0
      hxd/UString.hx
  3. 6 6
      hxd/res/FontBuilder.hx

+ 16 - 16
h2d/Text.hx

@@ -9,7 +9,7 @@ enum Align {
 class Text extends Drawable {
 
 	public var font(default, set) : Font;
-	public var text(default, set) : String;
+	public var text(default, set) : hxd.UString;
 	public var textColor(default, set) : Int;
 	public var maxWidth(default, set) : Null<Float>;
 	public var dropShadow : { dx : Float, dy : Float, color : Int, alpha : Float };
@@ -108,7 +108,7 @@ class Text extends Drawable {
 		glyphs.drawWith(ctx,this);
 	}
 
-	function set_text(t) {
+	function set_text(t : hxd.UString) {
 		var t = t == null ? "null" : t;
 		if( t == this.text ) return t;
 		this.text = t;
@@ -121,7 +121,7 @@ class Text extends Drawable {
 		if( allocated && text != null && font != null ) initGlyphs(text);
 	}
 
-	public function calcTextWidth( text : String ) {
+	public function calcTextWidth( text : hxd.UString ) {
 		if( calcDone ) {
 			var ow = calcWidth, oh = calcHeight, osh = calcSizeHeight, oy = calcYMin;
 			initGlyphs(text, false);
@@ -138,13 +138,13 @@ class Text extends Drawable {
 		}
 	}
 
-	public function splitText( text : String, leftMargin = 0 ) {
+	public function splitText( text : hxd.UString, leftMargin = 0 ) {
 		if( maxWidth == null )
 			return text;
 		var lines = [], rest = text, restPos = 0;
 		var x = leftMargin, prevChar = -1;
-		for( i in 0...haxe.Utf8.length(text) ) {
-			var cc = haxe.Utf8.charCodeAt(text, i);
+		for( i in 0...text.length ) {
+			var cc = text.charCodeAt(i);
 			var e = font.getChar(cc);
 			var newline = cc == '\n'.code;
 			var esize = e.width + e.getKerningOffset(prevChar);
@@ -154,10 +154,10 @@ class Text extends Drawable {
 					x -= leftMargin;
 				}
 				var size = x + esize + letterSpacing;
-				var k = i + 1, max = haxe.Utf8.length(text);
+				var k = i + 1, max = text.length;
 				var prevChar = prevChar;
 				while( size <= maxWidth && k < max ) {
-					var cc = haxe.Utf8.charCodeAt(text, k++);
+					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);
@@ -165,7 +165,7 @@ class Text extends Drawable {
 				}
 				if( size > maxWidth ) {
 					newline = true;
-					lines.push(haxe.Utf8.sub(text,restPos, i - restPos));
+					lines.push(text.substr(restPos, i - restPos));
 					restPos = i;
 					if( font.charset.isSpace(cc) ) {
 						e = null;
@@ -181,15 +181,15 @@ class Text extends Drawable {
 			} else
 				prevChar = cc;
 		}
-		if( restPos < haxe.Utf8.length(text) ) {
+		if( restPos < text.length ) {
 			if( lines.length == 0 && leftMargin > 0 && x > maxWidth )
 				lines.push("");
-			lines.push(haxe.Utf8.sub(text, restPos, haxe.Utf8.length(text) - restPos));
+			lines.push(text.substr(restPos, text.length - restPos));
 		}
 		return lines.join("\n");
 	}
 
-	function initGlyphs( text : String, rebuild = true, lines : Array<Int> = null ) : Void {
+	function initGlyphs( text : hxd.UString, rebuild = true, lines : Array<Int> = null ) : Void {
 		if( rebuild ) glyphs.clear();
 		var x = 0, y = 0, xMax = 0, prevChar = -1;
 		var align = rebuild ? textAlign : Left;
@@ -207,8 +207,8 @@ class Text extends Drawable {
 		var dl = font.lineHeight + lineSpacing;
 		var calcLines = !rebuild && lines != null;
 		var yMin = 0;
-		for( i in 0...haxe.Utf8.length(text) ) {
-			var cc = haxe.Utf8.charCodeAt(text,i);
+		for( i in 0...text.length ) {
+			var cc = text.charCodeAt(i);
 			var e = font.getChar(cc);
 			var newline = cc == '\n'.code;
 			var offs = e.getKerningOffset(prevChar);
@@ -216,10 +216,10 @@ class Text extends Drawable {
 			// if the next word goes past the max width, change it into a newline
 			if( font.charset.isBreakChar(cc) && maxWidth != null ) {
 				var size = x + esize + letterSpacing;
-				var k = i + 1, max = haxe.Utf8.length(text);
+				var k = i + 1, max = text.length;
 				var prevChar = prevChar;
 				while( size <= maxWidth && k < max ) {
-					var cc = haxe.Utf8.charCodeAt(text, k++);
+					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);

+ 39 - 0
hxd/UString.hx

@@ -0,0 +1,39 @@
+package hxd;
+
+abstract UString(String) from String to String {
+
+	public var length(get,never) : Int;
+
+	inline function get_length() : Int {
+		#if (flash || js)
+		return this.length;
+		#else
+		return haxe.Utf8.length( this );
+		#end
+	}
+
+	public inline function charCodeAt( pos : Int ) : Int {
+		#if (flash || js)
+		return this.charCodeAt( pos );
+		#else
+		return haxe.Utf8.charCodeAt( this, pos );
+		#end
+	}
+
+	public inline function substr( pos : Int, ?len : Int ) : UString {
+		#if (flash  || js)
+		return this.substr( pos, len );
+		#else
+		return haxe.Utf8.sub( this, pos, len );
+		#end
+	}
+
+	public inline function charAt( pos : Int ) : UString {
+		#if (flash || js)
+		return this.charAt( pos );
+		#else
+		return haxe.Utf8.sub( this, pos, 1 );
+		#end
+	}
+
+}

+ 6 - 6
hxd/res/FontBuilder.hx

@@ -2,7 +2,7 @@ package hxd.res;
 
 typedef FontBuildOptions = {
 	?antiAliasing : Bool,
-	?chars : String,
+	?chars : hxd.UString,
 	?kerning : Bool,
 };
 
@@ -226,9 +226,9 @@ class FontBuilder {
 		var bh = 0;
 		var tmp = [];
 		var gcode = new Map<lime.text.Glyph,Int>();
-		for( i in 0...haxe.Utf8.length(options.chars) ) {
-			var c = haxe.Utf8.sub(options.chars, i,1);
-			var code = haxe.Utf8.charCodeAt(options.chars, i);
+		for( i in 0...options.chars.length ) {
+			var c = options.chars.charAt(i);
+			var code = options.chars.charCodeAt(i);
 			var g = f.getGlyph(c);
 			gcode.set(g, code);
 			var img = f.renderGlyph(g,font.size);
@@ -266,7 +266,7 @@ class FontBuilder {
 			all = [];
 			var x = 0, y = 0, lineH = font.lineHeight;
 			px = haxe.io.Bytes.alloc( width * height  );
-			for( i in 0...haxe.Utf8.length(options.chars) ) {
+			for( i in 0...options.chars.length ) {
 				var size = tmp[i];
 				var w = size.x + size.w + 1;
 				if( x + w > width ) {
@@ -288,7 +288,7 @@ class FontBuilder {
 				}
 				var t = new h2d.Tile(innerTex, x, y, gx+size.w, gy+size.h);
 				all.push(t);
-				font.glyphs.set(haxe.Utf8.charCodeAt(options.chars,i), new h2d.Font.FontChar(t,size.adv-1));
+				font.glyphs.set(options.chars.charCodeAt(i), new h2d.Font.FontChar(t,size.adv-1));
 				// next element
 				x += w + 1;
 			}