// Class: openfl._internal.renderer.canvas.CanvasTextField

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 openfl_geom_Rectangle() {return require("./../../../../openfl/geom/Rectangle");}
function lime_text__$UTF8String_UTF8String_$Impl_$() {return require("./../../../../lime/text/_UTF8String/UTF8String_Impl_");}
function js_Browser() {return require("./../../../../js/Browser");}
function Std() {return require("./../../../../Std");}
function openfl_geom_Matrix() {return require("./../../../../openfl/geom/Matrix");}
function StringTools() {return require("./../../../../StringTools");}
function EReg() {return require("./../../../../EReg");}
function openfl__$internal_text_TextEngine() {return require("./../../../../openfl/_internal/text/TextEngine");}
function openfl_filters_GlowFilter() {return require("./../../../../openfl/filters/GlowFilter");}
function openfl_display_BitmapData() {return require("./../../../../openfl/display/BitmapData");}

// Constructor

var CanvasTextField = function(){}

// Meta

CanvasTextField.__name__ = ["openfl","_internal","renderer","canvas","CanvasTextField"];
CanvasTextField.prototype = {
	
};
CanvasTextField.prototype.__class__ = $hxClasses["openfl._internal.renderer.canvas.CanvasTextField"] = CanvasTextField;

// Init



// Statics

