Browse Source

Text: disable linebreak

Tom Spira 4 years ago
parent
commit
9c41b6d5fa
3 changed files with 23 additions and 7 deletions
  1. 3 3
      h2d/HtmlText.hx
  2. 13 2
      h2d/Text.hx
  3. 7 2
      h2d/domkit/BaseComponents.hx

+ 3 - 3
h2d/HtmlText.hx

@@ -271,7 +271,7 @@ class HtmlText extends Text {
 			info.height = splitNode.height;
 			info.baseLine = splitNode.baseLine;
  			var char = fnt.getChar(cc);
-			if (fnt.charset.isSpace(cc)) {
+			if (lineBreak && fnt.charset.isSpace(cc)) {
 				// Space characters are converted to \n
 				w -= (splitNode.width + letterSpacing + char.width + char.getKerningOffset(splitNode.prevChar));
 				splitNode.node.nodeValue = str.substr(0, splitNode.pos) + "\n" + str.substr(splitNode.pos + 1);
@@ -404,7 +404,7 @@ class HtmlText extends Text {
 					var prevChar = prevChar;
 					while ( size <= maxWidth && k < max ) {
 						var cc = text.charCodeAt(k++);
-						if ( font.charset.isSpace(cc) || cc == '\n'.code ) break;
+						if ( lineBreak && (font.charset.isSpace(cc) || cc == '\n'.code ) ) break;
 						var e = font.getChar(cc);
 						size += e.width + letterSpacing + e.getKerningOffset(prevChar);
 						prevChar = cc;
@@ -412,7 +412,7 @@ class HtmlText extends Text {
 						if ( font.charset.isBreakChar(cc) && (nc == null || !font.charset.isComplementChar(nc)) ) break;
 					}
 					// Avoid empty line when last char causes line-break while being CJK
-					if ( size > maxWidth && i != max - 1 ) {
+					if ( lineBreak && size > maxWidth && i != max - 1 ) {
 						// Next word will reach maxWidth
 						newline = true;
 						if ( font.charset.isSpace(cc) ) {

+ 13 - 2
h2d/Text.hx

@@ -107,6 +107,10 @@ class Text extends Drawable {
 		Extra line spacing in pixels.
 	**/
 	public var lineSpacing(default,set) : Float = 0;
+	/**
+		Allow line break.
+	**/
+	public var lineBreak(default,set) : Bool = true;
 
 	var glyphs : TileGroup;
 	var needsRebuild : Bool;
@@ -186,6 +190,13 @@ class Text extends Drawable {
 		return s;
 	}
 
+	function set_lineBreak(b) {
+		if( lineBreak == b ) return b;
+		lineBreak = b;
+		rebuild();
+		return b;
+	}
+
 	override function constraintSize(width:Float, height:Float) {
 		constraintWidth = width;
 		updateConstraint();
@@ -330,7 +341,7 @@ class Text extends Drawable {
 				var breakFound = false;
 				while( size <= maxWidth && k < max ) {
 					var cc = text.charCodeAt(k++);
-					if( font.charset.isSpace(cc) || cc == '\n'.code ) {
+					if( lineBreak && (font.charset.isSpace(cc) || cc == '\n'.code ) ) {
 						breakFound = true;
 						break;
 					}
@@ -342,7 +353,7 @@ class Text extends Drawable {
 				}
 				if( size > maxWidth || (!breakFound && size + afterData > maxWidth) ) {
 					newline = true;
-					if( font.charset.isSpace(cc) ){
+					if( lineBreak && font.charset.isSpace(cc) ){
 						lines.push(text.substr(restPos, i - restPos));
 						e = null;
 					}else{

+ 7 - 2
h2d/domkit/BaseComponents.hx

@@ -367,7 +367,7 @@ class ObjectComp implements h2d.domkit.Object implements domkit.Component.Compon
 	@:p var offsetY : Int;
 	@:p(none) var minWidth : Null<Int>;
 	@:p(none) var minHeight : Null<Int>;
-	@:p var lineBreak : Bool;
+	@:p var forceLineBreak : Bool;
 
 	static function set_rotation(o:h2d.Object, v:Float) {
 		o.rotation = v * Math.PI / 180;
@@ -485,7 +485,7 @@ class ObjectComp implements h2d.domkit.Object implements domkit.Component.Compon
 		if( p != null ) p.minHeight = v;
 	}
 
-	static function set_lineBreak(o:h2d.Object,v) {
+	static function set_forceLineBreak(o:h2d.Object,v) {
 		var p = getFlowProps(o);
 		if( p != null ) p.lineBreak = v;
 	}
@@ -601,6 +601,7 @@ class TextComp extends DrawableComp implements domkit.Component.ComponentDecl<h2
 	@:p(font) var font : h2d.Font;
 	@:p var letterSpacing = 0;
 	@:p var lineSpacing = 0;
+	@:p var lineBreak : Bool;
 	@:p(none) var maxWidth : Null<Int>;
 	@:p var textAlign : h2d.Text.Align = Left;
 	@:p(textShadow) var textShadow : { dx : Float, dy : Float, color : Int, alpha : Float };
@@ -617,6 +618,10 @@ class TextComp extends DrawableComp implements domkit.Component.ComponentDecl<h2
 	static function set_textShadow( t : h2d.Text, v ) {
 		t.dropShadow = v;
 	}
+
+	static function set_lineBreak( t : h2d.Text, v ) {
+		t.lineBreak = v;
+	}
 }
 
 @:uiComp("html-text") @:domkitDecl