from gk_global import *
from gk_shape import *
from gk_fonts import *
from gk_elements import *
from gk_shapes_to_elements import *
from gk_transforms import *

import string

#----------------------------------------------------------------------------
# Text
#----------------------------------------------------------------------------
class gds_text_element(gds_layer_element):
	def __init__(self, layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
		gds_layer_element.__init__(self, layer, transformation)
		self.text =  text
		self.coordinate = coordinate
		self.h_alignment = alignment[0]
		self.v_alignment = alignment[1]
		self.font = font
		self.height = height

	def is_empty(self):
		return (len(string.strip(self.text)) == 0) or (self.height == 0)


#----------------------------------------------------------------------------
# Polygon Text
#----------------------------------------------------------------------------

class gds_polygon_text_element(gds_compound_element, gds_text_element):
	def __init__(self, layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
		gds_text_element.__init__(self,layer,  text, coordinate, alignment, font, height, transformation)
		gds_compound_element.__init__(self, [], transformation)

	def __calc__(self):
		# TO DO: include full transformation!!!
		#angle = self.transformation.rotation
		
		if isinstance(self.font, int):
			if self.font < 0 or self.font >= len(TEXT_FONTS):
				print >> stderr, "Warning: Font " + str(font) + " is not available."
				font = 0
			F = TEXT_FONTS[self.font]
		elif isinstance(self.font, gds_font):
			F = self.font
			
		text = self.text.upper()

		#determine start position
		letter_height = self.height
		letter_spacing = letter_height * F.spacing
		line_width =  letter_height * F.line_width
		letter_width = letter_height * (F.letter_width() + F.spacing)
		text_width = letter_width * len(text) - letter_spacing

		#ca = cos(angle*DEG2RAD)
		#sa = sin(angle*DEG2RAD)
		
		if self.h_alignment == TEXT_ALIGN_RIGHT:
			x0 = - text_width
		elif self.h_alignment == TEXT_ALIGN_CENTER:
			x0 = - text_width/2.0
		else:
			x0 = 0.0

		if self.v_alignment == TEXT_ALIGN_BOTTOM:
			y0 = 0.0
		elif self.v_alignment == TEXT_ALIGN_MIDDLE:
			y0 = - letter_height/2.0
		else:
			y0 = -letter_height

		#xpos = x0 * ca - y0 * sa
		#ypos = x0 * sa + y0 * ca
		xpos = x0
		ypos = y0

		
		xpos += self.coordinate[0]
		ypos += self.coordinate[1]
		for c in text:
			self += F.elements_character(self.layer, c, letter_height, (xpos, ypos), angle = 0.0)
			xpos += letter_width #* ca
			#ypos += letter_width * sa

	def __copy__(self):
		return gds_polygon_text_element(self.layer, self.text, self.coordinate, (self.h_alignment, self.v_alignment), self.font, self.height, self.transformation)

	def __deepcopy__(self, memo):
		return gds_polygon_text_element(self.layer, self.text, self.coordinate, (self.h_alignment, self.v_alignment), copy.deepcopy(self.font), self.height, copy.deepcopy(self.transformation))


def el_polygon_text (layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
	return gds_polygon_text_element(layer,  text, coordinate, alignment, font, height, transformation)
#----------------------------------------------------------------------------
# Label text
#----------------------------------------------------------------------------
class gds_label_element(gds_text_element):
	def __init__(self,layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
		gds_text_element.__init__(self, layer, text, coordinate, alignment, font, height, transformation)

	def	size_info(self):
		text_width = len(self.text) * self.height
		if self.h_alignment == TEXT_ALIGN_RIGHT:
			L = self.coordinate[0] - text_width
		elif self.h_alignment == TEXT_ALIGN_CENTER:
			L = self.coordinate[0] - text_width/2.0
		else:
			L = self.coordinate[0]
		R = L + text_width

		if self.v_alignment == TEXT_ALIGN_BOTTOM:
			B = self.coordinate[1]
		elif self.v_alignment == TEXT_ALIGN_MIDDLE:
			B = self.coordinate[1]- self.height/2.0
		else:
			B = self.coordinate[1] - self.height
		T = B + self.height
		return gk_shape_info.size_info_from_numpyarray((gds_shape([(L,B), (L,T), (R, B), (R,T)], True).transform(self.transformation)).__points__)

	def __copy__(self):
		return gds_label_element(self.layer, self.text, self.coordinate, (self.h_alignment, self.v_alignment), self.font, self.height, self.transformation)

	def __deepcopy__(self, memo):
		return gds_label_element(self.layer, self.text, self.coordinate, (self.h_alignment, self.v_alignment), copy.deepcopy(self.font), self.height, copy.deepcopy(self.transformation))

	def flat_copy(self, level = -1):
		return self.__copy__()
	

def el_label (layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
	return gds_label_element(layer,  text, coordinate, alignment, font, height, transformation)


#----------------------------------------------------------------------------
# EL-text function 
#----------------------------------------------------------------------------
def el_text (layer,  text, coordinate = (0.0,0.0), alignment = (TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP) , font = TEXT_FONT_DEFAULT, height = 20.0, transformation = None):
	if get_text_style() == Text_Style_Labels:
		ret = el_label (layer,  text, coordinate, alignment, font, height, transformation)
	elif get_text_style() == Text_Style_Polygons:
		ret = el_polygon_text (layer,  text, coordinate, alignment, font, height, transformation)
	return ret