CanvasTextField.render = function(textField,renderer,transform) {
	var textEngine = textField.__textEngine;
	var bounds = textEngine.bounds;
	var graphics = textField.__graphics;
	if(textField.__dirty) {
		textField.__updateLayout();
		if(graphics.__bounds == null) {
			graphics.__bounds = new (openfl_geom_Rectangle().default)();
		}
		graphics.__bounds.copyFrom(bounds);
	}
	graphics.__update(renderer.__worldTransform);
	if(textField.__dirty || graphics.__dirty) {
		var width = graphics.__width;
		var height = graphics.__height;
		if((textEngine.text == null || (lime_text__$UTF8String_UTF8String_$Impl_$().default).equals(textEngine.text,"")) && !textEngine.background && !textEngine.border && !textEngine.__hasFocus && (textEngine.type != "input" || !textEngine.selectable) || (textEngine.width <= 0 || textEngine.height <= 0) && textEngine.autoSize != "none") {
			textField.__graphics.__canvas = null;
			textField.__graphics.__context = null;
			textField.__graphics.__bitmap = null;
			textField.__graphics.set___dirty(false);
			textField.__dirty = false;
		} else {
			if(textField.__graphics.__canvas == null) {
				textField.__graphics.__canvas = (js_Browser().default).get_document().createElement("canvas");
				textField.__graphics.__context = textField.__graphics.__canvas.getContext("2d");
			}
			CanvasTextField.context = graphics.__context;
			var transform1 = graphics.__renderTransform;
			if(renderer.__isDOM) {
				var scale = renderer.pixelRatio;
				graphics.__canvas.width = (Std().default)["int"](width * scale);
				graphics.__canvas.height = (Std().default)["int"](height * scale);
				graphics.__canvas.style.width = width + "px";
				graphics.__canvas.style.height = height + "px";
				var matrix = (openfl_geom_Matrix().default).__pool.get();
				matrix.copyFrom(transform1);
				matrix.scale(scale,scale);
				renderer.setTransform(matrix,CanvasTextField.context);
				(openfl_geom_Matrix().default).__pool.release(matrix);
			} else {
				graphics.__canvas.width = width;
				graphics.__canvas.height = height;
				CanvasTextField.context.setTransform(transform1.a,transform1.b,transform1.c,transform1.d,transform1.tx,transform1.ty);
			}
			if(CanvasTextField.clearRect == null) {
				CanvasTextField.clearRect = (typeof navigator !== 'undefined' && typeof navigator['isCocoonJS'] !== 'undefined');
			}
			if(CanvasTextField.clearRect) {
				CanvasTextField.context.clearRect(0,0,graphics.__canvas.width,graphics.__canvas.height);
			}
			if(textEngine.text != null && textEngine.text != "" || textEngine.__hasFocus) {
				var text = textEngine.text;
				if(!renderer.__allowSmoothing || textEngine.antiAliasType == "advanced" && textEngine.sharpness == 400) {
					graphics.__context.mozImageSmoothingEnabled = false;
					graphics.__context.msImageSmoothingEnabled = false;
					graphics.__context.imageSmoothingEnabled = false;
				} else {
					graphics.__context.mozImageSmoothingEnabled = true;
					graphics.__context.msImageSmoothingEnabled = true;
					graphics.__context.imageSmoothingEnabled = true;
				}
				if(textEngine.border || textEngine.background) {
					CanvasTextField.context.rect(0.5,0.5,bounds.width - 1,bounds.height - 1);
					if(textEngine.background) {
						var tmp = (StringTools().default).hex(textEngine.backgroundColor & 16777215,6);
						CanvasTextField.context.fillStyle = "#" + tmp;
						CanvasTextField.context.fill();
					}
					if(textEngine.border) {
						CanvasTextField.context.lineWidth = 1;
						var tmp1 = (StringTools().default).hex(textEngine.borderColor & 16777215,6);
						CanvasTextField.context.strokeStyle = "#" + tmp1;
						CanvasTextField.context.stroke();
					}
				}
				CanvasTextField.context.textBaseline = "top";
				CanvasTextField.context.textAlign = "start";
				var scrollX = -textField.get_scrollH();
				var scrollY = 0.0;
				var _g1 = 0;
				var _g = textField.get_scrollV() - 1;
				while(_g1 < _g) {
					var i = _g1++;
					scrollY -= textEngine.lineHeights.get(i);
				}
				var advance;
				var offsetY = 0.0;
				var applyHack = new (EReg().default)("(iPad|iPhone|iPod|Firefox)","g").match((js_Browser().default).get_window().navigator.userAgent);
				var _g2 = 0;
				var _g11 = textEngine.layoutGroups;
				while(_g2 < _g11.get_length()) {
					var group = _g11.get(_g2);
					++_g2;
					if(group.lineIndex < textField.get_scrollV() - 1) {
						continue;
					}
					if(group.lineIndex > textField.get_scrollV() + textEngine.bottomScrollV - 2) {
						break;
					}
					if(group.format.underline) {
						CanvasTextField.context.beginPath();
						CanvasTextField.context.strokeStyle = "#000000";
						CanvasTextField.context.lineWidth = .5;
						var x = group.offsetX + scrollX;
						var y = group.offsetY + offsetY + scrollY + group.ascent;
						CanvasTextField.context.moveTo(x,y);
						CanvasTextField.context.lineTo(x + group.width,y);
						CanvasTextField.context.stroke();
					}
					CanvasTextField.context.font = (openfl__$internal_text_TextEngine().default).getFont(group.format);
					var tmp2 = (StringTools().default).hex(group.format.color & 16777215,6);
					CanvasTextField.context.fillStyle = "#" + tmp2;
					if(applyHack) {
						offsetY = group.format.size * 0.185;
					}
					if(textField.__filters != null) {
						if((Std().default)["is"](textField.__filters[0],(openfl_filters_GlowFilter().default))) {
							var glowFilter = textField.__filters[0];
							var cacheAlpha = CanvasTextField.context.globalAlpha;
							var tmp3 = glowFilter.get_alpha();
							CanvasTextField.context.globalAlpha = cacheAlpha * tmp3;
							var tmp4 = (StringTools().default).hex(glowFilter.get_color() & 16777215,6);
							CanvasTextField.context.strokeStyle = "#" + tmp4;
							var tmp5 = glowFilter.get_blurX();
							var tmp6 = glowFilter.get_blurY();
							CanvasTextField.context.lineWidth = Math.max(tmp5,tmp6);
							CanvasTextField.context.strokeText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text,group.startIndex,group.endIndex),group.offsetX + scrollX,group.offsetY + offsetY + scrollY);
							CanvasTextField.context.strokeStyle = null;
							CanvasTextField.context.globalAlpha = cacheAlpha;
						}
					}
					CanvasTextField.context.fillText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text,group.startIndex,group.endIndex),group.offsetX + scrollX,group.offsetY + offsetY + scrollY);
					if(textField.__caretIndex > -1 && textEngine.selectable) {
						if(textField.__selectionIndex == textField.__caretIndex) {
							if(textField.__showCursor && group.startIndex <= textField.__caretIndex && group.endIndex >= textField.__caretIndex) {
								advance = 0.0;
								var _g3 = 0;
								var _g21 = textField.__caretIndex - group.startIndex;
								while(_g3 < _g21) {
									var i1 = _g3++;
									if(group.positions.length <= i1) {
										break;
									}
									advance += group.getAdvance(i1);
								}
								var scrollY1 = 0.0;
								var _g31 = textField.get_scrollV();
								var _g22 = group.lineIndex + 1;
								while(_g31 < _g22) {
									var i2 = _g31++;
									scrollY1 += textEngine.lineHeights.get(i2 - 1);
								}
								CanvasTextField.context.beginPath();
								var tmp7 = (StringTools().default).hex(group.format.color & 16777215,6);
								CanvasTextField.context.strokeStyle = "#" + tmp7;
								CanvasTextField.context.moveTo(group.offsetX + advance - textField.get_scrollH(),scrollY1 + 2);
								CanvasTextField.context.lineWidth = 1;
								CanvasTextField.context.lineTo(group.offsetX + advance - textField.get_scrollH(),scrollY1 + (openfl__$internal_text_TextEngine().default).getFormatHeight(textField.get_defaultTextFormat()) - 1);
								CanvasTextField.context.stroke();
								CanvasTextField.context.closePath();
							}
						} else if(group.startIndex <= textField.__caretIndex && group.endIndex >= textField.__caretIndex || group.startIndex <= textField.__selectionIndex && group.endIndex >= textField.__selectionIndex || group.startIndex > textField.__caretIndex && group.endIndex < textField.__selectionIndex || group.startIndex > textField.__selectionIndex && group.endIndex < textField.__caretIndex) {
							var selectionStart = (Std().default)["int"](Math.min(textField.__selectionIndex,textField.__caretIndex));
							var selectionEnd = (Std().default)["int"](Math.max(textField.__selectionIndex,textField.__caretIndex));
							if(group.startIndex > selectionStart) {
								selectionStart = group.startIndex;
							}
							if(group.endIndex < selectionEnd) {
								selectionEnd = group.endIndex;
							}
							var start;
							var end;
							start = textField.getCharBoundaries(selectionStart);
							if(selectionEnd >= (lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(textEngine.text)) {
								end = textField.getCharBoundaries((lime_text__$UTF8String_UTF8String_$Impl_$().default).get_length(textEngine.text) - 1);
								end.x += end.width + 2;
							} else {
								end = textField.getCharBoundaries(selectionEnd);
							}
							if(start != null && end != null) {
								CanvasTextField.context.fillStyle = "#000000";
								CanvasTextField.context.fillRect(start.x + scrollX,start.y + scrollY,end.x - start.x,group.height);
								CanvasTextField.context.fillStyle = "#FFFFFF";
								CanvasTextField.context.fillText((lime_text__$UTF8String_UTF8String_$Impl_$().default).substring(text,selectionStart,selectionEnd),scrollX + start.x,group.offsetY + offsetY + scrollY);
							}
						}
					}
				}
			} else {
				if(textEngine.border || textEngine.background) {
					if(textEngine.border) {
						CanvasTextField.context.rect(0.5,0.5,bounds.width - 1,bounds.height - 1);
					} else {
						CanvasTextField.context.rect(0,0,bounds.width,bounds.height);
					}
					if(textEngine.background) {
						var tmp8 = (StringTools().default).hex(textEngine.backgroundColor & 16777215,6);
						CanvasTextField.context.fillStyle = "#" + tmp8;
						CanvasTextField.context.fill();
					}
					if(textEngine.border) {
						CanvasTextField.context.lineWidth = 1;
						CanvasTextField.context.lineCap = "square";
						var tmp9 = (StringTools().default).hex(textEngine.borderColor & 16777215,6);
						CanvasTextField.context.strokeStyle = "#" + tmp9;
						CanvasTextField.context.stroke();
					}
				}
				if(textField.__caretIndex > -1 && textEngine.selectable && textField.__showCursor) {
					var scrollX1 = -textField.get_scrollH();
					var scrollY2 = 0.0;
					var _g12 = 0;
					var _g4 = textField.get_scrollV() - 1;
					while(_g12 < _g4) {
						var i3 = _g12++;
						scrollY2 += textEngine.lineHeights.get(i3);
					}
					CanvasTextField.context.beginPath();
					var tmp10 = (StringTools().default).hex(textField.get_defaultTextFormat().color & 16777215,6);
					CanvasTextField.context.strokeStyle = "#" + tmp10;
					CanvasTextField.context.moveTo(scrollX1 + 2.5,scrollY2 + 2.5);
					CanvasTextField.context.lineWidth = 1;
					CanvasTextField.context.lineTo(scrollX1 + 2.5,scrollY2 + (openfl__$internal_text_TextEngine().default).getFormatHeight(textField.get_defaultTextFormat()) - 1);
					CanvasTextField.context.stroke();
					CanvasTextField.context.closePath();
				}
			}
			graphics.__bitmap = (openfl_display_BitmapData().default).fromCanvas(textField.__graphics.__canvas);
			graphics.__visible = true;
			textField.__dirty = false;
			graphics.set___dirty(false);
		}
	}
}


// Export

exports.default = CanvasTextField;