Browse Source

Added a vertical align support for <img> tag to HtmlText (#882)

Azrou 5 years ago
parent
commit
79fafdda03
1 changed files with 38 additions and 4 deletions
  1. 38 4
      h2d/HtmlText.hx

+ 38 - 4
h2d/HtmlText.hx

@@ -17,6 +17,12 @@ enum LineHeightMode {
 	Constant;
 }
 
+enum ImageVerticalAlign {
+	Top;
+	Bottom;
+	Middle;
+}
+
 class HtmlText extends Text {
 
 	/**
@@ -52,6 +58,11 @@ class HtmlText extends Text {
 	**/
 	public var lineHeightMode(default,set) : LineHeightMode = Accurate;
 
+	/**
+		Vertical alignement of the image related to the text
+	**/
+	public var imageVerticalAlign(default,set) : ImageVerticalAlign = Bottom;
+
 	var elements : Array<Object> = [];
 	var xPos : Float;
 	var yPos : Float;
@@ -295,9 +306,17 @@ class HtmlText extends Text {
 					info.width = size;
 					if ( lineHeightMode == Accurate ) {
 						var grow = i.height - i.dy - info.baseLine;
-						if ( grow > 0 ) {
-							info.baseLine += grow;
-							info.height += grow;
+						if(grow > 0) {
+							switch(imageVerticalAlign) {
+								case Top:
+									info.height += grow;
+								case Bottom:
+									info.baseLine += grow;
+									info.height += grow;
+								case Middle:
+									info.height += grow;
+									info.baseLine += Std.int(grow/2);
+							}
 						}
 						grow = info.baseLine + i.dy;
 						if ( info.height < grow ) info.height = grow;
@@ -630,7 +649,14 @@ class HtmlText extends Text {
 			case "img":
 				var i : Tile = loadImage(e.get("src"));
 				if ( i == null ) i = Tile.fromColor(0xFF00FF, 8, 8);
-				var py = yPos + metrics[sizePos].baseLine - i.height;
+				var py = yPos;
+				switch(imageVerticalAlign) {
+					case Bottom:
+						py += metrics[sizePos].baseLine - i.height;
+					case Middle:
+						py += metrics[sizePos].baseLine - i.height/2;
+					case Top:
+				}
 				if( py + i.dy < calcYMin )
 					calcYMin = py + i.dy;
 				if( rebuild ) {
@@ -725,6 +751,14 @@ class HtmlText extends Text {
 		return value;
 	}
 
+	function set_imageVerticalAlign(align) {
+		if ( this.imageVerticalAlign != align ) {
+			this.imageVerticalAlign = align;
+			rebuild();
+		}
+		return align;
+	}
+
 	function set_lineHeightMode(v) {
 		if ( this.lineHeightMode != v ) {
 			this.lineHeightMode = v;