// Class: openfl._internal.text.TextEngine

var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this

$global.Object.defineProperty(exports, "__esModule", {value: true});

var __map_reserved = {};

// Imports

var $hxClasses = require("./../../../hxClasses_stub").default;
var $import = require("./../../../import_stub").default;
function EReg() {return require("./../../../EReg");}
function lime_text__$UTF8String_UTF8String_$Impl_$() {return require("./../../../lime/text/_UTF8String/UTF8String_Impl_");}
function Std() {return require("./../../../Std");}
function openfl_text_TextField() {return require("./../../../openfl/text/TextField");}
function openfl__$internal_text_TextLayoutGroup() {return require("./../../../openfl/_internal/text/TextLayoutGroup");}
function js_Browser() {return require("./../../../js/Browser");}
function haxe_ds_StringMap() {return require("./../../../haxe/ds/StringMap");}
function StringTools() {return require("./../../../StringTools");}
function openfl_text_Font() {return require("./../../../openfl/text/Font");}
function openfl_geom_Rectangle() {return require("./../../../openfl/geom/Rectangle");}
function openfl__$Vector_Vector_$Impl_$() {return require("./../../../openfl/_Vector/Vector_Impl_");}

// Constructor

var TextEngine = function(textField) {
	this.textField = textField;
	this.width = 100;
	this.height = 100;
	this.set_text("");
	this.bounds = new (openfl_geom_Rectangle().default)(0,0,0,0);
	this.type = "dynamic";
	this.autoSize = "none";
	this.embedFonts = false;
	this.selectable = true;
	this.borderColor = 0;
	this.border = false;
	this.backgroundColor = 16777215;
	this.background = false;
	this.gridFitType = "pixel";
	this.maxChars = 0;
	this.multiline = false;
	this.sharpness = 0;
	this.scrollH = 0;
	this.scrollV = 1;
	this.wordWrap = false;
	this.lineAscents = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.lineBreaks = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.lineDescents = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.lineLeadings = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.lineHeights = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.lineWidths = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.layoutGroups = (openfl__$Vector_Vector_$Impl_$().default)._new();
	this.textFormatRanges = (openfl__$Vector_Vector_$Impl_$().default)._new();
	TextEngine.__canvas = (js_Browser().default).get_document().createElement("canvas");
	TextEngine.__context = TextEngine.__canvas.getContext("2d");
}

// Meta

TextEngine.__name__ = ["openfl","_internal","text","TextEngine"];
TextEngine.prototype = {
	createRestrictRegexp: function(restrict) {
		var declinedRange = new (EReg().default)("\\^(.-.|.)","gu");
		var declined = "";
		var accepted = declinedRange.map(restrict,function(ereg) {
			declined += ereg.matched(1);
			return "";
		});
		var testRegexpParts = [];
		if(accepted.length > 0) {
			testRegexpParts.push("[^" + restrict + "]");
		}
		if(declined.length > 0) {
			testRegexpParts.push("[" + declined + "]");
		}
		return new (EReg().default)("(" + testRegexpParts.join("|") + ")","g");
	},
	getBounds: function() {
		var padding = this.border ? 1 : 0;
		this.bounds.width = this.width + padding;
		this.bounds.height = this.height + padding;
	},
	getLine: function(index) {
		if(index < 0 || index > this.lineBreaks.get_length() + 1) {
			return null;
		}
		if(this.lineBreaks.get_length() == 0) {
			return this.text;
		} else {
			return (lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(this.text,index > 0 ? this.lineBreaks.get(index - 1) : 0,this.lineBreaks.get(index));
		}
	},
	getLineBreakIndex: function(startIndex) {
		if(startIndex == null) {
			startIndex = 0;
		}
		var br = (lime_text__$UTF8String_UTF8String_$Impl_$().default).indexOf(this.text,"<br>",startIndex);
		var cr = (lime_text__$UTF8String_UTF8String_$Impl_$().default).indexOf(this.text,"\n",startIndex);
		var lf = (lime_text__$UTF8String_UTF8String_$Impl_$().default).indexOf(this.text,"\r",startIndex);
		if(cr == -1 && br == -1) {
			return lf;
		}
		if(lf == -1 && br == -1) {
			return cr;
		}
		if(lf == -1 && cr == -1) {
			return br;
		}
		if(cr == -1) {
			return (Std().default)["int"](Math.min(br,lf));
		}
		if(lf == -1) {
			return (Std().default)["int"](Math.min(br,cr));
		}
		if(br == -1) {
			return (Std().default)["int"](Math.min(cr,lf));
		}
		return (Std().default)["int"](Math.min(Math.min(cr,lf),br));
	},
	getLineMeasurements: function() {
		this.lineAscents.set_length(0);
		this.lineDescents.set_length(0);
		this.lineLeadings.set_length(0);
		this.lineHeights.set_length(0);
		this.lineWidths.set_length(0);
		var currentLineAscent = 0.0;
		var currentLineDescent = 0.0;
		var currentLineLeading = null;
		var currentLineHeight = 0.0;
		var currentLineWidth = 0.0;
		var currentTextHeight = 0.0;
		this.textWidth = 0;
		this.textHeight = 0;
		this.numLines = 1;
		this.bottomScrollV = 0;
		this.maxScrollH = 0;
		var _g = 0;
		var _g1 = this.layoutGroups;
		while(_g < _g1.get_length()) {
			var group = _g1.get(_g);
			++_g;
			while(group.lineIndex > this.numLines - 1) {
				this.lineAscents.push(currentLineAscent);
				this.lineDescents.push(currentLineDescent);
				this.lineLeadings.push(currentLineLeading != null ? currentLineLeading : 0);
				this.lineHeights.push(currentLineHeight);
				this.lineWidths.push(currentLineWidth);
				currentLineAscent = 0;
				currentLineDescent = 0;
				currentLineLeading = null;
				currentLineHeight = 0;
				currentLineWidth = 0;
				this.numLines++;
				if(this.textHeight <= this.height - 2) {
					this.bottomScrollV++;
				}
			}
			currentLineAscent = Math.max(currentLineAscent,group.ascent);
			currentLineDescent = Math.max(currentLineDescent,group.descent);
			if(currentLineLeading == null) {
				currentLineLeading = group.leading;
			} else {
				currentLineLeading = (Std().default)["int"](Math.max(currentLineLeading,group.leading));
			}
			currentLineHeight = Math.max(currentLineHeight,group.height);
			currentLineWidth = group.offsetX - 2 + group.width;
			if(currentLineWidth > this.textWidth) {
				this.textWidth = currentLineWidth;
			}
			currentTextHeight = group.offsetY - 2 + group.ascent + group.descent;
			if(currentTextHeight > this.textHeight) {
				this.textHeight = currentTextHeight;
			}
		}
		if(this.textHeight == 0 && this.textField != null && this.textField.get_type() == "input") {
			var currentFormat = this.textField.__textFormat;
			var ascent;
			var descent;
			var leading;
			var heightValue;
			if(currentFormat.__ascent != null) {
				ascent = currentFormat.size * currentFormat.__ascent;
				descent = currentFormat.size * currentFormat.__descent;
			} else {
				ascent = currentFormat.size;
				descent = currentFormat.size * 0.185;
			}
			leading = currentFormat.leading;
			heightValue = ascent + descent + leading;
			currentLineAscent = ascent;
			currentLineDescent = descent;
			currentLineLeading = leading;
			currentTextHeight = ascent + descent;
			this.textHeight = currentTextHeight;
		}
		this.lineAscents.push(currentLineAscent);
		this.lineDescents.push(currentLineDescent);
		this.lineLeadings.push(currentLineLeading != null ? currentLineLeading : 0);
		this.lineHeights.push(currentLineHeight);
		this.lineWidths.push(currentLineWidth);
		if(this.numLines == 1) {
			this.bottomScrollV = 1;
			if(currentLineLeading > 0) {
				this.textHeight += currentLineLeading;
			}
		} else if(this.textHeight <= this.height - 2) {
			this.bottomScrollV++;
		}
		if(this.autoSize != "none") {
			var _g2 = this.autoSize;
			switch(_g2) {
			case "center":case "left":case "right":
				if(!this.wordWrap) {
					this.width = this.textWidth + 4;
				}
				this.height = this.textHeight + 4;
				this.bottomScrollV = this.numLines;
				break;
			default:
			}
		}
		if(this.textWidth > this.width - 4) {
			this.maxScrollH = (Std().default)["int"](this.textWidth - this.width + 4);
		} else {
			this.maxScrollH = 0;
		}
		this.maxScrollV = this.numLines - this.bottomScrollV + 1;
		if(this.scrollV > this.maxScrollV) {
			this.scrollV = this.maxScrollV;
		}
		if(this.scrollH > this.maxScrollH) {
			this.scrollH = this.maxScrollH;
		}
	},
	getLayoutGroups: function() {
		var _gthis = this;
		this.layoutGroups.set_length(0);
		if(this.text == null || (lime_text__$UTF8String_UTF8String_$Impl_$().default).equals(this.text,"")) {
			return;
		}
		var rangeIndex = -1;
		var formatRange = null;
		var font = null;
		var currentFormat = (openfl_text_TextField().default).__defaultTextFormat.clone();
		var leading = 0;
		var ascent = 0.0;
		var maxAscent = 0.0;
		var descent = 0.0;
		var layoutGroup = null;
		var positions = null;
		var widthValue = 0.0;
		var heightValue = 0.0;
		var maxHeightValue = 0.0;
		var previousSpaceIndex = -2;
		var spaceIndex = (lime_text__$UTF8String_UTF8String_$Impl_$().default).indexOf(this.text," ");
		var breakIndex = this.getLineBreakIndex();
		var offsetX = 2.0;
		var offsetY = 2.0;
		var textIndex = 0;
		var lineIndex = 0;
		var lineFormat = null;
		if(rangeIndex < _gthis.textFormatRanges.get_length() - 1) {
			++rangeIndex;
			formatRange = _gthis.textFormatRanges.get(rangeIndex);
			currentFormat.__merge(formatRange.format);
			TextEngine.__context.font = TextEngine.getFont(currentFormat);
			if(currentFormat.__ascent != null) {
				ascent = currentFormat.size * currentFormat.__ascent;
				descent = currentFormat.size * currentFormat.__descent;
			} else {
				ascent = currentFormat.size;
				descent = currentFormat.size * 0.185;
			}
			leading = currentFormat.leading;
			heightValue = ascent + descent + leading;
		}
		if(heightValue > maxHeightValue) {
			maxHeightValue = heightValue;
		}
		if(ascent > maxAscent) {
			maxAscent = ascent;
		}
		lineFormat = formatRange.format;
		var wrap;
		var maxLoops = (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.text) + 1;
		while(textIndex < maxLoops) if(breakIndex > -1 && (spaceIndex == -1 || breakIndex < spaceIndex) && formatRange.end >= breakIndex) {
			if(textIndex <= breakIndex) {
				if(this.wordWrap && previousSpaceIndex <= textIndex && this.width >= 4) {
					var text = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(_gthis.text,textIndex,breakIndex);
					var tempWidth = TextEngine.__context.measureText(text).width;
					while(offsetX + tempWidth > _gthis.width - 2) {
						var i = 1;
						while(textIndex + i < breakIndex + 1) {
							var text1 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(_gthis.text,textIndex,i);
							tempWidth = TextEngine.__context.measureText(text1).width;
							if(offsetX + tempWidth > _gthis.width - 2) {
								--i;
								break;
							}
							++i;
						}
						if(i == 0 && tempWidth > _gthis.width - 4) {
							i = (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(_gthis.text);
						}
						if(i == 0) {
							offsetX = 2;
							offsetY += layoutGroup.height;
							++lineIndex;
							break;
						} else {
							var endIndex = textIndex + i;
							if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
								layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex);
								_gthis.layoutGroups.push(layoutGroup);
							} else {
								layoutGroup.format = formatRange.format;
								layoutGroup.startIndex = textIndex;
								layoutGroup.endIndex = endIndex;
							}
							var text2 = _gthis.text;
							var endIndex1 = textIndex + i;
							var positions1 = [];
							if(_gthis.__useIntAdvances == null) {
								_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
							}
							if(_gthis.__useIntAdvances) {
								var previousWidth = 0.0;
								var width;
								var _g1 = textIndex;
								var _g = endIndex1;
								while(_g1 < _g) {
									var i1 = _g1++;
									width = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text2,textIndex,i1 + 1)).width;
									positions1.push(width - previousWidth);
									previousWidth = width;
								}
							} else {
								var _g11 = textIndex;
								var _g2 = endIndex1;
								while(_g11 < _g2) {
									var i2 = _g11++;
									var advance;
									if(i2 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text2) - 1) {
										var nextWidth = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text2,i2 + 1)).width;
										var twoWidths = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text2,i2,2)).width;
										advance = twoWidths - nextWidth;
									} else {
										advance = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text2,i2)).width;
									}
									positions1.push(advance);
								}
							}
							layoutGroup.positions = positions1;
							layoutGroup.offsetX = offsetX;
							layoutGroup.ascent = ascent;
							layoutGroup.descent = descent;
							layoutGroup.leading = leading;
							layoutGroup.lineIndex = lineIndex;
							layoutGroup.offsetY = offsetY;
							var positions2 = layoutGroup.positions;
							var width1 = 0.0;
							var _g3 = 0;
							while(_g3 < positions2.length) {
								var position = positions2[_g3];
								++_g3;
								width1 += position;
							}
							layoutGroup.width = width1;
							layoutGroup.height = heightValue;
							layoutGroup = null;
							if(ascent > maxAscent) {
								maxAscent = ascent;
							}
							if(heightValue > maxHeightValue) {
								maxHeightValue = heightValue;
							}
							var _g4 = 0;
							var _g12 = _gthis.layoutGroups;
							while(_g4 < _g12.get_length()) {
								var lg = _g12.get(_g4);
								++_g4;
								if(lg.lineIndex < lineIndex) {
									continue;
								}
								if(lg.lineIndex > lineIndex) {
									break;
								}
								lg.ascent = maxAscent;
								lg.height = maxHeightValue;
							}
							offsetY += maxHeightValue;
							maxAscent = 0.0;
							maxHeightValue = 0.0;
							++lineIndex;
							offsetX = 2;
							textIndex += i;
							var text3 = _gthis.text;
							var positions3 = [];
							if(_gthis.__useIntAdvances == null) {
								_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
							}
							if(_gthis.__useIntAdvances) {
								var previousWidth1 = 0.0;
								var width2;
								var _g13 = textIndex;
								var _g5 = breakIndex;
								while(_g13 < _g5) {
									var i3 = _g13++;
									width2 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text3,textIndex,i3 + 1)).width;
									positions3.push(width2 - previousWidth1);
									previousWidth1 = width2;
								}
							} else {
								var _g14 = textIndex;
								var _g6 = breakIndex;
								while(_g14 < _g6) {
									var i4 = _g14++;
									var advance1;
									if(i4 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text3) - 1) {
										var nextWidth1 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text3,i4 + 1)).width;
										var twoWidths1 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text3,i4,2)).width;
										advance1 = twoWidths1 - nextWidth1;
									} else {
										advance1 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text3,i4)).width;
									}
									positions3.push(advance1);
								}
							}
							positions = positions3;
							var width3 = 0.0;
							var _g7 = 0;
							while(_g7 < positions.length) {
								var position1 = positions[_g7];
								++_g7;
								width3 += position1;
							}
							widthValue = width3;
							tempWidth = widthValue;
						}
					}
				}
				if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
					layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,breakIndex);
					_gthis.layoutGroups.push(layoutGroup);
				} else {
					layoutGroup.format = formatRange.format;
					layoutGroup.startIndex = textIndex;
					layoutGroup.endIndex = breakIndex;
				}
				var text4 = this.text;
				var positions4 = [];
				if(_gthis.__useIntAdvances == null) {
					_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
				}
				if(_gthis.__useIntAdvances) {
					var previousWidth2 = 0.0;
					var width4;
					var _g15 = textIndex;
					var _g8 = breakIndex;
					while(_g15 < _g8) {
						var i5 = _g15++;
						width4 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text4,textIndex,i5 + 1)).width;
						positions4.push(width4 - previousWidth2);
						previousWidth2 = width4;
					}
				} else {
					var _g16 = textIndex;
					var _g9 = breakIndex;
					while(_g16 < _g9) {
						var i6 = _g16++;
						var advance2;
						if(i6 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text4) - 1) {
							var nextWidth2 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text4,i6 + 1)).width;
							var twoWidths2 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text4,i6,2)).width;
							advance2 = twoWidths2 - nextWidth2;
						} else {
							advance2 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text4,i6)).width;
						}
						positions4.push(advance2);
					}
				}
				layoutGroup.positions = positions4;
				layoutGroup.offsetX = offsetX;
				layoutGroup.ascent = ascent;
				layoutGroup.descent = descent;
				layoutGroup.leading = leading;
				layoutGroup.lineIndex = lineIndex;
				layoutGroup.offsetY = offsetY;
				var positions5 = layoutGroup.positions;
				var width5 = 0.0;
				var _g10 = 0;
				while(_g10 < positions5.length) {
					var position2 = positions5[_g10];
					++_g10;
					width5 += position2;
				}
				layoutGroup.width = width5;
				layoutGroup.height = heightValue;
				layoutGroup = null;
			} else if(layoutGroup != null && layoutGroup.startIndex != layoutGroup.endIndex) {
				if(layoutGroup.endIndex == spaceIndex) {
					layoutGroup.width -= layoutGroup.getAdvance(layoutGroup.positions.length - 1);
				}
				layoutGroup = null;
			}
			if(formatRange.end == breakIndex) {
				if(rangeIndex < _gthis.textFormatRanges.get_length() - 1) {
					++rangeIndex;
					formatRange = _gthis.textFormatRanges.get(rangeIndex);
					currentFormat.__merge(formatRange.format);
					TextEngine.__context.font = TextEngine.getFont(currentFormat);
					if(currentFormat.__ascent != null) {
						ascent = currentFormat.size * currentFormat.__ascent;
						descent = currentFormat.size * currentFormat.__descent;
					} else {
						ascent = currentFormat.size;
						descent = currentFormat.size * 0.185;
					}
					leading = currentFormat.leading;
					heightValue = ascent + descent + leading;
				}
				if(heightValue > maxHeightValue) {
					maxHeightValue = heightValue;
				}
				if(ascent > maxAscent) {
					maxAscent = ascent;
				}
				lineFormat = formatRange.format;
			}
			if(breakIndex >= (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.text) - 1) {
				offsetY -= maxHeightValue;
			}
			if(ascent > maxAscent) {
				maxAscent = ascent;
			}
			if(heightValue > maxHeightValue) {
				maxHeightValue = heightValue;
			}
			var _g17 = 0;
			var _g18 = _gthis.layoutGroups;
			while(_g17 < _g18.get_length()) {
				var lg1 = _g18.get(_g17);
				++_g17;
				if(lg1.lineIndex < lineIndex) {
					continue;
				}
				if(lg1.lineIndex > lineIndex) {
					break;
				}
				lg1.ascent = maxAscent;
				lg1.height = maxHeightValue;
			}
			offsetY += maxHeightValue;
			maxAscent = 0.0;
			maxHeightValue = 0.0;
			++lineIndex;
			offsetX = 2;
			textIndex = breakIndex + 1;
			breakIndex = this.getLineBreakIndex(textIndex);
		} else if(formatRange.end >= spaceIndex && spaceIndex > -1 && textIndex < formatRange.end) {
			if(layoutGroup != null && layoutGroup.startIndex != layoutGroup.endIndex) {
				layoutGroup = null;
			}
			wrap = false;
			while(textIndex != formatRange.end) {
				var endIndex2 = -1;
				if(spaceIndex == -1) {
					endIndex2 = breakIndex;
				} else {
					endIndex2 = spaceIndex + 1;
					if(breakIndex > -1 && breakIndex < endIndex2) {
						endIndex2 = breakIndex;
					}
				}
				if(endIndex2 == -1 || endIndex2 > formatRange.end) {
					endIndex2 = formatRange.end;
				}
				var text5 = this.text;
				var positions6 = [];
				if(_gthis.__useIntAdvances == null) {
					_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
				}
				if(_gthis.__useIntAdvances) {
					var previousWidth3 = 0.0;
					var width6;
					var _g19 = textIndex;
					var _g20 = endIndex2;
					while(_g19 < _g20) {
						var i7 = _g19++;
						width6 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text5,textIndex,i7 + 1)).width;
						positions6.push(width6 - previousWidth3);
						previousWidth3 = width6;
					}
				} else {
					var _g110 = textIndex;
					var _g21 = endIndex2;
					while(_g110 < _g21) {
						var i8 = _g110++;
						var advance3;
						if(i8 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text5) - 1) {
							var nextWidth3 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text5,i8 + 1)).width;
							var twoWidths3 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text5,i8,2)).width;
							advance3 = twoWidths3 - nextWidth3;
						} else {
							advance3 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text5,i8)).width;
						}
						positions6.push(advance3);
					}
				}
				positions = positions6;
				var width7 = 0.0;
				var _g22 = 0;
				while(_g22 < positions.length) {
					var position3 = positions[_g22];
					++_g22;
					width7 += position3;
				}
				widthValue = width7;
				if(lineFormat.align == "justify") {
					if(positions.length > 0 && textIndex == previousSpaceIndex) {
						++textIndex;
						var spaceWidth = positions.shift();
						widthValue -= spaceWidth;
						offsetX += spaceWidth;
					}
					if(positions.length > 0 && endIndex2 == spaceIndex + 1) {
						--endIndex2;
						var spaceWidth1 = positions.pop();
						widthValue -= spaceWidth1;
					}
				}
				if(this.wordWrap) {
					if(offsetX + widthValue > this.width - 2) {
						wrap = true;
						if(positions.length > 0 && endIndex2 == spaceIndex + 1) {
							var lastPosition = positions[positions.length - 1];
							var spaceWidth2 = lastPosition;
							if(offsetX + widthValue - spaceWidth2 <= this.width - 2) {
								wrap = false;
							}
						}
					}
				}
				if(wrap) {
					if(lineFormat.align != "justify" && (layoutGroup != null || this.layoutGroups.get_length() > 0)) {
						var previous = layoutGroup;
						if(previous == null) {
							previous = this.layoutGroups.get(this.layoutGroups.get_length() - 1);
						}
						previous.width -= previous.getAdvance(previous.positions.length - 1);
						previous.endIndex--;
					}
					var i9 = this.layoutGroups.get_length() - 1;
					var offsetCount = 0;
					while(true) {
						layoutGroup = this.layoutGroups.get(i9);
						if(i9 > 0 && layoutGroup.startIndex > previousSpaceIndex) {
							++offsetCount;
						} else {
							break;
						}
						--i9;
					}
					if(textIndex == previousSpaceIndex + 1) {
						if(ascent > maxAscent) {
							maxAscent = ascent;
						}
						if(heightValue > maxHeightValue) {
							maxHeightValue = heightValue;
						}
						var _g23 = 0;
						var _g111 = _gthis.layoutGroups;
						while(_g23 < _g111.get_length()) {
							var lg2 = _g111.get(_g23);
							++_g23;
							if(lg2.lineIndex < lineIndex) {
								continue;
							}
							if(lg2.lineIndex > lineIndex) {
								break;
							}
							lg2.ascent = maxAscent;
							lg2.height = maxHeightValue;
						}
						offsetY += maxHeightValue;
						maxAscent = 0.0;
						maxHeightValue = 0.0;
						++lineIndex;
						offsetX = 2;
					}
					offsetX = 2;
					if(offsetCount > 0) {
						var bumpX = this.layoutGroups.get(this.layoutGroups.get_length() - offsetCount).offsetX;
						var _g112 = this.layoutGroups.get_length() - offsetCount;
						var _g24 = this.layoutGroups.get_length();
						while(_g112 < _g24) {
							var i10 = _g112++;
							layoutGroup = this.layoutGroups.get(i10);
							layoutGroup.offsetX -= bumpX;
							layoutGroup.offsetY = offsetY;
							layoutGroup.lineIndex = lineIndex;
							offsetX += layoutGroup.width;
						}
					}
					if(this.width >= 4) {
						var text6 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(_gthis.text,textIndex,endIndex2);
						var tempWidth1 = TextEngine.__context.measureText(text6).width;
						while(offsetX + tempWidth1 > _gthis.width - 2) {
							var i11 = 1;
							while(textIndex + i11 < endIndex2 + 1) {
								var text7 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(_gthis.text,textIndex,i11);
								tempWidth1 = TextEngine.__context.measureText(text7).width;
								if(offsetX + tempWidth1 > _gthis.width - 2) {
									--i11;
									break;
								}
								++i11;
							}
							if(i11 == 0 && tempWidth1 > _gthis.width - 4) {
								i11 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(_gthis.text);
							}
							if(i11 == 0) {
								offsetX = 2;
								offsetY += layoutGroup.height;
								++lineIndex;
								break;
							} else {
								var endIndex3 = textIndex + i11;
								if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
									layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex3);
									_gthis.layoutGroups.push(layoutGroup);
								} else {
									layoutGroup.format = formatRange.format;
									layoutGroup.startIndex = textIndex;
									layoutGroup.endIndex = endIndex3;
								}
								var text8 = _gthis.text;
								var endIndex4 = textIndex + i11;
								var positions7 = [];
								if(_gthis.__useIntAdvances == null) {
									_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
								}
								if(_gthis.__useIntAdvances) {
									var previousWidth4 = 0.0;
									var width8;
									var _g113 = textIndex;
									var _g25 = endIndex4;
									while(_g113 < _g25) {
										var i12 = _g113++;
										width8 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text8,textIndex,i12 + 1)).width;
										positions7.push(width8 - previousWidth4);
										previousWidth4 = width8;
									}
								} else {
									var _g114 = textIndex;
									var _g26 = endIndex4;
									while(_g114 < _g26) {
										var i13 = _g114++;
										var advance4;
										if(i13 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text8) - 1) {
											var nextWidth4 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text8,i13 + 1)).width;
											var twoWidths4 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text8,i13,2)).width;
											advance4 = twoWidths4 - nextWidth4;
										} else {
											advance4 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text8,i13)).width;
										}
										positions7.push(advance4);
									}
								}
								layoutGroup.positions = positions7;
								layoutGroup.offsetX = offsetX;
								layoutGroup.ascent = ascent;
								layoutGroup.descent = descent;
								layoutGroup.leading = leading;
								layoutGroup.lineIndex = lineIndex;
								layoutGroup.offsetY = offsetY;
								var positions8 = layoutGroup.positions;
								var width9 = 0.0;
								var _g27 = 0;
								while(_g27 < positions8.length) {
									var position4 = positions8[_g27];
									++_g27;
									width9 += position4;
								}
								layoutGroup.width = width9;
								layoutGroup.height = heightValue;
								layoutGroup = null;
								if(ascent > maxAscent) {
									maxAscent = ascent;
								}
								if(heightValue > maxHeightValue) {
									maxHeightValue = heightValue;
								}
								var _g28 = 0;
								var _g115 = _gthis.layoutGroups;
								while(_g28 < _g115.get_length()) {
									var lg3 = _g115.get(_g28);
									++_g28;
									if(lg3.lineIndex < lineIndex) {
										continue;
									}
									if(lg3.lineIndex > lineIndex) {
										break;
									}
									lg3.ascent = maxAscent;
									lg3.height = maxHeightValue;
								}
								offsetY += maxHeightValue;
								maxAscent = 0.0;
								maxHeightValue = 0.0;
								++lineIndex;
								offsetX = 2;
								textIndex += i11;
								var text9 = _gthis.text;
								var positions9 = [];
								if(_gthis.__useIntAdvances == null) {
									_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
								}
								if(_gthis.__useIntAdvances) {
									var previousWidth5 = 0.0;
									var width10;
									var _g116 = textIndex;
									var _g29 = endIndex2;
									while(_g116 < _g29) {
										var i14 = _g116++;
										width10 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text9,textIndex,i14 + 1)).width;
										positions9.push(width10 - previousWidth5);
										previousWidth5 = width10;
									}
								} else {
									var _g117 = textIndex;
									var _g30 = endIndex2;
									while(_g117 < _g30) {
										var i15 = _g117++;
										var advance5;
										if(i15 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text9) - 1) {
											var nextWidth5 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text9,i15 + 1)).width;
											var twoWidths5 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text9,i15,2)).width;
											advance5 = twoWidths5 - nextWidth5;
										} else {
											advance5 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text9,i15)).width;
										}
										positions9.push(advance5);
									}
								}
								positions = positions9;
								var width11 = 0.0;
								var _g31 = 0;
								while(_g31 < positions.length) {
									var position5 = positions[_g31];
									++_g31;
									width11 += position5;
								}
								widthValue = width11;
								tempWidth1 = widthValue;
							}
						}
					}
					if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
						layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex2);
						_gthis.layoutGroups.push(layoutGroup);
					} else {
						layoutGroup.format = formatRange.format;
						layoutGroup.startIndex = textIndex;
						layoutGroup.endIndex = endIndex2;
					}
					layoutGroup.positions = positions;
					layoutGroup.offsetX = offsetX;
					layoutGroup.ascent = ascent;
					layoutGroup.descent = descent;
					layoutGroup.leading = leading;
					layoutGroup.lineIndex = lineIndex;
					layoutGroup.offsetY = offsetY;
					layoutGroup.width = widthValue;
					layoutGroup.height = heightValue;
					offsetX += widthValue;
					textIndex = endIndex2;
					wrap = false;
				} else {
					if(layoutGroup != null && textIndex == spaceIndex) {
						if(lineFormat.align != "justify") {
							layoutGroup.endIndex = spaceIndex;
							layoutGroup.positions = layoutGroup.positions.concat(positions);
							layoutGroup.width += widthValue;
						}
					} else if(layoutGroup == null || lineFormat.align == "justify") {
						if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
							layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex2);
							_gthis.layoutGroups.push(layoutGroup);
						} else {
							layoutGroup.format = formatRange.format;
							layoutGroup.startIndex = textIndex;
							layoutGroup.endIndex = endIndex2;
						}
						layoutGroup.positions = positions;
						layoutGroup.offsetX = offsetX;
						layoutGroup.ascent = ascent;
						layoutGroup.descent = descent;
						layoutGroup.leading = leading;
						layoutGroup.lineIndex = lineIndex;
						layoutGroup.offsetY = offsetY;
						layoutGroup.width = widthValue;
						layoutGroup.height = heightValue;
					} else {
						layoutGroup.endIndex = endIndex2;
						layoutGroup.positions = layoutGroup.positions.concat(positions);
						layoutGroup.width += widthValue;
						if(breakIndex == endIndex2) {
							++endIndex2;
						}
					}
					offsetX += widthValue;
					textIndex = endIndex2;
				}
				var nextSpaceIndex = (lime_text__$UTF8String_UTF8String_$Impl_$().default).indexOf(this.text," ",textIndex);
				if(formatRange.end <= previousSpaceIndex) {
					layoutGroup = null;
					textIndex = formatRange.end;
					if(rangeIndex < _gthis.textFormatRanges.get_length() - 1) {
						++rangeIndex;
						formatRange = _gthis.textFormatRanges.get(rangeIndex);
						currentFormat.__merge(formatRange.format);
						TextEngine.__context.font = TextEngine.getFont(currentFormat);
						if(currentFormat.__ascent != null) {
							ascent = currentFormat.size * currentFormat.__ascent;
							descent = currentFormat.size * currentFormat.__descent;
						} else {
							ascent = currentFormat.size;
							descent = currentFormat.size * 0.185;
						}
						leading = currentFormat.leading;
						heightValue = ascent + descent + leading;
					}
					if(heightValue > maxHeightValue) {
						maxHeightValue = heightValue;
					}
					if(ascent > maxAscent) {
						maxAscent = ascent;
					}
				} else {
					if(breakIndex == previousSpaceIndex) {
						layoutGroup.endIndex = breakIndex;
						if(breakIndex - layoutGroup.startIndex - layoutGroup.positions.length < 0) {
							layoutGroup.positions.push(0.0);
						}
						textIndex = breakIndex + 1;
					}
					previousSpaceIndex = spaceIndex;
					spaceIndex = nextSpaceIndex;
				}
				if(breakIndex > -1 && breakIndex <= textIndex && (spaceIndex > breakIndex || spaceIndex == -1) || textIndex > (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.text) || spaceIndex > formatRange.end) {
					break;
				}
			}
		} else {
			if(textIndex > formatRange.end) {
				break;
			} else if(textIndex < formatRange.end || textIndex == (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.text)) {
				if(this.wordWrap && this.width >= 4) {
					var endIndex5 = formatRange.end;
					var text10 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(_gthis.text,textIndex,endIndex5);
					var tempWidth2 = TextEngine.__context.measureText(text10).width;
					while(offsetX + tempWidth2 > _gthis.width - 2) {
						var i16 = 1;
						while(textIndex + i16 < endIndex5 + 1) {
							var text11 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(_gthis.text,textIndex,i16);
							tempWidth2 = TextEngine.__context.measureText(text11).width;
							if(offsetX + tempWidth2 > _gthis.width - 2) {
								--i16;
								break;
							}
							++i16;
						}
						if(i16 == 0 && tempWidth2 > _gthis.width - 4) {
							i16 = (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(_gthis.text);
						}
						if(i16 == 0) {
							offsetX = 2;
							offsetY += layoutGroup.height;
							++lineIndex;
							break;
						} else {
							var endIndex6 = textIndex + i16;
							if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
								layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex6);
								_gthis.layoutGroups.push(layoutGroup);
							} else {
								layoutGroup.format = formatRange.format;
								layoutGroup.startIndex = textIndex;
								layoutGroup.endIndex = endIndex6;
							}
							var text12 = _gthis.text;
							var endIndex7 = textIndex + i16;
							var positions10 = [];
							if(_gthis.__useIntAdvances == null) {
								_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
							}
							if(_gthis.__useIntAdvances) {
								var previousWidth6 = 0.0;
								var width12;
								var _g118 = textIndex;
								var _g32 = endIndex7;
								while(_g118 < _g32) {
									var i17 = _g118++;
									width12 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text12,textIndex,i17 + 1)).width;
									positions10.push(width12 - previousWidth6);
									previousWidth6 = width12;
								}
							} else {
								var _g119 = textIndex;
								var _g33 = endIndex7;
								while(_g119 < _g33) {
									var i18 = _g119++;
									var advance6;
									if(i18 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text12) - 1) {
										var nextWidth6 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text12,i18 + 1)).width;
										var twoWidths6 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text12,i18,2)).width;
										advance6 = twoWidths6 - nextWidth6;
									} else {
										advance6 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text12,i18)).width;
									}
									positions10.push(advance6);
								}
							}
							layoutGroup.positions = positions10;
							layoutGroup.offsetX = offsetX;
							layoutGroup.ascent = ascent;
							layoutGroup.descent = descent;
							layoutGroup.leading = leading;
							layoutGroup.lineIndex = lineIndex;
							layoutGroup.offsetY = offsetY;
							var positions11 = layoutGroup.positions;
							var width13 = 0.0;
							var _g34 = 0;
							while(_g34 < positions11.length) {
								var position6 = positions11[_g34];
								++_g34;
								width13 += position6;
							}
							layoutGroup.width = width13;
							layoutGroup.height = heightValue;
							layoutGroup = null;
							if(ascent > maxAscent) {
								maxAscent = ascent;
							}
							if(heightValue > maxHeightValue) {
								maxHeightValue = heightValue;
							}
							var _g35 = 0;
							var _g120 = _gthis.layoutGroups;
							while(_g35 < _g120.get_length()) {
								var lg4 = _g120.get(_g35);
								++_g35;
								if(lg4.lineIndex < lineIndex) {
									continue;
								}
								if(lg4.lineIndex > lineIndex) {
									break;
								}
								lg4.ascent = maxAscent;
								lg4.height = maxHeightValue;
							}
							offsetY += maxHeightValue;
							maxAscent = 0.0;
							maxHeightValue = 0.0;
							++lineIndex;
							offsetX = 2;
							textIndex += i16;
							var text13 = _gthis.text;
							var positions12 = [];
							if(_gthis.__useIntAdvances == null) {
								_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
							}
							if(_gthis.__useIntAdvances) {
								var previousWidth7 = 0.0;
								var width14;
								var _g121 = textIndex;
								var _g36 = endIndex5;
								while(_g121 < _g36) {
									var i19 = _g121++;
									width14 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text13,textIndex,i19 + 1)).width;
									positions12.push(width14 - previousWidth7);
									previousWidth7 = width14;
								}
							} else {
								var _g122 = textIndex;
								var _g37 = endIndex5;
								while(_g122 < _g37) {
									var i20 = _g122++;
									var advance7;
									if(i20 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text13) - 1) {
										var nextWidth7 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text13,i20 + 1)).width;
										var twoWidths7 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text13,i20,2)).width;
										advance7 = twoWidths7 - nextWidth7;
									} else {
										advance7 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text13,i20)).width;
									}
									positions12.push(advance7);
								}
							}
							positions = positions12;
							var width15 = 0.0;
							var _g38 = 0;
							while(_g38 < positions.length) {
								var position7 = positions[_g38];
								++_g38;
								width15 += position7;
							}
							widthValue = width15;
							tempWidth2 = widthValue;
						}
					}
				}
				var text14 = this.text;
				var endIndex8 = formatRange.end;
				var positions13 = [];
				if(_gthis.__useIntAdvances == null) {
					_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
				}
				if(_gthis.__useIntAdvances) {
					var previousWidth8 = 0.0;
					var width16;
					var _g123 = textIndex;
					var _g39 = endIndex8;
					while(_g123 < _g39) {
						var i21 = _g123++;
						width16 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text14,textIndex,i21 + 1)).width;
						positions13.push(width16 - previousWidth8);
						previousWidth8 = width16;
					}
				} else {
					var _g124 = textIndex;
					var _g40 = endIndex8;
					while(_g124 < _g40) {
						var i22 = _g124++;
						var advance8;
						if(i22 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text14) - 1) {
							var nextWidth8 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text14,i22 + 1)).width;
							var twoWidths8 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text14,i22,2)).width;
							advance8 = twoWidths8 - nextWidth8;
						} else {
							advance8 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text14,i22)).width;
						}
						positions13.push(advance8);
					}
				}
				positions = positions13;
				var width17 = 0.0;
				var _g41 = 0;
				while(_g41 < positions.length) {
					var position8 = positions[_g41];
					++_g41;
					width17 += position8;
				}
				widthValue = width17;
				var endIndex9 = formatRange.end;
				if(layoutGroup == null || layoutGroup.startIndex != layoutGroup.endIndex) {
					layoutGroup = new (openfl__$internal_text_TextLayoutGroup().default)(formatRange.format,textIndex,endIndex9);
					_gthis.layoutGroups.push(layoutGroup);
				} else {
					layoutGroup.format = formatRange.format;
					layoutGroup.startIndex = textIndex;
					layoutGroup.endIndex = endIndex9;
				}
				var text15 = this.text;
				var endIndex10 = formatRange.end;
				var positions14 = [];
				if(_gthis.__useIntAdvances == null) {
					_gthis.__useIntAdvances = new (EReg().default)("Trident/7.0","").match((js_Browser().default).get_navigator().userAgent);
				}
				if(_gthis.__useIntAdvances) {
					var previousWidth9 = 0.0;
					var width18;
					var _g125 = textIndex;
					var _g42 = endIndex10;
					while(_g125 < _g42) {
						var i23 = _g125++;
						width18 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text15,textIndex,i23 + 1)).width;
						positions14.push(width18 - previousWidth9);
						previousWidth9 = width18;
					}
				} else {
					var _g126 = textIndex;
					var _g43 = endIndex10;
					while(_g126 < _g43) {
						var i24 = _g126++;
						var advance9;
						if(i24 < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(text15) - 1) {
							var nextWidth9 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text15,i24 + 1)).width;
							var twoWidths9 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(text15,i24,2)).width;
							advance9 = twoWidths9 - nextWidth9;
						} else {
							advance9 = TextEngine.__context.measureText((lime_text__$UTF8String_UTF8String_$Impl_$().default).charAt(text15,i24)).width;
						}
						positions14.push(advance9);
					}
				}
				layoutGroup.positions = positions14;
				layoutGroup.offsetX = offsetX;
				layoutGroup.ascent = ascent;
				layoutGroup.descent = descent;
				layoutGroup.leading = leading;
				layoutGroup.lineIndex = lineIndex;
				layoutGroup.offsetY = offsetY;
				var positions15 = layoutGroup.positions;
				var width19 = 0.0;
				var _g44 = 0;
				while(_g44 < positions15.length) {
					var position9 = positions15[_g44];
					++_g44;
					width19 += position9;
				}
				layoutGroup.width = width19;
				layoutGroup.height = heightValue;
				offsetX += widthValue;
				textIndex = formatRange.end;
			}
			if(rangeIndex < _gthis.textFormatRanges.get_length() - 1) {
				++rangeIndex;
				formatRange = _gthis.textFormatRanges.get(rangeIndex);
				currentFormat.__merge(formatRange.format);
				TextEngine.__context.font = TextEngine.getFont(currentFormat);
				if(currentFormat.__ascent != null) {
					ascent = currentFormat.size * currentFormat.__ascent;
					descent = currentFormat.size * currentFormat.__descent;
				} else {
					ascent = currentFormat.size;
					descent = currentFormat.size * 0.185;
				}
				leading = currentFormat.leading;
				heightValue = ascent + descent + leading;
			}
			if(heightValue > maxHeightValue) {
				maxHeightValue = heightValue;
			}
			if(ascent > maxAscent) {
				maxAscent = ascent;
			}
			if(textIndex == formatRange.end) {
				if(ascent > maxAscent) {
					maxAscent = ascent;
				}
				if(heightValue > maxHeightValue) {
					maxHeightValue = heightValue;
				}
				var _g45 = 0;
				var _g127 = _gthis.layoutGroups;
				while(_g45 < _g127.get_length()) {
					var lg5 = _g127.get(_g45);
					++_g45;
					if(lg5.lineIndex < lineIndex) {
						continue;
					}
					if(lg5.lineIndex > lineIndex) {
						break;
					}
					lg5.ascent = maxAscent;
					lg5.height = maxHeightValue;
				}
				offsetY += maxHeightValue;
				maxAscent = 0.0;
				maxHeightValue = 0.0;
				++lineIndex;
				offsetX = 2;
				++textIndex;
				break;
			}
		}
	},
	restrictText: function(value) {
		if(value == null) {
			return value;
		}
		if(this.__restrictRegexp != null) {
			value = this.__restrictRegexp.split(value).join("");
		}
		return value;
	},
	setTextAlignment: function() {
		var lineIndex = -1;
		var offsetX = 0.0;
		var totalWidth = this.width - 4;
		var group;
		var lineLength;
		var _g1 = 0;
		var _g = this.layoutGroups.get_length();
		while(_g1 < _g) {
			var i = _g1++;
			group = this.layoutGroups.get(i);
			if(group.lineIndex != lineIndex) {
				lineIndex = group.lineIndex;
				var _g2 = group.format.align;
				switch(_g2) {
				case "center":
					if(this.lineWidths.get(lineIndex) < totalWidth) {
						offsetX = Math.round((totalWidth - this.lineWidths.get(lineIndex)) / 2);
					} else {
						offsetX = 0;
					}
					break;
				case "justify":
					if(this.lineWidths.get(lineIndex) < totalWidth) {
						lineLength = 1;
						var _g3 = i + 1;
						var _g21 = this.layoutGroups.get_length();
						while(_g3 < _g21) {
							var j = _g3++;
							if(this.layoutGroups.get(j).lineIndex == lineIndex) {
								if(j == 0 || (lime_text__$UTF8String_UTF8String_$Impl_$().default).charCodeAt(this.text,this.layoutGroups.get(j).startIndex - 1) == 32) {
									++lineLength;
								}
							} else {
								break;
							}
						}
						if(lineLength > 1) {
							group = this.layoutGroups.get(i + lineLength - 1);
							var endChar = (lime_text__$UTF8String_UTF8String_$Impl_$().default).charCodeAt(this.text,group.endIndex);
							if(group.endIndex < (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.text) && endChar != 10 && endChar != 13) {
								offsetX = (totalWidth - this.lineWidths.get(lineIndex)) / (lineLength - 1);
								var j1 = 0;
								while(true) {
									if(j1 > 1 && (lime_text__$UTF8String_UTF8String_$Impl_$().default).charCodeAt(this.text,this.layoutGroups.get(j1).startIndex - 1) != 32) {
										this.layoutGroups.get(i + j1).offsetX += offsetX * (j1 - 1);
										++j1;
									}
									this.layoutGroups.get(i + j1).offsetX += offsetX * j1;
									if(!(++j1 < lineLength)) {
										break;
									}
								}
							}
						}
					}
					offsetX = 0;
					break;
				case "right":
					if(this.lineWidths.get(lineIndex) < totalWidth) {
						offsetX = Math.round(totalWidth - this.lineWidths.get(lineIndex));
					} else {
						offsetX = 0;
					}
					break;
				default:
					offsetX = 0;
				}
			}
			if(offsetX > 0) {
				group.offsetX += offsetX;
			}
		}
	},
	trimText: function(value) {
		if(value == null) {
			return value;
		}
		if(this.maxChars > 0 && (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(value) > this.maxChars) {
			value = (lime_text__$UTF8String_UTF8String_$Impl_$().default).substr(value,0,this.maxChars);
		}
		return value;
	},
	update: function() {
		if(this.text == null || this.textFormatRanges.get_length() == 0) {
			this.lineAscents.set_length(0);
			this.lineBreaks.set_length(0);
			this.lineDescents.set_length(0);
			this.lineLeadings.set_length(0);
			this.lineHeights.set_length(0);
			this.lineWidths.set_length(0);
			this.layoutGroups.set_length(0);
			this.textWidth = 0;
			this.textHeight = 0;
			this.numLines = 1;
			this.maxScrollH = 0;
			this.maxScrollV = 1;
			this.bottomScrollV = 1;
		} else {
			this.getLayoutGroups();
			this.getLineMeasurements();
			this.setTextAlignment();
		}
		this.getBounds();
	},
	set_restrict: function(value) {
		if((lime_text__$UTF8String_UTF8String_$Impl_$().default).equals(this.restrict,value)) {
			return this.restrict;
		}
		this.restrict = value;
		if(this.restrict == null || (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(this.restrict) == 0) {
			this.__restrictRegexp = null;
		} else {
			this.__restrictRegexp = this.createRestrictRegexp(value);
		}
		return this.restrict;
	},
	set_text: function(value) {
		return this.text = value;
	}
};
TextEngine.prototype.__class__ = $hxClasses["openfl._internal.text.TextEngine"] = TextEngine;

// Init



// Statics

TextEngine.findFont = function(name) {
	return null;
}
TextEngine.findFontVariant = function(format) {
	var fontName = format.font;
	var bold = format.bold;
	var italic = format.italic;
	var fontNamePrefix = (StringTools().default).replace((StringTools().default).replace(fontName," Normal","")," Regular","");
	if(bold && italic && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Bold Italic")) {
		return TextEngine.findFont(fontNamePrefix + " Bold Italic");
	} else if(bold && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Bold")) {
		return TextEngine.findFont(fontNamePrefix + " Bold");
	} else if(italic && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Italic")) {
		return TextEngine.findFont(fontNamePrefix + " Italic");
	}
	return TextEngine.findFont(fontName);
}
TextEngine.getFormatHeight = function(format) {
	var ascent;
	var descent;
	var leading;
	TextEngine.__context.font = TextEngine.getFont(format);
	if(format.__ascent != null) {
		ascent = format.size * format.__ascent;
		descent = format.size * format.__descent;
	} else {
		ascent = format.size;
		descent = format.size * 0.185;
	}
	leading = format.leading;
	return ascent + descent + leading;
}
TextEngine.getFont = function(format) {
	var fontName = format.font;
	var bold = format.bold;
	var italic = format.italic;
	if(fontName == null) {
		fontName = "_serif";
	}
	var fontNamePrefix = (StringTools().default).replace((StringTools().default).replace(fontName," Normal","")," Regular","");
	if(bold && italic && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Bold Italic")) {
		fontName = fontNamePrefix + " Bold Italic";
		bold = false;
		italic = false;
	} else if(bold && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Bold")) {
		fontName = fontNamePrefix + " Bold";
		bold = false;
	} else if(italic && (openfl_text_Font().default).__fontByName.exists(fontNamePrefix + " Italic")) {
		fontName = fontNamePrefix + " Italic";
		italic = false;
	} else {
		if(bold && (fontName.indexOf(" Bold ") > -1 || (StringTools().default).endsWith(fontName," Bold"))) {
			bold = false;
		}
		if(italic && (fontName.indexOf(" Italic ") > -1 || (StringTools().default).endsWith(fontName," Italic"))) {
			italic = false;
		}
	}
	var font = italic ? "italic " : "normal ";
	font += "normal ";
	font += bold ? "bold " : "normal ";
	font += format.size + "px";
	font += "/" + (format.leading + format.size + 3) + "px ";
	var font1;
	switch(fontName) {
	case "_sans":
		font1 = "sans-serif";
		break;
	case "_serif":
		font1 = "serif";
		break;
	case "_typewriter":
		font1 = "monospace";
		break;
	default:
		font1 = "'" + new (EReg().default)("^[\\s'\"]+(.*)[\\s'\"]+$","").replace(fontName,"$1") + "'";
	}
	font += "" + font1;
	return font;
}
TextEngine.getFontInstance = function(format) {
	return null;
}
TextEngine.UTF8_TAB = 9
TextEngine.UTF8_ENDLINE = 10
TextEngine.UTF8_SPACE = 32
TextEngine.UTF8_HYPHEN = 45
TextEngine.__defaultFonts = new (haxe_ds_StringMap().default)()

// Export

exports.default = TextEngine;